diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-02 17:41:03 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-02 17:41:03 +0100 |
| commit | dd8203877476993541a2c0e743a5d636fa6ea275 (patch) | |
| tree | 6d8fa0fcf9b97bff31cd5627fd9624ae4e2516d5 /src/tests | |
| parent | 6847a7e1b54c9f76feef0c4110a897600983416e (diff) | |
feat(test): Add comprehensive math and shader composer tests
- Implemented test_shader_composer.cc to verify WGSL snippet assembly.
- Expanded test_maths.cc with rigorous matrix inversion and transposition checks.
- Verified that A * inv(A) equals Identity for various TRS combinations.
- Updated CMakeLists.txt to include the new test targets.
Diffstat (limited to 'src/tests')
| -rw-r--r-- | src/tests/test_maths.cc | 63 | ||||
| -rw-r--r-- | src/tests/test_shader_composer.cc | 38 |
2 files changed, 101 insertions, 0 deletions
diff --git a/src/tests/test_maths.cc b/src/tests/test_maths.cc index 64bbb45..8eb7ec6 100644 --- a/src/tests/test_maths.cc +++ b/src/tests/test_maths.cc @@ -162,6 +162,68 @@ void test_spring() { assert(vp.x > 1.0f); // Should have moved significantly } +// Verifies that a matrix is approximately the identity matrix +void check_identity(const mat4& m) { + for (int i = 0; i < 16; ++i) { + float expected = (i % 5 == 0) ? 1.0f : 0.0f; + if (!near(m.m[i], expected, 0.005f)) { + std::cerr << "Matrix not Identity at index " << i << ": got " << m.m[i] + << " expected " << expected << std::endl; + assert(false); + } + } +} + +// Tests matrix inversion and transposition correctness +void test_matrix_inversion() { + std::cout << "Testing Matrix Inversion..." << std::endl; + + // 1. Identity + mat4 id; + check_identity(id.inverse()); + + // 2. Translation + mat4 t = mat4::translate({10.0f, -5.0f, 2.0f}); + mat4 t_inv = t.inverse(); + check_identity(t * t_inv); + check_identity(t_inv * t); + + // 3. Scale (non-uniform) + mat4 s = mat4::scale({2.0f, 0.5f, 4.0f}); + mat4 s_inv = s.inverse(); + check_identity(s * s_inv); + + // 4. Rotation + mat4 r = mat4::rotate({1.0f, 2.0f, 3.0f}, 0.785f); // 45 deg around complex axis + mat4 r_inv = r.inverse(); + check_identity(r * r_inv); + + // 5. Complex Transform (TRS) + mat4 trs = t * (r * s); + mat4 trs_inv = trs.inverse(); + check_identity(trs * trs_inv); + check_identity(trs_inv * trs); + + // 6. Transposition + std::cout << "Testing Matrix Transposition..." << std::endl; + mat4 trs_t = mat4::transpose(trs); + mat4 trs_tt = mat4::transpose(trs_t); + for (int i = 0; i < 16; ++i) { + assert(near(trs.m[i], trs_tt.m[i])); + } + + // 7. Manual "stress" matrix (some small values, some large) + mat4 stress; + stress.m[0] = 1.0f; + stress.m[5] = 2.0f; + stress.m[10] = 3.0f; + stress.m[15] = 4.0f; + stress.m[12] = 100.0f; + stress.m[1] = 0.5f; + mat4 stress_inv = stress.inverse(); + check_identity(stress * stress_inv); +} + int main() { std::cout << "Testing vec2..." << std::endl; test_vector_ops<vec2>(2); @@ -175,6 +237,7 @@ int main() { test_quat(); test_matrices(); + test_matrix_inversion(); // New tests test_ease(); test_spring(); diff --git a/src/tests/test_shader_composer.cc b/src/tests/test_shader_composer.cc new file mode 100644 index 0000000..4a5cb8b --- /dev/null +++ b/src/tests/test_shader_composer.cc @@ -0,0 +1,38 @@ +// This file is part of the 64k demo project. +// It tests the ShaderComposer utility. + +#include "gpu/effects/shader_composer.h" +#include <cassert> +#include <iostream> +#include <string> + +void test_composition() { + std::cout << "Testing Shader Composition..." << std::endl; + auto& sc = ShaderComposer::Get(); + + sc.RegisterSnippet("math", "fn add(a: f32, b: f32) -> f32 { return a + b; }"); + sc.RegisterSnippet("util", "fn square(a: f32) -> f32 { return a * a; }"); + + std::string main_code = "fn main() { let x = add(1.0, square(2.0)); }"; + std::string result = sc.Compose({"math", "util"}, main_code); + + // Verify order and presence + assert(result.find("Snippet: math") != std::string::npos); + assert(result.find("Snippet: util") != std::string::npos); + assert(result.find("Main Code") != std::string::npos); + + size_t pos_math = result.find("Snippet: math"); + size_t pos_util = result.find("Snippet: util"); + size_t pos_main = result.find("Main Code"); + + assert(pos_math < pos_util); + assert(pos_util < pos_main); + + std::cout << "Composition logic verified." << std::endl; +} + +int main() { + test_composition(); + std::cout << "--- ALL SHADER COMPOSER TESTS PASSED ---" << std::endl; + return 0; +} |
