-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathlist_reversed_test.dart
151 lines (139 loc) · 4.66 KB
/
list_reversed_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
testOperations();
}
class ThrowMarker {
const ThrowMarker();
String toString() => "<<THROWS>>";
}
void testOperations() {
// Comparison lists.
var l = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var r = const [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
// A base list that starts out like l.
var base = l.toList();
// A lazy reverse of base.
var reversed = base.reversed;
Expect.listEquals(r, reversed.toList());
Expect.listEquals(l, reversed.toList().reversed.toList());
for (int i = 0; i < r.length; i++) {
Expect.equals(r[i], reversed.elementAt(i));
}
Expect.equals(4, base.indexOf(5));
Expect.equals(5, reversed.toList().indexOf(5));
// Reversed followed by combinations of skip and take.
List subr = [8, 7, 6, 5, 4, 3];
Expect.listEquals(subr, reversed.skip(2).take(6).toList());
Expect.listEquals(subr, reversed.take(8).skip(2).toList());
Expect.listEquals(
subr,
reversed.toList().reversed.skip(2).take(6).toList().reversed.toList(),
);
Expect.listEquals(
subr,
reversed.toList().reversed.take(8).skip(2).toList().reversed.toList(),
);
Expect.listEquals(
subr,
reversed.take(8).toList().reversed.take(6).toList().reversed.toList(),
);
Expect.listEquals(
subr,
reversed.toList().reversed.take(8).toList().reversed.take(6).toList(),
);
Expect.listEquals(
subr,
reversed.toList().reversed.skip(2).toList().reversed.skip(2).toList(),
);
Expect.listEquals(
subr,
reversed.skip(2).toList().reversed.skip(2).toList().reversed.toList(),
);
void testList(List<int> list) {
var throws = const ThrowMarker();
void testEquals(v1, v2, path) {
if (v1 is Iterable) {
Iterator i1 = v1.iterator;
Iterator i2 = v2.iterator;
int index = 0;
while (i1.moveNext()) {
Expect.isTrue(
i2.moveNext(),
"Too few actual values. Expected[$index] == ${i1.current}",
);
testEquals(i1.current, i2.current, "$path[$index]");
index++;
}
if (i2.moveNext()) {
Expect.fail(
"Too many actual values. Actual[$index] == ${i2.current}",
);
}
} else {
Expect.equals(v1, v2, path);
}
}
void testOp(operation(Iterable<int> reversedList), name) {
var reversedList = [
for (var i = 0; i < list.length; i++) list[list.length - 1 - i],
];
var reversed = list.reversed;
var expect;
try {
expect = operation(reversedList);
} catch (e) {
expect = throws;
}
var actual;
try {
actual = operation(reversed);
} catch (e) {
actual = throws;
}
testEquals(expect, actual, "$name: $list");
}
testOp((i) => i.first, "first");
testOp((i) => i.last, "last");
testOp((i) => i.single, "single");
testOp((i) => i.firstWhere((n) => n < 5), "firstWhere<5");
testOp((i) => i.firstWhere((n) => n < 10), "firstWhere<10");
testOp((i) => i.lastWhere((n) => n < 5), "lastWhere<5");
testOp((i) => i.lastWhere((n) => n < 10), "lastWhere<10");
testOp((i) => i.singleWhere((n) => n < 5), "singelWhere<5");
testOp((i) => i.singleWhere((n) => n < 10), "singelWhere<10");
testOp((i) => i.contains(5), "contains(5)");
testOp((i) => i.contains(10), "contains(10)");
testOp((i) => i.any((n) => n < 5), "any<5");
testOp((i) => i.any((n) => n < 10), "any<10");
testOp((i) => i.every((n) => n < 5), "every<5");
testOp((i) => i.every((n) => n < 10), "every<10");
testOp((i) => i.reduce((a, b) => a + b), "reduce-sum");
testOp((i) => i.fold<int>(0, (a, b) => a + b), "fold-sum");
testOp((i) => i.join("-"), "join-");
testOp((i) => i.join(""), "join");
testOp((i) => i.join(), "join-null");
testOp((i) => i.map((n) => n * 2), "map*2");
testOp((i) => i.where((n) => n < 5), "where<5");
testOp((i) => i.where((n) => n < 10), "where<10");
testOp((i) => i.expand((n) => []), "expand[]");
testOp((i) => i.expand((n) => [n]), "expand[n]");
testOp((i) => i.expand((n) => [n, n]), "expand[n, n]");
}
// Combinations of lists with 0, 1 and more elements.
testList([]);
testList([0]);
testList([10]);
testList([0, 1]);
testList([0, 10]);
testList([10, 11]);
testList([0, 5, 10]);
testList([10, 5, 0]);
testList([0, 1, 2, 3]);
testList([3, 4, 5, 6]);
testList([10, 11, 12, 13]);
// Reverse const list.
Expect.listEquals(r, l.reversed.toList());
}