97 lines
2.5 KiB
C++
97 lines
2.5 KiB
C++
#include <J3ML/LinearAlgebra/Matrix3x3.h>
|
|
#include <jtest/jtest.hpp>
|
|
|
|
using namespace J3ML::LinearAlgebra;
|
|
|
|
// TODO: create RNG instance
|
|
|
|
int Matrix3x3Tests() {
|
|
|
|
|
|
TEST("Mat3x3::Add_Unary", []
|
|
{
|
|
Matrix3x3 m(1,2,3, 4,5,6, 7,8,9);
|
|
Matrix3x3 m2 = +m;
|
|
jtest::check(m.Equals(m2));
|
|
});
|
|
|
|
TEST("Mat3x3::Solve_Axb", []{
|
|
RNG rng;
|
|
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);
|
|
jtest::check(success || mayFail);
|
|
if (success)
|
|
{
|
|
Vector3 b2 = A*x;
|
|
jtest::check(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();
|
|
jtest::check(success);
|
|
});
|
|
|
|
TEST("Mat3x3::Inverse", []
|
|
{
|
|
RNG rng;
|
|
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();
|
|
jtest::check(success || mayFail);
|
|
if (success)
|
|
{
|
|
Matrix3x3 id = A * A2;
|
|
Matrix3x3 id2 = A2 * A;
|
|
jtest::check(id.Equals(Matrix3x3::Identity, 0.3f));
|
|
jtest::check(id2.Equals(Matrix3x3::Identity, 0.3f));
|
|
}
|
|
});
|
|
|
|
|
|
TEST("Mat3x3::InverseFast", []
|
|
{
|
|
// TODO: Fix implementation of 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", []
|
|
{
|
|
RNG rng;
|
|
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;
|
|
jtest::check(test.Equals(correct));
|
|
});
|
|
|
|
return 0;
|
|
}
|