Browse Source

Matrix: convert vector test to gtest

v1.13.0-BW
Matthias Grob 3 years ago
parent
commit
91493307b9
  1. 2
      src/lib/matrix/test/CMakeLists.txt
  2. 76
      src/lib/matrix/test/MatrixVectorTest.cpp
  3. 47
      src/lib/matrix/test/vector.cpp

2
src/lib/matrix/test/CMakeLists.txt

@ -6,7 +6,6 @@ add_compile_options( @@ -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) @@ -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)

76
src/lib/matrix/test/MatrixVectorTest.cpp

@ -0,0 +1,76 @@ @@ -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 <gtest/gtest.h>
#include <matrix/math.hpp>
using namespace matrix;
TEST(MatrixVectorTest, Vector)
{
// test data
float data1[] = {1, 2, 3, 4, 5};
float data2[] = {6, 7, 8, 9, 10};
Vector<float, 5> v1(data1);
Vector<float, 5> v2(data2);
// copy constructor
Vector<float, 5> 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<float, 5>().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<float, 5> v4(data1_sq);
EXPECT_EQ(v1, v4.sqrt());
// longerThan
Vector<float, 2> v5;
v5(0) = 3;
v5(1) = 4;
EXPECT_TRUE(v5.longerThan(4.99f));
EXPECT_FALSE(v5.longerThan(5.f));
}

47
src/lib/matrix/test/vector.cpp

@ -1,47 +0,0 @@ @@ -1,47 +0,0 @@
#include "test_macros.hpp"
#include <matrix/math.hpp>
using namespace matrix;
int main()
{
// test data
float data1[] = {1, 2, 3, 4, 5};
float data2[] = {6, 7, 8, 9, 10};
Vector<float, 5> v1(data1);
Vector<float, 5> v2(data2);
// copy constructor
Vector<float, 5> 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<float, 5>().unit_or_zero().norm(), 0.f));
v2.normalize();
TEST(isEqualF(v2.norm(), 1.f));
// sqrt
float data1_sq[] = {1, 4, 9, 16, 25};
Vector<float, 5> v4(data1_sq);
TEST(isEqual(v1, v4.sqrt()));
// longerThan
Vector<float, 2> v5;
v5(0) = 3;
v5(1) = 4;
TEST(v5.longerThan(4.99f));
TEST(!v5.longerThan(5.f));
return 0;
}
Loading…
Cancel
Save