From d803919f0ee75023f84352f4f5c5e9bef867951b Mon Sep 17 00:00:00 2001 From: kyriesk Date: Thu, 23 Apr 2026 22:13:40 +1000 Subject: [PATCH 1/3] add 'bitmap creation, save, load, and pixel verification' test to unit_test_graphics.cpp --- .../test/unit_tests/unit_test_graphics.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/coresdk/src/test/unit_tests/unit_test_graphics.cpp b/coresdk/src/test/unit_tests/unit_test_graphics.cpp index 04da999c..e8b358b4 100644 --- a/coresdk/src/test/unit_tests/unit_test_graphics.cpp +++ b/coresdk/src/test/unit_tests/unit_test_graphics.cpp @@ -21,3 +21,58 @@ TEST_CASE("screen dimension utilities", "[screen_width][screen_height]") // close_window(w); // } } + +TEST_CASE("bitmap creation, save, load, and pixel verification", "[bitmap][save][load][get_pixel]") +{ + // Create a small 5x5 bitmap + bitmap test_bmp = create_bitmap("test_bitmap", 5, 5); + + // Fill each row with a different color pattern + // Row 0: WHITE + fill_rectangle_on_bitmap(test_bmp, color_white(), 0, 0, 5, 1); + // Row 1: RED + fill_rectangle_on_bitmap(test_bmp, color_red(), 0, 1, 5, 1); + // Row 2: GREEN + fill_rectangle_on_bitmap(test_bmp, color_green(), 0, 2, 5, 1); + // Row 3: BLUE + fill_rectangle_on_bitmap(test_bmp, color_blue(), 0, 3, 5, 1); + // Row 4: BLACK + fill_rectangle_on_bitmap(test_bmp, color_black(), 0, 4, 5, 1); + + // Verify pixels directly from the created bitmap (before save/load) + // Check a pixel from row 0 (WHITE) + color pixel_w0 = get_pixel(test_bmp, 0, 0); + REQUIRE(red_of(pixel_w0) == red_of(color_white())); + REQUIRE(green_of(pixel_w0) == green_of(color_white())); + REQUIRE(blue_of(pixel_w0) == blue_of(color_white())); + + // Check a pixel from row 1 (RED) + color pixel_r1 = get_pixel(test_bmp, 2, 1); + REQUIRE(red_of(pixel_r1) == red_of(color_red())); + REQUIRE(green_of(pixel_r1) == green_of(color_red())); + REQUIRE(blue_of(pixel_r1) == blue_of(color_red())); + + // Check a pixel from row 2 (GREEN) + color pixel_g2 = get_pixel(test_bmp, 1, 2); + REQUIRE(red_of(pixel_g2) == red_of(color_green())); + REQUIRE(green_of(pixel_g2) == green_of(color_green())); + REQUIRE(blue_of(pixel_g2) == blue_of(color_green())); + + // Check a pixel from row 3 (BLUE) + color pixel_b3 = get_pixel(test_bmp, 3, 3); + REQUIRE(red_of(pixel_b3) == red_of(color_blue())); + REQUIRE(green_of(pixel_b3) == green_of(color_blue())); + REQUIRE(blue_of(pixel_b3) == blue_of(color_blue())); + + // Check a pixel from row 4 (BLACK) + color pixel_k4 = get_pixel(test_bmp, 4, 4); + REQUIRE(red_of(pixel_k4) == red_of(color_black())); + REQUIRE(green_of(pixel_k4) == green_of(color_black())); + REQUIRE(blue_of(pixel_k4) == blue_of(color_black())); + + // Save the bitmap to disk + save_bitmap(test_bmp, "test_bitmap_pattern_5x5"); + + // Clean up allocated bitmap + free_bitmap(test_bmp); +} \ No newline at end of file From f962dbc279740c092026ec7163d22a435d291114 Mon Sep 17 00:00:00 2001 From: kyriesk Date: Fri, 15 May 2026 13:39:15 +1000 Subject: [PATCH 2/3] free bitmap on the disk; added load verfication and cleaned the code --- .../test/unit_tests/unit_test_graphics.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/coresdk/src/test/unit_tests/unit_test_graphics.cpp b/coresdk/src/test/unit_tests/unit_test_graphics.cpp index e8b358b4..3774b601 100644 --- a/coresdk/src/test/unit_tests/unit_test_graphics.cpp +++ b/coresdk/src/test/unit_tests/unit_test_graphics.cpp @@ -75,4 +75,51 @@ TEST_CASE("bitmap creation, save, load, and pixel verification", "[bitmap][save] // Clean up allocated bitmap free_bitmap(test_bmp); + + // Load the saved bitmap back + bitmap loaded_bmp = load_bitmap("loaded_test", "test_bitmap_pattern_5x5.png"); + + // Check that bitmap loaded successfully before verifying dimensions + if (loaded_bmp == nullptr || bitmap_width(loaded_bmp) == 0) + { + // Bitmap failed to load (file path issue), skip further verification + if (loaded_bmp != nullptr) + { + free_bitmap(loaded_bmp); + } + } + else + { + REQUIRE(bitmap_width(loaded_bmp) == 5); + REQUIRE(bitmap_height(loaded_bmp) == 5); + + // Verify sample pixels from the loaded bitmap + color loaded_pixel_w = get_pixel(loaded_bmp, 0, 0); + REQUIRE(red_of(loaded_pixel_w) == red_of(color_white())); + REQUIRE(green_of(loaded_pixel_w) == green_of(color_white())); + REQUIRE(blue_of(loaded_pixel_w) == blue_of(color_white())); + + color loaded_pixel_r = get_pixel(loaded_bmp, 2, 1); + REQUIRE(red_of(loaded_pixel_r) == red_of(color_red())); + REQUIRE(green_of(loaded_pixel_r) == green_of(color_red())); + REQUIRE(blue_of(loaded_pixel_r) == blue_of(color_red())); + + color loaded_pixel_g = get_pixel(loaded_bmp, 1, 2); + REQUIRE(red_of(loaded_pixel_g) == red_of(color_green())); + REQUIRE(green_of(loaded_pixel_g) == green_of(color_green())); + REQUIRE(blue_of(loaded_pixel_g) == blue_of(color_green())); + + color loaded_pixel_b = get_pixel(loaded_bmp, 3, 3); + REQUIRE(red_of(loaded_pixel_b) == red_of(color_blue())); + REQUIRE(green_of(loaded_pixel_b) == green_of(color_blue())); + REQUIRE(blue_of(loaded_pixel_b) == blue_of(color_blue())); + + color loaded_pixel_k = get_pixel(loaded_bmp, 4, 4); + REQUIRE(red_of(loaded_pixel_k) == red_of(color_black())); + REQUIRE(green_of(loaded_pixel_k) == green_of(color_black())); + REQUIRE(blue_of(loaded_pixel_k) == blue_of(color_black())); + + // Clean up the loaded bitmap + free_bitmap(loaded_bmp); + } } \ No newline at end of file From 3370c80a9073b613263528ef92e4112b35872e33 Mon Sep 17 00:00:00 2001 From: rory-cd Date: Thu, 20 Aug 2026 21:05:17 +1000 Subject: [PATCH 3/3] Add saved file cleanup and restructure with sections --- .../test/unit_tests/unit_test_graphics.cpp | 131 +++++++++++------- 1 file changed, 78 insertions(+), 53 deletions(-) diff --git a/coresdk/src/test/unit_tests/unit_test_graphics.cpp b/coresdk/src/test/unit_tests/unit_test_graphics.cpp index 3774b601..222d1ffc 100644 --- a/coresdk/src/test/unit_tests/unit_test_graphics.cpp +++ b/coresdk/src/test/unit_tests/unit_test_graphics.cpp @@ -4,7 +4,11 @@ #include "catch.hpp" +#include #include "graphics.h" +#include "images.h" +#include "utility_functions.h" +#include "resources.h" #include "window_manager.h" using namespace splashkit_lib; @@ -22,10 +26,11 @@ TEST_CASE("screen dimension utilities", "[screen_width][screen_height]") // } } -TEST_CASE("bitmap creation, save, load, and pixel verification", "[bitmap][save][load][get_pixel]") +TEST_CASE("can create, save, and load a bitmap", "[bitmap][save][load][get_pixel]") { // Create a small 5x5 bitmap - bitmap test_bmp = create_bitmap("test_bitmap", 5, 5); + string testBitmapName = "test_bitmap"; + bitmap test_bmp = create_bitmap(testBitmapName, 5, 5); // Fill each row with a different color pattern // Row 0: WHITE @@ -39,57 +44,79 @@ TEST_CASE("bitmap creation, save, load, and pixel verification", "[bitmap][save] // Row 4: BLACK fill_rectangle_on_bitmap(test_bmp, color_black(), 0, 4, 5, 1); - // Verify pixels directly from the created bitmap (before save/load) - // Check a pixel from row 0 (WHITE) - color pixel_w0 = get_pixel(test_bmp, 0, 0); - REQUIRE(red_of(pixel_w0) == red_of(color_white())); - REQUIRE(green_of(pixel_w0) == green_of(color_white())); - REQUIRE(blue_of(pixel_w0) == blue_of(color_white())); - - // Check a pixel from row 1 (RED) - color pixel_r1 = get_pixel(test_bmp, 2, 1); - REQUIRE(red_of(pixel_r1) == red_of(color_red())); - REQUIRE(green_of(pixel_r1) == green_of(color_red())); - REQUIRE(blue_of(pixel_r1) == blue_of(color_red())); - - // Check a pixel from row 2 (GREEN) - color pixel_g2 = get_pixel(test_bmp, 1, 2); - REQUIRE(red_of(pixel_g2) == red_of(color_green())); - REQUIRE(green_of(pixel_g2) == green_of(color_green())); - REQUIRE(blue_of(pixel_g2) == blue_of(color_green())); - - // Check a pixel from row 3 (BLUE) - color pixel_b3 = get_pixel(test_bmp, 3, 3); - REQUIRE(red_of(pixel_b3) == red_of(color_blue())); - REQUIRE(green_of(pixel_b3) == green_of(color_blue())); - REQUIRE(blue_of(pixel_b3) == blue_of(color_blue())); - - // Check a pixel from row 4 (BLACK) - color pixel_k4 = get_pixel(test_bmp, 4, 4); - REQUIRE(red_of(pixel_k4) == red_of(color_black())); - REQUIRE(green_of(pixel_k4) == green_of(color_black())); - REQUIRE(blue_of(pixel_k4) == blue_of(color_black())); - - // Save the bitmap to disk - save_bitmap(test_bmp, "test_bitmap_pattern_5x5"); - - // Clean up allocated bitmap - free_bitmap(test_bmp); - - // Load the saved bitmap back - bitmap loaded_bmp = load_bitmap("loaded_test", "test_bitmap_pattern_5x5.png"); + SECTION("can create valid bitmap") + { + REQUIRE(has_bitmap(testBitmapName)); + REQUIRE(bitmap_width(test_bmp) != 0); + } - // Check that bitmap loaded successfully before verifying dimensions - if (loaded_bmp == nullptr || bitmap_width(loaded_bmp) == 0) + SECTION("bitmap displays pixels correctly") { - // Bitmap failed to load (file path issue), skip further verification - if (loaded_bmp != nullptr) - { - free_bitmap(loaded_bmp); - } + // Verify pixels directly from the created bitmap (before save/load) + // Check a pixel from row 0 (WHITE) + color pixel_w0 = get_pixel(test_bmp, 0, 0); + REQUIRE(red_of(pixel_w0) == red_of(color_white())); + REQUIRE(green_of(pixel_w0) == green_of(color_white())); + REQUIRE(blue_of(pixel_w0) == blue_of(color_white())); + + // Check a pixel from row 1 (RED) + color pixel_r1 = get_pixel(test_bmp, 2, 1); + REQUIRE(red_of(pixel_r1) == red_of(color_red())); + REQUIRE(green_of(pixel_r1) == green_of(color_red())); + REQUIRE(blue_of(pixel_r1) == blue_of(color_red())); + + // Check a pixel from row 2 (GREEN) + color pixel_g2 = get_pixel(test_bmp, 1, 2); + REQUIRE(red_of(pixel_g2) == red_of(color_green())); + REQUIRE(green_of(pixel_g2) == green_of(color_green())); + REQUIRE(blue_of(pixel_g2) == blue_of(color_green())); + + // Check a pixel from row 3 (BLUE) + color pixel_b3 = get_pixel(test_bmp, 3, 3); + REQUIRE(red_of(pixel_b3) == red_of(color_blue())); + REQUIRE(green_of(pixel_b3) == green_of(color_blue())); + REQUIRE(blue_of(pixel_b3) == blue_of(color_blue())); + + // Check a pixel from row 4 (BLACK) + color pixel_k4 = get_pixel(test_bmp, 4, 4); + REQUIRE(red_of(pixel_k4) == red_of(color_black())); + REQUIRE(green_of(pixel_k4) == green_of(color_black())); + REQUIRE(blue_of(pixel_k4) == blue_of(color_black())); + } + + SECTION("can free a bitmap") + { + free_bitmap(test_bmp); + REQUIRE(has_bitmap(testBitmapName) == false); } - else + + SECTION("can save and load a bitmap") { + // Save the bitmap to disk + string filename = "test_bitmap_pattern_5x5"; + save_bitmap(test_bmp, filename); + + // Get the path to the saved bitmap (should be desktop normally) + string path = path_from({path_to_user_home(), "Desktop"}); + + // Fall back to home if Desktop doesn't exist (mirrors save_bitmap functionality) + if (!directory_exists(path)) + { + path = path_to_user_home(); + } + + string filepath = path + "/" + filename + ".png"; + + // Load the saved bitmap back + bitmap loaded_bmp = load_bitmap("loaded_test", filepath); + + // Clean up the unneeded file + int result = std::remove(filepath.c_str()); + + // Ensure the bitmap was loaded + REQUIRE(has_bitmap("loaded_test")); + REQUIRE(bitmap_width(loaded_bmp) != 0); + REQUIRE(bitmap_width(loaded_bmp) == 5); REQUIRE(bitmap_height(loaded_bmp) == 5); @@ -118,8 +145,6 @@ TEST_CASE("bitmap creation, save, load, and pixel verification", "[bitmap][save] REQUIRE(red_of(loaded_pixel_k) == red_of(color_black())); REQUIRE(green_of(loaded_pixel_k) == green_of(color_black())); REQUIRE(blue_of(loaded_pixel_k) == blue_of(color_black())); - - // Clean up the loaded bitmap - free_bitmap(loaded_bmp); } -} \ No newline at end of file + free_all_bitmaps(); +}