From 0e475f3f502d6f84744b9800d6c4fa058dd64043 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 15 Sep 2022 10:11:17 -0600 Subject: [PATCH] Test reinterpreting an `sf::Color` as a `std::uint8_t*` --- test/Graphics/Color.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/Graphics/Color.cpp b/test/Graphics/Color.cpp index b50684b79..f7b77a2cc 100644 --- a/test/Graphics/Color.cpp +++ b/test/Graphics/Color.cpp @@ -3,6 +3,7 @@ #include #include +#include TEST_CASE("[Graphics] sf::Color") { @@ -152,4 +153,27 @@ TEST_CASE("[Graphics] sf::Color") static_assert(sf::Color::Black == sf::Color(0, 0, 0, 255)); } + + SUBCASE("Reinterpret as std::uint8_t*") + { + static_assert(sizeof(sf::Color) == 4); + static_assert(alignof(sf::Color) == 1); + + const std::vector pixels = {{10, 11, 12, 13}, {14, 15, 16, 17}, {18, 19, 20, 21}}; + const std::uint8_t* begin = reinterpret_cast(pixels.data()); + CHECK(begin[0] == pixels[0].r); + CHECK(begin[1] == pixels[0].g); + CHECK(begin[2] == pixels[0].b); + CHECK(begin[3] == pixels[0].a); + + CHECK(begin[4] == pixels[1].r); + CHECK(begin[5] == pixels[1].g); + CHECK(begin[6] == pixels[1].b); + CHECK(begin[7] == pixels[1].a); + + CHECK(begin[8] == pixels[2].r); + CHECK(begin[9] == pixels[2].g); + CHECK(begin[10] == pixels[2].b); + CHECK(begin[11] == pixels[2].a); + } }