OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 main() { | 6 main() { |
7 testOperations(); | 7 testOperations(); |
8 } | 8 } |
9 | 9 |
| 10 class ThrowMarker { |
| 11 const ThrowMarker(); |
| 12 String toString() => "<<THROWS>>"; |
| 13 } |
| 14 |
10 void testOperations() { | 15 void testOperations() { |
11 // Comparison lists. | 16 // Comparison lists. |
12 List l = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | 17 List l = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
13 List r = const [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; | 18 List r = const [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; |
14 // A base list that starts out like l. | 19 // A base list that starts out like l. |
15 List base = l.toList(); | 20 List base = l.toList(); |
16 // A lazy reverse of base. | 21 // A lazy reverse of base. |
17 List reversed = base.reversed; | 22 Iterable reversed = base.reversed; |
18 | 23 |
19 Expect.listEquals(r, reversed); | 24 Expect.listEquals(r, reversed.toList()); |
20 Expect.listEquals(l, reversed.reversed); | 25 Expect.listEquals(l, reversed.toList().reversed.toList()); |
21 for (int i = 0; i < r.length; i++) { | 26 for (int i = 0; i < r.length; i++) { |
22 Expect.equals(r[i], reversed[i]); | 27 Expect.equals(r[i], reversed.elementAt(i)); |
23 } | 28 } |
24 Expect.equals(4, base.indexOf(5)); | 29 Expect.equals(4, base.indexOf(5)); |
25 Expect.equals(5, reversed.indexOf(5)); | 30 Expect.equals(5, reversed.toList().indexOf(5)); |
26 | 31 |
27 // Combinations of start and end relative to start/end of list. | 32 // Reversed followed by combinations of skip and take. |
28 List subr = [8, 7, 6, 5, 4, 3]; | 33 List subr = [8, 7, 6, 5, 4, 3]; |
29 Expect.listEquals(subr, reversed.skip(2).take(6)); | 34 Expect.listEquals(subr, reversed.skip(2).take(6).toList()); |
30 Expect.listEquals(subr, reversed.take(8).skip(2)); | 35 Expect.listEquals(subr, reversed.take(8).skip(2).toList()); |
31 Expect.listEquals(subr, reversed.reversed.skip(2).take(6).reversed); | 36 Expect.listEquals(subr, |
32 Expect.listEquals(subr, reversed.reversed.take(8).skip(2).reversed); | 37 reversed.toList().reversed.skip(2).take(6).toList().reversed.toList()); |
33 Expect.listEquals(subr, reversed.take(8).reversed.take(6).reversed); | 38 Expect.listEquals(subr, |
34 Expect.listEquals(subr, reversed.reversed.take(8).reversed.take(6)); | 39 reversed.toList().reversed.take(8).skip(2).toList().reversed.toList()); |
35 Expect.listEquals(subr, reversed.reversed.skip(2).reversed.skip(2)); | 40 Expect.listEquals(subr, |
36 Expect.listEquals(subr, reversed.skip(2).reversed.skip(2).reversed); | 41 reversed.take(8).toList().reversed.take(6).toList().reversed.toList()); |
| 42 Expect.listEquals(subr, |
| 43 reversed.toList().reversed.take(8).toList().reversed.take(6).toList()); |
| 44 Expect.listEquals(subr, |
| 45 reversed.toList().reversed.skip(2).toList().reversed.skip(2).toList()); |
| 46 Expect.listEquals(subr, |
| 47 reversed.skip(2).toList().reversed.skip(2).toList().reversed.toList()); |
| 48 |
| 49 |
| 50 void testList(List list) { |
| 51 var throws = const ThrowMarker(); |
| 52 void testEquals(v1, v2, path) { |
| 53 if (v1 is Iterable) { |
| 54 Iterator i1 = v1.iterator; |
| 55 Iterator i2 = v2.iterator; |
| 56 int index = 0; |
| 57 while (i1.moveNext()) { |
| 58 Expect.isTrue(i2.moveNext(), |
| 59 "Too few actual values. Expected[$index] == ${i1.current}"); |
| 60 testEquals(i1.current, i2.current, "$path[$index]"); |
| 61 index++; |
| 62 } |
| 63 if (i2.moveNext()) { |
| 64 Expect.fail( |
| 65 "Too many actual values. Actual[$index] == ${i2.current}"); |
| 66 } |
| 67 } else { |
| 68 Expect.equals(v1, v2, path); |
| 69 } |
| 70 } |
| 71 |
| 72 void testOp(operation(Iterable reversedList), name) { |
| 73 List reversedList = new List(list.length); |
| 74 for (int i = 0; i < list.length; i++) { |
| 75 reversedList[i] = list[list.length - 1 - i]; |
| 76 } |
| 77 Iterable reversed = list.reversed; |
| 78 var expect; |
| 79 try { |
| 80 expect = operation(reversedList); |
| 81 } catch (e) { |
| 82 expect = throws; |
| 83 } |
| 84 var actual; |
| 85 try { |
| 86 actual = operation(reversed); |
| 87 } catch (e) { |
| 88 actual = throws; |
| 89 } |
| 90 testEquals(expect, actual, "$name: $list"); |
| 91 } |
| 92 testOp((i) => i.first, "first"); |
| 93 testOp((i) => i.last, "last"); |
| 94 testOp((i) => i.single, "single"); |
| 95 testOp((i) => i.firstMatching((n) => n < 5), "firstMatching<5"); |
| 96 testOp((i) => i.firstMatching((n) => n < 10), "firstMatching<10"); |
| 97 testOp((i) => i.lastMatching((n) => n < 5), "lastMatching<5"); |
| 98 testOp((i) => i.lastMatching((n) => n < 10), "lastMatching<10"); |
| 99 testOp((i) => i.singleMatching((n) => n < 5), "singelMatching<5"); |
| 100 testOp((i) => i.singleMatching((n) => n < 10), "singelMatching<10"); |
| 101 testOp((i) => i.contains(5), "contains(5)"); |
| 102 testOp((i) => i.contains(10), "contains(10)"); |
| 103 testOp((i) => i.any((n) => n < 5), "any<5"); |
| 104 testOp((i) => i.any((n) => n < 10), "any<10"); |
| 105 testOp((i) => i.every((n) => n < 5), "every<5"); |
| 106 testOp((i) => i.every((n) => n < 10), "every<10"); |
| 107 testOp((i) => i.max(), "max"); |
| 108 testOp((i) => i.min(), "min"); |
| 109 testOp((i) => i.reduce(0, (a, b) => a + b), "reduce-sum"); |
| 110 testOp((i) => i.join("-"), "join-"); |
| 111 testOp((i) => i.join(""), "join"); |
| 112 testOp((i) => i.join(), "join-null"); |
| 113 testOp((i) => i.map((n) => n * 2), "map*2"); |
| 114 testOp((i) => i.where((n) => n < 5), "where<5"); |
| 115 testOp((i) => i.where((n) => n < 10), "where<10"); |
| 116 testOp((i) => i.expand((n) => []), "expand[]"); |
| 117 testOp((i) => i.expand((n) => [n]), "expand[n]"); |
| 118 testOp((i) => i.expand((n) => [n, n]), "expand[n, n]"); |
| 119 } |
| 120 |
| 121 // Combinations of lists with 0, 1 and more elements. |
| 122 testList([]); |
| 123 testList([0]); |
| 124 testList([10]); |
| 125 testList([0, 1]); |
| 126 testList([0, 10]); |
| 127 testList([10, 11]); |
| 128 testList([0, 5, 10]); |
| 129 testList([10, 5, 0]); |
| 130 testList([0, 1, 2, 3]); |
| 131 testList([3, 4, 5, 6]); |
| 132 testList([10, 11, 12, 13]); |
37 | 133 |
38 // Reverse const list. | 134 // Reverse const list. |
39 Expect.listEquals(r, l.reversed); | 135 Expect.listEquals(r, l.reversed.toList()); |
40 } | 136 } |
OLD | NEW |