-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdecode_test.go
More file actions
361 lines (317 loc) · 9.14 KB
/
decode_test.go
File metadata and controls
361 lines (317 loc) · 9.14 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package blurhash_test
import (
"errors"
"image"
"image/png"
"io"
"testing"
"github.com/bbrks/go-blurhash"
"github.com/bbrks/go-blurhash/base83"
)
func TestDecodeRGBA(t *testing.T) {
for _, test := range testFixtures {
// skip tests without hashes
if test.hash == "" {
continue
}
t.Run(test.hash, func(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 32, 32))
err := blurhash.DecodeDraw(img, test.hash, 1)
if err != nil {
t.Fatalf("error decoding: %v", err)
}
err = png.Encode(io.Discard, img)
if err != nil {
t.Fatalf("error encoding png: %v", err)
}
})
}
}
func TestDecode(t *testing.T) {
for _, test := range testFixtures {
// skip tests without hashes
if test.hash == "" {
continue
}
t.Run(test.hash, func(t *testing.T) {
img, err := blurhash.Decode(test.hash, 32, 32, 1)
if err != nil {
t.Fatalf("error decoding: %v", err)
}
err = png.Encode(io.Discard, img)
if err != nil {
t.Fatalf("error encoding png: %v", err)
}
})
}
}
func TestComponents(t *testing.T) {
for _, test := range testFixtures {
// skip tests without expected component values
if test.hash == "" || test.xComp == 0 || test.yComp == 0 {
continue
}
t.Run(test.hash, func(t *testing.T) {
x, y, err := blurhash.Components(test.hash)
if err != nil {
t.Fatalf("unexpected error getting components: %v", err)
}
if x != test.xComp {
t.Errorf("x component mismatch: got %d, want %d", x, test.xComp)
}
if y != test.yComp {
t.Errorf("y component mismatch: got %d, want %d", y, test.yComp)
}
})
}
}
func TestComponentsInvalidHash(t *testing.T) {
t.Run("too short", func(t *testing.T) {
// Hashes shorter than 6 characters should return ErrInvalidHash
shortHashes := []string{"", "A", "ABCDE"}
for _, hash := range shortHashes {
_, _, err := blurhash.Components(hash)
if !errors.Is(err, blurhash.ErrInvalidHash) {
t.Errorf("short hash %q should return ErrInvalidHash, got %v", hash, err)
}
}
})
t.Run("invalid base83 character", func(t *testing.T) {
// '&' is not a valid base83 character
_, _, err := blurhash.Components("&BCDEF")
if err == nil {
t.Fatal("invalid character should return error")
}
if !errors.Is(err, base83.ErrInvalidInput) {
t.Errorf("expected invalid base83 error, got %v", err)
}
})
t.Run("wrong length for components", func(t *testing.T) {
// '9' encodes 1x2 components (sizeFlag=9), expecting 4+2*1*2=8 chars
// but we provide only 6 chars
_, _, err := blurhash.Components("900000")
if !errors.Is(err, blurhash.ErrInvalidHash) {
t.Errorf("wrong length should return ErrInvalidHash, got %v", err)
}
// Valid 1x1 hash is 6 chars, but provide 8
_, _, err = blurhash.Components("00000000")
if !errors.Is(err, blurhash.ErrInvalidHash) {
t.Errorf("wrong length should return ErrInvalidHash, got %v", err)
}
})
t.Run("invalid component count", func(t *testing.T) {
// '}' decodes to base83 value 81, giving y=(81/9)+1=10 which exceeds max of 9
// Found by fuzz testing
_, _, err := blurhash.Components("}00000000000000000000000")
if !errors.Is(err, blurhash.ErrInvalidHash) {
t.Errorf("invalid component count should return ErrInvalidHash, got %v", err)
}
})
}
func TestDecodeInvalidHash(t *testing.T) {
t.Run("oversized DC value", func(t *testing.T) {
// This hash has a DC value that would overflow the sRGB lookup table
// if the red component isn't properly masked to 8 bits.
// Found by fuzz testing - should not panic (decoder masks values defensively).
_, err := blurhash.Decode("10X00000", 32, 32, 1)
// No panic is the success condition; error is acceptable but not required
_ = err
})
t.Run("invalid base83 in hash body", func(t *testing.T) {
// First char is valid but hash body contains space (invalid base83)
_, err := blurhash.Decode("L0000000000000 0000000000000", 32, 32, 1)
if err == nil {
t.Error("invalid base83 character should return error")
}
if !errors.Is(err, base83.ErrInvalidInput) {
t.Errorf("expected base83.ErrInvalidInput, got %v", err)
}
})
}
func TestDecodeDrawSubImage(t *testing.T) {
// Create a larger image and get a sub-image from it
parent := image.NewNRGBA(image.Rect(0, 0, 100, 100))
subRect := image.Rect(10, 20, 42, 52) // 32x32 sub-image at offset (10, 20)
subImg := parent.SubImage(subRect).(*image.NRGBA)
// Decode into the sub-image
err := blurhash.DecodeDraw(subImg, testFixtures[0].hash, 1)
if err != nil {
t.Fatalf("error decoding: %v", err)
}
// Verify pixels were written to the correct location
// The sub-image should have non-zero pixels
hasNonZero := false
for y := subRect.Min.Y; y < subRect.Max.Y; y++ {
for x := subRect.Min.X; x < subRect.Max.X; x++ {
c := parent.NRGBAAt(x, y)
if c.R != 0 || c.G != 0 || c.B != 0 {
hasNonZero = true
break
}
}
}
if !hasNonZero {
t.Error("sub-image should have non-zero pixels")
}
// Verify pixels outside the sub-image are still zero
// Check a few pixels outside the sub-rect
outside := parent.NRGBAAt(0, 0)
if outside.R != 0 {
t.Errorf("pixel outside sub-image R should be zero, got %d", outside.R)
}
if outside.G != 0 {
t.Errorf("pixel outside sub-image G should be zero, got %d", outside.G)
}
if outside.B != 0 {
t.Errorf("pixel outside sub-image B should be zero, got %d", outside.B)
}
}
func TestDecodeInvalidDimensions(t *testing.T) {
tests := []struct {
name string
width int
height int
}{
{"zero width", 0, 32},
{"zero height", 32, 0},
{"both zero", 0, 0},
{"negative width", -1, 32},
{"negative height", 32, -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := blurhash.Decode(testFixtures[0].hash, tt.width, tt.height, 1)
if !errors.Is(err, blurhash.ErrInvalidDimensions) {
t.Errorf("invalid dimensions should return ErrInvalidDimensions, got %v", err)
}
})
}
}
func TestDecoderReuse(t *testing.T) {
// Use a single decoder for all hashes to verify buffer reuse
dec := blurhash.NewDecoder()
// Run multiple iterations to catch any buffer corruption issues
for iter := 0; iter < 3; iter++ {
for _, test := range testFixtures {
t.Run(test.hash, func(t *testing.T) {
// Decode with reusable decoder
got, err := dec.Decode(test.hash, 32, 32, 1)
if err != nil {
t.Fatalf("decode error: %v", err)
}
// Decode with fresh decoder as reference
want, err := blurhash.Decode(test.hash, 32, 32, 1)
if err != nil {
t.Fatalf("reference decode error: %v", err)
}
gotNRGBA := got.(*image.NRGBA)
wantNRGBA := want.(*image.NRGBA)
for i, p := range gotNRGBA.Pix {
if p != wantNRGBA.Pix[i] {
t.Errorf("pixel mismatch at index %d: got %d, want %d", i, p, wantNRGBA.Pix[i])
break
}
}
})
}
}
}
func TestDecoderDrawReuse(t *testing.T) {
// Use a single decoder and destination image for all hashes
dec := blurhash.NewDecoder()
dst := image.NewNRGBA(image.Rect(0, 0, 32, 32))
// Run multiple iterations to catch any buffer corruption issues
for iter := 0; iter < 3; iter++ {
for _, test := range testFixtures {
t.Run(test.hash, func(t *testing.T) {
// Decode with reusable decoder into reusable destination
err := dec.DecodeDraw(dst, test.hash, 1)
if err != nil {
t.Fatalf("decode error: %v", err)
}
// Decode with fresh decoder as reference
want, err := blurhash.Decode(test.hash, 32, 32, 1)
if err != nil {
t.Fatalf("reference decode error: %v", err)
}
wantNRGBA := want.(*image.NRGBA)
for i, p := range dst.Pix {
if p != wantNRGBA.Pix[i] {
t.Errorf("pixel mismatch at index %d: got %d, want %d", i, p, wantNRGBA.Pix[i])
break
}
}
})
}
}
}
func BenchmarkComponents(b *testing.B) {
for _, test := range testFixtures {
// skip tests without hashes
if test.hash == "" {
continue
}
b.Run(test.hash, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _ = blurhash.Components(test.hash)
}
})
}
}
func BenchmarkDecode(b *testing.B) {
for _, test := range testFixtures {
// skip tests without hashes
if test.hash == "" {
continue
}
b.Run(test.hash, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = blurhash.Decode(test.hash, 32, 32, 1)
}
})
}
}
func BenchmarkDecodeDraw(b *testing.B) {
for _, test := range testFixtures {
// skip tests without hashes
if test.hash == "" {
continue
}
b.Run(test.hash, func(b *testing.B) {
dst := image.NewRGBA(image.Rect(0, 0, 32, 32))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = blurhash.DecodeDraw(dst, test.hash, 1)
}
})
}
}
func BenchmarkDecoderReuse(b *testing.B) {
for _, test := range testFixtures {
if test.hash == "" {
continue
}
b.Run(test.hash, func(b *testing.B) {
dec := blurhash.NewDecoder()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = dec.Decode(test.hash, 32, 32, 1)
}
})
}
}
func BenchmarkDecoderDrawReuse(b *testing.B) {
for _, test := range testFixtures {
if test.hash == "" {
continue
}
b.Run(test.hash, func(b *testing.B) {
dec := blurhash.NewDecoder()
dst := image.NewRGBA(image.Rect(0, 0, 32, 32))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = dec.DecodeDraw(dst, test.hash, 1)
}
})
}
}