From c8dce7d7d832f723bed6a2de47957e46d8539308 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 9 Feb 2025 00:17:16 -0500 Subject: [PATCH] Fixed broken unit tests --- .vscode/launch.json | 4 +- CMakeLists.txt | 1 + unit-tests/CMakeLists.txt | 12 +++--- unit-tests/matrix-tests.cpp | 79 ++++++++++++++++++++++++++++++++++--- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index bf41fff..091ab1a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Debug Matrix Unit Tests", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/build/src/Matrix/matrix-tests", + "program": "${workspaceFolder}/build/unit-tests/matrix-tests", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", @@ -30,7 +30,7 @@ "name": "Debug Quaternion Unit Tests", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/build/src/Quaternion/quaternion-tests", + "program": "${workspaceFolder}/build/unit-tests/quaternion-tests", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/CMakeLists.txt b/CMakeLists.txt index a245e93..1945dfd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required (VERSION 3.11) project(Vector3D) add_subdirectory(src) +add_subdirectory(unit-tests) set(CMAKE_CXX_STANDARD 11) diff --git a/unit-tests/CMakeLists.txt b/unit-tests/CMakeLists.txt index 97d1133..9ee3fa1 100644 --- a/unit-tests/CMakeLists.txt +++ b/unit-tests/CMakeLists.txt @@ -1,26 +1,26 @@ # Quaternion tests -add_executable(quaternion-tests unit-tests/quaternion-tests.cpp) +add_executable(quaternion-tests quaternion-tests.cpp) target_link_libraries(quaternion-tests PRIVATE - vector-3d-intf + quaternion Catch2::Catch2WithMain ) # matrix tests -add_executable(matrix-tests unit-tests/matrix-tests.cpp) +add_executable(matrix-tests matrix-tests.cpp) target_link_libraries(matrix-tests PRIVATE - vector-3d-intf + matrix Catch2::Catch2WithMain ) # Vector 3D Tests -add_executable(vector-3d-tests unit-tests/vector-tests.cpp) +add_executable(vector-3d-tests vector-tests.cpp) target_link_libraries(vector-3d-tests PRIVATE - vector-3d-intf + vector-3d Catch2::Catch2WithMain ) \ No newline at end of file diff --git a/unit-tests/matrix-tests.cpp b/unit-tests/matrix-tests.cpp index eac240d..dc01a08 100644 --- a/unit-tests/matrix-tests.cpp +++ b/unit-tests/matrix-tests.cpp @@ -240,7 +240,7 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") SECTION("Invert") { - mat1.Invert(mat3); + mat3 = mat1.Invert(); REQUIRE_THAT(mat3.Get(0, 0), Catch::Matchers::WithinRel(-2.0F, 1e-6f)); REQUIRE_THAT(mat3.Get(0, 1), Catch::Matchers::WithinRel(1.0F, 1e-6f)); REQUIRE_THAT(mat3.Get(1, 0), Catch::Matchers::WithinRel(1.5F, 1e-6f)); @@ -250,7 +250,7 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") SECTION("Transpose") { // transpose a square matrix - mat1.Transpose(mat3); + mat3 = mat1.Transpose(); REQUIRE(mat3.Get(0, 0) == 1); REQUIRE(mat3.Get(0, 1) == 3); @@ -261,7 +261,7 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") Matrix<2, 3> mat4{1, 2, 3, 4, 5, 6}; Matrix<3, 2> mat5{}; - mat4.Transpose(mat5); + mat5 = mat4.Transpose(); REQUIRE(mat5.Get(0, 0) == 1); REQUIRE(mat5.Get(0, 1) == 4); @@ -315,6 +315,75 @@ TEST_CASE("Elementary Matrix Operations", "Matrix") REQUIRE(mat1Columns.Get(0, 0) == 2); REQUIRE(mat1Columns.Get(1, 0) == 4); } + + SECTION("Get Sub-Matrices") + { + Matrix<3, 3> mat4{ + 1, 2, 3, + 4, 5, 6, + 7, 8, 9}; + + Matrix<2, 2> mat5{0}; + + mat4.SubMatrix(mat5, 0, 0); + REQUIRE(mat5.Get(0, 0) == 1); + REQUIRE(mat5.Get(0, 1) == 2); + REQUIRE(mat5.Get(1, 0) == 4); + REQUIRE(mat5.Get(1, 1) == 5); + + mat4.SubMatrix(mat5, 1, 1); + REQUIRE(mat5.Get(0, 0) == 5); + REQUIRE(mat5.Get(0, 1) == 6); + REQUIRE(mat5.Get(1, 0) == 8); + REQUIRE(mat5.Get(1, 1) == 9); + + Matrix<3, 1> mat6{0}; + mat4.SubMatrix(mat6, 0, 0); + REQUIRE(mat6.Get(0, 0) == 1); + REQUIRE(mat6.Get(1, 0) == 4); + REQUIRE(mat6.Get(2, 0) == 7); + + Matrix<1, 3> mat7{0}; + mat4.SubMatrix(mat7, 0, 0); + REQUIRE(mat7.Get(0, 0) == 1); + REQUIRE(mat7.Get(0, 1) == 2); + REQUIRE(mat7.Get(0, 2) == 3); + } + + SECTION("Set Sub-Matrices") + { + Matrix<3, 3> startMatrix{ + 1, 2, 3, + 4, 5, 6, + 7, 8, 9}; + Matrix<3, 3> mat4 = startMatrix; + + Matrix<2, 2> mat5{10, 11, 12, 13}; + mat4.SetSubMatrix(mat5, 0, 0); + REQUIRE(mat4.Get(0, 0) == 10); + REQUIRE(mat4.Get(0, 1) == 11); + REQUIRE(mat4.Get(1, 0) == 12); + REQUIRE(mat4.Get(1, 1) == 13); + + mat4 = startMatrix; + mat4.SetSubMatrix(mat5, 1, 1); + REQUIRE(mat4.Get(1, 1) == 10); + REQUIRE(mat4.Get(1, 2) == 11); + REQUIRE(mat4.Get(2, 1) == 12); + REQUIRE(mat4.Get(2, 2) == 13); + + Matrix<3, 1> mat6{10, 11, 12}; + mat4.SetSubMatrix(mat6, 0, 0); + REQUIRE(mat4.Get(0, 0) == 10); + REQUIRE(mat4.Get(1, 0) == 11); + REQUIRE(mat4.Get(2, 0) == 12); + + Matrix<1, 3> mat7{10, 11, 12}; + mat4.SetSubMatrix(mat7, 0, 0); + REQUIRE(mat4.Get(0, 0) == 10); + REQUIRE(mat4.Get(0, 1) == 11); + REQUIRE(mat4.Get(0, 2) == 12); + } } // basically re-run all of the previous tests with huge matrices and time the @@ -418,7 +487,7 @@ TEST_CASE("Timing Tests", "Matrix") { for (uint32_t i{0}; i < 100000; i++) { - mat4.Invert(mat5); + mat5 = mat4.Invert(); } }; @@ -426,7 +495,7 @@ TEST_CASE("Timing Tests", "Matrix") { for (uint32_t i{0}; i < 10000; i++) { - mat1.Transpose(mat3); + mat3 = mat1.Transpose(); } }