Implement Matrix3x3 Tests (Fails on InverseFast)
Some checks are pending
Run tests / Explore-Gitea-Actions (push) Waiting to run
Build Docs With Doxygen / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
2024-06-04 16:24:57 -04:00
parent c17ed20fa9
commit be47e3f8fe
2 changed files with 89 additions and 3 deletions

View File

@@ -0,0 +1,86 @@
#include <gtest/gtest.h>
#include <J3ML/LinearAlgebra/Matrix3x3.h>
using namespace J3ML::LinearAlgebra;
// TODO: create RNG instance
RNG rng;
TEST(Mat3x3, Add_Unary)
{
Matrix3x3 m(1,2,3, 4,5,6, 7,8,9);
Matrix3x3 m2 = +m;
assert(m.Equals(m2));
}
TEST(Mat3x3, Solve_Axb)
{
Matrix3x3 A = Matrix3x3::RandomGeneral(rng, -10.f, 10.f);
bool mayFail = Math::EqualAbs(A.Determinant(), 0.f, 1e-2f);
Vector3 b = Vector3::RandomBox(rng, Vector3::FromScalar(-10.f), Vector3::FromScalar(10.f));
Vector3 x;
bool success = A.SolveAxb(b, x);
assert(success || mayFail);
if (success)
{
Vector3 b2 = A*x;
assert(b2.Equals(b, 1e-1f));
}
}
TEST(Mat3x3, Inverse_Case)
{
Matrix3x3 m(-8.75243664f,6.71196938f,-5.95816374f,6.81996822f,-6.85106039f,2.38949537f,-0.856015682f,3.45762491f,3.311584f);
bool success = m.Inverse();
assert(success);
}
TEST(Mat3x3, Inverse)
{
Matrix3x3 A = Matrix3x3::RandomGeneral(rng, -10.f, 10.f);
bool mayFail = Math::EqualAbs(A.Determinant(), 0.f, 1e-2f);
Matrix3x3 A2 = A;
bool success = A2.Inverse();
assert(success || mayFail);
if (success)
{
Matrix3x3 id = A * A2;
Matrix3x3 id2 = A2 * A;
assert(id.Equals(Matrix3x3::Identity, 0.3f));
assert(id2.Equals(Matrix3x3::Identity, 0.3f));
}
}
TEST(Mat3x3, InverseFast)
{
Matrix3x3 A = Matrix3x3::RandomGeneral(rng, -10.f, 10.f);
bool mayFail = Math::EqualAbs(A.Determinant(), 0.f, 1e-2f);
Matrix3x3 A2 = A;
bool success = A2.InverseFast();
assert(success || mayFail);
if (success)
{
Matrix3x3 id = A * A2;
Matrix3x3 id2 = A2 * A;
assert(id.Equals(Matrix3x3::Identity, 0.3f));
assert(id2.Equals(Matrix3x3::Identity, 0.3f));
}
}
TEST(Mat3x3, MulMat4x4)
{
Matrix3x3 m = Matrix3x3::RandomGeneral(rng, -10.f, 10.f);
Matrix4x4 m_ = m;
Matrix4x4 m2 = Matrix4x4::RandomGeneral(rng, -10.f, 10.f);
Matrix4x4 test = m * m2;
Matrix4x4 correct = m_ * m2;
assert(test.Equals(correct));
}

View File

@@ -156,7 +156,7 @@ TEST(Vector2Test, V2_Normalize)
{
Vector2 A{2, 0};
Vector2 B{1, 0};
EXPECT_EQ(A.Normalize(), B);
EXPECT_EQ(A.Normalized(), B);
}
TEST(Vector2Test, V2_Lerp)
@@ -175,8 +175,8 @@ TEST(Vector2Test, V2_AngleBetween)
Vector2 A {0.5f, 0.5};
Vector2 B {0.5f, 0.1f};
A = A.Normalize();
B = B.Normalize();
A = A.Normalized();
B = B.Normalized();
// TODO: AngleBetween returns not a number
EXPECT_FLOAT_EQ(A.AngleBetween(B), 0.58800244);