diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-09-14 01:07:24 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-09-14 03:58:59 +0200 |
commit | 24640fdb03eb098f5c308ee26d8372a366525a8f (patch) | |
tree | 1ef7c4b874cb39ebf60faf9706ba0325a54274c1 /test | |
parent | e264037a78005abfb39edaed05c6014dc5230413 (diff) |
add more constexpr math and tests
Diffstat (limited to 'test')
-rw-r--r-- | test/app.hpp | 3 | ||||
-rw-r--r-- | test/magnum-math.cpp (renamed from test/const-math.cpp) | 7 | ||||
-rw-r--r-- | test/main.cpp | 3 | ||||
-rw-r--r-- | test/math.cpp | 105 |
4 files changed, 110 insertions, 8 deletions
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/magnum-math.cpp index 114af28f..d9dc7bb6 100644 --- a/test/const-math.cpp +++ b/test/magnum-math.cpp @@ -6,8 +6,6 @@ #include <Magnum/Math/Vector3.h> #include <Magnum/Math/Vector4.h> -#if defined CORRADE_CONSTEXPR14_ - #ifdef __GNUG__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wfloat-equal" @@ -64,7 +62,7 @@ constexpr bool compile_tests() } // namespace -void test_app::test_const_math() +void test_app::test_magnum_math() { static_assert(compile_tests()); } @@ -74,6 +72,3 @@ void test_app::test_const_math() #ifdef __GNUG__ # pragma GCC diagnostic pop #endif -#else -void floormat::test_app::test_const_math() {} -#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<typename F> +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<typename F> +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<typename F> +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<float>()); + fm_assert(test_sqrt<double>()); + static_assert(test_floor<float>()); + static_assert(test_floor<double>()); + static_assert(test_ceil<float>()); + static_assert(test_ceil<double>()); +} + +} // namespace floormat |