mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-02 06:46:22 +01:00
Check if number is in specific range
This commit is contained in:
parent
bf0e60c474
commit
3e7b9ffe36
2 changed files with 72 additions and 0 deletions
11
lib/num_extension.dart
Normal file
11
lib/num_extension.dart
Normal file
|
@ -0,0 +1,11 @@
|
|||
extension NumExtension on num {
|
||||
bool inRange(
|
||||
num beg,
|
||||
num end, {
|
||||
bool isBegInclusive = true,
|
||||
bool isEndInclusive = true,
|
||||
}) {
|
||||
return (this > beg || (isBegInclusive && this == beg)) &&
|
||||
(this < end || (isEndInclusive && this == end));
|
||||
}
|
||||
}
|
61
test/num_extension_test.dart
Normal file
61
test/num_extension_test.dart
Normal file
|
@ -0,0 +1,61 @@
|
|||
import 'package:nc_photos/num_extension.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group("NumExtension", () {
|
||||
group("inRange", () {
|
||||
test("[x, y)", () {
|
||||
expect(10.inRange(0, 20, isBegInclusive: true, isEndInclusive: false),
|
||||
true);
|
||||
expect(0.inRange(0, 20, isBegInclusive: true, isEndInclusive: false),
|
||||
true);
|
||||
expect((-1).inRange(0, 20, isBegInclusive: true, isEndInclusive: false),
|
||||
false);
|
||||
expect(20.inRange(0, 20, isBegInclusive: true, isEndInclusive: false),
|
||||
false);
|
||||
expect(21.inRange(0, 20, isBegInclusive: true, isEndInclusive: false),
|
||||
false);
|
||||
});
|
||||
|
||||
test("[x, y]", () {
|
||||
expect(10.inRange(0, 20, isBegInclusive: true, isEndInclusive: true),
|
||||
true);
|
||||
expect(
|
||||
0.inRange(0, 20, isBegInclusive: true, isEndInclusive: true), true);
|
||||
expect((-1).inRange(0, 20, isBegInclusive: true, isEndInclusive: true),
|
||||
false);
|
||||
expect(20.inRange(0, 20, isBegInclusive: true, isEndInclusive: true),
|
||||
true);
|
||||
expect(21.inRange(0, 20, isBegInclusive: true, isEndInclusive: true),
|
||||
false);
|
||||
});
|
||||
|
||||
test("(x, y)", () {
|
||||
expect(10.inRange(0, 20, isBegInclusive: false, isEndInclusive: false),
|
||||
true);
|
||||
expect(0.inRange(0, 20, isBegInclusive: false, isEndInclusive: false),
|
||||
false);
|
||||
expect(
|
||||
(-1).inRange(0, 20, isBegInclusive: false, isEndInclusive: false),
|
||||
false);
|
||||
expect(20.inRange(0, 20, isBegInclusive: false, isEndInclusive: false),
|
||||
false);
|
||||
expect(21.inRange(0, 20, isBegInclusive: false, isEndInclusive: false),
|
||||
false);
|
||||
});
|
||||
|
||||
test("(x, y]", () {
|
||||
expect(10.inRange(0, 20, isBegInclusive: false, isEndInclusive: true),
|
||||
true);
|
||||
expect(0.inRange(0, 20, isBegInclusive: false, isEndInclusive: true),
|
||||
false);
|
||||
expect((-1).inRange(0, 20, isBegInclusive: false, isEndInclusive: true),
|
||||
false);
|
||||
expect(20.inRange(0, 20, isBegInclusive: false, isEndInclusive: true),
|
||||
true);
|
||||
expect(21.inRange(0, 20, isBegInclusive: false, isEndInclusive: true),
|
||||
false);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue