OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 |
| 7 class Override { |
| 8 int hash; |
| 9 int get superHash => super.hashCode; |
| 10 int get hashCode => hash; |
| 11 |
| 12 int foo() => hash; // Just some function that can be closurized. |
| 13 |
| 14 bool operator==(Object other) => |
| 15 other is Override && (other as Override).hash == hash; |
| 16 } |
| 17 |
| 18 int bar() => 42; // Some global function. |
| 19 |
| 20 main() { |
| 21 var o = new Object(); |
| 22 var hash = o.hashCode; |
| 23 // Doesn't change. |
| 24 Expect.equals(hash, o.hashCode); |
| 25 Expect.equals(hash, identityHashCode(o)); |
| 26 |
| 27 var c = new Override(); |
| 28 int identityHash = c.superHash; |
| 29 hash = (identityHash == 42) ? 37 : 42; |
| 30 c.hash = hash; |
| 31 Expect.equals(hash, c.hashCode); |
| 32 Expect.equals(identityHash, identityHashCode(c)); |
| 33 |
| 34 // These classes don't override hashCode. |
| 35 var samples = [0, 0x10000000, 1.5, -0, null, true, false, const Object()]; |
| 36 for (var v in samples) { |
| 37 print(v); |
| 38 Expect.equals(v.hashCode, identityHashCode(v)); |
| 39 } |
| 40 // These do, or might do, but we can still use hashCodeOf and get the same |
| 41 // result each time. |
| 42 samples = ["string", "", (x) => 42, c.foo, bar]; |
| 43 for (var v in samples) { |
| 44 print(v); |
| 45 Expect.equals(v.hashCode, v.hashCode); |
| 46 Expect.equals(identityHashCode(v), identityHashCode(v)); |
| 47 } |
| 48 } |
OLD | NEW |