From ae3a38345def2d52a5f7adc4ab9f03bcd39621e6 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Thu, 16 Dec 2021 17:58:53 +0100 Subject: [PATCH] Add tests for 'Vector' destructuring and 'constexpr' support --- test/src/System/Vector2.cpp | 44 ++++++++++++++++++++++++++++++++++ test/src/System/Vector3.cpp | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/test/src/System/Vector2.cpp b/test/src/System/Vector2.cpp index bb3b28ea..2b78dc31 100644 --- a/test/src/System/Vector2.cpp +++ b/test/src/System/Vector2.cpp @@ -1,5 +1,6 @@ #include #include "SystemUtil.hpp" +#include // Use sf::Vector2i for tests. Test coverage is given, as there are no template specializations. @@ -145,4 +146,47 @@ TEST_CASE("sf::Vector2 class template", "[system]") CHECK_FALSE(firstEqualVector != secondEqualVector); } } + + SECTION("Structured bindings") + { + sf::Vector2i vector(1, 2); + + SECTION("destructure by value") + { + auto [x, y] = vector; + + CHECK(x == 1); + CHECK(y == 2); + + static_assert(std::is_same_v); + + x = 3; + + CHECK(x == 3); + CHECK(vector.x == 1); + } + + SECTION("destructure by ref") + { + auto& [x, y] = vector; + + CHECK(x == 1); + CHECK(y == 2); + + static_assert(std::is_same_v); + + x = 3; + + CHECK(x == 3); + CHECK(vector.x == 3); + } + } + + SECTION("Constexpr support") + { + constexpr sf::Vector2i vector(1, 2); + static_assert(vector.x == 1); + static_assert(vector.y == 2); + static_assert(vector + sf::Vector2i(2, 1) == sf::Vector2i(3, 3)); + } } diff --git a/test/src/System/Vector3.cpp b/test/src/System/Vector3.cpp index 948a0ff7..beefa4bc 100644 --- a/test/src/System/Vector3.cpp +++ b/test/src/System/Vector3.cpp @@ -1,5 +1,6 @@ #include #include "SystemUtil.hpp" +#include // Use sf::Vector3i for tests. Test coverage is given, as there are no template specializations. @@ -158,4 +159,50 @@ TEST_CASE("sf::Vector3 class template", "[system]") CHECK_FALSE(firstEqualVector != secondEqualVector); } } + + SECTION("Structured bindings") + { + sf::Vector3i vector(1, 2, 3); + + SECTION("destructure by value") + { + auto [x, y, z] = vector; + + CHECK(x == 1); + CHECK(y == 2); + CHECK(z == 3); + + static_assert(std::is_same_v); + + x = 3; + + CHECK(x == 3); + CHECK(vector.x == 1); + } + + SECTION("destructure by ref") + { + auto& [x, y, z] = vector; + + CHECK(x == 1); + CHECK(y == 2); + CHECK(z == 3); + + static_assert(std::is_same_v); + + x = 3; + + CHECK(x == 3); + CHECK(vector.x == 3); + } + } + + SECTION("Constexpr support") + { + constexpr sf::Vector3i vector(1, 2, 3); + static_assert(vector.x == 1); + static_assert(vector.y == 2); + static_assert(vector.z == 3); + static_assert(vector + sf::Vector3i(3, 2, 1) == sf::Vector3i(4, 4, 4)); + } }