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