Skip to content

Commit 18b86ee

Browse files
authored
Add tests for pixel (#136)
* Add test for putting a pixel * Add test for putting a pixels outside of screen * Add test for putting pixels in screen corners * Add tests for custom clip window * Add test for clip window edges * Add tests for getting pixel out of clip window
1 parent acd4179 commit 18b86ee

2 files changed

Lines changed: 353 additions & 3 deletions

File tree

tests/Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SRCS = \
1717
../src/hagl_triangle.c \
1818
../src/rgb565.c
1919

20-
all: test_fill_polygon test_polygon test_fill_rectangle test_rectangle
20+
all: test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel
2121

2222
test_fill_polygon: test_fill_polygon.c $(SRCS)
2323
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
@@ -31,13 +31,17 @@ test_fill_rectangle: test_fill_rectangle.c $(SRCS)
3131
test_rectangle: test_rectangle.c $(SRCS)
3232
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
3333

34-
test: test_fill_polygon test_polygon test_fill_rectangle test_rectangle
34+
test_pixel: test_pixel.c $(SRCS)
35+
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
36+
37+
test: test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel
3538
./test_fill_polygon
3639
./test_polygon
3740
./test_fill_rectangle
3841
./test_rectangle
42+
./test_pixel
3943

4044
clean:
41-
rm -f test_fill_polygon test_polygon test_fill_rectangle test_rectangle
45+
rm -f test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel
4246

4347
.PHONY: all test clean

tests/test_pixel.c

Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2026 Mika Tuupola
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
-cut-
26+
27+
This file is part of the HAGL graphics library:
28+
https://github.com/tuupola/hagl
29+
30+
SPDX-License-Identifier: MIT
31+
32+
*/
33+
34+
#include <string.h>
35+
36+
#include "greatest.h"
37+
#include "crc32.h"
38+
#include "hagl.h"
39+
40+
static hagl_backend_t backend;
41+
42+
static uint32_t
43+
count_pixels(hagl_backend_t *backend, hagl_color_t color) {
44+
uint32_t count = 0;
45+
for (int16_t y = 0; y < backend->height; y++) {
46+
for (int16_t x = 0; x < backend->width; x++) {
47+
if (hagl_get_pixel(backend, x, y) == color) {
48+
count++;
49+
}
50+
}
51+
}
52+
return count;
53+
}
54+
55+
static void
56+
setup_callback(void *data) {
57+
memset(&backend, 0, sizeof(hagl_backend_t));
58+
hagl_hal_init(&backend);
59+
hagl_set_clip(&backend, 0, 0, backend.width - 1, backend.height - 1);
60+
hagl_clear(&backend);
61+
}
62+
63+
/*
64+
* Single pixel at (100, 120):
65+
*/
66+
TEST
67+
test_put_pixel(void) {
68+
hagl_put_pixel(&backend, 100, 120, 0xFFFF);
69+
70+
/* On pixel: the pixel itself */
71+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 100, 120));
72+
73+
/* Outside: all four neighbors */
74+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 99, 120));
75+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 101, 120));
76+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 100, 119));
77+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 100, 121));
78+
79+
/* Outside: diagonal neighbors */
80+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 99, 119));
81+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 101, 119));
82+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 99, 121));
83+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 101, 121));
84+
85+
/* Total: exactly 1 pixel */
86+
ASSERT_EQ(1, count_pixels(&backend, 0xFFFF));
87+
88+
PASS();
89+
}
90+
91+
TEST
92+
test_put_pixel_regression(void) {
93+
hagl_put_pixel(&backend, 100, 120, 0xFFFF);
94+
95+
size_t size = backend.width * backend.height * (backend.depth / 8);
96+
uint32_t crc = crc32(backend.buffer, size);
97+
98+
ASSERT_EQ(0xDE7EC0EF, crc);
99+
PASS();
100+
}
101+
102+
/*
103+
* Single pixel with a specific color (0x1234):
104+
*/
105+
TEST
106+
test_put_pixel_color(void) {
107+
hagl_put_pixel(&backend, 50, 75, 0x1234);
108+
109+
/* On pixel: correct color returned */
110+
ASSERT_EQ(0x1234, hagl_get_pixel(&backend, 50, 75));
111+
112+
/* Outside: neighbors are black */
113+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 49, 75));
114+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 51, 75));
115+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 50, 74));
116+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 50, 76));
117+
118+
/* Total: exactly 1 pixel of color 0x1234 */
119+
ASSERT_EQ(1, count_pixels(&backend, 0x1234));
120+
121+
/* Total: no white pixels */
122+
ASSERT_EQ(0, count_pixels(&backend, 0xFFFF));
123+
124+
PASS();
125+
}
126+
127+
/*
128+
* Overwrite pixel with a new color:
129+
*/
130+
TEST
131+
test_put_pixel_overwrite(void) {
132+
hagl_put_pixel(&backend, 80, 60, 0xAAAA);
133+
134+
/* On pixel: first color */
135+
ASSERT_EQ(0xAAAA, hagl_get_pixel(&backend, 80, 60));
136+
137+
/* Overwrite with a new color */
138+
hagl_put_pixel(&backend, 80, 60, 0x5555);
139+
140+
/* On pixel: second color replaced the first */
141+
ASSERT_EQ(0x5555, hagl_get_pixel(&backend, 80, 60));
142+
143+
/* Total: no pixels of the old color remain */
144+
ASSERT_EQ(0, count_pixels(&backend, 0xAAAA));
145+
146+
/* Total: exactly 1 pixel of the new color */
147+
ASSERT_EQ(1, count_pixels(&backend, 0x5555));
148+
149+
PASS();
150+
}
151+
152+
/*
153+
* Pixels at all four display corners (320x240):
154+
*
155+
* (0,0)--------------(319,0)
156+
* | |
157+
* | |
158+
* (0,239)------------(319,239)
159+
*/
160+
TEST
161+
test_put_pixel_corners(void) {
162+
hagl_put_pixel(&backend, 0, 0, 0xFFFF);
163+
hagl_put_pixel(&backend, 319, 0, 0xFFFF);
164+
hagl_put_pixel(&backend, 0, 239, 0xFFFF);
165+
hagl_put_pixel(&backend, 319, 239, 0xFFFF);
166+
167+
/* All four corners are set */
168+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 0, 0));
169+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 319, 0));
170+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 0, 239));
171+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 319, 239));
172+
173+
/* Neighbors of top-left */
174+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 1, 0));
175+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 0, 1));
176+
177+
/* Neighbors of top-right */
178+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 318, 0));
179+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 319, 1));
180+
181+
/* Neighbors of bottom-left */
182+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 1, 239));
183+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 0, 238));
184+
185+
/* Neighbors of bottom-right */
186+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 318, 239));
187+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 319, 238));
188+
189+
/* Total: exactly 4 pixels */
190+
ASSERT_EQ(4, count_pixels(&backend, 0xFFFF));
191+
192+
PASS();
193+
}
194+
195+
/*
196+
* Pixels placed outside the display bounds should be discarded.
197+
*/
198+
TEST
199+
test_put_pixel_clip_outside(void) {
200+
/* Left of display */
201+
hagl_put_pixel(&backend, -1, 100, 0xFFFF);
202+
203+
/* Above display */
204+
hagl_put_pixel(&backend, 100, -1, 0xFFFF);
205+
206+
/* Right of display */
207+
hagl_put_pixel(&backend, 320, 100, 0xFFFF);
208+
209+
/* Below display */
210+
hagl_put_pixel(&backend, 100, 240, 0xFFFF);
211+
212+
/* Far outside in both directions */
213+
hagl_put_pixel(&backend, -1000, -1000, 0xFFFF);
214+
hagl_put_pixel(&backend, 1000, 1000, 0xFFFF);
215+
216+
/* No pixels should have been drawn */
217+
ASSERT_EQ(0, count_pixels(&backend, 0xFFFF));
218+
219+
PASS();
220+
}
221+
222+
/*
223+
* Pixels inside a custom clip window are drawn. The clip
224+
* window boundary is inclusive.
225+
*/
226+
TEST
227+
test_put_pixel_custom_clip_inside(void) {
228+
hagl_set_clip(&backend, 50, 50, 100, 100);
229+
230+
/* Interior point */
231+
hagl_put_pixel(&backend, 75, 75, 0xFFFF);
232+
233+
/* All four corners of the clip window */
234+
hagl_put_pixel(&backend, 50, 50, 0xFFFF);
235+
hagl_put_pixel(&backend, 100, 50, 0xFFFF);
236+
hagl_put_pixel(&backend, 50, 100, 0xFFFF);
237+
hagl_put_pixel(&backend, 100, 100, 0xFFFF);
238+
239+
/* On pixel: interior is set */
240+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 75, 75));
241+
242+
/* On pixel: all four clip corners are set */
243+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 50, 50));
244+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 100, 50));
245+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 50, 100));
246+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 100, 100));
247+
248+
/* Total: exactly 5 pixels */
249+
ASSERT_EQ(5, count_pixels(&backend, 0xFFFF));
250+
251+
PASS();
252+
}
253+
254+
/*
255+
* Pixels outside a custom clip window are discarded:
256+
*
257+
* (49,75). (50,50)-------(100,50)
258+
* | |
259+
* (75,49). | |
260+
* | |
261+
* (50,100)------(100,100) .(101,75)
262+
* .
263+
* (75,101)
264+
*/
265+
TEST
266+
test_put_pixel_custom_clip_outside(void) {
267+
hagl_set_clip(&backend, 50, 50, 100, 100);
268+
269+
/* 1 pixel outside each edge of the clip window */
270+
hagl_put_pixel(&backend, 49, 75, 0xFFFF);
271+
hagl_put_pixel(&backend, 75, 49, 0xFFFF);
272+
hagl_put_pixel(&backend, 101, 75, 0xFFFF);
273+
hagl_put_pixel(&backend, 75, 101, 0xFFFF);
274+
275+
/* No pixels should have been drawn */
276+
ASSERT_EQ(0, count_pixels(&backend, 0xFFFF));
277+
278+
PASS();
279+
}
280+
281+
/*
282+
* Reading pixels outside the display bounds returns black.
283+
*/
284+
TEST
285+
test_get_pixel_clip_outside(void) {
286+
/* Place a pixel so the framebuffer is not empty */
287+
hagl_put_pixel(&backend, 100, 120, 0xFFFF);
288+
289+
/* Negative coordinates */
290+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, -1, 0));
291+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 0, -1));
292+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, -1, -1));
293+
294+
/* Beyond display bounds */
295+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 320, 0));
296+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 0, 240));
297+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 320, 240));
298+
299+
/* Far outside */
300+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, -1000, -1000));
301+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 1000, 1000));
302+
303+
PASS();
304+
}
305+
306+
/*
307+
* Reading a pixel that exists in the buffer but is outside
308+
* the current clip window returns black.
309+
*/
310+
TEST
311+
test_get_pixel_custom_clip_outside(void) {
312+
/* Place a pixel with the default clip window */
313+
hagl_put_pixel(&backend, 75, 75, 0xFFFF);
314+
ASSERT_EQ(0xFFFF, hagl_get_pixel(&backend, 75, 75));
315+
316+
/* Shrink the clip window to exclude (75,75) */
317+
hagl_set_clip(&backend, 0, 0, 49, 49);
318+
319+
/* Pixel data exists but is outside the clip window */
320+
ASSERT_EQ(0x0000, hagl_get_pixel(&backend, 75, 75));
321+
322+
PASS();
323+
}
324+
325+
SUITE(pixel_suite) {
326+
SET_SETUP(setup_callback, NULL);
327+
RUN_TEST(test_put_pixel);
328+
RUN_TEST(test_put_pixel_regression);
329+
RUN_TEST(test_put_pixel_color);
330+
RUN_TEST(test_put_pixel_overwrite);
331+
RUN_TEST(test_put_pixel_corners);
332+
RUN_TEST(test_put_pixel_clip_outside);
333+
RUN_TEST(test_put_pixel_custom_clip_inside);
334+
RUN_TEST(test_put_pixel_custom_clip_outside);
335+
RUN_TEST(test_get_pixel_clip_outside);
336+
RUN_TEST(test_get_pixel_custom_clip_outside);
337+
}
338+
339+
GREATEST_MAIN_DEFS();
340+
341+
int
342+
main(int argc, char **argv) {
343+
GREATEST_MAIN_BEGIN();
344+
RUN_SUITE(pixel_suite);
345+
GREATEST_MAIN_END();
346+
}

0 commit comments

Comments
 (0)