-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathobject_hash_test.dart
155 lines (136 loc) · 3.66 KB
/
object_hash_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:math";
import "dart:typed_data";
import "package:expect/expect.dart";
main() {
const nan = double.nan;
const inf = double.infinity;
int hash1234 = Object.hash(1, 2, 3, 4);
Expect.type<int>(hash1234);
Expect.equals(hash1234, Object.hash(1, 2, 3, 4)); // Consistent.
Expect.equals(hash1234, Object.hashAll([1, 2, 3, 4]));
Expect.equals(hash1234, Object.hashAll(Uint8List.fromList([1, 2, 3, 4])));
Expect.notEquals(hash1234, Object.hash(1, 2, 3, 4, null));
Expect.equals(
Object.hash(1, 2, 3, 4, 5, 6, 7, 8, 9),
Object.hashAll([1, 2, 3, 4, 5, 6, 7, 8, 9]),
);
// Check that we can call `hash` with 2-20 arguments,
// and they all agree with `hashAll`.
var random = Random();
for (var i = 2; i <= 20; i++) {
var arguments = [for (var j = 0; j < i; j++) random.nextInt(256)];
var hashAll = Object.hashAll(arguments);
var hash = Function.apply(Object.hash, arguments);
Expect.equals(
hashAll,
hash,
"hashAll and hash disagrees for $i values:\n"
"$arguments",
);
}
// Works for all kinds of objects;
int varHash = Object.hash(
"string",
3,
nan,
true,
null,
Type,
#Symbol,
const Object(),
function,
);
Expect.equals(
varHash,
Object.hashAll([
"string",
3,
nan,
true,
null,
Type,
#Symbol,
const Object(),
function,
]),
);
// Object doesn't matter, just its hash code.
Expect.equals(
hash1234,
Object.hash(Hashable(1), Hashable(2), Hashable(3), Hashable(4)),
);
// It's potentially possible to get a conflict, but it doesn't happen here.
Expect.notEquals("str".hashCode, Object.hashAll(["str"]));
var hash12345 = Object.hashAllUnordered([1, 2, 3, 4, 5]);
for (var p in permutations([1, 2, 3, 4, 5])) {
Expect.equals(hash12345, Object.hashAllUnordered(p));
}
Expect.notEquals(
Object.hashAllUnordered(["a", "a"]),
Object.hashAllUnordered(["a"]),
);
Expect.notEquals(
Object.hashAllUnordered(["a", "a"]),
Object.hashAllUnordered(["a", "a", "a", "a"]),
);
Expect.notEquals(
Object.hashAllUnordered(["a", "b"]),
Object.hashAllUnordered(["a", "a", "a", "b"]),
);
/// Unordered hashing works for all kinds of objects.
var unorderHash = Object.hashAllUnordered([
"string",
3,
nan,
true,
null,
Type,
#Symbol,
const Object(),
function,
]);
var unorderHash2 = Object.hashAllUnordered([
true,
const Object(),
3,
function,
Type,
"string",
null,
nan,
#Symbol,
]);
Expect.equals(unorderHash, unorderHash2);
}
/// Lazily emits all permutations of [values].
///
/// Modifies [values] rather than create a new list.
/// The [values] list is guaranteed to end up in its original state
/// after all permutations have been read.
Iterable<List<T>> permutations<T>(List<T> values) {
Iterable<List<T>> recPermute(int end) sync* {
if (end == 1) {
yield values;
return;
}
for (var i = 0; i < end; i++) {
yield* recPermute(end - 1);
// Rotate values[i:].
var tmp = values.first;
for (var k = 1; k < end; k++) values[k - 1] = values[k];
values[end - 1] = tmp;
}
}
return recPermute(values.length);
}
// static function, used as constant value.
void function() {}
class Hashable {
final Object o;
Hashable(this.o);
bool operator ==(Object other) => other is Hashable && o == other.o;
int get hashCode => o.hashCode;
}