From 1da2ddb1250ce045925788c3a00a9c30773c38ea Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 24 Jan 2024 15:45:46 +0900 Subject: [PATCH] Add some unit tests for helper functions --- src/unittest/test_helpers.cpp | 55 ++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/unittest/test_helpers.cpp b/src/unittest/test_helpers.cpp index fd68aba..2d9826a 100644 --- a/src/unittest/test_helpers.cpp +++ b/src/unittest/test_helpers.cpp @@ -6,7 +6,7 @@ #include "helpers.hpp" #include "triangle.hpp" -TEST_CASE("Test is_within_shape", "[Triangles]") { +TEST_CASE("Test is_within_shape", "Helpers") { Tri::Triangle triangle({{ {0.0f, 10.0f}, {10.0f, 10.0f}, @@ -18,3 +18,56 @@ TEST_CASE("Test is_within_shape", "[Triangles]") { CHECK(Tri::is_within_shape(triangle, {15.0f, 5.0f}) == false); CHECK(Tri::is_within_shape(triangle, {7.0f, 7.0f}) == true); } + +TEST_CASE("Test make_counter_clockwise", "Helpers") { + // Note that +x is right and +y is down. + + { + // Clockwise triangle. + std::array tri{{ + {0.0F, 0.0F}, {1.0F, 0.0F}, {2.0F, 2.0F} + }}; + + Tri::make_counter_clockwise(tri); + + CHECK(tri.at(0).x == 2.0F); + CHECK(tri.at(0).y == 2.0F); + + CHECK(tri.at(1).x == 1.0F); + CHECK(tri.at(1).y == 0.0F); + + CHECK(tri.at(2).x == 0.0F); + CHECK(tri.at(2).y == 0.0F); + } + + { + // Counter-Clockwise triangle. + std::array tri{{ + {2.0F, 0.0F}, {3.0F, 3.0F}, {4.0F, 1.0F} + }}; + + Tri::make_counter_clockwise(tri); + + CHECK(tri.at(0).x == 2.0F); + CHECK(tri.at(0).y == 0.0F); + + CHECK(tri.at(1).x == 3.0F); + CHECK(tri.at(1).y == 3.0F); + + CHECK(tri.at(2).x == 4.0F); + CHECK(tri.at(2).y == 1.0F); + } +} + +TEST_CASE("Test invert_color", "Helpers") { + Color c{ + 255, 255, 255, 255 + }; + + c = Tri::invert_color(c); + + CHECK(c.r == 0); + CHECK(c.g == 0); + CHECK(c.b == 0); + CHECK(c.a == 255); +}