diff options
author | 2025-02-07 11:27:18 -0500 | |
---|---|---|
committer | 2025-02-07 11:27:18 -0500 | |
commit | 4da7be39827ea5888ef9c97b1aadf61b0d76347c (patch) | |
tree | 15d0ff8f8bcb0e871efb1b2e65c2bc8d07b17565 /mons_math/tests |
initial commit (lol)
Diffstat (limited to 'mons_math/tests')
-rw-r--r-- | mons_math/tests/mat2_ops.c | 0 | ||||
-rw-r--r-- | mons_math/tests/mat3_ops.c | 0 | ||||
-rw-r--r-- | mons_math/tests/mat4_ops.c | 0 | ||||
-rw-r--r-- | mons_math/tests/test.h | 14 | ||||
-rw-r--r-- | mons_math/tests/vec2_ops.c | 81 | ||||
-rw-r--r-- | mons_math/tests/vec3_ops.c | 90 | ||||
-rw-r--r-- | mons_math/tests/vec4_ops.c | 81 |
7 files changed, 266 insertions, 0 deletions
diff --git a/mons_math/tests/mat2_ops.c b/mons_math/tests/mat2_ops.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mons_math/tests/mat2_ops.c diff --git a/mons_math/tests/mat3_ops.c b/mons_math/tests/mat3_ops.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mons_math/tests/mat3_ops.c diff --git a/mons_math/tests/mat4_ops.c b/mons_math/tests/mat4_ops.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mons_math/tests/mat4_ops.c diff --git a/mons_math/tests/test.h b/mons_math/tests/test.h new file mode 100644 index 0000000..ce94b2e --- /dev/null +++ b/mons_math/tests/test.h @@ -0,0 +1,14 @@ +#ifndef TEST_H +#define TEST_H + +#include <stdio.h> +#include <stdlib.h> + +#define ASSERT(cond) {\ + if (!(cond)) {\ + fprintf(stderr, "Assertion Failed @ %s:%d (%s): %s", __func__, __LINE__, __FILE__, #cond);\ + exit(EXIT_FAILURE);\ + }\ +} + +#endif diff --git a/mons_math/tests/vec2_ops.c b/mons_math/tests/vec2_ops.c new file mode 100644 index 0000000..c7c6d7a --- /dev/null +++ b/mons_math/tests/vec2_ops.c @@ -0,0 +1,81 @@ +#include "mons_math/vec2.h" +#include "mons_math/vec3.h" +#include "mons_math/util.h" +#include "test.h" + +int main(void) { + mons_vec2 a = {2.0, 3.0}; + mons_vec2 b = {6.0, 5.0}; + + // Add + mons_vec2 sum = mons_vec2_add(a, b); + ASSERT(mons_vec2_equal(sum, (mons_vec2){8.0, 8.0})); + + // Add in Place + mons_vec2_add_inplace(&a, b); + ASSERT(mons_vec2_equal(a, sum)); + + // Subtract + mons_vec2 diff = mons_vec2_sub(sum, b); + ASSERT(mons_vec2_equal(diff, (mons_vec2){2.0, 3.0})); + + // Subtract in Place + mons_vec2_sub_inplace(&a, b); + ASSERT(mons_vec2_equal(diff, a)); + + // Multiply (float) + mons_vec2 product_f = mons_vec2_mul_f(a, -3.0); + ASSERT(mons_vec2_equal(product_f, (mons_vec2){-6.0, -9.0})) + + // Multiply in Place (float) + mons_vec2_mul_f_inplace(&a, 3.0); + ASSERT(mons_vec2_equal(mons_vec2_negate(product_f), a)) + + // Divide (float) + mons_vec2 quotient_f = mons_vec2_div_f(product_f, 3.0); + mons_vec2_negate_inplace("ient_f); + ASSERT(mons_vec2_equal(quotient_f, (mons_vec2){2.0, 3.0})); + + // Divide in Place (float) + mons_vec2_div_f_inplace(&a, 3.0); + ASSERT(mons_vec2_equal(quotient_f, a)); + + // Multiply (int) + mons_vec2 product_i = mons_vec2_mul_i(a, -3); + ASSERT(mons_vec2_equal(product_i, (mons_vec2){-6.0, -9.0})) + + // Multiply in Place (int) + mons_vec2_mul_i_inplace(&a, 3); + ASSERT(mons_vec2_equal(mons_vec2_negate(product_i), a)) + + // Divide (int) + mons_vec2 quotient_i = mons_vec2_div_i(product_i, 3); + mons_vec2_negate_inplace("ient_i); + ASSERT(mons_vec2_equal(quotient_i, (mons_vec2){2.0, 3.0})); + + // Divide in Place (int) + mons_vec2_div_i_inplace(&a, 3); + ASSERT(mons_vec2_equal(quotient_i, a)); + + // Get Length + float a_len = mons_vec2_len(a); + ASSERT(mons_float_approx_equal(a_len, 3.60555)); + + // Dot Product + float dot = mons_vec2_dot(a, b); + ASSERT(mons_float_approx_equal(dot, 27.0)); + + // Extend + mons_vec3 extended = mons_vec2_extend(a); + ASSERT(mons_vec3_equal(extended, (mons_vec3){a.x, a.y, 0.0})); + + // Normalize + mons_vec2 normalized = mons_vec2_normalize(a); + ASSERT(mons_float_approx_equal(mons_vec2_len(normalized), 1.0)); + + // Normalize in Place + mons_vec2_normalize_inplace(&a); + ASSERT(mons_float_approx_equal(mons_vec2_len(a), 1.0)); + + return EXIT_SUCCESS; +} diff --git a/mons_math/tests/vec3_ops.c b/mons_math/tests/vec3_ops.c new file mode 100644 index 0000000..e29b396 --- /dev/null +++ b/mons_math/tests/vec3_ops.c @@ -0,0 +1,90 @@ +#include "mons_math/vec2.h" +#include "mons_math/vec3.h" +#include "mons_math/vec4.h" +#include "mons_math/util.h" +#include "test.h" + +int main(void) { + mons_vec3 a = {2.0, 3.0, 4.0}; + mons_vec3 b = {6.0, 5.0, 4.0}; + + // Add + mons_vec3 sum = mons_vec3_add(a, b); + ASSERT(mons_vec3_equal(sum, (mons_vec3){8.0, 8.0, 8.0})); + + // Add in Place + mons_vec3_add_inplace(&a, b); + ASSERT(mons_vec3_equal(a, sum)); + + // Subtract + mons_vec3 diff = mons_vec3_sub(sum, b); + ASSERT(mons_vec3_equal(diff, (mons_vec3){2.0, 3.0, 4.0})); + + // Subtract in Place + mons_vec3_sub_inplace(&a, b); + ASSERT(mons_vec3_equal(diff, a)); + + // Multiply (float) + mons_vec3 product_f = mons_vec3_mul_f(a, -3.0); + ASSERT(mons_vec3_equal(product_f, (mons_vec3){-6.0, -9.0, -12.0})) + + // Multiply in Place (float) + mons_vec3_mul_f_inplace(&a, 3.0); + ASSERT(mons_vec3_equal(mons_vec3_negate(product_f), a)) + + // Divide (float) + mons_vec3 quotient_f = mons_vec3_div_f(product_f, 3.0); + mons_vec3_negate_inplace("ient_f); + ASSERT(mons_vec3_equal(quotient_f, (mons_vec3){2.0, 3.0, 4.0})); + + // Divide in Place (float) + mons_vec3_div_f_inplace(&a, 3.0); + ASSERT(mons_vec3_equal(quotient_f, a)); + + // Multiply (int) + mons_vec3 product_i = mons_vec3_mul_i(a, -3); + ASSERT(mons_vec3_equal(product_i, (mons_vec3){-6.0, -9.0, -12.0})) + + // Multiply in Place (int) + mons_vec3_mul_i_inplace(&a, 3); + ASSERT(mons_vec3_equal(mons_vec3_negate(product_i), a)) + + // Divide (int) + mons_vec3 quotient_i = mons_vec3_div_i(product_i, 3); + mons_vec3_negate_inplace("ient_i); + ASSERT(mons_vec3_equal(quotient_i, (mons_vec3){2.0, 3.0, 4.0})); + + // Divide in Place (int) + mons_vec3_div_i_inplace(&a, 3); + ASSERT(mons_vec3_equal(quotient_i, a)); + + // Get Length + float a_len = mons_vec3_len(a); + ASSERT(mons_float_approx_equal(a_len, 5.38516)); + + // Dot Product + float dot = mons_vec3_dot(a, b); + ASSERT(mons_float_approx_equal(dot, 43.0)); + + // Cross Product + mons_vec3 cross = mons_vec3_cross(a, b); + ASSERT(mons_vec3_equal(cross, (mons_vec3){-8.0, 16.0, -8.0})); + + // Extend + mons_vec4 extended = mons_vec3_extend(a); + ASSERT(mons_vec4_equal(extended, (mons_vec4){a.x, a.y, a.z, 0.0})); + + // Truncate + mons_vec2 truncated = mons_vec3_truncate(a); + ASSERT(mons_vec2_equal(truncated, (mons_vec2){a.x, a.y})); + + // Normalize + mons_vec3 normalized = mons_vec3_normalize(a); + ASSERT(mons_float_approx_equal(mons_vec3_len(normalized), 1.0)); + + // Normalize in Place + mons_vec3_normalize_inplace(&a); + ASSERT(mons_float_approx_equal(mons_vec3_len(a), 1.0)); + + return EXIT_SUCCESS; +} diff --git a/mons_math/tests/vec4_ops.c b/mons_math/tests/vec4_ops.c new file mode 100644 index 0000000..e7837c4 --- /dev/null +++ b/mons_math/tests/vec4_ops.c @@ -0,0 +1,81 @@ +#include "mons_math/vec3.h" +#include "mons_math/vec4.h" +#include "mons_math/util.h" +#include "test.h" + +int main(void) { + mons_vec4 a = {2.0, 3.0, 4.0, 5.0}; + mons_vec4 b = {6.0, 5.0, 4.0, 3.0}; + + // Add + mons_vec4 sum = mons_vec4_add(a, b); + ASSERT(mons_vec4_equal(sum, (mons_vec4){8.0, 8.0, 8.0, 8.0})); + + // Add in Place + mons_vec4_add_inplace(&a, b); + ASSERT(mons_vec4_equal(a, sum)); + + // Subtract + mons_vec4 diff = mons_vec4_sub(sum, b); + ASSERT(mons_vec4_equal(diff, (mons_vec4){2.0, 3.0, 4.0, 5.0})); + + // Subtract in Place + mons_vec4_sub_inplace(&a, b); + ASSERT(mons_vec4_equal(diff, a)); + + // Multiply (float) + mons_vec4 product_f = mons_vec4_mul_f(a, -3.0); + ASSERT(mons_vec4_equal(product_f, (mons_vec4){-6.0, -9.0, -12.0, -15.0})) + + // Multiply in Place (float) + mons_vec4_mul_f_inplace(&a, 3.0); + ASSERT(mons_vec4_equal(mons_vec4_negate(product_f), a)) + + // Divide (float) + mons_vec4 quotient_f = mons_vec4_div_f(product_f, 3.0); + mons_vec4_negate_inplace("ient_f); + ASSERT(mons_vec4_equal(quotient_f, (mons_vec4){2.0, 3.0, 4.0, 5.0})); + + // Divide in Place (float) + mons_vec4_div_f_inplace(&a, 3.0); + ASSERT(mons_vec4_equal(quotient_f, a)); + + // Multiply (int) + mons_vec4 product_i = mons_vec4_mul_i(a, -3); + ASSERT(mons_vec4_equal(product_i, (mons_vec4){-6.0, -9.0, -12.0, -15.0})) + + // Multiply in Place (int) + mons_vec4_mul_i_inplace(&a, 3); + ASSERT(mons_vec4_equal(mons_vec4_negate(product_i), a)) + + // Divide (int) + mons_vec4 quotient_i = mons_vec4_div_i(product_i, 3); + mons_vec4_negate_inplace("ient_i); + ASSERT(mons_vec4_equal(quotient_i, (mons_vec4){2.0, 3.0, 4.0, 5.0})); + + // Divide in Place (int) + mons_vec4_div_i_inplace(&a, 3); + ASSERT(mons_vec4_equal(quotient_i, a)); + + // Get Length + float a_len = mons_vec4_len(a); + ASSERT(mons_float_approx_equal(a_len, 7.34846)); + + // Dot Product + float dot = mons_vec4_dot(a, b); + ASSERT(mons_float_approx_equal(dot, 58.0)); + + // Truncate + mons_vec3 truncated = mons_vec4_truncate(a); + ASSERT(mons_vec3_equal(truncated, (mons_vec3){a.x, a.y, a.z})); + + // Normalize + mons_vec4 normalized = mons_vec4_normalize(a); + ASSERT(mons_float_approx_equal(mons_vec4_len(normalized), 1.0)); + + // Normalize in Place + mons_vec4_normalize_inplace(&a); + ASSERT(mons_float_approx_equal(mons_vec4_len(a), 1.0)); + + return EXIT_SUCCESS; +} |