From 91493307b98311af98d17fb70d29dd7b8cbccfdb Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Wed, 23 Feb 2022 15:15:43 +0100 Subject: [PATCH] Matrix: convert vector test to gtest --- src/lib/matrix/test/CMakeLists.txt | 2 +- src/lib/matrix/test/MatrixVectorTest.cpp | 76 ++++++++++++++++++++++++ src/lib/matrix/test/vector.cpp | 47 --------------- 3 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 src/lib/matrix/test/MatrixVectorTest.cpp delete mode 100644 src/lib/matrix/test/vector.cpp diff --git a/src/lib/matrix/test/CMakeLists.txt b/src/lib/matrix/test/CMakeLists.txt index 5705b0e6f6..f0d9cec684 100644 --- a/src/lib/matrix/test/CMakeLists.txt +++ b/src/lib/matrix/test/CMakeLists.txt @@ -6,7 +6,6 @@ add_compile_options( ) set(tests - vector ) add_custom_target(test_build) @@ -40,6 +39,7 @@ 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 MatrixTransposeTest.cpp) +px4_add_unit_gtest(SRC MatrixVectorTest.cpp) px4_add_unit_gtest(SRC MatrixUnwrapTest.cpp) px4_add_unit_gtest(SRC MatrixUpperRightTriangleTest.cpp) px4_add_unit_gtest(SRC MatrixVector2Test.cpp) diff --git a/src/lib/matrix/test/MatrixVectorTest.cpp b/src/lib/matrix/test/MatrixVectorTest.cpp new file mode 100644 index 0000000000..8e8c2cc9eb --- /dev/null +++ b/src/lib/matrix/test/MatrixVectorTest.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** + * + * 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; + +TEST(MatrixVectorTest, Vector) +{ + // test data + float data1[] = {1, 2, 3, 4, 5}; + float data2[] = {6, 7, 8, 9, 10}; + Vector v1(data1); + Vector v2(data2); + + // copy constructor + Vector v3(v2); + EXPECT_EQ(v2, v3); + + // norm, dot product + EXPECT_FLOAT_EQ(v1.norm(), 7.416198487095663f); + EXPECT_FLOAT_EQ(v1.norm_squared(), v1.norm() * v1.norm()); + EXPECT_FLOAT_EQ(v1.norm(), v1.length()); + EXPECT_FLOAT_EQ(v1.dot(v2), 130.0f); + EXPECT_FLOAT_EQ(v1.dot(v2), v1 * v2); + + // unit, unit_zero, normalize + EXPECT_FLOAT_EQ(v2.unit().norm(), 1.f); + EXPECT_FLOAT_EQ(v2.unit_or_zero().norm(), 1.f); + EXPECT_FLOAT_EQ((Vector().unit_or_zero().norm()), 0.f); + v2.normalize(); + EXPECT_FLOAT_EQ(v2.norm(), 1.f); + + // sqrt + float data1_sq[] = {1, 4, 9, 16, 25}; + Vector v4(data1_sq); + EXPECT_EQ(v1, v4.sqrt()); + + // longerThan + Vector v5; + v5(0) = 3; + v5(1) = 4; + EXPECT_TRUE(v5.longerThan(4.99f)); + EXPECT_FALSE(v5.longerThan(5.f)); +} diff --git a/src/lib/matrix/test/vector.cpp b/src/lib/matrix/test/vector.cpp deleted file mode 100644 index f5c5b198a5..0000000000 --- a/src/lib/matrix/test/vector.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "test_macros.hpp" - -#include - -using namespace matrix; - -int main() -{ - // test data - float data1[] = {1, 2, 3, 4, 5}; - float data2[] = {6, 7, 8, 9, 10}; - Vector v1(data1); - Vector v2(data2); - - // copy constructor - Vector v3(v2); - TEST(isEqual(v2, v3)); - - // norm, dot product - TEST(isEqualF(v1.norm(), 7.416198487095663f)); - TEST(isEqualF(v1.norm_squared(), v1.norm() * v1.norm())); - TEST(isEqualF(v1.norm(), v1.length())); - TEST(isEqualF(v1.dot(v2), 130.0f)); - TEST(isEqualF(v1.dot(v2), v1 * v2)); - - // unit, unit_zero, normalize - TEST(isEqualF(v2.unit().norm(), 1.f)); - TEST(isEqualF(v2.unit_or_zero().norm(), 1.f)); - TEST(isEqualF(Vector().unit_or_zero().norm(), 0.f)); - v2.normalize(); - TEST(isEqualF(v2.norm(), 1.f)); - - // sqrt - float data1_sq[] = {1, 4, 9, 16, 25}; - Vector v4(data1_sq); - TEST(isEqual(v1, v4.sqrt())); - - // longerThan - Vector v5; - v5(0) = 3; - v5(1) = 4; - TEST(v5.longerThan(4.99f)); - TEST(!v5.longerThan(5.f)); - - return 0; -} -