xref: /llvm-project/libcxx/test/std/containers/views/mdspan/layout_right/ctor.extents.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 // constexpr mapping(const extents_type&) noexcept;
14 //
15 // Preconditions: The size of the multidimensional index space e is representable
16 //                as a value of type index_type ([basic.fundamental]).
17 //
18 // Effects: Direct-non-list-initializes extents_ with e.
19 
20 #include <cassert>
21 #include <cstddef>
22 #include <cstdint>
23 #include <mdspan>
24 #include <span> // dynamic_extent
25 
26 #include "test_macros.h"
27 
28 template <class E>
29 constexpr void test_construction(E e) {
30   using M = std::layout_right::mapping<E>;
31   ASSERT_NOEXCEPT(M{e});
32   M m(e);
33 
34   // check correct extents are returned
35   ASSERT_NOEXCEPT(m.extents());
36   assert(m.extents() == e);
37 
38   // check required_span_size()
39   typename E::index_type expected_size = 1;
40   for (typename E::rank_type r = 0; r < E::rank(); r++)
41     expected_size *= e.extent(r);
42   assert(m.required_span_size() == expected_size);
43 }
44 
45 constexpr bool test() {
46   constexpr size_t D = std::dynamic_extent;
47   test_construction(std::extents<int>());
48   test_construction(std::extents<unsigned, D>(7));
49   test_construction(std::extents<unsigned, 7>());
50   test_construction(std::extents<unsigned, 7, 8>());
51   test_construction(std::extents<int64_t, D, 8, D, D>(7, 9, 10));
52   return true;
53 }
54 
55 int main(int, char**) {
56   test();
57   static_assert(test());
58   return 0;
59 }
60