diff --git a/test/test_assert.h b/test/test_assert.h new file mode 100644 index 0000000..70faae9 --- /dev/null +++ b/test/test_assert.h @@ -0,0 +1,43 @@ +#pragma once + +/** + * \file Test.h + * \brief File containing unit testing utilities + */ + +#include +#include + +/** + * \def TEST_SUCCESSFUL + * \brief Used in tests to indicate that a test was successful + */ +#define TEST_SUCCESSFUL 0 + +/** + * \def TEST_FAILED + * \brief Used in tests to indicate that a test failed + */ +#define TEST_FAILED 1 + +#ifndef __FUNCTION_NAME__ +#ifdef _WIN32 +#define __FUNCTION_NAME__ __FUNCTION__ +#else +#define __FUNCTION_NAME__ __PRETTY_FUNCTION__ +#endif +#endif + +/** + * \def test_assert + * \param ... The expression to evaluate + * \brief Evaluates the expression and exits the program if not valid. + * \note It works like a basic assert() but also in release mode + */ +#define test_assert(...) \ + if (!static_cast(__VA_ARGS__)) { \ + std::cout << __FILE__ << ":" << __LINE__ << ": " << __FUNCTION_NAME__ << ": Assertion failed !\n"; \ + std::cout << " " << __LINE__ << " |\t" << #__VA_ARGS__ << std::endl; \ + std::exit(TEST_FAILED); \ + } + diff --git a/test/test_jordan.cpp b/test/test_jordan.cpp index d61e51a..5ebd205 100644 --- a/test/test_jordan.cpp +++ b/test/test_jordan.cpp @@ -1,10 +1,6 @@ #include "Gauss.h" #include "Matrix.h" -#include - -#ifdef NDEBUG -#error "Il faut être en debug mode ! xmake f -m debug" -#endif +#include "test_assert.h" struct Test { Matrix mat; @@ -37,7 +33,7 @@ static const std::vector TEST_MATRICES = { void test() { for (Test test : TEST_MATRICES) { Gauss::GaussJordan(test.mat, true, true); - assert(test.mat == test.res); + test_assert(test.mat == test.res); } } diff --git a/test/test_random_kernel.cpp b/test/test_random_kernel.cpp index 964ab23..f706c2a 100644 --- a/test/test_random_kernel.cpp +++ b/test/test_random_kernel.cpp @@ -1,5 +1,6 @@ #include "Solver.h" -#include + +#include "test_assert.h" #include #include #include @@ -42,18 +43,18 @@ static void Test() { nullVector.Fill(0.0); for (std::size_t i = 0; i < kernel.GetCardinal(); i++) { - assert(matrix * kernel.GetVector(i) == nullVector); + test_assert(matrix * kernel.GetVector(i) == nullVector); } for (std::size_t i = 0; i < KERNEL_CHECKS; i++) { Matrix vector = GetRandomMatrix(kernel.GetDimension(), 1); - assert(kernel.IsElementOf(vector) == (matrix * vector == nullVector)); + test_assert(kernel.IsElementOf(vector) == (matrix * vector == nullVector)); } Vect kernel2 = solver.Kernel(kernel.GetLinearSystem()); - assert(kernel == kernel2); + test_assert(kernel == kernel2); } int main() { @@ -61,6 +62,7 @@ int main() { std::vector> results; + // appelle la fonction Test() en parallèle for (int i = 0; i < EXECUTION_COUNT; i++) { auto handle = std::async(std::launch::async, &Test); results.push_back(std::move(handle)); diff --git a/test/test_rational.cpp b/test/test_rational.cpp index 2731813..a52d3ef 100644 --- a/test/test_rational.cpp +++ b/test/test_rational.cpp @@ -1,25 +1,25 @@ #include "NR.h" -#include +#include "test_assert.h" static void test() { - assert((NR {1, 5} == NR {5, 25})); - assert((NR {1, 5} != NR {4, 25})); + test_assert((NR {1, 5} == NR {5, 25})); + test_assert((NR {1, 5} != NR {4, 25})); - assert(NR {2} == NR {1} + 1); - assert(NR {1} == (NR {1, 4} + NR {3, 4})); - assert((NR {-3, -4} == NR {1, 2} + NR {1, 4})); + test_assert(NR {2} == NR {1} + 1); + test_assert(NR {1} == (NR {1, 4} + NR {3, 4})); + test_assert((NR {-3, -4} == NR {1, 2} + NR {1, 4})); - assert((NR {-1, 4} == NR {1, 4} - NR {1, 2})); - assert((NR {1, -4} == NR {1, 4} - NR {1, 2})); - assert((-NR {1, 4} == NR {1, 4} - NR {1, 2})); + test_assert((NR {-1, 4} == NR {1, 4} - NR {1, 2})); + test_assert((NR {1, -4} == NR {1, 4} - NR {1, 2})); + test_assert((-NR {1, 4} == NR {1, 4} - NR {1, 2})); - assert((NR {2} == NR {4, 3} * NR {3, 2})); - assert((NR {3, 5} == NR {4, 5} * NR {3, 4})); + test_assert((NR {2} == NR {4, 3} * NR {3, 2})); + test_assert((NR {3, 5} == NR {4, 5} * NR {3, 4})); - assert((NR {21, 16} == NR {7, 8} / NR {6, 9})); + test_assert((NR {21, 16} == NR {7, 8} / NR {6, 9})); - assert((NR {4, 3} == NR {3, 4}.Inverse())); + test_assert((NR {4, 3} == NR {3, 4}.Inverse())); } int main(int argc, char** argv) { diff --git a/test/test_solver.cpp b/test/test_solver.cpp index bf825aa..4ba6169 100644 --- a/test/test_solver.cpp +++ b/test/test_solver.cpp @@ -1,10 +1,10 @@ -#include #include #include #include #include "IO.h" #include "Solver.h" +#include "test_assert.h" namespace fs = std::filesystem; @@ -39,8 +39,8 @@ void TestKernelImage() { Solver solver; - assert(solver.Image(mat) == image); - assert(solver.Kernel(mat) == noyau); + test_assert(solver.Image(mat) == image); + test_assert(solver.Kernel(mat) == noyau); } } diff --git a/test/test_vect.cpp b/test/test_vect.cpp index b5eb887..a857555 100644 --- a/test/test_vect.cpp +++ b/test/test_vect.cpp @@ -1,5 +1,5 @@ #include "Vect.h" -#include +#include "test_assert.h" void TestVect() { Vect vect1 {{3, 2, { @@ -22,22 +22,23 @@ void TestVect() { 0, 0, 1, 11, }}}; - assert(vect1 == vect3); - assert(vect2 == vect4); - assert(vect1 != vect2); - assert(vect2 != vect3); - assert(vect3 != vect4); - assert(vect1.IsElementOf(Matrix::ColumnVector({3, 7, 11}))); - assert(!vect1.IsElementOf(Matrix::ColumnVector({3, 7, 12}))); + test_assert(vect1 == vect3); + test_assert(vect2 == vect4); + test_assert(vect1 != vect2); + test_assert(vect2 != vect3); + test_assert(vect3 != vect4); + + test_assert(vect1.IsElementOf(Matrix::ColumnVector({3, 7, 11}))); + test_assert(!vect1.IsElementOf(Matrix::ColumnVector({3, 7, 12}))); } void TestVectAffine() { - VectAffine aff {Matrix {3, 1, {-2, 3, 7}}, Matrix::ColumnVector({5, 2, -8})}; + VectAffine aff {Matrix::ColumnVector({-2, 3, 7}), Matrix::ColumnVector({5, 2, -8})}; - assert(aff.IsElementOf(Matrix::ColumnVector({5, 2, -8}))); - assert(aff.IsElementOf(Matrix::ColumnVector({3, 5, -1}))); - assert(!aff.IsElementOf(Matrix::ColumnVector({1, 2, 3}))); + test_assert(aff.IsElementOf(Matrix::ColumnVector({5, 2, -8}))); + test_assert(aff.IsElementOf(Matrix::ColumnVector({3, 5, -1}))); + test_assert(!aff.IsElementOf(Matrix::ColumnVector({1, 2, 3}))); } int main() {