summaryrefslogtreecommitdiff
path: root/src/tests/3d
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-14 02:39:49 +0100
committerskal <pascal.massimino@gmail.com>2026-02-14 02:39:49 +0100
commit8dd77545b5ec2f45ce46b98dd7d94a3c4a13e290 (patch)
treea338b9c1356da64a609621155c81d8d96f7ca7fe /src/tests/3d
parentc007d7fa6ddb1936108aeca156b2a4bda425ca84 (diff)
Factor common test patterns into reusable utilitiesHEADmain
Refactor duplicated test setup/teardown code into shared fixtures: - test_math_helpers.h: Float comparison (test_near, test_near_vec3) - AudioTestFixture: RAII wrapper for AudioEngine lifecycle - EffectTestFixture: Combined WebGPU + AudioEngine + MainSequence Migrated 9 test files (3 math, 6 audio) to use fixtures. Net reduction: 54 LOC (178 insertions, 232 deletions). All 34 tests passing. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/tests/3d')
-rw-r--r--src/tests/3d/test_3d.cc30
-rw-r--r--src/tests/3d/test_physics.cc32
2 files changed, 27 insertions, 35 deletions
diff --git a/src/tests/3d/test_3d.cc b/src/tests/3d/test_3d.cc
index e0fb2e0..7132b33 100644
--- a/src/tests/3d/test_3d.cc
+++ b/src/tests/3d/test_3d.cc
@@ -4,14 +4,10 @@
#include "3d/camera.h"
#include "3d/object.h"
#include "3d/scene.h"
+#include "../common/test_math_helpers.h"
#include <cassert>
-#include <cmath>
#include <iostream>
-bool near(float a, float b, float e = 0.001f) {
- return std::abs(a - b) < e;
-}
-
void test_camera() {
std::cout << "Testing Camera..." << std::endl;
Camera cam;
@@ -20,7 +16,7 @@ void test_camera() {
mat4 view = cam.get_view_matrix();
// Camera at (0,0,10) looking at (0,0,0). World (0,0,0) -> View (0,0,-10)
- assert(near(view.m[14], -10.0f));
+ assert(test_near(view.m[14], -10.0f, 0.001f));
// Test Camera::set_look_at
cam.set_look_at({5, 0, 0}, {0, 0, 0},
@@ -31,9 +27,9 @@ void test_camera() {
// -dot(s, eye), -dot(u, eye), dot(f, eye) s = (0,0,-1), u = (0,1,0), f =
// (-1,0,0) m[12] = -dot({0,0,-1}, {5,0,0}) = 0 m[13] = -dot({0,1,0}, {5,0,0})
// = 0 m[14] = dot({-1,0,0}, {5,0,0}) = -5
- assert(near(view_shifted.m[12], 0.0f));
- assert(near(view_shifted.m[13], 0.0f));
- assert(near(view_shifted.m[14], -5.0f));
+ assert(test_near(view_shifted.m[12], 0.0f, 0.001f));
+ assert(test_near(view_shifted.m[13], 0.0f, 0.001f));
+ assert(test_near(view_shifted.m[14], -5.0f, 0.001f));
// Test Camera::get_projection_matrix with varied parameters
// Change FOV and aspect ratio
@@ -54,7 +50,7 @@ void test_object_transform() {
// Model matrix should translate by (10,0,0)
mat4 m = obj.get_model_matrix();
- assert(near(m.m[12], 10.0f));
+ assert(test_near(m.m[12], 10.0f, 0.001f));
// Test composed transformations (translate then rotate)
obj.position = vec3(5, 0, 0);
@@ -65,8 +61,8 @@ void test_object_transform() {
// Translation moves it by (5,0,0). Final world pos: (5,0,-1).
vec4 p_comp(1, 0, 0, 1);
vec4 res_comp = m * p_comp;
- assert(near(res_comp.x, 5.0f));
- assert(near(res_comp.z, -1.0f));
+ assert(test_near(res_comp.x, 5.0f, 0.001f));
+ assert(test_near(res_comp.z, -1.0f, 0.001f));
// Test Object3D::inv_model calculation
// Model matrix for translation (5,0,0) is just translation
@@ -80,8 +76,8 @@ void test_object_transform() {
vec4 original_space_t =
inv_model_t *
vec4(translated_point.x, translated_point.y, translated_point.z, 1.0);
- assert(near(original_space_t.x, 0.0f) && near(original_space_t.y, 0.0f) &&
- near(original_space_t.z, 0.0f));
+ assert(test_near(original_space_t.x, 0.0f, 0.001f) && test_near(original_space_t.y, 0.0f, 0.001f) &&
+ test_near(original_space_t.z, 0.0f, 0.001f));
// Model matrix with rotation (90 deg Y) and translation (5,0,0)
obj.position = vec3(5, 0, 0);
@@ -92,11 +88,11 @@ void test_object_transform() {
// Translates to (5,0,-1)
vec4 p_trs(1, 0, 0, 1);
vec4 transformed_p = model_trs * p_trs;
- assert(near(transformed_p.x, 5.0f) && near(transformed_p.z, -1.0f));
+ assert(test_near(transformed_p.x, 5.0f, 0.001f) && test_near(transformed_p.z, -1.0f, 0.001f));
// Apply inverse to transformed point to get back original point
vec4 original_space_trs = inv_model_trs * transformed_p;
- assert(near(original_space_trs.x, 1.0f) && near(original_space_trs.y, 0.0f) &&
- near(original_space_trs.z, 0.0f));
+ assert(test_near(original_space_trs.x, 1.0f, 0.001f) && test_near(original_space_trs.y, 0.0f, 0.001f) &&
+ test_near(original_space_trs.z, 0.0f, 0.001f));
}
void test_scene() {
diff --git a/src/tests/3d/test_physics.cc b/src/tests/3d/test_physics.cc
index df21e70..c1c5c32 100644
--- a/src/tests/3d/test_physics.cc
+++ b/src/tests/3d/test_physics.cc
@@ -4,44 +4,40 @@
#include "3d/bvh.h"
#include "3d/physics.h"
#include "3d/sdf_cpu.h"
+#include "../common/test_math_helpers.h"
#include <cassert>
-#include <cmath>
#include <iostream>
-bool near(float a, float b, float e = 0.001f) {
- return std::abs(a - b) < e;
-}
-
void test_sdf_sphere() {
std::cout << "Testing sdSphere..." << std::endl;
float r = 1.0f;
- assert(near(sdf::sdSphere({0, 0, 0}, r), -1.0f));
- assert(near(sdf::sdSphere({1, 0, 0}, r), 0.0f));
- assert(near(sdf::sdSphere({2, 0, 0}, r), 1.0f));
+ assert(test_near(sdf::sdSphere({0, 0, 0}, r), -1.0f, 0.001f));
+ assert(test_near(sdf::sdSphere({1, 0, 0}, r), 0.0f, 0.001f));
+ assert(test_near(sdf::sdSphere({2, 0, 0}, r), 1.0f, 0.001f));
}
void test_sdf_box() {
std::cout << "Testing sdBox..." << std::endl;
vec3 b(1, 1, 1);
- assert(near(sdf::sdBox({0, 0, 0}, b), -1.0f));
- assert(near(sdf::sdBox({1, 1, 1}, b), 0.0f));
- assert(near(sdf::sdBox({2, 0, 0}, b), 1.0f));
+ assert(test_near(sdf::sdBox({0, 0, 0}, b), -1.0f, 0.001f));
+ assert(test_near(sdf::sdBox({1, 1, 1}, b), 0.0f, 0.001f));
+ assert(test_near(sdf::sdBox({2, 0, 0}, b), 1.0f, 0.001f));
}
void test_sdf_torus() {
std::cout << "Testing sdTorus..." << std::endl;
vec2 t(1.0f, 0.2f);
// Point on the ring: length(p.xz) = 1.0, p.y = 0
- assert(near(sdf::sdTorus({1, 0, 0}, t), -0.2f));
- assert(near(sdf::sdTorus({1.2f, 0, 0}, t), 0.0f));
+ assert(test_near(sdf::sdTorus({1, 0, 0}, t), -0.2f, 0.001f));
+ assert(test_near(sdf::sdTorus({1.2f, 0, 0}, t), 0.0f, 0.001f));
}
void test_sdf_plane() {
std::cout << "Testing sdPlane..." << std::endl;
vec3 n(0, 1, 0);
float h = 1.0f; // Plane is at y = -1 (dot(p,n) + 1 = 0 => y = -1)
- assert(near(sdf::sdPlane({0, 0, 0}, n, h), 1.0f));
- assert(near(sdf::sdPlane({0, -1, 0}, n, h), 0.0f));
+ assert(test_near(sdf::sdPlane({0, 0, 0}, n, h), 1.0f, 0.001f));
+ assert(test_near(sdf::sdPlane({0, -1, 0}, n, h), 0.0f, 0.001f));
}
void test_calc_normal() {
@@ -50,18 +46,18 @@ void test_calc_normal() {
// Sphere normal at (1,0,0) should be (1,0,0)
auto sphere_sdf = [](vec3 p) { return sdf::sdSphere(p, 1.0f); };
vec3 n = sdf::calc_normal({1, 0, 0}, sphere_sdf);
- assert(near(n.x, 1.0f) && near(n.y, 0.0f) && near(n.z, 0.0f));
+ assert(test_near(n.x, 1.0f, 0.001f) && test_near(n.y, 0.0f, 0.001f) && test_near(n.z, 0.0f, 0.001f));
// Box normal at side
auto box_sdf = [](vec3 p) { return sdf::sdBox(p, {1, 1, 1}); };
n = sdf::calc_normal({1, 0, 0}, box_sdf);
- assert(near(n.x, 1.0f) && near(n.y, 0.0f) && near(n.z, 0.0f));
+ assert(test_near(n.x, 1.0f, 0.001f) && test_near(n.y, 0.0f, 0.001f) && test_near(n.z, 0.0f, 0.001f));
// Plane normal should be n
vec3 plane_n(0, 1, 0);
auto plane_sdf = [plane_n](vec3 p) { return sdf::sdPlane(p, plane_n, 1.0f); };
n = sdf::calc_normal({0, 0, 0}, plane_sdf);
- assert(near(n.x, plane_n.x) && near(n.y, plane_n.y) && near(n.z, plane_n.z));
+ assert(test_near(n.x, plane_n.x, 0.001f) && test_near(n.y, plane_n.y, 0.001f) && test_near(n.z, plane_n.z, 0.001f));
}
void test_bvh() {