From 6d6ca161985e01fe044e3278e7e381485050a4a7 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 19 Apr 2024 13:07:54 +0200 Subject: [PATCH] Tests: add unit tests for Color class --- tests/src/App/CMakeLists.txt | 1 + tests/src/App/Color.cpp | 247 +++++++++++++++++++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 tests/src/App/Color.cpp diff --git a/tests/src/App/CMakeLists.txt b/tests/src/App/CMakeLists.txt index cdd545cadf..6718941efc 100644 --- a/tests/src/App/CMakeLists.txt +++ b/tests/src/App/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources( PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Application.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Branding.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Color.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ComplexGeoData.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Document.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Expression.cpp diff --git a/tests/src/App/Color.cpp b/tests/src/App/Color.cpp new file mode 100644 index 0000000000..99db0efd4c --- /dev/null +++ b/tests/src/App/Color.cpp @@ -0,0 +1,247 @@ +#include "gtest/gtest.h" +#include +#include "App/Color.h" + +// NOLINTBEGIN +TEST(TestColor, equal) +{ + int red = 85; + int green = 170; + int blue = 255; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F); + uint32_t packed = color.getPackedValue(); + + App::Color color2 {packed}; + EXPECT_EQ(color, color2); +} + +TEST(TestColor, round) +{ + for (int index = 0; index < 256; index++) { + float value = static_cast(index) / 255.0F; + EXPECT_EQ(std::lround(value * 255.0F), index); + } +} + +TEST(TestColor, packedValue) +{ + int red = 85; + int green = 170; + int blue = 255; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F); + uint32_t packed = color.getPackedValue(); + App::Color color2 {packed}; + + EXPECT_EQ(std::lround(color2.r * 255.0F), 85); + EXPECT_EQ(std::lround(color2.g * 255.0F), 170); + EXPECT_EQ(std::lround(color2.b * 255.0F), 255); +} + +TEST(TestColor, packedRGB) +{ + int red = 85; + int green = 170; + int blue = 255; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F); + uint32_t packed = color.getPackedRGB(); + App::Color color2; + color2.setPackedRGB(packed); + + EXPECT_EQ(std::lround(color2.r * 255.0F), 85); + EXPECT_EQ(std::lround(color2.g * 255.0F), 170); + EXPECT_EQ(std::lround(color2.b * 255.0F), 255); +} + +TEST(TestColor, packedARGB) +{ + int red = 85; + int green = 170; + int blue = 255; + int alpha = 127; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F, alpha / 255.0F); + uint32_t packed = color.getPackedARGB(); + App::Color color2; + color2.setPackedARGB(packed); + + EXPECT_EQ(std::lround(color2.r * 255.0F), 85); + EXPECT_EQ(std::lround(color2.g * 255.0F), 170); + EXPECT_EQ(std::lround(color2.b * 255.0F), 255); + EXPECT_EQ(std::lround(color2.a * 255.0F), 127); +} + +struct IntColor +{ + IntColor(int red, int green, int blue, int alpha = 0) + : red_ {red} + , green_ {green} + , blue_ {blue} + , alpha_ {alpha} + {} + + float redF() const + { + return static_cast(red_) / 255.0F; + } + + float greenF() const + { + return static_cast(green_) / 255.0F; + } + + float blueF() const + { + return static_cast(blue_) / 255.0F; + } + + float alphaF() const + { + return static_cast(alpha_) / 255.0F; + } + + int red() const + { + return red_; + } + + int green() const + { + return green_; + } + + int blue() const + { + return blue_; + } + + int alpha() const + { + return alpha_; + } + +private: + int red_ {}; + int green_ {}; + int blue_ {}; + int alpha_ {}; +}; + +TEST(TestColor, asPackedRGB) +{ + int red = 85; + int green = 170; + int blue = 255; + + IntColor intColor {red, green, blue}; + uint32_t packed = App::Color::asPackedRGB(intColor); + + + EXPECT_EQ(packed, 1437269760); +} + +TEST(TestColor, fromPackedRGB) +{ + int red = 85; + int green = 170; + int blue = 255; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F); + + IntColor intColor = App::Color::fromPackedRGB(color.getPackedRGB()); + + EXPECT_EQ(intColor.red(), red); + EXPECT_EQ(intColor.green(), green); + EXPECT_EQ(intColor.blue(), blue); +} + +TEST(TestColor, asPackedRGBA) +{ + int red = 85; + int green = 170; + int blue = 255; + int alpha = 127; + + IntColor intColor {red, green, blue, alpha}; + uint32_t packed = App::Color::asPackedRGBA(intColor); + + + EXPECT_EQ(packed, 1437269887); +} + +TEST(TestColor, fromPackedRGBA) +{ + int red = 85; + int green = 170; + int blue = 255; + int alpha = 127; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F, alpha / 255.0F); + + IntColor intColor = App::Color::fromPackedRGBA(color.getPackedValue()); + + EXPECT_EQ(intColor.red(), red); + EXPECT_EQ(intColor.green(), green); + EXPECT_EQ(intColor.blue(), blue); + EXPECT_EQ(intColor.alpha(), alpha); +} + +TEST(TestColor, setValue) +{ + int red = 85; + int green = 170; + int blue = 255; + + IntColor intColor {red, green, blue}; + App::Color color {}; + color.setValue(intColor); + + EXPECT_FLOAT_EQ(color.r, 85.0F / 255.0F); + EXPECT_FLOAT_EQ(color.g, 170.0F / 255.0F); + EXPECT_FLOAT_EQ(color.b, 255.0F / 255.0F); +} + +TEST(TestColor, asValue) +{ + int red = 85; + int green = 170; + int blue = 255; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F); + + IntColor intColor = color.asValue(); + + EXPECT_EQ(intColor.red(), 85); + EXPECT_EQ(intColor.green(), 170); + EXPECT_EQ(intColor.blue(), 255); +} + +TEST(TestColor, asHexString) +{ + int red = 85; + int green = 170; + int blue = 255; + + App::Color color(red / 255.0F, green / 255.0F, blue / 255.0F); + + EXPECT_EQ(color.asHexString(), "#55AAFF"); +} + +TEST(TestColor, fromHexString) +{ + App::Color color; + EXPECT_FALSE(color.fromHexString(std::string(""))); + EXPECT_FALSE(color.fromHexString(std::string("abcdef"))); + EXPECT_TRUE(color.fromHexString(std::string("#abcdef"))); + EXPECT_TRUE(color.fromHexString(std::string("#ABCDEF"))); + EXPECT_FALSE(color.fromHexString(std::string("#abcde"))); + EXPECT_FALSE(color.fromHexString(std::string("#abcdeff"))); + EXPECT_FALSE(color.fromHexString(std::string("#abcdeffff"))); + EXPECT_TRUE(color.fromHexString(std::string("#55AAFF"))); + EXPECT_FLOAT_EQ(color.r, 85.0F / 255.0F); + EXPECT_FLOAT_EQ(color.g, 170.0F / 255.0F); + EXPECT_FLOAT_EQ(color.b, 255.0F / 255.0F); +} +// NOLINTEND