1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
13 // Test default iteration:
15 // template<class... Indices>
16 // constexpr index_type operator()(Indices...) const noexcept;
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.
24 // * extents_type::index-cast(i) is a multidimensional index in extents_.
30 #include "test_macros.h"
32 #include "../ConvertibleToIntegral.h"
34 template<class Mapping
, class ... Indices
>
35 concept operator_constraints
= requires(Mapping m
, Indices
... idxs
) {
36 {std::is_same_v
<decltype(m(idxs
...)), typename
Mapping::index_type
>};
39 template<class Mapping
, class ... Indices
>
41 operator_constraints
<Mapping
, Indices
...>
43 constexpr bool check_operator_constraints(Mapping m
, Indices
... idxs
) {
48 template<class Mapping
, class ... Indices
>
49 constexpr bool check_operator_constraints(Mapping
, Indices
...) {
53 template <class M
, class T
, class... Args
>
54 constexpr void iterate_right(M m
, T
& count
, Args
... args
) {
55 constexpr size_t r
= sizeof...(Args
);
56 if constexpr (M::extents_type::rank() == r
) {
57 ASSERT_NOEXCEPT(m(args
...));
58 assert(count
== m(args
...));
61 for (typename
M::index_type i
= 0; i
< m
.extents().extent(r
); i
++) {
62 iterate_right(m
, count
, args
..., i
);
67 template <class E
, class... Args
>
68 constexpr void test_iteration(Args
... args
) {
69 using M
= std::layout_right::mapping
<E
>;
72 typename
E::index_type count
= 0;
73 iterate_right(m
, count
);
76 constexpr bool test() {
77 constexpr size_t D
= std::dynamic_extent
;
78 test_iteration
<std::extents
<int>>();
79 test_iteration
<std::extents
<unsigned, D
>>(1);
80 test_iteration
<std::extents
<unsigned, D
>>(7);
81 test_iteration
<std::extents
<unsigned, 7>>();
82 test_iteration
<std::extents
<unsigned, 7, 8>>();
83 test_iteration
<std::extents
<signed char, D
, D
, D
, D
>>(1, 1, 1, 1);
85 // Check operator constraint for number of arguments
86 static_assert(check_operator_constraints(std::layout_right::mapping
<std::extents
<int, D
>>(std::extents
<int, D
>(1)), 0));
87 static_assert(!check_operator_constraints(std::layout_right::mapping
<std::extents
<int, D
>>(std::extents
<int, D
>(1)), 0, 0));
89 // Check operator constraint for convertibility of arguments to index_type
90 static_assert(check_operator_constraints(std::layout_right::mapping
<std::extents
<int, D
>>(std::extents
<int, D
>(1)), IntType(0)));
91 static_assert(!check_operator_constraints(std::layout_right::mapping
<std::extents
<unsigned, D
>>(std::extents
<unsigned, D
>(1)), IntType(0)));
93 // Check operator constraint for no-throw-constructibility of index_type from arguments
94 static_assert(!check_operator_constraints(std::layout_right::mapping
<std::extents
<unsigned char, D
>>(std::extents
<unsigned char, D
>(1)), IntType(0)));
99 constexpr bool test_large() {
100 constexpr size_t D
= std::dynamic_extent
;
101 test_iteration
<std::extents
<int64_t, D
, 8, D
, D
>>(7, 9, 10);
102 test_iteration
<std::extents
<int64_t, D
, 8, 1, D
>>(7, 10);
106 int main(int, char**) {
108 static_assert(test());
110 // The large test iterates over ~10k loop indices.
111 // With assertions enabled this triggered the maximum default limit
112 // for steps in consteval expressions. Assertions roughly double the
113 // total number of instructions, so this was already close to the maximum.