summaryrefslogtreecommitdiff
path: root/src/tests/3d/test_physics.cc
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/test_physics.cc
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/test_physics.cc')
-rw-r--r--src/tests/3d/test_physics.cc32
1 files changed, 14 insertions, 18 deletions
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() {