xref: /llvm-project/libcxx/test/libcxx/containers/views/views.span/span.cons/assert.iter_size.pass.cpp (revision 4c4606a743d26ebe5fdd84e953254627a459a991)
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
9 
10 // <span>
11 //
12 // constexpr span<T, Extent>::span(Iterator, size_type);
13 //
14 // Check that the passed size is equal to the statically known extent.
15 // Note that it doesn't make sense to validate the incoming size in the
16 // dynamic_extent version.
17 
18 // REQUIRES: has-unix-headers
19 // UNSUPPORTED: libcpp-hardening-mode=none
20 // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
21 
22 #include <array>
23 #include <span>
24 
25 #include "check_assertion.h"
26 
27 int main(int, char**) {
28   std::array<int, 3> array{0, 1, 2};
29 
30   // Input range too large (exceeds the span extent)
31   {
32     auto f = [&] {
33       std::span<int, 3> const s(array.data(), 4);
34       (void)s;
35     };
36     TEST_LIBCPP_ASSERT_FAILURE(f(), "size mismatch in span's constructor (iterator, len)");
37   }
38 
39   // Input range too small (doesn't fill the span)
40   {
41     auto f = [&] {
42       std::span<int, 3> const s(array.data(), 2);
43       (void)s;
44     };
45     TEST_LIBCPP_ASSERT_FAILURE(f(), "size mismatch in span's constructor (iterator, len)");
46   }
47 
48   // Input range is non-empty but starts with a null pointer
49   {
50     // static extent
51     {
52       auto f = [&] {
53         int* p = nullptr;
54         std::span<int, 3> const s(p, 3);
55         (void)s;
56       };
57       TEST_LIBCPP_ASSERT_FAILURE(f(), "passed nullptr with non-zero length in span's constructor (iterator, len)");
58     }
59 
60     // dynamic extent
61     {
62       auto f = [&] {
63         int* p = nullptr;
64         std::span<int, std::dynamic_extent> const s(p, 1);
65         (void)s;
66       };
67       TEST_LIBCPP_ASSERT_FAILURE(f(), "passed nullptr with non-zero length in span's constructor (iterator, len)");
68     }
69   }
70 
71   return 0;
72 }
73