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 | // constexpr index_type stride(rank_type i) const noexcept; |
14 | // |
15 | // Constraints: extents_type::rank() > 0 is true. |
16 | // |
17 | // Preconditions: i < extents_type::rank() is true. |
18 | // |
19 | // Returns: extents().rev-prod-of-extents(i). |
20 | |
21 | #include <mdspan> |
22 | #include <array> |
23 | #include <cassert> |
24 | #include <cstdint> |
25 | #include <cstdio> |
26 | |
27 | #include "test_macros.h" |
28 | |
29 | template <class E, class... Args> |
30 | constexpr void test_stride(std::array<typename E::index_type, E::rank()> strides, Args... args) { |
31 | using M = std::layout_stride::mapping<E>; |
32 | M m(E(args...), strides); |
33 | |
34 | ASSERT_NOEXCEPT(m.stride(0)); |
35 | for (size_t r = 0; r < E::rank(); r++) |
36 | assert(strides[r] == m.stride(r)); |
37 | |
38 | ASSERT_NOEXCEPT(m.strides()); |
39 | auto strides_out = m.strides(); |
40 | static_assert(std::is_same_v<decltype(strides_out), std::array<typename E::index_type, E::rank()>>); |
41 | for (size_t r = 0; r < E::rank(); r++) |
42 | assert(strides[r] == strides_out[r]); |
43 | } |
44 | |
45 | constexpr bool test() { |
46 | constexpr size_t D = std::dynamic_extent; |
47 | test_stride<std::extents<unsigned, D>>(std::array<unsigned, 1>{1}, 7); |
48 | test_stride<std::extents<unsigned, 7>>(std::array<unsigned, 1>{1}); |
49 | test_stride<std::extents<unsigned, 7, 8>>(std::array<unsigned, 2>{8, 1}); |
50 | test_stride<std::extents<int64_t, D, 8, D, D>>(std::array<int64_t, 4>{720, 90, 10, 1}, 7, 9, 10); |
51 | return true; |
52 | } |
53 | |
54 | int main(int, char**) { |
55 | test(); |
56 | static_assert(test()); |
57 | return 0; |
58 | } |
59 | |