OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/containers/flat_set.h" |
| 6 |
| 7 // Following tests are ported and extended tests from libcpp for std::set. |
| 8 // They can be found here: |
| 9 // https://github.com/llvm-mirror/libcxx/tree/master/test/std/containers/associa
tive/set |
| 10 // |
| 11 // Not ported tests: |
| 12 // * No tests with PrivateConstructor and std::less<> changed to std::less<T> |
| 13 // These tests have to do with C++14 std::less<> |
| 14 // http://en.cppreference.com/w/cpp/utility/functional/less_void |
| 15 // and add support for templated versions of lookup functions. |
| 16 // Current implementation of flat containers doesn't support it. |
| 17 // * No tests with TemplateConstructor. |
| 18 // Library working group issue: LWG #2059 |
| 19 // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2059 |
| 20 // There is an ambiguity between erase with an iterator and erase with a key, |
| 21 // if key has a templated constructor. We have to fix this. |
| 22 // * No tests for max_size() |
| 23 // Has to do with allocator support. |
| 24 // * No tests with DefaultOnly. |
| 25 // Standard containers allocate each element in the separate node on the heap |
| 26 // and then manipulate these nodes. Flat containers store their elements in |
| 27 // contiguous memory and move them around, type is required to be movable. |
| 28 // * No tests for N3644. |
| 29 // This proposal suggests that all default constructed iterators compare |
| 30 // equal. Currently we use std::vector iterators and they don't implement |
| 31 // this. |
| 32 // * No tests with min_allocator and no tests counting allocations. |
| 33 // Flat sets currently don't support allocators. |
| 34 // * No tests for range insertion. Flat sets currently do not support this |
| 35 // functionality. |
| 36 |
| 37 #include <string> |
| 38 #include <vector> |
| 39 |
| 40 #include "base/macros.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" |
| 42 |
| 43 namespace { |
| 44 |
| 45 class MoveOnly { |
| 46 public: |
| 47 MoveOnly(int data = 1) : data_(data) {} |
| 48 MoveOnly(MoveOnly&& other) : data_(other.data_) { other.data_ = 0; } |
| 49 MoveOnly& operator=(MoveOnly&& other) { |
| 50 data_ = other.data_; |
| 51 other.data_ = 0; |
| 52 return *this; |
| 53 } |
| 54 |
| 55 friend bool operator==(const MoveOnly& lhs, const MoveOnly& rhs) { |
| 56 return lhs.data_ == rhs.data_; |
| 57 } |
| 58 |
| 59 friend bool operator<(const MoveOnly& lhs, const MoveOnly& rhs) { |
| 60 return lhs.data_ < rhs.data_; |
| 61 } |
| 62 |
| 63 int data() const { return data_; } |
| 64 |
| 65 private: |
| 66 int data_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(MoveOnly); |
| 69 }; |
| 70 |
| 71 template <class It> |
| 72 class InputIterator { |
| 73 public: |
| 74 using iterator_category = std::input_iterator_tag; |
| 75 using value_type = typename std::iterator_traits<It>::value_type; |
| 76 using difference_type = typename std::iterator_traits<It>::difference_type; |
| 77 using pointer = It; |
| 78 using reference = typename std::iterator_traits<It>::reference; |
| 79 |
| 80 InputIterator() : it_() {} |
| 81 explicit InputIterator(It it) : it_(it) {} |
| 82 |
| 83 reference operator*() const { return *it_; } |
| 84 pointer operator->() const { return it_; } |
| 85 |
| 86 InputIterator& operator++() { |
| 87 ++it_; |
| 88 return *this; |
| 89 } |
| 90 InputIterator operator++(int) { |
| 91 InputIterator tmp(*this); |
| 92 ++(*this); |
| 93 return tmp; |
| 94 } |
| 95 |
| 96 friend bool operator==(const InputIterator& lhs, const InputIterator& rhs) { |
| 97 return lhs.it_ == rhs.it_; |
| 98 } |
| 99 friend bool operator!=(const InputIterator& lhs, const InputIterator& rhs) { |
| 100 return !(lhs == rhs); |
| 101 } |
| 102 |
| 103 private: |
| 104 It it_; |
| 105 }; |
| 106 |
| 107 template <typename It> |
| 108 InputIterator<It> MakeInputIterator(It it) { |
| 109 return InputIterator<It>(it); |
| 110 } |
| 111 |
| 112 class Emplaceable { |
| 113 public: |
| 114 Emplaceable() : Emplaceable(0, 0.0) {} |
| 115 Emplaceable(int i, double d) : int_(i), double_(d) {} |
| 116 Emplaceable(Emplaceable&& other) : int_(other.int_), double_(other.double_) { |
| 117 other.int_ = 0; |
| 118 other.double_ = 0.0; |
| 119 } |
| 120 |
| 121 Emplaceable& operator=(Emplaceable&& other) { |
| 122 int_ = other.int_; |
| 123 other.int_ = 0; |
| 124 double_ = other.double_; |
| 125 other.double_ = 0.0; |
| 126 return *this; |
| 127 } |
| 128 |
| 129 friend bool operator==(const Emplaceable& lhs, const Emplaceable& rhs) { |
| 130 return std::tie(lhs.int_, lhs.double_) == std::tie(rhs.int_, rhs.double_); |
| 131 } |
| 132 |
| 133 friend bool operator<(const Emplaceable& lhs, const Emplaceable& rhs) { |
| 134 return std::tie(lhs.int_, lhs.double_) < std::tie(rhs.int_, rhs.double_); |
| 135 } |
| 136 |
| 137 private: |
| 138 int int_; |
| 139 double double_; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(Emplaceable); |
| 142 }; |
| 143 |
| 144 class NonDefaultConstructibleCompare { |
| 145 public: |
| 146 NonDefaultConstructibleCompare(int) {} |
| 147 |
| 148 template <typename T> |
| 149 bool operator()(const T& lhs, const T& rhs) { |
| 150 return std::less<T>()(lhs, rhs); |
| 151 } |
| 152 }; |
| 153 |
| 154 template <typename T> |
| 155 std::string ToString(const T& val) { |
| 156 return std::to_string(val); |
| 157 } |
| 158 |
| 159 std::string ToString(const MoveOnly& val) { |
| 160 return std::to_string(val.data()); |
| 161 } |
| 162 |
| 163 template <typename LhsRange, typename RhsRange> |
| 164 // Requires: |
| 165 // LhsRange is ForwardRange |
| 166 // RhsRange is ForwardRange |
| 167 // ValueType<LhsRange> is EqualityComparable to ValueType<RhsRange> |
| 168 bool EqualRange(const LhsRange& lhs_range, const RhsRange& rhs_range) { |
| 169 return (std::distance(lhs_range.begin(), lhs_range.end()) == |
| 170 std::distance(rhs_range.begin(), rhs_range.end())) && |
| 171 std::equal(lhs_range.begin(), lhs_range.end(), rhs_range.begin()); |
| 172 } |
| 173 |
| 174 template <typename Range> |
| 175 // Requires: |
| 176 // Range is InputRange |
| 177 // ValueType<Range> is ConvertibleToString. |
| 178 std::string RangeToString(const Range& range) { |
| 179 std::string res = "["; |
| 180 for (const auto& val : range) |
| 181 res += ToString(val) + ", "; |
| 182 res += ']'; |
| 183 return res; |
| 184 } |
| 185 |
| 186 template <typename TestedRange> |
| 187 // Requires: |
| 188 // TestedRange is ForwardRange |
| 189 // ExpectedRange is ForwardRange |
| 190 // ValueType<TestedRange> is EqualityComparable to int |
| 191 // ValueType<TestedRange> is ConvertibleToString. |
| 192 void ExpectRange(const TestedRange& tested_range, |
| 193 const std::vector<int>& expected_range) { |
| 194 EXPECT_TRUE(EqualRange(tested_range, expected_range)) |
| 195 << "Expected: " << RangeToString(expected_range) |
| 196 << ", Actual: " << RangeToString(tested_range); |
| 197 } |
| 198 |
| 199 // Common test sets. |
| 200 using IntSet = base::flat_set<int>; |
| 201 using MoveOnlySet = base::flat_set<MoveOnly>; |
| 202 using EmplaceableSet = base::flat_set<Emplaceable>; |
| 203 using ReversedSet = base::flat_set<int, std::greater<int>>; |
| 204 |
| 205 // TODO(dyaroshev): replace less<int> with less<>, once we have it |
| 206 // crbug.com/682254. |
| 207 using SetWithLess = base::flat_set<int, std::less<int>>; |
| 208 |
| 209 using SetWithStrangeCompare = |
| 210 base::flat_set<int, NonDefaultConstructibleCompare>; |
| 211 |
| 212 } // namespace |
| 213 |
| 214 // ---------------------------------------------------------------------------- |
| 215 // Class. |
| 216 |
| 217 // Check that base::flat_set and its iterators can be instantiated with an |
| 218 // incomplete type. |
| 219 |
| 220 TEST(FlatSet, IncompleteType) { |
| 221 struct A { |
| 222 using Set = base::flat_set<A>; |
| 223 int data; |
| 224 Set set_with_incomplete_type; |
| 225 Set::iterator it; |
| 226 Set::const_iterator cit; |
| 227 |
| 228 // We do not declare operator< because clang complains that it's unused. |
| 229 }; |
| 230 |
| 231 A a; |
| 232 } |
| 233 |
| 234 TEST(FlatSet, Stability) { |
| 235 using Pair = std::pair<int, int>; |
| 236 |
| 237 struct LessByFirst { |
| 238 bool operator()(const Pair& lhs, const Pair& rhs) { |
| 239 return lhs.first < rhs.first; |
| 240 } |
| 241 }; |
| 242 |
| 243 using Set = base::flat_set<Pair, LessByFirst>; |
| 244 |
| 245 auto AllSecondAreZero = [](const Set& cont) { |
| 246 return std::all_of(cont.begin(), cont.end(), |
| 247 [](const Pair& elem) { return elem.second == 0; }); |
| 248 }; |
| 249 |
| 250 Set cont{{0, 0}, {1, 0}, {0, 1}, {2, 0}, {0, 2}, {1, 1}}; |
| 251 EXPECT_TRUE(AllSecondAreZero(cont)) << "initializer_list constructor"; |
| 252 |
| 253 cont.insert(Pair(0, 1)); |
| 254 EXPECT_TRUE(AllSecondAreZero(cont)) << "single insert"; |
| 255 } |
| 256 |
| 257 // ---------------------------------------------------------------------------- |
| 258 // Types. |
| 259 |
| 260 // key_type |
| 261 // key_compare |
| 262 // value_type |
| 263 // value_compare |
| 264 // pointer |
| 265 // const_pointer |
| 266 // reference |
| 267 // const_reference |
| 268 // size_type |
| 269 // difference_type |
| 270 // iterator |
| 271 // const_iterator |
| 272 // reverse_iterator |
| 273 // const_reverse_iterator |
| 274 |
| 275 TEST(FlatSet, Types) { |
| 276 // These are guaranteed to be portable. |
| 277 static_assert((std::is_same<int, IntSet::key_type>::value), ""); |
| 278 static_assert((std::is_same<int, IntSet::value_type>::value), ""); |
| 279 static_assert((std::is_same<std::less<int>, IntSet::key_compare>::value), ""); |
| 280 static_assert((std::is_same<std::less<int>, IntSet::value_compare>::value), |
| 281 ""); |
| 282 static_assert((std::is_same<int&, IntSet::reference>::value), ""); |
| 283 static_assert((std::is_same<const int&, IntSet::const_reference>::value), ""); |
| 284 static_assert((std::is_same<int*, IntSet::pointer>::value), ""); |
| 285 static_assert((std::is_same<const int*, IntSet::const_pointer>::value), ""); |
| 286 } |
| 287 |
| 288 // ---------------------------------------------------------------------------- |
| 289 // Lifetime. |
| 290 |
| 291 // flat_set() |
| 292 // flat_set(const Compare& comp) |
| 293 |
| 294 TEST(FlatSet, DefaultConstructor) { |
| 295 { |
| 296 IntSet cont; |
| 297 ExpectRange(cont, {}); |
| 298 } |
| 299 |
| 300 { |
| 301 SetWithStrangeCompare cont(NonDefaultConstructibleCompare(0)); |
| 302 ExpectRange(cont, {}); |
| 303 } |
| 304 } |
| 305 |
| 306 // flat_set(InputIterator first, |
| 307 // InputIterator last, |
| 308 // const Compare& comp = Compare()) |
| 309 |
| 310 TEST(FlatSet, RangeConstructor) { |
| 311 { |
| 312 IntSet::value_type input_vals[] = {1, 1, 1, 2, 2, 2, 3, 3, 3}; |
| 313 |
| 314 IntSet cont(MakeInputIterator(std::begin(input_vals)), |
| 315 MakeInputIterator(std::end(input_vals))); |
| 316 ExpectRange(cont, {1, 2, 3}); |
| 317 } |
| 318 { |
| 319 SetWithStrangeCompare::value_type input_vals[] = {1, 1, 1, 2, 2, |
| 320 2, 3, 3, 3}; |
| 321 |
| 322 SetWithStrangeCompare cont(MakeInputIterator(std::begin(input_vals)), |
| 323 MakeInputIterator(std::end(input_vals)), |
| 324 NonDefaultConstructibleCompare(0)); |
| 325 ExpectRange(cont, {1, 2, 3}); |
| 326 } |
| 327 } |
| 328 |
| 329 // flat_set(const flat_set& x) |
| 330 |
| 331 TEST(FlatSet, CopyConstructor) { |
| 332 IntSet original{1, 2, 3, 4}; |
| 333 IntSet copied(original); |
| 334 |
| 335 ExpectRange(copied, {1, 2, 3, 4}); |
| 336 ExpectRange(original, {1, 2, 3, 4}); |
| 337 EXPECT_EQ(original, copied); |
| 338 } |
| 339 |
| 340 // flat_set(flat_set&& x) |
| 341 |
| 342 TEST(FlatSet, MoveConstructor) { |
| 343 int input_range[] = {1, 2, 3, 4}; |
| 344 |
| 345 MoveOnlySet original(std::begin(input_range), std::end(input_range)); |
| 346 MoveOnlySet moved(std::move(original)); |
| 347 |
| 348 ExpectRange(moved, {1, 2, 3, 4}); |
| 349 } |
| 350 |
| 351 // flat_set(std::initializer_list<value_type> ilist, |
| 352 // const Compare& comp = Compare()) |
| 353 |
| 354 TEST(FlatSet, InitializerListConstructor) { |
| 355 { |
| 356 IntSet cont{1, 2, 3, 4, 5, 6, 10, 8}; |
| 357 ExpectRange(cont, {1, 2, 3, 4, 5, 6, 8, 10}); |
| 358 } |
| 359 { |
| 360 SetWithStrangeCompare cont({1, 2, 3, 4, 5, 6, 10, 8}, |
| 361 NonDefaultConstructibleCompare(0)); |
| 362 ExpectRange(cont, {1, 2, 3, 4, 5, 6, 8, 10}); |
| 363 } |
| 364 } |
| 365 |
| 366 // ---------------------------------------------------------------------------- |
| 367 // Assignments. |
| 368 |
| 369 // flat_set& operator=(const flat_set&) |
| 370 |
| 371 TEST(FlatSet, CopyAssignable) { |
| 372 IntSet original{1, 2, 3, 4}; |
| 373 IntSet copied; |
| 374 copied = original; |
| 375 |
| 376 ExpectRange(copied, {1, 2, 3, 4}); |
| 377 ExpectRange(original, {1, 2, 3, 4}); |
| 378 EXPECT_EQ(original, copied); |
| 379 } |
| 380 |
| 381 // flat_set& operator=(flat_set&&) |
| 382 |
| 383 TEST(FlatSet, MoveAssignable) { |
| 384 int input_range[] = {1, 2, 3, 4}; |
| 385 |
| 386 MoveOnlySet original(std::begin(input_range), std::end(input_range)); |
| 387 MoveOnlySet moved; |
| 388 moved = std::move(original); |
| 389 |
| 390 ExpectRange(moved, {1, 2, 3, 4}); |
| 391 } |
| 392 |
| 393 // flat_set& operator=(std::initializer_list<value_type> ilist) |
| 394 |
| 395 TEST(FlatSet, InitializerListAssignable) { |
| 396 IntSet cont{0}; |
| 397 cont = {1, 2, 3, 4, 5, 6, 10, 8}; |
| 398 |
| 399 EXPECT_EQ(0U, cont.count(0)); |
| 400 ExpectRange(cont, {1, 2, 3, 4, 5, 6, 8, 10}); |
| 401 } |
| 402 |
| 403 // -------------------------------------------------------------------------- |
| 404 // Memory management. |
| 405 |
| 406 // void reserve(size_type new_capacity) |
| 407 |
| 408 TEST(FlatSet, Reserve) { |
| 409 IntSet cont{1, 2, 3}; |
| 410 |
| 411 cont.reserve(5); |
| 412 EXPECT_LE(5U, cont.capacity()); |
| 413 } |
| 414 |
| 415 // size_type capacity() const |
| 416 |
| 417 TEST(FlatSet, Capacity) { |
| 418 IntSet cont{1, 2, 3}; |
| 419 |
| 420 EXPECT_LE(cont.size(), cont.capacity()); |
| 421 cont.reserve(5); |
| 422 EXPECT_LE(cont.size(), cont.capacity()); |
| 423 } |
| 424 |
| 425 // void shrink_to_fit() |
| 426 |
| 427 TEST(FlatSet, ShrinkToFit) { |
| 428 IntSet cont{1, 2, 3}; |
| 429 |
| 430 IntSet::size_type capacity_before = cont.capacity(); |
| 431 cont.shrink_to_fit(); |
| 432 EXPECT_GE(capacity_before, cont.capacity()); |
| 433 } |
| 434 |
| 435 // ---------------------------------------------------------------------------- |
| 436 // Size management. |
| 437 |
| 438 // void clear(); |
| 439 |
| 440 TEST(FlatSet, Clear) { |
| 441 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8}; |
| 442 cont.clear(); |
| 443 ExpectRange(cont, {}); |
| 444 } |
| 445 |
| 446 // size_type size() const; |
| 447 |
| 448 TEST(FlatSet, Size) { |
| 449 IntSet cont; |
| 450 |
| 451 EXPECT_EQ(0U, cont.size()); |
| 452 cont.insert(2); |
| 453 EXPECT_EQ(1U, cont.size()); |
| 454 cont.insert(1); |
| 455 EXPECT_EQ(2U, cont.size()); |
| 456 cont.insert(3); |
| 457 EXPECT_EQ(3U, cont.size()); |
| 458 cont.erase(cont.begin()); |
| 459 EXPECT_EQ(2U, cont.size()); |
| 460 cont.erase(cont.begin()); |
| 461 EXPECT_EQ(1U, cont.size()); |
| 462 cont.erase(cont.begin()); |
| 463 EXPECT_EQ(0U, cont.size()); |
| 464 } |
| 465 |
| 466 // bool empty() const; |
| 467 |
| 468 TEST(FlatSet, Empty) { |
| 469 IntSet cont; |
| 470 |
| 471 EXPECT_TRUE(cont.empty()); |
| 472 cont.insert(1); |
| 473 EXPECT_FALSE(cont.empty()); |
| 474 cont.clear(); |
| 475 EXPECT_TRUE(cont.empty()); |
| 476 } |
| 477 |
| 478 // ---------------------------------------------------------------------------- |
| 479 // Iterators. |
| 480 |
| 481 // iterator begin(); |
| 482 // const_iterator begin() const; |
| 483 // iterator end(); |
| 484 // const_iterator end() const; |
| 485 // |
| 486 // reverse_iterator rbegin(); |
| 487 // const_reverse_iterator rbegin() const; |
| 488 // reverse_iterator rend(); |
| 489 // const_reverse_iterator rend() const; |
| 490 // |
| 491 // const_iterator cbegin() const; |
| 492 // const_iterator cend() const; |
| 493 // const_reverse_iterator crbegin() const; |
| 494 // const_reverse_iterator crend() const; |
| 495 |
| 496 TEST(FlatSet, Iterators) { |
| 497 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8}; |
| 498 |
| 499 auto size = static_cast<IntSet::difference_type>(cont.size()); |
| 500 |
| 501 EXPECT_EQ(size, std::distance(cont.begin(), cont.end())); |
| 502 EXPECT_EQ(size, std::distance(cont.cbegin(), cont.cend())); |
| 503 EXPECT_EQ(size, std::distance(cont.rbegin(), cont.rend())); |
| 504 EXPECT_EQ(size, std::distance(cont.crbegin(), cont.crend())); |
| 505 |
| 506 { |
| 507 IntSet::iterator it = cont.begin(); |
| 508 IntSet::const_iterator c_it = cont.cbegin(); |
| 509 EXPECT_EQ(it, c_it); |
| 510 for (int j = 1; it != cont.end(); ++it, ++c_it, ++j) { |
| 511 EXPECT_EQ(j, *it); |
| 512 EXPECT_EQ(j, *c_it); |
| 513 } |
| 514 } |
| 515 { |
| 516 IntSet::reverse_iterator rit = cont.rbegin(); |
| 517 IntSet::const_reverse_iterator c_rit = cont.crbegin(); |
| 518 EXPECT_EQ(rit, c_rit); |
| 519 for (int j = static_cast<int>(size); rit != cont.rend(); |
| 520 ++rit, ++c_rit, --j) { |
| 521 EXPECT_EQ(j, *rit); |
| 522 EXPECT_EQ(j, *c_rit); |
| 523 } |
| 524 } |
| 525 } |
| 526 |
| 527 // ---------------------------------------------------------------------------- |
| 528 // Insert operations. |
| 529 |
| 530 // pair<iterator, bool> insert(const value_type& val); |
| 531 |
| 532 TEST(FlatSet, InsertCV) { |
| 533 IntSet cont; |
| 534 |
| 535 int value = 2; |
| 536 std::pair<IntSet::iterator, bool> result = cont.insert(value); |
| 537 EXPECT_TRUE(result.second); |
| 538 EXPECT_EQ(cont.begin(), result.first); |
| 539 EXPECT_EQ(1U, cont.size()); |
| 540 EXPECT_EQ(2, *result.first); |
| 541 |
| 542 value = 1; |
| 543 result = cont.insert(value); |
| 544 EXPECT_TRUE(result.second); |
| 545 EXPECT_EQ(cont.begin(), result.first); |
| 546 EXPECT_EQ(2U, cont.size()); |
| 547 EXPECT_EQ(1, *result.first); |
| 548 |
| 549 value = 3; |
| 550 result = cont.insert(value); |
| 551 EXPECT_TRUE(result.second); |
| 552 EXPECT_EQ(std::prev(cont.end()), result.first); |
| 553 EXPECT_EQ(3U, cont.size()); |
| 554 EXPECT_EQ(3, *result.first); |
| 555 |
| 556 value = 3; |
| 557 result = cont.insert(value); |
| 558 EXPECT_FALSE(result.second); |
| 559 EXPECT_EQ(std::prev(cont.end()), result.first); |
| 560 EXPECT_EQ(3U, cont.size()); |
| 561 EXPECT_EQ(3, *result.first); |
| 562 } |
| 563 |
| 564 // pair<iterator, bool> insert(value_type&& val); |
| 565 |
| 566 TEST(FlatSet, InsertRV) { |
| 567 MoveOnlySet cont; |
| 568 |
| 569 std::pair<MoveOnlySet::iterator, bool> result = cont.insert(MoveOnly(2)); |
| 570 EXPECT_TRUE(result.second); |
| 571 EXPECT_EQ(cont.begin(), result.first); |
| 572 EXPECT_EQ(1U, cont.size()); |
| 573 EXPECT_EQ(2, *result.first); |
| 574 |
| 575 result = cont.insert(MoveOnly(1)); |
| 576 EXPECT_TRUE(result.second); |
| 577 EXPECT_EQ(cont.begin(), result.first); |
| 578 EXPECT_EQ(2U, cont.size()); |
| 579 EXPECT_EQ(1, *result.first); |
| 580 |
| 581 result = cont.insert(MoveOnly(3)); |
| 582 EXPECT_TRUE(result.second); |
| 583 EXPECT_EQ(std::prev(cont.end()), result.first); |
| 584 EXPECT_EQ(3U, cont.size()); |
| 585 EXPECT_EQ(3, *result.first); |
| 586 |
| 587 result = cont.insert(MoveOnly(3)); |
| 588 EXPECT_FALSE(result.second); |
| 589 EXPECT_EQ(std::prev(cont.end()), result.first); |
| 590 EXPECT_EQ(3U, cont.size()); |
| 591 EXPECT_EQ(3, *result.first); |
| 592 } |
| 593 |
| 594 // iterator insert(const_iterator position_hint, const value_type& val); |
| 595 |
| 596 TEST(FlatSet, InsertIterCV) { |
| 597 IntSet cont; |
| 598 |
| 599 IntSet::iterator result = cont.insert(cont.cend(), 2); |
| 600 EXPECT_EQ(cont.begin(), result); |
| 601 EXPECT_EQ(1U, cont.size()); |
| 602 EXPECT_EQ(2, *result); |
| 603 |
| 604 result = cont.insert(cont.cend(), 1); |
| 605 EXPECT_EQ(cont.begin(), result); |
| 606 EXPECT_EQ(2U, cont.size()); |
| 607 EXPECT_EQ(1, *result); |
| 608 |
| 609 result = cont.insert(cont.cend(), 3); |
| 610 EXPECT_EQ(std::prev(cont.end()), result); |
| 611 EXPECT_EQ(3U, cont.size()); |
| 612 EXPECT_EQ(3, *result); |
| 613 |
| 614 result = cont.insert(cont.cend(), 3); |
| 615 EXPECT_EQ(std::prev(cont.end()), result); |
| 616 EXPECT_EQ(3U, cont.size()); |
| 617 EXPECT_EQ(3, *result); |
| 618 } |
| 619 |
| 620 // iterator insert(const_iterator position_hint, value_type&& val); |
| 621 |
| 622 TEST(FlatSet, InsertIterRV) { |
| 623 MoveOnlySet cont; |
| 624 |
| 625 MoveOnlySet::iterator result = cont.insert(cont.cend(), MoveOnly(2)); |
| 626 EXPECT_EQ(cont.begin(), result); |
| 627 EXPECT_EQ(1U, cont.size()); |
| 628 EXPECT_EQ(2, *result); |
| 629 |
| 630 result = cont.insert(cont.cend(), MoveOnly(1)); |
| 631 EXPECT_EQ(cont.begin(), result); |
| 632 EXPECT_EQ(2U, cont.size()); |
| 633 EXPECT_EQ(1, *result); |
| 634 |
| 635 result = cont.insert(cont.cend(), MoveOnly(3)); |
| 636 EXPECT_EQ(std::prev(cont.end()), result); |
| 637 EXPECT_EQ(3U, cont.size()); |
| 638 EXPECT_EQ(3, *result); |
| 639 |
| 640 result = cont.insert(cont.cend(), MoveOnly(3)); |
| 641 EXPECT_EQ(std::prev(cont.end()), result); |
| 642 EXPECT_EQ(3U, cont.size()); |
| 643 EXPECT_EQ(3, *result); |
| 644 } |
| 645 |
| 646 // template <class... Args> |
| 647 // pair<iterator, bool> emplace(Args&&... args); |
| 648 |
| 649 TEST(FlatSet, Emplace) { |
| 650 { |
| 651 EmplaceableSet cont; |
| 652 |
| 653 std::pair<EmplaceableSet::iterator, bool> result = cont.emplace(); |
| 654 EXPECT_TRUE(result.second); |
| 655 EXPECT_EQ(cont.begin(), result.first); |
| 656 EXPECT_EQ(1U, cont.size()); |
| 657 EXPECT_EQ(Emplaceable(), *cont.begin()); |
| 658 |
| 659 result = cont.emplace(2, 3.5); |
| 660 EXPECT_TRUE(result.second); |
| 661 EXPECT_EQ(std::next(cont.begin()), result.first); |
| 662 EXPECT_EQ(2U, cont.size()); |
| 663 EXPECT_EQ(Emplaceable(2, 3.5), *result.first); |
| 664 |
| 665 result = cont.emplace(2, 3.5); |
| 666 EXPECT_FALSE(result.second); |
| 667 EXPECT_EQ(std::next(cont.begin()), result.first); |
| 668 EXPECT_EQ(2U, cont.size()); |
| 669 EXPECT_EQ(Emplaceable(2, 3.5), *result.first); |
| 670 } |
| 671 { |
| 672 IntSet cont; |
| 673 |
| 674 std::pair<IntSet::iterator, bool> result = cont.emplace(2); |
| 675 EXPECT_TRUE(result.second); |
| 676 EXPECT_EQ(cont.begin(), result.first); |
| 677 EXPECT_EQ(1U, cont.size()); |
| 678 EXPECT_EQ(2, *result.first); |
| 679 } |
| 680 } |
| 681 |
| 682 // template <class... Args> |
| 683 // iterator emplace_hint(const_iterator position_hint, Args&&... args); |
| 684 |
| 685 TEST(FlatSet, EmplaceHint) { |
| 686 { |
| 687 EmplaceableSet cont; |
| 688 |
| 689 EmplaceableSet::iterator result = cont.emplace_hint(cont.cend()); |
| 690 EXPECT_EQ(cont.begin(), result); |
| 691 EXPECT_EQ(1U, cont.size()); |
| 692 EXPECT_EQ(Emplaceable(), *cont.begin()); |
| 693 |
| 694 result = cont.emplace_hint(cont.cend(), 2, 3.5); |
| 695 EXPECT_EQ(std::next(cont.begin()), result); |
| 696 EXPECT_EQ(2U, cont.size()); |
| 697 EXPECT_EQ(Emplaceable(2, 3.5), *result); |
| 698 |
| 699 result = cont.emplace_hint(cont.cbegin(), 2, 3.5); |
| 700 EXPECT_EQ(std::next(cont.begin()), result); |
| 701 EXPECT_EQ(2U, cont.size()); |
| 702 EXPECT_EQ(Emplaceable(2, 3.5), *result); |
| 703 } |
| 704 { |
| 705 IntSet cont; |
| 706 |
| 707 IntSet::iterator result = cont.emplace_hint(cont.cend(), 2); |
| 708 EXPECT_EQ(cont.begin(), result); |
| 709 EXPECT_EQ(1U, cont.size()); |
| 710 EXPECT_EQ(2, *result); |
| 711 } |
| 712 } |
| 713 |
| 714 // ---------------------------------------------------------------------------- |
| 715 // Erase operations. |
| 716 |
| 717 // iterator erase(const_iterator position_hint); |
| 718 |
| 719 TEST(FlatSet, EraseCIter) { |
| 720 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8}; |
| 721 |
| 722 IntSet::iterator it = cont.erase(std::next(cont.cbegin(), 3)); |
| 723 EXPECT_EQ(std::next(cont.begin(), 3), it); |
| 724 ExpectRange(cont, {1, 2, 3, 5, 6, 7, 8}); |
| 725 |
| 726 it = cont.erase(std::next(cont.cbegin(), 0)); |
| 727 EXPECT_EQ(cont.begin(), it); |
| 728 ExpectRange(cont, {2, 3, 5, 6, 7, 8}); |
| 729 |
| 730 it = cont.erase(std::next(cont.cbegin(), 5)); |
| 731 EXPECT_EQ(cont.end(), it); |
| 732 ExpectRange(cont, {2, 3, 5, 6, 7}); |
| 733 |
| 734 it = cont.erase(std::next(cont.cbegin(), 1)); |
| 735 EXPECT_EQ(std::next(cont.begin()), it); |
| 736 ExpectRange(cont, {2, 5, 6, 7}); |
| 737 |
| 738 it = cont.erase(std::next(cont.cbegin(), 2)); |
| 739 EXPECT_EQ(std::next(cont.begin(), 2), it); |
| 740 ExpectRange(cont, {2, 5, 7}); |
| 741 |
| 742 it = cont.erase(std::next(cont.cbegin(), 2)); |
| 743 EXPECT_EQ(std::next(cont.begin(), 2), it); |
| 744 ExpectRange(cont, {2, 5}); |
| 745 |
| 746 it = cont.erase(std::next(cont.cbegin(), 0)); |
| 747 EXPECT_EQ(std::next(cont.begin(), 0), it); |
| 748 ExpectRange(cont, {5}); |
| 749 |
| 750 it = cont.erase(cont.cbegin()); |
| 751 EXPECT_EQ(cont.begin(), it); |
| 752 EXPECT_EQ(cont.end(), it); |
| 753 } |
| 754 |
| 755 // iterator erase(const_iterator first, const_iterator last); |
| 756 |
| 757 TEST(FlatSet, EraseCIterCIter) { |
| 758 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8}; |
| 759 |
| 760 IntSet::iterator it = |
| 761 cont.erase(std::next(cont.cbegin(), 5), std::next(cont.cbegin(), 5)); |
| 762 EXPECT_EQ(std::next(cont.begin(), 5), it); |
| 763 ExpectRange(cont, {1, 2, 3, 4, 5, 6, 7, 8}); |
| 764 |
| 765 it = cont.erase(std::next(cont.cbegin(), 3), std::next(cont.cbegin(), 4)); |
| 766 EXPECT_EQ(std::next(cont.begin(), 3), it); |
| 767 ExpectRange(cont, {1, 2, 3, 5, 6, 7, 8}); |
| 768 |
| 769 it = cont.erase(std::next(cont.cbegin(), 2), std::next(cont.cbegin(), 5)); |
| 770 EXPECT_EQ(std::next(cont.begin(), 2), it); |
| 771 ExpectRange(cont, {1, 2, 7, 8}); |
| 772 |
| 773 it = cont.erase(std::next(cont.cbegin(), 0), std::next(cont.cbegin(), 2)); |
| 774 EXPECT_EQ(std::next(cont.begin(), 0), it); |
| 775 ExpectRange(cont, {7, 8}); |
| 776 |
| 777 it = cont.erase(cont.cbegin(), cont.cend()); |
| 778 EXPECT_EQ(cont.begin(), it); |
| 779 EXPECT_EQ(cont.end(), it); |
| 780 } |
| 781 |
| 782 // size_type erase(const key_type& key); |
| 783 |
| 784 TEST(FlatSet, EraseKey) { |
| 785 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8}; |
| 786 |
| 787 EXPECT_EQ(0U, cont.erase(9)); |
| 788 ExpectRange(cont, {1, 2, 3, 4, 5, 6, 7, 8}); |
| 789 |
| 790 EXPECT_EQ(1U, cont.erase(4)); |
| 791 ExpectRange(cont, {1, 2, 3, 5, 6, 7, 8}); |
| 792 |
| 793 EXPECT_EQ(1U, cont.erase(1)); |
| 794 ExpectRange(cont, {2, 3, 5, 6, 7, 8}); |
| 795 |
| 796 EXPECT_EQ(1U, cont.erase(8)); |
| 797 ExpectRange(cont, {2, 3, 5, 6, 7}); |
| 798 |
| 799 EXPECT_EQ(1U, cont.erase(3)); |
| 800 ExpectRange(cont, {2, 5, 6, 7}); |
| 801 |
| 802 EXPECT_EQ(1U, cont.erase(6)); |
| 803 ExpectRange(cont, {2, 5, 7}); |
| 804 |
| 805 EXPECT_EQ(1U, cont.erase(7)); |
| 806 ExpectRange(cont, {2, 5}); |
| 807 |
| 808 EXPECT_EQ(1U, cont.erase(2)); |
| 809 ExpectRange(cont, {5}); |
| 810 |
| 811 EXPECT_EQ(1U, cont.erase(5)); |
| 812 ExpectRange(cont, {}); |
| 813 } |
| 814 |
| 815 // ---------------------------------------------------------------------------- |
| 816 // Comparators. |
| 817 |
| 818 // key_compare key_comp() const |
| 819 |
| 820 TEST(FlatSet, KeyComp) { |
| 821 ReversedSet cont{1, 2, 3, 4, 5}; |
| 822 |
| 823 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp())); |
| 824 int new_elements[] = {6, 7, 8, 9, 10}; |
| 825 std::copy(std::begin(new_elements), std::end(new_elements), |
| 826 std::inserter(cont, cont.end())); |
| 827 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp())); |
| 828 } |
| 829 |
| 830 // value_compare value_comp() const |
| 831 |
| 832 TEST(FlatSet, ValueComp) { |
| 833 ReversedSet cont{1, 2, 3, 4, 5}; |
| 834 |
| 835 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp())); |
| 836 int new_elements[] = {6, 7, 8, 9, 10}; |
| 837 std::copy(std::begin(new_elements), std::end(new_elements), |
| 838 std::inserter(cont, cont.end())); |
| 839 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp())); |
| 840 } |
| 841 |
| 842 // ---------------------------------------------------------------------------- |
| 843 // Search operations. |
| 844 |
| 845 // size_type count(const key_type& key) const; |
| 846 |
| 847 TEST(FlatSet, Count) { |
| 848 { |
| 849 const IntSet cont{5, 6, 7, 8, 9, 10, 11, 12}; |
| 850 |
| 851 EXPECT_EQ(1U, cont.count(5)); |
| 852 EXPECT_EQ(1U, cont.count(6)); |
| 853 EXPECT_EQ(1U, cont.count(7)); |
| 854 EXPECT_EQ(1U, cont.count(8)); |
| 855 EXPECT_EQ(1U, cont.count(9)); |
| 856 EXPECT_EQ(1U, cont.count(10)); |
| 857 EXPECT_EQ(1U, cont.count(11)); |
| 858 EXPECT_EQ(1U, cont.count(12)); |
| 859 EXPECT_EQ(0U, cont.count(4)); |
| 860 } |
| 861 { |
| 862 const SetWithLess cont{5, 6, 7, 8, 9, 10, 11, 12}; |
| 863 |
| 864 EXPECT_EQ(1U, cont.count(5)); |
| 865 EXPECT_EQ(1U, cont.count(6)); |
| 866 EXPECT_EQ(1U, cont.count(7)); |
| 867 EXPECT_EQ(1U, cont.count(8)); |
| 868 EXPECT_EQ(1U, cont.count(9)); |
| 869 EXPECT_EQ(1U, cont.count(10)); |
| 870 EXPECT_EQ(1U, cont.count(11)); |
| 871 EXPECT_EQ(1U, cont.count(12)); |
| 872 EXPECT_EQ(0U, cont.count(4)); |
| 873 } |
| 874 } |
| 875 |
| 876 // iterator find(const key_type& key); |
| 877 // const_iterator find(const key_type& key) const; |
| 878 |
| 879 TEST(FlatSet, Find) { |
| 880 { |
| 881 IntSet cont{5, 6, 7, 8, 9, 10, 11, 12}; |
| 882 |
| 883 EXPECT_EQ(cont.begin(), cont.find(5)); |
| 884 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); |
| 885 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); |
| 886 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); |
| 887 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); |
| 888 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); |
| 889 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); |
| 890 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); |
| 891 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); |
| 892 } |
| 893 { |
| 894 const IntSet cont{5, 6, 7, 8, 9, 10, 11, 12}; |
| 895 |
| 896 EXPECT_EQ(cont.begin(), cont.find(5)); |
| 897 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); |
| 898 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); |
| 899 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); |
| 900 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); |
| 901 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); |
| 902 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); |
| 903 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); |
| 904 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); |
| 905 } |
| 906 { |
| 907 SetWithLess cont{5, 6, 7, 8, 9, 10, 11, 12}; |
| 908 |
| 909 EXPECT_EQ(cont.begin(), cont.find(5)); |
| 910 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); |
| 911 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); |
| 912 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); |
| 913 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); |
| 914 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); |
| 915 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); |
| 916 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); |
| 917 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); |
| 918 } |
| 919 } |
| 920 |
| 921 // pair<iterator,iterator> equal_range(const key_type& key); |
| 922 // pair<const_iterator,const_iterator> equal_range(const key_type& key) const; |
| 923 |
| 924 TEST(FlatSet, EqualRange) { |
| 925 { |
| 926 IntSet cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 927 |
| 928 std::pair<IntSet::iterator, IntSet::iterator> result = cont.equal_range(5); |
| 929 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
| 930 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
| 931 result = cont.equal_range(7); |
| 932 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
| 933 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
| 934 result = cont.equal_range(9); |
| 935 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
| 936 EXPECT_EQ(std::next(cont.begin(), 3), result.second); |
| 937 result = cont.equal_range(11); |
| 938 EXPECT_EQ(std::next(cont.begin(), 3), result.first); |
| 939 EXPECT_EQ(std::next(cont.begin(), 4), result.second); |
| 940 result = cont.equal_range(13); |
| 941 EXPECT_EQ(std::next(cont.begin(), 4), result.first); |
| 942 EXPECT_EQ(std::next(cont.begin(), 5), result.second); |
| 943 result = cont.equal_range(15); |
| 944 EXPECT_EQ(std::next(cont.begin(), 5), result.first); |
| 945 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
| 946 result = cont.equal_range(17); |
| 947 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
| 948 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
| 949 result = cont.equal_range(19); |
| 950 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
| 951 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
| 952 result = cont.equal_range(4); |
| 953 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
| 954 EXPECT_EQ(std::next(cont.begin(), 0), result.second); |
| 955 result = cont.equal_range(6); |
| 956 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
| 957 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
| 958 result = cont.equal_range(8); |
| 959 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
| 960 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
| 961 result = cont.equal_range(10); |
| 962 EXPECT_EQ(std::next(cont.begin(), 3), result.first); |
| 963 EXPECT_EQ(std::next(cont.begin(), 3), result.second); |
| 964 result = cont.equal_range(12); |
| 965 EXPECT_EQ(std::next(cont.begin(), 4), result.first); |
| 966 EXPECT_EQ(std::next(cont.begin(), 4), result.second); |
| 967 result = cont.equal_range(14); |
| 968 EXPECT_EQ(std::next(cont.begin(), 5), result.first); |
| 969 EXPECT_EQ(std::next(cont.begin(), 5), result.second); |
| 970 result = cont.equal_range(16); |
| 971 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
| 972 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
| 973 result = cont.equal_range(18); |
| 974 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
| 975 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
| 976 result = cont.equal_range(20); |
| 977 EXPECT_EQ(std::next(cont.begin(), 8), result.first); |
| 978 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
| 979 } |
| 980 { |
| 981 const IntSet cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 982 |
| 983 std::pair<IntSet::const_iterator, IntSet::const_iterator> result = |
| 984 cont.equal_range(5); |
| 985 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
| 986 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
| 987 result = cont.equal_range(7); |
| 988 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
| 989 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
| 990 result = cont.equal_range(9); |
| 991 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
| 992 EXPECT_EQ(std::next(cont.begin(), 3), result.second); |
| 993 result = cont.equal_range(11); |
| 994 EXPECT_EQ(std::next(cont.begin(), 3), result.first); |
| 995 EXPECT_EQ(std::next(cont.begin(), 4), result.second); |
| 996 result = cont.equal_range(13); |
| 997 EXPECT_EQ(std::next(cont.begin(), 4), result.first); |
| 998 EXPECT_EQ(std::next(cont.begin(), 5), result.second); |
| 999 result = cont.equal_range(15); |
| 1000 EXPECT_EQ(std::next(cont.begin(), 5), result.first); |
| 1001 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
| 1002 result = cont.equal_range(17); |
| 1003 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
| 1004 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
| 1005 result = cont.equal_range(19); |
| 1006 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
| 1007 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
| 1008 result = cont.equal_range(4); |
| 1009 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
| 1010 EXPECT_EQ(std::next(cont.begin(), 0), result.second); |
| 1011 result = cont.equal_range(6); |
| 1012 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
| 1013 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
| 1014 result = cont.equal_range(8); |
| 1015 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
| 1016 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
| 1017 result = cont.equal_range(10); |
| 1018 EXPECT_EQ(std::next(cont.begin(), 3), result.first); |
| 1019 EXPECT_EQ(std::next(cont.begin(), 3), result.second); |
| 1020 result = cont.equal_range(12); |
| 1021 EXPECT_EQ(std::next(cont.begin(), 4), result.first); |
| 1022 EXPECT_EQ(std::next(cont.begin(), 4), result.second); |
| 1023 result = cont.equal_range(14); |
| 1024 EXPECT_EQ(std::next(cont.begin(), 5), result.first); |
| 1025 EXPECT_EQ(std::next(cont.begin(), 5), result.second); |
| 1026 result = cont.equal_range(16); |
| 1027 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
| 1028 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
| 1029 result = cont.equal_range(18); |
| 1030 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
| 1031 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
| 1032 result = cont.equal_range(20); |
| 1033 EXPECT_EQ(std::next(cont.begin(), 8), result.first); |
| 1034 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
| 1035 } |
| 1036 { |
| 1037 SetWithLess cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 1038 |
| 1039 std::pair<SetWithLess::iterator, SetWithLess::iterator> result = |
| 1040 cont.equal_range(5); |
| 1041 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
| 1042 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
| 1043 result = cont.equal_range(7); |
| 1044 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
| 1045 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
| 1046 result = cont.equal_range(9); |
| 1047 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
| 1048 EXPECT_EQ(std::next(cont.begin(), 3), result.second); |
| 1049 result = cont.equal_range(11); |
| 1050 EXPECT_EQ(std::next(cont.begin(), 3), result.first); |
| 1051 EXPECT_EQ(std::next(cont.begin(), 4), result.second); |
| 1052 result = cont.equal_range(13); |
| 1053 EXPECT_EQ(std::next(cont.begin(), 4), result.first); |
| 1054 EXPECT_EQ(std::next(cont.begin(), 5), result.second); |
| 1055 result = cont.equal_range(15); |
| 1056 EXPECT_EQ(std::next(cont.begin(), 5), result.first); |
| 1057 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
| 1058 result = cont.equal_range(17); |
| 1059 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
| 1060 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
| 1061 result = cont.equal_range(19); |
| 1062 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
| 1063 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
| 1064 result = cont.equal_range(4); |
| 1065 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
| 1066 EXPECT_EQ(std::next(cont.begin(), 0), result.second); |
| 1067 result = cont.equal_range(6); |
| 1068 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
| 1069 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
| 1070 result = cont.equal_range(8); |
| 1071 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
| 1072 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
| 1073 result = cont.equal_range(10); |
| 1074 EXPECT_EQ(std::next(cont.begin(), 3), result.first); |
| 1075 EXPECT_EQ(std::next(cont.begin(), 3), result.second); |
| 1076 result = cont.equal_range(12); |
| 1077 EXPECT_EQ(std::next(cont.begin(), 4), result.first); |
| 1078 EXPECT_EQ(std::next(cont.begin(), 4), result.second); |
| 1079 result = cont.equal_range(14); |
| 1080 EXPECT_EQ(std::next(cont.begin(), 5), result.first); |
| 1081 EXPECT_EQ(std::next(cont.begin(), 5), result.second); |
| 1082 result = cont.equal_range(16); |
| 1083 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
| 1084 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
| 1085 result = cont.equal_range(18); |
| 1086 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
| 1087 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
| 1088 result = cont.equal_range(20); |
| 1089 EXPECT_EQ(std::next(cont.begin(), 8), result.first); |
| 1090 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
| 1091 } |
| 1092 } |
| 1093 |
| 1094 // iterator lower_bound(const key_type& key); |
| 1095 // const_iterator lower_bound(const key_type& key) const; |
| 1096 |
| 1097 TEST(FlatSet, LowerBound) { |
| 1098 { |
| 1099 IntSet cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 1100 |
| 1101 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); |
| 1102 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); |
| 1103 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); |
| 1104 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); |
| 1105 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); |
| 1106 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); |
| 1107 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); |
| 1108 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); |
| 1109 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); |
| 1110 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); |
| 1111 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); |
| 1112 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); |
| 1113 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); |
| 1114 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); |
| 1115 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); |
| 1116 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); |
| 1117 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); |
| 1118 } |
| 1119 { |
| 1120 const IntSet cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 1121 |
| 1122 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); |
| 1123 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); |
| 1124 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); |
| 1125 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); |
| 1126 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); |
| 1127 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); |
| 1128 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); |
| 1129 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); |
| 1130 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); |
| 1131 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); |
| 1132 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); |
| 1133 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); |
| 1134 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); |
| 1135 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); |
| 1136 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); |
| 1137 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); |
| 1138 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); |
| 1139 } |
| 1140 { |
| 1141 SetWithLess cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 1142 |
| 1143 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); |
| 1144 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); |
| 1145 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); |
| 1146 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); |
| 1147 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); |
| 1148 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); |
| 1149 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); |
| 1150 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); |
| 1151 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); |
| 1152 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); |
| 1153 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); |
| 1154 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); |
| 1155 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); |
| 1156 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); |
| 1157 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); |
| 1158 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); |
| 1159 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); |
| 1160 } |
| 1161 } |
| 1162 |
| 1163 // iterator upper_bound(const key_type& key); |
| 1164 // const_iterator upper_bound(const key_type& key) const; |
| 1165 |
| 1166 TEST(FlatSet, UpperBound) { |
| 1167 { |
| 1168 IntSet cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 1169 |
| 1170 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); |
| 1171 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); |
| 1172 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); |
| 1173 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); |
| 1174 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); |
| 1175 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); |
| 1176 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); |
| 1177 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); |
| 1178 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); |
| 1179 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); |
| 1180 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); |
| 1181 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); |
| 1182 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); |
| 1183 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); |
| 1184 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); |
| 1185 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); |
| 1186 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); |
| 1187 } |
| 1188 { |
| 1189 const IntSet cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 1190 |
| 1191 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); |
| 1192 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); |
| 1193 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); |
| 1194 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); |
| 1195 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); |
| 1196 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); |
| 1197 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); |
| 1198 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); |
| 1199 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); |
| 1200 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); |
| 1201 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); |
| 1202 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); |
| 1203 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); |
| 1204 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); |
| 1205 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); |
| 1206 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); |
| 1207 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); |
| 1208 } |
| 1209 { |
| 1210 SetWithLess cont{5, 7, 9, 11, 13, 15, 17, 19}; |
| 1211 |
| 1212 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); |
| 1213 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); |
| 1214 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); |
| 1215 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); |
| 1216 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); |
| 1217 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); |
| 1218 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); |
| 1219 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); |
| 1220 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); |
| 1221 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); |
| 1222 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); |
| 1223 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); |
| 1224 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); |
| 1225 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); |
| 1226 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); |
| 1227 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); |
| 1228 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); |
| 1229 } |
| 1230 } |
| 1231 |
| 1232 // ---------------------------------------------------------------------------- |
| 1233 // General operations. |
| 1234 |
| 1235 // void swap(flat_set& other) |
| 1236 // void swap(flat_set& lhs, flat_set& rhs) |
| 1237 |
| 1238 TEST(FlatSetOurs, Swap) { |
| 1239 IntSet x{1, 2, 3}; |
| 1240 IntSet y{4}; |
| 1241 swap(x, y); |
| 1242 ExpectRange(x, {4}); |
| 1243 ExpectRange(y, {1, 2, 3}); |
| 1244 |
| 1245 y.swap(x); |
| 1246 ExpectRange(x, {1, 2, 3}); |
| 1247 ExpectRange(y, {4}); |
| 1248 } |
| 1249 |
| 1250 // bool operator==(const flat_set& lhs, const flat_set& rhs) |
| 1251 // bool operator!=(const flat_set& lhs, const flat_set& rhs) |
| 1252 // bool operator<(const flat_set& lhs, const flat_set& rhs) |
| 1253 // bool operator>(const flat_set& lhs, const flat_set& rhs) |
| 1254 // bool operator<=(const flat_set& lhs, const flat_set& rhs) |
| 1255 // bool operator>=(const flat_set& lhs, const flat_set& rhs) |
| 1256 |
| 1257 TEST(FlatSet, Comparison) { |
| 1258 // Provided comparator does not participate in comparison. |
| 1259 ReversedSet biggest{3}; |
| 1260 ReversedSet smallest{1}; |
| 1261 ReversedSet middle{1, 2}; |
| 1262 |
| 1263 EXPECT_EQ(biggest, biggest); |
| 1264 EXPECT_NE(biggest, smallest); |
| 1265 EXPECT_LT(smallest, middle); |
| 1266 EXPECT_LE(smallest, middle); |
| 1267 EXPECT_LE(middle, middle); |
| 1268 EXPECT_GT(biggest, middle); |
| 1269 EXPECT_GE(biggest, middle); |
| 1270 EXPECT_GE(biggest, biggest); |
| 1271 } |
OLD | NEW |