xref: /llvm-project/libcxx/test/std/containers/views/mdspan/layout_stride/stride.pass.cpp (revision 5e19fd172063c8957a35c7fa3596620f79ebba97)
1639a0986SChristian Trott //===----------------------------------------------------------------------===//
2639a0986SChristian Trott //
3639a0986SChristian Trott // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4639a0986SChristian Trott // See https://llvm.org/LICENSE.txt for license information.
5639a0986SChristian Trott // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6639a0986SChristian Trott //
7639a0986SChristian Trott //===----------------------------------------------------------------------===//
8639a0986SChristian Trott 
9639a0986SChristian Trott // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10639a0986SChristian Trott 
11639a0986SChristian Trott // <mdspan>
12639a0986SChristian Trott 
13639a0986SChristian Trott // constexpr index_type stride(rank_type i) const noexcept;
14639a0986SChristian Trott //
15639a0986SChristian Trott //   Constraints: extents_type::rank() > 0 is true.
16639a0986SChristian Trott //
17639a0986SChristian Trott //   Preconditions: i < extents_type::rank() is true.
18639a0986SChristian Trott //
19639a0986SChristian Trott //   Returns: extents().rev-prod-of-extents(i).
20639a0986SChristian Trott 
21639a0986SChristian Trott #include <mdspan>
22639a0986SChristian Trott #include <array>
23*5e19fd17SLouis Dionne #include <cassert>
24639a0986SChristian Trott #include <cstdint>
25639a0986SChristian Trott #include <cstdio>
26*5e19fd17SLouis Dionne #include <span> // dynamic_extent
27*5e19fd17SLouis Dionne 
28639a0986SChristian Trott #include "test_macros.h"
29639a0986SChristian Trott 
30639a0986SChristian Trott template <class E, class... Args>
31639a0986SChristian Trott constexpr void test_stride(std::array<typename E::index_type, E::rank()> strides, Args... args) {
32639a0986SChristian Trott   using M = std::layout_stride::mapping<E>;
33639a0986SChristian Trott   M m(E(args...), strides);
34639a0986SChristian Trott 
35639a0986SChristian Trott   ASSERT_NOEXCEPT(m.stride(0));
36639a0986SChristian Trott   for (size_t r = 0; r < E::rank(); r++)
37639a0986SChristian Trott     assert(strides[r] == m.stride(r));
38639a0986SChristian Trott 
39639a0986SChristian Trott   ASSERT_NOEXCEPT(m.strides());
40639a0986SChristian Trott   auto strides_out = m.strides();
41639a0986SChristian Trott   static_assert(std::is_same_v<decltype(strides_out), std::array<typename E::index_type, E::rank()>>);
42639a0986SChristian Trott   for (size_t r = 0; r < E::rank(); r++)
43639a0986SChristian Trott     assert(strides[r] == strides_out[r]);
44639a0986SChristian Trott }
45639a0986SChristian Trott 
46639a0986SChristian Trott constexpr bool test() {
47639a0986SChristian Trott   constexpr size_t D = std::dynamic_extent;
48639a0986SChristian Trott   test_stride<std::extents<unsigned, D>>(std::array<unsigned, 1>{1}, 7);
49639a0986SChristian Trott   test_stride<std::extents<unsigned, 7>>(std::array<unsigned, 1>{1});
50639a0986SChristian Trott   test_stride<std::extents<unsigned, 7, 8>>(std::array<unsigned, 2>{8, 1});
51639a0986SChristian Trott   test_stride<std::extents<int64_t, D, 8, D, D>>(std::array<int64_t, 4>{720, 90, 10, 1}, 7, 9, 10);
52639a0986SChristian Trott   return true;
53639a0986SChristian Trott }
54639a0986SChristian Trott 
55639a0986SChristian Trott int main(int, char**) {
56639a0986SChristian Trott   test();
57639a0986SChristian Trott   static_assert(test());
58639a0986SChristian Trott   return 0;
59639a0986SChristian Trott }
60