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