forked from peterhellberg/imagefile
/
farbfeld_test.go
112 lines (100 loc) · 2.19 KB
/
farbfeld_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
package farbfeld
import (
"bytes"
"crypto/rand"
"image"
"image/png"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"testing/quick"
)
func Test(t *testing.T) {
files, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, fi := range files {
name := fi.Name()
if !strings.HasSuffix(name, ".png") {
continue
}
f, err := os.Open("testdata/" + name)
if err != nil {
t.Errorf("failed to open file %q", "testdata/"+name)
continue
}
want, err := ioutil.ReadFile("testdata/" + name + ".ff")
if err != nil {
t.Errorf("failed to open file %q", "testdata/"+name+".ff")
continue
}
img, err := png.Decode(f)
if err != nil {
t.Errorf("failed to decode png %q: %v", "testdata/"+name, err)
continue
}
var buf bytes.Buffer
err = Encode(&buf, img)
if err != nil {
t.Errorf("failed to encode farbfeld: %v", err)
continue
}
if !bytes.Equal(buf.Bytes(), want) {
t.Errorf("invalid output for input file %q", "testdata/"+name)
continue
}
}
}
func TestQuickCheck(t *testing.T) {
f := func(w, h uint8, pix [1 << 14]byte) bool {
if w == 0 || h == 0 {
return true
}
img1 := image.NewNRGBA64(image.Rect(0, 0, int(w>>2), int(h>>2)))
copy(img1.Pix, pix[:])
var buf bytes.Buffer
if Encode(&buf, img1) != nil {
return false
}
img2raw, err := Decode(&buf)
if err != nil {
return false
}
img2 := img2raw.(*image.NRGBA64)
return bytes.Equal(img1.Pix, img2.Pix)
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func BenchmarkEncodeNRGBA64(b *testing.B) {
img := image.NewNRGBA64(image.Rect(0, 0, 1<<10, 1<<10))
io.ReadFull(rand.Reader, img.Pix)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img)
}
}
func BenchmarkEncodeRGBA64(b *testing.B) {
img := image.NewRGBA64(image.Rect(0, 0, 1<<10, 1<<10))
io.ReadFull(rand.Reader, img.Pix)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img)
}
}
func BenchmarkDecode(b *testing.B) {
img := image.NewNRGBA64(image.Rect(0, 0, 256, 256))
io.ReadFull(rand.Reader, img.Pix)
var buf bytes.Buffer
Encode(&buf, img)
b.ResetTimer()
var r io.Reader
for i := 0; i < b.N; i++ {
r = bytes.NewReader(buf.Bytes())
Decode(r)
}
}