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 // <vector> 10 11 // iterator begin(); 12 // iterator end(); 13 // const_iterator begin() const; 14 // const_iterator end() const; 15 // const_iterator cbegin() const; 16 // const_iterator cend() const; 17 18 #include <vector> 19 #include <cassert> 20 #include <iterator> 21 22 #include "test_macros.h" 23 #include "min_allocator.h" 24 25 struct A 26 { 27 int first; 28 int second; 29 }; 30 31 TEST_CONSTEXPR_CXX20 bool tests() 32 { 33 { 34 typedef int T; 35 typedef std::vector<T> C; 36 C c; 37 C::iterator i = c.begin(); 38 C::iterator j = c.end(); 39 assert(std::distance(i, j) == 0); 40 assert(i == j); 41 } 42 { 43 typedef int T; 44 typedef std::vector<T> C; 45 const C c; 46 C::const_iterator i = c.begin(); 47 C::const_iterator j = c.end(); 48 assert(std::distance(i, j) == 0); 49 assert(i == j); 50 } 51 { 52 typedef int T; 53 typedef std::vector<T> C; 54 C c; 55 C::const_iterator i = c.cbegin(); 56 C::const_iterator j = c.cend(); 57 assert(std::distance(i, j) == 0); 58 assert(i == j); 59 assert(i == c.end()); 60 } 61 { 62 typedef int T; 63 typedef std::vector<T> C; 64 const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 65 C c(std::begin(t), std::end(t)); 66 C::iterator i = c.begin(); 67 assert(*i == 0); 68 ++i; 69 assert(*i == 1); 70 *i = 10; 71 assert(*i == 10); 72 assert(std::distance(c.begin(), c.end()) == 10); 73 } 74 { 75 typedef int T; 76 typedef std::vector<T> C; 77 C::iterator i; 78 C::const_iterator j; 79 (void) i; 80 (void) j; 81 } 82 #if TEST_STD_VER >= 11 83 { 84 typedef int T; 85 typedef std::vector<T, min_allocator<T>> C; 86 C c; 87 C::iterator i = c.begin(); 88 C::iterator j = c.end(); 89 assert(std::distance(i, j) == 0); 90 91 assert(i == j); 92 assert(!(i != j)); 93 94 assert(!(i < j)); 95 assert((i <= j)); 96 97 assert(!(i > j)); 98 assert((i >= j)); 99 100 # if TEST_STD_VER >= 20 101 // P1614 + LWG3352 102 // When the allocator does not have operator<=> then the iterator uses a 103 // fallback to provide operator<=>. 104 // Make sure to test with an allocator that does not have operator<=>. 105 static_assert(!std::three_way_comparable<min_allocator<int>, std::strong_ordering>); 106 static_assert(std::three_way_comparable<typename C::iterator, std::strong_ordering>); 107 108 std::same_as<std::strong_ordering> decltype(auto) r1 = i <=> j; 109 assert(r1 == std::strong_ordering::equal); 110 # endif 111 } 112 { 113 typedef int T; 114 typedef std::vector<T, min_allocator<T>> C; 115 const C c; 116 C::const_iterator i = c.begin(); 117 C::const_iterator j = c.end(); 118 assert(std::distance(i, j) == 0); 119 120 assert(i == j); 121 assert(!(i != j)); 122 123 assert(!(i < j)); 124 assert((i <= j)); 125 126 assert(!(i > j)); 127 assert((i >= j)); 128 129 # if TEST_STD_VER >= 20 130 // When the allocator does not have operator<=> then the iterator uses a 131 // fallback to provide operator<=>. 132 // Make sure to test with an allocator that does not have operator<=>. 133 static_assert(!std::three_way_comparable<min_allocator<int>, std::strong_ordering>); 134 static_assert(std::three_way_comparable<typename C::iterator, std::strong_ordering>); 135 136 std::same_as<std::strong_ordering> decltype(auto) r1 = i <=> j; 137 assert(r1 == std::strong_ordering::equal); 138 # endif 139 } 140 { 141 typedef int T; 142 typedef std::vector<T, min_allocator<T>> C; 143 C c; 144 C::const_iterator i = c.cbegin(); 145 C::const_iterator j = c.cend(); 146 assert(std::distance(i, j) == 0); 147 assert(i == j); 148 assert(i == c.end()); 149 } 150 { 151 typedef int T; 152 typedef std::vector<T, min_allocator<T>> C; 153 const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 154 C c(std::begin(t), std::end(t)); 155 C::iterator i = c.begin(); 156 assert(*i == 0); 157 ++i; 158 assert(*i == 1); 159 *i = 10; 160 assert(*i == 10); 161 assert(std::distance(c.begin(), c.end()) == 10); 162 } 163 { 164 typedef int T; 165 typedef std::vector<T, min_allocator<T>> C; 166 C::iterator i; 167 C::const_iterator j; 168 (void) i; 169 (void) j; 170 } 171 { 172 typedef A T; 173 typedef std::vector<T, min_allocator<T>> C; 174 C c = {A{1, 2}}; 175 C::iterator i = c.begin(); 176 i->first = 3; 177 C::const_iterator j = i; 178 assert(j->first == 3); 179 } 180 #endif 181 #if TEST_STD_VER > 11 182 { // N3644 testing 183 typedef std::vector<int> C; 184 C::iterator ii1{}, ii2{}; 185 C::iterator ii4 = ii1; 186 C::const_iterator cii{}; 187 assert ( ii1 == ii2 ); 188 assert ( ii1 == ii4 ); 189 190 assert (!(ii1 != ii2 )); 191 192 assert ( (ii1 == cii )); 193 assert ( (cii == ii1 )); 194 assert (!(ii1 != cii )); 195 assert (!(cii != ii1 )); 196 assert (!(ii1 < cii )); 197 assert (!(cii < ii1 )); 198 assert ( (ii1 <= cii )); 199 assert ( (cii <= ii1 )); 200 assert (!(ii1 > cii )); 201 assert (!(cii > ii1 )); 202 assert ( (ii1 >= cii )); 203 assert ( (cii >= ii1 )); 204 assert (cii - ii1 == 0); 205 assert (ii1 - cii == 0); 206 # if TEST_STD_VER >= 20 207 // P1614 + LWG3352 208 std::same_as<std::strong_ordering> decltype(auto) r1 = ii1 <=> ii2; 209 assert(r1 == std::strong_ordering::equal); 210 211 std::same_as<std::strong_ordering> decltype(auto) r2 = cii <=> ii2; 212 assert(r2 == std::strong_ordering::equal); 213 # endif // TEST_STD_VER > 20 214 } 215 #endif // TEST_STD_VER > 11 216 217 return true; 218 } 219 220 int main(int, char**) 221 { 222 tests(); 223 #if TEST_STD_VER > 17 224 static_assert(tests()); 225 #endif 226 return 0; 227 } 228