[PATCH v3 1/4] libstdc++: Prepare mapping layout tests for padded layouts.
Jonathan Wakely
jwakely@redhat.com
Thu Sep 25 13:29:22 GMT 2025
On Tue, 23 Sept 2025 at 14:28, Luc Grosheintz <luc.grosheintz@gmail.com> wrote:
>
> Using the existing tests for padded layouts requires the following
> changes:
>
> * The padded layouts are template classes. In order to be able to use
> partially specializes templates, functions need to be converted
> structs.
"specializes" -> "specialized"
"converted structs" -> "converted to structs"
OK for trunk with those tweaks, thanks.
>
> * The layout mapping tests include a check that only applies if
> is_exhaustive is static. This commit introduces a concept to check if
> is_exhaustive is a static member function.
>
> * Fix a test to not use a hard-coded layout_left.
>
> The test empty.cc contains indentation mistakes that are fixed.
>
> libstdc++-v3/ChangeLog:
>
> * testsuite/23_containers/mdspan/layouts/empty.cc: Fix indent.
> * testsuite/23_containers/mdspan/layouts/mapping.cc
> (test_stride_1d): Fix test.
> (test_stride_2d): Rewrite using a struct.
> (test_stride_3d): Ditto.
> (has_static_is_exhaustive): New concept.
> (test_mapping_properties): Update test.
>
> Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
> ---
> .../23_containers/mdspan/layouts/empty.cc | 144 +++++++++---------
> .../23_containers/mdspan/layouts/mapping.cc | 132 ++++++++++------
> 2 files changed, 158 insertions(+), 118 deletions(-)
>
> diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc
> index 655b9b6d6c3..cbc425f6c15 100644
> --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc
> +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc
> @@ -30,96 +30,96 @@ template<typename Mapping>
> }
>
> template<typename Layout, typename Int>
> -constexpr void
> -test_static_overflow()
> -{
> - constexpr Int n1 = std::numeric_limits<Int>::max();
> - constexpr size_t n2 = std::dynamic_extent - 1;
> - constexpr size_t n = std::cmp_less(n1, n2) ? size_t(n1) : n2;
> + constexpr void
> + test_static_overflow()
> + {
> + constexpr Int n1 = std::numeric_limits<Int>::max();
> + constexpr size_t n2 = std::dynamic_extent - 1;
> + constexpr size_t n = std::cmp_less(n1, n2) ? size_t(n1) : n2;
>
> - verify_all(typename Layout::mapping<std::extents<Int, n, n, 0, n, n>>{});
> - verify_all(typename Layout::mapping<std::extents<Int, 0, n, n, n>>{});
> - verify_all(typename Layout::mapping<std::extents<Int, dyn, n, n, n>>{});
> - verify_all(typename Layout::mapping<std::extents<Int, n, n, n, 0>>{});
> - verify_all(typename Layout::mapping<std::extents<Int, n, n, n, dyn>>{});
> -}
> + verify_all(typename Layout::mapping<std::extents<Int, n, n, 0, n, n>>{});
> + verify_all(typename Layout::mapping<std::extents<Int, 0, n, n, n>>{});
> + verify_all(typename Layout::mapping<std::extents<Int, dyn, n, n, n>>{});
> + verify_all(typename Layout::mapping<std::extents<Int, n, n, n, 0>>{});
> + verify_all(typename Layout::mapping<std::extents<Int, n, n, n, dyn>>{});
> + }
>
> template<typename Int, size_t N>
> -constexpr std::array<Int, N>
> -make_strides()
> -{
> - std::array<Int, N> strides;
> - std::ranges::fill(strides, Int(1));
> - return strides;
> -}
> + constexpr std::array<Int, N>
> + make_strides()
> + {
> + std::array<Int, N> strides;
> + std::ranges::fill(strides, Int(1));
> + return strides;
> + }
>
> template<typename Layout, typename Extents>
> -constexpr typename Layout::mapping<Extents>
> -make_mapping(Extents exts)
> -{
> - using IndexType = typename Extents::index_type;
> - constexpr auto rank = Extents::rank();
> - constexpr auto strides = make_strides<IndexType, rank>();
> -
> - if constexpr (std::same_as<Layout, std::layout_stride>)
> - return typename Layout::mapping(exts, strides);
> - else
> - return typename Layout::mapping(exts);
> -}
> + constexpr typename Layout::mapping<Extents>
> + make_mapping(Extents exts)
> + {
> + using IndexType = typename Extents::index_type;
> + constexpr auto rank = Extents::rank();
> + constexpr auto strides = make_strides<IndexType, rank>();
> +
> + if constexpr (std::same_as<Layout, std::layout_stride>)
> + return typename Layout::mapping(exts, strides);
> + else
> + return typename Layout::mapping(exts);
> + }
>
> template<typename Layout, typename Int>
> -constexpr void
> -test_dynamic_overflow()
> -{
> - constexpr Int n1 = std::numeric_limits<Int>::max();
> - constexpr size_t n2 = std::dynamic_extent - 1;
> - constexpr Int n = std::cmp_less(n1, n2) ? n1 : Int(n2);
> + constexpr void
> + test_dynamic_overflow()
> + {
> + constexpr Int n1 = std::numeric_limits<Int>::max();
> + constexpr size_t n2 = std::dynamic_extent - 1;
> + constexpr Int n = std::cmp_less(n1, n2) ? n1 : Int(n2);
>
> - verify_all(make_mapping<Layout>(
> - std::extents<Int, dyn, dyn, 0, dyn, dyn>{n, n, n, n}));
> + verify_all(make_mapping<Layout>(
> + std::extents<Int, dyn, dyn, 0, dyn, dyn>{n, n, n, n}));
>
> - verify_all(make_mapping<Layout>(
> - std::extents<Int, dyn, dyn, dyn, dyn, dyn>{n, n, 0, n, n}));
> + verify_all(make_mapping<Layout>(
> + std::extents<Int, dyn, dyn, dyn, dyn, dyn>{n, n, 0, n, n}));
>
> - verify_all(make_mapping<Layout>(
> - std::extents<Int, dyn, dyn, dyn, 0>{n, n, n}));
> + verify_all(make_mapping<Layout>(
> + std::extents<Int, dyn, dyn, dyn, 0>{n, n, n}));
>
> - verify_all(make_mapping<Layout>(
> - std::extents<Int, dyn, dyn, dyn, dyn>{n, n, n, 0}));
> + verify_all(make_mapping<Layout>(
> + std::extents<Int, dyn, dyn, dyn, dyn>{n, n, n, 0}));
>
> - verify_all(make_mapping<Layout>(
> - std::extents<Int, 0, dyn, dyn, dyn>{n, n, n}));
> + verify_all(make_mapping<Layout>(
> + std::extents<Int, 0, dyn, dyn, dyn>{n, n, n}));
>
> - verify_all(make_mapping<Layout>(
> - std::extents<Int, dyn, dyn, dyn, dyn>{0, n, n, n}));
> -}
> + verify_all(make_mapping<Layout>(
> + std::extents<Int, dyn, dyn, dyn, dyn>{0, n, n, n}));
> + }
>
> template<typename Layout, typename Int>
> -constexpr void
> -test_overflow()
> -{
> - test_static_overflow<Layout, Int>();
> - test_dynamic_overflow<Layout, Int>();
> -}
> + constexpr void
> + test_overflow()
> + {
> + test_static_overflow<Layout, Int>();
> + test_dynamic_overflow<Layout, Int>();
> + }
>
> template<typename Layout>
> -constexpr bool
> -test_all()
> -{
> - test_overflow<Layout, signed char>();
> - test_overflow<Layout, short int>();
> - test_overflow<Layout, int>();
> - test_overflow<Layout, long int>();
> - test_overflow<Layout, long long int>();
> + constexpr bool
> + test_all()
> + {
> + test_overflow<Layout, signed char>();
> + test_overflow<Layout, short int>();
> + test_overflow<Layout, int>();
> + test_overflow<Layout, long int>();
> + test_overflow<Layout, long long int>();
>
> - test_overflow<Layout, unsigned char>();
> - test_overflow<Layout, unsigned short int>();
> - test_overflow<Layout, unsigned int>();
> - test_overflow<Layout, unsigned long int>();
> - test_overflow<Layout, unsigned long long int>();
> - test_overflow<Layout, size_t>();
> - return true;
> -}
> + test_overflow<Layout, unsigned char>();
> + test_overflow<Layout, unsigned short int>();
> + test_overflow<Layout, unsigned int>();
> + test_overflow<Layout, unsigned long int>();
> + test_overflow<Layout, unsigned long long int>();
> + test_overflow<Layout, size_t>();
> + return true;
> + }
>
> int
> main()
> diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc
> index 58bce514435..db15e2a48f3 100644
> --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc
> +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc
> @@ -7,6 +7,15 @@
>
> constexpr size_t dyn = std::dynamic_extent;
>
> +template<typename Mapping>
> + concept has_static_is_exhaustive = requires
> + {
> + { Mapping::is_exhaustive() } -> std::same_as<bool>;
> + };
> +
> +static_assert(has_static_is_exhaustive<std::layout_right::mapping<std::extents<int>>>);
> +static_assert(!has_static_is_exhaustive<std::layout_stride::mapping<std::extents<int>>>);
> +
> template<typename Layout, typename Extents>
> constexpr bool
> test_mapping_properties()
> @@ -32,7 +41,7 @@ template<typename Layout, typename Extents>
>
> static_assert(M::is_always_unique() && M::is_unique());
> static_assert(M::is_always_strided() && M::is_strided());
> - if constexpr (!std::is_same_v<Layout, std::layout_stride>)
> + if constexpr (has_static_is_exhaustive<M>)
> static_assert(M::is_always_exhaustive() && M::is_exhaustive());
> return true;
> }
> @@ -306,7 +315,7 @@ template<typename Layout>
> constexpr void
> test_stride_1d()
> {
> - std::layout_left::mapping<std::extents<int, 3>> m;
> + typename Layout::mapping<std::extents<int, 3>> m;
> VERIFY(m.stride(0) == 1);
> }
>
> @@ -321,73 +330,104 @@ template<>
> }
>
> template<typename Layout>
> - constexpr void
> - test_stride_2d();
> +struct TestStride2D;
>
> template<>
> - constexpr void
> - test_stride_2d<std::layout_left>()
> + struct TestStride2D<std::layout_left>
> {
> - std::layout_left::mapping<std::extents<int, 3, 5>> m;
> - VERIFY(m.stride(0) == 1);
> - VERIFY(m.stride(1) == 3);
> - }
> + static constexpr void
> + run()
> + {
> + std::layout_left::mapping<std::extents<int, 3, 5>> m;
> + VERIFY(m.stride(0) == 1);
> + VERIFY(m.stride(1) == 3);
> + }
> + };
>
> template<>
> - constexpr void
> - test_stride_2d<std::layout_right>()
> + struct TestStride2D<std::layout_right>
> {
> - std::layout_right::mapping<std::extents<int, 3, 5>> m;
> - VERIFY(m.stride(0) == 5);
> - VERIFY(m.stride(1) == 1);
> - }
> + static constexpr void
> + run()
> + {
> + std::layout_right::mapping<std::extents<int, 3, 5>> m;
> + VERIFY(m.stride(0) == 5);
> + VERIFY(m.stride(1) == 1);
> + }
> + };
>
> template<>
> + struct TestStride2D<std::layout_stride>
> + {
> + static constexpr void
> + run()
> + {
> + std::array<int, 2> strides{13, 2};
> + std::layout_stride::mapping m(std::extents<int, 3, 5>{}, strides);
> + VERIFY(m.stride(0) == strides[0]);
> + VERIFY(m.stride(1) == strides[1]);
> + VERIFY(m.strides() == strides);
> + }
> + };
> +
> +template<typename Layout>
> constexpr void
> - test_stride_2d<std::layout_stride>()
> + test_stride_2d()
> {
> - std::array<int, 2> strides{13, 2};
> - std::layout_stride::mapping m(std::extents<int, 3, 5>{}, strides);
> - VERIFY(m.stride(0) == strides[0]);
> - VERIFY(m.stride(1) == strides[1]);
> - VERIFY(m.strides() == strides);
> + TestStride2D<Layout>::run();
> }
>
> template<typename Layout>
> - constexpr void
> - test_stride_3d();
> +struct TestStride3D;
>
> template<>
> - constexpr void
> - test_stride_3d<std::layout_left>()
> + struct TestStride3D<std::layout_left>
> {
> - std::layout_left::mapping m(std::dextents<int, 3>(3, 5, 7));
> - VERIFY(m.stride(0) == 1);
> - VERIFY(m.stride(1) == 3);
> - VERIFY(m.stride(2) == 3*5);
> - }
> + static constexpr void
> + run()
> + {
> + std::layout_left::mapping m(std::dextents<int, 3>(3, 5, 7));
> + VERIFY(m.stride(0) == 1);
> + VERIFY(m.stride(1) == 3);
> + VERIFY(m.stride(2) == 3*5);
> + }
> + };
> +
>
> template<>
> - constexpr void
> - test_stride_3d<std::layout_right>()
> + struct TestStride3D<std::layout_right>
> {
> - std::layout_right::mapping m(std::dextents<int, 3>(3, 5, 7));
> - VERIFY(m.stride(0) == 5*7);
> - VERIFY(m.stride(1) == 7);
> - VERIFY(m.stride(2) == 1);
> - }
> + static constexpr void
> + run()
> + {
> + std::layout_right::mapping m(std::dextents<int, 3>(3, 5, 7));
> + VERIFY(m.stride(0) == 5*7);
> + VERIFY(m.stride(1) == 7);
> + VERIFY(m.stride(2) == 1);
> + }
> + };
>
> template<>
> + struct TestStride3D<std::layout_stride>
> + {
> + static constexpr void
> + run()
> + {
> + std::dextents<int, 3> exts(3, 5, 7);
> + std::array<int, 3> strides{11, 2, 41};
> + std::layout_stride::mapping<std::dextents<int, 3>> m(exts, strides);
> + VERIFY(m.stride(0) == strides[0]);
> + VERIFY(m.stride(1) == strides[1]);
> + VERIFY(m.stride(2) == strides[2]);
> + VERIFY(m.strides() == strides);
> + }
> + };
> +
> +template<typename Layout>
> constexpr void
> - test_stride_3d<std::layout_stride>()
> + test_stride_3d()
> {
> - std::dextents<int, 3> exts(3, 5, 7);
> - std::array<int, 3> strides{11, 2, 41};
> - std::layout_stride::mapping<std::dextents<int, 3>> m(exts, strides);
> - VERIFY(m.stride(0) == strides[0]);
> - VERIFY(m.stride(1) == strides[1]);
> - VERIFY(m.stride(2) == strides[2]);
> - VERIFY(m.strides() == strides);
> + TestStride3D<Layout>::run();
> }
>
> template<typename Layout>
> --
> 2.50.1
>
More information about the Libstdc++
mailing list