diff --git a/CMakeLists.txt b/CMakeLists.txt index e7666b9..ecbd659 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,3 +21,6 @@ target_link_libraries("test_wm" ${SDL2_LIBRARY}) add_executable("test_primitives" "${TESTS_DIR}/test_primitives.c" ${LIB_SRC_FILES}) target_link_libraries("test_primitives" ${SDL2_LIBRARY}) + +add_executable("test_surfaces" "${TESTS_DIR}/test_surfaces.c" ${LIB_SRC_FILES}) +target_link_libraries("test_surfaces" ${SDL2_LIBRARY}) diff --git a/test/test_surfaces.c b/test/test_surfaces.c new file mode 100644 index 0000000..d6f7cf4 --- /dev/null +++ b/test/test_surfaces.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +void test_rgba(SURFACE_FLAGS flags) { + SURFACE *surface = surface_create(100, 100, SURFACE_FORMAT_RGBA, flags); + assert(surface != NULL); + + // set + get + { + COLOR color = color_create_rgba(255, 0, 0, 255); + surface_set_pixel(surface, 20, 10, color); + COLOR read_color = surface_get_pixel(surface, 20, 10); + assert(read_color != 0); + assert(color == read_color); + } + + // get (out of bounds, clipped) + { + COLOR read_color = surface_get_pixel(surface, -1, -1); + assert(read_color == 0); + } + + surface_destroy(surface); +} + +void test_rgb(SURFACE_FLAGS flags) { + SURFACE *surface = surface_create(100, 100, SURFACE_FORMAT_RGB, flags); + assert(surface != NULL); + + // set + get + { + COLOR color = color_create_rgb(255, 0, 0); + surface_set_pixel(surface, 20, 10, color); + COLOR read_color = surface_get_pixel(surface, 20, 10); + assert(read_color != 0); + assert(color == read_color); + } + + // get (out of bounds, clipped) + { + COLOR read_color = surface_get_pixel(surface, -1, -1); + assert(read_color == 0); + } + + surface_destroy(surface); +} + +void test_alpha(SURFACE_FLAGS flags) { + SURFACE *surface = surface_create(100, 100, SURFACE_FORMAT_ALPHA, flags); + assert(surface != NULL); + + // set + get + // destination surface is alpha-only. setting RGB/RGBA colors drops the RGB portion + // and keeps only the alpha component, setting it in the destination + { + COLOR color = color_create_rgba(255, 0, 0, 255); + surface_set_pixel(surface, 20, 10, color); + COLOR read_color = surface_get_pixel(surface, 20, 10); + assert(read_color != 0); + assert(color != read_color); + assert(read_color = color_create_rgba(0, 0, 0, 255)); + } + + // get (out of bounds, clipped) + { + COLOR read_color = surface_get_pixel(surface, -1, -1); + assert(read_color == 0); + } + + surface_destroy(surface); +} + +int main(int argc, char **argv) { + test_rgba(SURFACE_FLAGS_NONE); + test_rgb(SURFACE_FLAGS_NONE); + test_alpha(SURFACE_FLAGS_NONE); + + return 0; +}