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 // XFAIL: c++03, c++98 11 12 // <iterator> 13 // template <class C> auto begin(C& c) -> decltype(c.begin()); 14 // template <class C> auto begin(const C& c) -> decltype(c.begin()); 15 // template <class C> auto end(C& c) -> decltype(c.end()); 16 // template <class C> auto end(const C& c) -> decltype(c.end()); 17 // template <class E> reverse_iterator<const E*> rbegin(initializer_list<E> il); 18 // template <class E> reverse_iterator<const E*> rend(initializer_list<E> il); 19 20 #include "test_macros.h" 21 22 #include <iterator> 23 #include <cassert> 24 #include <vector> 25 #include <array> 26 #include <list> 27 #include <initializer_list> 28 29 template<typename C> 30 void test_const_container( const C & c, typename C::value_type val ) { 31 assert ( std::begin(c) == c.begin()); 32 assert (*std::begin(c) == val ); 33 assert ( std::begin(c) != c.end()); 34 assert ( std::end(c) == c.end()); 35 #if _LIBCPP_STD_VER > 11 36 assert ( std::cbegin(c) == c.cbegin()); 37 assert ( std::cbegin(c) != c.cend()); 38 assert ( std::cend(c) == c.cend()); 39 assert ( std::rbegin(c) == c.rbegin()); 40 assert ( std::rbegin(c) != c.rend()); 41 assert ( std::rend(c) == c.rend()); 42 assert ( std::crbegin(c) == c.crbegin()); 43 assert ( std::crbegin(c) != c.crend()); 44 assert ( std::crend(c) == c.crend()); 45 #endif 46 } 47 48 template<typename T> 49 void test_const_container( const std::initializer_list<T> & c, T val ) { 50 assert ( std::begin(c) == c.begin()); 51 assert (*std::begin(c) == val ); 52 assert ( std::begin(c) != c.end()); 53 assert ( std::end(c) == c.end()); 54 #if _LIBCPP_STD_VER > 11 55 // initializer_list doesn't have cbegin/cend/rbegin/rend 56 // but std::cbegin(),etc work (b/c they're general fn templates) 57 // assert ( std::cbegin(c) == c.cbegin()); 58 // assert ( std::cbegin(c) != c.cend()); 59 // assert ( std::cend(c) == c.cend()); 60 // assert ( std::rbegin(c) == c.rbegin()); 61 // assert ( std::rbegin(c) != c.rend()); 62 // assert ( std::rend(c) == c.rend()); 63 // assert ( std::crbegin(c) == c.crbegin()); 64 // assert ( std::crbegin(c) != c.crend()); 65 // assert ( std::crend(c) == c.crend()); 66 #endif 67 } 68 69 template<typename C> 70 void test_container( C & c, typename C::value_type val ) { 71 assert ( std::begin(c) == c.begin()); 72 assert (*std::begin(c) == val ); 73 assert ( std::begin(c) != c.end()); 74 assert ( std::end(c) == c.end()); 75 #if _LIBCPP_STD_VER > 11 76 assert ( std::cbegin(c) == c.cbegin()); 77 assert ( std::cbegin(c) != c.cend()); 78 assert ( std::cend(c) == c.cend()); 79 assert ( std::rbegin(c) == c.rbegin()); 80 assert ( std::rbegin(c) != c.rend()); 81 assert ( std::rend(c) == c.rend()); 82 assert ( std::crbegin(c) == c.crbegin()); 83 assert ( std::crbegin(c) != c.crend()); 84 assert ( std::crend(c) == c.crend()); 85 #endif 86 } 87 88 template<typename T> 89 void test_container( std::initializer_list<T> & c, T val ) { 90 assert ( std::begin(c) == c.begin()); 91 assert (*std::begin(c) == val ); 92 assert ( std::begin(c) != c.end()); 93 assert ( std::end(c) == c.end()); 94 #if _LIBCPP_STD_VER > 11 95 // initializer_list doesn't have cbegin/cend/rbegin/rend 96 // assert ( std::cbegin(c) == c.cbegin()); 97 // assert ( std::cbegin(c) != c.cend()); 98 // assert ( std::cend(c) == c.cend()); 99 // assert ( std::rbegin(c) == c.rbegin()); 100 // assert ( std::rbegin(c) != c.rend()); 101 // assert ( std::rend(c) == c.rend()); 102 // assert ( std::crbegin(c) == c.crbegin()); 103 // assert ( std::crbegin(c) != c.crend()); 104 // assert ( std::crend(c) == c.crend()); 105 #endif 106 } 107 108 template<typename T, size_t Sz> 109 void test_const_array( const T (&array)[Sz] ) { 110 assert ( std::begin(array) == array ); 111 assert (*std::begin(array) == array[0] ); 112 assert ( std::begin(array) != std::end(array)); 113 assert ( std::end(array) == array + Sz); 114 #if _LIBCPP_STD_VER > 11 115 assert ( std::cbegin(array) == array ); 116 assert (*std::cbegin(array) == array[0] ); 117 assert ( std::cbegin(array) != std::cend(array)); 118 assert ( std::cend(array) == array + Sz); 119 #endif 120 } 121 122 int main(){ 123 std::vector<int> v; v.push_back(1); 124 std::list<int> l; l.push_back(2); 125 std::array<int, 1> a; a[0] = 3; 126 std::initializer_list<int> il = { 4 }; 127 128 test_container ( v, 1 ); 129 test_container ( l, 2 ); 130 test_container ( a, 3 ); 131 test_container ( il, 4 ); 132 133 test_const_container ( v, 1 ); 134 test_const_container ( l, 2 ); 135 test_const_container ( a, 3 ); 136 test_const_container ( il, 4 ); 137 138 static constexpr int arrA [] { 1, 2, 3 }; 139 test_const_array ( arrA ); 140 #if _LIBCPP_STD_VER > 11 141 constexpr const int *b = std::cbegin(arrA); 142 constexpr const int *e = std::cend(arrA); 143 static_assert(e - b == 3, ""); 144 #endif 145 } 146