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