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

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