Add test macro and a few more functions

This commit is contained in:
Alex Selimov 2026-10-22 23:04:56 -04:00
parent c171a2ff7a
commit 2ff1e0791b
3 changed files with 65 additions and 12 deletions

View file

@ -12,10 +12,10 @@ SRC = $(wildcard *.c)
# -Wall to get all the possible warnings # -Wall to get all the possible warnings
# -ansi -pendantic to get portable code # -ansi -pendantic to get portable code
ifeq ($(DEBUG),yes) ifeq ($(DEBUG),yes)
CFLAGS=-W -Wall -ansi -pedantic -g -std=c23 CFLAGS=-Wall -Wpedantic -g3 -std=c23 -Wextra -Wconversion -Wshadow -fsanitize=address,undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls
LDFLAGS= -Wall -lm LDFLAGS= -Wall -lm -fsanitize=address,undefined
else else
CFLAGS=-W -Wall -ansi -pedantic -std=c23 -O3 CFLAGS=-W -Wall -pedantic -std=c23 -O3
LDFLAGS= -Wall -lm LDFLAGS= -Wall -lm
endif endif

View file

@ -18,7 +18,19 @@ static inline struct Vec3 vec3_add(struct Vec3 a, struct Vec3 b) {
return c; return c;
} }
static inline bool vec3_eql_tol(struct Vec3 a, struct Vec3 b, double tol) { static inline struct Vec3 vec3_sub(struct Vec3 a, struct Vec3 b) {
struct Vec3 c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return c;
}
static inline double vec3_dot(struct Vec3 a, struct Vec3 b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static inline int vec3_eql_tol(struct Vec3 a, struct Vec3 b, double tol) {
return fabs(a.x - b.x) < tol && fabs(a.y - b.y) < tol && return fabs(a.x - b.x) < tol && fabs(a.y - b.y) < tol &&
fabs(a.z - b.z) < tol; fabs(a.z - b.z) < tol;
} }

View file

@ -1,10 +1,21 @@
#include "./mathprimitives.h" #include "./mathprimitives.h"
#include <math.h>
#include <stdio.h> #include <stdio.h>
#ifdef assert #ifdef run_test
#undef assert #undef run_test
#endif #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() { int test_vec3_equal_tol() {
struct Vec3 a = {.x = 1.0, .y = 2.0, .z = 3.0}; 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 b = {.x = 1.0, .y = 2.0, .z = 3.0};
@ -12,16 +23,46 @@ int test_vec3_equal_tol() {
return vec3_eql_tol(a, b, 1e-12); 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 main() {
int tests_total = 0; int tests_total = 0;
int tests_passed = 0; int tests_passed = 0;
tests_total += 1; run_test(test_vec3_equal_tol);
if (test_vec3_equal_tol()) { run_test(test_vec3_add);
tests_passed += 1; run_test(test_vec3_sub);
} else { run_test(test_vec3_dot);
printf("\033[91mtest_vec3_equal_tol failed\033[0m");
};
if (tests_total == tests_passed) { if (tests_total == tests_passed) {
printf("\033[92mAll tests passed!\033[0m"); printf("\033[92mAll tests passed!\033[0m");