xref: /llvm-project/libcxx/test/std/containers/views/views.span/span.elem/data.pass.cpp (revision a8cf78c73914f614d4ec339f7bd5bba8b20c3c29)
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 
16 #include <span>
17 #include <cassert>
18 #include <string>
19 
20 #include "test_macros.h"
21 
22 
23 template <typename Span>
testConstexprSpan(Span sp,typename Span::pointer ptr)24 constexpr bool testConstexprSpan(Span sp, typename Span::pointer ptr)
25 {
26     ASSERT_NOEXCEPT(sp.data());
27     return sp.data() == ptr;
28 }
29 
30 
31 template <typename Span>
testRuntimeSpan(Span sp,typename Span::pointer ptr)32 void testRuntimeSpan(Span sp, typename Span::pointer ptr)
33 {
34     ASSERT_NOEXCEPT(sp.data());
35     assert(sp.data() == ptr);
36 }
37 
38 struct A{};
39 constexpr 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 
main(int,char **)42 int main(int, char**)
43 {
44 
45 //  dynamic size
46     static_assert(testConstexprSpan(std::span<int>(), nullptr),         "");
47     static_assert(testConstexprSpan(std::span<long>(), nullptr),        "");
48     static_assert(testConstexprSpan(std::span<double>(), nullptr),      "");
49     static_assert(testConstexprSpan(std::span<A>(), nullptr),           "");
50     static_assert(testConstexprSpan(std::span<std::string>(), nullptr), "");
51 
52     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), iArr1), "");
53     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), iArr1), "");
54     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), iArr1), "");
55     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), iArr1), "");
56 
57     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 1, 1), iArr1 + 1), "");
58     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 2, 2), iArr1 + 2), "");
59     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 3, 3), iArr1 + 3), "");
60     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 4, 4), iArr1 + 4), "");
61 
62 //  static size
63     static_assert(testConstexprSpan(std::span<int, 0>(), nullptr),         "");
64     static_assert(testConstexprSpan(std::span<long, 0>(), nullptr),        "");
65     static_assert(testConstexprSpan(std::span<double, 0>(), nullptr),      "");
66     static_assert(testConstexprSpan(std::span<A, 0>(), nullptr),           "");
67     static_assert(testConstexprSpan(std::span<std::string, 0>(), nullptr), "");
68 
69     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1), iArr1), "");
70     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), iArr1), "");
71     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), iArr1), "");
72     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), iArr1), "");
73 
74     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1 + 1, 1), iArr1 + 1), "");
75     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1 + 2, 2), iArr1 + 2), "");
76     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1 + 3, 3), iArr1 + 3), "");
77     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1 + 4, 4), iArr1 + 4), "");
78 
79 
80 //  dynamic size
81     testRuntimeSpan(std::span<int>(), nullptr);
82     testRuntimeSpan(std::span<long>(), nullptr);
83     testRuntimeSpan(std::span<double>(), nullptr);
84     testRuntimeSpan(std::span<A>(), nullptr);
85     testRuntimeSpan(std::span<std::string>(), nullptr);
86 
87     testRuntimeSpan(std::span<int>(iArr2, 1), iArr2);
88     testRuntimeSpan(std::span<int>(iArr2, 2), iArr2);
89     testRuntimeSpan(std::span<int>(iArr2, 3), iArr2);
90     testRuntimeSpan(std::span<int>(iArr2, 4), iArr2);
91 
92     testRuntimeSpan(std::span<int>(iArr2 + 1, 1), iArr2 + 1);
93     testRuntimeSpan(std::span<int>(iArr2 + 2, 2), iArr2 + 2);
94     testRuntimeSpan(std::span<int>(iArr2 + 3, 3), iArr2 + 3);
95     testRuntimeSpan(std::span<int>(iArr2 + 4, 4), iArr2 + 4);
96 
97 //  static size
98     testRuntimeSpan(std::span<int, 0>(), nullptr);
99     testRuntimeSpan(std::span<long, 0>(), nullptr);
100     testRuntimeSpan(std::span<double, 0>(), nullptr);
101     testRuntimeSpan(std::span<A, 0>(), nullptr);
102     testRuntimeSpan(std::span<std::string, 0>(), nullptr);
103 
104     testRuntimeSpan(std::span<int, 1>(iArr2, 1), iArr2);
105     testRuntimeSpan(std::span<int, 2>(iArr2, 2), iArr2);
106     testRuntimeSpan(std::span<int, 3>(iArr2, 3), iArr2);
107     testRuntimeSpan(std::span<int, 4>(iArr2, 4), iArr2);
108 
109     testRuntimeSpan(std::span<int, 1>(iArr2 + 1, 1), iArr2 + 1);
110     testRuntimeSpan(std::span<int, 2>(iArr2 + 2, 2), iArr2 + 2);
111     testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3), iArr2 + 3);
112     testRuntimeSpan(std::span<int, 4>(iArr2 + 4, 4), iArr2 + 4);
113 
114 
115     std::string s;
116     testRuntimeSpan(std::span<std::string>(&s, 1), &s);
117     testRuntimeSpan(std::span<std::string, 1>(&s, 1), &s);
118 
119 
120   return 0;
121 }
122