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

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

Issue 23883003: Revert "Add custom equals and hashCode for HashMap implementation." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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 | « sdk/lib/collection/hash_map.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 map_test; 5 library map_test;
6 import "package:expect/expect.dart"; 6 import "package:expect/expect.dart";
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 void main() { 9 void main() {
10 test(new HashMap()); 10 test(new HashMap());
(...skipping 11 matching lines...) Expand all
22 testWeirdStringKeys(new HashMap<String, String>()); 22 testWeirdStringKeys(new HashMap<String, String>());
23 testWeirdStringKeys(new LinkedHashMap()); 23 testWeirdStringKeys(new LinkedHashMap());
24 testWeirdStringKeys(new LinkedHashMap<String, String>()); 24 testWeirdStringKeys(new LinkedHashMap<String, String>());
25 testWeirdStringKeys(new SplayTreeMap()); 25 testWeirdStringKeys(new SplayTreeMap());
26 testWeirdStringKeys(new SplayTreeMap<String, String>()); 26 testWeirdStringKeys(new SplayTreeMap<String, String>());
27 27
28 testNumericKeys(new Map()); 28 testNumericKeys(new Map());
29 testNumericKeys(new Map<num, String>()); 29 testNumericKeys(new Map<num, String>());
30 testNumericKeys(new HashMap()); 30 testNumericKeys(new HashMap());
31 testNumericKeys(new HashMap<num, String>()); 31 testNumericKeys(new HashMap<num, String>());
32 testNumericKeys(new HashMap(equals: identical));
33 testNumericKeys(new HashMap<num, String>(equals: identical));
34 testNumericKeys(new LinkedHashMap()); 32 testNumericKeys(new LinkedHashMap());
35 testNumericKeys(new LinkedHashMap<num, String>()); 33 testNumericKeys(new LinkedHashMap<num, String>());
36
37 testNaNKeys(new Map());
38 testNaNKeys(new Map<num, String>());
39 testNaNKeys(new HashMap());
40 testNaNKeys(new HashMap<num, String>());
41 testNaNKeys(new LinkedHashMap());
42 testNaNKeys(new LinkedHashMap<num, String>());
43 // Identity maps fail the NaN-keys tests because the test assumes that
44 // NaN is not equal to NaN.
45
46 testIdentityMap(new HashMap(equals: identical));
47
48 testCustomMap(new HashMap(equals: myEquals, hashCode: myHashCode));
49
50 testIterationOrder(new LinkedHashMap());
51 } 34 }
52 35
53 36
54 void test(Map map) { 37 void test(Map map) {
55 testDeletedElement(map); 38 testDeletedElement(map);
56 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); 39 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8);
57 map.clear(); 40 map.clear();
58 testMap(map, "value1", "value2", "value3", "value4", "value5", 41 testMap(map, "value1", "value2", "value3", "value4", "value5",
59 "value6", "value7", "value8"); 42 "value6", "value7", "value8");
60 } 43 }
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 Expect.equals(null, map[key]); 366 Expect.equals(null, map[key]);
384 var value = 'value:$key'; 367 var value = 'value:$key';
385 map[key] = value; 368 map[key] = value;
386 Expect.isTrue(map.containsKey(key)); 369 Expect.isTrue(map.containsKey(key));
387 Expect.equals(value, map[key]); 370 Expect.equals(value, map[key]);
388 Expect.equals(value, map.remove(key)); 371 Expect.equals(value, map.remove(key));
389 Expect.isFalse(map.containsKey(key)); 372 Expect.isFalse(map.containsKey(key));
390 Expect.equals(null, map[key]); 373 Expect.equals(null, map[key]);
391 } 374 }
392 Expect.isTrue(map.isEmpty); 375 Expect.isTrue(map.isEmpty);
393 }
394 376
395 void testNaNKeys(Map map) {
396 Expect.isTrue(map.isEmpty);
397 // Test NaN. 377 // Test NaN.
398 var nan = double.NAN; 378 var nan = double.NAN;
399 Expect.isFalse(map.containsKey(nan)); 379 Expect.isFalse(map.containsKey(nan));
400 Expect.equals(null, map[nan]); 380 Expect.equals(null, map[nan]);
401 381
402 map[nan] = 'value:0'; 382 map[nan] = 'value:0';
403 Expect.isFalse(map.containsKey(nan)); 383 Expect.isFalse(map.containsKey(nan));
404 Expect.equals(null, map[nan]); 384 Expect.equals(null, map[nan]);
405 testLength(1, map); 385 testLength(1, map);
406 386
(...skipping 13 matching lines...) Expand all
420 400
421 map.clear(); 401 map.clear();
422 Expect.isTrue(map.isEmpty); 402 Expect.isTrue(map.isEmpty);
423 } 403 }
424 404
425 void testLength(int length, Map map) { 405 void testLength(int length, Map map) {
426 Expect.equals(length, map.length); 406 Expect.equals(length, map.length);
427 (length == 0 ? Expect.isTrue : Expect.isFalse)(map.isEmpty); 407 (length == 0 ? Expect.isTrue : Expect.isFalse)(map.isEmpty);
428 (length != 0 ? Expect.isTrue : Expect.isFalse)(map.isNotEmpty); 408 (length != 0 ? Expect.isTrue : Expect.isFalse)(map.isNotEmpty);
429 } 409 }
430
431
432 testIdentityMap(Map map) {
433 Expect.isTrue(map.isEmpty);
434
435 var nan = double.NAN;
436 // TODO(11551): Remove guard when dart2js makes identical(NaN, NaN) true.
437 if (identical(nan, nan)) {
438 map[nan] = 42;
439 testLength(1, map);
440 Expect.isTrue(map.containsKey(nan));
441 Expect.equals(42, map[nan]);
442 map[nan] = 37;
443 testLength(1, map);
444 Expect.equals(37, map[nan]);
445 Expect.equals(37, map.remove(nan));
446 testLength(0, map);
447 }
448
449 Vampire v1 = const Vampire(1);
450 Vampire v2 = const Vampire(2);
451 Expect.isFalse(v1 == v1);
452 Expect.isFalse(v2 == v2);
453 Expect.isTrue(v2 == v1); // Snob!
454
455 map[v1] = 1;
456 map[v2] = 2;
457 testLength(2, map);
458
459 Expect.isTrue(map.containsKey(v1));
460 Expect.isTrue(map.containsKey(v2));
461
462 Expect.equals(1, map[v1]);
463 Expect.equals(2, map[v2]);
464
465 Expect.equals(1, map.remove(v1));
466 testLength(1, map);
467 Expect.isFalse(map.containsKey(v1));
468 Expect.isTrue(map.containsKey(v2));
469
470 Expect.isNull(map.remove(v1));
471 Expect.equals(2, map.remove(v2));
472 testLength(0, map);
473
474 var eq01 = new Equalizer(0);
475 var eq02 = new Equalizer(0);
476 var eq11 = new Equalizer(1);
477 var eq12 = new Equalizer(1);
478 // Sanity.
479 Expect.equals(eq01, eq02);
480 Expect.equals(eq02, eq01);
481 Expect.equals(eq11, eq12);
482 Expect.equals(eq12, eq11);
483 Expect.notEquals(eq01, eq11);
484 Expect.notEquals(eq01, eq12);
485 Expect.notEquals(eq02, eq11);
486 Expect.notEquals(eq02, eq12);
487 Expect.notEquals(eq11, eq01);
488 Expect.notEquals(eq11, eq02);
489 Expect.notEquals(eq12, eq01);
490 Expect.notEquals(eq12, eq02);
491
492 map[eq01] = 0;
493 map[eq02] = 1;
494 map[eq11] = 2;
495 map[eq12] = 3;
496 testLength(4, map);
497
498 Expect.equals(0, map[eq01]);
499 Expect.equals(1, map[eq02]);
500 Expect.equals(2, map[eq11]);
501 Expect.equals(3, map[eq12]);
502
503 Expect.isTrue(map.containsKey(eq01));
504 Expect.isTrue(map.containsKey(eq02));
505 Expect.isTrue(map.containsKey(eq11));
506 Expect.isTrue(map.containsKey(eq12));
507
508 Expect.equals(1, map.remove(eq02));
509 Expect.equals(3, map.remove(eq12));
510 testLength(2, map);
511 Expect.isTrue(map.containsKey(eq01));
512 Expect.isFalse(map.containsKey(eq02));
513 Expect.isTrue(map.containsKey(eq11));
514 Expect.isFalse(map.containsKey(eq12));
515
516 Expect.equals(0, map[eq01]);
517 Expect.equals(null, map[eq02]);
518 Expect.equals(2, map[eq11]);
519 Expect.equals(null, map[eq12]);
520
521 Expect.equals(0, map.remove(eq01));
522 Expect.equals(2, map.remove(eq11));
523 testLength(0, map);
524
525 map[eq01] = 0;
526 map[eq02] = 1;
527 map[eq11] = 2;
528 map[eq12] = 3;
529 testLength(4, map);
530
531 // Transfer to equality-based map will collapse elements.
532 Map eqMap = new HashMap();
533 eqMap.addAll(map);
534 testLength(2, eqMap);
535 Expect.isTrue(eqMap.containsKey(eq01));
536 Expect.isTrue(eqMap.containsKey(eq02));
537 Expect.isTrue(eqMap.containsKey(eq11));
538 Expect.isTrue(eqMap.containsKey(eq12));
539 }
540
541 /** Class of objects that are equal if they hold the same id. */
542 class Equalizer {
543 int id;
544 Equalizer(this.id);
545 int get hashCode => id;
546 bool operator==(Object other) =>
547 other is Equalizer && id == (other as Equalizer).id;
548 }
549
550 /**
551 * Objects that are not reflexive.
552 *
553 * They think they are better than their equals.
554 */
555 class Vampire {
556 final int generation;
557 const Vampire(this.generation);
558
559 int get hashCode => generation;
560
561 // The double-fang operator falsely claims that a vampire is equal to
562 // any of its sire's generation.
563 bool operator==(Object other) =>
564 other is Vampire && generation - 1 == (other as Vampire).generation;
565 }
566
567 void testCustomMap(Map map) {
568 testLength(0, map);
569 var c11 = const Customer(1, 1);
570 var c12 = const Customer(1, 2);
571 var c21 = const Customer(2, 1);
572 var c22 = const Customer(2, 2);
573 // Sanity.
574 Expect.equals(c11, c12);
575 Expect.notEquals(c11, c21);
576 Expect.notEquals(c11, c22);
577 Expect.equals(c21, c22);
578 Expect.notEquals(c21, c11);
579 Expect.notEquals(c21, c12);
580
581 Expect.isTrue(myEquals(c11, c21));
582 Expect.isFalse(myEquals(c11, c12));
583 Expect.isFalse(myEquals(c11, c22));
584 Expect.isTrue(myEquals(c12, c22));
585 Expect.isFalse(myEquals(c12, c11));
586 Expect.isFalse(myEquals(c12, c21));
587
588 map[c11] = 42;
589 testLength(1, map);
590 Expect.isTrue(map.containsKey(c11));
591 Expect.isTrue(map.containsKey(c21));
592 Expect.isFalse(map.containsKey(c12));
593 Expect.isFalse(map.containsKey(c22));
594 Expect.equals(42, map[c11]);
595 Expect.equals(42, map[c21]);
596
597 map[c21] = 37;
598 testLength(1, map);
599 Expect.isTrue(map.containsKey(c11));
600 Expect.isTrue(map.containsKey(c21));
601 Expect.isFalse(map.containsKey(c12));
602 Expect.isFalse(map.containsKey(c22));
603 Expect.equals(37, map[c11]);
604 Expect.equals(37, map[c21]);
605
606 map[c22] = 42;
607 testLength(2, map);
608 Expect.isTrue(map.containsKey(c11));
609 Expect.isTrue(map.containsKey(c21));
610 Expect.isTrue(map.containsKey(c12));
611 Expect.isTrue(map.containsKey(c22));
612 Expect.equals(37, map[c11]);
613 Expect.equals(37, map[c21]);
614 Expect.equals(42, map[c12]);
615 Expect.equals(42, map[c22]);
616
617 Expect.equals(42, map.remove(c12));
618 testLength(1, map);
619 Expect.isTrue(map.containsKey(c11));
620 Expect.isTrue(map.containsKey(c21));
621 Expect.isFalse(map.containsKey(c12));
622 Expect.isFalse(map.containsKey(c22));
623 Expect.equals(37, map[c11]);
624 Expect.equals(37, map[c21]);
625
626 Expect.equals(37, map.remove(c11));
627 testLength(0, map);
628 }
629
630 class Customer {
631 final int id;
632 final int secondId;
633 const Customer(this.id, this.secondId);
634 int get hashCode => id;
635 bool operator==(Object other) {
636 if (other is! Customer) return false;
637 Customer otherCustomer = other;
638 return id == otherCustomer.id;
639 }
640 }
641
642 int myHashCode(Customer c) => c.secondId;
643 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId;
644
645 testIterationOrder(Map map) {
646 var order = [0, 6, 4, 2, 7, 9, 7, 1, 2, 5, 3];
647 for (int i = 0; i < order.length; i++) map[order[i]] = i;
648 Expect.listEquals(map.keys.toList(), [0, 6, 4, 2, 7, 9, 1, 5, 3]);
649 Expect.listEquals(map.values.toList(), [0, 1, 2, 8, 6, 5, 7, 9, 10]);
650 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/hash_map.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698