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 size_type size() const noexcept;
13//
14
15
16#include <span>
17#include <cassert>
18#include <string>
19
20#include "test_macros.h"
21
22
23template <typename Span>
24constexpr bool testConstexprSpan(Span sp, std::size_t sz)
25{
26 ASSERT_NOEXCEPT(sp.size());
27 return sp.size() == sz;
28}
29
30
31template <typename Span>
32void testRuntimeSpan(Span sp, std::size_t sz)
33{
34 ASSERT_NOEXCEPT(sp.size());
35 assert(sp.size() == sz);
36}
37
38struct A{};
39constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
40 int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
41
42int main(int, char**)
43{
44 static_assert(testConstexprSpan(std::span<int>(), 0), "");
45 static_assert(testConstexprSpan(std::span<long>(), 0), "");
46 static_assert(testConstexprSpan(std::span<double>(), 0), "");
47 static_assert(testConstexprSpan(std::span<A>(), 0), "");
48 static_assert(testConstexprSpan(std::span<std::string>(), 0), "");
49
50 static_assert(testConstexprSpan(std::span<int, 0>(), 0), "");
51 static_assert(testConstexprSpan(std::span<long, 0>(), 0), "");
52 static_assert(testConstexprSpan(std::span<double, 0>(), 0), "");
53 static_assert(testConstexprSpan(std::span<A, 0>(), 0), "");
54 static_assert(testConstexprSpan(std::span<std::string, 0>(), 0), "");
55
56 static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), 1), "");
57 static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 2), "");
58 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 3), "");
59 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 4), "");
60 static_assert(testConstexprSpan(std::span<const int>(iArr1, 5), 5), "");
61
62 testRuntimeSpan(std::span<int> (), 0);
63 testRuntimeSpan(std::span<long> (), 0);
64 testRuntimeSpan(std::span<double> (), 0);
65 testRuntimeSpan(std::span<A> (), 0);
66 testRuntimeSpan(std::span<std::string>(), 0);
67
68 testRuntimeSpan(std::span<int, 0> (), 0);
69 testRuntimeSpan(std::span<long, 0> (), 0);
70 testRuntimeSpan(std::span<double, 0> (), 0);
71 testRuntimeSpan(std::span<A, 0> (), 0);
72 testRuntimeSpan(std::span<std::string, 0>(), 0);
73
74 testRuntimeSpan(std::span<int>(iArr2, 1), 1);
75 testRuntimeSpan(std::span<int>(iArr2, 2), 2);
76 testRuntimeSpan(std::span<int>(iArr2, 3), 3);
77 testRuntimeSpan(std::span<int>(iArr2, 4), 4);
78 testRuntimeSpan(std::span<int>(iArr2, 5), 5);
79
80 testRuntimeSpan(std::span<int, 1>(iArr2 + 5, 1), 1);
81 testRuntimeSpan(std::span<int, 2>(iArr2 + 4, 2), 2);
82 testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3), 3);
83 testRuntimeSpan(std::span<int, 4>(iArr2 + 2, 4), 4);
84 testRuntimeSpan(std::span<int, 5>(iArr2 + 1, 5), 5);
85
86 std::string s;
87 testRuntimeSpan(std::span<std::string>(&s, (std::size_t) 0), 0);
88 testRuntimeSpan(std::span<std::string>(&s, 1), 1);
89
90 return 0;
91}
92

source code of libcxx/test/std/containers/views/views.span/span.obs/size.pass.cpp