From 821c79572f3f2631738c0cb9c5583349fdbf0d9a Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Sun, 15 Apr 2018 20:47:38 +0300 Subject: [PATCH] test: add some tests for mat3 --- makefile.am | 3 ++- test/src/test_common.c | 23 +++++++++++++++++ test/src/test_common.h | 6 +++++ test/src/test_main.c | 3 +++ test/src/test_mat3.c | 58 ++++++++++++++++++++++++++++++++++++++++++ test/src/test_tests.h | 3 +++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 test/src/test_mat3.c diff --git a/makefile.am b/makefile.am index 00800c0..1a27132 100644 --- a/makefile.am +++ b/makefile.am @@ -111,7 +111,8 @@ test_tests_SOURCES=\ test/src/test_euler.c \ test/src/test_quat.c \ test/src/test_vec4.c \ - test/src/test_vec3.c + test/src/test_vec3.c \ + test/src/test_mat3.c all-local: sh ./post-build.sh diff --git a/test/src/test_common.c b/test/src/test_common.c index 60baac3..405000d 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -27,6 +27,17 @@ test_rand_mat4(mat4 dest) { /* glm_scale(dest, (vec3){drand48(), drand48(), drand48()}); */ } +void +test_rand_mat3(mat3 dest) { + mat4 m4; + + srand((unsigned int)time(NULL)); + + /* random rotatation around random axis with random angle */ + glm_rotate_make(m4, drand48(), (vec3){drand48(), drand48(), drand48()}); + glm_mat4_pick3(m4, dest); +} + void test_rand_vec3(vec3 dest) { srand((unsigned int)time(NULL)); @@ -84,6 +95,18 @@ test_assert_mat4_eq2(mat4 m1, mat4 m2, float eps) { } } +void +test_assert_mat3_eq(mat3 m1, mat3 m2) { + int i, j, k; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + for (k = 0; k < 3; k++) + assert_true(fabsf(m1[i][j] - m2[i][j]) <= 0.0000009); + } + } +} + void test_assert_eqf(float a, float b) { assert_true(fabsf(a - b) <= 0.000009); /* rounding errors */ diff --git a/test/src/test_common.h b/test/src/test_common.h index db113a4..7881e7a 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -25,6 +25,9 @@ void test_rand_mat4(mat4 dest); +void +test_rand_mat3(mat3 dest); + void test_assert_eqf(float a, float b); @@ -34,6 +37,9 @@ test_assert_mat4_eq(mat4 m1, mat4 m2); void test_assert_mat4_eq2(mat4 m1, mat4 m2, float eps); +void +test_assert_mat3_eq(mat3 m1, mat3 m2); + void test_assert_vec3_eq(vec3 v1, vec3 v2); diff --git a/test/src/test_main.c b/test/src/test_main.c index b0649cd..9e4f5aa 100644 --- a/test/src/test_main.c +++ b/test/src/test_main.c @@ -12,6 +12,9 @@ main(int argc, const char * argv[]) { /* mat4 */ cmocka_unit_test(test_mat4), + /* mat3 */ + cmocka_unit_test(test_mat3), + /* camera */ cmocka_unit_test(test_camera_lookat), cmocka_unit_test(test_camera_decomp), diff --git a/test/src/test_mat3.c b/test/src/test_mat3.c new file mode 100644 index 0000000..d97d1f5 --- /dev/null +++ b/test/src/test_mat3.c @@ -0,0 +1,58 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "test_common.h" + +#define m 3 +#define n 3 + +void +test_mat3(void **state) { + mat3 m1 = GLM_MAT3_IDENTITY_INIT; + mat3 m2 = GLM_MAT3_IDENTITY_INIT; + mat3 m3; + mat3 m4 = GLM_MAT3_ZERO_INIT; + mat3 m5; + int i, j, k; + + /* test identity matrix multiplication */ + glmc_mat3_mul(m1, m2, m3); + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + if (i == j) + assert_true(m3[i][j] == 1.0f); + else + assert_true(m3[i][j] == 0.0f); + } + } + + /* test random matrices */ + /* random matrices */ + test_rand_mat3(m1); + test_rand_mat3(m2); + + glmc_mat3_mul(m1, m2, m3); + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + for (k = 0; k < m; k++) + /* column-major */ + m4[i][j] += m1[k][j] * m2[i][k]; + } + } + + test_assert_mat3_eq(m3, m4); + + for (i = 0; i < 100000; i++) { + test_rand_mat3(m3); + test_rand_mat3(m4); + + /* test inverse precise */ + glmc_mat3_inv(m3, m4); + glmc_mat3_inv(m4, m5); + test_assert_mat3_eq(m3, m5); + } +} diff --git a/test/src/test_tests.h b/test/src/test_tests.h index 224d943..dea8860 100644 --- a/test/src/test_tests.h +++ b/test/src/test_tests.h @@ -9,6 +9,9 @@ /* mat4 */ void test_mat4(void **state); +/* mat3 */ +void test_mat3(void **state); + /* camera */ void test_camera_lookat(void **state);