|
| 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