Chromium Code Reviews| 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 // dart2js specific test to make sure hashCode on intercepted types behaves as | 7 // dart2js specific test to make sure hashCode on intercepted types behaves as |
| 8 // intended. | 8 // intended. |
| 9 | 9 |
| 10 | 10 |
| 11 class Hasher { | 11 class Hasher { |
| 12 confuse(x) => [1, 'x', true, null, x].last; | 12 confuse(x) => [1, 'x', true, null, x].last; |
| 13 hash(x) => confuse(x).hashCode; | 13 hash(x) => confuse(x).hashCode; |
| 14 } | 14 } |
| 15 | 15 |
| 16 // Hashing via [hash] should be forced to use the general interceptor, but the | 16 // Hashing via [hash] should be forced to use the general interceptor, but the |
| 17 // local x.hashCode calls might be optimized. | 17 // local x.hashCode calls might be optimized. |
| 18 var hash = new Hasher().hash; | 18 var hash = new Hasher().hash; |
| 19 | 19 |
| 20 check(value1, value2) { | 20 check(value1, value2, {identityHashCode}) { |
|
ahe
2013/06/04 13:19:49
bool identityHashCode: false
| |
| 21 var h1 = hash(value1); | 21 var h1 = hash(value1); |
| 22 var h2 = hash(value2); | 22 var h2 = hash(value2); |
| 23 | |
| 24 Expect.isTrue(h1 is int); | 23 Expect.isTrue(h1 is int); |
| 25 Expect.isTrue(h2 is int); | 24 Expect.isTrue(h2 is int); |
| 26 Expect.isFalse(h1 == h2); | |
| 27 | |
| 28 // We expect that the hash function is reasonable quality - there are some | |
| 29 // difference in the low bits. | |
| 30 Expect.isFalse((h1 & 0xf) == (h2 & 0xf)); | |
| 31 | 25 |
| 32 // Quality check - the values should be SMIs for efficient arithmetic. | 26 // Quality check - the values should be SMIs for efficient arithmetic. |
| 33 Expect.equals((h1 & 0x3fffffff), h1); | 27 Expect.equals((h1 & 0x3fffffff), h1); |
| 34 Expect.equals((h2 & 0x3fffffff), h2); | 28 Expect.equals((h2 & 0x3fffffff), h2); |
| 29 | |
| 30 // If we're checking the (randomized) identity hash code function, | |
| 31 // we cannot guarantee anything about the actual hash code values. | |
| 32 if (identityHashCode) return; | |
|
ahe
2013/06/04 13:19:49
Otherwise this will fail in checked mode.
| |
| 33 | |
| 34 // We expect that the hash function is reasonable quality - there | |
| 35 // are some difference in the low bits. | |
| 36 Expect.isFalse(h1 == h2); | |
| 37 Expect.isFalse((h1 & 0xf) == (h2 & 0xf)); | |
| 35 } | 38 } |
| 36 | 39 |
| 37 bools() { | 40 bools() { |
| 38 check(true, false); | 41 check(true, false, identityHashCode: false); |
| 39 | 42 |
| 40 Expect.equals(true.hashCode, hash(true)); // First can be optimized. | 43 Expect.equals(true.hashCode, hash(true)); // First can be optimized. |
| 41 Expect.equals(false.hashCode, hash(false)); | 44 Expect.equals(false.hashCode, hash(false)); |
| 42 } | 45 } |
| 43 | 46 |
| 44 ints() { | 47 ints() { |
| 45 var i1 = 100; | 48 var i1 = 100; |
| 46 var i2 = 101; | 49 var i2 = 101; |
| 47 check(i1, i2); | 50 check(i1, i2, identityHashCode: false); |
| 48 Expect.equals(i1.hashCode, hash(i1)); | 51 Expect.equals(i1.hashCode, hash(i1)); |
| 49 Expect.equals(i2.hashCode, hash(i2)); | 52 Expect.equals(i2.hashCode, hash(i2)); |
| 50 } | 53 } |
| 51 | 54 |
| 52 lists() { | 55 lists() { |
| 53 var list1 = []; | 56 var list1 = []; |
| 54 var list2 = []; | 57 var list2 = []; |
| 55 check(list1, list2); | 58 check(list1, list2, identityHashCode: true); |
| 56 | 59 |
| 57 Expect.equals(list1.hashCode, hash(list1)); | 60 Expect.equals(list1.hashCode, hash(list1)); |
| 58 Expect.equals(list2.hashCode, hash(list2)); | 61 Expect.equals(list2.hashCode, hash(list2)); |
| 59 } | 62 } |
| 60 | 63 |
| 61 strings() { | 64 strings() { |
| 62 var str1 = 'a'; | 65 var str1 = 'a'; |
| 63 var str2 = 'b'; | 66 var str2 = 'b'; |
| 64 var str3 = 'c'; | 67 var str3 = 'c'; |
| 65 check(str1, str2); | 68 check(str1, str2, identityHashCode: false); |
| 66 check(str1, str3); | 69 check(str1, str3, identityHashCode: false); |
| 67 check(str2, str3); | 70 check(str2, str3, identityHashCode: false); |
| 68 | 71 |
| 69 Expect.equals(str1.hashCode, hash(str1)); | 72 Expect.equals(str1.hashCode, hash(str1)); |
| 70 Expect.equals(str2.hashCode, hash(str2)); | 73 Expect.equals(str2.hashCode, hash(str2)); |
| 71 Expect.equals(str3.hashCode, hash(str3)); | 74 Expect.equals(str3.hashCode, hash(str3)); |
| 72 | 75 |
| 73 Expect.equals(0xA2E9442, 'a'.hashCode); | 76 Expect.equals(0xA2E9442, 'a'.hashCode); |
| 74 Expect.equals(0x0DB819B, 'b'.hashCode); | 77 Expect.equals(0x0DB819B, 'b'.hashCode); |
| 75 Expect.equals(0xEBA5D59, 'c'.hashCode); | 78 Expect.equals(0xEBA5D59, 'c'.hashCode); |
| 76 } | 79 } |
| 77 | 80 |
| 78 main() { | 81 main() { |
| 79 bools(); | 82 bools(); |
| 80 ints(); | 83 ints(); |
| 81 lists(); | 84 lists(); |
| 82 strings(); | 85 strings(); |
| 83 } | 86 } |
| OLD | NEW |