-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_graphics.p8
493 lines (435 loc) · 8.44 KB
/
test_graphics.p8
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
pico-8 cartridge // http://www.pico-8.com
version 18
__lua__
-- Unit tests for the graphics module
-- by sparr
-- to run the tests use `pico8 -x tests/test_graphics.p8`
-- includes required for testing
#include ../pico8lib/class.lua
#include ../pico8lib/strings.lua
#include ../pico8lib/log.lua
#include ../pico8lib/functions.lua
#include ../pico8lib/tables.lua
#include ../pico8lib/test.lua
-- file being tested
#include ../pico8lib/graphics.lua
local function check_pixels(test, x, y, pixelstrings)
for i=x,x+#pixelstrings[1]-1 do
for j=y,y+#pixelstrings-1 do
c = pget( i, j )
e = tonum( "0x" .. pixelstrings[j-y+1][i-x+1] )
test:assert_equal( c, e, "color at " .. tostr(i) .. "," .. tostr(j) .. " is " .. tostr(c) .. ", expected " .. tostr(e) )
end
end
end
local function compare_pixels(test, x1, y1, x2, y2, w, h)
for i=1,w do
for j=1,h do
c = pget( x1+i-1, y1+j-1 )
e = pget( x2+i-1, y2+j-1 )
test:assert_equal( c, e, "color at " .. tostr(x1+i-1) .. "," .. tostr(y1+j-1) .. " is " .. tostr(c) .. ", expected " .. tostr(e) )
end
end
end
local function reset()
cls()
pal()
end
local suite = TestSuite("graphics.p8")
-- testing the test capabilities
local Meta = TestCase("meta", reset)
function Meta:test_check_pixels ()
pset(1,1,4)
pset(2,3,7)
check_pixels(self, 0,0,{
"0000",
"0400",
"0000",
"0070",
"0000",
})
end
suite:add_test_case(Meta)
-- Line functions
local Line = TestCase("line", reset)
function Line:test_line__diagonal ()
line_(1,1,5,7,7)
check_pixels(self, 0,0,{
"00000000",
"07000000",
"00700000",
"00700000",
"00070000",
"00007000",
"00007000",
"00000700",
"00000000",
})
end
function Line:test_line__horizontal ()
line_(3,72,115,72,7)
check_pixels(self, 2,71,{
"00000000",
"07777777",
"00000000",
})
check_pixels(self, 109,71,{
"00000000",
"77777770",
"00000000",
})
end
function Line:test_line__vertical ()
line_(19,3,19,91,7)
check_pixels(self, 18,2,{
"000",
"070",
"070",
"070",
"070",
"070",
"070",
"070",
})
check_pixels(self, 18,85,{
"070",
"070",
"070",
"070",
"070",
"070",
"070",
"000",
})
end
function Line:test_line_fat ()
line_fat(1,1,5,7,2,3,7)
check_pixels(self, 0,0,{
"00000000",
"07700000",
"07770000",
"07770000",
"00777000",
"00777700",
"00077700",
"00007770",
"00007770",
"00000770",
"00000000",
})
end
function Line:test_line_func ()
expected = {
{1,1,7,1},
{2,2,7,2},
{2,3,7,3},
{3,4,7,4},
{4,5,7,5},
{4,6,7,6},
{5,7,7,7},
}
result = {}
line_func(1,1,5,7,function (x, y, c, p) result[#result+1] = {x,y,c,p} end,7)
for y,row in pairs(result) do
for x,val in pairs(row) do
self:assert_equal(expected[y][x], val)
end
end
end
suite:add_test_case(Line)
-- Sprite functions
local Sprite = TestCase("sprite", reset)
function Sprite:test_spr4fast ()
spr4_fast(3,1,1)
check_pixels(self, 0,0,{
"000000",
"008880",
"080770",
"077660",
"066550",
"000000",
})
end
function Sprite:test_spr4 ()
spr4(3,1,1)
check_pixels(self, 0,0,{
"000000",
"008880",
"080770",
"077660",
"066550",
"000000",
})
end
function Sprite:test_spr4_shrink ()
spr4(3,1,1,2,2)
check_pixels(self, 0,0,{
"0000",
"0070",
"0650",
"0000",
})
end
function Sprite:test_spr4_expand ()
spr4(3,1,1,6,8)
check_pixels(self, 0,0,{
"00000000",
"00088880",
"00088880",
"08807770",
"08807770",
"07776660",
"07776660",
"06665550",
"06665550",
"00000000",
})
end
function Sprite:test_spr4_fliph ()
spr4(3,1,1,4,4,true)
check_pixels(self, 0,0,{
"000000",
"088800",
"077080",
"066770",
"055660",
"000000",
})
end
function Sprite:test_spr4_flipv ()
spr4(3,1,1,4,4,false,true)
check_pixels(self, 0,0,{
"000000",
"066550",
"077660",
"080770",
"008880",
"000000",
})
end
suite:add_test_case(Sprite)
-- Circle functions
local Circle = TestCase("circle", reset)
function Circle:test_circ_even_1 ()
circ_even(1,1,1,1,7)
check_pixels(self, 0,0,{
"0000",
"0770",
"0770",
"0000",
})
end
function Circle:test_circ_even_2 ()
circ_even(1,1,1,2,7)
check_pixels(self, 0,0,{
"000000",
"007700",
"070070",
"070070",
"007700",
"000000",
})
end
function Circle:test_circ_even_8 ()
circ_even(1,1,1,8,7)
check_pixels(self, 0,0,{
"000000000000000000",
"000000777777000000",
"000077000000770000",
"000700000000007000",
"007000000000000700",
"007000000000000700",
"070000000000000070",
"070000000000000070",
"070000000000000070",
"070000000000000070",
"070000000000000070",
"070000000000000070",
"007000000000000700",
"007000000000000700",
"000700000000007000",
"000077000000770000",
"000000777777000000",
"000000000000000000",
})
end
function Circle:test_circ_even_small_1 ()
circ_even_small(3,1,1,1,7)
check_pixels(self, 0,0,{
"0000",
"0770",
"0770",
"0000",
})
end
function Circle:test_circ_even_small_2 ()
circ_even_small(3,1,1,2,7)
check_pixels(self, 0,0,{
"000000",
"007700",
"070070",
"070070",
"007700",
"000000",
})
end
function Circle:test_circ_even_small_4 ()
circ_even_small(3,1,1,4,7)
check_pixels(self, 0,0,{
"0000000000",
"0007777000",
"0070000700",
"0700000070",
"0700000070",
"0700000070",
"0700000070",
"0070000700",
"0007777000",
"0000000000",
})
end
function Circle:test_circfill_even_1 ()
circfill_even(2,1,1,1,7)
check_pixels(self, 0,0,{
"0000",
"0770",
"0770",
"0000",
})
end
function Circle:test_circfill_even_3 ()
circfill_even(2,1,1,3,7)
check_pixels(self, 0,0,{
"00000000",
"00777700",
"07777770",
"07777770",
"07777770",
"07777770",
"00777700",
"00000000",
})
end
function Circle:test_circfill_even_8 ()
circfill_even(2,1,1,8,7)
check_pixels(self, 0,0,{
"000000000000000000",
"000000777777000000",
"000077777777770000",
"000777777777777000",
"007777777777777700",
"007777777777777700",
"077777777777777770",
"077777777777777770",
"077777777777777770",
"077777777777777770",
"077777777777777770",
"077777777777777770",
"007777777777777700",
"007777777777777700",
"000777777777777000",
"000077777777770000",
"000000777777000000",
"000000000000000000",
})
end
function Circle:test_circfill_even_small_1 ()
circfill_even_small(4,1,1,1,7)
check_pixels(self, 0,0,{
"0000",
"0770",
"0770",
"0000",
})
end
function Circle:test_circfill_even_small_3 ()
circfill_even_small(4,1,1,3,7)
check_pixels(self, 0,0,{
"00000000",
"00777700",
"07777770",
"07777770",
"07777770",
"07777770",
"00777700",
"00000000",
})
end
function Circle:test_circfill_even_small_4 ()
circfill_even_small(4,1,1,4,7)
check_pixels(self, 0,0,{
"0000000000",
"0007777000",
"0077777700",
"0777777770",
"0777777770",
"0777777770",
"0777777770",
"0077777700",
"0007777000",
"0000000000",
})
end
suite:add_test_case(Circle)
-- Fill functions
local FillP = TestCase("fillp", reset)
function FillP:test_fillp_no_shift ()
fillp(0b0011110001011001)
rectfill(3,4,9,8)
_fillp(0b0011110001011001)
rectfill(23,24,29,28)
compare_pixels(self, 3, 4, 23, 24, 7, 5)
end
function FillP:test_fillp_x_shift ()
fillp(0b0011110001011001,1,0)
rectfill(3,4,9,8)
_fillp(0b0011110001011001)
rectfill(22,24,28,28)
compare_pixels(self, 3, 4, 22, 24, 7, 5)
end
function FillP:test_fillp_y_shift ()
fillp(0b0011110001011001,0,2)
rectfill(3,4,9,8)
_fillp(0b0011110001011001)
rectfill(23,22,29,26)
compare_pixels(self, 3, 4, 23, 22, 7, 5)
end
function FillP:test_fillp_negative_x_shift ()
fillp(0b0011110001011001,-3,0)
rectfill(3,4,9,8)
_fillp(0b0011110001011001)
rectfill(26,24,32,28)
compare_pixels(self, 3, 4, 26, 24, 7, 5)
end
function FillP:test_fillp_negative_y_shift ()
fillp(0b0011110001011001,0,-1)
rectfill(3,4,9,8)
_fillp(0b0011110001011001)
rectfill(23,25,29,29)
compare_pixels(self, 3, 4, 23, 25, 7, 5)
end
function FillP:test_fillp_shifts ()
fillp(0b0011110001011001,3,-1)
rectfill(3,4,9,8)
_fillp(0b0011110001011001)
rectfill(20,25,26,29)
compare_pixels(self, 3, 4, 20, 25, 7, 5)
end
-- TODO test decimal fill pattern for transparency
suite:add_test_case(FillP)
run_suites{suite}
-- sprites
-- 0 blank
-- 1 circ_even
-- 2 circfill_even
-- 3 circ_even_small
-- 4 circfill_even_small
__gfx__
0000000000000888000008880044440000444400
0000000000088077000888770433334004333340
0000000000807766008877664302203443322334
0000000008076655088766554321123443211234
0000000008760544087665444321123443211234
0000000080765433887654334302203443322334
0000000087654302876543320433334004333340
0000000087654321876543210044440000444400