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

Side by Side Diff: tests/corelib_strong/hash_set_test.dart

Issue 2774783002: Re-land "Format all multitests" (Closed)
Patch Set: Created 3 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // VMOptions= 5 // VMOptions=
6 6
7 // Tests of hash set behavior, with focus in iteration and concurrent 7 // Tests of hash set behavior, with focus in iteration and concurrent
8 // modification errors. 8 // modification errors.
9 9
10 library hash_map2_test; 10 library hash_map2_test;
11
11 import "package:expect/expect.dart"; 12 import "package:expect/expect.dart";
12 import 'dart:collection'; 13 import 'dart:collection';
13 import 'dart:math' as math; 14 import 'dart:math' as math;
14 15
15 testSet(Set newSet(), Set newSetFrom(Iterable from)) { 16 testSet(Set newSet(), Set newSetFrom(Iterable from)) {
16
17 Set gen(int from, int to) => 17 Set gen(int from, int to) =>
18 new Set.from(new Iterable.generate(to - from, (n) => n + from)); 18 new Set.from(new Iterable.generate(to - from, (n) => n + from));
19 19
20 bool odd(int n) => (n & 1) == 1; 20 bool odd(int n) => (n & 1) == 1;
21 bool even(int n) => (n & 1) == 0; 21 bool even(int n) => (n & 1) == 0;
22 22
23 { // Test growing to largish capacity. 23 {
24 // Test growing to largish capacity.
24 Set set = newSet(); 25 Set set = newSet();
25 26
26 for (int i = 0; i < 256; i++) { 27 for (int i = 0; i < 256; i++) {
27 set.add(i); 28 set.add(i);
28 } 29 }
29 30
30 set.addAll(gen(256, 512)); 31 set.addAll(gen(256, 512));
31 set.addAll(newSetFrom(gen(512, 1000))); 32 set.addAll(newSetFrom(gen(512, 1000)));
32 Expect.equals(1000, set.length); 33 Expect.equals(1000, set.length);
33 34
34 // Remove half. 35 // Remove half.
35 for (int i = 0; i < 1000; i += 2) set.remove(i); 36 for (int i = 0; i < 1000; i += 2) set.remove(i);
36 Expect.equals(500, set.length); 37 Expect.equals(500, set.length);
37 Expect.isFalse(set.any(even)); 38 Expect.isFalse(set.any(even));
38 Expect.isTrue(set.every(odd)); 39 Expect.isTrue(set.every(odd));
39 40
40 // Re-add all. 41 // Re-add all.
41 set.addAll(gen(0, 1000)); 42 set.addAll(gen(0, 1000));
42 Expect.equals(1000, set.length); 43 Expect.equals(1000, set.length);
43 } 44 }
44 45
45 { // Test having many deleted elements. 46 {
47 // Test having many deleted elements.
46 Set set = newSet(); 48 Set set = newSet();
47 set.add(0); 49 set.add(0);
48 for (int i = 0; i < 1000; i++) { 50 for (int i = 0; i < 1000; i++) {
49 set.add(i + 1); 51 set.add(i + 1);
50 set.remove(i); 52 set.remove(i);
51 Expect.equals(1, set.length); 53 Expect.equals(1, set.length);
52 } 54 }
53 } 55 }
54 56
55 { // Test having many elements with same hashCode 57 {
58 // Test having many elements with same hashCode
56 Set set = newSet(); 59 Set set = newSet();
57 for (int i = 0; i < 1000; i++) { 60 for (int i = 0; i < 1000; i++) {
58 set.add(new BadHashCode()); 61 set.add(new BadHashCode());
59 } 62 }
60 Expect.equals(1000, set.length); 63 Expect.equals(1000, set.length);
61 } 64 }
62 65
63 { // Check concurrent modification 66 {
67 // Check concurrent modification
64 Set set = newSet()..add(0)..add(1); 68 Set set = newSet()..add(0)..add(1);
65 69
66 { // Test adding before a moveNext. 70 {
71 // Test adding before a moveNext.
67 Iterator iter = set.iterator; 72 Iterator iter = set.iterator;
68 iter.moveNext(); 73 iter.moveNext();
69 set.add(1); // Updating existing key isn't a modification. 74 set.add(1); // Updating existing key isn't a modification.
70 iter.moveNext(); 75 iter.moveNext();
71 set.add(2); 76 set.add(2);
72 Expect.throws(iter.moveNext, (e) => e is Error); 77 Expect.throws(iter.moveNext, (e) => e is Error);
73 } 78 }
74 79
75 { // Test adding after last element. 80 {
81 // Test adding after last element.
76 Iterator iter = set.iterator; 82 Iterator iter = set.iterator;
77 Expect.equals(3, set.length); 83 Expect.equals(3, set.length);
78 iter.moveNext(); 84 iter.moveNext();
79 iter.moveNext(); 85 iter.moveNext();
80 iter.moveNext(); 86 iter.moveNext();
81 set.add(3); 87 set.add(3);
82 Expect.throws(iter.moveNext, (e) => e is Error); 88 Expect.throws(iter.moveNext, (e) => e is Error);
83 } 89 }
84 90
85 { // Test removing during iteration. 91 {
92 // Test removing during iteration.
86 Iterator iter = set.iterator; 93 Iterator iter = set.iterator;
87 iter.moveNext(); 94 iter.moveNext();
88 set.remove(1000); // Not a modification if it's not there. 95 set.remove(1000); // Not a modification if it's not there.
89 iter.moveNext(); 96 iter.moveNext();
90 int n = iter.current; 97 int n = iter.current;
91 set.remove(n); 98 set.remove(n);
92 // Removing doesn't change current. 99 // Removing doesn't change current.
93 Expect.equals(n, iter.current); 100 Expect.equals(n, iter.current);
94 Expect.throws(iter.moveNext, (e) => e is Error); 101 Expect.throws(iter.moveNext, (e) => e is Error);
95 } 102 }
96 103
97 { // Test removing after last element. 104 {
105 // Test removing after last element.
98 Iterator iter = set.iterator; 106 Iterator iter = set.iterator;
99 Expect.equals(3, set.length); 107 Expect.equals(3, set.length);
100 iter.moveNext(); 108 iter.moveNext();
101 iter.moveNext(); 109 iter.moveNext();
102 iter.moveNext(); 110 iter.moveNext();
103 int n = iter.current; 111 int n = iter.current;
104 set.remove(n); 112 set.remove(n);
105 // Removing doesn't change current. 113 // Removing doesn't change current.
106 Expect.equals(n, iter.current); 114 Expect.equals(n, iter.current);
107 Expect.throws(iter.moveNext, (e) => e is Error); 115 Expect.throws(iter.moveNext, (e) => e is Error);
108 } 116 }
109 117
110 { // Test that updating value doesn't cause error. 118 {
119 // Test that updating value doesn't cause error.
111 Iterator iter = set.iterator; 120 Iterator iter = set.iterator;
112 Expect.equals(2, set.length); 121 Expect.equals(2, set.length);
113 iter.moveNext(); 122 iter.moveNext();
114 int n = iter.current; 123 int n = iter.current;
115 set.add(n); 124 set.add(n);
116 iter.moveNext(); 125 iter.moveNext();
117 Expect.isTrue(set.contains(iter.current)); 126 Expect.isTrue(set.contains(iter.current));
118 } 127 }
119 128
120 { // Check adding many existing values isn't considered modification. 129 {
130 // Check adding many existing values isn't considered modification.
121 Set set2 = newSet(); 131 Set set2 = newSet();
122 for (var value in set) { 132 for (var value in set) {
123 set2.add(value); 133 set2.add(value);
124 } 134 }
125 Iterator iter = set.iterator; 135 Iterator iter = set.iterator;
126 set.addAll(set2); 136 set.addAll(set2);
127 // Shouldn't throw. 137 // Shouldn't throw.
128 iter.moveNext(); 138 iter.moveNext();
129 } 139 }
130 } 140 }
131 141
132 { // Check that updating existing elements is not a modification. 142 {
133 // This must be the case even if the underlying data structure is 143 // Check that updating existing elements is not a modification.
134 // nearly full. 144 // This must be the case even if the underlying data structure is
135 for (int i = 1; i < 128; i++) { 145 // nearly full.
136 // Create maps of different sizes, some of which should be 146 for (int i = 1; i < 128; i++) {
137 // at a limit of the underlying data structure. 147 // Create maps of different sizes, some of which should be
138 Set set = newSetFrom(gen(0, i)); 148 // at a limit of the underlying data structure.
139 Iterator iter = set.iterator; 149 Set set = newSetFrom(gen(0, i));
140 for (int j = 0; j < i; j++) { 150 Iterator iter = set.iterator;
141 set.add(j); 151 for (int j = 0; j < i; j++) {
142 } 152 set.add(j);
143 iter.moveNext(); // Should not throw. 153 }
154 iter.moveNext(); // Should not throw.
144 155
145 for (int j = 1; j < i; j++) { 156 for (int j = 1; j < i; j++) {
146 set.remove(j); 157 set.remove(j);
147 } 158 }
148 iter = set.iterator; 159 iter = set.iterator;
149 set.add(0); 160 set.add(0);
150 iter.moveNext(); // Should not throw. 161 iter.moveNext(); // Should not throw.
151 } 162 }
152 } 163 }
153 164
154 { // Check that null can be in the set. 165 {
166 // Check that null can be in the set.
155 Set set = newSet(); 167 Set set = newSet();
156 set.add(null); 168 set.add(null);
157 Expect.equals(1, set.length); 169 Expect.equals(1, set.length);
158 Expect.isTrue(set.contains(null)); 170 Expect.isTrue(set.contains(null));
159 Expect.isNull(set.first); 171 Expect.isNull(set.first);
160 Expect.isNull(set.last); 172 Expect.isNull(set.last);
161 set.add(null); 173 set.add(null);
162 Expect.equals(1, set.length); 174 Expect.equals(1, set.length);
163 Expect.isTrue(set.contains(null)); 175 Expect.isTrue(set.contains(null));
164 set.remove(null); 176 set.remove(null);
(...skipping 22 matching lines...) Expand all
187 Expect.equals(128, set.length); 199 Expect.equals(128, set.length);
188 Expect.isTrue(set.contains(null)); 200 Expect.isTrue(set.contains(null));
189 set.add(null); 201 set.add(null);
190 Expect.equals(128, set.length); 202 Expect.equals(128, set.length);
191 Expect.isTrue(set.contains(null)); 203 Expect.isTrue(set.contains(null));
192 set.remove(null); 204 set.remove(null);
193 Expect.equals(127, set.length); 205 Expect.equals(127, set.length);
194 Expect.isFalse(set.contains(null)); 206 Expect.isFalse(set.contains(null));
195 } 207 }
196 208
197 { // Check that addAll and clear works. 209 {
210 // Check that addAll and clear works.
198 Set set = newSet(); 211 Set set = newSet();
199 set.addAll([]); 212 set.addAll([]);
200 Expect.isTrue(set.isEmpty); 213 Expect.isTrue(set.isEmpty);
201 set.addAll([1, 3, 2]); 214 set.addAll([1, 3, 2]);
202 Expect.equals(3, set.length); 215 Expect.equals(3, set.length);
203 Expect.isTrue(set.contains(1)); 216 Expect.isTrue(set.contains(1));
204 Expect.isTrue(set.contains(3)); 217 Expect.isTrue(set.contains(3));
205 Expect.isTrue(set.contains(2)); 218 Expect.isTrue(set.contains(2));
206 Expect.isFalse(set.contains(4)); 219 Expect.isFalse(set.contains(4));
207 set.clear(); 220 set.clear();
208 Expect.isTrue(set.isEmpty); 221 Expect.isTrue(set.isEmpty);
209 } 222 }
210 223
211 { // Check that removeWhere and retainWhere work. 224 {
225 // Check that removeWhere and retainWhere work.
212 Set set = newSetFrom([1, 2, 3]); 226 Set set = newSetFrom([1, 2, 3]);
213 set.removeWhere((each) => each == 2); 227 set.removeWhere((each) => each == 2);
214 Expect.equals(2, set.length); 228 Expect.equals(2, set.length);
215 Expect.isTrue(set.contains(1)); 229 Expect.isTrue(set.contains(1));
216 Expect.isFalse(set.contains(2)); 230 Expect.isFalse(set.contains(2));
217 Expect.isTrue(set.contains(3)); 231 Expect.isTrue(set.contains(3));
218 set.retainWhere((each) => each == 3); 232 set.retainWhere((each) => each == 3);
219 Expect.equals(1, set.length); 233 Expect.equals(1, set.length);
220 Expect.isFalse(set.contains(1)); 234 Expect.isFalse(set.contains(1));
221 Expect.isFalse(set.contains(2)); 235 Expect.isFalse(set.contains(2));
222 Expect.isTrue(set.contains(3)); 236 Expect.isTrue(set.contains(3));
223 } 237 }
224 238
225 { // Test lookup 239 {
240 // Test lookup
226 Set set = newSet(); 241 Set set = newSet();
227 var m1a = new Mutable(1); 242 var m1a = new Mutable(1);
228 var m1b = new Mutable(1); 243 var m1b = new Mutable(1);
229 var m2a = new Mutable(2); 244 var m2a = new Mutable(2);
230 var m2b = new Mutable(2); 245 var m2b = new Mutable(2);
231 Expect.isNull(set.lookup(m1a)); 246 Expect.isNull(set.lookup(m1a));
232 Expect.isNull(set.lookup(m1b)); 247 Expect.isNull(set.lookup(m1b));
233 set.add(m1a); 248 set.add(m1a);
234 Expect.identical(m1a, set.lookup(m1a)); 249 Expect.identical(m1a, set.lookup(m1a));
235 Expect.identical(m1a, set.lookup(m1b)); 250 Expect.identical(m1a, set.lookup(m1b));
236 251
237 Expect.isNull(set.lookup(m2a)); 252 Expect.isNull(set.lookup(m2a));
238 Expect.isNull(set.lookup(m2b)); 253 Expect.isNull(set.lookup(m2b));
239 set.add(m2a); 254 set.add(m2a);
240 Expect.identical(m2a, set.lookup(m2a)); 255 Expect.identical(m2a, set.lookup(m2a));
241 Expect.identical(m2a, set.lookup(m2b)); 256 Expect.identical(m2a, set.lookup(m2b));
242 257
243 set.add(m2b); // Adding doesn't change element. 258 set.add(m2b); // Adding doesn't change element.
244 Expect.identical(m2a, set.lookup(m2a)); 259 Expect.identical(m2a, set.lookup(m2a));
245 Expect.identical(m2a, set.lookup(m2b)); 260 Expect.identical(m2a, set.lookup(m2b));
246 261
247 set.remove(m1a); 262 set.remove(m1a);
248 set.add(m1b); 263 set.add(m1b);
249 Expect.identical(m1b, set.lookup(m1a)); 264 Expect.identical(m1b, set.lookup(m1a));
250 Expect.identical(m1b, set.lookup(m1b)); 265 Expect.identical(m1b, set.lookup(m1b));
251 266
252 set.add(1); 267 set.add(1);
253 Expect.identical(1, set.lookup(1.0)); 268 Expect.identical(1, set.lookup(1.0));
254 set.add(-0.0); 269 set.add(-0.0);
255 Expect.identical(-0.0, set.lookup(0.0)); 270 Expect.identical(-0.0, set.lookup(0.0));
256 } 271 }
257 272
258 { // Test special hash codes 273 {
274 // Test special hash codes
259 Set set = newSet(); 275 Set set = newSet();
260 List keys = []; 276 List keys = [];
261 // Powers of two 277 // Powers of two
262 for (int i = 65; i >= 2; --i) { 278 for (int i = 65; i >= 2; --i) {
263 keys.add(new Mutable(math.pow(2, i))); 279 keys.add(new Mutable(math.pow(2, i)));
264 } 280 }
265 for (var key in keys) { 281 for (var key in keys) {
266 Expect.isTrue(set.add(key)); 282 Expect.isTrue(set.add(key));
267 } 283 }
268 for (var key in keys) { 284 for (var key in keys) {
269 Expect.isTrue(set.contains(key)); 285 Expect.isTrue(set.contains(key));
270 } 286 }
271 } 287 }
272 } 288 }
273 289
274
275 void testIdentitySet(Set create()) { 290 void testIdentitySet(Set create()) {
276 Set set = create(); 291 Set set = create();
277 set.add(1); 292 set.add(1);
278 set.add(2); 293 set.add(2);
279 set.add(1); // Integers are identical if equal. 294 set.add(1); // Integers are identical if equal.
280 Expect.equals(2, set.length); 295 Expect.equals(2, set.length);
281 var complex = 4; 296 var complex = 4;
282 complex = set.length == 2 ? complex ~/ 4 : 87; // Avoid compile-time constant . 297 complex = set.length == 2 ? complex ~/ 4 : 87; // Avoid compile-time constant.
283 Expect.isTrue(set.contains(complex)); // 1 is in set, even if computed. 298 Expect.isTrue(set.contains(complex)); // 1 is in set, even if computed.
284 set.clear(); 299 set.clear();
285 300
286 // All compile time constants are identical to themselves. 301 // All compile time constants are identical to themselves.
287 var constants = [double.INFINITY, 302 var constants = [
303 double.INFINITY,
288 double.NAN, -0.0, //# 01: ok 304 double.NAN, -0.0, //# 01: ok
289 0.0, 42, "", null, false, true, #bif, testIdentitySet]; 305 0.0, 42, "", null, false, true, #bif, testIdentitySet
306 ];
290 set.addAll(constants); 307 set.addAll(constants);
291 Expect.equals(constants.length, set.length); 308 Expect.equals(constants.length, set.length);
292 for (var c in constants) { 309 for (var c in constants) {
293 Expect.isTrue(set.contains(c), "constant: $c"); 310 Expect.isTrue(set.contains(c), "constant: $c");
294 } 311 }
295 Expect.isTrue(set.containsAll(constants), "constants: $set"); 312 Expect.isTrue(set.containsAll(constants), "constants: $set");
296 set.clear(); 313 set.clear();
297 314
298 var m1 = new Mutable(1); 315 var m1 = new Mutable(1);
299 var m2 = new Mutable(2); 316 var m2 = new Mutable(2);
300 var m3 = new Mutable(3); 317 var m3 = new Mutable(3);
301 var m4 = new Mutable(2); // Equal to m2, but not identical. 318 var m4 = new Mutable(2); // Equal to m2, but not identical.
302 set.addAll([m1, m2, m3, m4]); 319 set.addAll([m1, m2, m3, m4]);
303 Expect.equals(4, set.length); 320 Expect.equals(4, set.length);
304 Expect.equals(3, m3.hashCode); 321 Expect.equals(3, m3.hashCode);
305 m3.id = 1; 322 m3.id = 1;
306 Expect.equals(1, m3.hashCode); 323 Expect.equals(1, m3.hashCode);
307 // Changing hashCode doesn't affect lookup. 324 // Changing hashCode doesn't affect lookup.
308 Expect.isTrue(set.contains(m3)); 325 Expect.isTrue(set.contains(m3));
309 Expect.isTrue(set.contains(m1)); 326 Expect.isTrue(set.contains(m1));
310 set.remove(m3); 327 set.remove(m3);
311 Expect.isFalse(set.contains(m3)); 328 Expect.isFalse(set.contains(m3));
312 Expect.isTrue(set.contains(m1)); 329 Expect.isTrue(set.contains(m1));
313 330
314 Expect.identical(m1, set.lookup(m1)); 331 Expect.identical(m1, set.lookup(m1));
315 Expect.identical(null, set.lookup(m3)); 332 Expect.identical(null, set.lookup(m3));
316 } 333 }
317 334
318
319 void main() { 335 void main() {
320 testSet(() => new Set(), (m) => new Set.from(m)); 336 testSet(() => new Set(), (m) => new Set.from(m));
321 testSet(() => new HashSet(), (m) => new HashSet.from(m)); 337 testSet(() => new HashSet(), (m) => new HashSet.from(m));
322 testSet(() => new LinkedHashSet(), (m) => new LinkedHashSet.from(m)); 338 testSet(() => new LinkedHashSet(), (m) => new LinkedHashSet.from(m));
323 testIdentitySet(() => new Set.identity()); 339 testIdentitySet(() => new Set.identity());
324 testIdentitySet(() => new HashSet.identity()); 340 testIdentitySet(() => new HashSet.identity());
325 testIdentitySet(() => new LinkedHashSet.identity()); 341 testIdentitySet(() => new LinkedHashSet.identity());
326 testIdentitySet(() => new HashSet(equals: (x, y) => identical(x, y), 342 testIdentitySet(() => new HashSet(
327 hashCode: (x) => identityHashCode(x))); 343 equals: (x, y) => identical(x, y), hashCode: (x) => identityHashCode(x)));
328 testIdentitySet( 344 testIdentitySet(() => new LinkedHashSet(
329 () => new LinkedHashSet(equals: (x, y) => identical(x, y), 345 equals: (x, y) => identical(x, y), hashCode: (x) => identityHashCode(x)));
330 hashCode: (x) => identityHashCode(x)));
331 } 346 }
332 347
333
334 class BadHashCode { 348 class BadHashCode {
335 static int idCounter = 0; 349 static int idCounter = 0;
336 final int id; 350 final int id;
337 BadHashCode() : id = idCounter++; 351 BadHashCode() : id = idCounter++;
338 int get hashCode => 42; 352 int get hashCode => 42;
339 // operator == is identity. 353 // operator == is identity.
340 // Can't make a bad compareTo that isn't invalid. 354 // Can't make a bad compareTo that isn't invalid.
341 int compareTo(BadHashCode other) => id - other.id; 355 int compareTo(BadHashCode other) => id - other.id;
342 } 356 }
343 357
344 class Mutable { 358 class Mutable {
345 int id; 359 int id;
346 Mutable(this.id); 360 Mutable(this.id);
347 int get hashCode => id; 361 int get hashCode => id;
348 bool operator==(other) => other is Mutable && id == other.id; 362 bool operator ==(other) => other is Mutable && id == other.id;
349 } 363 }
OLDNEW
« no previous file with comments | « tests/corelib_strong/double_parse_test.dart ('k') | tests/corelib_strong/int_modulo_arith_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698