forked from sysoftheworld/spritesheet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencode_test.go
137 lines (128 loc) · 3.68 KB
/
encode_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
package spritesheet
import (
"image"
"image/color"
"math/rand"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestEncode(t *testing.T) {
tests := []struct {
name string
numOfImgs int
width, height int // per image
opts *EncodeOpts
err error
}{
{name: "no images", numOfImgs: 0, width: 10, height: 10, opts: &EncodeOpts{ImgsPerRow: 5}, err: ErrNoImages},
{name: "100", numOfImgs: 100, width: 10, height: 10, opts: &EncodeOpts{ImgsPerRow: 5}, err: nil},
{name: "100x100", numOfImgs: 10, width: 100, height: 100, opts: &EncodeOpts{ImgsPerRow: 2}, err: nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var (
imgs = make([]image.Image, 0, test.numOfImgs)
r, g, b, a = uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255))
color = color.RGBA{R: r, G: g, B: b, A: a}
point = image.Pt(rand.Intn(test.width), rand.Intn(test.height))
img = buildTestImage(test.width, test.height, color, point)
)
for i := 0; i < test.numOfImgs; i++ {
imgs = append(imgs, img)
}
sheet, err := Encode(imgs, test.opts)
if err != test.err {
t.Errorf("got %v; wanted %v", err, test.err)
return
}
if err != nil {
return
}
var row, column int
for i := 0; i < test.numOfImgs; i++ {
c := sheet.At(point.X+(test.width*column), point.Y+(test.height*row))
if c != color {
t.Errorf("got %v; wanted %v at point %v at row %d column %d", c, color, point, row, column)
}
column++
if column > test.opts.ImgsPerRow-1 {
row++
column = 0
}
}
})
}
}
func TestSheetDimensions(t *testing.T) {
tests := []struct {
name string
numOfImgs, imgsPerRow int
inWidth, inHeight int
outWidth, outHeight int
}{
{name: "empty"},
{name: "zero height", imgsPerRow: 10, numOfImgs: 5, inWidth: 10, inHeight: 0, outWidth: 50, outHeight: 0},
{name: "no remainder", imgsPerRow: 10, numOfImgs: 10, inWidth: 10, inHeight: 10, outWidth: 100, outHeight: 10},
{name: "add a row", imgsPerRow: 10, numOfImgs: 11, inWidth: 10, inHeight: 10, outWidth: 100, outHeight: 20},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.imgsPerRow = imgsPerRow(test.imgsPerRow, test.numOfImgs) // set defaults
w, h := sheetDimensions(test.imgsPerRow, test.numOfImgs, test.inWidth, test.inHeight)
if w != test.outWidth {
t.Errorf("want width %d, got %d", test.outWidth, w)
}
if h != test.outHeight {
t.Errorf("want height %d, got %d", test.outHeight, h)
}
})
}
}
func TestMaxDimensions(t *testing.T) {
tests := []struct {
name string
in []image.Image
width, height int
}{
{name: "nil images", in: nil, width: 0, height: 0},
{name: "nil image", in: []image.Image{nil, nil}, width: 0, height: 0},
{
name: "same size",
in: []image.Image{
image.NewAlpha(image.Rect(0, 0, 100, 100)),
image.NewAlpha(image.Rect(0, 0, 100, 100)),
},
width: 100, height: 100,
},
{
name: "different sizes",
in: []image.Image{
image.NewAlpha(image.Rect(0, 0, 100, 100)),
image.NewAlpha(image.Rect(0, 0, 101, 101)),
},
width: 101, height: 101,
},
{
name: "one from each",
in: []image.Image{
image.NewAlpha(image.Rect(0, 0, 200, 100)),
image.NewAlpha(image.Rect(0, 0, 101, 150)),
},
width: 200, height: 150,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
w, h := maxDimensions(test.in)
if w != test.width {
t.Errorf("want width %d, got %d", test.width, w)
}
if h != test.height {
t.Errorf("want height %d, got %d", test.height, h)
}
})
}
}