From b07242226e30f71e807e0011996ae8a6aa011780 Mon Sep 17 00:00:00 2001 From: maakshishov Date: Thu, 25 Feb 2021 15:41:20 +0300 Subject: [PATCH 1/5] Tests1-10 --- CMakeLists.txt | 3 +- tests/math/testpimathmatrix.cpp | 509 +++++-------------------------- tests/math/testpimathmatrixt.cpp | 489 ----------------------------- tests/math/testpivector2d.cpp | 84 ----- 4 files changed, 72 insertions(+), 1013 deletions(-) delete mode 100644 tests/math/testpimathmatrixt.cpp delete mode 100644 tests/math/testpivector2d.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0285a7b7..32fdf8c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,8 @@ set(PIP_DLL_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE STRING "") option(ICU "ICU support for convert codepages" ${_ICU_DEFAULT}) option(STD_IOSTREAM "Building with std iostream operators support" OFF) option(INTROSPECTION "Build with introspection" OFF) -option(TESTS "Build tests and perform their before install step" OFF) +option( + "Build tests and perform their before install step" OFF) option(COVERAGE "Build project with coverage info" OFF) set(PIP_UTILS 1) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) diff --git a/tests/math/testpimathmatrix.cpp b/tests/math/testpimathmatrix.cpp index 94f3076b..bf75203c 100644 --- a/tests/math/testpimathmatrix.cpp +++ b/tests/math/testpimathmatrix.cpp @@ -1,7 +1,8 @@ #include "gtest/gtest.h" #include "pimathmatrix.h" -bool cmpSquareMatrixWithValue(PIMathMatrix matrix, double val, int num) { +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++) { @@ -13,7 +14,24 @@ bool cmpSquareMatrixWithValue(PIMathMatrix matrix, double val, int num) return b; } -TEST(PIMathMatrix_Test, identity) { +TEST(PIMathMatrix_Test, constructor1) { + 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)); +} + +TEST(PIMathMatrix_Test, constructor3) { + PIVector2D vector(2, 2, 5.0); + PIMathMatrix matrix(vector); + ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 5.0, 2)); +} + +TEST(PIMathMatrix_Test, identity1) { auto matrix = PIMathMatrix::identity(3, 3); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { @@ -29,7 +47,24 @@ TEST(PIMathMatrix_Test, identity) { } } } - ASSERT_TRUE(true); +} + +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); + } + } + } + } } TEST(PIMathMatrixT_Test, element) { @@ -104,440 +139,36 @@ TEST(PIMathMatrix_Test, setRow) { } 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)); -} - -TEST(PIMathMatrix_Test, swapRows) { - 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(0, i); - a2[i] = matrix1.element(1, i); - a3[i] = matrix1.element(2, i); - } - matrix1.swapRows(i1, i2); - for(int i = 0; i < 3; i++) { - b1[i] = matrix1.element(0, i); - b2[i] = matrix1.element(1, i); - b3[i] = matrix1.element(2, i); - } - ASSERT_TRUE((memcmp(a1, b2, sizeof(b1)) == 0) && (memcmp(a2, b1, sizeof(b1)) == 0) && (memcmp(a3, b3, sizeof(b1)) == 0)); -} -TEST(PIMathMatrix_Test, fill) { - PIMathMatrix matrix(3, 3, 5.0); - matrix.fill(7.0); - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix, 7.0, 3)); -} - -TEST(PIMathMatrix_Test, isSquareTrue) { - PIMathMatrix matrix(3, 3, 1.0); - ASSERT_TRUE(matrix.isSquare()); -} - -TEST(PIMathMatrix_Test, isSquareFalse) { - PIMathMatrix matrix(2, 4, 1.0); - ASSERT_FALSE(matrix.isSquare()); -} - -TEST(PIMathMatrix_Test, isIdentityTrue) { - auto matrix = PIMathMatrix::identity(3, 3); - ASSERT_TRUE(matrix.isIdentity()); -} - -TEST(PIMathMatrix_Test, isIdentityFalse) { - PIMathMatrix matrix(3, 3, 5.0); - ASSERT_FALSE(matrix.isIdentity()); -} - - -TEST(PIMathMatrix_Test, isNullTrue) { - PIMathMatrix matrix(3, 3, 0.0); - ASSERT_TRUE(matrix.isNull()); -} - -TEST(PIMathMatrix_Test, isNullFalse) { - PIMathMatrix matrix(3, 3, 5.0); - ASSERT_FALSE(matrix.isNull()); -} - -TEST(PIMathMatrix_Test, isValidTrue) { - PIMathMatrix matrix(3, 3, 1.62); - ASSERT_TRUE(matrix.isValid()); -} - -TEST(PIMathMatrix_Test, isValidFalse) { - PIMathMatrix matrix; - ASSERT_FALSE(matrix.isValid()); -} - -TEST(PIMathMatrix_Test, operator_Assignment) { - PIMathMatrix matrix1(3, 3, 5.72); - PIMathMatrix matrix2(3, 3, 7.12); - matrix1 = matrix2; - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 7.12, 3)); -} - -TEST(PIMathMatrix_Test, operator_EqualTrue) { - PIMathMatrix matrix1(2, 2, 2.0); - PIMathMatrix matrix2(2, 2, 2.0); - 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(PIMathMatrix_Test, operator_EqualFalse) { - PIMathMatrix matrix1(2, 2, 2.0); - PIMathMatrix matrix2(2, 2, 2.0); - 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(PIMathMatrix_Test, operator_Not_EqualTrue) { - PIMathMatrix matrix1(2, 2, 2.0); - PIMathMatrix matrix2(2, 2, 2.0); - 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(PIMathMatrix_Test, operator_Not_EqualFalse) { - PIMathMatrix matrix1(2, 2, 2.0); - PIMathMatrix matrix2(2, 2, 2.0); - 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(PIMathMatrix_Test, operator_Addition_Aassignment) { - PIMathMatrix matrix1(3, 3, 6.72); - PIMathMatrix matrix2(3, 3, 1.0); - matrix1 += matrix2; - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 7.72, 3)); -} - -TEST(PIMathMatrix_Test, operator_Subtraction_Assignment) { - PIMathMatrix matrix1(3, 3, 1.0); - PIMathMatrix matrix2(3, 3, 6.72); - matrix1 -= matrix2; - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, -5.72, 3)); -} - -TEST(PIMathMatrix_Test, operator_Multiplication_Assignment) { - PIMathMatrix matrix1(3, 3, 6.72); - matrix1 *= 2.0; - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 13.44, 3)); -} - -TEST(PIMathMatrix_Test, operator_Division_Assignment) { - PIMathMatrix matrix1(3, 3, 6.72); - matrix1 /= 2.0; - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 3.36, 3)); -} - -TEST(PIMathMatrix_Test, operator_Addition) { - PIMathMatrix matrix1(3, 3, 6.72); - PIMathMatrix matrix2(3, 3, 8.28); - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 + matrix2, 15.0, 3)); -} - -TEST(PIMathMatrix_Test, operator_Subtraction) { - PIMathMatrix matrix1(3, 3, 6.0); - PIMathMatrix matrix2(3, 3, 5.0); - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 - matrix2, 1.0, 3)); -} - -TEST(PIMathMatrix_Test, operator_Multiplication) { - PIMathMatrix matrix1(3, 3, 6.72); - PIMathMatrix matrix2(3, 3, 5.0); - matrix2 = matrix1*4.0; - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix2, 26.88, 3)); -} -TEST(PIMathMatrix_Test, operator_Division) { - PIMathMatrix matrix1(3, 3, 6.72); - PIMathMatrix matrix2(3, 3, 5.0); - matrix2 = matrix1/4.0; - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix2, 1.68, 3)); -} - -TEST(PIMathMatrix_Test, determinantIfSquare) { - double d; - double i = 59.0; - PIMathMatrix matrix(3, 3, 0.0); - PIMathVector vector; - vector.resize(3, 3.0); - vector[0] = 3.0; - vector[1] = 6.0; - vector[2] = 8.0; - matrix.setCol(0, vector); - vector[0] = 2.0; - vector[1] = 1.0; - vector[2] = 4.0; - matrix.setCol(1, vector); - vector[0] = 6.0; - vector[1] = 2.0; - vector[2] = 5.0; - matrix.setCol(2, vector); - d = matrix.determinant(); - ASSERT_DOUBLE_EQ(d, i); -} - -TEST(PIMathMatrix_Test, trace) { - PIMathMatrix matrix(3, 3, 0.0); - double t; - double i = 9.0; - PIMathVector vector; - vector.resize(3, 3.0); - vector[0] = 3.0; - vector[1] = 6.0; - vector[2] = 8.0; - matrix.setCol(0, vector); - vector[0] = 2.0; - vector[1] = 1.0; - vector[2] = 4.0; - matrix.setCol(1, vector); - vector[0] = 6.0; - vector[1] = 2.0; - vector[2] = 5.0; - matrix.setCol(2, vector); - t = matrix.trace(); - ASSERT_DOUBLE_EQ(t, i); -} - -TEST(PIMathMatrix_Test, toUpperTriangular) { - PIMathMatrix matrix(3, 3, 0.0); - double d1, d2 = 1; - int i; - PIMathVector vector; - vector.resize(3, 3.0); - vector[0] = 3.0; - vector[1] = 6.0; - vector[2] = 8.0; - matrix.setCol(0, vector); - vector[0] = 2.0; - vector[1] = 1.0; - vector[2] = 4.0; - matrix.setCol(1, vector); - vector[0] = 6.0; - vector[1] = 2.0; - vector[2] = 5.0; - matrix.setCol(2, vector); - d1 = matrix.determinant(); - matrix.toUpperTriangular(); - for(i = 0; i < 3; i++) - { - d2 = d2 * matrix.element(i, i); - } - ASSERT_DOUBLE_EQ(d1, d2); -} - -TEST(PIMathMatrix_Test, invert) { - double d1, d2; - PIMathMatrix matrix1(3, 3, 0.0); - PIMathMatrix matrix2(3, 3, 0.0); - PIMathMatrix matrix3(3, 3, 0.0); - PIMathMatrix matrix4(3, 3, 0.0); - PIMathVector vector; - vector.resize(3, 3.0); - vector[0] = 3.0; - vector[1] = 6.0; - vector[2] = 8.0; - 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); - d1 = matrix1.determinant(); - matrix2 = matrix1; - matrix2.invert(); - d2 = matrix2.determinant(); - matrix4.invert(); - ASSERT_TRUE((matrix3 == matrix4) && (round((1/d1)*10000)/10000 == round(d2*10000)/10000)); -} - -TEST(PIMathMatrix_Test, inverted) { - double d1, d2; - PIMathMatrix matrix1(3, 3, 0.0); - PIMathMatrix matrix2(3, 3, 0.0); - PIMathMatrix matrix3(3, 3, 0.0); - PIMathMatrix matrix4(3, 3, 0.0); - PIMathVector vector; - vector.resize(3, 3.0); - vector[0] = 3.0; - vector[1] = 6.0; - vector[2] = 8.0; - 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); - d1 = matrix1.determinant(); - matrix2 = matrix1; - matrix1 = matrix2.invert(); - d2 = matrix1.determinant(); - matrix3 = matrix4.invert(); - ASSERT_TRUE((matrix3 == matrix4) && (round((1/d1)*10000)/10000 == round(d2*10000)/10000)); -} - -TEST(PIMathMatrix_Test, transposed) { - PIMathMatrix origMatr; - double d1, d2; - PIMathMatrix matrix1; - PIMathMatrix matrix2; - PIMathMatrix matrix3; - PIMathVector vector; - 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); - d1 = matrix1.determinant(); - matrix2 = matrix1.transposed(); - d2 = matrix2.determinant(); - matrix3 = matrix2.transposed(); - ASSERT_TRUE((d1 == d2) && (matrix1 == matrix3)); -} - -TEST(PIMathMatrix_Test, matrixMultiplication) { - PIMathMatrix matrix1(2, 2, 1.5); - PIMathMatrix matrix2(2, 2, 2.5); - ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 * matrix2, 7.5, 2)); -} - -TEST(PIMathMatrix_Test, matrixAndVectorMultiplication) { - PIMathMatrix matrix1(2, 2, 1.5); - PIMathVector vector; - vector.resize(2, 2.5); - for(uint i = 0; i < 2; i++) { - if((matrix1 * vector)[i] != 7.5) { - ASSERT_TRUE(false); - } - } - ASSERT_TRUE(true); -} - -TEST(PIMathMatrix_Test, vectorAndMatrixMultiplication) { - PIMathMatrix matrix1(2, 2, 1.5); - PIMathVector vector; - vector.resize(2, 2.5); - for(uint i = 0; i < 2; i++) { - if((vector * matrix1)[i] != 7.5) { - ASSERT_TRUE(false); - } - } - ASSERT_TRUE(true); -} - -TEST(PIMathMatrix_Test, valAndMatrixMultiplication) { - PIMathMatrix matrix1(3, 3, 1.5); - ASSERT_TRUE(cmpSquareMatrixWithValue(25.0*matrix1, 37.5, 3)); -} - -TEST(PIMathMatrix_Test, hermitian) { - complex val; - complex res; - val.imag(1.0); - val.real(1.0); - PIMathMatrix> matrix(3, 3, val); - res.imag(-1.0); - res.real(1.0); - auto matr = hermitian(matrix); - for(uint i = 0; i < 3; i++) { - for(uint j = 0; j < 3; j++) { - if(matr.element(i, j) != res) { - ASSERT_TRUE(false); - } - } - } - ASSERT_TRUE(true); + 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 deleted file mode 100644 index 5f3a20bf..00000000 --- a/tests/math/testpimathmatrixt.cpp +++ /dev/null @@ -1,489 +0,0 @@ -#include "gtest/gtest.h" -#include "pimathmatrix.h" - -const uint rows = 3; -const uint cols = 3; - -bool cmpSquareMatrixWithValue(PIMathMatrixT matrix, double val, int num) { - bool b = true; - for(int i = 0; i < num; i++) { - for(int j = 0; j < num; j++) { - if(matrix.at(i, j) != val) { - b = false; - } - } - } - return b; -} - -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 deleted file mode 100644 index ae66b01c..00000000 --- a/tests/math/testpivector2d.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#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); -} From aa76a15f40c29edff1eb33a9ed53e95fed45179c Mon Sep 17 00:00:00 2001 From: peri4 Date: Mon, 4 Oct 2021 21:50:49 +0300 Subject: [PATCH 2/5] version 2.32.0 PIObject::Connection struct --- CMakeLists.txt | 4 +- libs/main/core/piobject.cpp | 141 +++++++++++++++---------------- libs/main/core/piobject.h | 108 ++++++++++++++--------- libs/main/core/piobject_macros.h | 10 ++- libs/main/thread/pithread.cpp | 2 +- 5 files changed, 152 insertions(+), 113 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index df3aa574..4817cbf6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.0) cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(pip) set(pip_MAJOR 2) -set(pip_MINOR 31) -set(pip_REVISION 1) +set(pip_MINOR 32) +set(pip_REVISION 0) set(pip_SUFFIX ) set(pip_COMPANY SHS) set(pip_DOMAIN org.SHS) diff --git a/libs/main/core/piobject.cpp b/libs/main/core/piobject.cpp index a5b6b6c4..b9aa1c26 100644 --- a/libs/main/core/piobject.cpp +++ b/libs/main/core/piobject.cpp @@ -143,50 +143,6 @@ bool PIObject::executeQueued(PIObject * performer, const PIString & method, cons } -void PIObject::piConnect(const PIString & src, const PIString & sig, void * dest, void * ev_h) { - PIObject * o = findByName(src); - if (o == 0) { - piCout << "[PIObject] Can`t find object with name \"" << src << "\"!"; - return; - } - PIMutexLocker _ml(o->mutex_connect); - PIMutexLocker _mld(((PIObject*)dest)->mutex_connect, ((PIObject*)dest) != o); - o->connections << __Connection(ev_h, 0, sig, (PIObject*)dest, dest); - ((PIObject*)dest)->connectors << o; -} - - -void PIObject::piConnect(PIObject * src, const PIString & sig, const PIString & dest, void * ev_h) { - PIObject * o = findByName(dest); - if (o == 0) { - piCout << "[PIObject] Can`t find object with name \"" << dest << "\"!"; - return; - } - PIMutexLocker _ml(src->mutex_connect); - PIMutexLocker _mld(o->mutex_connect, src != o); - src->connections << __Connection(ev_h, 0, sig, o, o); - ((PIObject*)o)->connectors << src; -} - - -void PIObject::piConnect(const PIString & src, const PIString & sig, const PIString & dest, void * ev_h) { - PIObject * s = findByName(src); - if (s == 0) { - piCout << "[PIObject] Can`t find object with name \"" << src << "\"!"; - return; - } - PIObject * d = findByName(dest); - if (d == 0) { - piCout << "[PIObject] Can`t find object with name \"" << dest << "\"!"; - return; - } - PIMutexLocker _ml(s->mutex_connect); - PIMutexLocker _mld(d->mutex_connect, s != d); - s->connections << __Connection(ev_h, 0, sig, d, d); - d->connectors << s; -} - - PIStringList PIObject::scopeList() const { PIMutexLocker ml(__meta_mutex()); return __meta_data()[classNameID()].scope_list; @@ -258,31 +214,33 @@ PIObject::__MetaFunc PIObject::methodEH(const void * addr) const { } -void PIObject::piConnect(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, void * ev_h, void * e_h, int args, const char * loc) { +PIObject::Connection PIObject::piConnect(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, void * ev_h, void * e_h, int args, const char * loc) { //piCout << "piConnect ..."; //piCout << "piConnect" << src << (void*)(dest) << sig; //piCout << "piConnect" << src->className() << "->" << ((PIObject*)dest)->className(); PIMutexLocker _ml(src->mutex_connect); PIMutexLocker _mld(dest_o->mutex_connect, src != dest_o); - src->connections << __Connection(ev_h, e_h, sig, dest_o, dest, args); + Connection conn(ev_h, e_h, sig, src, dest_o, dest, args); + src->connections << conn; //piCout << "piConnect" << ((PIObject*)dest) << sig << ((PIObject*)dest)->connectors.size_s() << "..."; //piCout << "addConnector" << dest_o << src; dest_o->connectors << src; //piCout << "piConnect" << ((PIObject*)dest) << sig << ((PIObject*)dest)->connectors.size_s(); //piCout << "piConnect ok"; + return conn; } -bool PIObject::piConnectU(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, const PIString & hname, const char * loc, PIObject * performer) { - if (src == 0 || dest_o == 0 || dest == 0) return false; +PIObject::Connection PIObject::piConnectU(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, const PIString & hname, const char * loc, PIObject * performer) { + if (src == 0 || dest_o == 0 || dest == 0) return Connection(); if (!src->isPIObject()) { piCout << "[piConnectU] \"" << sig << "\" -> \"" << hname << "\" error: source object is not PIObject! (" << loc << ")"; - return false; + return Connection(); } if (!dest_o->isPIObject()) { piCout << "[piConnectU] \"" << sig << "\" -> \"" << hname << "\" error: destination object is not PIObject! (" << loc << ")"; - return false; + return Connection(); } PIMutexLocker ml(__meta_mutex()); PIMutexLocker mls(src->mutex_connect); @@ -290,11 +248,11 @@ bool PIObject::piConnectU(PIObject * src, const PIString & sig, PIObject * dest_ PIVector<__MetaFunc> m_src = src->findEH(sig), m_dest = dest_o->findEH(hname); if (m_src.isEmpty()) { piCout << "[piConnectU] Error: can`t find event \"" << sig << "\" in class \"" << src->className() << "\"! (" << loc << ")"; - return false; + return Connection(); } if (m_dest.isEmpty()) { piCout << "[piConnectU] Error: can`t find handler \"" << hname << "\" in class \"" << dest_o->className() << "\"! (" << loc << ")"; - return false; + return Connection(); } void * addr_src(0), * addr_dest(0); int args(0); @@ -313,25 +271,26 @@ bool PIObject::piConnectU(PIObject * src, const PIString & sig, PIObject * dest_ if (addr_src == 0) { piCout << "[piConnectU] Error: can`t find suitable pair of event \"" << sig << "\" in class \"" << src->className() << "\" and handler \"" << hname << "\" in class \"" << dest_o->className() << "\"! (" << loc << ")"; - return false; + return Connection(); } - src->connections << PIObject::__Connection(addr_dest, addr_src, sig, dest_o, dest, args, performer); + Connection conn(addr_dest, addr_src, sig, src, dest_o, dest, args, performer); + src->connections << conn; if (que) performer->proc_event_queue = true; dest_o->connectors << src; //piCout << cc << cq << _ol.size();//"connect" << src << "->" << dest_o << ", dest.connectors.size() =" << dest_o->connectors.size(); - return true; + return conn; } -bool PIObject::piConnectLS(PIObject * src, const PIString & sig, std::function * f, const char * loc) { +PIObject::Connection PIObject::piConnectLS(PIObject * src, const PIString & sig, std::function * f, const char * loc) { if (src == 0) { delete f; - return false; + return Connection(); } if (!src->isPIObject()) { piCout << "[piConnectLS] \"" << sig << "\" -> [lambda] error: source object is not PIObject! (" << loc << ")"; delete f; - return false; + return Connection(); } PIMutexLocker ml(__meta_mutex()); PIMutexLocker mls(src->mutex_connect); @@ -340,19 +299,19 @@ bool PIObject::piConnectLS(PIObject * src, const PIString & sig, std::functionclassName() << "\"! (" << loc << ")"; delete f; - return false; + return Connection(); } if (m_src.size() != 1) { piCout << "[piConnectLS] Error: can`t connect overloaded event \"" << sig << "\" in class \"" << src->className() << "\"! (" << loc << ")"; delete f; - return false; + return Connection(); } - PIObject::__Connection conn(0, m_src[0].addr, sig); + PIObject::Connection conn(0, m_src[0].addr, sig, src); //piCout << "found"; conn.functor = f; src->connections << conn; //piCout << "finished"; - return true; + return conn; } @@ -360,7 +319,7 @@ void PIObject::piDisconnect(PIObject * src, const PIString & sig, PIObject * des PIMutexLocker _ml(src->mutex_connect); PIMutexLocker _mld(dest->mutex_connect, src != dest); for (int i = 0; i < src->connections.size_s(); ++i) { - __Connection & cc(src->connections[i]); + Connection & cc(src->connections[i]); if (cc.event == sig && cc.dest_o == dest && cc.slot == ev_h) { src->connections[i].destroy(); src->connections.remove(i); @@ -375,7 +334,7 @@ void PIObject::piDisconnect(PIObject * src, const PIString & sig, PIObject * des PIMutexLocker _ml(src->mutex_connect); PIMutexLocker _mld(dest->mutex_connect, src != dest); for (int i = 0; i < src->connections.size_s(); ++i) { - __Connection & cc(src->connections[i]); + Connection & cc(src->connections[i]); if (cc.event == sig && cc.dest_o == dest) { src->connections[i].destroy(); src->connections.remove(i); @@ -389,7 +348,7 @@ void PIObject::piDisconnect(PIObject * src, const PIString & sig, PIObject * des void PIObject::piDisconnect(PIObject * src, const PIString & sig) { PIMutexLocker _ml(src->mutex_connect); for (int i = 0; i < src->connections.size_s(); ++i) { - __Connection & cc(src->connections[i]); + Connection & cc(src->connections[i]); if (cc.event == sig) { PIObject * dest = cc.dest_o; src->connections[i].destroy(); @@ -417,7 +376,7 @@ void PIObject::piDisconnectAll() { #if !defined(ANDROID) && !defined(MAC_OS) && !defined(FREERTOS) PIMutexLocker _mld(o->mutex_connect, this != o); #endif - PIVector<__Connection> & oc(o->connections); + PIVector & oc(o->connections); for (int i = 0; i < oc.size_s(); ++i) { if (oc[i].functor) continue; //piCout << " check" << (void*)(oc[i].dest_o) << "==" << (void*)(src); @@ -429,7 +388,7 @@ void PIObject::piDisconnectAll() { } } // piCout << "disconnect connections =" << connections.size(); - piForeachC (PIObject::__Connection & c, connections) { + piForeachC (PIObject::Connection & c, connections) { if (c.functor) continue; if (!c.dest_o) continue; if (!c.dest_o->isPIObject()) continue; @@ -447,8 +406,8 @@ void PIObject::updateConnectors() { PIMutexLocker _ml(mutexObjects()); piForeach (PIObject * o, objects()) { if (o == this) continue; - PIVector<__Connection> & oc(o->connections); - piForeach (__Connection & c, oc) + PIVector & oc(o->connections); + piForeach (Connection & c, oc) if (c.dest == this) connectors << o; } @@ -609,7 +568,7 @@ void PIObject::dump(const PIString & line_prefix) const { PICout(PICoutManipulators::AddNewLine) << line_prefix << " connections {"; PICout(PICoutManipulators::AddNewLine) << line_prefix << " count: " << connections.size_s(); //printf("dump %d connections\n",connections.size()); - piForeachC (__Connection & c, connections) { + piForeachC (Connection & c, connections) { PIObject * dst = c.dest_o; __MetaFunc ef = methodEH(c.signal); PIString src(c.event); @@ -682,12 +641,52 @@ void PIObject::__MetaData::addScope(const PIString & s, uint shash) { } -void PIObject::__Connection::destroy() { + + +void PIObject::Connection::destroy() { if (functor) delete functor; functor = nullptr; } +PIObject::Connection::Connection() { + slot = signal = dest = nullptr; + src_o = dest_o = performer = nullptr; + functor = nullptr; + eventID = 0; + args_count = 0; +} + + +bool PIObject::Connection::disconnect() { + if (!isValid() || !src_o) return false; + bool ndm = dest_o && (src_o != dest_o), ret = false, found = false; + PIMutexLocker _ml(src_o->mutex_connect); + if (ndm) dest_o->mutex_connect.lock(); + for (int i = 0; i < src_o->connections.size_s(); ++i) { + Connection & cc(src_o->connections[i]); + if (cc.eventID == eventID) { + if (dest_o && (cc.dest_o == dest_o)) { + if (cc.slot == slot) + found = true; + } + if (functor && (cc.functor == functor)) + found = true; + } + if (found) { + src_o->connections[i].destroy(); + src_o->connections.remove(i); + ret = true; + break; + } + } + if (dest_o) + dest_o->updateConnectors(); + if (ndm) dest_o->mutex_connect.unlock(); + return ret; +} + + PRIVATE_DEFINITION_START(PIObject::Deleter) diff --git a/libs/main/core/piobject.h b/libs/main/core/piobject.h index cb19bcfa..72f54c48 100644 --- a/libs/main/core/piobject.h +++ b/libs/main/core/piobject.h @@ -50,6 +50,54 @@ public: virtual ~PIObject(); + //! Helper class for obtain info about if connection successful and disconnect single connection + class PIP_EXPORT Connection { + friend class PIObject; + Connection(void * sl, void * si, const PIString & e = PIString(), + PIObject * s_o = nullptr, PIObject * d_o = nullptr, + void * d = nullptr, int ac = 0, PIObject * p = nullptr) { + slot = sl; + signal = si; + event = e; + eventID = e.hash(); + src_o = s_o; + dest_o = d_o; + dest = d; + args_count = ac; + performer = p; + functor = 0; + } + void destroy(); + void * slot; + void * signal; + std::function * functor; + PIString event; + uint eventID; + PIObject * src_o, * dest_o; + PIObject * performer; + void * dest; + int args_count; + public: + + //! Contructs invalid %Connection + Connection(); + + //! Returns if %Connection is valid + bool isValid() const {return signal;} + + //! Returns source object + PIObject * sourceObject() const {return src_o;} + + //! Returns destination object or nullptr if this is lambda connection + PIObject * destinationObject() const {return dest_o;} + + //! Returns performer object or nullptr if this is non-queued connection + PIObject * performerObject() const {return performer;} + + //! Disconnect this %Connection, returns if operation successful + bool disconnect(); + }; + private: uint _signature_; @@ -141,21 +189,29 @@ public: PIString methodEHFromAddr(const void * addr) const; // / Direct connect - static void piConnect(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, void * ev_h, void * e_h, int args, const char * loc); - static bool piConnectU(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, const PIString & hname, const char * loc, PIObject * performer = 0); - static bool piConnectLS(PIObject * src, const PIString & sig, std::function * f, const char * loc); + static PIObject::Connection piConnect(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, void * ev_h, void * e_h, int args, const char * loc); + static PIObject::Connection piConnectU(PIObject * src, const PIString & sig, PIObject * dest_o, void * dest, const PIString & hname, const char * loc, PIObject * performer = 0); + static PIObject::Connection piConnectLS(PIObject * src, const PIString & sig, std::function * f, const char * loc); template static std::function * __newFunctor(void(*stat_handler)(void*,TYPES...), INPUT functor) { return (std::function*)(new std::function(functor)); } - // / Through names and mixed - static void piConnect(const PIString & src, const PIString & sig, void * dest, void * ev_h); - static void piConnect(PIObject * src, const PIString & sig, const PIString & dest, void * ev_h); - static void piConnect(const PIString & src, const PIString & sig, const PIString & dest, void * ev_h); - + //! Disconnect object from all connections with event name "sig", connected to destination object "dest" and handler "ev_h" + void piDisconnect(const PIString & sig, PIObject * dest, void * ev_h) {piDisconnect(this, sig, dest, ev_h);} + + //! Disconnect object from all connections with event name "sig", connected to destination object "dest" + void piDisconnect(const PIString & sig, PIObject * dest) {piDisconnect(this, sig, dest);} + + //! Disconnect object from all connections with event name "sig" + void piDisconnect(const PIString & sig) {piDisconnect(this, sig);} + + + //! Disconnect object "src" from all connections with event name "sig", connected to destination object "dest" and handler "ev_h" static void piDisconnect(PIObject * src, const PIString & sig, PIObject * dest, void * ev_h); + + //! Disconnect object "src" from all connections with event name "sig", connected to destination object "dest" static void piDisconnect(PIObject * src, const PIString & sig, PIObject * dest); //! Disconnect object "src" from all connections with event name "sig" @@ -164,7 +220,7 @@ public: // / Raise events static void raiseEvent(PIObject * sender, const uint eventID) { for (int j = 0; j < sender->connections.size_s(); ++j) { - __Connection i(sender->connections[j]); + Connection i(sender->connections[j]); if (i.eventID != eventID) continue; if (i.functor) { (*(i.functor))(); @@ -193,7 +249,7 @@ public: template static void raiseEvent(PIObject * sender, const uint eventID, const T0 & v0 = T0()) { for (int j = 0; j < sender->connections.size_s(); ++j) { - __Connection i(sender->connections[j]); + Connection i(sender->connections[j]); if (i.eventID != eventID) continue; if (i.functor) { (*((std::function*)i.functor))(v0); @@ -224,7 +280,7 @@ public: template static void raiseEvent(PIObject * sender, const uint eventID, const T0 & v0 = T0(), const T1 & v1 = T1()) { for (int j = 0; j < sender->connections.size_s(); ++j) { - __Connection i(sender->connections[j]); + Connection i(sender->connections[j]); if (i.eventID != eventID) continue; if (i.functor) { (*((std::function*)i.functor))(v0, v1); @@ -259,7 +315,7 @@ public: template static void raiseEvent(PIObject * sender, const uint eventID, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2()) { for (int j = 0; j < sender->connections.size_s(); ++j) { - __Connection i(sender->connections[j]); + Connection i(sender->connections[j]); if (i.eventID != eventID) continue; if (i.functor) { (*((std::function*)i.functor))(v0, v1, v2); @@ -296,7 +352,7 @@ public: template static void raiseEvent(PIObject * sender, const uint eventID, const T0 & v0 = T0(), const T1 & v1 = T1(), const T2 & v2 = T2(), const T3 & v3 = T3()) { for (int j = 0; j < sender->connections.size_s(); ++j) { - __Connection i(sender->connections[j]); + Connection i(sender->connections[j]); if (i.eventID != eventID) continue; if (i.functor) { (*((std::function*)i.functor))(v0, v1, v2, v3); @@ -425,30 +481,6 @@ protected: private: - struct __Connection { - __Connection(void * sl = 0, void * si = 0, const PIString & e = PIString(), PIObject * d_o = 0, void * d = 0, int ac = 0, PIObject * p = 0) { - slot = sl; - signal = si; - event = e; - eventID = e.hash(); - dest_o = d_o; - dest = d; - args_count = ac; - performer = p; - functor = 0; - } - void destroy(); - void * slot; - void * signal; - std::function * functor; - PIString event; - uint eventID; - PIObject * dest_o; - PIObject * performer; - void * dest; - int args_count; - }; - struct __QueuedEvent { __QueuedEvent(void * sl = 0, void * d = 0, PIObject * d_o = 0, PIObject * s = 0, const PIVector & v = PIVector()) { slot = sl; @@ -499,7 +531,7 @@ private: static void callAddrV(void * slot, void * obj, int args, const PIVector & vl); - PIVector<__Connection> connections; + PIVector connections; PIMap > properties_; PISet connectors; PIVector<__QueuedEvent> events_queue; diff --git a/libs/main/core/piobject_macros.h b/libs/main/core/piobject_macros.h index e115a427..cff78bca 100644 --- a/libs/main/core/piobject_macros.h +++ b/libs/main/core/piobject_macros.h @@ -102,28 +102,36 @@ /// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\". \"Event\" and \"handler\" must has equal argument lists. +/// Returns PIObject::Connection #define CONNECTU(src, event, dest, handler) /// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\". /// Event handler will be executed by \"performer\". \"Event\" and \"handler\" must has equal argument lists. +/// Returns PIObject::Connection #define CONNECTU_QUEUED(src, event, dest, handler, performer) /// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to lambda-expression \"functor\". \"Event\" and \"functor\" must has equal argument lists. +/// Returns PIObject::Connection #define CONNECTL(src, event, functor) /// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists +/// Returns PIObject::Connection #define CONNECT0(ret, src, event, dest, handler) /// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists +/// Returns PIObject::Connection #define CONNECT1(ret, type0, src, event, dest, handler) /// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists +/// Returns PIObject::Connection #define CONNECT2(ret, type0, type1, src, event, dest, handler) /// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists +/// Returns PIObject::Connection #define CONNECT3(ret, type0, type1, type2, src, event, dest, handler) -/// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists +/// \relatesalso PIObject @brief connect event \"event\" from object \"src\" to event handler \"handler\" with return type \"ret\" from object \"dest\" with check of event and handler exists. +/// Returns PIObject::Connection #define CONNECT4(ret, type0, type1, type2, type3, src, event, dest, handler) /// \relatesalso PIObject @brief CONNECT is synonym of CONNECT0 diff --git a/libs/main/thread/pithread.cpp b/libs/main/thread/pithread.cpp index 7011d9e9..d478b857 100644 --- a/libs/main/thread/pithread.cpp +++ b/libs/main/thread/pithread.cpp @@ -554,7 +554,7 @@ void PIThread::__thread_func_once__() { void PIThread::runOnce(PIObject * object, const char * handler, const PIString & name) { PIThread * t = new PIThread(); t->setName(name); - if (!PIObject::piConnectU(t, PIStringAscii("started"), object, object, PIStringAscii(handler), "PIThread::runOnce")) { + if (!PIObject::piConnectU(t, PIStringAscii("started"), object, object, PIStringAscii(handler), "PIThread::runOnce").isValid()) { delete t; return; } From c404688bbdde051e36693000f09e670cb9c48bfd Mon Sep 17 00:00:00 2001 From: peri4 Date: Mon, 4 Oct 2021 21:57:34 +0300 Subject: [PATCH 3/5] more safety for PIObject::Connection::disconnect() --- libs/main/core/piobject.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libs/main/core/piobject.cpp b/libs/main/core/piobject.cpp index b9aa1c26..1f2bfc64 100644 --- a/libs/main/core/piobject.cpp +++ b/libs/main/core/piobject.cpp @@ -660,7 +660,11 @@ PIObject::Connection::Connection() { bool PIObject::Connection::disconnect() { if (!isValid() || !src_o) return false; + if (!src_o->isPIObject()) return false; bool ndm = dest_o && (src_o != dest_o), ret = false, found = false; + if (dest_o) { + if (!dest_o->isPIObject()) ndm = false; + } PIMutexLocker _ml(src_o->mutex_connect); if (ndm) dest_o->mutex_connect.lock(); for (int i = 0; i < src_o->connections.size_s(); ++i) { @@ -680,8 +684,10 @@ bool PIObject::Connection::disconnect() { break; } } - if (dest_o) - dest_o->updateConnectors(); + if (dest_o) { + if (dest_o->isPIObject()) + dest_o->updateConnectors(); + } if (ndm) dest_o->mutex_connect.unlock(); return ret; } From 948a90fcd906ad700f62615d8c115373950062f3 Mon Sep 17 00:00:00 2001 From: peri4 Date: Mon, 4 Oct 2021 22:14:15 +0300 Subject: [PATCH 4/5] option revert --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32fdf8c7..0285a7b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,8 +29,7 @@ set(PIP_DLL_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE STRING "") option(ICU "ICU support for convert codepages" ${_ICU_DEFAULT}) option(STD_IOSTREAM "Building with std iostream operators support" OFF) option(INTROSPECTION "Build with introspection" OFF) -option( - "Build tests and perform their before install step" OFF) +option(TESTS "Build tests and perform their before install step" OFF) option(COVERAGE "Build project with coverage info" OFF) set(PIP_UTILS 1) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) From 07ec32c9695b3881590a359b0f9a13603a495748 Mon Sep 17 00:00:00 2001 From: peri4 Date: Tue, 5 Oct 2021 20:31:00 +0300 Subject: [PATCH 5/5] 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); +}