-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_test.go
104 lines (97 loc) · 3.16 KB
/
hash_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
package disgo
import (
"image"
"testing"
)
func TestHashDistance(t *testing.T) {
tests := []struct {
hash1 PHash
hash2 PHash
expectedDistance int
}{
{0x00, 0xff, 8},
{0xf0, 0x0f, 8},
{0xff, 0xfe, 1},
}
for i, test := range tests {
distance := test.hash1.Distance(test.hash2)
if distance != test.expectedDistance {
t.Errorf("tests[%d] expected %d got %d", i, test.expectedDistance, distance)
}
distance = test.hash2.Distance(test.hash1)
if distance != test.expectedDistance {
t.Errorf("tests[%d] expected %d got %d", i, test.expectedDistance, distance)
}
}
}
func TestHash(t *testing.T) {
tests := []struct {
expectedHash PHash
expectedString string
img image.Image
}{
{
expectedHash: 0x00ff00ff00ff00ff,
expectedString: "0x00ff00ff00ff00ff 0000000011111111000000001111111100000000111111110000000011111111",
img: &image.Gray{
Pix: []byte{
0x00, 0x03, 0x07, 0x0a, 0x0e, 0x11, 0x15, 0x18, 0x1c,
0xdf, 0xdb, 0xd8, 0xd4, 0xd0, 0xcd, 0xc9, 0xc6, 0xc2,
0x3f, 0x43, 0x46, 0x4a, 0x4d, 0x51, 0x55, 0x58, 0x5c,
0x9f, 0x9b, 0x98, 0x94, 0x91, 0x8d, 0x8a, 0x86, 0x83,
0x7f, 0x83, 0x86, 0x8a, 0x8d, 0x91, 0x94, 0x98, 0x9b,
0x5f, 0x5c, 0x58, 0x54, 0x51, 0x4d, 0x4a, 0x46, 0x43,
0xbf, 0xc2, 0xc6, 0xc9, 0xcd, 0xd0, 0xd4, 0xd8, 0xdb,
0x1f, 0x1c, 0x18, 0x15, 0x11, 0x0e, 0x0a, 0x07, 0x03,
},
Stride: 9,
Rect: image.Rect(0, 0, 9, 8),
},
},
{
expectedHash: 0,
expectedString: "0x0000000000000000 0000000000000000000000000000000000000000000000000000000000000000",
img: &image.Gray{
Pix: []byte{
0x03, 0x07, 0x0a, 0x0e, 0x11, 0x15, 0x18, 0x1c, 0x1f,
0x23, 0x26, 0x2a, 0x2e, 0x31, 0x35, 0x38, 0x3c, 0x3f,
0x43, 0x46, 0x4a, 0x4d, 0x51, 0x55, 0x58, 0x5c, 0x5f,
0x63, 0x66, 0x6a, 0x6d, 0x71, 0x74, 0x78, 0x7b, 0x7f,
0x83, 0x86, 0x8a, 0x8d, 0x91, 0x94, 0x98, 0x9b, 0x9f,
0xa2, 0xa6, 0xaa, 0xad, 0xb1, 0xb4, 0xb8, 0xbb, 0xbf,
0xc2, 0xc6, 0xc9, 0xcd, 0xd0, 0xd4, 0xd8, 0xdb, 0xdf,
0xe2, 0xe6, 0xe9, 0xed, 0xf0, 0xf4, 0xf7, 0xfb, 0xff,
},
Stride: 9,
Rect: image.Rect(0, 0, 9, 8),
},
},
{
expectedHash: 0xffffffffffffffff,
expectedString: "0xffffffffffffffff 1111111111111111111111111111111111111111111111111111111111111111",
img: &image.Gray{
Pix: []byte{
0xff, 0xfb, 0xf7, 0xf4, 0xf0, 0xed, 0xe9, 0xe6, 0xe2,
0xdf, 0xdb, 0xd8, 0xd4, 0xd0, 0xcd, 0xc9, 0xc6, 0xc2,
0xbf, 0xbb, 0xb8, 0xb4, 0xb1, 0xad, 0xa9, 0xa6, 0xa2,
0x9f, 0x9b, 0x98, 0x94, 0x91, 0x8d, 0x8a, 0x86, 0x83,
0x7f, 0x7b, 0x78, 0x74, 0x71, 0x6d, 0x6a, 0x66, 0x63,
0x5f, 0x5c, 0x58, 0x55, 0x51, 0x4d, 0x4a, 0x46, 0x43,
0x3f, 0x3c, 0x38, 0x35, 0x31, 0x2e, 0x2a, 0x26, 0x23,
0x1f, 0x1c, 0x18, 0x15, 0x11, 0x0e, 0x0a, 0x07, 0x03,
},
Stride: 9,
Rect: image.Rect(0, 0, 9, 8),
},
},
}
for i, test := range tests {
hash, _ := Hash(test.img)
if hash != test.expectedHash {
t.Errorf("tests[%d] expected %x got %x", i, test.expectedHash, hash)
}
if hash.String() != test.expectedString {
t.Errorf("tests[%d] expected %q got %q", i, test.expectedString, hash.String())
}
}
}