Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: tests/corelib/list_map_test.dart

Issue 12295009: Recommit changing List.skip/take/revert returns Iterable and remove mappedBy. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make Future.wait simpler. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
6 main() {
7 testOperations();
8 }
9
10 class ThrowMarker {
11 const ThrowMarker();
12 String toString() => "<<THROWS>>";
13 }
14
15 void testOperations() {
16 // Comparison lists.
17 List l = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
18 List r = const [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
19 // Function that reverses l and r lists when used to map.
20 int rev(x) => 11 - x;
21 // A base list that starts out like l, but isn't const.
22 Iterable base = l.map((x) => x).toList();
23
24 Iterable reversed = l.map(rev);
25
26 Expect.listEquals(r, l.map(rev).toList());
27 Expect.listEquals(l, l.map(rev).map(rev).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 List mappedList = new List(list.length);
55 for (int i = 0; i < list.length; i++) {
56 mappedList[i] = rev(list[i]);
57 }
58 Iterable reversed = list.map(rev);
59
60 void testEquals(v1, v2, path) {
61 if (v1 is Iterable) {
62 Iterator i1 = v1.iterator;
63 Iterator i2 = v2.iterator;
64 int index = 0;
65 while (i1.moveNext()) {
66 Expect.isTrue(i2.moveNext(),
67 "Too few actual values. Expected[$index] == ${i1.current}");
68 testEquals(i1.current, i2.current, "$path[$index]");
69 index++;
70 }
71 if (i2.moveNext()) {
72 Expect.fail(
73 "Too many actual values. Actual[$index] == ${i2.current}");
74 }
75 } else {
76 Expect.equals(v1, v2, path);
77 }
78 }
79
80 void testOp(operation(Iterable mappedList), name) {
81 var expect;
82 try {
83 expect = operation(mappedList);
84 } catch (e) {
85 expect = throws;
86 }
87 var actual;
88 try {
89 actual = operation(reversed);
90 } catch (e) {
91 actual = throws;
92 }
93 testEquals(expect, actual, "$name: $list");
94 }
95 testOp((i) => i.first, "first");
96 testOp((i) => i.last, "last");
97 testOp((i) => i.single, "single");
98 testOp((i) => i.firstMatching((n) => false), "firstMatching<false");
99 testOp((i) => i.firstMatching((n) => n < 10), "firstMatching<10");
100 testOp((i) => i.firstMatching((n) => n < 5), "firstMatching<5");
101 testOp((i) => i.firstMatching((n) => true), "firstMatching<true");
102 testOp((i) => i.lastMatching((n) => false), "lastMatching<false");
103 testOp((i) => i.lastMatching((n) => n < 5), "lastMatching<5");
104 testOp((i) => i.lastMatching((n) => n < 10), "lastMatching<10");
105 testOp((i) => i.lastMatching((n) => true), "lastMatching<true");
106 testOp((i) => i.singleMatching((n) => false), "singleMatching<false");
107 testOp((i) => i.singleMatching((n) => n < 5), "singelMatching<5");
108 testOp((i) => i.singleMatching((n) => n < 10), "singelMatching<10");
109 testOp((i) => i.singleMatching((n) => true), "singleMatching<true");
110 testOp((i) => i.contains(5), "contains(5)");
111 testOp((i) => i.contains(10), "contains(10)");
112 testOp((i) => i.any((n) => n < 5), "any<5");
113 testOp((i) => i.any((n) => n < 10), "any<10");
114 testOp((i) => i.every((n) => n < 5), "every<5");
115 testOp((i) => i.every((n) => n < 10), "every<10");
116 testOp((i) => i.max(), "max");
117 testOp((i) => i.min(), "min");
118 testOp((i) => i.reduce(0, (a, b) => a + b), "reduce-sum");
119 testOp((i) => i.join("-"), "join-");
120 testOp((i) => i.join(""), "join");
121 testOp((i) => i.join(), "join-null");
122 testOp((i) => i.map((n) => n * 2), "map*2");
123 testOp((i) => i.where((n) => n < 5), "where<5");
124 testOp((i) => i.where((n) => n < 10), "where<10");
125 testOp((i) => i.expand((n) => []), "expand[]");
126 testOp((i) => i.expand((n) => [n]), "expand[n]");
127 testOp((i) => i.expand((n) => [n, n]), "expand[n, n]");
128 testOp((i) => i.take(0), "take(0)");
129 testOp((i) => i.take(5), "take(5)");
130 testOp((i) => i.take(10), "take(10)");
131 testOp((i) => i.take(15), "take(15)");
132 testOp((i) => i.skip(0), "skip(0)");
133 testOp((i) => i.skip(5), "skip(5)");
134 testOp((i) => i.skip(10), "skip(10)");
135 testOp((i) => i.skip(15), "skip(15)");
136 testOp((i) => i.takeWhile((n) => false), "takeWhile(t)");
137 testOp((i) => i.takeWhile((n) => n < 5), "takeWhile(n<5)");
138 testOp((i) => i.takeWhile((n) => n > 5), "takeWhile(n>5)");
139 testOp((i) => i.takeWhile((n) => true), "takeWhile(f)");
140 testOp((i) => i.skipWhile((n) => false), "skipWhile(t)");
141 testOp((i) => i.skipWhile((n) => n < 5), "skipWhile(n<5)");
142 testOp((i) => i.skipWhile((n) => n > 5), "skipWhile(n>5)");
143 testOp((i) => i.skipWhile((n) => true), "skipWhile(f)");
144 }
145
146 // Combinations of lists with 0, 1 and more elements.
147 testList([]);
148 testList([0]);
149 testList([10]);
150 testList([0, 1]);
151 testList([0, 10]);
152 testList([10, 11]);
153 testList([0, 5, 10]);
154 testList([10, 5, 0]);
155 testList([0, 1, 2, 3]);
156 testList([3, 4, 5, 6]);
157 testList([10, 11, 12, 13]);
158 testList(l);
159 testList(r);
160 testList(base);
161
162 // Reverse const list.
163 Expect.listEquals(r, l.map(rev).toList());
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698