-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_test.go
149 lines (128 loc) · 4.96 KB
/
database_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
package disgo
import (
"bytes"
"fmt"
"image"
"image/png"
"reflect"
"testing"
)
type testIndex struct {
err error
matches []PHash
}
func (ti *testIndex) Insert(PHash) error { return ti.err }
func (ti *testIndex) Search(PHash, int) ([]PHash, error) { return ti.matches, ti.err }
func newTestIndex() *testIndex {
return &testIndex{}
}
func TestDBAdd(t *testing.T) {
tests := []struct {
img image.Image
expectedErr error
}{
{image.NewRGBA(image.Rect(0, 0, 10, 10)), nil},
{image.NewRGBA(image.Rect(0, 0, 10, 10)), fmt.Errorf("Just some test error")},
}
for i, test := range tests {
db := NewDB(newTestIndex())
db.hasher = func(image.Image) (PHash, error) { return PHash(0), test.expectedErr }
_, err := db.Add(test.img)
if err != test.expectedErr {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedErr, err)
}
}
}
func TestDBAddFile(t *testing.T) {
tests := []struct {
expectedErr error
}{
{nil},
{fmt.Errorf("Just some test error")},
}
for i, test := range tests {
db := NewDB(newTestIndex())
db.hasher = func(image.Image) (PHash, error) { return PHash(0), test.expectedErr }
image := image.NewAlpha(image.Rect(0, 0, 1, 1))
buf := bytes.NewBuffer([]byte{})
png.Encode(buf, image)
_, err := db.AddFile(buf)
if err != test.expectedErr {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedErr, err)
}
}
}
func TestDBSearch(t *testing.T) {
tests := []struct {
img image.Image
expectedMatches []PHash
expectedHashErr error
expectedSearchErr error
}{
{image.NewRGBA(image.Rect(0, 0, 10, 10)), []PHash{}, nil, nil},
{image.NewRGBA(image.Rect(0, 0, 10, 10)), []PHash{}, nil, fmt.Errorf("Some test error")},
}
for i, test := range tests {
testIndex := newTestIndex()
testIndex.err = test.expectedSearchErr
testIndex.matches = test.expectedMatches
db := NewDB(testIndex)
db.hasher = func(image.Image) (PHash, error) { return PHash(0), test.expectedHashErr }
matches, err := db.Search(test.img, 0)
if test.expectedHashErr != nil && err != test.expectedHashErr {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedHashErr, err)
} else if test.expectedSearchErr != nil && err != test.expectedSearchErr {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedSearchErr, err)
}
if !reflect.DeepEqual(test.expectedMatches, matches) {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedMatches, matches)
}
}
}
func TestDBSearchByFile(t *testing.T) {
tests := []struct {
expectedMatches []PHash
expectedHashErr error
expectedSearchErr error
}{
{[]PHash{}, nil, nil},
{[]PHash{}, nil, fmt.Errorf("Some test error")},
}
for i, test := range tests {
testIndex := newTestIndex()
testIndex.err = test.expectedSearchErr
testIndex.matches = test.expectedMatches
db := NewDB(testIndex)
db.hasher = func(image.Image) (PHash, error) { return PHash(0), test.expectedHashErr }
image := image.NewAlpha(image.Rect(0, 0, 1, 1))
buf := bytes.NewBuffer([]byte{})
png.Encode(buf, image)
matches, err := db.SearchByFile(buf, 0)
if test.expectedHashErr != nil && err != test.expectedHashErr {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedHashErr, err)
} else if test.expectedSearchErr != nil && err != test.expectedSearchErr {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedSearchErr, err)
}
if !reflect.DeepEqual(test.expectedMatches, matches) {
t.Errorf("tests[%d] expected %v got %v", i, test.expectedMatches, matches)
}
}
}
/*
func BenchmarkLinearIndexAdd10(b *testing.B) { benchmarkAdd(b, NewLinearIndex(), 10) }
func BenchmarkLinearIndexAdd100(b *testing.B) { benchmarkAdd(b, NewLinearIndex(), 100) }
func BenchmarkLinearIndexAdd1000(b *testing.B) { benchmarkAdd(b, NewLinearIndex(), 1000) }
func BenchmarkLinearIndexAdd10000(b *testing.B) { benchmarkAdd(b, NewLinearIndex(), 10000) }
func BenchmarkLinearIndexSearch10(b *testing.B) { benchmarkSearch(b, NewLinearIndex(), 10) }
func BenchmarkLinearIndexSearch100(b *testing.B) { benchmarkSearch(b, NewLinearIndex(), 100) }
func BenchmarkLinearIndexSearch1000(b *testing.B) { benchmarkSearch(b, NewLinearIndex(), 1000) }
func BenchmarkLinearIndexSearch10000(b *testing.B) { benchmarkSearch(b, NewLinearIndex(), 10000) }
func BenchmarkRadixIndexAdd10(b *testing.B) { benchmarkAdd(b, NewRadixIndex(), 10) }
func BenchmarkRadixIndexAdd100(b *testing.B) { benchmarkAdd(b, NewRadixIndex(), 100) }
func BenchmarkRadixIndexAdd1000(b *testing.B) { benchmarkAdd(b, NewRadixIndex(), 1000) }
func BenchmarkRadixIndexAdd10000(b *testing.B) { benchmarkAdd(b, NewRadixIndex(), 10000) }
func BenchmarkRadixIndexSearch10(b *testing.B) { benchmarkSearch(b, NewRadixIndex(), 10) }
func BenchmarkRadixIndexSearch100(b *testing.B) { benchmarkSearch(b, NewRadixIndex(), 100) }
func BenchmarkRadixIndexSearch1000(b *testing.B) { benchmarkSearch(b, NewRadixIndex(), 1000) }
func BenchmarkRadixIndexSearch10000(b *testing.B) { benchmarkSearch(b, NewRadixIndex(), 10000) }
*/