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

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

Issue 2991363002: Migrated test block 14 to Dart 2.0. (Closed)
Patch Set: Migrated test block 14 to Dart 2.0. Created 3 years, 4 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
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 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 // Function that reverses l and r lists when used to map.
21 int rev(x) => 11 - x;
22 // A base list that starts out like l, but isn't const.
23 List base = l.map((x) => x).toList();
24
25 Iterable reversed = l.map(rev);
26
27 Expect.listEquals(r, l.map(rev).toList());
28 Expect.listEquals(l, l.map(rev).map(rev).toList());
29 for (int i = 0; i < r.length; i++) {
30 Expect.equals(r[i], reversed.elementAt(i));
31 }
32 Expect.equals(4, base.indexOf(5));
33 Expect.equals(5, reversed.toList().indexOf(5));
34
35 // Reversed followed by combinations of skip and take.
36 List subr = [8, 7, 6, 5, 4, 3];
37 Expect.listEquals(subr, reversed.skip(2).take(6).toList());
38 Expect.listEquals(subr, reversed.take(8).skip(2).toList());
39 Expect.listEquals(subr,
40 reversed.toList().reversed.skip(2).take(6).toList().reversed.toList());
41 Expect.listEquals(subr,
42 reversed.toList().reversed.take(8).skip(2).toList().reversed.toList());
43 Expect.listEquals(subr,
44 reversed.take(8).toList().reversed.take(6).toList().reversed.toList());
45 Expect.listEquals(subr,
46 reversed.toList().reversed.take(8).toList().reversed.take(6).toList());
47 Expect.listEquals(subr,
48 reversed.toList().reversed.skip(2).toList().reversed.skip(2).toList());
49 Expect.listEquals(subr,
50 reversed.skip(2).toList().reversed.skip(2).toList().reversed.toList());
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
73 .fail("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
96 testOp((i) => i.first, "first");
97 testOp((i) => i.last, "last");
98 testOp((i) => i.single, "single");
99 testOp((i) => i.firstWhere((n) => false), "firstWhere<false");
100 testOp((i) => i.firstWhere((n) => n < 10), "firstWhere<10");
101 testOp((i) => i.firstWhere((n) => n < 5), "firstWhere<5");
102 testOp((i) => i.firstWhere((n) => true), "firstWhere<true");
103 testOp((i) => i.lastWhere((n) => false), "lastWhere<false");
104 testOp((i) => i.lastWhere((n) => n < 5), "lastWhere<5");
105 testOp((i) => i.lastWhere((n) => n < 10), "lastWhere<10");
106 testOp((i) => i.lastWhere((n) => true), "lastWhere<true");
107 testOp((i) => i.singleWhere((n) => false), "singleWhere<false");
108 testOp((i) => i.singleWhere((n) => n < 5), "singelWhere<5");
109 testOp((i) => i.singleWhere((n) => n < 10), "singelWhere<10");
110 testOp((i) => i.singleWhere((n) => true), "singleWhere<true");
111 testOp((i) => i.contains(5), "contains(5)");
112 testOp((i) => i.contains(10), "contains(10)");
113 testOp((i) => i.any((n) => n < 5), "any<5");
114 testOp((i) => i.any((n) => n < 10), "any<10");
115 testOp((i) => i.every((n) => n < 5), "every<5");
116 testOp((i) => i.every((n) => n < 10), "every<10");
117 testOp((i) => i.reduce((a, b) => a + b), "reduce-sum");
118 testOp((i) => i.fold(0, (a, b) => a + b), "fold-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