diff --git a/lib/main/math/test/testpimathmatrix.cpp b/lib/main/math/test/testpimathmatrix.cpp index 8dbef289..4eeccd0c 100644 --- a/lib/main/math/test/testpimathmatrix.cpp +++ b/lib/main/math/test/testpimathmatrix.cpp @@ -1,18 +1,44 @@ #include "gtest/gtest.h" #include "pimathmatrix.h" -using namespace std; - -PIMathMatrix origMatr; +bool cmpMatrixWithValue(PIMathMatrix matrix, double val) +{ + int i = 0; + int j = 0; + int k = 0; + bool b; + while(i < 9) + { + if(k < 3) + { + if(matrix.element(j,k) == val) + { + b = true; + } + else + { + b = false; + break; + } + k++; + if(k == 3) + { + j++; + k = 0; + } + } + i++; + } + return b; +} TEST(PIMathMatrix_Test, identity) { + PIMathMatrix origMatr; PIMathMatrix matrix; int i; - double d; - bool a, b; + bool b; matrix = origMatr.identity(3, 3); - d = matrix.determinant(); for(i = 0; i < 3; i++) { if(matrix[i][i] == 1.0) @@ -25,19 +51,12 @@ TEST(PIMathMatrix_Test, identity) break; } } - if(d == 1.0) - { - a = true; - } - else - { - a = false; - } - ASSERT_TRUE(a && b); + ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, matrixRow) { + PIMathMatrix origMatr; PIMathMatrix matrix; PIMathVector vector; uint i; @@ -62,6 +81,7 @@ TEST(PIMathMatrix_Test, matrixRow) TEST(PIMathMatrix_Test, matrixCol) { + PIMathMatrix origMatr; PIMathMatrix matrix; PIMathVector vector; uint i; @@ -86,6 +106,7 @@ TEST(PIMathMatrix_Test, matrixCol) TEST(PIMathMatrix_Test, setCol) { + PIMathMatrix origMatr; PIMathMatrix matrix; PIMathVector vector; uint i; @@ -112,6 +133,7 @@ TEST(PIMathMatrix_Test, setCol) TEST(PIMathMatrix_Test, setRow) { + PIMathMatrix origMatr; PIMathMatrix matrix; PIMathVector vector; uint i; @@ -138,6 +160,7 @@ TEST(PIMathMatrix_Test, setRow) TEST(PIMathMatrix_Test, swapCols) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathVector vector; uint i1 = 0; uint i2 = 1; @@ -184,6 +207,7 @@ TEST(PIMathMatrix_Test, swapCols) TEST(PIMathMatrix_Test, swapRows) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathVector vector; uint i1 = 0; uint i2 = 1; @@ -229,41 +253,18 @@ TEST(PIMathMatrix_Test, swapRows) } TEST(PIMathMatrix_Test, fill) { + PIMathMatrix origMatr; PIMathMatrix matrix; - int i = 0; - int j = 0; - int k = 0; bool b; matrix = origMatr.identity(3, 3); matrix.fill(5.0); - while(i < 9) - { - if(k < 3) - { - if(matrix.element(j,k) == 5.0) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix, 5.0); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, isSquare) { - + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; @@ -282,6 +283,7 @@ TEST(PIMathMatrix_Test, isSquare) TEST(PIMathMatrix_Test, isIdentity) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; @@ -301,6 +303,7 @@ TEST(PIMathMatrix_Test, isIdentity) TEST(PIMathMatrix_Test, isNull) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; @@ -321,6 +324,7 @@ TEST(PIMathMatrix_Test, isNull) TEST(PIMathMatrix_Test, isValid) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; @@ -338,43 +342,21 @@ TEST(PIMathMatrix_Test, isValid) TEST(PIMathMatrix_Test, operator_Assignment) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; - int i = 0; - int j = 0; - int k = 0; bool b; matrix1 = origMatr.identity(3,3); matrix2 = origMatr.identity(3,3); matrix2.fill(6.72); matrix1 = matrix2; - while(i < 9) - { - if(k < 3) - { - if(matrix1.element(j,k) == 6.72) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 6.72); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, operator_Equal) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; PIMathMatrix matrix3; @@ -418,6 +400,7 @@ TEST(PIMathMatrix_Test, operator_Equal) TEST(PIMathMatrix_Test, operator_Not_Equal) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; PIMathMatrix matrix3; @@ -461,189 +444,75 @@ TEST(PIMathMatrix_Test, operator_Not_Equal) TEST(PIMathMatrix_Test, operator_Addition_Aassignment) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1 = origMatr.identity(3,3); matrix2 = origMatr.identity(3,3); matrix2.fill(6.72); matrix1.fill(1.0); matrix1 += matrix2; - while(i < 9) - { - if(k < 3) - { - if(matrix1.element(j,k) == 7.72) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 7.72); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, operator_Subtraction_Assignment) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1 = origMatr.identity(3,3); matrix2 = origMatr.identity(3,3); matrix2.fill(6.72); matrix1.fill(1.0); matrix1 -= matrix2; - while(i < 9) - { - if(k < 3) - { - if(matrix1.element(j,k) == -5.72) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, -5.72); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, operator_Multiplication_Assignment) { + PIMathMatrix origMatr; PIMathMatrix matrix1; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1 = origMatr.identity(3,3); matrix1.fill(6.72); matrix1 *= 2.0; - while(i < 9) - { - if(k < 3) - { - if(matrix1.element(j,k) == 13.44) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 13.44); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, operator_Division_Assignment) { + PIMathMatrix origMatr; PIMathMatrix matrix1; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1 = origMatr.identity(3,3); matrix1.fill(6.72); matrix1 /= 2.0; - while(i < 9) - { - if(k < 3) - { - if(matrix1.element(j,k) == 3.36) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 3.36); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, operator_Addition) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1 = origMatr.identity(3,3); matrix2 = origMatr.identity(3,3); matrix1.fill(6.72); matrix2.fill(8.28); - while(i < 9) - { - if(k < 3) - { - if((matrix1+matrix2).element(j,k) == 15.0) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1 + matrix2, 15.0); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, operator_Subtraction) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; @@ -666,79 +535,34 @@ TEST(PIMathMatrix_Test, operator_Subtraction) TEST(PIMathMatrix_Test, operator_Multiplication) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1 = origMatr.identity(3,3); matrix2 = origMatr.identity(3,3); matrix1.fill(6.72); matrix2 = matrix1*4.0; - while(i < 9) - { - if(k < 3) - { - if(matrix2.element(j,k) == 26.88) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix2, 26.88); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, operator_Division) { + PIMathMatrix origMatr; PIMathMatrix matrix1; PIMathMatrix matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1 = origMatr.identity(3,3); matrix2 = origMatr.identity(3,3); matrix1.fill(6.72); matrix2 = matrix1/4.0; - while(i < 9) - { - if(k < 3) - { - if(matrix2.element(j,k) == 1.68) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix2, 1.68); ASSERT_TRUE(b); } TEST(PIMathMatrix_Test, determinant) { + PIMathMatrix origMatr; double d; double i = 59.0; PIMathMatrix matrix; @@ -763,6 +587,7 @@ TEST(PIMathMatrix_Test, determinant) TEST(PIMathMatrix_Test, trace) { + PIMathMatrix origMatr; double t; double i = 9.0; PIMathMatrix matrix; @@ -787,6 +612,7 @@ TEST(PIMathMatrix_Test, trace) TEST(PIMathMatrix_Test, toUpperTriangular) { + PIMathMatrix origMatr; double d1, d2 = 1; int i; PIMathMatrix matrix; @@ -816,6 +642,7 @@ TEST(PIMathMatrix_Test, toUpperTriangular) TEST(PIMathMatrix_Test, invert) { + PIMathMatrix origMatr; double d1, d2; bool b; PIMathMatrix matrix1; @@ -857,6 +684,7 @@ TEST(PIMathMatrix_Test, invert) TEST(PIMathMatrix_Test, inverted) { + PIMathMatrix origMatr; double d1, d2; bool b; PIMathMatrix matrix1; @@ -898,6 +726,7 @@ TEST(PIMathMatrix_Test, inverted) TEST(PIMathMatrix_Test, transposed) { + PIMathMatrix origMatr; double d1, d2; bool b; PIMathMatrix matrix1; @@ -932,5 +761,3 @@ TEST(PIMathMatrix_Test, transposed) } ASSERT_TRUE(b); } - - diff --git a/lib/main/math/test/testpimathmatrixt.cpp b/lib/main/math/test/testpimathmatrixt.cpp index 1d343237..38976bf8 100644 --- a/lib/main/math/test/testpimathmatrixt.cpp +++ b/lib/main/math/test/testpimathmatrixt.cpp @@ -3,11 +3,42 @@ const uint rows = 3; const uint cols = 3; -PIMathMatrixT matr; -PIMathVectorT vect; + +bool cmpMatrixWithValue(PIMathMatrixT matrix, double val) +{ + int i = 0; + int j = 0; + int k = 0; + bool b; + while(i < 9) + { + if(k < 3) + { + if(matrix.at(j,k) == val) + { + b = true; + } + else + { + b = false; + break; + } + k++; + if(k == 3) + { + j++; + k = 0; + } + } + i++; + } + return b; +} + TEST(PIMathMatrixT_Test, identity) { + PIMathMatrixT matr; PIMathMatrixT matrix; double d; double i = 1.0; @@ -38,6 +69,7 @@ TEST(PIMathMatrixT_Test, at) { uint i; bool b; + PIMathMatrixT matr; PIMathMatrixT matrix1; PIMathMatrixT matrix2; matrix1 = matr.identity(); @@ -60,6 +92,8 @@ TEST(PIMathMatrixT_Test, filled) { PIMathMatrixT matrix1; PIMathMatrixT matrix2; + PIMathMatrixT matr; + PIMathVectorT vect; double g = 1.0; matrix2 = matr.fill(g); uint j = 0, i = 0; @@ -75,16 +109,20 @@ TEST(PIMathMatrixT_Test, filled) 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 i; uint g = 2; bool b; @@ -115,6 +153,8 @@ TEST(PIMathMatrixT_Test, col) TEST(PIMathMatrixT_Test, row) { + PIMathMatrixT matr; + PIMathVectorT vect; uint i; uint g = 2; bool b; @@ -145,6 +185,8 @@ TEST(PIMathMatrixT_Test, row) TEST(PIMathMatrixT_Test, setCol) { + PIMathMatrixT matr; + PIMathVectorT vect; vect.at(0) = 1.0; vect.at(1) = 3.0; vect.at(2) = 5.0; @@ -169,6 +211,8 @@ TEST(PIMathMatrixT_Test, setCol) TEST(PIMathMatrixT_Test, setRow) { + PIMathMatrixT matr; + PIMathVectorT vect; vect.at(0) = 1.0; vect.at(1) = 3.0; vect.at(2) = 5.0; @@ -193,6 +237,7 @@ TEST(PIMathMatrixT_Test, setRow) TEST(PIMathMatrixT_Test, swapCols) { + PIMathMatrixT matr; PIMathVectorT before_Vect1; PIMathVectorT before_Vect2; PIMathVectorT after_Vect1; @@ -226,6 +271,7 @@ TEST(PIMathMatrixT_Test, swapCols) TEST(PIMathMatrixT_Test, swapRows) { + PIMathMatrixT matr; PIMathVectorT before_Vect1; PIMathVectorT before_Vect2; PIMathVectorT after_Vect1; @@ -259,6 +305,7 @@ TEST(PIMathMatrixT_Test, swapRows) TEST(PIMathMatrixT_Test, fill) { + PIMathMatrixT matr; PIMathMatrixT matrix1; double g = 1.0; matr.fill(g); @@ -275,6 +322,7 @@ TEST(PIMathMatrixT_Test, fill) TEST(PIMathMatrixT_Test, isSquare) { + PIMathMatrixT matr; PIMathMatrixT matrix1; const uint new_Cols = 4; PIMathMatrixT matrix2; @@ -292,6 +340,7 @@ TEST(PIMathMatrixT_Test, isSquare) TEST(PIMathMatrixT_Test, isIdentity) { + PIMathMatrixT matr; PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; @@ -312,6 +361,7 @@ TEST(PIMathMatrixT_Test, isNull) { PIMathMatrixT matrix1; PIMathMatrixT matrix2; + PIMathMatrixT matr; bool b; matrix2 = matr.filled(3.67); if((matrix1.isNull() == true) && (matrix2.isNull() == false)) @@ -329,34 +379,11 @@ TEST(PIMathMatrixT_Test, operator_Assignment) { PIMathMatrixT matrix1; PIMathMatrixT matrix2; - int i = 0; - int j = 0; - int k = 0; + PIMathMatrixT matr; bool b; matrix2.fill(6.72); matrix1 = matrix2; - while(i < 9) - { - if(k < 3) - { - if(matrix1.at(j,k) == 6.72) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 6.72); ASSERT_TRUE(b); } @@ -364,6 +391,7 @@ TEST(PIMathMatrixT_Test, operator_Equal) { PIMathMatrixT matrix1; PIMathMatrixT matrix2; + PIMathMatrixT matr; bool b; matr.at(0,0) = 3; matr.at(0,1) = 6; @@ -393,6 +421,7 @@ TEST(PIMathMatrixT_Test, operator_Not_Equal) PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; + PIMathMatrixT matr; matr.at(0,0) = 3; matr.at(0,1) = 6; matr.at(0,2) = 8; @@ -421,34 +450,10 @@ TEST(PIMathMatrixT_Test, operator_Addition_Aassignment) PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix2.fill(6.72); matrix1.fill(1.0); matrix1 += matrix2; - while(i < 9) - { - if(k < 3) - { - if(matrix1.at(j,k) == 7.72) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 7.72); ASSERT_TRUE(b); } @@ -457,34 +462,10 @@ TEST(PIMathMatrixT_Test, operator_Subtraction_Assignment) PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix2.fill(6.72); matrix1.fill(1.0); matrix1 -= matrix2; - while(i < 9) - { - if(k < 3) - { - if(matrix1.at(j,k) == -5.72) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, -5.72); ASSERT_TRUE(b); } @@ -492,33 +473,9 @@ TEST(PIMathMatrixT_Test, operator_Multiplication_Assignment) { PIMathMatrixT matrix1; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1.fill(6.72); matrix1 *= 2.0; - while(i < 9) - { - if(k < 3) - { - if(matrix1.at(j,k) == 13.44) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 13.44); ASSERT_TRUE(b); } @@ -526,33 +483,9 @@ TEST(PIMathMatrixT_Test, operator_Division_Assignment) { PIMathMatrixT matrix1; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1.fill(6.72); matrix1 /= 2.0; - while(i < 9) - { - if(k < 3) - { - if(matrix1.at(j,k) == 3.36) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1, 3.36); ASSERT_TRUE(b); } @@ -561,33 +494,9 @@ TEST(PIMathMatrixT_Test, operator_Addition) PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1.fill(6.72); matrix2.fill(8.28); - while(i < 9) - { - if(k < 3) - { - if((matrix1 + matrix2).at(j,k) == 15.0) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix1 + matrix2, 15.0); ASSERT_TRUE(b); } @@ -596,33 +505,9 @@ TEST(PIMathMatrixT_Test, operator_Subtraction) PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; - matrix1.fill(6.72); - matrix2.fill(8.28); - while(i < 9) - { - if(k < 3) - { - if(round((matrix1 - matrix2).at(j,k) * 10000) / 10000 == -1.56) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + matrix1.fill(6.0); + matrix2.fill(5.0); + b = cmpMatrixWithValue(matrix1 - matrix2, 1.0); ASSERT_TRUE(b); } @@ -631,33 +516,9 @@ TEST(PIMathMatrixT_Test, operator_Multiplication) PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1.fill(6.72); matrix2 = matrix1*4.0; - while(i < 9) - { - if(k < 3) - { - if(matrix2.at(j,k) == 26.88) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix2, 26.88); ASSERT_TRUE(b); } TEST(PIMathMatrixT_Test, operator_Division) @@ -665,33 +526,9 @@ TEST(PIMathMatrixT_Test, operator_Division) PIMathMatrixT matrix1; PIMathMatrixT matrix2; bool b; - int i = 0; - int j = 0; - int k = 0; matrix1.fill(6.72); matrix2 = matrix1/4.0; - while(i < 9) - { - if(k < 3) - { - if(matrix2.at(j,k) == 1.68) - { - b = true; - } - else - { - b = false; - break; - } - k++; - if(k == 3) - { - j++; - k = 0; - } - } - i++; - } + b = cmpMatrixWithValue(matrix2, 1.68); ASSERT_TRUE(b); } @@ -700,6 +537,7 @@ TEST(PIMathMatrixT_Test, determinant) { double d; double i = 59.0; + PIMathMatrixT matr; matr.at(0,0) = 3; matr.at(0,1) = 6; matr.at(0,2) = 8; @@ -718,6 +556,7 @@ TEST(PIMathMatrixT_Test, invert) PIMathMatrixT matrix1; PIMathMatrixT matrix2; PIMathMatrixT matrix3; + PIMathMatrixT matr; double d1, d2; bool b; matrix1 = matr.identity(); @@ -752,6 +591,7 @@ TEST(PIMathMatrixT_Test, inverted) PIMathMatrixT matrix1; PIMathMatrixT matrix2; PIMathMatrixT matrix3; + PIMathMatrixT matr; double d1, d2; bool b; matrix1 = matr.identity(); @@ -784,6 +624,7 @@ TEST(PIMathMatrixT_Test, toUpperTriangular) PIMathMatrixT matrix; double d1, d2 = 1; uint i; + PIMathMatrixT matr; matr.at(0,0) = 3; matr.at(0,1) = 6; matr.at(0,2) = 8; @@ -806,6 +647,7 @@ TEST(PIMathMatrixT_Test, transposed) { PIMathMatrixT matrix1; PIMathMatrixT matrix2; + PIMathMatrixT matr; double d1, d2; bool b; matr.at(0,0) = 3;