Tests: add unit tests for Matrix4D and Rotation

This commit is contained in:
wmayer
2023-02-22 15:24:09 +01:00
committed by wwmayer
parent aa788af4bd
commit 3a589cba11
3 changed files with 190 additions and 0 deletions

View File

@@ -1,6 +1,8 @@
target_sources(
Tests_run
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Matrix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Rotation.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tst_Tools.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Units.cpp
)

123
tests/src/Base/Matrix.cpp Normal file
View File

@@ -0,0 +1,123 @@
#include "gtest/gtest.h"
#include <Base/Matrix.h>
TEST(Matrix, TestShearing)
{
Base::Matrix4D mat;
mat.setRow(1, Base::Vector3d(0, 1, 1));
EXPECT_EQ(mat.hasScale(), Base::ScaleType::Other);
}
TEST(Matrix, TestMultLeftRight)
{
Base::Matrix4D mat1;
mat1.rotX(1.0);
Base::Matrix4D mat2;
mat2.scale(1.0, 2.0, 3.0);
EXPECT_EQ((mat1 * mat2).hasScale(), Base::ScaleType::NonUniformRight);
EXPECT_EQ((mat2 * mat1).hasScale(), Base::ScaleType::NonUniformLeft);
}
TEST(Matrix, TestNoScale)
{
Base::Matrix4D mat;
mat.rotX(1.0);
mat.scale(1.0);
EXPECT_EQ(mat.hasScale(), Base::ScaleType::NoScaling);
}
TEST(Matrix, TestUniformScalePlusTwo)
{
Base::Matrix4D mat;
mat.rotX(1.0);
mat.scale(2.0);
EXPECT_EQ(mat.hasScale(), Base::ScaleType::Uniform);
}
TEST(Matrix, TestUniformScaleRight)
{
Base::Matrix4D mat;
mat.rotX(1.0);
Base::Matrix4D scale;
scale.scale(2.0);
mat = mat * scale;
EXPECT_EQ(mat.hasScale(), Base::ScaleType::Uniform);
}
TEST(Matrix, TestNonUniformScaleRight)
{
Base::Matrix4D mat;
mat.rotX(1.0);
Base::Matrix4D scale;
scale.scale(2.0, 1.0, 2.0);
mat = mat * scale;
EXPECT_EQ(mat.hasScale(), Base::ScaleType::NonUniformRight);
}
TEST(Matrix, TestUniformScaleMinusOne)
{
Base::Matrix4D mat;
mat.scale(-1.0);
EXPECT_EQ(mat.hasScale(), Base::ScaleType::Uniform);
}
TEST(Matrix, TestUniformScaleMinusTwo)
{
Base::Matrix4D mat;
mat.scale(-2.0);
EXPECT_EQ(mat.hasScale(), Base::ScaleType::Uniform);
}
TEST(Matrix, TestNonUniformScaleLeftOne)
{
Base::Matrix4D mat;
mat.rotX(1.0);
mat.scale(1.0, -1.0, 1.0);
// A scale(1,-1,1) is like scale(-1,-1,-1) * rotation
EXPECT_EQ(mat.hasScale(), Base::ScaleType::Uniform);
}
TEST(Matrix, TestNonUniformScaleLeftTwo)
{
Base::Matrix4D mat;
mat.rotX(1.0);
mat.scale(2.0, -2.0, 2.0);
EXPECT_EQ(mat.hasScale(), Base::ScaleType::Uniform);
}
TEST(Matrix, TestTrace)
{
Base::Matrix4D mat;
mat.scale(2.0, 2.0, 2.0);
Base::Vector3d trace = mat.trace();
EXPECT_EQ(trace.x + trace.y + trace.z, 6.0);
}
TEST(Matrix, TestColRow)
{
Base::Matrix4D mat;
EXPECT_EQ(mat.getCol(1), Base::Vector3d(0, 1, 0));
EXPECT_EQ(mat.getCol(2), Base::Vector3d(0, 0, 1));
}
TEST(Matrix, TestUnity)
{
Base::Matrix4D mat;
EXPECT_EQ(mat.isUnity(), true);
mat.nullify();
EXPECT_EQ(mat.isUnity(), false);
mat.setToUnity();
EXPECT_EQ(mat.isUnity(), true);
}
TEST(Matrix, TestNull)
{
Base::Matrix4D mat;
EXPECT_EQ(mat.isNull(), false);
mat.nullify();
EXPECT_EQ(mat.isNull(), true);
}

View File

@@ -0,0 +1,65 @@
#include "gtest/gtest.h"
#include <Base/Matrix.h>
#include <Base/Rotation.h>
TEST(Rotation, TestNonUniformScaleLeft)
{
Base::Rotation rot;
rot.setYawPitchRoll(20.0, 0.0, 0.0);
Base::Matrix4D mat;
rot.getValue(mat);
Base::Matrix4D scale;
scale.scale(2.0, 3.0, 4.0);
Base::Rotation scaled_rot(scale * mat);
EXPECT_EQ(scaled_rot.isSame(rot, 1.0e-7), true);
EXPECT_EQ(rot.isSame(scaled_rot, 1.0e-7), true);
}
TEST(Rotation, TestNonUniformScaleRight)
{
Base::Rotation rot;
rot.setYawPitchRoll(20.0, 0.0, 0.0);
Base::Matrix4D mat;
rot.getValue(mat);
Base::Matrix4D scale;
scale.scale(2.0, 3.0, 4.0);
Base::Rotation scaled_rot(mat * scale);
EXPECT_EQ(scaled_rot.isSame(rot, 1.0e-7), true);
EXPECT_EQ(rot.isSame(scaled_rot, 1.0e-7), true);
}
TEST(Rotation, TestUniformScaleGT1)
{
Base::Rotation rot;
rot.setYawPitchRoll(20.0, 0.0, 0.0);
Base::Matrix4D mat;
rot.getValue(mat);
Base::Matrix4D scale;
scale.scale(3.0, 3.0, 3.0);
Base::Rotation scaled_rot(mat * scale);
EXPECT_EQ(scaled_rot.isSame(rot, 1.0e-7), true);
EXPECT_EQ(rot.isSame(scaled_rot, 1.0e-7), true);
}
TEST(Rotation, TestUniformScaleLT1)
{
Base::Matrix4D mat;
mat.scale(0.5);
Base::Rotation scaled_rot(mat);
EXPECT_EQ(scaled_rot.isSame(scaled_rot, 1.0e-7), true);
}