From 07ec32c9695b3881590a359b0f9a13603a495748 Mon Sep 17 00:00:00 2001 From: peri4 Date: Tue, 5 Oct 2021 20:31:00 +0300 Subject: [PATCH] tests --- tests/math/testpimathmatrix.cpp | 146 ++++----- tests/math/testpimathmatrixt.cpp | 488 +++++++++++++++++++++++++++++++ tests/math/testpivector2d.cpp | 84 ++++++ 3 files changed, 637 insertions(+), 81 deletions(-) create mode 100644 tests/math/testpimathmatrixt.cpp create mode 100644 tests/math/testpivector2d.cpp diff --git a/tests/math/testpimathmatrix.cpp b/tests/math/testpimathmatrix.cpp index bf75203c..07b65215 100644 --- a/tests/math/testpimathmatrix.cpp +++ b/tests/math/testpimathmatrix.cpp @@ -3,32 +3,31 @@ template bool cmpSquareMatrixWithValue(PIMathMatrix matrix, Type val, int num) { - bool b = true; for(int i = 0; i < num; i++) { for(int j = 0; j < num; j++) { if(matrix.element(i, j) != val) { - b = false; + return false; } } } - return b; + return true; } TEST(PIMathMatrix_Test, constructor1) { - PIMathMatrix matrix(3, 3, 5.0); - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 5.0, 3)); + PIMathMatrix matrix(3, 3, 5.0); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 5.0, 3)); } TEST(PIMathMatrix_Test, constructor2) { - PIVector vector(2, 5.0); - PIMathMatrix> matrix(3, 3, vector); - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, vector, 3)); + PIVector vector(2, 5.0); + PIMathMatrix> matrix(3, 3, vector); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, vector, 3)); } TEST(PIMathMatrix_Test, constructor3) { - PIVector2D vector(2, 2, 5.0); - PIMathMatrix matrix(vector); - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 5.0, 2)); + PIVector2D vector(2, 2, 5.0); + PIMathMatrix matrix(vector); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 5.0, 2)); } TEST(PIMathMatrix_Test, identity1) { @@ -37,53 +36,38 @@ TEST(PIMathMatrix_Test, identity1) { for(int j = 0; j < 3; j++) { if(i != j) { if(matrix[i][j] != 0.0){ - ASSERT_TRUE(false); + FAIL(); } } else { if(matrix[i][i] != 1.0){ - ASSERT_TRUE(false); + FAIL(); } } } } + SUCCEED(); } TEST(PIMathMatrix_Test, identity2) { - auto matrix = PIMathMatrix::identity(4, 3); - for(int i = 0; i < 3; i++) { - for(int j = 0; j < 4; j++) { - if(i != j) { - if(matrix[i][j] != 0.0){ - ASSERT_TRUE(false); - } - } - else { - if(matrix[i][i] != 1.0){ - ASSERT_TRUE(false); - } - } - } - } + auto matrix = PIMathMatrix::identity(4, 3); + for(int i = 0; i < 3; i++) { + for(int j = 0; j < 4; j++) { + if(matrix.element(i,j) != (i == j ? 1. : 0.)) + FAIL(); + } + } } TEST(PIMathMatrixT_Test, element) { auto matrix = PIMathMatrix::identity(3, 3); - for(int i = 0; i < 3; i++){ - for(int j = 0; j < 3; j++){ - if(i != j){ - if(matrix[i][j] != 0.0){ - ASSERT_TRUE(false); - } - } - else { - if(matrix.element(i,i) != 1.0) { - ASSERT_TRUE(false); - } - } + for(int i = 0; i < 3; i++) { + for(int j = 0; j < 3; j++) { + if(matrix.element(i,j) != (i == j ? 1. : 0.)) + FAIL(); } } - ASSERT_TRUE(true); + SUCCEED(); } TEST(PIMathMatrix_Test, matrixRow) { @@ -92,10 +76,10 @@ TEST(PIMathMatrix_Test, matrixRow) { auto matrix = PIMathMatrix::matrixRow(vector); for(uint i = 0; i < vector.size(); i++) { if(matrix[0][i] != 3.0) { - ASSERT_TRUE(false); + FAIL(); } } - ASSERT_TRUE(true); + SUCCEED(); } TEST(PIMathMatrix_Test, matrixCol) { @@ -104,10 +88,10 @@ TEST(PIMathMatrix_Test, matrixCol) { auto matrix = PIMathMatrix::matrixCol(vector); for(uint i = 0; i < vector.size(); i++) { if(matrix[i][0] != 3.0) { - ASSERT_TRUE(false); + FAIL(); } } - ASSERT_TRUE(true); + SUCCEED(); } TEST(PIMathMatrix_Test, setCol) { @@ -118,10 +102,10 @@ TEST(PIMathMatrix_Test, setCol) { matrix.setCol(0, vector); for(uint i = 0; i < vector.size(); i++) { if(matrix[i][0] != 10.0) { - ASSERT_TRUE(false); + FAIL(); } } - ASSERT_TRUE(true); + SUCCEED(); } TEST(PIMathMatrix_Test, setRow) { @@ -132,43 +116,43 @@ TEST(PIMathMatrix_Test, setRow) { matrix.setRow(0, vector); for(uint i = 0; i < vector.size(); i++) { if(matrix[0][i] != 10.0) { - ASSERT_TRUE(false); + FAIL(); } } - ASSERT_TRUE(true); + SUCCEED(); } TEST(PIMathMatrix_Test, swapCols) { - PIMathMatrix origMatr; - PIMathMatrix matrix1; - PIMathVector vector; - uint i1 = 0; uint i2 = 1; - double a1[3], a2[3], a3[3]; - double b1[3], b2[3], b3[3]; - vector.resize(3, 3.0); - vector[0] = 3.0; - vector[1] = 6.0; - vector[2] = 8.0; - matrix1 = origMatr.identity(3, 3); - matrix1.setCol(0, vector); - vector[0] = 2.0; - vector[1] = 1.0; - vector[2] = 4.0; - matrix1.setCol(1, vector); - vector[0] = 6.0; - vector[1] = 2.0; - vector[2] = 5.0; - matrix1.setCol(2, vector); - for(int i = 0; i < 3; i++) { - a1[i] = matrix1.element(i, 0); - a2[i] = matrix1.element(i, 1); - a3[i] = matrix1.element(i, 2); - } - matrix1.swapCols(i1, i2); - for(int i = 0; i < 3; i++) { - b1[i] = matrix1.element(i, 0); - b2[i] = matrix1.element(i, 1); - b3[i] = matrix1.element(i, 2); - } - ASSERT_TRUE((memcmp(a1, b2, sizeof(b1)) == 0) && (memcmp(a2, b1, sizeof(b1)) == 0) && (memcmp(a3, b3, sizeof(b1)) == 0)); + PIMathMatrix origMatr; + PIMathMatrix matrix1; + PIMathVector vector; + uint i1 = 0; uint i2 = 1; + double a1[3], a2[3], a3[3]; + double b1[3], b2[3], b3[3]; + vector.resize(3, 3.0); + vector[0] = 3.0; + vector[1] = 6.0; + vector[2] = 8.0; + matrix1 = origMatr.identity(3, 3); + matrix1.setCol(0, vector); + vector[0] = 2.0; + vector[1] = 1.0; + vector[2] = 4.0; + matrix1.setCol(1, vector); + vector[0] = 6.0; + vector[1] = 2.0; + vector[2] = 5.0; + matrix1.setCol(2, vector); + for(int i = 0; i < 3; i++) { + a1[i] = matrix1.element(i, 0); + a2[i] = matrix1.element(i, 1); + a3[i] = matrix1.element(i, 2); + } + matrix1.swapCols(i1, i2); + for(int i = 0; i < 3; i++) { + b1[i] = matrix1.element(i, 0); + b2[i] = matrix1.element(i, 1); + b3[i] = matrix1.element(i, 2); + } + ASSERT_TRUE((memcmp(a1, b2, sizeof(b1)) == 0) && (memcmp(a2, b1, sizeof(b1)) == 0) && (memcmp(a3, b3, sizeof(b1)) == 0)); } diff --git a/tests/math/testpimathmatrixt.cpp b/tests/math/testpimathmatrixt.cpp new file mode 100644 index 00000000..98fc78db --- /dev/null +++ b/tests/math/testpimathmatrixt.cpp @@ -0,0 +1,488 @@ +#include "gtest/gtest.h" +#include "pimathmatrix.h" + +const uint rows = 3; +const uint cols = 3; + +bool cmpSquareMatrixWithValue(PIMathMatrixT matrix, double val, int num) { + for(int i = 0; i < num; i++) { + for(int j = 0; j < num; j++) { + if(matrix.at(i, j) != val) { + return false; + } + } + } + return true; +} + +TEST(PIMathMatrixT_Test, identity) { + auto matrix = PIMathMatrixT::identity(); + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 3; j++){ + if(i != j){ + if(matrix[i][j] != 0.0){ + ASSERT_TRUE(false); + } + } + else { + if(matrix[i][i] != 1.0){ + ASSERT_TRUE(false); + } + } + } + } + ASSERT_TRUE(true); +} + +TEST(PIMathMatrixT_Test, at) { + auto matrix1 = PIMathMatrixT::identity(); + for(uint i = 0; i < rows; i++) { + if(matrix1.at(i,i) != 1.0) { + ASSERT_TRUE(false); + } + } + ASSERT_TRUE(true); +} +TEST(PIMathMatrixT_Test, filled) { + auto matr = PIMathMatrixT(1.0); + ASSERT_TRUE(cmpSquareMatrixWithValue(matr, 1.0, rows)); +} + +TEST(PIMathMatrixT_Test, cols) { + PIMathMatrixT matr; + ASSERT_EQ(cols,matr.cols()); +} + +TEST(PIMathMatrixT_Test, rows) { + PIMathMatrixT matr; + ASSERT_EQ(rows,matr.rows()); +} + +TEST(PIMathMatrixT_Test, col) { + PIMathMatrixT matr; + PIMathVectorT vect; + uint g = 2; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + vect = matr.col(g); + for(uint i = 0; i < matr.cols(); i++) { + if(matr.element(i, g) != vect[i]) { + ASSERT_TRUE(false); + } + } + ASSERT_TRUE(true); +} + +TEST(PIMathMatrixT_Test, row) { + PIMathMatrixT matr; + PIMathVectorT vect; + uint g = 2; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + vect = matr.row(g); + for(uint i = 0; i < matr.rows(); i++) { + if(matr.element(g, i) != vect[i]) { + ASSERT_TRUE(false); + } + } + ASSERT_TRUE(true); +} + +TEST(PIMathMatrixT_Test, setCol) { + PIMathMatrixT matr; + PIMathVectorT vect; + vect[0] = 1.0; + vect[1] = 3.0; + vect[2] = 5.0; + uint g = 1; + matr.setCol(g, vect); + for(uint i = 0; i < vect.size(); i++) { + if(matr.element(i, g) != vect[i]) { + ASSERT_TRUE(false); + } + } + ASSERT_TRUE(true); +} + +TEST(PIMathMatrixT_Test, setRow) { + PIMathMatrixT matr; + PIMathVectorT vect; + vect[0] = 1.0; + vect[1] = 3.0; + vect[2] = 5.0; + uint g = 1; + matr.setRow(g, vect); + for(uint i = 0; i < vect.size(); i++) { + if(matr.element(g,i) != vect[i]) { + ASSERT_TRUE(false); + } + } + ASSERT_TRUE(true); +} + +TEST(PIMathMatrixT_Test, swapCols) { + PIMathMatrixT matr; + int g1 = 1, g2 = 2; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + const PIMathVectorT before_Vect1 = matr.col(g1); + const PIMathVectorT before_Vect2 = matr.col(g2); + matr.swapCols(g1, g2); + const PIMathVectorT after_Vect1 = matr.col(g1); + const PIMathVectorT after_Vect2 = matr.col(g2); + if((before_Vect1 == after_Vect2) && (before_Vect2 == after_Vect1)) { + ASSERT_TRUE(true); + } + else { + ASSERT_TRUE(false); + } +} + +TEST(PIMathMatrixT_Test, swapRows) { + PIMathMatrixT matr; + int g1 = 1, g2 = 2; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + const PIMathVectorT before_Vect1 = matr.row(g1); + const PIMathVectorT before_Vect2 = matr.row(g2); + matr.swapRows(g1, g2); + const PIMathVectorT after_Vect1 = matr.row(g1); + const PIMathVectorT after_Vect2 = matr.row(g2); + if((before_Vect1 == after_Vect2) && (before_Vect2 == after_Vect1)) { + ASSERT_TRUE(true); + } + else { + ASSERT_TRUE(false); + } +} + +TEST(PIMathMatrixT_Test, fill) { + PIMathMatrixT matr; + PIMathMatrixT matrix1; + double g = 1.0; + matr.fill(g); + for(uint i = 0; i < cols; i++) { + for(uint j = 0; j < rows; j++) { + matrix1.element(j,i) = g; + } + } + ASSERT_TRUE(matr == matrix1); +} + +TEST(PIMathMatrixT_Test, isSquareTrue) { + PIMathMatrixT matrix1; + ASSERT_TRUE(matrix1.isSquare()); +} + +TEST(PIMathMatrixT_Test, isSquareFalse) { + const uint new_Cols = 4; + PIMathMatrixT matrix2; + ASSERT_FALSE(matrix2.isSquare()); +} + +TEST(PIMathMatrixT_Test, isIdentityTrue) { + auto matrix1 = PIMathMatrixT::identity(); + ASSERT_TRUE(matrix1.isIdentity()); +} + +TEST(PIMathMatrixT_Test, isIdentityFalse) { + auto matrix1 = PIMathMatrixT(2.5); + ASSERT_FALSE(matrix1.isIdentity()); +} + +TEST(PIMathMatrixT_Test, isNullTrue) { + PIMathMatrixT matrix1; + ASSERT_TRUE(matrix1.isNull()); +} + +TEST(PIMathMatrixT_Test, isNullFalse) { + auto matrix1 = PIMathMatrixT::identity(); + ASSERT_FALSE(matrix1.isNull()); +} + +TEST(PIMathMatrixT_Test, operator_Assignment) { + PIMathMatrixT matrix1; + auto matrix2 = PIMathMatrixT(6.72); + matrix1 = matrix2; + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 6.72, rows)); +} + +TEST(PIMathMatrixT_Test, operator_EqualTrue) { + PIMathMatrixT matrix1; + PIMathMatrixT matrix2; + matrix1.element(0, 0) = 5.1; + matrix1.element(0, 1) = 1.21; + matrix1.element(1, 1) = 0.671; + matrix1.element(1, 0) = 2.623; + matrix2.element(0, 0) = 5.1; + matrix2.element(0, 1) = 1.21; + matrix2.element(1, 1) = 0.671; + matrix2.element(1, 0) = 2.623; + ASSERT_TRUE(matrix1 == matrix2); +} + +TEST(PIMathMatrixT_Test, operator_EqualFalse) { + PIMathMatrixT matrix1; + PIMathMatrixT matrix2; + matrix1.element(0, 0) = 5.1; + matrix1.element(0, 1) = 1.21; + matrix1.element(1, 1) = 0.671; + matrix1.element(1, 0) = 2.623; + matrix2.element(0, 0) = 5.1; + matrix2.element(0, 1) = 1.21; + matrix2.element(1, 1) = 665.671; + matrix2.element(1, 0) = 2.623; + ASSERT_FALSE(matrix1 == matrix2); +} + +TEST(PIMathMatrixT_Test, operator_Not_EqualTrue) { + PIMathMatrixT matrix1; + PIMathMatrixT matrix2; + matrix1.element(0, 0) = 5.1; + matrix1.element(0, 1) = 1.21; + matrix1.element(1, 1) = 0.671; + matrix1.element(1, 0) = 2.623; + matrix2.element(0, 0) = 5.1; + matrix2.element(0, 1) = 1.21; + matrix2.element(1, 1) = 665.671; + matrix2.element(1, 0) = 2.623; + ASSERT_TRUE(matrix1 != matrix2); +} + +TEST(PIMathMatrixT_Test, operator_Not_EqualFalse) { + PIMathMatrixT matrix1; + PIMathMatrixT matrix2; + matrix1.element(0, 0) = 5.1; + matrix1.element(0, 1) = 1.21; + matrix1.element(1, 1) = 0.671; + matrix1.element(1, 0) = 2.623; + matrix2.element(0, 0) = 5.1; + matrix2.element(0, 1) = 1.21; + matrix2.element(1, 1) = 0.671; + matrix2.element(1, 0) = 2.623; + ASSERT_FALSE(matrix1 != matrix2); +} + +TEST(PIMathMatrixT_Test, operator_Addition_Assignment) { + auto matrix1 = PIMathMatrixT(6.72) ; + auto matrix2 = PIMathMatrixT(1.0) ; + matrix1 += matrix2; + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 7.72, rows)); +} + +TEST(PIMathMatrixT_Test, operator_Subtraction_Assignment) { + auto matrix1 = PIMathMatrixT(1.0); + auto matrix2 = PIMathMatrixT(6.72); + matrix1 -= matrix2; + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, -5.72, rows)); +} + +TEST(PIMathMatrixT_Test, operator_Multiplication_Assignment) { + auto matrix1 = PIMathMatrixT(6.72); + matrix1 *= 2.0; + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 13.44, rows)); +} + +TEST(PIMathMatrixT_Test, operator_Division_Assignment) { + auto matrix1 = PIMathMatrixT(6.72); + matrix1 /= 2.0; + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 3.36, rows)); +} + +TEST(PIMathMatrixT_Test, operator_Addition) { + auto matrix1 = PIMathMatrixT(6.72); + auto matrix2 = PIMathMatrixT(8.28); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 + matrix2, 15.0, rows)); +} + +TEST(PIMathMatrixT_Test, operator_Subtraction) { + auto matrix1 = PIMathMatrixT(6.0); + auto matrix2 = PIMathMatrixT(5.0); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 - matrix2, 1.0, rows)); +} + +TEST(PIMathMatrixT_Test, operator_Multiplication) { + auto matrix1 = PIMathMatrixT(6.72); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 * 4.0, 26.88, rows)); +} +TEST(PIMathMatrixT_Test, operator_Division) { + auto matrix1 = PIMathMatrixT(6.72); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 / 4.0, 1.68, rows)); +} + +TEST(PIMathMatrixT_Test, determinantIfSquare) { + double d; + double i = 59.0; + PIMathMatrixT matr; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + d = matr.determinant(); + ASSERT_DOUBLE_EQ(i, d); +} + +TEST(PIMathMatrixT_Test, invert) { + PIMathMatrixT matrix1; + PIMathMatrixT matrix2; + PIMathMatrixT matrix3; + PIMathMatrixT matr; + double d1, d2; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + matrix2 = matr; + matr.invert(); + d1 = matr.determinant(); + d2 = matrix2.determinant(); + matrix3 = matrix1; + matrix1.invert(); + ASSERT_TRUE((matrix1 == matrix3) && (d1 == 1/d2)); +} + +TEST(PIMathMatrixT_Test, inverted) { + PIMathMatrixT matrix1; + PIMathMatrixT matrix2; + PIMathMatrixT matrix3; + PIMathMatrixT matr; + double d1, d2; + matrix1 = matr.identity(); + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + matrix2 = matr.inverted(); + d1 = matr.determinant(); + d2 = matrix2.determinant(); + matrix3 = matrix1.inverted(); + ASSERT_TRUE((matrix1 == matrix3) && (round((1/d1)*10000)/10000 == round(d2*10000)/10000)); +} + +TEST(PIMathMatrixT_Test, toUpperTriangular) { + PIMathMatrixT matrix; + double d1, d2 = 1; + PIMathMatrixT matr; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + matrix = matr.toUpperTriangular(); + d1 = matrix.determinant(); + for(uint i = 0; i < cols; i++) + { + d2 = d2*matrix.at(i,i); + } + ASSERT_DOUBLE_EQ(d1, d2); +} + +TEST(PIMathMatrixT_Test, transposed) { + PIMathMatrixT matrix1; + PIMathMatrixT matrix2; + PIMathMatrixT matr; + double d1, d2; + matr.element(0,0) = 3; + matr.element(0,1) = 6; + matr.element(0,2) = 8; + matr.element(1,0) = 2; + matr.element(1,1) = 1; + matr.element(1,2) = 4; + matr.element(2,0) = 6; + matr.element(2,1) = 2; + matr.element(2,2) = 5; + d1 = matr.determinant(); + matrix1 = matr.transposed(); + d2 = matrix1.determinant(); + matrix2 = matrix1.transposed(); + ASSERT_TRUE((d1 == d2) && (matr == matrix2)); +} + +TEST(PIMathMatrixT_Test, rotation_2x2) { + double angle = 1.0; + auto matrix = PIMathMatrixT<2u, 2u, double>::identity(); + matrix.rotate(angle); + double c = cos(angle); + double s = sin(angle); + ASSERT_TRUE((c == matrix.at(1u,1u)) && (c == matrix.at(0u,0u)) && (-s == matrix.at(0u,1u)) && (s == matrix.at(1u,0u))); +} + +TEST(PIMathMatrixT_Test, matrixMultiplication) +{ + auto matrix1 = PIMathMatrixT(1.5); + auto matrix2 = PIMathMatrixT(2.5); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 * matrix2, 11.25, 3)); +} + +TEST(PIMathMatrixT_Test, matrixAndVectorMultiplication) { + auto matrix1 = PIMathMatrixT(1.5); + auto vector = PIMathVectorT(2.5); + for(uint i = 0; i < 2; i++) { + if((matrix1 * vector)[i] != 11.25) { + ASSERT_TRUE(false); + } + } + ASSERT_TRUE(true); +} + +TEST(PIMathMatrixT_Test, vectorAndMatrixMultiplication) { + auto matrix1 = PIMathMatrixT(1.5); + auto vector = PIMathVectorT(2.5); + for(uint i = 0; i < 2; i++) { + if((vector * matrix1)[i] != 11.25) { + ASSERT_TRUE(false); + } + } +} + +TEST(PIMathMatrixT_Test, valAndMatrixMultiplication) { + auto matrix1 = PIMathMatrixT(1.5); + ASSERT_TRUE(cmpSquareMatrixWithValue(25.0*matrix1, 37.5, 3)); +} diff --git a/tests/math/testpivector2d.cpp b/tests/math/testpivector2d.cpp new file mode 100644 index 00000000..ae66b01c --- /dev/null +++ b/tests/math/testpivector2d.cpp @@ -0,0 +1,84 @@ +#include "gtest/gtest.h" +#include "pivector2d.h" + +int ROWS_COUNT_INIT = 31; +int ROWS_COUNT_INCREASE = 41; +int ROWS_COUNT_REDUCE = 22; + +int COLS_COUNT_INIT = 34; +int COLS_COUNT_INCREASE = 44; +int COLS_COUNT_REDUCE = 13; + +void assert_fill_with(PIVector2D vec, int rows, int cols) { + for(int r = 0; r < rows; r++) { + for(int c = 0; c < cols; c++) { + ASSERT_EQ(vec.element(r, c), r * COLS_COUNT_INIT + c); + } + } +} + +class Vector2D : public ::testing::Test { +protected: + PIVector2D vec = PIVector2D(ROWS_COUNT_INIT, COLS_COUNT_INIT); + + void SetUp() override { + for (int r = 0; r < ROWS_COUNT_INIT; ++r) { + for (int c = 0; c < COLS_COUNT_INIT; ++c) { + vec.element(r, c) = r * COLS_COUNT_INIT + c; + } + } + } + + void resize_reduce_is_data_stay_consistent(int newRowsCount, int newColsCount) { + vec.resize(newRowsCount, newColsCount, 0); + assert_fill_with(vec, newRowsCount, newColsCount); + } + + void resize_increase_is_data_stay_consistent(int newRowsCount, int newColsCount) { + vec.resize(newRowsCount, newColsCount, 0); + assert_fill_with(vec, ROWS_COUNT_INIT, COLS_COUNT_INIT); + + for (int r = 0; r < newRowsCount; ++r) { + for (int c = 0; c < newColsCount; ++c) { + if (r < ROWS_COUNT_INIT || c < COLS_COUNT_INIT) continue; + ASSERT_EQ(vec.element(r, c), 0); + } + } + } +}; + +TEST_F(Vector2D, resize_is_increase_col_count) { + vec.resize(ROWS_COUNT_INIT, COLS_COUNT_INCREASE, 0); + ASSERT_EQ(vec.cols(), COLS_COUNT_INCREASE); +} + +TEST_F(Vector2D, resize_is_reduce_col_count) { + vec.resize(ROWS_COUNT_INIT, COLS_COUNT_REDUCE, 0); + ASSERT_EQ(vec.cols(), COLS_COUNT_REDUCE); +} + +TEST_F(Vector2D, resize_is_increase_rows_count) { + vec.resize(ROWS_COUNT_INCREASE, COLS_COUNT_INIT, 0); + ASSERT_EQ(vec.rows(), ROWS_COUNT_INCREASE); +} + +TEST_F(Vector2D, resize_is_reduce_rows_count) { + vec.resize(ROWS_COUNT_REDUCE, COLS_COUNT_INIT, 0); + ASSERT_EQ(vec.rows(), ROWS_COUNT_REDUCE); +} + +TEST_F(Vector2D, resize_increase_both_is_data_stay_consistent) { + resize_increase_is_data_stay_consistent(ROWS_COUNT_INCREASE, COLS_COUNT_INCREASE); +} + +TEST_F(Vector2D, resize_reduce_cols_is_data_stay_consistent) { + resize_reduce_is_data_stay_consistent(ROWS_COUNT_INIT, COLS_COUNT_REDUCE); +} + +TEST_F(Vector2D, resize_reduce_rows_is_data_stay_consistent) { + resize_reduce_is_data_stay_consistent(ROWS_COUNT_REDUCE, COLS_COUNT_INIT); +} + +TEST_F(Vector2D, resize_reduce_both_is_data_stay_consistent) { + resize_reduce_is_data_stay_consistent(ROWS_COUNT_REDUCE, COLS_COUNT_REDUCE); +}