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