From 8f4a8d3883e7bd4a574278c365179c84b6705cc2 Mon Sep 17 00:00:00 2001 From: Ricardo Antunes Date: Sun, 18 Jun 2023 10:17:51 +0100 Subject: [PATCH] test(core): glm reflection --- .../cubos/core/reflection/external/glm.cpp | 7 - core/tests/CMakeLists.txt | 1 + core/tests/reflection/external/glm.cpp | 135 ++++++++++++++++++ 3 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 core/tests/reflection/external/glm.cpp diff --git a/core/src/cubos/core/reflection/external/glm.cpp b/core/src/cubos/core/reflection/external/glm.cpp index 3d38e94b60..4ae8b25891 100644 --- a/core/src/cubos/core/reflection/external/glm.cpp +++ b/core/src/cubos/core/reflection/external/glm.cpp @@ -15,7 +15,6 @@ AUTO_VEC2(glm::ivec2); AUTO_VEC2(glm::uvec2); AUTO_VEC2(glm::vec2); -AUTO_VEC2(glm::dvec2); #define AUTO_VEC3(type) \ CUBOS_REFLECT_EXTERNAL_IMPL(type) \ @@ -31,7 +30,6 @@ AUTO_VEC2(glm::dvec2); AUTO_VEC3(glm::ivec3); AUTO_VEC3(glm::uvec3); AUTO_VEC3(glm::vec3); -AUTO_VEC3(glm::dvec3); #define AUTO_VEC4(type) \ CUBOS_REFLECT_EXTERNAL_IMPL(type) \ @@ -48,7 +46,6 @@ AUTO_VEC3(glm::dvec3); AUTO_VEC4(glm::ivec4); AUTO_VEC4(glm::uvec4); AUTO_VEC4(glm::vec4); -AUTO_VEC4(glm::dvec4); #define ACCESS_MAT(type, x, y) \ [](const void* mat) -> uintptr_t { return reinterpret_cast(&(*static_cast(mat))[x][y]); } @@ -68,7 +65,6 @@ AUTO_VEC4(glm::dvec4); } AUTO_MAT2(glm::mat2); -AUTO_MAT2(glm::dmat2); #define AUTO_MAT3(type) \ CUBOS_REFLECT_EXTERNAL_IMPL(type) \ @@ -89,7 +85,6 @@ AUTO_MAT2(glm::dmat2); } AUTO_MAT3(glm::mat3); -AUTO_MAT3(glm::dmat3); #define AUTO_MAT4(type) \ CUBOS_REFLECT_EXTERNAL_IMPL(type) \ @@ -117,8 +112,6 @@ AUTO_MAT3(glm::dmat3); } AUTO_MAT4(glm::mat4); -AUTO_MAT4(glm::dmat4); // Reuse the vec4 macro for quaternions. AUTO_VEC4(glm::quat); -AUTO_VEC4(glm::dquat); diff --git a/core/tests/CMakeLists.txt b/core/tests/CMakeLists.txt index 53944fe0f7..98e49f8266 100644 --- a/core/tests/CMakeLists.txt +++ b/core/tests/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable( cubos-core-tests main.cpp + reflection/external/glm.cpp reflection/external/primitives_and_string.cpp reflection/external/variant.cpp reflection/external/vector.cpp diff --git a/core/tests/reflection/external/glm.cpp b/core/tests/reflection/external/glm.cpp new file mode 100644 index 0000000000..737e88af95 --- /dev/null +++ b/core/tests/reflection/external/glm.cpp @@ -0,0 +1,135 @@ +#include +#include + +#include +#include +#include + +#include "../utils.hpp" + +using cubos::core::reflection::ObjectType; +using cubos::core::reflection::reflect; + +template +static void testVec(glm::vec, const char* name) +{ + using Vec = glm::vec; + testTypeGetters(name, name); + testTypeDefaultConstructor(); + + // The fields of vector must be named x, y, z, w, in that order. + auto& to = reflect().template asKind(); + REQUIRE(to.fields().size() == static_cast(L)); + + CHECK(to.fields()[0]->name() == "x"); + CHECK(to.fields()[1]->name() == "y"); + + if (L >= 3) + { + CHECK(to.fields()[2]->name() == "z"); + } + + if (L >= 4) + { + CHECK(to.fields()[3]->name() == "w"); + } + + // Check if the correct field types are returned. + for (auto& field : to.fields()) + { + REQUIRE(field->type().template is()); + } + + // Check if the fields are set correctly. + for (glm::length_t i = 0; i < L; ++i) + { + Vec vec{}; + to.fields()[i]->template get(&vec) = static_cast(1); + CHECK(vec[i] == static_cast(1)); + } +} + +template +static void testMat(glm::mat, const char* name) +{ + using Mat = glm::mat; + testTypeGetters(name, name); + testTypeDefaultConstructor(); + + // The fields of matrix must be named 00, 01, 02, 10, 11, etc, in that order. + auto& to = reflect().template asKind(); + REQUIRE(to.fields().size() == static_cast(C * R)); + for (glm::length_t c = 0; c < C; ++c) + { + for (glm::length_t r = 0; r < R; ++r) + { + std::string expected = std::to_string(c) + std::to_string(r); + CHECK(to.fields()[c * R + r]->name() == expected); + REQUIRE(to.fields()[c * R + r]->type().template is()); + } + } + + // Check if the fields are set correctly. + for (glm::length_t c = 0; c < C; ++c) + { + for (glm::length_t r = 0; r < R; ++r) + { + Mat mat{}; + to.fields()[c * R + r]->template get(&mat) = static_cast(1); + CHECK(mat[c][r] == static_cast(1)); + } + } +} + +template +static void testQuat(glm::qua, const char* name) +{ + using Quat = glm::qua; + testTypeGetters(name, name); + testTypeDefaultConstructor(); + + // The fields of quaternion must be named x, y, z, w, in that order. + auto& to = reflect().template asKind(); + REQUIRE(to.fields().size() == 4); + CHECK(to.fields()[0]->name() == "x"); + CHECK(to.fields()[1]->name() == "y"); + CHECK(to.fields()[2]->name() == "z"); + CHECK(to.fields()[3]->name() == "w"); + + // Check if the correct field types are returned. + for (auto& field : to.fields()) + { + REQUIRE(field->type().template is()); + } + + // Check if the fields are set correctly. + for (glm::length_t i = 0; i < 4; ++i) + { + Quat qua{}; + to.fields()[i]->template get(&qua) = static_cast(1); + CHECK(qua[i] == static_cast(1)); + } +} + +TEST_CASE("reflection::reflect<(external/glm)>()") +{ + testVec(glm::ivec2{}, "glm::ivec2"); + testVec(glm::uvec2{}, "glm::uvec2"); + testVec(glm::vec2{}, "glm::vec2"); + + testVec(glm::ivec3{}, "glm::ivec3"); + testVec(glm::uvec3{}, "glm::uvec3"); + testVec(glm::vec3{}, "glm::vec3"); + + testVec(glm::ivec4{}, "glm::ivec4"); + testVec(glm::uvec4{}, "glm::uvec4"); + testVec(glm::vec4{}, "glm::vec4"); + + testMat(glm::mat2{}, "glm::mat2"); + + testMat(glm::mat3{}, "glm::mat3"); + + testMat(glm::mat4{}, "glm::mat4"); + + testQuat(glm::quat{}, "glm::quat"); +}