diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-08-10 18:36:41 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-08-11 11:07:56 +0200 |
commit | 4a3dd8b6fe07961f5f9b4b839f718019657f8dd2 (patch) | |
tree | 480a10d268168c20542459b8d72f82a4e71c6baf | |
parent | dd9b8bd02f301291aa47cf4b52f19c6f5805ac1f (diff) |
compat, test: improve {static_,}array_size
-rw-r--r-- | compat/array-size.hpp | 7 | ||||
-rw-r--r-- | test/app.cpp | 1 | ||||
-rw-r--r-- | test/app.hpp | 1 | ||||
-rw-r--r-- | test/util.cpp | 40 |
4 files changed, 47 insertions, 2 deletions
diff --git a/compat/array-size.hpp b/compat/array-size.hpp index 15eb66ab..4d9b6ce6 100644 --- a/compat/array-size.hpp +++ b/compat/array-size.hpp @@ -5,15 +5,18 @@ namespace floormat::detail { template<typename T> struct array_size_; template<typename T, size_t N> struct array_size_<T(&)[N]> : std::integral_constant<size_t, N> {}; +template<typename T, size_t N> struct array_size_<T(*)[N]> : std::integral_constant<size_t, N> {}; template<typename T, size_t N> struct array_size_<T[N]> : std::integral_constant<size_t, N> {}; template<typename T, size_t N> struct array_size_<std::array<T, N>> : std::integral_constant<size_t, N> {}; template<typename T, size_t N> struct array_size_<StaticArray<N, T>> : std::integral_constant<size_t, N> {}; +template<typename C, typename T> struct array_size_<T C::*> : std::integral_constant<size_t, array_size_<std::remove_cvref_t<T>>::value> {}; + } // namespace floormat::detail namespace floormat { -template<typename T> constexpr inline size_t static_array_size = detail::array_size_<T>::value; -template<typename T> constexpr inline size_t array_size(const T&) noexcept { return detail::array_size_<T>::value; } +template<typename T> constexpr inline size_t static_array_size = detail::array_size_<std::remove_cvref_t<T>>::value; +template<typename T> constexpr inline size_t array_size(const T&) noexcept { return detail::array_size_<std::remove_cvref_t<T>>::value; } } // namespace floormat diff --git a/test/app.cpp b/test/app.cpp index 6ace3ca5..c3ffa8fd 100644 --- a/test/app.cpp +++ b/test/app.cpp @@ -58,6 +58,7 @@ int App::exec() FM_TEST(test_local), // fast FM_TEST(test_magnum_math), + FM_TEST(test_util), FM_TEST(test_math), FM_TEST(test_astar_pool), FM_TEST(test_coords), diff --git a/test/app.hpp b/test/app.hpp index b1995e77..0e4d7a3a 100644 --- a/test/app.hpp +++ b/test/app.hpp @@ -41,6 +41,7 @@ void test_rtree(); void test_save(); void test_saves(); void test_script(); +void test_util(); void test_wall_atlas(); void test_wall_atlas2(); diff --git a/test/util.cpp b/test/util.cpp new file mode 100644 index 00000000..48332621 --- /dev/null +++ b/test/util.cpp @@ -0,0 +1,40 @@ +#include "app.hpp" +#include "compat/array-size.hpp" + +namespace floormat::Test { + +namespace { + +struct Foo +{ + static constexpr std::array<int, 11> Array_1 = {}; + static constexpr const void* Array_2[22] = {}; + + std::array<int, 33> array_3; +}; + +constexpr bool test_array_size() +{ + fm_assert(static_array_size<decltype(Foo::Array_1)> == 11); + fm_assert(array_size(Foo::Array_1) == 11); + + fm_assert(static_array_size<decltype(Foo::Array_2)> == 22); + fm_assert(array_size(&Foo::Array_2) == 22); + + fm_assert(static_array_size<decltype(Foo{}.array_3)> == 33); + fm_assert(array_size(Foo{}.array_3) == 33); + fm_assert(array_size(&Foo::array_3) == 33); + + fm_assert(static_array_size<const int(&)[44]> == 44); + + return true; +} + +} // namespace + +void test_util() +{ + static_assert(test_array_size()); +} + +} // namespace floormat::Test |