Skip to content

Commit

Permalink
fix matrix == and != and add tests (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
petiaccja authored Jun 16, 2024
1 parent 975363b commit f64eb78
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
26 changes: 22 additions & 4 deletions include/Mathter/Matrix/MatrixCompare.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@

namespace mathter {

template <int Rows, int Columns, class T1, class T2, eMatrixOrder Order1, eMatrixOrder Order2, eMatrixLayout Layout1, eMatrixLayout Layout2, bool Packed1, bool Packed2>
bool operator==(const Matrix<T1, Rows, Columns, Order1, Layout1, Packed1>& lhs, const Matrix<T2, Rows, Columns, Order2, Layout2, Packed2>& rhs) {
template <int Rows, int Columns, class T1, class T2, eMatrixOrder Order, eMatrixLayout Layout1, eMatrixLayout Layout2, bool Packed1, bool Packed2>
bool operator==(const Matrix<T1, Rows, Columns, Order, Layout1, Packed1>& lhs, const Matrix<T2, Columns, Rows, traits::OppositeOrder<Order>::value, Layout2, Packed2>& rhs) {
bool equal = true;
for (int i = 0; i < Rows; ++i) {
for (int j = 0; j < Columns; ++j) {
equal = equal && lhs(i, j) == rhs(j, i);
}
}
return equal;
}


template <int Rows, int Columns, class T1, class T2, eMatrixOrder Order, eMatrixLayout Layout1, eMatrixLayout Layout2, bool Packed1, bool Packed2>
bool operator==(const Matrix<T1, Rows, Columns, Order, Layout1, Packed1>& lhs, const Matrix<T2, Rows, Columns, Order, Layout2, Packed2>& rhs) {
bool equal = true;
for (int i = 0; i < Rows; ++i) {
for (int j = 0; j < Columns; ++j) {
Expand All @@ -20,10 +32,16 @@ bool operator==(const Matrix<T1, Rows, Columns, Order1, Layout1, Packed1>& lhs,
return equal;
}

template <int Rows, int Columns, class T1, class T2, eMatrixOrder Order1, eMatrixOrder Order2, eMatrixLayout Layout1, eMatrixLayout Layout2, bool Packed1, bool Packed2>
bool operator!=(const Matrix<T1, Rows, Columns, Order1, Layout1, Packed1>& lhs, const Matrix<T2, Rows, Columns, Order2, Layout2, Packed2>& rhs) {

template <int Rows, int Columns, class T1, class T2, eMatrixOrder Order, eMatrixLayout Layout1, eMatrixLayout Layout2, bool Packed1, bool Packed2>
bool operator!=(const Matrix<T1, Rows, Columns, Order, Layout1, Packed1>& lhs, const Matrix<T2, Columns, Rows, traits::OppositeOrder<Order>::value, Layout2, Packed2>& rhs) {
return !(lhs == rhs);
}


template <int Rows, int Columns, class T1, class T2, eMatrixOrder Order, eMatrixLayout Layout1, eMatrixLayout Layout2, bool Packed1, bool Packed2>
bool operator!=(const Matrix<T1, Rows, Columns, Order, Layout1, Packed1>& lhs, const Matrix<T2, Rows, Columns, Order, Layout2, Packed2>& rhs) {
return !(lhs == rhs);
}

} // namespace mathter
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ target_sources(UnitTest
"TestIntersection.cpp"
"TestMatrix/TestMatrixImpl.cpp"
"TestMatrix/TestMatrixArithmetic.cpp"
"TestMatrix/TestMatrixCompare.cpp"
"TestMatrix/TestMatrixVectorArithmetic.cpp"
"TestMatrix/TestMatrixDecomposition.cpp"
"TestMatrix/TestMatrixFunction.cpp"
Expand All @@ -17,7 +18,7 @@ target_sources(UnitTest
"TestVector/TestVectorArithmetic.cpp"
"TestGenerators.hpp"
"TestGeometry.cpp"
)
)

find_package(Catch2 REQUIRED)

Expand Down
54 changes: 54 additions & 0 deletions test/TestMatrix/TestMatrixCompare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// L=============================================================================
// L This software is distributed under the MIT license.
// L Copyright 2024 Péter Kardos
// L=============================================================================

#pragma warning(disable : 4244)

#include "../TestGenerators.hpp"

#include <Mathter/Common/Approx.hpp>
#include <Mathter/Common/Traits.hpp>
#include <Mathter/Matrix.hpp>

#include <catch2/catch_approx.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <complex>


using namespace mathter;
using Catch::Approx;


using TypeListAll = TestTypeList<TypesAll, PackedAll, OrdersAll, LayoutsAll>;


TEMPLATE_LIST_TEST_CASE("Matrix - Compare", "[Matrix]", TypeListAll) {
SECTION(TestType::Name()) {
using M32 = typename TestType::template Matrix<3, 2>;
using M32L = invert_layout_t<M32>;
using M23O = invert_order_t<M32>;
M32 value = { 1, 2, 3, 4, 5, 6 };

SECTION("equal values") {
M32L valueL = { 1, 2, 3, 4, 5, 6 };
M23O valueO = { 1, 3, 5, 2, 4, 6 };

REQUIRE(value == valueL);
REQUIRE(value == valueO);
REQUIRE(!(value != valueL));
REQUIRE(!(value != valueO));
}

SECTION("unequal values") {
M32L valueL = { 1, 2, 3, 4, 5, 7 };
M23O valueO = { 1, 3, 5, 2, 4, 7 };

REQUIRE(value != valueL);
REQUIRE(value != valueO);
REQUIRE(!(value == valueL));
REQUIRE(!(value == valueO));
}
}
}

0 comments on commit f64eb78

Please sign in to comment.