74 lines
2 KiB
C
74 lines
2 KiB
C
#include "./mathprimitives.h"
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
|
|
#ifdef run_test
|
|
#undef run_test
|
|
#endif
|
|
|
|
#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)); \
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
int main() {
|
|
int tests_total = 0;
|
|
int tests_passed = 0;
|
|
|
|
run_test(test_vec3_equal_tol);
|
|
run_test(test_vec3_add);
|
|
run_test(test_vec3_sub);
|
|
run_test(test_vec3_dot);
|
|
|
|
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);
|
|
}
|
|
}
|