From eae9e03be4c9082187508a14a075b768db5f1aaa Mon Sep 17 00:00:00 2001 From: skal Date: Thu, 29 Jan 2026 23:18:25 +0100 Subject: add mini_math.h header-only vector lib --- src/tests/test_maths.cc | 149 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/tests/test_maths.cc (limited to 'src/tests') diff --git a/src/tests/test_maths.cc b/src/tests/test_maths.cc new file mode 100644 index 0000000..03b2a4c --- /dev/null +++ b/src/tests/test_maths.cc @@ -0,0 +1,149 @@ +#include "util/mini_math.h" +#include +#include +#include +#include + +// Checks if two floats are approximately equal +bool near(float a, float b, float e = 0.001f) { return std::abs(a - b) < e; } + +// Generic test runner for any vector type (vec2, vec3, vec4) +template +void test_vector_ops(int n) { + T a, b; + // Set values + for(int i=0; i 0.5f); // Out curves should exceed linear value early + assert(near(ease::in_out_quad(0.5f), 0.5f)); // Symmetric curves hit 0.5 at 0.5 +} + +// Tests spring solver +void test_spring() { + std::cout << "Testing Spring..." << std::endl; + float p = 0, v = 0; + // Simulate approx 1 sec with 0.5s smooth time + for(int i=0; i<60; ++i) spring::solve(p, v, 10.0f, 0.5f, 0.016f); + assert(p > 8.5f); + + // Test vector spring + vec3 vp(0,0,0), vv(0,0,0), vt(10,0,0); + spring::solve(vp, vv, vt, 0.5f, 0.016f * 60.0f); // 1 huge step approx + assert(vp.x > 1.0f); // Should have moved significantly +} + +int main() { + std::cout << "Testing vec2..." << std::endl; + test_vector_ops(2); + + std::cout << "Testing vec3..." << std::endl; + test_vector_ops(3); + test_vec3_special(); + + std::cout << "Testing vec4..." << std::endl; + test_vector_ops(4); + + test_quat(); + test_matrices(); + test_ease(); + test_spring(); + + std::cout << "--- ALL TESTS PASSED ---" << std::endl; + return 0; +} -- cgit v1.2.3