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

Side by Side Diff: tests/language/savannah_test.dart

Issue 10956026: - Add definition of "int hashCode()" to Object. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 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
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 // Dart test using an identity hash. 4 // Dart test using an identity hash.
5 5
6 interface BigGame extends Hashable { 6 abstract class BigGame {
7 final String name; 7 final String name;
8 } 8 }
9 9
10 // Giraffe overrides hashCode and provides its own identity hash.
10 class Giraffe implements BigGame { 11 class Giraffe implements BigGame {
11 final String name; 12 final String name;
12 final int identityHash_; 13 final int identityHash_;
13 14
14 Giraffe(this.name) : identityHash_ = nextId() {} 15 Giraffe(this.name) : identityHash_ = nextId() {}
15 16
16 int hashCode() { 17 int hashCode() {
17 return identityHash_; 18 return identityHash_;
18 } 19 }
19 20
20 // Calculate identity hash for a giraffe. 21 // Calculate identity hash for a giraffe.
21 static int nextId_; 22 static int nextId_;
22 static int nextId() { 23 static int nextId() {
23 if (nextId_ == null) { 24 if (nextId_ == null) {
24 nextId_ = 17; 25 nextId_ = 17;
25 } 26 }
26 return nextId_++; 27 return nextId_++;
27 } 28 }
28 } 29 }
29 30
31 // Zebra relies on the system provided identity hash.
30 class Zebra implements BigGame { 32 class Zebra implements BigGame {
31 final String name; 33 final String name;
32 Zebra(this.name) {} 34 Zebra(this.name) {}
33 } 35 }
34 36
35 37
36 class SavannahTest { 38 class SavannahTest {
37 39
38 static void testMain() { 40 static void testMain() {
39 Map<BigGame, String> savannah = new Map<BigGame, String>(); 41 Map<BigGame, String> savannah = new Map<BigGame, String>();
42
40 Giraffe giraffe1 = new Giraffe("Tony"); 43 Giraffe giraffe1 = new Giraffe("Tony");
41 Giraffe giraffe2 = new Giraffe("Rose"); 44 Giraffe giraffe2 = new Giraffe("Rose");
42 savannah[giraffe1] = giraffe1.name; 45 savannah[giraffe1] = giraffe1.name;
43 savannah[giraffe2] = giraffe2.name; 46 savannah[giraffe2] = giraffe2.name;
47 print("giraffe1 hash: ${giraffe1.hashCode()}");
48 print("giraffe2 hash: ${giraffe2.hashCode()}");
44 49
45 var count = savannah.length; 50 var count = savannah.length;
46 print("getCount is $count"); 51 print("getCount is $count");
47 Expect.equals(2, count); 52 Expect.equals(2, count);
53
48 print("giraffe1: ${savannah[giraffe1]}"); 54 print("giraffe1: ${savannah[giraffe1]}");
49 print("giraffe2: ${savannah[giraffe2]}"); 55 print("giraffe2: ${savannah[giraffe2]}");
50 Expect.equals("Tony", savannah[giraffe1]); 56 Expect.equals("Tony", savannah[giraffe1]);
51 Expect.equals("Rose", savannah[giraffe2]); 57 Expect.equals("Rose", savannah[giraffe2]);
52 58
53 bool caught = false; 59 Zebra zebra1 = new Zebra("Paolo");
54 Zebra zebra1 = new Zebra("Paul"); 60 Zebra zebra2 = new Zebra("Zeeta");
55 Zebra zebra2 = new Zebra("Joe"); 61 savannah[zebra1] = zebra1.name;
56 try { 62 savannah[zebra2] = zebra2.name;
57 savannah[zebra1] = zebra1.name; 63 print("zebra1 hash: ${zebra1.hashCode()}");
58 savannah[zebra2] = zebra2.name; 64 print("zebra2 hash: ${zebra2.hashCode()}");
59 } on NoSuchMethodError catch (e) {
60 print("Caught: $e");
61 caught = true;
62 }
63 Expect.equals(true, caught);
64 65
65 count = savannah.length; 66 count = savannah.length;
66 print("getCount is $count"); 67 print("getCount is $count");
67 Expect.equals(2, count); 68 Expect.equals(4, count);
68 69
69 caught = false; 70 print("zebra1: ${savannah[zebra1]}");
70 try { 71 print("zebra2: ${savannah[zebra2]}");
71 print("zebra1: ${savannah[zebra1]}"); 72 Expect.equals("Paolo", savannah[zebra1]);
72 print("zebra2: ${savannah[zebra2]}"); 73 Expect.equals("Zeeta", savannah[zebra2]);
73 } on NoSuchMethodError catch (e) {
74 print("Caught: $e");
75 caught = true;
76 }
77 Expect.equals(true, caught);
78 } 74 }
79 75
80 } 76 }
81 77
82 main() { 78 main() {
83 SavannahTest.testMain(); 79 SavannahTest.testMain();
84 } 80 }
OLDNEW
« lib/compiler/implementation/lib/core_patch.dart ('K') | « tests/language/language_dart2js.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698