Skip to content

Commit 8484da5

Browse files
authored
Add tests for color (#171)
1 parent 8ed64b0 commit 8484da5

2 files changed

Lines changed: 135 additions & 3 deletions

File tree

tests/Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SRCS = \
1818
../src/hagl_blit.c \
1919
../src/rgb565.c
2020

21-
all: test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel test_line test_hline test_vline test_circle test_ellipse test_fill_ellipse test_clip test_blit test_fontx test_char test_fps test_aps
21+
all: test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel test_line test_hline test_vline test_circle test_ellipse test_fill_ellipse test_clip test_blit test_fontx test_char test_fps test_aps test_color
2222

2323
test_fill_polygon: test_fill_polygon.c save_image.c $(SRCS)
2424
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
@@ -71,7 +71,10 @@ test_fps: test_fps.c
7171
test_aps: test_aps.c
7272
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
7373

74-
test: test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel test_line test_hline test_vline test_circle test_ellipse test_fill_ellipse test_clip test_blit test_fontx test_char test_fps test_aps
74+
test_color: test_color.c ../src/hagl_color.c ../src/hagl_bitmap.c ../src/rgb565.c
75+
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
76+
77+
test: test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel test_line test_hline test_vline test_circle test_ellipse test_fill_ellipse test_clip test_blit test_fontx test_char test_fps test_aps test_color
7578
./test_fill_polygon
7679
./test_polygon
7780
./test_fill_rectangle
@@ -88,9 +91,11 @@ test: test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pix
8891
./test_fontx
8992
./test_char
9093
./test_fps
94+
./test_aps
95+
./test_color
9196

9297
clean:
93-
rm -f test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel test_line test_hline test_vline test_circle test_ellipse test_fill_ellipse test_clip test_blit test_fontx test_char test_fps test_aps
98+
rm -f test_fill_polygon test_polygon test_fill_rectangle test_rectangle test_pixel test_line test_hline test_vline test_circle test_ellipse test_fill_ellipse test_clip test_blit test_fontx test_char test_fps test_aps test_color
9499
rm -rf output
95100

96101
.PHONY: all test clean

tests/test_color.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 <stdint.h>
35+
36+
#include "greatest.h"
37+
#include "hagl/bitmap.h"
38+
#include "hagl/color.h"
39+
#include "rgb565.h"
40+
41+
#define TEST_WIDTH 320
42+
#define TEST_HEIGHT 240
43+
#define TEST_DEPTH 16
44+
45+
static hagl_bitmap_t bitmap;
46+
static uint8_t buffer[TEST_WIDTH * TEST_HEIGHT * (TEST_DEPTH / 8)];
47+
48+
static uint8_t last_r, last_g, last_b;
49+
static hagl_color_t mock_color_result;
50+
51+
static hagl_color_t mock_color(void *self, uint8_t r, uint8_t g, uint8_t b) {
52+
last_r = r;
53+
last_g = g;
54+
last_b = b;
55+
return mock_color_result;
56+
}
57+
58+
static void setup_callback(void *data) {
59+
memset(buffer, 0, sizeof(buffer));
60+
hagl_bitmap_init(&bitmap, TEST_WIDTH, TEST_HEIGHT, TEST_DEPTH, buffer);
61+
last_r = last_g = last_b = 0;
62+
mock_color_result = 0;
63+
}
64+
65+
TEST test_color_default_black(void) {
66+
bitmap.color = NULL;
67+
68+
hagl_color_t result = hagl_color(&bitmap, 0, 0, 0);
69+
hagl_color_t expected = rgb565(0, 0, 0);
70+
71+
ASSERT_EQ(expected, result);
72+
73+
PASS();
74+
}
75+
76+
TEST test_color_default_white(void) {
77+
bitmap.color = NULL;
78+
79+
hagl_color_t result = hagl_color(&bitmap, 255, 255, 255);
80+
hagl_color_t expected = rgb565(255, 255, 255);
81+
82+
ASSERT_EQ(expected, result);
83+
84+
PASS();
85+
}
86+
87+
TEST test_color_fallback_arbitrary(void) {
88+
bitmap.color = NULL;
89+
90+
uint8_t r = 128, g = 64, b = 32;
91+
hagl_color_t result = hagl_color(&bitmap, r, g, b);
92+
hagl_color_t expected = rgb565(r, g, b);
93+
94+
ASSERT_EQ(expected, result);
95+
96+
PASS();
97+
}
98+
99+
TEST test_color_delegates_to_surface(void) {
100+
bitmap.color = mock_color;
101+
mock_color_result = 0x1234;
102+
103+
hagl_color_t result = hagl_color(&bitmap, 100, 150, 200);
104+
105+
ASSERT_EQ(100, last_r);
106+
ASSERT_EQ(150, last_g);
107+
ASSERT_EQ(200, last_b);
108+
ASSERT_EQ(0x1234, result);
109+
110+
PASS();
111+
}
112+
113+
SUITE(color_suite) {
114+
SET_SETUP(setup_callback, NULL);
115+
RUN_TEST(test_color_default_black);
116+
RUN_TEST(test_color_default_white);
117+
RUN_TEST(test_color_fallback_arbitrary);
118+
RUN_TEST(test_color_delegates_to_surface);
119+
}
120+
121+
GREATEST_MAIN_DEFS();
122+
123+
int main(int argc, char **argv) {
124+
GREATEST_MAIN_BEGIN();
125+
RUN_SUITE(color_suite);
126+
GREATEST_MAIN_END();
127+
}

0 commit comments

Comments
 (0)