| 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 | // <set> |
| 10 | |
| 11 | // class set |
| 12 | |
| 13 | // pair<iterator,iterator> equal_range(const key_type& k); |
| 14 | // pair<const_iterator,const_iterator> equal_range(const key_type& k) const; |
| 15 | |
| 16 | #include <set> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "min_allocator.h" |
| 21 | #include "private_constructor.h" |
| 22 | |
| 23 | int main(int, char**) { |
| 24 | { |
| 25 | typedef int V; |
| 26 | typedef std::set<int> M; |
| 27 | { |
| 28 | typedef std::pair<M::iterator, M::iterator> R; |
| 29 | V ar[] = {5, 7, 9, 11, 13, 15, 17, 19}; |
| 30 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 31 | R r = m.equal_range(x: 5); |
| 32 | assert(r.first == std::next(m.begin(), 0)); |
| 33 | assert(r.second == std::next(m.begin(), 1)); |
| 34 | r = m.equal_range(x: 7); |
| 35 | assert(r.first == std::next(m.begin(), 1)); |
| 36 | assert(r.second == std::next(m.begin(), 2)); |
| 37 | r = m.equal_range(x: 9); |
| 38 | assert(r.first == std::next(m.begin(), 2)); |
| 39 | assert(r.second == std::next(m.begin(), 3)); |
| 40 | r = m.equal_range(x: 11); |
| 41 | assert(r.first == std::next(m.begin(), 3)); |
| 42 | assert(r.second == std::next(m.begin(), 4)); |
| 43 | r = m.equal_range(x: 13); |
| 44 | assert(r.first == std::next(m.begin(), 4)); |
| 45 | assert(r.second == std::next(m.begin(), 5)); |
| 46 | r = m.equal_range(x: 15); |
| 47 | assert(r.first == std::next(m.begin(), 5)); |
| 48 | assert(r.second == std::next(m.begin(), 6)); |
| 49 | r = m.equal_range(x: 17); |
| 50 | assert(r.first == std::next(m.begin(), 6)); |
| 51 | assert(r.second == std::next(m.begin(), 7)); |
| 52 | r = m.equal_range(x: 19); |
| 53 | assert(r.first == std::next(m.begin(), 7)); |
| 54 | assert(r.second == std::next(m.begin(), 8)); |
| 55 | r = m.equal_range(x: 4); |
| 56 | assert(r.first == std::next(m.begin(), 0)); |
| 57 | assert(r.second == std::next(m.begin(), 0)); |
| 58 | r = m.equal_range(x: 6); |
| 59 | assert(r.first == std::next(m.begin(), 1)); |
| 60 | assert(r.second == std::next(m.begin(), 1)); |
| 61 | r = m.equal_range(x: 8); |
| 62 | assert(r.first == std::next(m.begin(), 2)); |
| 63 | assert(r.second == std::next(m.begin(), 2)); |
| 64 | r = m.equal_range(x: 10); |
| 65 | assert(r.first == std::next(m.begin(), 3)); |
| 66 | assert(r.second == std::next(m.begin(), 3)); |
| 67 | r = m.equal_range(x: 12); |
| 68 | assert(r.first == std::next(m.begin(), 4)); |
| 69 | assert(r.second == std::next(m.begin(), 4)); |
| 70 | r = m.equal_range(x: 14); |
| 71 | assert(r.first == std::next(m.begin(), 5)); |
| 72 | assert(r.second == std::next(m.begin(), 5)); |
| 73 | r = m.equal_range(x: 16); |
| 74 | assert(r.first == std::next(m.begin(), 6)); |
| 75 | assert(r.second == std::next(m.begin(), 6)); |
| 76 | r = m.equal_range(x: 18); |
| 77 | assert(r.first == std::next(m.begin(), 7)); |
| 78 | assert(r.second == std::next(m.begin(), 7)); |
| 79 | r = m.equal_range(x: 20); |
| 80 | assert(r.first == std::next(m.begin(), 8)); |
| 81 | assert(r.second == std::next(m.begin(), 8)); |
| 82 | } |
| 83 | { |
| 84 | typedef std::pair<M::const_iterator, M::const_iterator> R; |
| 85 | V ar[] = {5, 7, 9, 11, 13, 15, 17, 19}; |
| 86 | const M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 87 | R r = m.equal_range(x: 5); |
| 88 | assert(r.first == std::next(m.begin(), 0)); |
| 89 | assert(r.second == std::next(m.begin(), 1)); |
| 90 | r = m.equal_range(x: 7); |
| 91 | assert(r.first == std::next(m.begin(), 1)); |
| 92 | assert(r.second == std::next(m.begin(), 2)); |
| 93 | r = m.equal_range(x: 9); |
| 94 | assert(r.first == std::next(m.begin(), 2)); |
| 95 | assert(r.second == std::next(m.begin(), 3)); |
| 96 | r = m.equal_range(x: 11); |
| 97 | assert(r.first == std::next(m.begin(), 3)); |
| 98 | assert(r.second == std::next(m.begin(), 4)); |
| 99 | r = m.equal_range(x: 13); |
| 100 | assert(r.first == std::next(m.begin(), 4)); |
| 101 | assert(r.second == std::next(m.begin(), 5)); |
| 102 | r = m.equal_range(x: 15); |
| 103 | assert(r.first == std::next(m.begin(), 5)); |
| 104 | assert(r.second == std::next(m.begin(), 6)); |
| 105 | r = m.equal_range(x: 17); |
| 106 | assert(r.first == std::next(m.begin(), 6)); |
| 107 | assert(r.second == std::next(m.begin(), 7)); |
| 108 | r = m.equal_range(x: 19); |
| 109 | assert(r.first == std::next(m.begin(), 7)); |
| 110 | assert(r.second == std::next(m.begin(), 8)); |
| 111 | r = m.equal_range(x: 4); |
| 112 | assert(r.first == std::next(m.begin(), 0)); |
| 113 | assert(r.second == std::next(m.begin(), 0)); |
| 114 | r = m.equal_range(x: 6); |
| 115 | assert(r.first == std::next(m.begin(), 1)); |
| 116 | assert(r.second == std::next(m.begin(), 1)); |
| 117 | r = m.equal_range(x: 8); |
| 118 | assert(r.first == std::next(m.begin(), 2)); |
| 119 | assert(r.second == std::next(m.begin(), 2)); |
| 120 | r = m.equal_range(x: 10); |
| 121 | assert(r.first == std::next(m.begin(), 3)); |
| 122 | assert(r.second == std::next(m.begin(), 3)); |
| 123 | r = m.equal_range(x: 12); |
| 124 | assert(r.first == std::next(m.begin(), 4)); |
| 125 | assert(r.second == std::next(m.begin(), 4)); |
| 126 | r = m.equal_range(x: 14); |
| 127 | assert(r.first == std::next(m.begin(), 5)); |
| 128 | assert(r.second == std::next(m.begin(), 5)); |
| 129 | r = m.equal_range(x: 16); |
| 130 | assert(r.first == std::next(m.begin(), 6)); |
| 131 | assert(r.second == std::next(m.begin(), 6)); |
| 132 | r = m.equal_range(x: 18); |
| 133 | assert(r.first == std::next(m.begin(), 7)); |
| 134 | assert(r.second == std::next(m.begin(), 7)); |
| 135 | r = m.equal_range(x: 20); |
| 136 | assert(r.first == std::next(m.begin(), 8)); |
| 137 | assert(r.second == std::next(m.begin(), 8)); |
| 138 | } |
| 139 | } |
| 140 | #if TEST_STD_VER >= 11 |
| 141 | { |
| 142 | typedef int V; |
| 143 | typedef std::set<int, std::less<int>, min_allocator<int>> M; |
| 144 | typedef std::pair<M::iterator, M::iterator> R; |
| 145 | V ar[] = {5, 7, 9, 11, 13, 15, 17, 19}; |
| 146 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 147 | R r = m.equal_range(5); |
| 148 | assert(r.first == std::next(m.begin(), 0)); |
| 149 | assert(r.second == std::next(m.begin(), 1)); |
| 150 | r = m.equal_range(7); |
| 151 | assert(r.first == std::next(m.begin(), 1)); |
| 152 | assert(r.second == std::next(m.begin(), 2)); |
| 153 | r = m.equal_range(9); |
| 154 | assert(r.first == std::next(m.begin(), 2)); |
| 155 | assert(r.second == std::next(m.begin(), 3)); |
| 156 | r = m.equal_range(11); |
| 157 | assert(r.first == std::next(m.begin(), 3)); |
| 158 | assert(r.second == std::next(m.begin(), 4)); |
| 159 | r = m.equal_range(13); |
| 160 | assert(r.first == std::next(m.begin(), 4)); |
| 161 | assert(r.second == std::next(m.begin(), 5)); |
| 162 | r = m.equal_range(15); |
| 163 | assert(r.first == std::next(m.begin(), 5)); |
| 164 | assert(r.second == std::next(m.begin(), 6)); |
| 165 | r = m.equal_range(17); |
| 166 | assert(r.first == std::next(m.begin(), 6)); |
| 167 | assert(r.second == std::next(m.begin(), 7)); |
| 168 | r = m.equal_range(19); |
| 169 | assert(r.first == std::next(m.begin(), 7)); |
| 170 | assert(r.second == std::next(m.begin(), 8)); |
| 171 | r = m.equal_range(4); |
| 172 | assert(r.first == std::next(m.begin(), 0)); |
| 173 | assert(r.second == std::next(m.begin(), 0)); |
| 174 | r = m.equal_range(6); |
| 175 | assert(r.first == std::next(m.begin(), 1)); |
| 176 | assert(r.second == std::next(m.begin(), 1)); |
| 177 | r = m.equal_range(8); |
| 178 | assert(r.first == std::next(m.begin(), 2)); |
| 179 | assert(r.second == std::next(m.begin(), 2)); |
| 180 | r = m.equal_range(10); |
| 181 | assert(r.first == std::next(m.begin(), 3)); |
| 182 | assert(r.second == std::next(m.begin(), 3)); |
| 183 | r = m.equal_range(12); |
| 184 | assert(r.first == std::next(m.begin(), 4)); |
| 185 | assert(r.second == std::next(m.begin(), 4)); |
| 186 | r = m.equal_range(14); |
| 187 | assert(r.first == std::next(m.begin(), 5)); |
| 188 | assert(r.second == std::next(m.begin(), 5)); |
| 189 | r = m.equal_range(16); |
| 190 | assert(r.first == std::next(m.begin(), 6)); |
| 191 | assert(r.second == std::next(m.begin(), 6)); |
| 192 | r = m.equal_range(18); |
| 193 | assert(r.first == std::next(m.begin(), 7)); |
| 194 | assert(r.second == std::next(m.begin(), 7)); |
| 195 | r = m.equal_range(20); |
| 196 | assert(r.first == std::next(m.begin(), 8)); |
| 197 | assert(r.second == std::next(m.begin(), 8)); |
| 198 | } |
| 199 | #endif |
| 200 | #if TEST_STD_VER > 11 |
| 201 | { |
| 202 | typedef int V; |
| 203 | typedef std::set<V, std::less<>> M; |
| 204 | { |
| 205 | typedef std::pair<M::iterator, M::iterator> R; |
| 206 | V ar[] = {5, 7, 9, 11, 13, 15, 17, 19}; |
| 207 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 208 | R r = m.equal_range(5); |
| 209 | assert(r.first == std::next(m.begin(), 0)); |
| 210 | assert(r.second == std::next(m.begin(), 1)); |
| 211 | r = m.equal_range(7); |
| 212 | assert(r.first == std::next(m.begin(), 1)); |
| 213 | assert(r.second == std::next(m.begin(), 2)); |
| 214 | r = m.equal_range(9); |
| 215 | assert(r.first == std::next(m.begin(), 2)); |
| 216 | assert(r.second == std::next(m.begin(), 3)); |
| 217 | r = m.equal_range(11); |
| 218 | assert(r.first == std::next(m.begin(), 3)); |
| 219 | assert(r.second == std::next(m.begin(), 4)); |
| 220 | r = m.equal_range(13); |
| 221 | assert(r.first == std::next(m.begin(), 4)); |
| 222 | assert(r.second == std::next(m.begin(), 5)); |
| 223 | r = m.equal_range(15); |
| 224 | assert(r.first == std::next(m.begin(), 5)); |
| 225 | assert(r.second == std::next(m.begin(), 6)); |
| 226 | r = m.equal_range(17); |
| 227 | assert(r.first == std::next(m.begin(), 6)); |
| 228 | assert(r.second == std::next(m.begin(), 7)); |
| 229 | r = m.equal_range(19); |
| 230 | assert(r.first == std::next(m.begin(), 7)); |
| 231 | assert(r.second == std::next(m.begin(), 8)); |
| 232 | r = m.equal_range(4); |
| 233 | assert(r.first == std::next(m.begin(), 0)); |
| 234 | assert(r.second == std::next(m.begin(), 0)); |
| 235 | r = m.equal_range(6); |
| 236 | assert(r.first == std::next(m.begin(), 1)); |
| 237 | assert(r.second == std::next(m.begin(), 1)); |
| 238 | r = m.equal_range(8); |
| 239 | assert(r.first == std::next(m.begin(), 2)); |
| 240 | assert(r.second == std::next(m.begin(), 2)); |
| 241 | r = m.equal_range(10); |
| 242 | assert(r.first == std::next(m.begin(), 3)); |
| 243 | assert(r.second == std::next(m.begin(), 3)); |
| 244 | r = m.equal_range(12); |
| 245 | assert(r.first == std::next(m.begin(), 4)); |
| 246 | assert(r.second == std::next(m.begin(), 4)); |
| 247 | r = m.equal_range(14); |
| 248 | assert(r.first == std::next(m.begin(), 5)); |
| 249 | assert(r.second == std::next(m.begin(), 5)); |
| 250 | r = m.equal_range(16); |
| 251 | assert(r.first == std::next(m.begin(), 6)); |
| 252 | assert(r.second == std::next(m.begin(), 6)); |
| 253 | r = m.equal_range(18); |
| 254 | assert(r.first == std::next(m.begin(), 7)); |
| 255 | assert(r.second == std::next(m.begin(), 7)); |
| 256 | r = m.equal_range(20); |
| 257 | assert(r.first == std::next(m.begin(), 8)); |
| 258 | assert(r.second == std::next(m.begin(), 8)); |
| 259 | } |
| 260 | } |
| 261 | { |
| 262 | typedef PrivateConstructor V; |
| 263 | typedef std::set<V, std::less<>> M; |
| 264 | typedef std::pair<M::iterator, M::iterator> R; |
| 265 | |
| 266 | M m; |
| 267 | m.insert(V::make(5)); |
| 268 | m.insert(V::make(7)); |
| 269 | m.insert(V::make(9)); |
| 270 | m.insert(V::make(11)); |
| 271 | m.insert(V::make(13)); |
| 272 | m.insert(V::make(15)); |
| 273 | m.insert(V::make(17)); |
| 274 | m.insert(V::make(19)); |
| 275 | |
| 276 | R r = m.equal_range(5); |
| 277 | assert(r.first == std::next(m.begin(), 0)); |
| 278 | assert(r.second == std::next(m.begin(), 1)); |
| 279 | r = m.equal_range(7); |
| 280 | assert(r.first == std::next(m.begin(), 1)); |
| 281 | assert(r.second == std::next(m.begin(), 2)); |
| 282 | r = m.equal_range(9); |
| 283 | assert(r.first == std::next(m.begin(), 2)); |
| 284 | assert(r.second == std::next(m.begin(), 3)); |
| 285 | r = m.equal_range(11); |
| 286 | assert(r.first == std::next(m.begin(), 3)); |
| 287 | assert(r.second == std::next(m.begin(), 4)); |
| 288 | r = m.equal_range(13); |
| 289 | assert(r.first == std::next(m.begin(), 4)); |
| 290 | assert(r.second == std::next(m.begin(), 5)); |
| 291 | r = m.equal_range(15); |
| 292 | assert(r.first == std::next(m.begin(), 5)); |
| 293 | assert(r.second == std::next(m.begin(), 6)); |
| 294 | r = m.equal_range(17); |
| 295 | assert(r.first == std::next(m.begin(), 6)); |
| 296 | assert(r.second == std::next(m.begin(), 7)); |
| 297 | r = m.equal_range(19); |
| 298 | assert(r.first == std::next(m.begin(), 7)); |
| 299 | assert(r.second == std::next(m.begin(), 8)); |
| 300 | r = m.equal_range(4); |
| 301 | assert(r.first == std::next(m.begin(), 0)); |
| 302 | assert(r.second == std::next(m.begin(), 0)); |
| 303 | r = m.equal_range(6); |
| 304 | assert(r.first == std::next(m.begin(), 1)); |
| 305 | assert(r.second == std::next(m.begin(), 1)); |
| 306 | r = m.equal_range(8); |
| 307 | assert(r.first == std::next(m.begin(), 2)); |
| 308 | assert(r.second == std::next(m.begin(), 2)); |
| 309 | r = m.equal_range(10); |
| 310 | assert(r.first == std::next(m.begin(), 3)); |
| 311 | assert(r.second == std::next(m.begin(), 3)); |
| 312 | r = m.equal_range(12); |
| 313 | assert(r.first == std::next(m.begin(), 4)); |
| 314 | assert(r.second == std::next(m.begin(), 4)); |
| 315 | r = m.equal_range(14); |
| 316 | assert(r.first == std::next(m.begin(), 5)); |
| 317 | assert(r.second == std::next(m.begin(), 5)); |
| 318 | r = m.equal_range(16); |
| 319 | assert(r.first == std::next(m.begin(), 6)); |
| 320 | assert(r.second == std::next(m.begin(), 6)); |
| 321 | r = m.equal_range(18); |
| 322 | assert(r.first == std::next(m.begin(), 7)); |
| 323 | assert(r.second == std::next(m.begin(), 7)); |
| 324 | r = m.equal_range(20); |
| 325 | assert(r.first == std::next(m.begin(), 8)); |
| 326 | assert(r.second == std::next(m.begin(), 8)); |
| 327 | } |
| 328 | #endif |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |