-
Notifications
You must be signed in to change notification settings - Fork 7
/
encode_test.go
67 lines (52 loc) · 1.46 KB
/
encode_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
package blurhash_test
import (
"image"
"os"
"path/filepath"
"testing"
"github.com/matryer/is"
"github.com/bbrks/go-blurhash"
)
func TestEncode(t *testing.T) {
for _, test := range testFixtures {
if test.file == "" {
// skip tests without files
continue
}
t.Run(test.hash, func(t *testing.T) {
is := is.New(t)
f, err := os.Open(filepath.FromSlash(test.file))
is.NoErr(err) // error opening test fixture file
defer f.Close()
is.True(f != nil) // file should not be nil
img, _, err := image.Decode(f)
is.NoErr(err) // error decoding image from test fixture
is.True(img != nil) // image should not be nil
hash, err := blurhash.Encode(test.xComp, test.yComp, img)
is.NoErr(err) // error hashing test fixture image
is.Equal(hash, test.hash) // blurhash mismatch
})
}
}
func BenchmarkEncode(b *testing.B) {
for _, test := range testFixtures {
if test.file == "" {
// skip tests without files
continue
}
b.Run(test.hash, func(b *testing.B) {
is := is.New(b)
f, err := os.Open(filepath.FromSlash(test.file))
is.NoErr(err) // error opening test fixture file
defer f.Close()
is.True(f != nil) // file should not be nil
img, _, err := image.Decode(f)
is.NoErr(err) // error decoding image from test fixture
is.True(img != nil) // image should not be nil
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = blurhash.Encode(test.xComp, test.yComp, img)
}
})
}
}