From 4c217a4b20ab57c43e0cb0e57dc939ff861962f3 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 10 Feb 2024 16:58:44 +0100 Subject: [PATCH] add basic test --- src/Matrix.cpp | 14 ++++++++++++++ src/Matrix.h | 2 ++ test/mainTest.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ xmake.lua | 12 ++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 test/mainTest.cpp diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 535b7f9..22a62a9 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -134,6 +134,20 @@ bool Matrix::IsInversed() const { return true; } +bool Matrix::operator==(const Matrix& other) const { + if (m_Lignes != other.m_Lignes || m_Colonnes != other.m_Colonnes) + return false; + + for (std::size_t i = 0; i < m_Lignes; i++) { + for (std::size_t j = 0; j < m_Colonnes; j++) { + if (!IsEqualZero(at(i, j) - other.at(i, j))) + return false; + } + } + + return true; +} + void Matrix::GaussNonJordan(bool reduite) { int r = -1; for (std::size_t j = 0; j < m_Colonnes; j++) { diff --git a/src/Matrix.h b/src/Matrix.h index fe397e8..8ff0ec1 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -37,6 +37,8 @@ class Matrix { bool IsInversed() const; + bool operator==(const Matrix& other) const; + long double& operator[](std::size_t indice); long double& at(std::size_t ligne, std::size_t colonne); diff --git a/test/mainTest.cpp b/test/mainTest.cpp new file mode 100644 index 0000000..1748358 --- /dev/null +++ b/test/mainTest.cpp @@ -0,0 +1,45 @@ +#include "Matrix.h" +#include + +#ifdef NDEBUG +#error "Il faut ĂȘtre en debug mode ! xmake f -m debug" +#endif +struct Test{ + Matrix mat; + Matrix res; +}; + +static const std::vector TEST_MATRICES = { + // test 1 + {{3, 3, { + 1, 2, 3, + 4, 5, 6, + 7, 8, 9, + }}, {3, 3, { + 1, 0, -1, + 0, 1, 2, + 0, 0, 0, + }}}, + // test 2 + {{3, 3, { + 4, 5, 6, + 1, 2, 3, + 7, 8, 9, + }}, {3, 3, { + 1, 0, -1, + 0, 1, 2, + 0, 0, 0, + }}} +}; + +void test() { + for (Test test : TEST_MATRICES) { + test.mat.GaussJordan(true); + assert(test.mat == test.res); + } +} + +int main(int argc, char** argv) { + test(); + return 0; +} diff --git a/xmake.lua b/xmake.lua index 769129f..0cd4b3f 100644 --- a/xmake.lua +++ b/xmake.lua @@ -6,6 +6,18 @@ target("Pivot") set_rundir("$(projectdir)/matricies") set_languages("c++17") + + + + +target("PivotTest") + set_kind("binary") + add_files("test/*.cpp", "src/Matrix.cpp") + add_includedirs("src") + set_default(false) + add_tests("compile_and_run") + set_rundir("$(projectdir)/matricies") + -- -- If you want to known more usage about xmake, please see https://xmake.io --