From a10d28667d734dfc27b0eaac5444ff944d046b94 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Dec 2021 18:39:11 -0800 Subject: Initial commit. --- test/mat4_test.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 test/mat4_test.c (limited to 'test/mat4_test.c') diff --git a/test/mat4_test.c b/test/mat4_test.c new file mode 100644 index 0000000..d4f61bc --- /dev/null +++ b/test/mat4_test.c @@ -0,0 +1,111 @@ +#include + +#include + +#include "test.h" + +#include + +static const float eps = 1e-9; + +static inline void print_mat4(mat4 m) { + printf("\n"); + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + printf("%6.3f ", mat4_at(m, i, j)); + } + printf("\n"); + } +} + +TEST_CASE(lookat_right) { + const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0), + /*target=*/vec3_make(1, 0, 0), + /*up=*/vec3_make(0, 1, 0)); + + const mat4 expected = mat4_from_array((const float*)(float[4][4]){ + {0, 0, 1, 0}, + {0, 1, 0, 0}, + {1, 0, 0, 0}, + {0, 0, 0, 1}, + }); + + TEST_TRUE(mat4_eq(m, expected, eps)); +} + +TEST_CASE(lookat_left) { + const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0), + /*target=*/vec3_make(-1, 0, 0), + /*up=*/vec3_make(0, 1, 0)); + + const mat4 expected = mat4_from_array((const float*)(float[4][4]){ + {0, 0, -1, 0}, + {0, 1, 0, 0}, + {-1, 0, 0, 0}, + {0, 0, 0, 1}, + }); + + TEST_TRUE(mat4_eq(m, expected, eps)); +} + +TEST_CASE(lookat_up) { + const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0), + /*target=*/vec3_make(0, 1, 0), + /*up=*/vec3_make(0, 0, 1)); + + const mat4 expected = mat4_from_array((const float*)(float[4][4]){ + {1, 0, 0, 0}, + {0, 0, 1, 0}, + {0, 1, 0, 0}, + {0, 0, 0, 1}, + }); + + TEST_TRUE(mat4_eq(m, expected, eps)); +} + +TEST_CASE(lookat_down) { + const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0), + /*target=*/vec3_make(0, -1, 0), + /*up=*/vec3_make(0, 0, -1)); + + const mat4 expected = mat4_from_array((const float*)(float[4][4]){ + {1, 0, 0, 0}, + {0, 0, -1, 0}, + {0, -1, 0, 0}, + {0, 0, 0, 1}, + }); + + TEST_TRUE(mat4_eq(m, expected, eps)); +} + +TEST_CASE(lookat_forward) { + const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0), + /*target=*/vec3_make(0, 0, -1), + /*up=*/vec3_make(0, 1, 0)); + + const mat4 expected = mat4_from_array((const float*)(float[4][4]){ + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, -1, 0}, + {0, 0, 0, 1}, + }); + + TEST_TRUE(mat4_eq(m, expected, eps)); +} + +TEST_CASE(lookat_back) { + const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0), + /*target=*/vec3_make(0, 0, 1), + /*up=*/vec3_make(0, 1, 0)); + + const mat4 expected = mat4_from_array((const float*)(float[4][4]){ + {-1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1}, + }); + + TEST_TRUE(mat4_eq(m, expected, eps)); +} + +int main() { return 0; } -- cgit v1.2.3