Debian Codegraph @

utils_test.go

raw symbols SCIP
1 package gift
2
3 import (
4 "bytes"
5 "image"
6 "image/color"
7 "testing"
8 )
9
10 func TestParallelize(t *testing.T) {
11 for _, e := range []int{5, 1} {
12 for _, n := range []int{0, 1, 5, 10, 50, 100, 500, 1000, 5000} {
13 for _, p := range []int{1, 2, 4, 8, 16, 32, 64, 128} {
14 if !testParallelizeN(e, n, p) {
15 t.Fatalf("test [e=%v n=%d p=%d] failed", e, n, p)
16 }
17 }
18 }
19 }
20 }
21
22 func testParallelizeN(workers int, n, procs int) bool {
23 data := make([]int, n)
24 parallelize(workers, 0, n, func(start, stop int) {
25 for i := start; i < stop; i++ {
26 data[i]++
27 }
28 })
29 for i := range n {
30 if data[i] != 1 {
31 return false
32 }
33 }
34 return true
35 }
36
37 func TestSplitRange(t *testing.T) {
38 for count := range 100 {
39 for procs := range 100 {
40 start := -55
41
42 var parts [][2]int
43 splitRange(start, start+count, procs, func(start, stop int) {
44 parts = append(parts, [2]int{start, stop})
45 })
46
47 wantLen := max(procs, 1)
48 if wantLen > count {
49 wantLen = count
50 }
51 if len(parts) != wantLen {
52 t.Fatalf("test [count=%d procs=%d] got len(parts) %d want %d", count, procs, len(parts), wantLen)
53 }
54
55 data := make([]int, count)
56 for _, p := range parts {
57 for i := p[0]; i < p[1]; i++ {
58 data[i-start]++
59 }
60 }
61 for i := range data {
62 if data[i] != 1 {
63 t.Fatalf("test [count=%d procs=%d] got data[%d] == %d want 1", count, procs, i, data[i])
64 }
65 }
66 }
67 }
68 }
69
70 func TestTempImageCopy(t *testing.T) {
71 tmp1 := createTempImage(image.Rect(-1, -2, 1, 2))
72 if !tmp1.Bounds().Eq(image.Rect(-1, -2, 1, 2)) {
73 t.Error("unexpected temp image bounds")
74 }
75 tmp2 := createTempImage(image.Rect(-3, -4, 3, 4))
76 if !tmp2.Bounds().Eq(image.Rect(-3, -4, 3, 4)) {
77 t.Error("unexpected temp image bounds")
78 }
79 copyimage(tmp1, tmp2, nil)
80 }
81
82 func TestSort(t *testing.T) {
83 testData := []struct {
84 a, b []float32
85 }{
86 {
87 []float32{},
88 []float32{},
89 },
90 {
91 []float32{0.1},
92 []float32{0.1},
93 },
94 {
95 []float32{0.4, 0.2, 0.5, -0.5, 0.3, 0.0, 0.1},
96 []float32{-0.5, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5},
97 },
98 {
99 []float32{-10, 10, -20, 20, -30, 30},
100 []float32{-30, -20, -10, 10, 20, 30},
101 },
102 {
103 []float32{
104 0.60, 0.94, 0.66, 0.44, 0.42, 0.69, 0.07, 0.16, 0.10, 0.30,
105 0.52, 0.81, 0.21, 0.38, 0.32, 0.47, 0.28, 0.29, 0.68, 0.22,
106 0.20, 0.36, 0.57, 0.86, 0.29, 0.30, 0.75, 0.21, 0.87, 0.70,
107 },
108 []float32{
109 0.07, 0.10, 0.16, 0.20, 0.21, 0.21, 0.22, 0.28, 0.29, 0.29,
110 0.30, 0.30, 0.32, 0.36, 0.38, 0.42, 0.44, 0.47, 0.52, 0.57,
111 0.60, 0.66, 0.68, 0.69, 0.70, 0.75, 0.81, 0.86, 0.87, 0.94,
112 },
113 },
114 }
115
116 for _, d := range testData {
117 sort(d.a)
118 for i := range d.a {
119 if d.a[i] != d.b[i] {
120 t.Errorf("sort failed: %#v", d.a)
121 }
122 }
123 }
124 }
125
126 func TestDisk(t *testing.T) {
127 testData := []struct {
128 ksize int
129 k []float32
130 }{
131 {
132 -5,
133 []float32{},
134 },
135 {
136 0,
137 []float32{},
138 },
139 {
140 1,
141 []float32{1},
142 },
143 {
144 2,
145 []float32{1},
146 },
147 {
148 3,
149 []float32{
150 0, 1, 0,
151 1, 1, 1,
152 0, 1, 0,
153 },
154 },
155 {
156 4,
157 []float32{
158 0, 1, 0,
159 1, 1, 1,
160 0, 1, 0,
161 },
162 },
163 {
164 5,
165 []float32{
166 0, 0, 1, 0, 0,
167 0, 1, 1, 1, 0,
168 1, 1, 1, 1, 1,
169 0, 1, 1, 1, 0,
170 0, 0, 1, 0, 0,
171 },
172 },
173 {
174 6,
175 []float32{
176 0, 0, 1, 0, 0,
177 0, 1, 1, 1, 0,
178 1, 1, 1, 1, 1,
179 0, 1, 1, 1, 0,
180 0, 0, 1, 0, 0,
181 },
182 },
183 {
184 7,
185 []float32{
186 0, 0, 0, 1, 0, 0, 0,
187 0, 1, 1, 1, 1, 1, 0,
188 0, 1, 1, 1, 1, 1, 0,
189 1, 1, 1, 1, 1, 1, 1,
190 0, 1, 1, 1, 1, 1, 0,
191 0, 1, 1, 1, 1, 1, 0,
192 0, 0, 0, 1, 0, 0, 0,
193 },
194 },
195 }
196
197 for _, d := range testData {
198 disk := genDisk(d.ksize)
199 for i := range disk {
200 if disk[i] != d.k[i] {
201 t.Errorf("gen disk failed: %d %#v", d.ksize, disk)
202 }
203 }
204 }
205 }
206
207 type customImage struct{}
208
209 func (customImage) ColorModel() color.Model {
210 return color.GrayModel
211 }
212
213 func (customImage) Bounds() image.Rectangle {
214 return image.Rectangle{}
215 }
216
217 func (customImage) At(x, y int) color.Color {
218 return color.Gray{}
219 }
220
221 func TestIsOpaque(t *testing.T) {
222 type opqt struct {
223 img image.Image
224 opaque bool
225 }
226 var testData []opqt
227
228 testData = append(testData, opqt{customImage{}, false})
229 testData = append(testData, opqt{image.NewNRGBA(image.Rect(0, 0, 1, 1)), false})
230 testData = append(testData, opqt{image.NewNRGBA64(image.Rect(0, 0, 1, 1)), false})
231 testData = append(testData, opqt{image.NewRGBA(image.Rect(0, 0, 1, 1)), false})
232 testData = append(testData, opqt{image.NewRGBA64(image.Rect(0, 0, 1, 1)), false})
233 testData = append(testData, opqt{image.NewGray(image.Rect(0, 0, 1, 1)), true})
234 testData = append(testData, opqt{image.NewGray16(image.Rect(0, 0, 1, 1)), true})
235 testData = append(testData, opqt{image.NewYCbCr(image.Rect(0, 0, 1, 1), image.YCbCrSubsampleRatio444), true})
236 testData = append(testData, opqt{image.NewAlpha(image.Rect(0, 0, 1, 1)), false})
237
238 img1 := image.NewNRGBA(image.Rect(0, 0, 1, 1))
239 img1.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
240 testData = append(testData, opqt{img1, true})
241 img2 := image.NewNRGBA64(image.Rect(0, 0, 1, 1))
242 img2.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
243 testData = append(testData, opqt{img2, true})
244 img3 := image.NewRGBA(image.Rect(0, 0, 1, 1))
245 img3.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
246 testData = append(testData, opqt{img3, true})
247 img4 := image.NewRGBA64(image.Rect(0, 0, 1, 1))
248 img4.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
249 testData = append(testData, opqt{img4, true})
250 imgp1 := image.NewPaletted(image.Rect(0, 0, 1, 1), []color.Color{color.NRGBA{0x00, 0x00, 0x00, 0xff}})
251 imgp1.SetColorIndex(0, 0, 0)
252 testData = append(testData, opqt{imgp1, true})
253 imgp2 := image.NewPaletted(image.Rect(0, 0, 1, 1), []color.Color{color.NRGBA{0x00, 0x00, 0x00, 0xfe}})
254 imgp2.SetColorIndex(0, 0, 0)
255 testData = append(testData, opqt{imgp2, false})
256
257 for _, d := range testData {
258 isop := isOpaque(d.img)
259 if isop != d.opaque {
260 t.Errorf("isOpaque failed %#v, %v", d.img, isop)
261 }
262 }
263 }
264
265 func checkBoundsAndPix(b1, b2 image.Rectangle, pix1, pix2 []uint8) bool {
266 if !b1.Eq(b2) {
267 return false
268 }
269 if !bytes.Equal(pix1, pix2) {
270 return false
271 }
272 return true
273 }