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

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

Issue 12286004: Unbreak pub: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
« no previous file with comments | « tests/corelib/iterable_take_test.dart ('k') | tests/html/element_classes_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
15 void testOperations() { 10 void testOperations() {
16 // Comparison lists. 11 // Comparison lists.
17 List l = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 12 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]; 13 List r = const [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
19 // A base list that starts out like l. 14 // A base list that starts out like l.
20 List base = l.toList(); 15 List base = l.toList();
21 // A lazy reverse of base. 16 // A lazy reverse of base.
22 Iterable reversed = base.reversed; 17 List reversed = base.reversed;
23 18
24 Expect.listEquals(r, reversed.toList()); 19 Expect.listEquals(r, reversed);
25 Expect.listEquals(l, reversed.toList().reversed.toList()); 20 Expect.listEquals(l, reversed.reversed);
26 for (int i = 0; i < r.length; i++) { 21 for (int i = 0; i < r.length; i++) {
27 Expect.equals(r[i], reversed.elementAt(i)); 22 Expect.equals(r[i], reversed[i]);
28 } 23 }
29 Expect.equals(4, base.indexOf(5)); 24 Expect.equals(4, base.indexOf(5));
30 Expect.equals(5, reversed.toList().indexOf(5)); 25 Expect.equals(5, reversed.indexOf(5));
31 26
32 // Reversed followed by combinations of skip and take. 27 // Combinations of start and end relative to start/end of list.
33 List subr = [8, 7, 6, 5, 4, 3]; 28 List subr = [8, 7, 6, 5, 4, 3];
34 Expect.listEquals(subr, reversed.skip(2).take(6).toList()); 29 Expect.listEquals(subr, reversed.skip(2).take(6));
35 Expect.listEquals(subr, reversed.take(8).skip(2).toList()); 30 Expect.listEquals(subr, reversed.take(8).skip(2));
36 Expect.listEquals(subr, 31 Expect.listEquals(subr, reversed.reversed.skip(2).take(6).reversed);
37 reversed.toList().reversed.skip(2).take(6).toList().reversed.toList()); 32 Expect.listEquals(subr, reversed.reversed.take(8).skip(2).reversed);
38 Expect.listEquals(subr, 33 Expect.listEquals(subr, reversed.take(8).reversed.take(6).reversed);
39 reversed.toList().reversed.take(8).skip(2).toList().reversed.toList()); 34 Expect.listEquals(subr, reversed.reversed.take(8).reversed.take(6));
40 Expect.listEquals(subr, 35 Expect.listEquals(subr, reversed.reversed.skip(2).reversed.skip(2));
41 reversed.take(8).toList().reversed.take(6).toList().reversed.toList()); 36 Expect.listEquals(subr, reversed.skip(2).reversed.skip(2).reversed);
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 Expect.isFalse(i2.moveNext(),
64 "Too many actual values. Actual[$index] == ${i2.current}");
65 } else {
66 Expect.equals(v1, v2, path);
67 }
68 }
69
70 void testOp(operation(Iterable reversedList), name) {
71 List reversedList = new List(list.length);
72 for (int i = 0; i < list.length; i++) {
73 reversedList[i] = list[list.length - 1 - i];
74 }
75 Iterable reversed = list.reversed;
76 var expect;
77 try {
78 expect = operation(reversedList);
79 } catch (e) {
80 expect = throws;
81 }
82 var actual;
83 try {
84 actual = operation(reversed);
85 } catch (e) {
86 actual = throws;
87 }
88 testEquals(expect, actual, "$name: $list");
89 }
90 testOp((i) => i.first, "first");
91 testOp((i) => i.last, "last");
92 testOp((i) => i.single, "single");
93 testOp((i) => i.firstMatching((n) => n < 5), "firstMatching<5");
94 testOp((i) => i.firstMatching((n) => n < 10), "firstMatching<10");
95 testOp((i) => i.lastMatching((n) => n < 5), "lastMatching<5");
96 testOp((i) => i.lastMatching((n) => n < 10), "lastMatching<10");
97 testOp((i) => i.singleMatching((n) => n < 5), "singelMatching<5");
98 testOp((i) => i.singleMatching((n) => n < 10), "singelMatching<10");
99 testOp((i) => i.contains(5), "contains(5)");
100 testOp((i) => i.contains(10), "contains(10)");
101 testOp((i) => i.any((n) => n < 5), "any<5");
102 testOp((i) => i.any((n) => n < 10), "any<10");
103 testOp((i) => i.every((n) => n < 5), "every<5");
104 testOp((i) => i.every((n) => n < 10), "every<10");
105 testOp((i) => i.max(), "max");
106 testOp((i) => i.min(), "min");
107 testOp((i) => i.reduce(0, (a, b) => a + b), "reduce-sum");
108 testOp((i) => i.join("-"), "join-");
109 testOp((i) => i.join(""), "join");
110 testOp((i) => i.join(), "join-null");
111 testOp((i) => i.map((n) => n * 2), "map*2");
112 testOp((i) => i.where((n) => n < 5), "where<5");
113 testOp((i) => i.where((n) => n < 10), "where<10");
114 testOp((i) => i.expand((n) => []), "expand[]");
115 testOp((i) => i.expand((n) => [n]), "expand[n]");
116 testOp((i) => i.expand((n) => [n, n]), "expand[n, n]");
117 }
118
119 // Combinations of lists with 0, 1 and more elements.
120 testList([]);
121 testList([0]);
122 testList([10]);
123 testList([0, 1]);
124 testList([0, 10]);
125 testList([10, 11]);
126 testList([0, 5, 10]);
127 testList([10, 5, 0]);
128 testList([0, 1, 2, 3]);
129 testList([3, 4, 5, 6]);
130 testList([10, 11, 12, 13]);
131 37
132 // Reverse const list. 38 // Reverse const list.
133 Expect.listEquals(r, l.reversed.toList()); 39 Expect.listEquals(r, l.reversed);
134 } 40 }
OLDNEW
« no previous file with comments | « tests/corelib/iterable_take_test.dart ('k') | tests/html/element_classes_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698