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
9 // XFAIL: c++03
10
11 // <iterator>
12 // template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
13 // template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
14 // template <class C> constexpr auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14
15 // template <class C> constexpr auto cend(const C& c) -> decltype(std::end(c)); // C++14
16 // template <class C> constexpr auto end (C& c) -> decltype(c.end());
17 // template <class C> constexpr auto end (const C& c) -> decltype(c.end());
18 // template <class E> constexpr reverse_iterator<const E*> rbegin(initializer_list<E> il);
19 // template <class E> constexpr reverse_iterator<const E*> rend (initializer_list<E> il);
20 //
21 // template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
22 // template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
23 // template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
24 // template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
25 // template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
26 // template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
27 // template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
28 // template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
29 //
30 // All of these are constexpr in C++17
31
32 #include "test_macros.h"
33
34 #include <iterator>
35 #include <cassert>
36 #include <vector>
37 #include <array>
38 #include <list>
39 #include <initializer_list>
40
41 // std::array is explicitly allowed to be initialized with A a = { init-list };.
42 // Disable the missing braces warning for this reason.
43 #include "disable_missing_braces_warning.h"
44
45 template<typename C>
test_const_container(const C & c,typename C::value_type val)46 void test_const_container( const C & c, typename C::value_type val ) {
47 assert ( std::begin(c) == c.begin());
48 assert (*std::begin(c) == val );
49 assert ( std::begin(c) != c.end());
50 assert ( std::end(c) == c.end());
51 #if TEST_STD_VER > 11
52 assert ( std::cbegin(c) == c.cbegin());
53 assert ( std::cbegin(c) != c.cend());
54 assert ( std::cend(c) == c.cend());
55 assert ( std::rbegin(c) == c.rbegin());
56 assert ( std::rbegin(c) != c.rend());
57 assert ( std::rend(c) == c.rend());
58 assert ( std::crbegin(c) == c.crbegin());
59 assert ( std::crbegin(c) != c.crend());
60 assert ( std::crend(c) == c.crend());
61 #endif
62 }
63
64 template<typename T>
test_const_container(const std::initializer_list<T> & c,T val)65 void test_const_container( const std::initializer_list<T> & c, T val ) {
66 assert ( std::begin(c) == c.begin());
67 assert (*std::begin(c) == val );
68 assert ( std::begin(c) != c.end());
69 assert ( std::end(c) == c.end());
70 #if TEST_STD_VER > 11
71 // initializer_list doesn't have cbegin/cend/rbegin/rend
72 // but std::cbegin(),etc work (b/c they're general fn templates)
73 // assert ( std::cbegin(c) == c.cbegin());
74 // assert ( std::cbegin(c) != c.cend());
75 // assert ( std::cend(c) == c.cend());
76 // assert ( std::rbegin(c) == c.rbegin());
77 // assert ( std::rbegin(c) != c.rend());
78 // assert ( std::rend(c) == c.rend());
79 // assert ( std::crbegin(c) == c.crbegin());
80 // assert ( std::crbegin(c) != c.crend());
81 // assert ( std::crend(c) == c.crend());
82 #endif
83 }
84
85 template<typename C>
test_container(C & c,typename C::value_type val)86 void test_container( C & c, typename C::value_type val ) {
87 assert ( std::begin(c) == c.begin());
88 assert (*std::begin(c) == val );
89 assert ( std::begin(c) != c.end());
90 assert ( std::end(c) == c.end());
91 #if TEST_STD_VER > 11
92 assert ( std::cbegin(c) == c.cbegin());
93 assert ( std::cbegin(c) != c.cend());
94 assert ( std::cend(c) == c.cend());
95 assert ( std::rbegin(c) == c.rbegin());
96 assert ( std::rbegin(c) != c.rend());
97 assert ( std::rend(c) == c.rend());
98 assert ( std::crbegin(c) == c.crbegin());
99 assert ( std::crbegin(c) != c.crend());
100 assert ( std::crend(c) == c.crend());
101 #endif
102 }
103
104 template<typename T>
test_container(std::initializer_list<T> & c,T val)105 void test_container( std::initializer_list<T> & c, T val ) {
106 assert ( std::begin(c) == c.begin());
107 assert (*std::begin(c) == val );
108 assert ( std::begin(c) != c.end());
109 assert ( std::end(c) == c.end());
110 #if TEST_STD_VER > 11
111 // initializer_list doesn't have cbegin/cend/rbegin/rend
112 // assert ( std::cbegin(c) == c.cbegin());
113 // assert ( std::cbegin(c) != c.cend());
114 // assert ( std::cend(c) == c.cend());
115 // assert ( std::rbegin(c) == c.rbegin());
116 // assert ( std::rbegin(c) != c.rend());
117 // assert ( std::rend(c) == c.rend());
118 // assert ( std::crbegin(c) == c.crbegin());
119 // assert ( std::crbegin(c) != c.crend());
120 // assert ( std::crend(c) == c.crend());
121 #endif
122 }
123
124 template<typename T, size_t Sz>
test_const_array(const T (& array)[Sz])125 void test_const_array( const T (&array)[Sz] ) {
126 assert ( std::begin(array) == array );
127 assert (*std::begin(array) == array[0] );
128 assert ( std::begin(array) != std::end(array));
129 assert ( std::end(array) == array + Sz);
130 #if TEST_STD_VER > 11
131 assert ( std::cbegin(array) == array );
132 assert (*std::cbegin(array) == array[0] );
133 assert ( std::cbegin(array) != std::cend(array));
134 assert ( std::cend(array) == array + Sz);
135 #endif
136 }
137
main(int,char **)138 int main(int, char**) {
139 std::vector<int> v; v.push_back(1);
140 std::list<int> l; l.push_back(2);
141 std::array<int, 1> a; a[0] = 3;
142 std::initializer_list<int> il = { 4 };
143
144 test_container ( v, 1 );
145 test_container ( l, 2 );
146 test_container ( a, 3 );
147 test_container ( il, 4 );
148
149 test_const_container ( v, 1 );
150 test_const_container ( l, 2 );
151 test_const_container ( a, 3 );
152 test_const_container ( il, 4 );
153
154 static constexpr int arrA [] { 1, 2, 3 };
155 test_const_array ( arrA );
156 #if TEST_STD_VER > 11
157 constexpr const int *b = std::cbegin(arrA);
158 constexpr const int *e = std::cend(arrA);
159 static_assert(e - b == 3, "");
160 #endif
161
162 #if TEST_STD_VER > 14
163 {
164 typedef std::array<int, 5> C;
165 constexpr const C c{0,1,2,3,4};
166
167 static_assert ( c.begin() == std::begin(c), "");
168 static_assert ( c.cbegin() == std::cbegin(c), "");
169 static_assert ( c.end() == std::end(c), "");
170 static_assert ( c.cend() == std::cend(c), "");
171
172 static_assert ( c.rbegin() == std::rbegin(c), "");
173 static_assert ( c.crbegin() == std::crbegin(c), "");
174 static_assert ( c.rend() == std::rend(c), "");
175 static_assert ( c.crend() == std::crend(c), "");
176
177 static_assert ( std::begin(c) != std::end(c), "");
178 static_assert ( std::rbegin(c) != std::rend(c), "");
179 static_assert ( std::cbegin(c) != std::cend(c), "");
180 static_assert ( std::crbegin(c) != std::crend(c), "");
181
182 static_assert ( *c.begin() == 0, "");
183 static_assert ( *c.rbegin() == 4, "");
184
185 static_assert ( *std::begin(c) == 0, "" );
186 static_assert ( *std::cbegin(c) == 0, "" );
187 static_assert ( *std::rbegin(c) == 4, "" );
188 static_assert ( *std::crbegin(c) == 4, "" );
189 }
190
191 {
192 static constexpr const int c[] = {0,1,2,3,4};
193
194 static_assert ( *std::begin(c) == 0, "" );
195 static_assert ( *std::cbegin(c) == 0, "" );
196 static_assert ( *std::rbegin(c) == 4, "" );
197 static_assert ( *std::crbegin(c) == 4, "" );
198 }
199 #endif
200
201 return 0;
202 }
203