1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <iterator>
11 // template <class C> auto begin(C& c) -> decltype(c.begin());
12 // template <class C> auto begin(const C& c) -> decltype(c.begin());
13 // template <class C> auto end(C& c) -> decltype(c.end());
14 // template <class C> auto end(const C& c) -> decltype(c.end());
15 // template <class E> reverse_iterator<const E*> rbegin(initializer_list<E> il);
16 // template <class E> reverse_iterator<const E*> rend(initializer_list<E> il);
17 
18 #if __cplusplus >= 201103L
19 #include <iterator>
20 #include <cassert>
21 #include <vector>
22 #include <array>
23 #include <list>
24 #include <initializer_list>
25 
26 template<typename C>
test_const_container(const C & c,typename C::value_type val)27 void test_const_container( const C & c, typename C::value_type val ) {
28     assert ( std::begin(c)   == c.begin());
29     assert (*std::begin(c)   ==  val );
30     assert ( std::begin(c)   != c.end());
31     assert ( std::end(c)     == c.end());
32 #if _LIBCPP_STD_VER > 11
33     assert ( std::cbegin(c)  == c.cbegin());
34     assert ( std::cbegin(c)  != c.cend());
35     assert ( std::cend(c)    == c.cend());
36     assert ( std::rbegin(c)  == c.rbegin());
37     assert ( std::rbegin(c)  != c.rend());
38     assert ( std::rend(c)    == c.rend());
39     assert ( std::crbegin(c) == c.crbegin());
40     assert ( std::crbegin(c) != c.crend());
41     assert ( std::crend(c)   == c.crend());
42 #endif
43     }
44 
45 template<typename T>
test_const_container(const std::initializer_list<T> & c,T val)46 void test_const_container( const std::initializer_list<T> & c, T 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 _LIBCPP_STD_VER > 11
52 //  initializer_list doesn't have cbegin/cend/rbegin/rend
53 //  but std::cbegin(),etc work (b/c they're general fn templates)
54 //     assert ( std::cbegin(c)  == c.cbegin());
55 //     assert ( std::cbegin(c)  != c.cend());
56 //     assert ( std::cend(c)    == c.cend());
57 //     assert ( std::rbegin(c)  == c.rbegin());
58 //     assert ( std::rbegin(c)  != c.rend());
59 //     assert ( std::rend(c)    == c.rend());
60 //     assert ( std::crbegin(c) == c.crbegin());
61 //     assert ( std::crbegin(c) != c.crend());
62 //     assert ( std::crend(c)   == c.crend());
63 #endif
64     }
65 
66 template<typename C>
test_container(C & c,typename C::value_type val)67 void test_container( C & c, typename C::value_type val ) {
68     assert ( std::begin(c)   == c.begin());
69     assert (*std::begin(c)   ==  val );
70     assert ( std::begin(c)   != c.end());
71     assert ( std::end(c)     == c.end());
72 #if _LIBCPP_STD_VER > 11
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 T>
test_container(std::initializer_list<T> & c,T val)86 void test_container( std::initializer_list<T> & c, T 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 _LIBCPP_STD_VER > 11
92 //  initializer_list doesn't have cbegin/cend/rbegin/rend
93 //     assert ( std::cbegin(c)  == c.cbegin());
94 //     assert ( std::cbegin(c)  != c.cend());
95 //     assert ( std::cend(c)    == c.cend());
96 //     assert ( std::rbegin(c)  == c.rbegin());
97 //     assert ( std::rbegin(c)  != c.rend());
98 //     assert ( std::rend(c)    == c.rend());
99 //     assert ( std::crbegin(c) == c.crbegin());
100 //     assert ( std::crbegin(c) != c.crend());
101 //     assert ( std::crend(c)   == c.crend());
102 #endif
103     }
104 
105 template<typename T, size_t Sz>
test_const_array(const T (& array)[Sz])106 void test_const_array( const T (&array)[Sz] ) {
107     assert ( std::begin(array)  == array );
108     assert (*std::begin(array)  ==  array[0] );
109     assert ( std::begin(array)  != std::end(array));
110     assert ( std::end(array)    == array + Sz);
111 #if _LIBCPP_STD_VER > 11
112     assert ( std::cbegin(array) == array );
113     assert (*std::cbegin(array) == array[0] );
114     assert ( std::cbegin(array) != std::cend(array));
115     assert ( std::cend(array)   == array + Sz);
116 #endif
117     }
118 
main()119 int main(){
120     std::vector<int> v; v.push_back(1);
121     std::list<int> l;   l.push_back(2);
122     std::array<int, 1> a; a[0] = 3;
123     std::initializer_list<int> il = { 4 };
124 
125     test_container ( v, 1 );
126     test_container ( l, 2 );
127     test_container ( a, 3 );
128     test_container ( il, 4 );
129 
130     test_const_container ( v, 1 );
131     test_const_container ( l, 2 );
132     test_const_container ( a, 3 );
133     test_const_container ( il, 4 );
134 
135     static constexpr int arrA [] { 1, 2, 3 };
136     test_const_array ( arrA );
137 #if _LIBCPP_STD_VER > 11
138     constexpr const int *b = std::cbegin(arrA);
139     constexpr const int *e = std::cend(arrA);
140     static_assert(e - b == 3, "");
141 #endif
142 }
143 
144 #else
main()145 int main(){}
146 #endif
147