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

source code of libcxx/test/std/containers/views/views.span/span.iterators/end.pass.cpp