forked from disintegration/imaging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
340 lines (320 loc) · 5.54 KB
/
utils_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package imaging
import (
"image"
"math"
"runtime"
"sync/atomic"
"testing"
)
var (
testdataBranchesJPG = mustOpen("testdata/branches.jpg")
testdataBranchesPNG = mustOpen("testdata/branches.png")
testdataFlowersSmallPNG = mustOpen("testdata/flowers_small.png")
)
func mustOpen(filename string) image.Image {
img, err := Open(filename)
if err != nil {
panic(err)
}
return img
}
func TestParallel(t *testing.T) {
for _, n := range []int{0, 1, 10, 100, 1000} {
for _, p := range []int{1, 2, 4, 8, 16, 100} {
if !testParallelN(n, p) {
t.Fatalf("test [parallel %d %d] failed", n, p)
}
}
}
}
func testParallelN(n, procs int) bool {
data := make([]bool, n)
before := runtime.GOMAXPROCS(0)
runtime.GOMAXPROCS(procs)
parallel(0, n, func(is <-chan int) {
for i := range is {
data[i] = true
}
})
runtime.GOMAXPROCS(before)
for i := 0; i < n; i++ {
if !data[i] {
return false
}
}
return true
}
func TestParallelMaxProcs(t *testing.T) {
for _, n := range []int{0, 1, 10, 100, 1000} {
for _, p := range []int{1, 2, 4, 8, 16, 100} {
if !testParallelMaxProcsN(n, p) {
t.Fatalf("test [parallel max procs %d %d] failed", n, p)
}
}
}
}
func testParallelMaxProcsN(n, procs int) bool {
data := make([]bool, n)
SetMaxProcs(procs)
parallel(0, n, func(is <-chan int) {
for i := range is {
data[i] = true
}
})
SetMaxProcs(0)
for i := 0; i < n; i++ {
if !data[i] {
return false
}
}
return true
}
func TestSetMaxProcs(t *testing.T) {
for _, p := range []int{-1, 0, 10} {
SetMaxProcs(p)
if int(atomic.LoadInt64(&maxProcs)) != p {
t.Fatalf("test [set max procs %d] failed", p)
}
}
SetMaxProcs(0)
}
func TestClamp(t *testing.T) {
testCases := []struct {
f float64
u uint8
}{
{0, 0},
{255, 255},
{128, 128},
{0.49, 0},
{0.50, 1},
{254.9, 255},
{254.0, 254},
{256, 255},
{2500, 255},
{-10, 0},
{127.6, 128},
}
for _, tc := range testCases {
if clamp(tc.f) != tc.u {
t.Fatalf("test [clamp %v %v] failed: %v", tc.f, tc.u, clamp(tc.f))
}
}
}
func TestReverse(t *testing.T) {
testCases := []struct {
pix []uint8
want []uint8
}{
{
pix: []uint8{},
want: []uint8{},
},
{
pix: []uint8{1, 2, 3, 4},
want: []uint8{1, 2, 3, 4},
},
{
pix: []uint8{1, 2, 3, 4, 5, 6, 7, 8},
want: []uint8{5, 6, 7, 8, 1, 2, 3, 4},
},
{
pix: []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
want: []uint8{9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4},
},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
reverse(tc.pix)
if !compareBytes(tc.pix, tc.want, 0) {
t.Fatalf("got pix %v want %v", tc.pix, tc.want)
}
})
}
}
func compareNRGBA(img1, img2 *image.NRGBA, delta int) bool {
if !img1.Rect.Eq(img2.Rect) {
return false
}
return compareBytes(img1.Pix, img2.Pix, delta)
}
func compareBytes(a, b []uint8, delta int) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if absint(int(a[i])-int(b[i])) > delta {
return false
}
}
return true
}
// compareNRGBAGolden is a special version of compareNRGBA used in golden tests.
// All the golden images are generated on amd64 architecture. Due to differences
// in floating-point rounding on different architectures, we need to add some
// level of tolerance when comparing images on architectures other than amd64.
// See https://golang.org/ref/spec#Floating_point_operators for information on
// fused multiply and add (FMA) instruction.
func compareNRGBAGolden(img1, img2 *image.NRGBA) bool {
delta := 0
if runtime.GOARCH != "amd64" {
delta = 1
}
return compareNRGBA(img1, img2, delta)
}
func compareFloat64(a, b, delta float64) bool {
return math.Abs(a-b) <= delta
}
var rgbHSLTestCases = []struct {
r, g, b uint8
h, s, l float64
}{
{
r: 255,
g: 0,
b: 0,
h: 0.000,
s: 1.000,
l: 0.500,
},
{
r: 191,
g: 191,
b: 0,
h: 0.167,
s: 1.000,
l: 0.375,
},
{
r: 0,
g: 128,
b: 0,
h: 0.333,
s: 1.000,
l: 0.251,
},
{
r: 128,
g: 255,
b: 255,
h: 0.500,
s: 1.000,
l: 0.751,
},
{
r: 128,
g: 128,
b: 255,
h: 0.667,
s: 1.000,
l: 0.751,
},
{
r: 191,
g: 64,
b: 191,
h: 0.833,
s: 0.498,
l: 0.500,
},
{
r: 160,
g: 164,
b: 36,
h: 0.172,
s: 0.640,
l: 0.392,
},
{
r: 65,
g: 27,
b: 234,
h: 0.697,
s: 0.831,
l: 0.512,
},
{
r: 30,
g: 172,
b: 65,
h: 0.374,
s: 0.703,
l: 0.396,
},
{
r: 240,
g: 200,
b: 14,
h: 0.137,
s: 0.890,
l: 0.498,
},
{
r: 180,
g: 48,
b: 229,
h: 0.788,
s: 0.777,
l: 0.543,
},
{
r: 237,
g: 119,
b: 81,
h: 0.040,
s: 0.813,
l: 0.624,
},
{
r: 254,
g: 248,
b: 136,
h: 0.158,
s: 0.983,
l: 0.765,
},
{
r: 25,
g: 203,
b: 151,
h: 0.451,
s: 0.781,
l: 0.447,
},
{
r: 54,
g: 38,
b: 152,
h: 0.690,
s: 0.600,
l: 0.373,
},
{
r: 126,
g: 126,
b: 184,
h: 0.667,
s: 0.290,
l: 0.608,
},
}
func TestRGBToHSL(t *testing.T) {
for _, tc := range rgbHSLTestCases {
t.Run("", func(t *testing.T) {
h, s, l := rgbToHSL(tc.r, tc.g, tc.b)
if !compareFloat64(h, tc.h, 0.001) || !compareFloat64(s, tc.s, 0.001) || !compareFloat64(l, tc.l, 0.001) {
t.Fatalf("(%d, %d, %d): got (%.3f, %.3f, %.3f) want (%.3f, %.3f, %.3f)", tc.r, tc.g, tc.b, h, s, l, tc.h, tc.s, tc.l)
}
})
}
}
func TestHSLToRGB(t *testing.T) {
for _, tc := range rgbHSLTestCases {
t.Run("", func(t *testing.T) {
r, g, b := hslToRGB(tc.h, tc.s, tc.l)
if r != tc.r || g != tc.g || b != tc.b {
t.Fatalf("(%.3f, %.3f, %.3f): got (%d, %d, %d) want (%d, %d, %d)", tc.h, tc.s, tc.l, r, g, b, tc.r, tc.g, tc.b)
}
})
}
}