From: Luc Grosheintz Date: Wed, 4 Jun 2025 14:58:52 +0000 (+0200) Subject: libstdc++: Add tests for layout_stride. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e3dbecb90dec76714474df867bfcc2073ad6a9b;p=thirdparty%2Fgcc.git libstdc++: Add tests for layout_stride. Implements the tests for layout_stride and for the features of the other two layouts that depend on layout_stride. libstdc++-v3/ChangeLog: * testsuite/23_containers/mdspan/layouts/class_mandate_neg.cc: Add tests for layout_stride. * testsuite/23_containers/mdspan/layouts/ctors.cc: Add test for layout_stride and the interaction with other layouts. * testsuite/23_containers/mdspan/layouts/empty.cc: Ditto. * testsuite/23_containers/mdspan/layouts/mapping.cc: Ditto. * testsuite/23_containers/mdspan/layouts/stride.cc: New test. Signed-off-by: Luc Grosheintz Reviewed-by: Tomasz KamiƄski --- diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/class_mandate_neg.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/class_mandate_neg.cc index ce721277435..70a25707cb2 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/class_mandate_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/class_mandate_neg.cc @@ -31,9 +31,16 @@ template A a_left; // { dg-error "required from" } A a_right; // { dg-error "required from" } +A a_stride; // { dg-error "required from" } B<1, std::layout_left, std::layout_left> b0; // { dg-error "required here" } +B<2, std::layout_left, std::layout_stride> b1; // { dg-error "required here" } B<3, std::layout_right, std::layout_right> b2; // { dg-error "required here" } +B<4, std::layout_right, std::layout_stride> b3; // { dg-error "required here" } + +B<5, std::layout_stride, std::layout_right> b4; // { dg-error "required here" } +B<6, std::layout_stride, std::layout_left> b5; // { dg-error "required here" } +B<7, std::layout_stride, std::layout_stride> b6; // { dg-error "required here" } // { dg-prune-output "must be representable as index_type" } diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc index 6c428c4b111..92507a8e769 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc @@ -321,6 +321,104 @@ namespace from_left_or_right } } +// ctor: mapping(layout_stride::mapping) +namespace from_stride +{ + template + constexpr auto + strides(Mapping m) + { + constexpr auto rank = Mapping::extents_type::rank(); + std::array s; + + if constexpr (rank > 0) + for(size_t i = 0; i < rank; ++i) + s[i] = m.stride(i); + return s; + } + + template + constexpr void + verify_convertible(OExtents oexts) + { + using Mapping = typename Layout::mapping; + using OMapping = std::layout_stride::mapping; + + constexpr auto other = OMapping(oexts, strides(Mapping(Extents(oexts)))); + if constexpr (std::is_same_v) + ::verify_nothrow_convertible(other); + else + ::verify_convertible(other); + } + + template + constexpr void + verify_constructible(OExtents oexts) + { + using Mapping = typename Layout::mapping; + using OMapping = std::layout_stride::mapping; + + constexpr auto other = OMapping(oexts, strides(Mapping(Extents(oexts)))); + if constexpr (std::is_same_v) + ::verify_nothrow_constructible(other); + else + ::verify_constructible(other); + } + + template + constexpr bool + test_ctor() + { + assert_not_constructible< + typename Layout::mapping>, + std::layout_stride::mapping>>(); + + assert_not_constructible< + typename Layout::mapping>, + std::layout_stride::mapping>>(); + + assert_not_constructible< + typename Layout::mapping>, + std::layout_stride::mapping>>(); + + verify_convertible>(std::extents{}); + + verify_convertible>( + std::extents{}); + + // Rank == 0 doesn't check IndexType for convertibility. + verify_convertible>( + std::extents{}); + + verify_constructible>( + std::extents{}); + + verify_constructible>( + std::extents{}); + + verify_constructible>( + std::extents{}); + + verify_constructible>( + std::extents{}); + + verify_constructible>( + std::extents{}); + + verify_constructible>( + std::extents{}); + return true; + } + + template + constexpr void + test_all() + { + test_ctor(); + static_assert(test_ctor()); + } +} + template constexpr void test_all() @@ -328,6 +426,7 @@ template default_ctor::test_all(); from_extents::test_all(); from_same_layout::test_all(); + from_stride::test_all(); } int diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc index d89fe293cdb..655b9b6d6c3 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc @@ -2,6 +2,7 @@ #include #include +#include #include constexpr size_t dyn = std::dynamic_extent; @@ -43,11 +44,27 @@ test_static_overflow() verify_all(typename Layout::mapping>{}); } +template +constexpr std::array +make_strides() +{ + std::array strides; + std::ranges::fill(strides, Int(1)); + return strides; +} + template constexpr typename Layout::mapping make_mapping(Extents exts) { - return typename Layout::mapping(exts); + using IndexType = typename Extents::index_type; + constexpr auto rank = Extents::rank(); + constexpr auto strides = make_strides(); + + if constexpr (std::same_as) + return typename Layout::mapping(exts, strides); + else + return typename Layout::mapping(exts); } template @@ -109,5 +126,6 @@ main() { static_assert(test_all()); static_assert(test_all()); + static_assert(test_all()); return 0; } diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc index 4dc0db66865..963c804a369 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc @@ -12,6 +12,7 @@ template { using M = typename Layout::mapping; static_assert(std::__mdspan::__is_extents); + static_assert(std::__mdspan::__mapping_alike); static_assert(std::copyable); static_assert(std::is_nothrow_move_constructible_v); static_assert(std::is_nothrow_move_assignable_v); @@ -30,6 +31,8 @@ template static_assert(M::is_always_unique() && M::is_unique()); static_assert(M::is_always_strided() && M::is_strided()); + if constexpr (!std::is_same_v) + static_assert(M::is_always_exhaustive() && M::is_exhaustive()); return true; } @@ -105,6 +108,39 @@ template { return exts; } }; +template<> + struct MappingFactory + { + template + static constexpr std::layout_stride::mapping + create(Extents exts) + { + if constexpr (Extents::rank() == 0) + { + auto strides = std::array{}; + return std::layout_stride::mapping(exts, strides); + } + else if constexpr (Extents::rank() == 1) + { + auto strides = std::array{2}; + return std::layout_stride::mapping(exts, strides); + } + else if constexpr (Extents::rank() == 2) + { + size_t m = exts.extent(1); + auto strides = std::array{3*m, 2}; + return std::layout_stride::mapping(exts, strides); + } + else if constexpr (Extents::rank() == 3) + { + size_t n = exts.extent(0); + size_t m = exts.extent(1); + auto strides = std::array{3*m, 2, 11*m*n}; + return std::layout_stride::mapping(exts, strides); + } + } + }; + template constexpr void test_linear_index_3d() @@ -280,6 +316,16 @@ template VERIFY(m.stride(0) == 1); } +template<> + constexpr void + test_stride_1d() + { + std::array strides{13}; + std::layout_stride::mapping m(std::extents{}, strides); + VERIFY(m.stride(0) == strides[0]); + VERIFY(m.strides() == strides); + } + template constexpr void test_stride_2d(); @@ -302,6 +348,17 @@ template<> VERIFY(m.stride(1) == 1); } +template<> + constexpr void + test_stride_2d() + { + std::array strides{13, 2}; + std::layout_stride::mapping m(std::extents{}, strides); + VERIFY(m.stride(0) == strides[0]); + VERIFY(m.stride(1) == strides[1]); + VERIFY(m.strides() == strides); + } + template constexpr void test_stride_3d(); @@ -326,6 +383,19 @@ template<> VERIFY(m.stride(2) == 1); } +template<> + constexpr void + test_stride_3d() + { + std::dextents exts(3, 5, 7); + std::array strides{11, 2, 41}; + std::layout_stride::mapping> 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 constexpr bool test_stride_all() @@ -347,7 +417,7 @@ template test_has_stride_0d() { using Mapping = typename Layout::mapping>; - constexpr bool expected = false; + constexpr bool expected = std::is_same_v; static_assert(has_stride == expected); } @@ -488,8 +558,11 @@ main() { test_all(); test_all(); + test_all(); test_has_op_eq(); + test_has_op_eq(); + test_has_op_eq(); test_has_op_eq_peculiar(); return 0; } diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride.cc new file mode 100644 index 00000000000..c8af5c61254 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride.cc @@ -0,0 +1,526 @@ +// { dg-do run { target c++23 } } +#include + +#include + +constexpr size_t dyn = std::dynamic_extent; + +template + constexpr void + test_ctor_default_stride() + { + using Extents = typename MappingStride::extents_type; + MappingStride actual; + typename std::layout_right::mapping expected; + + constexpr auto rank = MappingStride::extents_type::rank(); + if constexpr (rank > 0) + for(size_t i = 0; i < rank; ++i) + VERIFY(actual.stride(i) == expected.stride(i)); + } + +constexpr bool +test_ctor_default_stride_all() +{ + test_ctor_default_stride< + std::layout_stride::mapping>>(); + + test_ctor_default_stride< + std::layout_stride::mapping>>(); + + test_ctor_default_stride< + std::layout_stride::mapping>>(); + + test_ctor_default_stride< + std::layout_stride::mapping>>(); + + test_ctor_default_stride< + std::layout_stride::mapping>>(); + + test_ctor_default_stride< + std::layout_stride::mapping>>(); + return true; +} + +struct IntLikeA +{ + operator int() + { return 0; } +}; + +struct IntLikeB +{ + operator int() noexcept + { return 0; } +}; + +struct NotIntLike +{ }; + +template +constexpr void +test_stride_constructible() +{ + static_assert(std::is_nothrow_constructible_v< + std::layout_stride::mapping, E_arg, std::span> == Expected); + static_assert(std::is_nothrow_constructible_v< + std::layout_stride::mapping, E_arg, std::array> == Expected); + static_assert(!std::is_constructible_v, + E_arg>); +} + +constexpr void +test_stride_constructible_all() +{ + using E0 = std::extents; + using E1 = std::extents; + using E2 = std::extents; + + test_stride_constructible(); + test_stride_constructible(); + test_stride_constructible(); + test_stride_constructible(); + test_stride_constructible(); + test_stride_constructible(); + test_stride_constructible(); + test_stride_constructible(); +} + +template + constexpr void + test_ctor_shape_strides(Extents exts, Shape strides) + { + using M = std::layout_stride::mapping; + M m(exts, strides); + + if constexpr (Extents::rank() > 0) + for(size_t i = 0; i < exts.rank(); ++i) + { + VERIFY(m.stride(i) == strides[i]); + VERIFY(m.extents().extent(i) == exts.extent(i)); + } + } + +constexpr bool +test_ctor_shape_stride_all() +{ + test_ctor_shape_strides(std::extents{}, std::array{}); + test_ctor_shape_strides(std::extents{}, std::array{3}); + test_ctor_shape_strides(std::extents{}, + std::array{20, 5, 45}); + return true; +} + +template Strided, + std::array Unique, std::array Exhautive, + typename Extents::index_type Offset = 0> + struct MappingLike + { + using extents_type = Extents; + using index_type = typename Extents::index_type; + + constexpr + MappingLike(extents_type extents, + std::array strides) + : _extents(extents), _strides(strides) + { } + + static constexpr bool + is_always_strided() requires (Strided[0]) + { return Strided[1]; } + + static constexpr bool + is_always_unique() requires (Unique[0]) + { return Unique[1]; } + + static constexpr bool + is_always_exhaustive() requires (Exhautive[0]) + { return Exhautive[1]; } + + constexpr Extents + extents() const { return _extents; } + + constexpr index_type + stride(size_t i) const { return _strides[i]; } + + template + constexpr index_type + operator()(Indices... indices) const + { + if (empty()) + VERIFY(false); + + std::array ind_arr{indices...}; + index_type ret = Offset; + for(size_t i = 0; i < Extents::rank(); ++i) + ret += ind_arr[i]*_strides[i]; + return ret; + } + + private: + constexpr bool + empty() const + { + for (size_t i = 0; i < extents_type::rank(); ++i) + if (_extents.extent(i) == 0) + return true; + return false; + } + + Extents _extents; + std::array _strides; + }; + + +template +struct ExtentLike +{ + using index_type = int; + + static constexpr size_t + rank() { return Rank; } +}; + + +template +constexpr void +test_mapping_like_constructible() +{ + using M = std::layout_stride::mapping; + using E2 = std::dextents; + using E3 = std::dextents; + using E4 = ExtentLike; + + constexpr auto TT = std::array{true, true}; + constexpr auto FT = std::array{false, true}; + constexpr auto TF = std::array{true, false}; + + static_assert(std::is_constructible_v>); + static_assert(std::is_constructible_v>); + static_assert(!std::is_constructible_v>); + static_assert(!std::is_constructible_v>); + static_assert(!std::is_constructible_v>); + static_assert(!std::is_constructible_v>); + static_assert(!std::is_constructible_v>); + static_assert(!std::is_constructible_v>); + static_assert(std::is_constructible_v>); + static_assert(!std::is_constructible_v>); + static_assert(!std::is_constructible_v>); +} + +constexpr void +test_mapping_like_constructible_all() +{ + test_mapping_like_constructible>(); + test_mapping_like_constructible>(); + test_mapping_like_constructible>(); +} + +template +constexpr void +test_mapping_like_convertible() +{ + using M1 = std::layout_stride::mapping; + using M2 = std::layout_stride::mapping; + constexpr auto TT = std::array{true, true}; + + static_assert(!std::is_convertible_v, M1>); + static_assert(!std::is_convertible_v, M1>); + static_assert(!std::is_convertible_v, M2>); + + static_assert(std::is_convertible_v, M1>); + static_assert(std::is_convertible_v, M1>); + static_assert(std::is_convertible_v, M1>); + + static_assert(!std::is_convertible_v, M2>); + static_assert(!std::is_convertible_v, M2>); + static_assert(!std::is_convertible_v, M2>); +} + +constexpr void +test_mapping_like_convertible_all() +{ + test_mapping_like_convertible, + std::extents>(); + test_mapping_like_convertible, + std::extents>(); + test_mapping_like_convertible, + std::extents>(); +} + +template +constexpr void +test_ctor_stride_like(Extents exts, std::array strides) +{ + auto other_right = std::layout_right::mapping(exts); + auto other_left = std::layout_left::mapping(exts); + auto other_stride = std::layout_stride::mapping(exts, strides); + + VERIFY(std::layout_stride::mapping(other_right) == other_right); + VERIFY(std::layout_stride::mapping(other_left) == other_left); + VERIFY(std::layout_stride::mapping(other_stride) == other_stride); +} + +constexpr void +test_ctor_stride_like_all() +{ + using E1 = std::extents; + auto s1 = std::array{}; + test_ctor_stride_like(E1{}, s1); + + using E2 = std::extents; + auto s2 = std::array{2}; + test_ctor_stride_like(E2{}, s2); + + using E3 = std::extents; + auto s3 = std::array{5, 1, 15}; + test_ctor_stride_like(E3{}, s3); +} + +constexpr bool +test_ctor_strides_all() +{ + test_ctor_default_stride_all(); + test_ctor_shape_stride_all(); + test_ctor_stride_like_all(); + return true; +} + +// Check is_exhaustive. +template + constexpr void + test_is_exhaustive(Extents extents, Strides strides, bool expected) + { + std::layout_stride::mapping m(extents, strides); + VERIFY(m.is_exhaustive() == expected); + + bool always_exhaustive = extents.rank() == 0 || m.required_span_size() == 0; + VERIFY(m.is_always_exhaustive() == always_exhaustive); + } + +constexpr void +test_is_exhaustive_zero_1d() +{ + std::extents extents; + test_is_exhaustive(extents, std::array{1}, true); + test_is_exhaustive(extents, std::array{2}, true); +} + +constexpr void +test_is_exhaustive_zero_3d() +{ + std::extents extents; + + test_is_exhaustive(extents, std::array{1, 1, 1}, true); + test_is_exhaustive(extents, std::array{1, 2*21, 2*3}, true); + test_is_exhaustive(extents, std::array{7, 2*21, 1}, true); + test_is_exhaustive(extents, std::array{1, 21, 3}, true); + test_is_exhaustive(extents, std::array{7, 21, 1}, true); +} + +constexpr void +test_is_exhaustive_0d() +{ + std::extents extents; + test_is_exhaustive(extents, std::array{}, true); +} + +constexpr void +test_is_exhaustive_1d() +{ + std::extents extents; + test_is_exhaustive(extents, std::array{1}, true); + test_is_exhaustive(extents, std::array{3}, false); +} + + +constexpr void +test_is_exhaustive_3d() +{ + std::extents extents(5); + + test_is_exhaustive(extents, std::array{1, 3, 3*5}, true); + test_is_exhaustive(extents, std::array{5*7, 1, 5}, true); + test_is_exhaustive(extents, std::array{7, 3*7, 1}, true); + + test_is_exhaustive(extents, std::array{1, 3, 2*3*5}, false); + test_is_exhaustive(extents, std::array{2*5*7, 1, 2*5}, false); + test_is_exhaustive(extents, std::array{2*7, 2*3*7, 2}, false); +} + +constexpr void +test_is_exhaustive_ones() +{ + std::extents extents; + test_is_exhaustive(extents, std::array{1, 1, 1, 1}, true); + test_is_exhaustive(extents, std::array{1, 1, 1, 3}, true); + test_is_exhaustive(extents, std::array{3, 3, 1, 3}, true); + test_is_exhaustive(extents, std::array{3, 1, 1, 3}, true); +} + +constexpr bool +test_is_exhaustive_all() +{ + test_is_exhaustive_zero_1d(); + test_is_exhaustive_zero_3d(); + test_is_exhaustive_ones(); + test_is_exhaustive_0d(); + test_is_exhaustive_1d(); + test_is_exhaustive_3d(); + return true; +} + +template + using OffsetMapping = MappingLike; + +template + constexpr void + test_eq(Extents exts, + std::array left_strides, + std::array right_strides, + std::array padded_strides) + { + using DExtents = std::dextents; + + std::layout_left::mapping ml; + std::layout_right::mapping mr(exts); + + std::layout_stride::mapping msd; + std::layout_stride::mapping msl(exts, left_strides); + std::layout_stride::mapping msr(exts, right_strides); + std::layout_stride::mapping msp(exts, padded_strides); + + OffsetMapping mor{exts, right_strides}; + OffsetMapping mol{exts, left_strides}; + OffsetMapping mop{exts, padded_strides}; + OffsetMapping moo{exts, right_strides}; + + VERIFY(msd == mr); + VERIFY(msd == mor); + VERIFY(msd != msp); + VERIFY(msd != mop); + + VERIFY(msl == ml); + VERIFY(msl == mol); + VERIFY(msd != msp); + VERIFY(msl != mop); + + VERIFY(msp == mop); + VERIFY(msp != ml); + VERIFY(msp != mr); + + VERIFY(msd != moo); + } + +constexpr void +test_eq_0d() +{ + using Extents = std::extents; + Extents exts; + std::layout_left::mapping ml; + std::layout_right::mapping mr; + std::layout_stride::mapping ms; + OffsetMapping mor{exts, {}}; + OffsetMapping moo{exts, {}}; + + VERIFY(ms == ml); + VERIFY(ms == mr); + VERIFY(ms == mor); + VERIFY(ms != moo); +} + +constexpr void +test_eq_1d() +{ + using Extents = std::extents; + auto exhaustive_strides = std::array{1}; + auto padded_strides = std::array{2}; + + test_eq(Extents{}, exhaustive_strides, exhaustive_strides, padded_strides); +} + +constexpr void +test_eq_2d() +{ + using Extents = std::extents; + auto left_strides = std::array{1, 1}; + auto right_strides = std::array{2, 1}; + auto padded_strides = std::array{2, 8}; + + test_eq(Extents{}, left_strides, right_strides, padded_strides); +} + +constexpr void +test_eq_zero() +{ + using Extents = std::extents; + using Mapping = std::layout_stride::mapping; + + Extents exts; + std::array sl{1, 5}; + std::array sr{5, 1}; + + Mapping m1(exts, sl); + Mapping m2(exts, sl); + Mapping m3(exts, sr); + OffsetMapping m4(exts, sl); + + VERIFY(m1 == m2); + VERIFY(m1 != m3); + VERIFY(m1 == m4); + +} + +constexpr bool +test_eq_all() +{ + test_eq_0d(); + test_eq_1d(); + test_eq_2d(); + test_eq_zero(); + return true; +} + +template + concept has_op_eq = requires (M1 m1, M2 m2) + { + { m1 == m2 } -> std::same_as; + { m2 == m1 } -> std::same_as; + { m1 != m2 } -> std::same_as; + { m2 != m1 } -> std::same_as; + }; + +constexpr void +test_has_op_eq() +{ + using E1 = std::extents; + using E2 = std::extents; + using E3 = std::extents; + constexpr auto FT = std::array{false, true}; + + static_assert(!has_op_eq< + std::layout_stride::mapping, MappingLike>); + + static_assert(!has_op_eq< + std::layout_stride::mapping, MappingLike>); + + static_assert(!has_op_eq< + std::layout_stride::mapping, MappingLike>); +} + +int +main() +{ + test_ctor_strides_all(); + static_assert(test_ctor_strides_all()); + test_mapping_like_convertible_all(); + test_mapping_like_constructible_all(); + test_stride_constructible_all(); + test_is_exhaustive_all(); + static_assert(test_is_exhaustive_all()); + test_eq_all(); + static_assert(test_eq_all()); + test_has_op_eq(); + return 0; +}