summaryrefslogtreecommitdiff
path: root/src/tests/3d/test_3d.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_3d.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_3d.cc')
-rw-r--r--src/tests/3d/test_3d.cc30
1 files changed, 13 insertions, 17 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() {