-
Notifications
You must be signed in to change notification settings - Fork 11
/
phash_test.go
154 lines (131 loc) · 3.38 KB
/
phash_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
package phash
import (
"fmt"
"testing"
)
var hashTestData = []struct {
file string
expected uint64
}{
{"test_data/jpg/cat.jpg", 11220389026139797626},
{"test_data/png/gopher.png", 4855808264951085874},
{"test_data/gif/zebras.gif", 7086864532766789979},
{"test_data/jpg/zebras.jpg", 7086864532766789979},
{"test_data/png/zebras.png", 7086864532766789979},
// {"test_data/png/grass.png", 54086765383280},
// {"test_data/gif/animated_zebras.gif", 7086864532766789979},
}
// ImageHash
func BenchmarkImageHash(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ImageHashDCT(hashTestData[0].file)
if err != nil {
b.Fatal(err)
}
}
}
func TestImageHash(t *testing.T) {
for _, test := range hashTestData {
hash, err := ImageHashDCT(test.file)
if err != nil {
t.Fatal(err)
}
if hash != test.expected {
t.Errorf("%s: Hash Mismatch: expected %d, got %d", test.file, test.expected, hash)
}
}
}
func ExampleImageHashDCT() {
hash, err := ImageHashDCT("test_data/jpg/cat.jpg")
if err != nil {
panic(err)
}
fmt.Println(hash)
// Output: 11220389026139797626
}
// HammingDistanceForHashes
func BenchmarkHammingDistanceForHashesForSameImage(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := HammingDistanceForHashes(7086864532766789979, 7086864532766789979)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHammingDistanceForHashesForDifferentImages(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := HammingDistanceForHashes(7086864532766789979, 11220389026139797626)
if err != nil {
b.Fatal(err)
}
}
}
func TestHammingDistanceForHashesForSameImage(t *testing.T) {
d, err := HammingDistanceForHashes(7086864532766789979, 7086864532766789979)
if err != nil {
t.Fatal(err)
}
if d != 0 {
t.Errorf("Distance Mismatch: expected 0, got %d", d)
}
}
func TestHammingDistanceForHashesForDifferentImages(t *testing.T) {
d, err := HammingDistanceForHashes(11220389026139797626, 4855808264951085874)
if err != nil {
t.Fatal(err)
}
if d == 0 {
t.Error("Distance Mismatch: got unexpected 0")
}
}
func ExampleHammingDistanceForHashes() {
d, err := HammingDistanceForHashes(11220389026139797626, 4855808264951085874)
if err != nil {
panic(err)
}
fmt.Println(d)
// Output: 30
}
// HammingDistanceForFiles
func BenchmarkHammingDistanceForFilesForSameImage(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := HammingDistanceForFiles("test_data/jpg/cat.jpg", "test_data/jpg/cat.jpg")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHammingDistanceForFilesForDifferentImages(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := HammingDistanceForFiles("test_data/jpg/cat.jpg", "test_data/png/gopher.png")
if err != nil {
b.Fatal(err)
}
}
}
func TestHammingDistanceForFilesForSameImage(t *testing.T) {
d, err := HammingDistanceForFiles("test_data/jpg/cat.jpg", "test_data/jpg/cat.jpg")
if err != nil {
t.Fatal(err)
}
if d != 0 {
t.Errorf("Distance Mismatch: expected 0, got %d", d)
}
}
func TestHammingDistanceForFilesForDifferentImages(t *testing.T) {
d, err := HammingDistanceForFiles("test_data/jpg/cat.jpg", "test_data/png/gopher.png")
if err != nil {
t.Fatal(err)
}
if d == 0 {
t.Error("Distance Mismatch: got unexpected 0")
}
}
func ExampleHammingDistanceForFiles() {
d, err := HammingDistanceForFiles("test_data/jpg/cat.jpg", "test_data/png/gopher.png")
if err != nil {
panic(err)
}
fmt.Println(d)
// Output: 30
}