Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions coresdk/src/test/unit_tests/unit_test_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,105 @@ 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");
Comment thread
kyriesk marked this conversation as resolved.

// 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);
}
}