From 24640fdb03eb098f5c308ee26d8372a366525a8f Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 14 Sep 2023 01:07:24 +0200 Subject: add more constexpr math and tests --- test/app.hpp | 3 +- test/const-math.cpp | 79 -------------------------------------- test/magnum-math.cpp | 74 ++++++++++++++++++++++++++++++++++++ test/main.cpp | 3 +- test/math.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 81 deletions(-) delete mode 100644 test/const-math.cpp create mode 100644 test/magnum-math.cpp create mode 100644 test/math.cpp (limited to 'test') diff --git a/test/app.hpp b/test/app.hpp index f3e57da2..db1d11a9 100644 --- a/test/app.hpp +++ b/test/app.hpp @@ -33,7 +33,8 @@ struct test_app final : private FM_APPLICATION static void test_json(); static void test_tile_iter(); - static void test_const_math(); + static void test_magnum_math(); + static void test_math(); static void test_serializer_1(); static void test_serializer_2(); static void test_entity(); diff --git a/test/const-math.cpp b/test/const-math.cpp deleted file mode 100644 index 114af28f..00000000 --- a/test/const-math.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "app.hpp" -#include "compat/assert.hpp" -#include -#include -#include -#include -#include - -#if defined CORRADE_CONSTEXPR14_ - -#ifdef __GNUG__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wfloat-equal" -#endif - -namespace floormat { - -using Magnum::Math::Vector; - -namespace { - -template -constexpr void test_float2() -{ - const vec a{(T)1, (T)2}, b{(T)2, (T)3}; - - fm_assert(a[0] == (T)1 && a[1] == (T)2); - fm_assert(a + b == vec{(T)3, (T)5}); - fm_assert(a - b == vec{(T)-1, (T)-1}); - fm_assert(a * b == vec{(T)2, (T)6}); - fm_assert(b / a == vec{(T)2, (T)1.5}); - fm_assert(b.product() == (T)6); - fm_assert(b.sum() == (T)5); -} - -template -constexpr void test_int() -{ - using I = typename ivec::Type; - constexpr auto vec = [](auto x, auto y) { return ivec{(I)x, (I)y}; }; - const auto a = vec(3, 5), b = vec(11, 7); - - fm_assert(a[0] == 3 && a[1] == 5); - fm_assert(a + b == vec(14,12)); - fm_assert(b - a == vec(8, 2)); - fm_assert(b % a == vec(2, 2)); - fm_assert(b / a == vec(3, 1)); - fm_assert(a.product() == 15); - fm_assert(a.sum() == 8); -} - -constexpr bool compile_tests() -{ - test_float2, float>(); - test_float2, double>(); - test_float2(); - - test_int>(); - test_int>(); - test_int>(); - - return true; -} - -} // namespace - -void test_app::test_const_math() -{ - static_assert(compile_tests()); -} - -} // namespace floormat - -#ifdef __GNUG__ -# pragma GCC diagnostic pop -#endif -#else -void floormat::test_app::test_const_math() {} -#endif diff --git a/test/magnum-math.cpp b/test/magnum-math.cpp new file mode 100644 index 00000000..d9dc7bb6 --- /dev/null +++ b/test/magnum-math.cpp @@ -0,0 +1,74 @@ +#include "app.hpp" +#include "compat/assert.hpp" +#include +#include +#include +#include +#include + +#ifdef __GNUG__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + +namespace floormat { + +using Magnum::Math::Vector; + +namespace { + +template +constexpr void test_float2() +{ + const vec a{(T)1, (T)2}, b{(T)2, (T)3}; + + fm_assert(a[0] == (T)1 && a[1] == (T)2); + fm_assert(a + b == vec{(T)3, (T)5}); + fm_assert(a - b == vec{(T)-1, (T)-1}); + fm_assert(a * b == vec{(T)2, (T)6}); + fm_assert(b / a == vec{(T)2, (T)1.5}); + fm_assert(b.product() == (T)6); + fm_assert(b.sum() == (T)5); +} + +template +constexpr void test_int() +{ + using I = typename ivec::Type; + constexpr auto vec = [](auto x, auto y) { return ivec{(I)x, (I)y}; }; + const auto a = vec(3, 5), b = vec(11, 7); + + fm_assert(a[0] == 3 && a[1] == 5); + fm_assert(a + b == vec(14,12)); + fm_assert(b - a == vec(8, 2)); + fm_assert(b % a == vec(2, 2)); + fm_assert(b / a == vec(3, 1)); + fm_assert(a.product() == 15); + fm_assert(a.sum() == 8); +} + +constexpr bool compile_tests() +{ + test_float2, float>(); + test_float2, double>(); + test_float2(); + + test_int>(); + test_int>(); + test_int>(); + + return true; +} + +} // namespace + +void test_app::test_magnum_math() +{ + static_assert(compile_tests()); +} + +} // namespace floormat + +#ifdef __GNUG__ +# pragma GCC diagnostic pop +#endif diff --git a/test/main.cpp b/test/main.cpp index fe07ce79..131e8452 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -23,13 +23,14 @@ int test_app::exec() { test_json(); test_tile_iter(); - test_const_math(); + test_magnum_math(); test_entity(); test_loader(); test_bitmask(); test_serializer_1(); test_serializer_2(); test_path_search(); + test_math(); zzz_test_misc(); return 0; diff --git a/test/math.cpp b/test/math.cpp new file mode 100644 index 00000000..d70a64fb --- /dev/null +++ b/test/math.cpp @@ -0,0 +1,105 @@ +#include "app.hpp" +#include "compat/math.hpp" + +namespace floormat { + +namespace { + +constexpr bool test_double_sqrt() +{ + using F = double; + constexpr auto eps = F(1e-11); + + static_assert(math::abs(math::sqrt((F)3) - (F)1.73205080757) < eps); + return true; +} + +template +bool test_sqrt() +{ + constexpr auto eps = F(1e-11); + constexpr auto test = [](double x) + { + auto x_ = (F)x; + auto y1 = math::sqrt(x_); + auto y2 = std::sqrt(x_); + return math::abs(y1 - y2) < eps; + }; + + static_assert(math::abs(math::sqrt((F)0) - (F)0) < eps); + static_assert(math::abs(math::sqrt((F)0.5) - (F)0.70710678118) < eps); + static_assert(math::abs(math::sqrt((F)1e-8) - (F)0.0001) < eps); + static_assert(math::abs(math::sqrt((F)2) - (F)1.41421356237) < eps); + static_assert(math::abs(math::sqrt((F)3) - (F)1.73205080757) < (F)1e-6); + + static_assert(math::sqrt((F)0) == (F)0); + static_assert(math::sqrt((F)1) == (F)1); + static_assert(math::sqrt((F)4) == (F)2); + static_assert(math::sqrt((F)9) == (F)3); + static_assert(math::sqrt((F)36) == (F)6); + + fm_assert(test(0)); + fm_assert(test(0.5)); + fm_assert(test(1.5)); + fm_assert(test(2)); + fm_assert(test(3)); + fm_assert(test(0)); + fm_assert(test(42)); + fm_assert(test(41.5)); + fm_assert(test(1e-8)); + fm_assert(test(1e8)); + fm_assert(test(1.23456789)); + fm_assert(test(1e10 + 1.23456789)); + fm_assert(test(531610)); + fm_assert(test(1e10)); + fm_assert(test(1 << 20)); + + return true; +} + +template +constexpr bool test_floor() +{ + fm_assert(math::floor((F)-1.5) == -2); + fm_assert(math::floor((F)0) == 0); + fm_assert(math::floor((F)1) == 1); + fm_assert(math::floor((F)-1) == -1); + fm_assert(math::floor((F)-2) == -2); + fm_assert(math::floor((F)1.0000001) == 1); + fm_assert(math::floor((F)-1.000001) == -2); + fm_assert(math::floor((F)1e-8) == 0); + fm_assert(math::floor((F)-1e-8) == -1); + + return true; +} + +template +constexpr bool test_ceil() +{ + fm_assert(math::ceil((F)-1.5) == -1); + fm_assert(math::ceil((F)0) == 0); + fm_assert(math::ceil((F)1) == 1); + fm_assert(math::ceil((F)-1) == -1); + fm_assert(math::ceil((F)-2) == -2); + fm_assert(math::ceil((F)1.0000001) == 2); + fm_assert(math::ceil((F)-1.000001) == -1); + fm_assert(math::ceil((F)1e-8) == 1); + fm_assert(math::ceil((F)-1e-8) == 0); + + return true; +} + +} // namespace + +void test_app::test_math() +{ + static_assert(test_double_sqrt()); + fm_assert(test_sqrt()); + fm_assert(test_sqrt()); + static_assert(test_floor()); + static_assert(test_floor()); + static_assert(test_ceil()); + static_assert(test_ceil()); +} + +} // namespace floormat -- cgit v1.2.3