mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-04-14 10:07:39 +08:00
Matrix: convert vector3 test to gtest
This commit is contained in:
parent
ddfd62dfc2
commit
f4b53d2762
@ -8,7 +8,6 @@ add_compile_options(
|
||||
set(tests
|
||||
vectorAssignment
|
||||
vector
|
||||
vector3
|
||||
)
|
||||
|
||||
add_custom_target(test_build)
|
||||
@ -45,3 +44,4 @@ px4_add_unit_gtest(SRC MatrixTransposeTest.cpp)
|
||||
px4_add_unit_gtest(SRC MatrixUnwrapTest.cpp)
|
||||
px4_add_unit_gtest(SRC MatrixUpperRightTriangleTest.cpp)
|
||||
px4_add_unit_gtest(SRC MatrixVector2Test.cpp)
|
||||
px4_add_unit_gtest(SRC MatrixVector3Test.cpp)
|
||||
|
||||
89
src/lib/matrix/test/MatrixVector3Test.cpp
Normal file
89
src/lib/matrix/test/MatrixVector3Test.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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(MatrixVector3Test, Vector3)
|
||||
{
|
||||
Vector3f a(1, 0, 0);
|
||||
Vector3f b(0, 1, 0);
|
||||
Vector3f c = a.cross(b);
|
||||
EXPECT_EQ(c, Vector3f(0, 0, 1));
|
||||
c = a % b;
|
||||
EXPECT_EQ(c, Vector3f(0, 0, 1));
|
||||
Matrix<float, 3, 1> d(c);
|
||||
Vector3f e(d);
|
||||
EXPECT_EQ(e, d);
|
||||
float data[] = {4, 5, 6};
|
||||
Vector3f f(data);
|
||||
EXPECT_EQ(f, Vector3f(4, 5, 6));
|
||||
|
||||
EXPECT_EQ(a + b, Vector3f(1, 1, 0));
|
||||
EXPECT_EQ(a - b, Vector3f(1, -1, 0));
|
||||
EXPECT_FLOAT_EQ(a * b, 0.0f);
|
||||
EXPECT_EQ(-a, Vector3f(-1, 0, 0));
|
||||
EXPECT_EQ(a.unit(), a);
|
||||
EXPECT_EQ(a.unit(), a.normalized());
|
||||
EXPECT_EQ(a * 2.0, Vector3f(2, 0, 0));
|
||||
|
||||
Vector2f g2(1, 3);
|
||||
Vector3f g3(7, 11, 17);
|
||||
g3.xy() = g2;
|
||||
EXPECT_EQ(g3, Vector3f(1, 3, 17));
|
||||
|
||||
const Vector3f g4(g3);
|
||||
Vector2f g5 = g4.xy();
|
||||
EXPECT_EQ(g5, g2);
|
||||
EXPECT_EQ(g2, Vector2f(g4.xy()));
|
||||
|
||||
Vector3f h;
|
||||
EXPECT_EQ(h, Vector3f(0, 0, 0));
|
||||
|
||||
Vector<float, 4> j;
|
||||
j(0) = 1;
|
||||
j(1) = 2;
|
||||
j(2) = 3;
|
||||
j(3) = 4;
|
||||
|
||||
Vector3f k = j.slice<3, 1>(0, 0);
|
||||
Vector3f k_test(1, 2, 3);
|
||||
EXPECT_EQ(k, k_test);
|
||||
|
||||
Vector3f m1(1, 2, 3);
|
||||
Vector3f m2(3.1f, 4.1f, 5.1f);
|
||||
EXPECT_EQ(m2, m1 + 2.1f);
|
||||
EXPECT_EQ(m2 - 2.1f, m1);
|
||||
}
|
||||
@ -1,60 +0,0 @@
|
||||
#include "test_macros.hpp"
|
||||
|
||||
#include <matrix/math.hpp>
|
||||
|
||||
using namespace matrix;
|
||||
|
||||
int main()
|
||||
{
|
||||
Vector3f a(1, 0, 0);
|
||||
Vector3f b(0, 1, 0);
|
||||
Vector3f c = a.cross(b);
|
||||
TEST(isEqual(c, Vector3f(0, 0, 1)));
|
||||
c = a % b;
|
||||
TEST(isEqual(c, Vector3f(0, 0, 1)));
|
||||
Matrix<float, 3, 1> d(c);
|
||||
Vector3f e(d);
|
||||
TEST(isEqual(e, d));
|
||||
float data[] = {4, 5, 6};
|
||||
Vector3f f(data);
|
||||
TEST(isEqual(f, Vector3f(4, 5, 6)));
|
||||
|
||||
TEST(isEqual(a + b, Vector3f(1, 1, 0)));
|
||||
TEST(isEqual(a - b, Vector3f(1, -1, 0)));
|
||||
TEST(isEqualF(a * b, 0.0f));
|
||||
TEST(isEqual(-a, Vector3f(-1, 0, 0)));
|
||||
TEST(isEqual(a.unit(), a));
|
||||
TEST(isEqual(a.unit(), a.normalized()));
|
||||
TEST(isEqual(a * 2.0, Vector3f(2, 0, 0)));
|
||||
|
||||
Vector2f g2(1, 3);
|
||||
Vector3f g3(7, 11, 17);
|
||||
g3.xy() = g2;
|
||||
TEST(isEqual(g3, Vector3f(1, 3, 17)));
|
||||
|
||||
const Vector3f g4(g3);
|
||||
Vector2f g5 = g4.xy();
|
||||
TEST(isEqual(g5, g2));
|
||||
TEST(isEqual(g2, Vector2f(g4.xy())));
|
||||
|
||||
Vector3f h;
|
||||
TEST(isEqual(h, Vector3f(0, 0, 0)));
|
||||
|
||||
Vector<float, 4> j;
|
||||
j(0) = 1;
|
||||
j(1) = 2;
|
||||
j(2) = 3;
|
||||
j(3) = 4;
|
||||
|
||||
Vector3f k = j.slice<3, 1>(0, 0);
|
||||
Vector3f k_test(1, 2, 3);
|
||||
TEST(isEqual(k, k_test));
|
||||
|
||||
Vector3f m1(1, 2, 3);
|
||||
Vector3f m2(3.1f, 4.1f, 5.1f);
|
||||
TEST(isEqual(m2, m1 + 2.1f));
|
||||
TEST(isEqual(m2 - 2.1f, m1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user