forked from zulip/zulip-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms_test.dart
58 lines (51 loc) · 1.65 KB
/
algorithms_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import 'package:checks/checks.dart';
import 'package:test/scaffolding.dart';
import 'package:zulip/model/algorithms.dart';
void main() {
group('binarySearchByKey', () {
late List<({int data})> list;
int search(int key) => binarySearchByKey(list, key, (element, key) =>
element.data.compareTo(key));
test('empty', () {
list = [];
check(search(1)).equals(-1);
});
test('2 elements', () {
list = [(data: 2), (data: 4)];
check(search(1)).equals(-1);
check(search(2)).equals(0);
check(search(3)).equals(-1);
check(search(4)).equals(1);
check(search(5)).equals(-1);
});
test('3 elements', () {
list = [(data: 2), (data: 4), (data: 6)];
// Exercise the binary search before, at, and after each element of the list.
check(search(1)).equals(-1);
check(search(2)).equals(0);
check(search(3)).equals(-1);
check(search(4)).equals(1);
check(search(5)).equals(-1);
check(search(6)).equals(2);
check(search(7)).equals(-1);
});
});
group('setUnion', () {
for (final (String desc, Iterable<int> xs, Iterable<int> ys) in [
('empty', [], []),
('nonempty, empty', [1, 2], []),
('empty, nonempty', [], [1, 2]),
('in order', [1, 2], [3, 4]),
('reversed', [3, 4], [1, 2]),
('interleaved', [1, 3], [2, 4]),
('all dupes', [1, 2], [1, 2]),
('some dupes', [1, 2], [2, 3]),
('comparison is numeric, not lexicographic', [11], [2]),
]) {
test(desc, () {
final expected = Set.of(xs.followedBy(ys)).toList()..sort();
check(setUnion(xs, ys)).deepEquals(expected);
});
}
});
}