forked from adamchalmers/approx_draw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
140 lines (122 loc) · 3.63 KB
/
server_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
package main
import (
"github.com/stretchr/testify/assert"
"image"
"image/color"
"log"
"os"
"runtime/pprof"
"testing"
)
func blackBox(t *testing.T) (*image.RGBA, color.RGBA) {
img := image.NewRGBA(image.Rect(0, 0, 2, 2))
black := color.RGBA{0, 0, 0, 255}
err := mutate(img, mutation{0, 0, 2, 2, black})
assert.Nil(t, err)
return img, black
}
func whiteBox(t *testing.T) (*image.RGBA, color.RGBA) {
img := image.NewRGBA(image.Rect(0, 0, 2, 2))
white := color.RGBA{255, 255, 255, 255}
err := mutate(img, mutation{0, 0, 2, 2, white})
assert.Nil(t, err)
return img, white
}
func TestCanary(t *testing.T) {
assert.True(t, true, "True is true!")
}
func TestAbs(t *testing.T) {
x := uint8(255)
y := uint8(100)
assert.Equal(t, 155, abs(x, y))
assert.Equal(t, 155, abs(y, x))
}
func TestMutate(t *testing.T) {
// Make a 2x2 black square.
img, black := blackBox(t)
// Check all four pixels are really black.
assert.Equal(t, black, img.At(0, 0))
assert.Equal(t, black, img.At(0, 1))
assert.Equal(t, black, img.At(1, 0))
assert.Equal(t, black, img.At(1, 1))
// Mutate the bottom-right square white and check it.
white := color.RGBA{255, 255, 255, 255}
errr := mutate(img, mutation{1, 1, 1, 1, white})
assert.Nil(t, errr)
assert.Equal(t, black, img.At(0, 0))
assert.Equal(t, black, img.At(0, 1))
assert.Equal(t, black, img.At(1, 0))
assert.Equal(t, white, img.At(1, 1))
}
func TestFailedMutateCanvas(t *testing.T) {
img, black := blackBox(t)
errWide := mutate(img, mutation{1, 1, 1, 4, black})
assert.NotNil(t, errWide)
errHigh := mutate(img, mutation{1, 1, 4, 1, black})
assert.NotNil(t, errHigh)
}
func TestColorDist(t *testing.T) {
black := color.RGBA{0, 0, 0, 255}
grey := color.RGBA{100, 110, 120, 255}
assert.Equal(t, 330, colorDist(black, grey))
assert.Equal(t, 330, colorDist(grey, black))
}
func TestImgDistMutation(t *testing.T) {
imgBlack, _ := blackBox(t)
imgWhite, white := whiteBox(t)
score, err := imgDist(imgBlack, imgWhite)
assert.Nil(t, err)
tryScore := imgDistMutated(imgBlack, imgWhite, score, mutation{1, 1, 1, 1, white}, 1)
expected := 255 * 3 * 3
assert.Equal(t, expected, tryScore)
}
func TestImgDist(t *testing.T) {
imgBlack, _ := blackBox(t)
imgWhite := image.NewRGBA(image.Rect(0, 0, 2, 2))
white := color.RGBA{255, 255, 255, 255}
err := mutate(imgWhite, mutation{0, 0, 2, 2, white})
assert.Nil(t, err)
expected := 255 * 3 * 4
// Compare the two same-size images
score, err := imgDist(imgBlack, imgWhite)
assert.Nil(t, err)
assert.Equal(t, expected, score)
// Test the inverse - should be exact same.
score2, err2 := imgDist(imgWhite, imgBlack)
assert.Nil(t, err2)
assert.Equal(t, expected, score2)
}
// Benchmark the speed of generating an approximate image.
func BenchmarkApproxing(b *testing.B) {
// Load the file to an image.RGBA
target, err := os.Open("./img/pat.png")
if err != nil {
log.Fatal("Couldn't open test file.")
}
_img, _, err := image.Decode(target)
if err != nil {
log.Fatal("Couldn't decodetest file.")
}
img := toRGBA(_img)
// Actually run the benchmark
f, _ := os.Create("test1_approxdraw.cpuprofile")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
for n := 0; n < b.N; n++ {
approximate(img, 20, 2000, 2)
}
// to profile this benchmark:
// $ go test -c && ./approx_draw.test -test.bench=.
// $ go tool pprof approx_draw.test test1_approxdraw.cpuprofile
}
func BenchmarkColorDist(b *testing.B) {
black := color.RGBA{0, 0, 0, 255}
white := color.RGBA{255, 255, 255, 255}
f, _ := os.Create("colordist_approxdraw.cpuprofile")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
for n := 0; n < b.N; n++ {
colorDist(black, white)
colorDist(white, black)
}
}