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

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

Issue 12646005: Add Set.union, Set.difference. Change Set methods to expect Set. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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
« sdk/lib/core/set.dart ('K') | « sdk/lib/core/set.dart ('k') | no next file » | 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 library set_test; 5 library set_test;
6 6
7 import "dart:collection"; 7 import "dart:collection";
8 8
9 void testMain(Set create()) { 9 void testMain(Set create()) {
10 Set set = create(); 10 Set set = create();
11 Expect.equals(0, set.length); 11 Expect.equals(0, set.length);
12 set.add(1); 12 set.add(1);
13 Expect.equals(1, set.length); 13 Expect.equals(1, set.length);
14 Expect.equals(true, set.contains(1)); 14 Expect.isTrue(set.contains(1));
15 15
16 set.add(1); 16 set.add(1);
17 Expect.equals(1, set.length); 17 Expect.equals(1, set.length);
18 Expect.equals(true, set.contains(1)); 18 Expect.isTrue(set.contains(1));
19 19
20 set.remove(1); 20 set.remove(1);
21 Expect.equals(0, set.length); 21 Expect.equals(0, set.length);
22 Expect.equals(false, set.contains(1)); 22 Expect.isFalse(set.contains(1));
23 23
24 for (int i = 0; i < 10; i++) { 24 for (int i = 0; i < 10; i++) {
25 set.add(i); 25 set.add(i);
26 } 26 }
27 27
28 Expect.equals(10, set.length); 28 Expect.equals(10, set.length);
29 for (int i = 0; i < 10; i++) { 29 for (int i = 0; i < 10; i++) {
30 Expect.equals(true, set.contains(i)); 30 Expect.isTrue(set.contains(i));
31 } 31 }
32 32
33 Expect.equals(10, set.length); 33 Expect.equals(10, set.length);
34 34
35 for (int i = 10; i < 20; i++) { 35 for (int i = 10; i < 20; i++) {
36 Expect.equals(false, set.contains(i)); 36 Expect.isFalse(set.contains(i));
37 } 37 }
38 38
39 // Test Set.forEach. 39 // Test Set.forEach.
40 int sum = 0; 40 int sum = 0;
41 testForEach(int val) { 41 testForEach(int val) {
42 sum += (val + 1); 42 sum += (val + 1);
43 } 43 }
44 44
45 set.forEach(testForEach); 45 set.forEach(testForEach);
46 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum); 46 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum);
47 47
48 Expect.equals(true, set.isSubsetOf(set)); 48 Expect.isTrue(set.isSubsetOf(set));
49 Expect.equals(true, set.containsAll(set)); 49 Expect.isTrue(set.containsAll(set));
50 50
51 // Test Set.map. 51 // Test Set.map.
52 testMap(int val) { 52 testMap(int val) {
53 return val * val; 53 return val * val;
54 } 54 }
55 55
56 Set mapped = set.map(testMap).toSet(); 56 Set mapped = set.map(testMap).toSet();
57 Expect.equals(10, mapped.length); 57 Expect.equals(10, mapped.length);
58 58
59 Expect.equals(true, mapped.contains(0)); 59 Expect.isTrue(mapped.contains(0));
60 Expect.equals(true, mapped.contains(1)); 60 Expect.isTrue(mapped.contains(1));
61 Expect.equals(true, mapped.contains(4)); 61 Expect.isTrue(mapped.contains(4));
62 Expect.equals(true, mapped.contains(9)); 62 Expect.isTrue(mapped.contains(9));
63 Expect.equals(true, mapped.contains(16)); 63 Expect.isTrue(mapped.contains(16));
64 Expect.equals(true, mapped.contains(25)); 64 Expect.isTrue(mapped.contains(25));
65 Expect.equals(true, mapped.contains(36)); 65 Expect.isTrue(mapped.contains(36));
66 Expect.equals(true, mapped.contains(49)); 66 Expect.isTrue(mapped.contains(49));
67 Expect.equals(true, mapped.contains(64)); 67 Expect.isTrue(mapped.contains(64));
68 Expect.equals(true, mapped.contains(81)); 68 Expect.isTrue(mapped.contains(81));
69 69
70 sum = 0; 70 sum = 0;
71 set.forEach(testForEach); 71 set.forEach(testForEach);
72 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum); 72 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum);
73 73
74 sum = 0; 74 sum = 0;
75 75
76 mapped.forEach(testForEach); 76 mapped.forEach(testForEach);
77 Expect.equals(1 + 2 + 5 + 10 + 17 + 26 + 37 + 50 + 65 + 82, sum); 77 Expect.equals(1 + 2 + 5 + 10 + 17 + 26 + 37 + 50 + 65 + 82, sum);
78 78
79 // Test Set.filter. 79 // Test Set.filter.
80 testFilter(int val) { 80 testFilter(int val) {
81 return val.isEven; 81 return val.isEven;
82 } 82 }
83 83
84 Set filtered = set.where(testFilter).toSet(); 84 Set filtered = set.where(testFilter).toSet();
85 85
86 Expect.equals(5, filtered.length); 86 Expect.equals(5, filtered.length);
87 87
88 Expect.equals(true, filtered.contains(0)); 88 Expect.isTrue(filtered.contains(0));
89 Expect.equals(true, filtered.contains(2)); 89 Expect.isTrue(filtered.contains(2));
90 Expect.equals(true, filtered.contains(4)); 90 Expect.isTrue(filtered.contains(4));
91 Expect.equals(true, filtered.contains(6)); 91 Expect.isTrue(filtered.contains(6));
92 Expect.equals(true, filtered.contains(8)); 92 Expect.isTrue(filtered.contains(8));
93 93
94 sum = 0; 94 sum = 0;
95 filtered.forEach(testForEach); 95 filtered.forEach(testForEach);
96 Expect.equals(1 + 3 + 5 + 7 + 9, sum); 96 Expect.equals(1 + 3 + 5 + 7 + 9, sum);
97 97
98 Expect.equals(true, set.containsAll(filtered)); 98 Expect.isTrue(set.containsAll(filtered));
99 Expect.equals(true, filtered.isSubsetOf(set)); 99 Expect.isTrue(filtered.isSubsetOf(set));
100 100
101 // Test Set.every. 101 // Test Set.every.
102 testEvery(int val) { 102 testEvery(int val) {
103 return (val < 10); 103 return (val < 10);
104 } 104 }
105 105
106 Expect.equals(true, set.every(testEvery)); 106 Expect.isTrue(set.every(testEvery));
107 Expect.equals(true, filtered.every(testEvery)); 107 Expect.isTrue(filtered.every(testEvery));
108 108
109 filtered.add(10); 109 filtered.add(10);
110 Expect.equals(false, filtered.every(testEvery)); 110 Expect.isFalse(filtered.every(testEvery));
111 111
112 // Test Set.some. 112 // Test Set.some.
113 testSome(int val) { 113 testSome(int val) {
114 return (val == 4); 114 return (val == 4);
115 } 115 }
116 116
117 Expect.equals(true, set.any(testSome)); 117 Expect.isTrue(set.any(testSome));
118 Expect.equals(true, filtered.any(testSome)); 118 Expect.isTrue(filtered.any(testSome));
119 filtered.remove(4); 119 filtered.remove(4);
120 Expect.equals(false, filtered.any(testSome)); 120 Expect.isFalse(filtered.any(testSome));
121 121
122 // Test Set.intersection. 122 // Test Set.intersection.
123 Set intersection = set.intersection(filtered); 123 Set intersection = set.intersection(filtered);
124 Expect.equals(true, set.contains(0)); 124 Expect.isTrue(set.contains(0));
125 Expect.equals(true, set.contains(2)); 125 Expect.isTrue(set.contains(2));
126 Expect.equals(true, set.contains(6)); 126 Expect.isTrue(set.contains(6));
127 Expect.equals(true, set.contains(8)); 127 Expect.isTrue(set.contains(8));
128 Expect.equals(false, intersection.contains(1)); 128 Expect.isFalse(intersection.contains(1));
129 Expect.equals(false, intersection.contains(3)); 129 Expect.isFalse(intersection.contains(3));
130 Expect.equals(false, intersection.contains(4)); 130 Expect.isFalse(intersection.contains(4));
131 Expect.equals(false, intersection.contains(5)); 131 Expect.isFalse(intersection.contains(5));
132 Expect.equals(false, intersection.contains(7)); 132 Expect.isFalse(intersection.contains(7));
133 Expect.equals(false, intersection.contains(9)); 133 Expect.isFalse(intersection.contains(9));
134 Expect.equals(false, intersection.contains(10)); 134 Expect.isFalse(intersection.contains(10));
135 Expect.equals(4, intersection.length); 135 Expect.equals(4, intersection.length);
136 136
137 Expect.equals(true, set.containsAll(intersection)); 137 Expect.isTrue(set.containsAll(intersection));
138 Expect.equals(true, filtered.containsAll(intersection)); 138 Expect.isTrue(filtered.containsAll(intersection));
139 Expect.equals(true, intersection.isSubsetOf(set)); 139 Expect.isTrue(intersection.isSubsetOf(set));
140 Expect.equals(true, intersection.isSubsetOf(filtered)); 140 Expect.isTrue(intersection.isSubsetOf(filtered));
141
142 // Test Set.union.
143 Set twice = create()..addAll([0, 2, 4, 6, 8, 10, 12, 14]);
144 Set thrice = create()..addAll([0, 3, 6, 9, 12, 15]);
145 Set union = twice.union(thrice);
146 Expect.equals(11, union.length);
147 for (int i = 0; i < 16; i++) {
148 Expect.equals(i.isEven || (i % 3) == 0, union.contains(i));
149 }
150
151 // Test Set.difference.
152 Set difference = twice.difference(thrice);
153 Expect.equals(5, difference.length);
154 for (int i = 0; i < 16; i++) {
155 Expect.equals(i.isEven && (i % 3) != 0, difference.contains(i));
156 }
157 Expect.isTrue(twice.difference(thrice).difference(twice).isEmpty);
141 158
142 // Test Set.addAll. 159 // Test Set.addAll.
143 List list = new List.fixedLength(10); 160 List list = new List.fixedLength(10);
144 for (int i = 0; i < 10; i++) { 161 for (int i = 0; i < 10; i++) {
145 list[i] = i + 10; 162 list[i] = i + 10;
146 } 163 }
147 set.addAll(list); 164 set.addAll(list);
148 Expect.equals(20, set.length); 165 Expect.equals(20, set.length);
149 for (int i = 0; i < 20; i++) { 166 for (int i = 0; i < 20; i++) {
150 Expect.equals(true, set.contains(i)); 167 Expect.isTrue(set.contains(i));
151 } 168 }
152 169
153 // Test Set.removeAll 170 // Test Set.removeAll
154 set.removeAll(list); 171 set.removeAll(list);
155 Expect.equals(10, set.length); 172 Expect.equals(10, set.length);
156 for (int i = 0; i < 10; i++) { 173 for (int i = 0; i < 10; i++) {
157 Expect.equals(true, set.contains(i)); 174 Expect.isTrue(set.contains(i));
158 } 175 }
159 for (int i = 10; i < 20; i++) { 176 for (int i = 10; i < 20; i++) {
160 Expect.equals(false, set.contains(i)); 177 Expect.isFalse(set.contains(i));
161 } 178 }
162 179
163 // Test Set.clear. 180 // Test Set.clear.
164 set.clear(); 181 set.clear();
165 Expect.equals(0, set.length); 182 Expect.equals(0, set.length);
166 set.add(11); 183 set.add(11);
167 Expect.equals(1, set.length); 184 Expect.equals(1, set.length);
168 } 185 }
169 186
170 main() { 187 main() {
171 testMain(() => new Set()); 188 testMain(() => new Set());
172 testMain(() => new HashSet()); 189 testMain(() => new HashSet());
173 } 190 }
OLDNEW
« sdk/lib/core/set.dart ('K') | « sdk/lib/core/set.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698