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 <type_traits>
31
32#include "test_macros.h"
33
34#include "../ConvertibleToIntegral.h"
35
36template <class Mapping, class... Indices>
37concept operator_constraints = requires(Mapping m, Indices... idxs) {
38 { std::is_same_v<decltype(m(idxs...)), typename Mapping::index_type> };
39};
40
41template <class Mapping, class... Indices>
42 requires(operator_constraints<Mapping, Indices...>)
43constexpr bool check_operator_constraints(Mapping m, Indices... idxs) {
44 (void)m(idxs...);
45 return true;
46}
47
48template <class Mapping, class... Indices>
49constexpr bool check_operator_constraints(Mapping, Indices...) {
50 return false;
51}
52
53template <class M, class... Args>
54constexpr void iterate_stride(M m, const std::array<int, M::extents_type::rank()>& strides, Args... args) {
55 constexpr int r = static_cast<int>(M::extents_type::rank()) - 1 - static_cast<int>(sizeof...(Args));
56 if constexpr (-1 == r) {
57 ASSERT_NOEXCEPT(m(args...));
58 std::size_t expected_val = static_cast<std::size_t>([&]<std::size_t... Pos>(std::index_sequence<Pos...>) {
59 return ((args * strides[Pos]) + ... + 0);
60 }(std::make_index_sequence<M::extents_type::rank()>()));
61 assert(expected_val == static_cast<std::size_t>(m(args...)));
62 } else {
63 for (typename M::index_type i = 0; i < m.extents().extent(r); i++) {
64 iterate_stride(m, strides, i, args...);
65 }
66 }
67}
68
69template <class E, class... Args>
70constexpr void test_iteration(std::array<int, E::rank()> strides, Args... args) {
71 using M = std::layout_stride::mapping<E>;
72 M m(E(args...), strides);
73
74 iterate_stride(m, strides);
75}
76
77constexpr bool test() {
78 constexpr std::size_t D = std::dynamic_extent;
79 test_iteration<std::extents<int>>(std::array<int, 0>{});
80 test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{2}, 1);
81 test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{3}, 7);
82 test_iteration<std::extents<unsigned, 7>>(std::array<int, 1>{4});
83 test_iteration<std::extents<unsigned, 7, 8>>(std::array<int, 2>{25, 3});
84 test_iteration<std::extents<signed char, D, D, D, D>>(std::array<int, 4>{1, 1, 1, 1}, 1, 1, 1, 1);
85
86 // Check operator constraint for number of arguments
87 static_assert(check_operator_constraints(
88 std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0));
89 static_assert(!check_operator_constraints(
90 std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0, 0));
91
92 // Check operator constraint for convertibility of arguments to index_type
93 static_assert(check_operator_constraints(
94 std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), IntType(0)));
95 static_assert(!check_operator_constraints(
96 std::layout_stride::mapping<std::extents<unsigned, D>>(std::extents<unsigned, D>(1), std::array{1}), IntType(0)));
97
98 // Check operator constraint for no-throw-constructibility of index_type from arguments
99 static_assert(!check_operator_constraints(
100 std::layout_stride::mapping<std::extents<unsigned char, D>>(std::extents<unsigned char, D>(1), std::array{1}),
101 IntType(0)));
102
103 return true;
104}
105
106constexpr bool test_large() {
107 constexpr std::size_t D = std::dynamic_extent;
108 test_iteration<std::extents<int64_t, D, 8, D, D>>(std::array<int, 4>{2000, 2, 20, 200}, 7, 9, 10);
109 test_iteration<std::extents<int64_t, D, 8, 1, D>>(std::array<int, 4>{2000, 20, 20, 200}, 7, 10);
110 return true;
111}
112
113int main(int, char**) {
114 test();
115 static_assert(test());
116
117 // The large test iterates over ~10k loop indices.
118 // With assertions enabled this triggered the maximum default limit
119 // for steps in consteval expressions. Assertions roughly double the
120 // total number of instructions, so this was already close to the maximum.
121 test_large();
122 return 0;
123}
124

source code of libcxx/test/std/containers/views/mdspan/layout_stride/index_operator.pass.cpp