| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import "package:expect/expect.dart"; | |
| 6 | |
| 7 main() { | |
| 8 testOperations(); | |
| 9 } | |
| 10 | |
| 11 class ThrowMarker { | |
| 12 const ThrowMarker(); | |
| 13 String toString() => "<<THROWS>>"; | |
| 14 } | |
| 15 | |
| 16 void testOperations() { | |
| 17 // Comparison lists. | |
| 18 List l = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
| 19 List r = const [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; | |
| 20 // A base list that starts out like l. | |
| 21 List base = l.toList(); | |
| 22 // A lazy reverse of base. | |
| 23 Iterable reversed = base.reversed; | |
| 24 | |
| 25 Expect.listEquals(r, reversed.toList()); | |
| 26 Expect.listEquals(l, reversed.toList().reversed.toList()); | |
| 27 for (int i = 0; i < r.length; i++) { | |
| 28 Expect.equals(r[i], reversed.elementAt(i)); | |
| 29 } | |
| 30 Expect.equals(4, base.indexOf(5)); | |
| 31 Expect.equals(5, reversed.toList().indexOf(5)); | |
| 32 | |
| 33 // Reversed followed by combinations of skip and take. | |
| 34 List subr = [8, 7, 6, 5, 4, 3]; | |
| 35 Expect.listEquals(subr, reversed.skip(2).take(6).toList()); | |
| 36 Expect.listEquals(subr, reversed.take(8).skip(2).toList()); | |
| 37 Expect.listEquals(subr, | |
| 38 reversed.toList().reversed.skip(2).take(6).toList().reversed.toList()); | |
| 39 Expect.listEquals(subr, | |
| 40 reversed.toList().reversed.take(8).skip(2).toList().reversed.toList()); | |
| 41 Expect.listEquals(subr, | |
| 42 reversed.take(8).toList().reversed.take(6).toList().reversed.toList()); | |
| 43 Expect.listEquals(subr, | |
| 44 reversed.toList().reversed.take(8).toList().reversed.take(6).toList()); | |
| 45 Expect.listEquals(subr, | |
| 46 reversed.toList().reversed.skip(2).toList().reversed.skip(2).toList()); | |
| 47 Expect.listEquals(subr, | |
| 48 reversed.skip(2).toList().reversed.skip(2).toList().reversed.toList()); | |
| 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 | |
| 65 .fail("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 | |
| 93 testOp((i) => i.first, "first"); | |
| 94 testOp((i) => i.last, "last"); | |
| 95 testOp((i) => i.single, "single"); | |
| 96 testOp((i) => i.firstWhere((n) => n < 5), "firstWhere<5"); | |
| 97 testOp((i) => i.firstWhere((n) => n < 10), "firstWhere<10"); | |
| 98 testOp((i) => i.lastWhere((n) => n < 5), "lastWhere<5"); | |
| 99 testOp((i) => i.lastWhere((n) => n < 10), "lastWhere<10"); | |
| 100 testOp((i) => i.singleWhere((n) => n < 5), "singelWhere<5"); | |
| 101 testOp((i) => i.singleWhere((n) => n < 10), "singelWhere<10"); | |
| 102 testOp((i) => i.contains(5), "contains(5)"); | |
| 103 testOp((i) => i.contains(10), "contains(10)"); | |
| 104 testOp((i) => i.any((n) => n < 5), "any<5"); | |
| 105 testOp((i) => i.any((n) => n < 10), "any<10"); | |
| 106 testOp((i) => i.every((n) => n < 5), "every<5"); | |
| 107 testOp((i) => i.every((n) => n < 10), "every<10"); | |
| 108 testOp((i) => i.reduce((a, b) => a + b), "reduce-sum"); | |
| 109 testOp((i) => i.fold(0, (a, b) => a + b), "fold-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]); | |
| 133 | |
| 134 // Reverse const list. | |
| 135 Expect.listEquals(r, l.reversed.toList()); | |
| 136 } | |
| OLD | NEW |