From e7160ffef061134b234374e7042b84e9cd748d88 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 8 Oct 2024 12:42:01 +0300 Subject: [PATCH] tests file for RGB10A2, proper SFINAE condition for integral types --- openVulkanoCpp/Math/RGB10A2.hpp | 12 +-- tests/Math/RGB10A2.cpp | 134 ++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 tests/Math/RGB10A2.cpp diff --git a/openVulkanoCpp/Math/RGB10A2.hpp b/openVulkanoCpp/Math/RGB10A2.hpp index aac3434..b22aab0 100644 --- a/openVulkanoCpp/Math/RGB10A2.hpp +++ b/openVulkanoCpp/Math/RGB10A2.hpp @@ -30,21 +30,21 @@ namespace OpenVulkano::Math RGB10A2(TYPE value = 0) : value(value) {} - template || std::is_signed_v>> + template>> void Set(Vector3 vec3) { vec3 &= VALUE_BITMASK; SetUnchecked(vec3); } - template || std::is_signed_v>> + template>> void Set(Vector3_SIMD vec3) { vec3 &= VALUE_BITMASK; SetUnchecked(vec3); } - template || std::is_signed_v>> + template>> void Set(Vector4 vec4) { vec4 &= VALUE_BITMASK; @@ -69,19 +69,19 @@ namespace OpenVulkano::Math Set(Math::Vector4(vec4)); } - template || std::is_signed_v>> + template>> void const SetUnchecked(Vector3& vec3) { value = vec3.r | vec3.g << 10 | vec3.b << 20 | MAX_ALPHA_VALUE << 30; } - template || std::is_signed_v>> + template>> void SetUnchecked(const Vector3_SIMD& vec3) { value = vec3.r | vec3.g << 10 | vec3.b << 20 | MAX_ALPHA_VALUE << 30; } - template || std::is_signed_v>> + template>> void SetUnchecked(const Vector4& vec4) { value = vec4.r | vec4.g << 10 | vec4.b << 20 | vec4.a << 30; diff --git a/tests/Math/RGB10A2.cpp b/tests/Math/RGB10A2.cpp new file mode 100644 index 0000000..8ba5f9e --- /dev/null +++ b/tests/Math/RGB10A2.cpp @@ -0,0 +1,134 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +#include "Math/RGB10A2.hpp" + +using namespace OpenVulkano::Math; + +namespace +{ + bool almostEqual(float a, float b, float epsilon = 0.001f) { return std::fabs(a - b) < epsilon; } +} + +TEST_CASE("Default Constructor", "[RGB10A2U]") +{ + RGB10A2U color; + REQUIRE(color.value == 0); +} + +TEST_CASE("Set Method with Vector3", "[RGB10A2U]") +{ + RGB10A2U color; + Vector3 vec3 { 1000, 500, 250 }; + color.Set(vec3); + REQUIRE(color.r == 1000); + REQUIRE(color.g == 500); + REQUIRE(color.b == 250); + REQUIRE(color.a == 3); +} + +TEST_CASE("Set Method with Vector4", "[RGB10A2U]") +{ + RGB10A2U color; + Vector4 vec4 { 1000, 500, 250, 1 }; + color.Set(vec4); + REQUIRE(color.r == 1000); + REQUIRE(color.g == 500); + REQUIRE(color.b == 250); + REQUIRE(color.a == 1); +} + +TEST_CASE("SetNormalized Method with Vector3", "[RGB10A2U]") +{ + RGB10A2U color; + Vector3 vec3 { 0.5f, 0.25f, 0.125f }; + color.SetNormalized(vec3); + REQUIRE(color.r == int(0.5 * 1023)); + REQUIRE(color.g == int(0.25 * 1023)); + REQUIRE(color.b == int(0.125 * 1023)); + REQUIRE(color.a == 3); +} + +TEST_CASE("Equality Operators", "[RGB10A2U]") +{ + RGB10A2U color; + RGB10A2U color2 { color }; + REQUIRE(color == color2); + color2.r = 1; + REQUIRE(color != color2); +} + +TEST_CASE("Conversion to Vector4", "[RGB10A2U]") +{ + RGB10A2U color; + color.Set(Vector4 { 1000, 500, 250, 2 }); + Vector4f vec4 = color; + REQUIRE(almostEqual(vec4.x, 1000.0f / 1023.0f)); + REQUIRE(almostEqual(vec4.y, 500.0f / 1023.0f)); + REQUIRE(almostEqual(vec4.z, 250.0f / 1023.0f)); + REQUIRE(almostEqual(vec4.w, 2.0f / 3.0f)); +} + +TEST_CASE("Default Constructor", "[RGB10A2S]") +{ + RGB10A2S color; + REQUIRE(color.value == 0); +} + +TEST_CASE("Set Method with Vector3", "[RGB10A2S]") +{ + RGB10A2S color; + Vector3 vec3 { 1000, 500, 250 }; + color.Set(vec3); + REQUIRE(color.r == -24); + REQUIRE(color.g == 500); + REQUIRE(color.b == 250); + REQUIRE(color.a == 1); +} + +TEST_CASE("Set Method with Vector4", "[RGB10A2S]") +{ + RGB10A2S color; + Vector4 vec4 { 1000, 500, 250, 1 }; + color.Set(vec4); + REQUIRE(color.r == -24); // because it is RGB10A2S + REQUIRE(color.g == 500); + REQUIRE(color.b == 250); + REQUIRE(color.a == 1); +} + +TEST_CASE("SetNormalized Method with Vector3", "[RGB10A2S]") +{ + RGB10A2S color; + Vector3 vec3 { 0.5f, 0.25f, 0.125f }; + color.SetNormalized(vec3); + REQUIRE(color.r == 255); // because it is RGB10A2S + REQUIRE(color.g == 127); // because it is RGB10A2S + REQUIRE(color.b == 63); // because it is RGB10A2S + REQUIRE(color.a == 1); // because it is RGB10A2S +} + +TEST_CASE("Equality Operators", "[RGB10A2S]") +{ + RGB10A2S color; + RGB10A2S color2 { color }; + REQUIRE(color == color2); + color2.r = 1; + REQUIRE(color != color2); +} + +TEST_CASE("Conversion to Vector4", "[RGB10A2S]") +{ + RGB10A2S color; + color.Set(Vector4 { 1000, 500, 250, 2 }); + Vector4f vec4 = color; + REQUIRE(almostEqual(vec4.x, -0.0469667315)); // because the type is Signed. Ugh! + REQUIRE(almostEqual(vec4.y, 0.978474)); // because the type is Signed. Ugh! + REQUIRE(almostEqual(vec4.z, 0.489237)); // because the type is Signed. Ugh! + REQUIRE(almostEqual(vec4.w, -2.0f)); // because the type is Signed. Ugh! +} \ No newline at end of file