2026-10-20 22:48:22 -04:00
|
|
|
#include "./mathprimitives.h"
|
2026-10-22 23:04:56 -04:00
|
|
|
#include <math.h>
|
2026-10-20 22:48:22 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2026-10-22 23:04:56 -04:00
|
|
|
#ifdef run_test
|
|
|
|
|
#undef run_test
|
2026-10-20 22:48:22 -04:00
|
|
|
#endif
|
|
|
|
|
|
2026-10-22 23:04:56 -04:00
|
|
|
#define STR(x) #x
|
|
|
|
|
#define run_test(test) \
|
|
|
|
|
tests_total += 1; \
|
|
|
|
|
if (test()) { \
|
|
|
|
|
tests_passed += 1; \
|
|
|
|
|
printf("[OK] %s\n", STR(test)); \
|
|
|
|
|
} else { \
|
|
|
|
|
printf("\033[91m[FAILED] %s\n\033[0m", STR(test)); \
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 22:48:22 -04:00
|
|
|
int test_vec3_equal_tol() {
|
|
|
|
|
struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
|
|
|
|
|
return vec3_eql_tol(a, b, 1e-12);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 23:04:56 -04:00
|
|
|
int test_vec3_add() {
|
|
|
|
|
|
|
|
|
|
struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
struct Vec3 expected_c = {.x = 2.0, .y = 4.0, .z = 6.0};
|
|
|
|
|
|
|
|
|
|
struct Vec3 real_c = vec3_add(a, b);
|
|
|
|
|
|
|
|
|
|
return vec3_eql_tol(expected_c, real_c, 1e-12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int test_vec3_sub() {
|
|
|
|
|
|
|
|
|
|
struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
struct Vec3 expected_c = {.x = 0.0, .y = 0.0, .z = 0.0};
|
|
|
|
|
|
|
|
|
|
struct Vec3 real_c = vec3_sub(a, b);
|
|
|
|
|
|
|
|
|
|
return vec3_eql_tol(expected_c, real_c, 1e-12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int test_vec3_dot() {
|
|
|
|
|
struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
struct Vec3 b = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
double expected_dot = 14.0;
|
|
|
|
|
|
|
|
|
|
double real_dot = vec3_dot(a, b);
|
|
|
|
|
|
|
|
|
|
return fabs(real_dot - expected_dot) < 1e-12;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 23:01:53 -04:00
|
|
|
int test_vec3_scale() {
|
|
|
|
|
struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
double b = 3.0;
|
|
|
|
|
struct Vec3 expected_c = {.x = 3.0, .y = 6.0, .z = 9.0};
|
|
|
|
|
|
|
|
|
|
struct Vec3 real_c = vec3_scale(a, b);
|
|
|
|
|
|
|
|
|
|
return vec3_eql_tol(expected_c, real_c, 1e-12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int test_vec3_cross() {
|
|
|
|
|
struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0};
|
|
|
|
|
struct Vec3 b = {.x = 3.0, .y = 1.0, .z = 0.0};
|
|
|
|
|
struct Vec3 expected_c = {.x = -3.0, .y = 9.0, .z = -5.0};
|
|
|
|
|
|
|
|
|
|
struct Vec3 real_c = vec3_cross(a, b);
|
|
|
|
|
|
|
|
|
|
return vec3_eql_tol(expected_c, real_c, 1e-12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int test_mat3_identity() {
|
|
|
|
|
struct Mat3 i = mat3_identity();
|
|
|
|
|
return fabs(i.r1.x - 1.0) < 1e-12 && fabs(i.r1.y - 0.0) < 1e-12 &&
|
|
|
|
|
fabs(i.r1.z - 0.0) < 1e-12 && fabs(i.r2.x - 0.0) < 1e-12 &&
|
|
|
|
|
fabs(i.r2.y - 1.0) < 1e-12 && fabs(i.r2.z - 0.0) < 1e-12 &&
|
|
|
|
|
fabs(i.r3.x - 0.0) < 1e-12 && fabs(i.r3.y - 0.0) < 1e-12 &&
|
|
|
|
|
fabs(i.r3.z - 1.0) < 1e-12;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 22:56:57 -04:00
|
|
|
int test_mat3_eql() {
|
|
|
|
|
struct Mat3 a = mat3_identity();
|
|
|
|
|
struct Mat3 b = mat3_identity();
|
|
|
|
|
return mat3_eql_tol(a, b, 1e+12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int test_mat3_add() {
|
|
|
|
|
struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 0}};
|
|
|
|
|
struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 2}};
|
|
|
|
|
|
|
|
|
|
struct Mat3 expected_c = {{1, 3, 3}, {0, 2, 6}, {0, 0, 2}};
|
|
|
|
|
return mat3_eql_tol(expected_c, mat3_add(a, b), 1e-12);
|
|
|
|
|
}
|
2026-10-20 22:48:22 -04:00
|
|
|
int main() {
|
|
|
|
|
int tests_total = 0;
|
|
|
|
|
int tests_passed = 0;
|
|
|
|
|
|
2026-10-22 23:04:56 -04:00
|
|
|
run_test(test_vec3_equal_tol);
|
|
|
|
|
run_test(test_vec3_add);
|
|
|
|
|
run_test(test_vec3_sub);
|
|
|
|
|
run_test(test_vec3_dot);
|
2026-10-23 23:01:53 -04:00
|
|
|
run_test(test_vec3_scale);
|
|
|
|
|
run_test(test_vec3_cross);
|
|
|
|
|
run_test(test_mat3_identity);
|
2026-10-25 22:56:57 -04:00
|
|
|
run_test(test_mat3_eql);
|
|
|
|
|
run_test(test_mat3_add);
|
2026-10-20 22:48:22 -04:00
|
|
|
|
|
|
|
|
if (tests_total == tests_passed) {
|
|
|
|
|
printf("\033[92mAll tests passed!\033[0m");
|
|
|
|
|
} else {
|
|
|
|
|
printf("\033[91m%d of %d tests failed\033[0m", tests_total - tests_passed,
|
|
|
|
|
tests_total);
|
|
|
|
|
}
|
|
|
|
|
}
|