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 <mdspan>
30#include <cassert>
31#include <utility>
32
33#include "test_macros.h"
34
35template <class E, size_t rank, size_t rank_dynamic, size_t... StaticExts, size_t... Indices>
36void test_static_observers(std::index_sequence<StaticExts...>, std::index_sequence<Indices...>) {
37 ASSERT_NOEXCEPT(E::rank());
38 static_assert(E::rank() == rank);
39 ASSERT_NOEXCEPT(E::rank_dynamic());
40 static_assert(E::rank_dynamic() == rank_dynamic);
41
42 // Let's only test this if the call isn't a precondition violation
43 if constexpr (rank > 0) {
44 ASSERT_NOEXCEPT(E::static_extent(0));
45 ASSERT_SAME_TYPE(decltype(E::static_extent(0)), size_t);
46 static_assert(((E::static_extent(Indices) == StaticExts) && ...));
47 }
48}
49
50template <class E, size_t rank, size_t rank_dynamic, size_t... StaticExts>
51void test_static_observers() {
52 test_static_observers<E, rank, rank_dynamic>(
53 std::index_sequence<StaticExts...>(), std::make_index_sequence<sizeof...(StaticExts)>());
54}
55
56template <class T>
57void test() {
58 constexpr size_t D = std::dynamic_extent;
59 constexpr size_t S = 5;
60
61 test_static_observers<std::extents<T>, 0, 0>();
62
63 test_static_observers<std::extents<T, S>, 1, 0, S>();
64 test_static_observers<std::extents<T, D>, 1, 1, D>();
65
66 test_static_observers<std::extents<T, S, S>, 2, 0, S, S>();
67 test_static_observers<std::extents<T, S, D>, 2, 1, S, D>();
68 test_static_observers<std::extents<T, D, S>, 2, 1, D, S>();
69 test_static_observers<std::extents<T, D, D>, 2, 2, D, D>();
70
71 test_static_observers<std::extents<T, S, S, S>, 3, 0, S, S, S>();
72 test_static_observers<std::extents<T, S, S, D>, 3, 1, S, S, D>();
73 test_static_observers<std::extents<T, S, D, S>, 3, 1, S, D, S>();
74 test_static_observers<std::extents<T, D, S, S>, 3, 1, D, S, S>();
75 test_static_observers<std::extents<T, S, D, D>, 3, 2, S, D, D>();
76 test_static_observers<std::extents<T, D, S, D>, 3, 2, D, S, D>();
77 test_static_observers<std::extents<T, D, D, S>, 3, 2, D, D, S>();
78 test_static_observers<std::extents<T, D, D, D>, 3, 3, D, D, D>();
79}
80
81int main(int, char**) {
82 test<int>();
83 test<unsigned>();
84 test<signed char>();
85 test<long long>();
86 test<size_t>();
87 return 0;
88}
89

source code of libcxx/test/std/containers/views/mdspan/extents/obs_static.pass.cpp