From b3cc18c6de26dfa884ccd3f1cb21bd8696486136 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Wed, 23 Feb 2022 15:04:54 +0100 Subject: [PATCH] Matrix: convert square test to gtest --- src/lib/matrix/test/CMakeLists.txt | 2 +- ...{squareMatrix.cpp => MatrixSquareTest.cpp} | 84 +++++++++++++------ 2 files changed, 58 insertions(+), 28 deletions(-) rename src/lib/matrix/test/{squareMatrix.cpp => MatrixSquareTest.cpp} (52%) diff --git a/src/lib/matrix/test/CMakeLists.txt b/src/lib/matrix/test/CMakeLists.txt index 0900470907..43dee79eca 100644 --- a/src/lib/matrix/test/CMakeLists.txt +++ b/src/lib/matrix/test/CMakeLists.txt @@ -11,7 +11,6 @@ set(tests vector vector2 vector3 - squareMatrix upperRightTriangle ) @@ -44,4 +43,5 @@ px4_add_unit_gtest(SRC MatrixScalarMultiplicationTest.cpp) px4_add_unit_gtest(SRC MatrixSetIdentityTest.cpp) px4_add_unit_gtest(SRC MatrixSliceTest.cpp) px4_add_unit_gtest(SRC MatrixSparseVectorTest.cpp) +px4_add_unit_gtest(SRC MatrixSquareTest.cpp) px4_add_unit_gtest(SRC MatrixUnwrapTest.cpp) diff --git a/src/lib/matrix/test/squareMatrix.cpp b/src/lib/matrix/test/MatrixSquareTest.cpp similarity index 52% rename from src/lib/matrix/test/squareMatrix.cpp rename to src/lib/matrix/test/MatrixSquareTest.cpp index e42f8f4b53..b52e9dd665 100644 --- a/src/lib/matrix/test/squareMatrix.cpp +++ b/src/lib/matrix/test/MatrixSquareTest.cpp @@ -1,10 +1,42 @@ -#include "test_macros.hpp" +/**************************************************************************** + * + * Copyright (C) 2022 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ +#include #include using namespace matrix; -int main() +TEST(MatrixSquareTest, Square) { float data[9] = {1, 2, 3, 4, 5, 6, @@ -13,8 +45,8 @@ int main() SquareMatrix A(data); Vector3 diag_check(1, 5, 10); - TEST(isEqual(A.diag(), diag_check)); - TEST(A.trace() - 16 < FLT_EPSILON); + EXPECT_EQ(A.diag(), diag_check); + EXPECT_FLOAT_EQ(A.trace(), 16); float data_check[9] = { 1.01158503f, 0.02190432f, 0.03238144f, @@ -25,7 +57,7 @@ int main() float dt = 0.01f; SquareMatrix eA = expm(SquareMatrix(A * dt), 5); SquareMatrix eA_check(data_check); - TEST((eA - eA_check).abs().max() < 1e-3f); + EXPECT_TRUE(isEqual(eA, eA_check, 1e-3f)); SquareMatrix A_bottomright = A.slice<2, 2>(1, 1); SquareMatrix A_bottomright2; @@ -35,8 +67,8 @@ int main() 8, 10 }; SquareMatrix bottomright_check(data_bottomright); - TEST(isEqual(A_bottomright, bottomright_check)); - TEST(isEqual(A_bottomright2, bottomright_check)); + EXPECT_EQ(A_bottomright, bottomright_check); + EXPECT_EQ(A_bottomright2, bottomright_check); // test diagonal functions float data_4x4[16] = {1, 2, 3, 4, @@ -53,7 +85,7 @@ int main() 13, 0, 15, 16 }; SquareMatrix B_check(data_B_check); - TEST(isEqual(B, B_check)) + EXPECT_EQ(B, B_check); SquareMatrix C(data_4x4); C.uncorrelateCovariance<2>(1); @@ -63,7 +95,7 @@ int main() 13, 0, 0, 16 }; SquareMatrix C_check(data_C_check); - TEST(isEqual(C, C_check)) + EXPECT_EQ(C, C_check); SquareMatrix D(data_4x4); D.uncorrelateCovarianceSetVariance<2>(0, Vector2f{20, 21}); @@ -73,7 +105,7 @@ int main() 0, 0, 15, 16 }; SquareMatrix D_check(data_D_check); - TEST(isEqual(D, D_check)) + EXPECT_EQ(D, D_check); SquareMatrix E(data_4x4); E.uncorrelateCovarianceSetVariance<3>(1, 33); @@ -83,7 +115,7 @@ int main() 0, 0, 0, 33 }; SquareMatrix E_check(data_E_check); - TEST(isEqual(E, E_check)) + EXPECT_EQ(E, E_check); // test symmetric functions SquareMatrix F(data_4x4); @@ -94,9 +126,9 @@ int main() 13, 14, 15, 16 }; SquareMatrix F_check(data_F_check); - TEST(isEqual(F, F_check)) - TEST(F.isBlockSymmetric<2>(1)); - TEST(!F.isRowColSymmetric<2>(1)); + EXPECT_EQ(F, F_check); + EXPECT_TRUE(F.isBlockSymmetric<2>(1)); + EXPECT_FALSE(F.isRowColSymmetric<2>(1)); SquareMatrix G(data_4x4); G.makeRowColSymmetric<2>(1); @@ -106,9 +138,9 @@ int main() 13, 11, 13.5, 16 }; SquareMatrix G_check(data_G_check); - TEST(isEqual(G, G_check)); - TEST(G.isBlockSymmetric<2>(1)); - TEST(G.isRowColSymmetric<2>(1)); + EXPECT_EQ(G, G_check); + EXPECT_TRUE(G.isBlockSymmetric<2>(1)); + EXPECT_TRUE(G.isRowColSymmetric<2>(1)); SquareMatrix H(data_4x4); H.makeBlockSymmetric<1>(1); @@ -118,9 +150,9 @@ int main() 13, 14, 15, 16 }; SquareMatrix H_check(data_H_check); - TEST(isEqual(H, H_check)) - TEST(H.isBlockSymmetric<1>(1)); - TEST(!H.isRowColSymmetric<1>(1)); + EXPECT_EQ(H, H_check); + EXPECT_TRUE(H.isBlockSymmetric<1>(1)); + EXPECT_FALSE(H.isRowColSymmetric<1>(1)); SquareMatrix J(data_4x4); J.makeRowColSymmetric<1>(1); @@ -130,10 +162,10 @@ int main() 13, 11, 15, 16 }; SquareMatrix J_check(data_J_check); - TEST(isEqual(J, J_check)); - TEST(J.isBlockSymmetric<1>(1)); - TEST(J.isRowColSymmetric<1>(1)); - TEST(!J.isBlockSymmetric<3>(1)); + EXPECT_EQ(J, J_check); + EXPECT_TRUE(J.isBlockSymmetric<1>(1)); + EXPECT_TRUE(J.isRowColSymmetric<1>(1)); + EXPECT_FALSE(J.isBlockSymmetric<3>(1)); float data_K[16] = {1, 2, 3, 4, 2, 3, 4, 11, @@ -141,7 +173,5 @@ int main() 4, 11, 15, 16 }; SquareMatrix K(data_K); - TEST(!K.isRowColSymmetric<1>(2)); - return 0; + EXPECT_FALSE(K.isRowColSymmetric<1>(2)); } -