From 38be5383420544877958ab34e7eff16f23333ebd Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Tue, 25 Apr 2017 23:27:15 +0300 Subject: [PATCH] test: test mat4_mul --- makefile.am | 16 +++++++++ test/src/test_common.h | 40 ++++++++++++++++++++++ test/src/test_mat4.c | 77 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 test/src/test_common.h create mode 100644 test/src/test_mat4.c diff --git a/makefile.am b/makefile.am index eac1d25..7db19d9 100644 --- a/makefile.am +++ b/makefile.am @@ -18,6 +18,20 @@ AM_CFLAGS = -Wall \ lib_LTLIBRARIES = libcglm.la libcglm_la_LDFLAGS = -no-undefined -version-info 0:1:0 +checkLDFLAGS = -L./.libs \ + -L./test/lib/cmocka/build/src \ + -lcmocka \ + -lm \ + -lcglm +checkCFLAGS = -I./test/lib/cmocka/include \ + -I./include + +check_PROGRAMS = test/test_mat4 +TESTS = $(check_PROGRAMS) + +test_test_mat4_LDFLAGS = $(checkLDFLAGS) +test_test_mat4_CFLAGS = $(checkCFLAGS) + nobase_include_HEADERS = include/cglm.h \ include/cglm-call.h \ include/cglm-cam.h \ @@ -57,3 +71,5 @@ libcglm_la_SOURCES=\ src/cglm-vec.c \ src/cglm-mat3.c \ src/cglm-mat.c + +test_test_mat4_SOURCES=test/src/test_mat4.c diff --git a/test/src/test_common.h b/test/src/test_common.h new file mode 100644 index 0000000..71e5962 --- /dev/null +++ b/test/src/test_common.h @@ -0,0 +1,40 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef test_common_h +#define test_common_h + +#include +#include +#include +#include +#include +#include + +#include +#include + +#define precision 0.00001f + +static +void +test_rand_mat4(mat4 dest); + +static +void +test_rand_mat4(mat4 dest) { + int i, j; + + srand((unsigned int)time(NULL)); + for (i = 0; i < 4; i++) { + for (j = 0; j < 4; j++) { + dest[i][j] = drand48(); + } + } +} + +#endif /* test_common_h */ diff --git a/test/src/test_mat4.c b/test/src/test_mat4.c new file mode 100644 index 0000000..e6a7947 --- /dev/null +++ b/test/src/test_mat4.c @@ -0,0 +1,77 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include +#include +#include +#include + +#include "test_common.h" + +#define m 4 +#define n 4 + +void +test_mat4_mul(void **state) { + mat4 m1 = GLM_MAT4_IDENTITY_INIT; + mat4 m2 = GLM_MAT4_IDENTITY_INIT; + mat4 m3; + mat4 m4 = GLM_MAT4_ZERO_INIT; + int i, j, k; + + /* test identity matrix multiplication */ + glm_mat4_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_mat4(m1); + test_rand_mat4(m2); + + glm_mat4_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]; + } + } + + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + for (k = 0; k < m; k++) + assert_true(fabsf(m3[i][j] - m4[i][j]) <= FLT_EPSILON); + } + } + + /* test pre compiled */ + glmc_mat4_mul(m1, m2, m3); + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + for (k = 0; k < m; k++) + assert_true(fabsf(m3[i][j] - m4[i][j]) <= FLT_EPSILON); + } + } +} + +int +main(int argc, const char * argv[]) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_mat4_mul) + }; + + return cmocka_run_group_tests(tests, + NULL, + NULL); +}