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