From 5de16bf1f3004378a6afe4f8ea71b2f8a98afabf Mon Sep 17 00:00:00 2001 From: Matias Lindgren Date: Mon, 20 May 2024 14:52:01 +0200 Subject: [PATCH] test vec3 at compile time, add dot+cross tests --- test.cpp | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 159 insertions(+), 2 deletions(-) diff --git a/test.cpp b/test.cpp index 94a1bd4..719b111 100644 --- a/test.cpp +++ b/test.cpp @@ -373,6 +373,20 @@ template void test_vec3() { vec3 rhs(1, 2, 3); assert_equal(lhs.distance(rhs), 8, "vec3(-1, 2, -3) distance to vec3(1, 2, 3)"); } + { + vec3 lhs(-1, 2, -3); + vec3 rhs(1, 2, 3); + assert_equal(lhs.dot(rhs), -6, "vec3(-1, 2, -3).dot(vec3(1, 2, 3))"); + } + { + vec3 lhs(-1, 2, -3); + vec3 rhs(1, 2, 3); + assert_equal( + lhs.cross(rhs), + vec3(12, 0, -4), + "vec3(-1, 2, -3).cross(vec3(1, 2, 3))" + ); + } } template void test_vec4() { @@ -440,6 +454,11 @@ template void test_vec4() { "vec4(10, 8, 6, 4).max(vec4(2, 4, 6, 8))" ); } + { + vec4 lhs(-1, 2, -4, 3); + vec4 rhs(1, 2, 4, 3); + assert_equal(lhs.dot(rhs), -4, "vec4(-1, 2, -4, 3).dot(vec4(1, 2, 4, 3))"); + } { vec4 lhs(10, 8, 6, 4), rhs(2, 4, 6, 8); lhs.swap(rhs); @@ -557,7 +576,136 @@ template void test_vec() { (test_vec4(), ...); } -template consteval void test_vec_compile_time_impl() { +template consteval void test_vec3_compile_time_impl() { + static_assert(std::regular>); + { + constexpr vec3 v; + static_assert(v.x() == T{}, "vec3: default init x should be T{}"); + static_assert(v.y() == T{}, "vec3: default init y should be T{}"); + static_assert(v.z() == T{}, "vec3: default init z should be T{}"); + } + { + constexpr vec3 v(1, 2, 3); + static_assert(v.x() == 1, "vec3(1, 2, 3).x() should be 1"); + static_assert(v.y() == 2, "vec3(1, 2, 3).y() should be 2"); + static_assert(v.z() == 3, "vec3(1, 2, 3).z() should be 3"); + } + { + constexpr vec3 lhs, rhs; + static_assert((lhs <=> rhs) == 0, "expected vec3() == vec3()"); + } + { + constexpr vec3 lhs(-1, 0, 1); + constexpr vec3 rhs(-1, 0, 1); + static_assert((lhs <=> rhs) == 0, "expected vec3(-1, 0, 1) == vec3(-1, 0, 1)"); + } + { + constexpr vec3 lhs(-1, 0, 0); + constexpr vec3 rhs(-1, 0, 1); + static_assert((lhs <=> rhs) < 0, "expected vec3(-1, 0, 0) < vec3(-1, 0, 1)"); + static_assert((rhs <=> lhs) > 0, "expected vec3(-1, 0, 1) < vec3(-1, 0, 0)"); + } + { + constexpr vec3 lhs(-2, 0, 2); + constexpr vec3 rhs(-1, 0, 1); + static_assert((lhs <=> rhs) < 0, "expected vec3(-2, 0, 2) < vec3(-1, 0, 1)"); + static_assert((rhs <=> lhs) > 0, "expected vec3(-1, 0, 1) > vec3(-2, 0, 2)"); + } + { + constexpr vec3 lhs(1, 2, 3); + constexpr vec3 rhs(12, 10, 8); + constexpr vec3 res(13, 12, 11); + static_assert((lhs + rhs) == res, "vec3(1, 2, 3) + vec3(12, 10, 8)"); + } + { + constexpr vec3 lhs(7, 6, 5); + constexpr vec3 rhs(4, 5, 6); + constexpr vec3 res(3, 1, -1); + static_assert((lhs - rhs) == res, "vec3(7, 6, 5) - vec3(4, 5, 6)"); + } + { + constexpr vec3 lhs(1, -2, 0); + constexpr vec3 rhs(8, 6, 4); + constexpr vec3 res(8, -12, 0); + static_assert((lhs * rhs) == res, "vec3(1, -2, 0) * vec3(8, 6, 4)"); + } + { + constexpr vec3 lhs(10, 20, 0); + constexpr vec3 rhs(10, -5, 3); + constexpr vec3 res(1, -4, 0); + static_assert((lhs / rhs) == res, "vec3(10, 20, 0) / vec3(10, -5, 3)"); + } + { + constexpr vec3 lhs(10, 20, 0); + constexpr vec3 rhs(10, -5, 3); + constexpr vec3 res(10, -5, 0); + static_assert(lhs.min(rhs) == res, "vec3(10, 20, 0).min(vec3(10, -5, 3))"); + } + { + constexpr vec3 lhs(10, 20, 0); + constexpr vec3 rhs(10, -5, 3); + constexpr vec3 res(10, 20, 3); + static_assert(lhs.max(rhs) == res, "vec3(10, 20, 0).max(vec3(10, -5, 3))"); + } + { + constexpr vec3 v; + static_assert(v.distance(v) == 0, "wrong distance from center to center"); + } + { + constexpr vec3 lhs(-1, 2, -4); + constexpr vec3 center; + static_assert( + lhs.distance(center) == 7, + "wrong distance from vec3(-1, 2, -4) to center" + ); + static_assert(lhs.distance(lhs) == 0, "wrong distance from vec3(-1, 2, -4) to self"); + } + { + constexpr vec3 lhs(-1, 2, -4); + constexpr vec3 rhs(1, 2, 4); + static_assert( + lhs.distance(rhs) == 10, + "wrong distance from vec3(-1, 2, -4) to vec3(1, 2, 4)" + ); + } + { + constexpr vec3 lhs(-1, 2, -4); + constexpr vec3 rhs(1, 2, 4); + static_assert(lhs.dot(rhs) == -13, "vec3(-1, 2, -4) dot vec3(1, 2, 4) should be -13"); + } + { + constexpr vec3 lhs(-1, 2, -4); + constexpr vec3 rhs(1, 2, 4); + constexpr vec3 res(16, 0, -4); + static_assert(lhs.cross(rhs) == res, "vec3(-1, 2, -4).cross(vec3(1, 2, 4))"); + } + { + constexpr vec3 lhs(-1, 2, -4); + constexpr vec3 res(-1, 1, -1); + static_assert( + lhs.signum() == res, + "expected vec3(-1, 2, -4).signum() == vec3(-1, 1, -1)" + ); + } + { + constexpr vec3 v(-1, 2, -4); + static_assert(v.sum() == -3, "expected vec3(-1, 2, -4).sum() == -3"); + } + { + constexpr vec3 v(-1, 2, -4); + static_assert(v.prod() == 8, "expected vec3(-1, 2, -4).prod() == 8"); + } + { + constexpr vec3 v(-1, 2, -4); + static_assert(v.min() == -4, "expected vec3(-1, 2, -4).min() == -4"); + } + { + constexpr vec3 v(-1, 2, -4); + static_assert(v.max() == 2, "expected vec3(-1, 2, -4).max() == 2"); + } +} + +template consteval void test_vec4_compile_time_impl() { static_assert(std::regular>); { constexpr vec4 v; @@ -660,6 +808,14 @@ template consteval void test_vec_compile_time_impl() { "wrong distance from vec4(-1, 2, -4, 3) to vec4(1, 2, 4, 3)" ); } + { + constexpr vec4 lhs(-1, 2, -4, 3); + constexpr vec4 rhs(1, 2, 4, 3); + static_assert( + lhs.dot(rhs) == -4, + "vec4(-1, 2, -4, 3) dot vec4(1, 2, 4, 3) should be -4" + ); + } { constexpr vec4 lhs(-1, 2, -4, 3); constexpr vec4 res(-1, 1, -1, 1); @@ -687,7 +843,8 @@ template consteval void test_vec_compile_time_impl() { } template consteval void test_vec_compile_time() { - (test_vec_compile_time_impl(), ...); + (test_vec3_compile_time_impl(), ...); + (test_vec4_compile_time_impl(), ...); } template void test_vec_hash() { (test_hash(), ...); }