Got first real unit test passing
This commit is contained in:
34
.vscode/launch.json
vendored
34
.vscode/launch.json
vendored
@@ -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"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
16
.vscode/tasks.json
vendored
Normal file
16
.vscode/tasks.json
vendored
Normal file
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
49
Matrix.hpp
49
Matrix.hpp
@@ -15,6 +15,7 @@ public:
|
||||
*/
|
||||
Matrix(const std::array<float, rows*columns> &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<rows, columns> &result) const;
|
||||
|
||||
constexpr bool isSquare() { return rows == columns; }
|
||||
|
||||
void setMatrixToArray(const std::array<float, rows*columns> & array);
|
||||
|
||||
std::array<std::array<float, columns>, rows> matrix;
|
||||
};
|
||||
|
||||
template <uint8_t rows, uint8_t columns> Matrix<rows, columns>::Matrix() {
|
||||
this->zeroMatrix();
|
||||
}
|
||||
|
||||
template <uint8_t rows, uint8_t columns>
|
||||
Matrix<rows, columns>::Matrix(const std::array<float, rows*columns> &array) {
|
||||
void Matrix<rows, columns>::setMatrixToArray(const std::array<float, rows*columns> & 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<uint16_t>(row_idx) + static_cast<uint16_t>(column_idx);
|
||||
static_cast<uint16_t>(row_idx) * static_cast<uint16_t>(columns) + static_cast<uint16_t>(column_idx);
|
||||
if (array_idx < array.size()) {
|
||||
this->matrix[row_idx][column_idx] = array[array_idx];
|
||||
} else {
|
||||
@@ -201,15 +203,30 @@ Matrix<rows, columns>::Matrix(const std::array<float, rows*columns> &array) {
|
||||
}
|
||||
}
|
||||
|
||||
template <uint8_t rows, uint8_t columns> Matrix<rows, columns>::Matrix() {
|
||||
this->zeroMatrix();
|
||||
}
|
||||
|
||||
template <uint8_t rows, uint8_t columns>
|
||||
Matrix<rows, columns>::Matrix(const std::array<float, rows*columns> &array) {
|
||||
this->setMatrixToArray(array);
|
||||
}
|
||||
|
||||
// template <uint8_t rows, uint8_t columns>
|
||||
// template <typename... Args>
|
||||
// Matrix<rows, columns>::Matrix(Args&&... args){
|
||||
|
||||
// // Initialize a std::array with the arguments
|
||||
// std::array<float, sizeof...(args)> values = {args...};
|
||||
// if(typeid(args) == typeid(std::array<float, 4>)){
|
||||
// this->setMatrixToArray(args);
|
||||
// }
|
||||
// else{
|
||||
// std::array<float, rows*columns> values = {static_cast<float>(args)...};
|
||||
|
||||
// // now call our other constructor which can take this array as an argument
|
||||
// this = Matrix<rows, columns>{values};
|
||||
// // now store the array in our internal matrix
|
||||
// this->setMatrixToArray(values);
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
template <uint8_t rows, uint8_t columns>
|
||||
@@ -425,6 +442,20 @@ void Matrix<rows, columns>::GetColumn(uint8_t column_index,
|
||||
}
|
||||
}
|
||||
|
||||
template <uint8_t rows, uint8_t columns>
|
||||
void Matrix<rows, columns>::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 <uint8_t rows, uint8_t columns>
|
||||
template <uint8_t vector_size>
|
||||
float Matrix<rows, columns>::dotProduct(const Matrix<vector_size, 1> &vec1,
|
||||
|
||||
@@ -6,17 +6,7 @@
|
||||
|
||||
// any other libraries
|
||||
#include <array>
|
||||
|
||||
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 <iostream>
|
||||
|
||||
TEST_CASE("Matrix Addition", "Matrix::Add") {
|
||||
std::array<float, 4> 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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user