Added Matrix identity macros and few matrix unit tests (#742)

* Add matrix identity macros

* Add matrix some matrix unit tests

---------

Co-authored-by: Tonis <tanton@paysure.solutions>
This commit is contained in:
Tonis
2023-03-05 21:39:55 +02:00
committed by GitHub
parent 549e1b6029
commit ff4c35fae1
3 changed files with 53 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
module math_tests;
import std::math;
fn void! test_abs() @test
{
int x = -21;
assert(math::abs(x) == 21);
double y = -123.0;
assert(math::abs(y) == 123.0);
float z = -21.0f;
assert(math::abs(z) == 21.0f);
$assert(@typeis(math::abs(z), float));
int[<3>] xx = { -1, -1000, 1000 };
assert(math::abs(xx) == int[<3>] { 1, 1000, 1000 });
double[<3>] yy = { -1, -0.5, 1000 };
assert(math::abs(yy) == double[<3>] { 1, 0.5, 1000 });
}
fn void! test_sign() @test
{
int x = -21;
assert(math::sign(x) == -1);
x = 238219382;
assert(math::sign(x) == 1);
x = 0;
assert(math::sign(x) == 0);
uint y = 23;
assert(math::sign(y) == 1);
y = 0;
assert(math::sign(y) == 0);
y = (uint)-21;
assert(math::sign(y) == 1);
assert(@typeis(math::sign(y), uint));
}
fn void! test_atan() @test
{
int x = 231;
assert(math::atan(x) == 1.5664673495078372);
x = 1;
assert(math::atan(x) == 0.7853981633974483);
x = 0;
assert(math::atan(x) == 0.0);
x = -1;
assert(math::atan(x) == -0.7853981633974483);
x = -231;
assert(math::atan(x) == -1.5664673495078372);
float f = 0;
assert(math::atan(f) == 0.0f);
f = 1;
assert(math::atan(f) == 0.7853981633974483f);
f = -1;
assert(math::atan(f) == -0.7853981633974483f);
assert(@typeis(math::atan(f), float));
double d = 0;
assert(math::atan(d) == 0.0);
d = 1;
assert(math::atan(d) == 0.7853981633974483);
d = -1;
assert(math::atan(d) == -0.7853981633974483);
assert(@typeis(math::atan(d), double));
}
fn void! test_ceil() @test
{
double d = -123.1;
assert(math::ceil(d) == -123.0);
d = 123.1;
assert(math::ceil(d) == 124.0);
d = 0.1;
assert(math::ceil(d) == 1);
d = -0.9;
assert(math::ceil(d) == 0);
$assert(@typeis(math::ceil(d), double));
float f = -123.1f;
assert(math::ceil(f) == -123.0f);
f = 123.1f;
assert(math::ceil(f) == 124.0f);
f = 0.1f;
assert(math::ceil(f) == 1.0f);
f = -0.9f;
assert(math::ceil(f) == 0.0f);
$assert(@typeis(math::ceil(f), float));
double[<5>] vec = { -123.1, 123.1, 0.1, -0.9, 0 };
assert(math::ceil(vec) == double[<5>] { -123, 124, 1, 0, 0 });
}
fn void! test_floor() @test
{
double d = -123.1;
assert(math::floor(d) == -124.0);
d = 123.1;
assert(math::floor(d) == 123.0);
d = 0.9;
assert(math::floor(d) == 0);
d = -0.1;
assert(math::floor(d) == -1);
$assert(@typeis(math::floor(d), double));
float f = -123.1f;
assert(math::floor(f) == -124.0f);
f = 123.1f;
assert(math::floor(f) == 123.0f);
f = 0.9f;
assert(math::floor(f) == 0.0f);
f = -0.1f;
assert(math::floor(f) == -1.0f);
$assert(@typeis(math::floor(f), float));
double[<5>] vec = { -123.1, 123.1, 0.9, -0.1, 0 };
assert(math::floor(vec) == double[<5>] { -124, 123, 0, -1, 0 });
}
fn void! test_trunc() @test
{
double d = -123.9;
assert(math::trunc(d) == -123.0);
d = 123.1;
assert(math::trunc(d) == 123.0);
d = 0.9;
assert(math::trunc(d) == 0);
d = -0.9;
assert(math::trunc(d) == 0);
$assert(@typeis(math::trunc(d), double));
float f = -123.9f;
assert(math::trunc(f) == -123.0f);
f = 123.9f;
assert(math::trunc(f) == 123.0f);
f = 0.9f;
assert(math::trunc(f) == 0.0f);
f = -0.9f;
assert(math::trunc(f) == -0.0f);
$assert(@typeis(math::trunc(f), float));
double[<5>] vec = { -123.9, 123.9, 0.9, -0.9, 0 };
assert(math::trunc(vec) == double[<5>] { -123, 123, 0, 0, 0 });
}

View File

@@ -0,0 +1,50 @@
module math_matrix @test;
import std::math;
fn void test_mat4()
{
{|
Matrix4 mat = math::matrix4_indentity();
Matrix4 mat2 = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
Matrix4 calc = mat.mul(mat2);
assert(calc.m == mat.m);
Matrix4 translated = mat.translate(Vec3{0.0, 0.0, 0.0});
assert(translated.m == mat.m);
|};
{|
Matrix4 mat = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
Matrix4 mat2 = { 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1 };
Matrix4 calc = mat.mul(mat2);
Matrix4 value = { 56, 46, 36, 26, 152, 126, 100, 74, 56, 46, 36, 26, 152, 126, 100, 74 };
assert(calc.m == value.m);
|};
}
fn void test_mat3()
{
Matrix3 mat = { 3, 5, 3, 5, 2, 6, 6, 2, 1 };
Matrix3 mat2 = { 4, 2, 6, 7, 8, 9, 2, 3, 4 };
Matrix3 calc = mat.mul(mat2);
Matrix3 value = { 53, 55, 75, 46, 44, 72, 40, 31, 58 };
assert(calc.m == value.m);
}
fn void test_mat2()
{
Matrix2 mat = { 3, 5, 5, 2};
Matrix2 mat2 = { 4, 2, 7, 8 };
Matrix2 calc = mat.mul(mat2);
Matrix2 value = { 47, 46, 34, 26 };
assert(calc.m == value.m);
}
fn void test_vec3()
{
Vec3 cross = Vec3{2,3,4}.cross(Vec3{5,6,7});
assert(cross == Vec3{-3,6,-3});
}