From 1fb211912db3f3e69739eb14f1aa0d03dd1a5c49 Mon Sep 17 00:00:00 2001 From: Cynopolis Date: Tue, 10 Dec 2024 23:55:23 -0500 Subject: [PATCH] Got first real unit test passing --- .vscode/launch.json | 34 ++++++++++++++++--------- .vscode/tasks.json | 16 ++++++++++++ Matrix.hpp | 49 ++++++++++++++++++++++++++++++------- unit-tests/matrix-tests.cpp | 16 ++++-------- 4 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 6cb6f46..d860e76 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,29 +5,39 @@ "version": "0.2.0", "configurations": [ { - "name": "Matrix Tests", + "name": "Debug Matrix Unit Tests", "type": "cppdbg", "request": "launch", - "program": "enter program name, for example ${workspaceFolder}/build/unit-tests/matrix-tests", - "args": [], + "program": "${workspaceFolder}/build/unit-tests/matrix-tests", + "args": [], "stopAtEntry": false, - "cwd": "${fileDirname}", + "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, - "MIMode": "gdb", + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", // Adjust to your debugger path "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true - }, - { - "description": "Set Disassembly Flavor to Intel", - "text": "-gdb-set disassembly-flavor intel", - "ignoreFailures": true } - ] + ], + "preLaunchTask": "build_tests", // Task to compile unit tests + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": "Run Matrix Unit Tests", + "type": "cpp", + "request": "launch", + "program": "${workspaceFolder}/build/unit-tests/matrix-tests", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "preLaunchTask": "build_tests", // Compile unit tests before running + "internalConsoleOptions": "openOnSessionStart" } - ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..45f53aa --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,16 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build_tests", + "type": "shell", + "command": "cd build && ninja matrix-tests", + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": ["$gcc"], + "detail": "Generated task to build unit test executable" + } + ] +} \ No newline at end of file diff --git a/Matrix.hpp b/Matrix.hpp index 353b34b..9ad0b61 100644 --- a/Matrix.hpp +++ b/Matrix.hpp @@ -15,6 +15,7 @@ public: */ Matrix(const std::array &array); + // TODO: Figure out how to do this /** * @brief Initialize a matrix directly with any number of arguments */ @@ -152,6 +153,8 @@ public: */ constexpr uint8_t GetColumnSize() { return columns; } + void ToString(std::string & stringBuffer) const; + private: /** * @brief take the dot product of the two vectors @@ -179,19 +182,18 @@ private: void normalize(Matrix &result) const; constexpr bool isSquare() { return rows == columns; } + + void setMatrixToArray(const std::array & array); + std::array, rows> matrix; }; -template Matrix::Matrix() { - this->zeroMatrix(); -} - template -Matrix::Matrix(const std::array &array) { +void Matrix::setMatrixToArray(const std::array & array){ for (uint8_t row_idx{0}; row_idx < rows; row_idx++) { for (uint8_t column_idx{0}; column_idx < columns; column_idx++) { uint16_t array_idx = - static_cast(row_idx) + static_cast(column_idx); + static_cast(row_idx) * static_cast(columns) + static_cast(column_idx); if (array_idx < array.size()) { this->matrix[row_idx][column_idx] = array[array_idx]; } else { @@ -201,15 +203,30 @@ Matrix::Matrix(const std::array &array) { } } +template Matrix::Matrix() { + this->zeroMatrix(); +} + +template +Matrix::Matrix(const std::array &array) { + this->setMatrixToArray(array); +} + // template // template // Matrix::Matrix(Args&&... args){ // // Initialize a std::array with the arguments -// std::array values = {args...}; +// if(typeid(args) == typeid(std::array)){ +// this->setMatrixToArray(args); +// } +// else{ +// std::array values = {static_cast(args)...}; -// // now call our other constructor which can take this array as an argument -// this = Matrix{values}; +// // now store the array in our internal matrix +// this->setMatrixToArray(values); +// } + // } template @@ -425,6 +442,20 @@ void Matrix::GetColumn(uint8_t column_index, } } +template +void Matrix::ToString(std::string & stringBuffer) const{ + for(uint8_t row_idx{0}; row_idx < rows; row_idx++){ + stringBuffer += "|"; + for(uint8_t column_idx{0}; column_idx < columns; column_idx++){ + stringBuffer += std::to_string(this->matrix[row_idx][column_idx]); + if(column_idx != columns - 1){ + stringBuffer += "\t"; + } + } + stringBuffer += "|\n"; + } +} + template template float Matrix::dotProduct(const Matrix &vec1, diff --git a/unit-tests/matrix-tests.cpp b/unit-tests/matrix-tests.cpp index c06a1f6..dd53aae 100644 --- a/unit-tests/matrix-tests.cpp +++ b/unit-tests/matrix-tests.cpp @@ -6,17 +6,7 @@ // any other libraries #include - -unsigned int Factorial(unsigned int number) { - return number <= 1 ? number : Factorial(number - 1) * number; -} - -TEST_CASE("Factorials are computed", "[factorial]") { - REQUIRE(Factorial(1) == 1); - REQUIRE(Factorial(2) == 2); - REQUIRE(Factorial(3) == 6); - REQUIRE(Factorial(10) == 3628800); -} +#include TEST_CASE("Matrix Addition", "Matrix::Add") { std::array arr1{1, 2, 3, 4}; @@ -24,6 +14,10 @@ TEST_CASE("Matrix Addition", "Matrix::Add") { Matrix<2, 2> mat1{arr1}; Matrix<2, 2> mat2{arr2}; + std::string strBuf1 = ""; + mat1.ToString(strBuf1); + std::cout << strBuf1 << std::endl; + Matrix<2, 2> mat3{}; mat1.Add(mat2, mat3);