xref: /llvm-project/libcxx/test/std/containers/views/mdspan/layout_stride/index_operator.pass.cpp (revision 5e19fd172063c8957a35c7fa3596620f79ebba97)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 
11 // <mdspan>
12 
13 // Test default iteration:
14 //
15 // template<class... Indices>
16 //   constexpr index_type operator()(Indices...) const noexcept;
17 //
18 // Constraints:
19 //   * sizeof...(Indices) == extents_type::rank() is true,
20 //   * (is_convertible_v<Indices, index_type> && ...) is true, and
21 //   * (is_nothrow_constructible_v<index_type, Indices> && ...) is true.
22 //
23 // Preconditions:
24 //   * extents_type::index-cast(i) is a multidimensional index in extents_.
25 
26 #include <mdspan>
27 #include <array>
28 #include <cassert>
29 #include <cstdint>
30 #include <span> // dynamic_extent
31 #include <type_traits>
32 
33 #include "test_macros.h"
34 
35 #include "../ConvertibleToIntegral.h"
36 
37 template <class Mapping, class... Indices>
38 concept operator_constraints = requires(Mapping m, Indices... idxs) {
39   { std::is_same_v<decltype(m(idxs...)), typename Mapping::index_type> };
40 };
41 
42 template <class Mapping, class... Indices>
43   requires(operator_constraints<Mapping, Indices...>)
44 constexpr bool check_operator_constraints(Mapping m, Indices... idxs) {
45   (void)m(idxs...);
46   return true;
47 }
48 
49 template <class Mapping, class... Indices>
50 constexpr bool check_operator_constraints(Mapping, Indices...) {
51   return false;
52 }
53 
54 template <class M, class... Args>
55 constexpr void iterate_stride(M m, const std::array<int, M::extents_type::rank()>& strides, Args... args) {
56   constexpr int r = static_cast<int>(M::extents_type::rank()) - 1 - static_cast<int>(sizeof...(Args));
57   if constexpr (-1 == r) {
58     ASSERT_NOEXCEPT(m(args...));
59     std::size_t expected_val = static_cast<std::size_t>([&]<std::size_t... Pos>(std::index_sequence<Pos...>) {
60       return ((args * strides[Pos]) + ... + 0);
61     }(std::make_index_sequence<M::extents_type::rank()>()));
62     assert(expected_val == static_cast<std::size_t>(m(args...)));
63   } else {
64     for (typename M::index_type i = 0; i < m.extents().extent(r); i++) {
65       iterate_stride(m, strides, i, args...);
66     }
67   }
68 }
69 
70 template <class E, class... Args>
71 constexpr void test_iteration(std::array<int, E::rank()> strides, Args... args) {
72   using M = std::layout_stride::mapping<E>;
73   M m(E(args...), strides);
74 
75   iterate_stride(m, strides);
76 }
77 
78 constexpr bool test() {
79   constexpr std::size_t D = std::dynamic_extent;
80   test_iteration<std::extents<int>>(std::array<int, 0>{});
81   test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{2}, 1);
82   test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{3}, 7);
83   test_iteration<std::extents<unsigned, 7>>(std::array<int, 1>{4});
84   test_iteration<std::extents<unsigned, 7, 8>>(std::array<int, 2>{25, 3});
85   test_iteration<std::extents<signed char, D, D, D, D>>(std::array<int, 4>{1, 1, 1, 1}, 1, 1, 1, 1);
86 
87   // Check operator constraint for number of arguments
88   static_assert(check_operator_constraints(
89       std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0));
90   static_assert(!check_operator_constraints(
91       std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0, 0));
92 
93   // Check operator constraint for convertibility of arguments to index_type
94   static_assert(check_operator_constraints(
95       std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), IntType(0)));
96   static_assert(!check_operator_constraints(
97       std::layout_stride::mapping<std::extents<unsigned, D>>(std::extents<unsigned, D>(1), std::array{1}), IntType(0)));
98 
99   // Check operator constraint for no-throw-constructibility of index_type from arguments
100   static_assert(!check_operator_constraints(
101       std::layout_stride::mapping<std::extents<unsigned char, D>>(std::extents<unsigned char, D>(1), std::array{1}),
102       IntType(0)));
103 
104   return true;
105 }
106 
107 constexpr bool test_large() {
108   constexpr std::size_t D = std::dynamic_extent;
109   test_iteration<std::extents<int64_t, D, 8, D, D>>(std::array<int, 4>{2000, 2, 20, 200}, 7, 9, 10);
110   test_iteration<std::extents<int64_t, D, 8, 1, D>>(std::array<int, 4>{2000, 20, 20, 200}, 7, 10);
111   return true;
112 }
113 
114 int main(int, char**) {
115   test();
116   static_assert(test());
117 
118   // The large test iterates over ~10k loop indices.
119   // With assertions enabled this triggered the maximum default limit
120   // for steps in consteval expressions. Assertions roughly double the
121   // total number of instructions, so this was already close to the maximum.
122   test_large();
123   return 0;
124 }
125