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 pointer data() 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, typename Span::pointer ptr) {
23 ASSERT_NOEXCEPT(sp.data());
24 return sp.data() == ptr;
25}
26
27template <typename Span>
28void testRuntimeSpan(Span sp, typename Span::pointer ptr) {
29 ASSERT_NOEXCEPT(sp.data());
30 assert(sp.data() == ptr);
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 // dynamic size
39 static_assert(testConstexprSpan(std::span<int>(), nullptr), "");
40 static_assert(testConstexprSpan(std::span<long>(), nullptr), "");
41 static_assert(testConstexprSpan(std::span<double>(), nullptr), "");
42 static_assert(testConstexprSpan(std::span<A>(), nullptr), "");
43 static_assert(testConstexprSpan(std::span<std::string>(), nullptr), "");
44
45 static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), iArr1), "");
46 static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), iArr1), "");
47 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), iArr1), "");
48 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), iArr1), "");
49
50 static_assert(testConstexprSpan(std::span<const int>(iArr1 + 1, 1), iArr1 + 1), "");
51 static_assert(testConstexprSpan(std::span<const int>(iArr1 + 2, 2), iArr1 + 2), "");
52 static_assert(testConstexprSpan(std::span<const int>(iArr1 + 3, 3), iArr1 + 3), "");
53 static_assert(testConstexprSpan(std::span<const int>(iArr1 + 4, 4), iArr1 + 4), "");
54
55 // static size
56 static_assert(testConstexprSpan(std::span<int, 0>(), nullptr), "");
57 static_assert(testConstexprSpan(std::span<long, 0>(), nullptr), "");
58 static_assert(testConstexprSpan(std::span<double, 0>(), nullptr), "");
59 static_assert(testConstexprSpan(std::span<A, 0>(), nullptr), "");
60 static_assert(testConstexprSpan(std::span<std::string, 0>(), nullptr), "");
61
62 static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1), iArr1), "");
63 static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), iArr1), "");
64 static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), iArr1), "");
65 static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), iArr1), "");
66
67 static_assert(testConstexprSpan(std::span<const int, 1>(iArr1 + 1, 1), iArr1 + 1), "");
68 static_assert(testConstexprSpan(std::span<const int, 2>(iArr1 + 2, 2), iArr1 + 2), "");
69 static_assert(testConstexprSpan(std::span<const int, 3>(iArr1 + 3, 3), iArr1 + 3), "");
70 static_assert(testConstexprSpan(std::span<const int, 4>(iArr1 + 4, 4), iArr1 + 4), "");
71
72 // dynamic size
73 testRuntimeSpan(std::span<int>(), nullptr);
74 testRuntimeSpan(std::span<long>(), nullptr);
75 testRuntimeSpan(std::span<double>(), nullptr);
76 testRuntimeSpan(std::span<A>(), nullptr);
77 testRuntimeSpan(std::span<std::string>(), nullptr);
78
79 testRuntimeSpan(std::span<int>(iArr2, 1), iArr2);
80 testRuntimeSpan(std::span<int>(iArr2, 2), iArr2);
81 testRuntimeSpan(std::span<int>(iArr2, 3), iArr2);
82 testRuntimeSpan(std::span<int>(iArr2, 4), iArr2);
83
84 testRuntimeSpan(std::span<int>(iArr2 + 1, 1), iArr2 + 1);
85 testRuntimeSpan(std::span<int>(iArr2 + 2, 2), iArr2 + 2);
86 testRuntimeSpan(std::span<int>(iArr2 + 3, 3), iArr2 + 3);
87 testRuntimeSpan(std::span<int>(iArr2 + 4, 4), iArr2 + 4);
88
89 // static size
90 testRuntimeSpan(std::span<int, 0>(), nullptr);
91 testRuntimeSpan(std::span<long, 0>(), nullptr);
92 testRuntimeSpan(std::span<double, 0>(), nullptr);
93 testRuntimeSpan(std::span<A, 0>(), nullptr);
94 testRuntimeSpan(std::span<std::string, 0>(), nullptr);
95
96 testRuntimeSpan(std::span<int, 1>(iArr2, 1), iArr2);
97 testRuntimeSpan(std::span<int, 2>(iArr2, 2), iArr2);
98 testRuntimeSpan(std::span<int, 3>(iArr2, 3), iArr2);
99 testRuntimeSpan(std::span<int, 4>(iArr2, 4), iArr2);
100
101 testRuntimeSpan(std::span<int, 1>(iArr2 + 1, 1), iArr2 + 1);
102 testRuntimeSpan(std::span<int, 2>(iArr2 + 2, 2), iArr2 + 2);
103 testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3), iArr2 + 3);
104 testRuntimeSpan(std::span<int, 4>(iArr2 + 4, 4), iArr2 + 4);
105
106 std::string s;
107 testRuntimeSpan(std::span<std::string>(&s, 1), &s);
108 testRuntimeSpan(std::span<std::string, 1>(&s, 1), &s);
109
110 return 0;
111}
112

source code of libcxx/test/std/containers/views/views.span/span.elem/data.pass.cpp