xref: /llvm-project/libcxx/test/libcxx/containers/views/mdspan/extents/assert.obs.pass.cpp (revision 58780b811c23df3d928d8452ee21c862dde754a2)
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 // REQUIRES: has-unix-headers
8 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9 // UNSUPPORTED: libcpp-hardening-mode=none
10 // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
11 
12 // <mdspan>
13 
14 // static constexpr size_t static_extent(rank_type i) noexcept;
15 //
16 //   Preconditions: i < rank() is true.
17 //
18 //   Returns: Ei.
19 //
20 //
21 // constexpr index_type extent(rank_type i) const noexcept;
22 //
23 //   Preconditions: i < rank() is true.
24 //
25 //   Returns: Di.
26 
27 #include <mdspan>
28 #include <cassert>
29 
30 #include "check_assertion.h"
31 
main(int,char **)32 int main(int, char**) {
33   constexpr size_t D = std::dynamic_extent;
34 
35   // mismatch of static extent
36   {
37     std::extents<int> e;
38     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.extent(0); }()), "extents access: index must be less than rank");
39     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.static_extent(0); }()), "extents access: index must be less than rank");
40   }
41   {
42     std::extents<int, D> e;
43     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.extent(2); }()), "extents access: index must be less than rank");
44     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.static_extent(2); }()), "extents access: index must be less than rank");
45   }
46   {
47     std::extents<int, 5> e;
48     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.extent(2); }()), "extents access: index must be less than rank");
49     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.static_extent(2); }()), "extents access: index must be less than rank");
50   }
51   {
52     std::extents<int, D, 5> e;
53     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.extent(2); }()), "extents access: index must be less than rank");
54     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.static_extent(2); }()), "extents access: index must be less than rank");
55   }
56   {
57     std::extents<int, 1, 2, 3, 4, 5, 6, 7, 8> e;
58     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.extent(9); }()), "extents access: index must be less than rank");
59     TEST_LIBCPP_ASSERT_FAILURE(([=] { e.static_extent(9); }()), "extents access: index must be less than rank");
60   }
61 
62   // check that static_extent works in constant expression with assertions enabled
63   static_assert(std::extents<int, D, 5>::static_extent(1) == 5);
64   return 0;
65 }
66