xref: /llvm-project/libcxx/test/std/containers/views/mdspan/layout_right/ctor.extents.pass.cpp (revision e99c4906e44ae3f921fa05356909d006cda8d954)
1cfa096d9SChristian Trott //===----------------------------------------------------------------------===//
2cfa096d9SChristian Trott //
3cfa096d9SChristian Trott // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cfa096d9SChristian Trott // See https://llvm.org/LICENSE.txt for license information.
5cfa096d9SChristian Trott // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cfa096d9SChristian Trott //
7cfa096d9SChristian Trott //===----------------------------------------------------------------------===//
8cfa096d9SChristian Trott 
9cfa096d9SChristian Trott // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10cfa096d9SChristian Trott 
11cfa096d9SChristian Trott // <mdspan>
12cfa096d9SChristian Trott 
13cfa096d9SChristian Trott // constexpr mapping(const extents_type&) noexcept;
14cfa096d9SChristian Trott //
15cfa096d9SChristian Trott // Preconditions: The size of the multidimensional index space e is representable
16cfa096d9SChristian Trott //                as a value of type index_type ([basic.fundamental]).
17cfa096d9SChristian Trott //
18cfa096d9SChristian Trott // Effects: Direct-non-list-initializes extents_ with e.
19cfa096d9SChristian Trott 
20cfa096d9SChristian Trott #include <cassert>
21*e99c4906SNikolas Klauser #include <cstddef>
22cfa096d9SChristian Trott #include <cstdint>
23*e99c4906SNikolas Klauser #include <mdspan>
245e19fd17SLouis Dionne #include <span> // dynamic_extent
25cfa096d9SChristian Trott 
26cfa096d9SChristian Trott #include "test_macros.h"
27cfa096d9SChristian Trott 
28cfa096d9SChristian Trott template <class E>
29cfa096d9SChristian Trott constexpr void test_construction(E e) {
30cfa096d9SChristian Trott   using M = std::layout_right::mapping<E>;
31cfa096d9SChristian Trott   ASSERT_NOEXCEPT(M{e});
32cfa096d9SChristian Trott   M m(e);
33cfa096d9SChristian Trott 
34cfa096d9SChristian Trott   // check correct extents are returned
35cfa096d9SChristian Trott   ASSERT_NOEXCEPT(m.extents());
36cfa096d9SChristian Trott   assert(m.extents() == e);
37cfa096d9SChristian Trott 
38cfa096d9SChristian Trott   // check required_span_size()
39cfa096d9SChristian Trott   typename E::index_type expected_size = 1;
40cfa096d9SChristian Trott   for (typename E::rank_type r = 0; r < E::rank(); r++)
41cfa096d9SChristian Trott     expected_size *= e.extent(r);
42cfa096d9SChristian Trott   assert(m.required_span_size() == expected_size);
43cfa096d9SChristian Trott }
44cfa096d9SChristian Trott 
45cfa096d9SChristian Trott constexpr bool test() {
46cfa096d9SChristian Trott   constexpr size_t D = std::dynamic_extent;
47cfa096d9SChristian Trott   test_construction(std::extents<int>());
48cfa096d9SChristian Trott   test_construction(std::extents<unsigned, D>(7));
49cfa096d9SChristian Trott   test_construction(std::extents<unsigned, 7>());
50cfa096d9SChristian Trott   test_construction(std::extents<unsigned, 7, 8>());
51cfa096d9SChristian Trott   test_construction(std::extents<int64_t, D, 8, D, D>(7, 9, 10));
52cfa096d9SChristian Trott   return true;
53cfa096d9SChristian Trott }
54cfa096d9SChristian Trott 
55cfa096d9SChristian Trott int main(int, char**) {
56cfa096d9SChristian Trott   test();
57cfa096d9SChristian Trott   static_assert(test());
58cfa096d9SChristian Trott   return 0;
59cfa096d9SChristian Trott }
60