xref: /llvm-project/libcxx/test/std/containers/views/views.span/span.iterators/begin.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       iterator  begin() const noexcept;
13 // constexpr const_iterator cbegin() const noexcept;
14 
15 #include <span>
16 #include <cassert>
17 #include <string>
18 
19 #include "test_macros.h"
20 
21 template <class Span>
testConstexprSpan(Span s)22 constexpr bool testConstexprSpan(Span s)
23 {
24     bool ret = true;
25     typename Span::iterator b = s.begin();
26 
27     if (s.empty())
28     {
29         ret = ret && (b == s.end());
30     }
31     else
32     {
33         ret = ret && ( *b ==  s[0]);
34         ret = ret && (&*b == &s[0]);
35     }
36     return ret;
37 }
38 
39 
40 template <class Span>
testRuntimeSpan(Span s)41 void testRuntimeSpan(Span s)
42 {
43     typename Span::iterator b = s.begin();
44 
45     if (s.empty())
46     {
47         assert(b == s.end());
48     }
49     else
50     {
51         assert( *b ==  s[0]);
52         assert(&*b == &s[0]);
53     }
54 }
55 
56 struct A{};
operator ==(A,A)57 bool operator==(A, A) {return true;}
58 
59 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
60           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
61 
62 
main(int,char **)63 int main(int, char**)
64 {
65     static_assert(testConstexprSpan(std::span<int>()),            "");
66     static_assert(testConstexprSpan(std::span<long>()),           "");
67     static_assert(testConstexprSpan(std::span<double>()),         "");
68     static_assert(testConstexprSpan(std::span<A>()),              "");
69     static_assert(testConstexprSpan(std::span<std::string>()),    "");
70 
71     static_assert(testConstexprSpan(std::span<int, 0>()),         "");
72     static_assert(testConstexprSpan(std::span<long, 0>()),        "");
73     static_assert(testConstexprSpan(std::span<double, 0>()),      "");
74     static_assert(testConstexprSpan(std::span<A, 0>()),           "");
75     static_assert(testConstexprSpan(std::span<std::string, 0>()), "");
76 
77     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)),    "");
78     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)),    "");
79     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)),    "");
80     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)),    "");
81     static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)),    "");
82 
83 
84     testRuntimeSpan(std::span<int>        ());
85     testRuntimeSpan(std::span<long>       ());
86     testRuntimeSpan(std::span<double>     ());
87     testRuntimeSpan(std::span<A>          ());
88     testRuntimeSpan(std::span<std::string>());
89 
90     testRuntimeSpan(std::span<int, 0>        ());
91     testRuntimeSpan(std::span<long, 0>       ());
92     testRuntimeSpan(std::span<double, 0>     ());
93     testRuntimeSpan(std::span<A, 0>          ());
94     testRuntimeSpan(std::span<std::string, 0>());
95 
96     testRuntimeSpan(std::span<int>(iArr2, 1));
97     testRuntimeSpan(std::span<int>(iArr2, 2));
98     testRuntimeSpan(std::span<int>(iArr2, 3));
99     testRuntimeSpan(std::span<int>(iArr2, 4));
100     testRuntimeSpan(std::span<int>(iArr2, 5));
101 
102     std::string s;
103     testRuntimeSpan(std::span<std::string>(&s, (std::size_t) 0));
104     testRuntimeSpan(std::span<std::string>(&s, 1));
105 
106   return 0;
107 }
108