xref: /llvm-project/libcxx/test/std/containers/views/mdspan/layout_stride/ctor.default.pass.cpp (revision e99c4906e44ae3f921fa05356909d006cda8d954)
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 // Test default construction:
14639a0986SChristian Trott //
15639a0986SChristian Trott // constexpr mapping() noexcept;
16639a0986SChristian Trott //
17639a0986SChristian Trott //
18639a0986SChristian Trott // Preconditions: layout_right::mapping<extents_type>().required_span_size() is representable as a value of type index_type ([basic.fundamental]).
19639a0986SChristian Trott //
20639a0986SChristian Trott // Effects: Direct-non-list-initializes extents_ with extents_type(), and for all d in the range [0, rank_),
21639a0986SChristian Trott //          direct-non-list-initializes strides_[d] with layout_right::mapping<extents_type>().stride(d).
22639a0986SChristian Trott 
23639a0986SChristian Trott #include <cassert>
24*e99c4906SNikolas Klauser #include <cstddef>
25639a0986SChristian Trott #include <cstdint>
26*e99c4906SNikolas Klauser #include <mdspan>
275e19fd17SLouis Dionne #include <span> // dynamic_extent
28639a0986SChristian Trott 
29639a0986SChristian Trott #include "test_macros.h"
30639a0986SChristian Trott 
31639a0986SChristian Trott template <class E>
32639a0986SChristian Trott constexpr void test_construction() {
33639a0986SChristian Trott   using M = std::layout_stride::mapping<E>;
34639a0986SChristian Trott   ASSERT_NOEXCEPT(M{});
35639a0986SChristian Trott   M m;
36639a0986SChristian Trott   E e;
37639a0986SChristian Trott 
38639a0986SChristian Trott   // check correct extents are returned
39639a0986SChristian Trott   ASSERT_NOEXCEPT(m.extents());
40639a0986SChristian Trott   assert(m.extents() == e);
41639a0986SChristian Trott 
42639a0986SChristian Trott   // check required_span_size()
43639a0986SChristian Trott   typename E::index_type expected_size = 1;
44639a0986SChristian Trott   for (typename E::rank_type r = 0; r < E::rank(); r++)
45639a0986SChristian Trott     expected_size *= e.extent(r);
46639a0986SChristian Trott   assert(m.required_span_size() == expected_size);
47639a0986SChristian Trott 
48639a0986SChristian Trott   // check strides: node stride function is constrained on rank>0, e.extent(r) is not
49639a0986SChristian Trott   auto strides = m.strides();
50639a0986SChristian Trott   ASSERT_NOEXCEPT(m.strides());
51639a0986SChristian Trott   if constexpr (E::rank() > 0) {
52639a0986SChristian Trott     std::layout_right::mapping<E> m_right;
53639a0986SChristian Trott     for (typename E::rank_type r = 0; r < E::rank(); r++) {
54639a0986SChristian Trott       assert(m.stride(r) == m_right.stride(r));
55639a0986SChristian Trott       assert(strides[r] == m.stride(r));
56639a0986SChristian Trott     }
57639a0986SChristian Trott   }
58639a0986SChristian Trott }
59639a0986SChristian Trott 
60639a0986SChristian Trott constexpr bool test() {
61639a0986SChristian Trott   constexpr size_t D = std::dynamic_extent;
62639a0986SChristian Trott   test_construction<std::extents<int>>();
63639a0986SChristian Trott   test_construction<std::extents<unsigned, D>>();
64639a0986SChristian Trott   test_construction<std::extents<unsigned, 7>>();
65639a0986SChristian Trott   test_construction<std::extents<unsigned, 0>>();
66639a0986SChristian Trott   test_construction<std::extents<unsigned, 7, 8>>();
67639a0986SChristian Trott   test_construction<std::extents<int64_t, D, 8, D, D>>();
68639a0986SChristian Trott   return true;
69639a0986SChristian Trott }
70639a0986SChristian Trott 
71639a0986SChristian Trott int main(int, char**) {
72639a0986SChristian Trott   test();
73639a0986SChristian Trott   static_assert(test());
74639a0986SChristian Trott   return 0;
75639a0986SChristian Trott }
76