-
Notifications
You must be signed in to change notification settings - Fork 10
/
deephash_test.go
261 lines (232 loc) · 5.88 KB
/
deephash_test.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package deephash
import (
"bytes"
"fmt"
"reflect"
"testing"
)
type testStruct struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
Interface interface{}
}
var differentTestCases = []interface{}{
// simple types
"dave",
"foo",
"foobar",
" foo",
1,
1.0,
// structs
testStruct{S: "foo"},
testStruct{S: "bar"},
// pointers to structs
&testStruct{S: "foo1"},
&testStruct{S: "bar1"},
// structs with different types of ints
&testStruct{I: 43, I8: 43, I16: 43, I32: 43, I64: 43, U8: 43, U16: 43, U32: 43, U64: 43},
&testStruct{I: 44, I8: 44, I16: 44, I32: 44, I64: 44, U8: 44, U16: 44, U32: 44, U64: 44},
&testStruct{I: 11, I8: 43, I16: 43, I32: 43, I64: 43, U8: 43, U16: 43, U32: 43, U64: 43},
&testStruct{I: 43, I8: 11, I16: 43, I32: 43, I64: 43, U8: 43, U16: 43, U32: 43, U64: 43},
&testStruct{I: 43, I8: 43, I16: 11, I32: 43, I64: 43, U8: 43, U16: 43, U32: 43, U64: 43},
&testStruct{I: 43, I8: 43, I16: 43, I32: 11, I64: 43, U8: 43, U16: 43, U32: 43, U64: 43},
&testStruct{I: 43, I8: 43, I16: 43, I32: 43, I64: 11, U8: 43, U16: 43, U32: 43, U64: 43},
&testStruct{I: 43, I8: 43, I16: 43, I32: 43, I64: 43, U8: 11, U16: 43, U32: 43, U64: 43},
&testStruct{I: 43, I8: 43, I16: 43, I32: 43, I64: 43, U8: 43, U16: 11, U32: 43, U64: 43},
&testStruct{I: 43, I8: 43, I16: 43, I32: 43, I64: 43, U8: 43, U16: 43, U32: 11, U64: 43},
&testStruct{I: 43, I8: 43, I16: 43, I32: 43, I64: 43, U8: 43, U16: 43, U32: 43, U64: 11},
// structs with different types of floats
&testStruct{F32: 43.0, F64: 43.0},
&testStruct{F32: 44.0, F64: 44.0},
&testStruct{F32: 11.0, F64: 43.0},
&testStruct{F32: 43.0, F64: 11.0},
// string maps
map[string]testStruct{
"foo": testStruct{S: "baz"},
"bar": testStruct{S: "baz"},
},
map[string]testStruct{
"foo": testStruct{S: "BAZZER"},
"bar": testStruct{S: "BAZZER"},
},
// other maps
map[testStruct]testStruct{
testStruct{S: "baz"}: testStruct{S: "baz"},
testStruct{S: "bar"}: testStruct{S: "bar"},
},
// slices -- ordered here
[]testStruct{
testStruct{S: "foo"},
testStruct{S: "bar"},
testStruct{S: "baz"},
},
[]testStruct{
testStruct{S: "bar"},
testStruct{S: "foo"},
testStruct{S: "baz"},
},
[]testStruct{
testStruct{S: "bar"},
testStruct{S: "baz"},
testStruct{S: "foo"},
},
// arrays -- we're looking at the contents, so we have to be differnet to the slices
[3]testStruct{
testStruct{S: "FOO"},
testStruct{S: "BAR"},
testStruct{S: "BAZ"},
},
[3]testStruct{
testStruct{S: "BAR"},
testStruct{S: "FOO"},
testStruct{S: "BAZ"},
},
[3]testStruct{
testStruct{S: "BAR"},
testStruct{S: "BAZ"},
testStruct{S: "FOO"},
},
// Interface types
&testStruct{Interface: testStruct{I: 42}},
&testStruct{Interface: &testStruct{I: 100}},
}
var sameCases = [][]interface{}{
// simple stuff
[]interface{}{
"foo",
"foo",
},
// hash order shouldn't matter
[]interface{}{
map[string]testStruct{
"foo": testStruct{S: "baz"},
"bar": testStruct{S: "baz"},
},
map[string]testStruct{
"bar": testStruct{S: "baz"},
"foo": testStruct{S: "baz"},
},
},
// we care about the contents, so we want different values of a struct with same contents to be same
[]interface{}{
&testStruct{F32: 43.0, F64: 43.0},
&testStruct{F32: 43.0, F64: 43.0},
testStruct{F32: 43.0, F64: 43.0},
},
// slices and arrays should match
[]interface{}{
[3]testStruct{
testStruct{S: "FOO"},
testStruct{S: "BAR"},
testStruct{S: "BAZ"},
},
[]testStruct{
testStruct{S: "FOO"},
testStruct{S: "BAR"},
testStruct{S: "BAZ"},
},
[]testStruct{
testStruct{S: "FOO"},
testStruct{S: "BAR"},
testStruct{S: "BAZ"},
},
},
// We should follow pointers of pointers and pointers within interfaces
[]interface{}{
&testStruct{Interface: testStruct{I: 42}},
&testStruct{Interface: &testStruct{I: 42}},
&testStruct{Interface: reflect.ValueOf(&testStruct{I: 42}).Interface()},
},
}
func TestDifferentCases(t *testing.T) {
seen := make(map[string]bool)
for _, tc := range differentTestCases {
h := Hash(tc)
hs := fmt.Sprintf("%x", h)
if len(h) == 0 {
t.Errorf("Test case %v yields zero length hash", tc)
continue
}
if seen[hs] {
t.Errorf("Test case %v hashes to %v which has already been seen", tc, hs)
}
seen[hs] = true
}
}
func TestSameCases(t *testing.T) {
for _, tcs := range sameCases {
hash := ""
for _, tc := range tcs {
h := Hash(tc)
hs := fmt.Sprintf("%x", h)
if len(h) == 0 {
t.Errorf("Test case %v yields zero length hash", tc)
continue
}
if hash == "" {
hash = hs
} else if hash != hs {
t.Errorf("Test case %v hashes to '%v' which is different to previous '%v'", tc, hs, hash)
}
}
}
}
type circular struct {
V *circular
}
func TestCircular(t *testing.T) {
a := &circular{}
b := &circular{V: a}
h := Hash(b)
if h == nil || len(h) == 0 {
t.Error("Hash circular should yield some hash value")
}
// now actually circular it up
a.V = b
h = Hash(b)
if h == nil || len(h) == 0 {
t.Error("Hash circular should yield some hash value")
}
}
type RefB struct {
Id string
}
type RefA struct {
Id string
B RefB
}
func TestRef(t *testing.T) {
a := RefA{
Id: "test",
B: RefB{Id: "anothertest"},
}
b := RefA{
Id: "test",
B: RefB{Id: "anothertest"},
}
if !bytes.Equal(Hash(a), Hash(b)) {
t.Fatal("Expecting our two reference cases to hash the same even though different underlying objects, because same values")
}
if !bytes.Equal(Hash(a), Hash(a)) {
t.Fatal("Expecting our two reference cases to hash the same because they are the same")
}
}
func TestBooleans(t *testing.T) {
if !bytes.Equal(Hash(true), Hash(true)) {
t.Fatal("Expecting the same boolean value to have the same hash")
}
if bytes.Equal(Hash(true), Hash(false)) {
t.Fatal("Expecting true to hash differently than false")
}
}