-
Notifications
You must be signed in to change notification settings - Fork 0
/
webp_test.go
87 lines (81 loc) · 1.68 KB
/
webp_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
package webp
import (
"bytes"
"image"
"image/color"
"testing"
)
func TestEncode(t *testing.T) {
tests := []struct {
name string
img image.Image
quality float32
}{
{
name: "empty image",
img: image.NewRGBA(image.Rect(0, 0, 0, 0)),
quality: float32(75.0),
},
{
name: "small image",
img: image.NewRGBA(image.Rect(0, 0, 2, 2)),
quality: float32(75.0),
},
{
name: "large image",
img: randomImage(500, 500),
quality: float32(75.0),
},
{
name: "high quality",
img: randomImage(100, 100),
quality: float32(100.0),
},
{
name: "low quality",
img: randomImage(100, 100),
quality: float32(10.0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
err := Encode(tt.img, tt.quality, buf)
if err != nil {
t.Fatalf("Encode() error = %v, wantErr = %v", err, false)
}
})
}
}
func TestEncode_EmptyImage(t *testing.T) {
tests := []struct {
name string
img image.Image
quality float32
}{
{
name: "nil image",
img: nil,
quality: float32(75.0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
err := Encode(tt.img, tt.quality, buf)
if err == nil {
t.Fatalf("Encode() error = %v, wantErr = %v", err, true)
}
})
}
}
// This function generates an image with random colors
func randomImage(width, height int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, color.RGBA{R: uint8(x % 256), G: uint8(y % 256), B: uint8((x + y) % 256), A: 255})
}
}
return img
}