Index: tests/corelib/list_reversed_test.dart |
diff --git a/tests/corelib/list_reversed_test.dart b/tests/corelib/list_reversed_test.dart |
index 03b99ce4432d72224a96914557b47b79bdde41c0..1e12d527378627f35ab915d4469e735ca0af8b7d 100644 |
--- a/tests/corelib/list_reversed_test.dart |
+++ b/tests/corelib/list_reversed_test.dart |
@@ -7,6 +7,11 @@ main() { |
testOperations(); |
} |
+class ThrowMarker { |
+ const ThrowMarker(); |
+ String toString() => "<<THROWS>>"; |
+} |
+ |
void testOperations() { |
// Comparison lists. |
List l = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
@@ -14,27 +19,118 @@ void testOperations() { |
// A base list that starts out like l. |
List base = l.toList(); |
// A lazy reverse of base. |
- List reversed = base.reversed; |
+ Iterable reversed = base.reversed; |
- Expect.listEquals(r, reversed); |
- Expect.listEquals(l, reversed.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[i]); |
+ Expect.equals(r[i], reversed.elementAt(i)); |
} |
Expect.equals(4, base.indexOf(5)); |
- Expect.equals(5, reversed.indexOf(5)); |
+ Expect.equals(5, reversed.toList().indexOf(5)); |
- // Combinations of start and end relative to start/end of list. |
+ // Reversed followed by combinations of skip and take. |
List subr = [8, 7, 6, 5, 4, 3]; |
- Expect.listEquals(subr, reversed.skip(2).take(6)); |
- Expect.listEquals(subr, reversed.take(8).skip(2)); |
- Expect.listEquals(subr, reversed.reversed.skip(2).take(6).reversed); |
- Expect.listEquals(subr, reversed.reversed.take(8).skip(2).reversed); |
- Expect.listEquals(subr, reversed.take(8).reversed.take(6).reversed); |
- Expect.listEquals(subr, reversed.reversed.take(8).reversed.take(6)); |
- Expect.listEquals(subr, reversed.reversed.skip(2).reversed.skip(2)); |
- Expect.listEquals(subr, reversed.skip(2).reversed.skip(2).reversed); |
+ 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 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++; |
+ } |
+ Expect.isFalse(i2.moveNext(), |
+ "Too many actual values. Actual[$index] == ${i2.current}"); |
+ } else { |
+ Expect.equals(v1, v2, path); |
+ } |
+ } |
+ |
+ void testOp(operation(Iterable reversedList), name) { |
+ List reversedList = new List(list.length); |
+ for (int i = 0; i < list.length; i++) { |
+ reversedList[i] = list[list.length - 1 - i]; |
+ } |
+ Iterable reversed = list.reversed; |
+ var expect; |
+ try { |
+ expect = operation(reversedList); |
+ } catch (e) { |
+ print(e); |
+ expect = throws; |
+ } |
+ var actual; |
+ try { |
+ actual = operation(reversed); |
+ } catch (e) { |
+ print(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.firstMatching((n) => n < 5), "firstMatching<5"); |
+ testOp((i) => i.firstMatching((n) => n < 10), "firstMatching<10"); |
+ testOp((i) => i.lastMatching((n) => n < 5), "lastMatching<5"); |
+ testOp((i) => i.lastMatching((n) => n < 10), "lastMatching<10"); |
+ testOp((i) => i.singleMatching((n) => n < 5), "singelMatching<5"); |
+ testOp((i) => i.singleMatching((n) => n < 10), "singelMatching<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.max(), "max"); |
+ testOp((i) => i.min(), "min"); |
+ testOp((i) => i.reduce(0, (a, b) => a + b), "reduce-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); |
+ Expect.listEquals(r, l.reversed.toList()); |
} |