xref: /llvm-project/libcxx/test/std/containers/views/mdspan/extents/obs_static.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 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9 
10 // <mdspan>
11 
12 // static constexpr rank_type rank() noexcept;
13 // static constexpr rank_type rank_dynamic() noexcept;
14 //
15 // static constexpr size_t static_extent(rank_type i) noexcept;
16 //
17 //   Preconditions: i < rank() is true.
18 //
19 //   Returns: Ei.
20 //
21 //
22 // constexpr index_type extent(rank_type i) const noexcept;
23 //
24 //   Preconditions: i < rank() is true.
25 //
26 //   Returns: Di.
27 //
28 
29 #include <cassert>
30 #include <cstddef>
31 #include <mdspan>
32 #include <span> // dynamic_extent
33 #include <utility>
34 
35 #include "test_macros.h"
36 
37 template <class E, size_t rank, size_t rank_dynamic, size_t... StaticExts, size_t... Indices>
38 void test_static_observers(std::index_sequence<StaticExts...>, std::index_sequence<Indices...>) {
39   ASSERT_NOEXCEPT(E::rank());
40   static_assert(E::rank() == rank);
41   ASSERT_NOEXCEPT(E::rank_dynamic());
42   static_assert(E::rank_dynamic() == rank_dynamic);
43 
44   // Let's only test this if the call isn't a precondition violation
45   if constexpr (rank > 0) {
46     ASSERT_NOEXCEPT(E::static_extent(0));
47     ASSERT_SAME_TYPE(decltype(E::static_extent(0)), size_t);
48     static_assert(((E::static_extent(Indices) == StaticExts) && ...));
49   }
50 }
51 
52 template <class E, size_t rank, size_t rank_dynamic, size_t... StaticExts>
53 void test_static_observers() {
54   test_static_observers<E, rank, rank_dynamic>(
55       std::index_sequence<StaticExts...>(), std::make_index_sequence<sizeof...(StaticExts)>());
56 }
57 
58 template <class T>
59 void test() {
60   constexpr size_t D = std::dynamic_extent;
61   constexpr size_t S = 5;
62 
63   test_static_observers<std::extents<T>, 0, 0>();
64 
65   test_static_observers<std::extents<T, S>, 1, 0, S>();
66   test_static_observers<std::extents<T, D>, 1, 1, D>();
67 
68   test_static_observers<std::extents<T, S, S>, 2, 0, S, S>();
69   test_static_observers<std::extents<T, S, D>, 2, 1, S, D>();
70   test_static_observers<std::extents<T, D, S>, 2, 1, D, S>();
71   test_static_observers<std::extents<T, D, D>, 2, 2, D, D>();
72 
73   test_static_observers<std::extents<T, S, S, S>, 3, 0, S, S, S>();
74   test_static_observers<std::extents<T, S, S, D>, 3, 1, S, S, D>();
75   test_static_observers<std::extents<T, S, D, S>, 3, 1, S, D, S>();
76   test_static_observers<std::extents<T, D, S, S>, 3, 1, D, S, S>();
77   test_static_observers<std::extents<T, S, D, D>, 3, 2, S, D, D>();
78   test_static_observers<std::extents<T, D, S, D>, 3, 2, D, S, D>();
79   test_static_observers<std::extents<T, D, D, S>, 3, 2, D, D, S>();
80   test_static_observers<std::extents<T, D, D, D>, 3, 3, D, D, D>();
81 }
82 
83 int main(int, char**) {
84   test<int>();
85   test<unsigned>();
86   test<signed char>();
87   test<long long>();
88   test<size_t>();
89   return 0;
90 }
91