• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===------------------------------ span ---------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===---------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
11 
12 // <span>
13 
14 // constexpr pointer data() const noexcept;
15 //
16 
17 
18 #include <span>
19 #include <cassert>
20 #include <string>
21 
22 #include "test_macros.h"
23 
24 
25 template <typename Span>
testConstexprSpan(Span sp,typename Span::pointer ptr)26 constexpr bool testConstexprSpan(Span sp, typename Span::pointer ptr)
27 {
28     ASSERT_NOEXCEPT(sp.data());
29     return sp.data() == ptr;
30 }
31 
32 
33 template <typename Span>
testRuntimeSpan(Span sp,typename Span::pointer ptr)34 void testRuntimeSpan(Span sp, typename Span::pointer ptr)
35 {
36     ASSERT_NOEXCEPT(sp.data());
37     assert(sp.data() == ptr);
38 }
39 
40 struct A{};
41 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
42           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
43 
main()44 int main ()
45 {
46 
47 //  dynamic size
48     static_assert(testConstexprSpan(std::span<int>(), nullptr),         "");
49     static_assert(testConstexprSpan(std::span<long>(), nullptr),        "");
50     static_assert(testConstexprSpan(std::span<double>(), nullptr),      "");
51     static_assert(testConstexprSpan(std::span<A>(), nullptr),           "");
52     static_assert(testConstexprSpan(std::span<std::string>(), nullptr), "");
53 
54     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), iArr1), "");
55     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), iArr1), "");
56     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), iArr1), "");
57     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), iArr1), "");
58 
59     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 1, 1), iArr1 + 1), "");
60     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 2, 2), iArr1 + 2), "");
61     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 3, 3), iArr1 + 3), "");
62     static_assert(testConstexprSpan(std::span<const int>(iArr1 + 4, 4), iArr1 + 4), "");
63 
64 //  static size
65     static_assert(testConstexprSpan(std::span<int, 0>(), nullptr),         "");
66     static_assert(testConstexprSpan(std::span<long, 0>(), nullptr),        "");
67     static_assert(testConstexprSpan(std::span<double, 0>(), nullptr),      "");
68     static_assert(testConstexprSpan(std::span<A, 0>(), nullptr),           "");
69     static_assert(testConstexprSpan(std::span<std::string, 0>(), nullptr), "");
70 
71     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1), iArr1), "");
72     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), iArr1), "");
73     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), iArr1), "");
74     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), iArr1), "");
75 
76     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1 + 1, 1), iArr1 + 1), "");
77     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1 + 2, 2), iArr1 + 2), "");
78     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1 + 3, 3), iArr1 + 3), "");
79     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1 + 4, 4), iArr1 + 4), "");
80 
81 
82 //  dynamic size
83     testRuntimeSpan(std::span<int>(), nullptr);
84     testRuntimeSpan(std::span<long>(), nullptr);
85     testRuntimeSpan(std::span<double>(), nullptr);
86     testRuntimeSpan(std::span<A>(), nullptr);
87     testRuntimeSpan(std::span<std::string>(), nullptr);
88 
89     testRuntimeSpan(std::span<int>(iArr2, 1), iArr2);
90     testRuntimeSpan(std::span<int>(iArr2, 2), iArr2);
91     testRuntimeSpan(std::span<int>(iArr2, 3), iArr2);
92     testRuntimeSpan(std::span<int>(iArr2, 4), iArr2);
93 
94     testRuntimeSpan(std::span<int>(iArr2 + 1, 1), iArr2 + 1);
95     testRuntimeSpan(std::span<int>(iArr2 + 2, 2), iArr2 + 2);
96     testRuntimeSpan(std::span<int>(iArr2 + 3, 3), iArr2 + 3);
97     testRuntimeSpan(std::span<int>(iArr2 + 4, 4), iArr2 + 4);
98 
99 //  static size
100     testRuntimeSpan(std::span<int, 0>(), nullptr);
101     testRuntimeSpan(std::span<long, 0>(), nullptr);
102     testRuntimeSpan(std::span<double, 0>(), nullptr);
103     testRuntimeSpan(std::span<A, 0>(), nullptr);
104     testRuntimeSpan(std::span<std::string, 0>(), nullptr);
105 
106     testRuntimeSpan(std::span<int, 1>(iArr2, 1), iArr2);
107     testRuntimeSpan(std::span<int, 2>(iArr2, 2), iArr2);
108     testRuntimeSpan(std::span<int, 3>(iArr2, 3), iArr2);
109     testRuntimeSpan(std::span<int, 4>(iArr2, 4), iArr2);
110 
111     testRuntimeSpan(std::span<int, 1>(iArr2 + 1, 1), iArr2 + 1);
112     testRuntimeSpan(std::span<int, 2>(iArr2 + 2, 2), iArr2 + 2);
113     testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3), iArr2 + 3);
114     testRuntimeSpan(std::span<int, 4>(iArr2 + 4, 4), iArr2 + 4);
115 
116 
117     std::string s;
118     testRuntimeSpan(std::span<std::string>(&s, 1), &s);
119     testRuntimeSpan(std::span<std::string, 1>(&s, 1), &s);
120 
121 }
122