xref: /llvm-project/libcxx/test/std/containers/views/mdspan/layout_stride/ctor.default.pass.cpp (revision e99c4906e44ae3f921fa05356909d006cda8d954)
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 construction:
14 //
15 // constexpr mapping() noexcept;
16 //
17 //
18 // Preconditions: layout_right::mapping<extents_type>().required_span_size() is representable as a value of type index_type ([basic.fundamental]).
19 //
20 // Effects: Direct-non-list-initializes extents_ with extents_type(), and for all d in the range [0, rank_),
21 //          direct-non-list-initializes strides_[d] with layout_right::mapping<extents_type>().stride(d).
22 
23 #include <cassert>
24 #include <cstddef>
25 #include <cstdint>
26 #include <mdspan>
27 #include <span> // dynamic_extent
28 
29 #include "test_macros.h"
30 
31 template <class E>
32 constexpr void test_construction() {
33   using M = std::layout_stride::mapping<E>;
34   ASSERT_NOEXCEPT(M{});
35   M m;
36   E e;
37 
38   // check correct extents are returned
39   ASSERT_NOEXCEPT(m.extents());
40   assert(m.extents() == e);
41 
42   // check required_span_size()
43   typename E::index_type expected_size = 1;
44   for (typename E::rank_type r = 0; r < E::rank(); r++)
45     expected_size *= e.extent(r);
46   assert(m.required_span_size() == expected_size);
47 
48   // check strides: node stride function is constrained on rank>0, e.extent(r) is not
49   auto strides = m.strides();
50   ASSERT_NOEXCEPT(m.strides());
51   if constexpr (E::rank() > 0) {
52     std::layout_right::mapping<E> m_right;
53     for (typename E::rank_type r = 0; r < E::rank(); r++) {
54       assert(m.stride(r) == m_right.stride(r));
55       assert(strides[r] == m.stride(r));
56     }
57   }
58 }
59 
60 constexpr bool test() {
61   constexpr size_t D = std::dynamic_extent;
62   test_construction<std::extents<int>>();
63   test_construction<std::extents<unsigned, D>>();
64   test_construction<std::extents<unsigned, 7>>();
65   test_construction<std::extents<unsigned, 0>>();
66   test_construction<std::extents<unsigned, 7, 8>>();
67   test_construction<std::extents<int64_t, D, 8, D, D>>();
68   return true;
69 }
70 
71 int main(int, char**) {
72   test();
73   static_assert(test());
74   return 0;
75 }
76