OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // dart2js specific test to make sure String.hashCode behaves as | 5 // dart2js specific test to make sure hashCode on intercepted types behaves as |
6 // intended. | 6 // intended. |
7 | 7 |
| 8 |
| 9 class Hasher { |
| 10 confuse(x) => [1, 'x', true, null, x].last; |
| 11 hash(x) => confuse(x).hashCode; |
| 12 } |
| 13 |
| 14 // Hashing via [hash] should be forced to use the general interceptor, but the |
| 15 // local x.hashCode calls might be optimized. |
| 16 var hash = new Hasher().hash; |
| 17 |
| 18 check(value1, value2) { |
| 19 var h1 = hash(value1); |
| 20 var h2 = hash(value2); |
| 21 |
| 22 Expect.isTrue(h1 is int); |
| 23 Expect.isTrue(h2 is int); |
| 24 Expect.isFalse(h1 == h2); |
| 25 |
| 26 // We expect that the hash function is reasonable quality - there are some |
| 27 // difference in the low bits. |
| 28 Expect.isFalse((h1 & 0xf) == (h2 & 0xf)); |
| 29 |
| 30 // Quality check - the values should be SMIs for efficient arithmetic. |
| 31 Expect.equals((h1 & 0x1fffffff), h1); |
| 32 Expect.equals((h2 & 0x1fffffff), h2); |
| 33 } |
| 34 |
| 35 bools() { |
| 36 check(true, false); |
| 37 |
| 38 Expect.equals(true.hashCode, hash(true)); // First can be optimized. |
| 39 Expect.equals(false.hashCode, hash(false)); |
| 40 } |
| 41 |
| 42 ints() { |
| 43 var i1 = 100; |
| 44 var i2 = 101; |
| 45 check(i1, i2); |
| 46 Expect.equals(i1.hashCode, hash(i1)); |
| 47 Expect.equals(i2.hashCode, hash(i2)); |
| 48 } |
| 49 |
| 50 lists() { |
| 51 var list1 = []; |
| 52 var list2 = []; |
| 53 check(list1, list2); |
| 54 |
| 55 Expect.equals(list1.hashCode, hash(list1)); |
| 56 Expect.equals(list2.hashCode, hash(list2)); |
| 57 } |
| 58 |
| 59 strings() { |
| 60 var str1 = 'a'; |
| 61 var str2 = 'b'; |
| 62 var str3 = 'c'; |
| 63 check(str1, str2); |
| 64 check(str1, str3); |
| 65 check(str2, str3); |
| 66 |
| 67 Expect.equals(str1.hashCode, hash(str1)); |
| 68 Expect.equals(str2.hashCode, hash(str2)); |
| 69 Expect.equals(str3.hashCode, hash(str3)); |
| 70 |
| 71 Expect.equals(0xA2E9442, 'a'.hashCode); |
| 72 Expect.equals(0x0DB819B, 'b'.hashCode); |
| 73 Expect.equals(0xEBA5D59, 'c'.hashCode); |
| 74 } |
| 75 |
8 main() { | 76 main() { |
9 Expect.equals(67633152, 'a'.hashCode); | 77 bools(); |
10 Expect.equals(37224448, 'b'.hashCode); | 78 ints(); |
| 79 lists(); |
| 80 strings(); |
11 } | 81 } |
OLD | NEW |