Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: base/containers/flat_set_unittest.cc

Issue 2552343003: First implementation of flat sets (Closed)
Patch Set: Review, round 8. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/containers/flat_set.h ('K') | « base/containers/flat_set.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
danakj 2017/02/03 20:30:49 2017
dyaroshev 2017/02/04 00:59:16 Done. Sorry)
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/containers/flat_set.h"
6
7 // Following tests are ported and extended tests from libcpp for std::set.
8 // They can be found here:
9 // https://github.com/llvm-mirror/libcxx/tree/master/test/std/containers/associa tive/set
10 //
11 // Not ported tests:
12 // * No tests with PrivateConstructor and std::less<> changed to std::less<T>
13 // These tests have to do with C++14 std::less<>
14 // http://en.cppreference.com/w/cpp/utility/functional/less_void
15 // and add support for templated versions of lookup functions.
16 // Current implementation of flat containers doesn't support it.
17 // * No tests with TemplateConstructor.
18 // Library working group issue: LWG #2059
19 // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2059
20 // There is an ambiguity between erase with an iterator and erase with a key,
21 // if key has a templated constructor. We have to fix this.
22 // * No tests for max_size()
23 // Has to do with allocator support.
24 // * No tests with DefaultOnly.
25 // Standard containers allocate each element in the separate node on the heap
26 // and then manipulate these nodes. Flat containers store their elements in
27 // contiguous memory and move them around, type is required to be movable.
28 // * No tests for N3644.
29 // This proposal suggests that all default constructed iterators compare
30 // equal. Currently we use std::vector iterators and they don't implement
31 // this.
32 // * No tests with min_allocator and no tests counting allocations.
33 // Flat sets currently don't support allocators.
34 // * No tests for range insertion. Flat sets currently do not support this
35 // functionality.
36
37 #include <string>
38 #include <vector>
39
40 #include "base/macros.h"
41 #include "testing/gtest/include/gtest/gtest.h"
42
43 namespace {
44
45 class MoveOnly {
46 public:
47 MoveOnly(int data = 1) : data_(data) {}
danakj 2017/02/03 20:30:49 can this be explicit?
dyaroshev 2017/02/04 00:59:16 Done. It was useful for ExpectRange, but I got rid
danakj 2017/02/07 16:35:51 Looks like it wasn't done?
dyaroshev 2017/02/07 23:47:24 So sorry, I made it work but forgot adding explici
48 MoveOnly(MoveOnly&& other) : data_(other.data_) { other.data_ = 0; }
49 MoveOnly& operator=(MoveOnly&& other) {
50 data_ = other.data_;
51 other.data_ = 0;
52 return *this;
53 }
54
55 friend bool operator==(const MoveOnly& lhs, const MoveOnly& rhs) {
56 return lhs.data_ == rhs.data_;
57 }
58
59 friend bool operator<(const MoveOnly& lhs, const MoveOnly& rhs) {
60 return lhs.data_ < rhs.data_;
61 }
62
63 int data() const { return data_; }
64
65 private:
66 int data_;
67
68 DISALLOW_COPY_AND_ASSIGN(MoveOnly);
69 };
70
71 template <class It>
72 class InputIterator {
73 public:
74 using iterator_category = std::input_iterator_tag;
75 using value_type = typename std::iterator_traits<It>::value_type;
76 using difference_type = typename std::iterator_traits<It>::difference_type;
77 using pointer = It;
78 using reference = typename std::iterator_traits<It>::reference;
79
80 InputIterator() : it_() {}
81 explicit InputIterator(It it) : it_(it) {}
82
83 reference operator*() const { return *it_; }
84 pointer operator->() const { return it_; }
85
86 InputIterator& operator++() {
87 ++it_;
88 return *this;
89 }
90 InputIterator operator++(int) {
91 InputIterator tmp(*this);
92 ++(*this);
93 return tmp;
94 }
95
96 friend bool operator==(const InputIterator& lhs, const InputIterator& rhs) {
97 return lhs.it_ == rhs.it_;
98 }
99 friend bool operator!=(const InputIterator& lhs, const InputIterator& rhs) {
100 return !(lhs == rhs);
101 }
102
103 private:
104 It it_;
105 };
106
107 template <typename It>
108 InputIterator<It> MakeInputIterator(It it) {
109 return InputIterator<It>(it);
110 }
111
112 class Emplaceable {
113 public:
114 Emplaceable() : Emplaceable(0, 0.0) {}
115 Emplaceable(int i, double d) : int_(i), double_(d) {}
116 Emplaceable(Emplaceable&& other) : int_(other.int_), double_(other.double_) {
117 other.int_ = 0;
118 other.double_ = 0.0;
119 }
120
121 Emplaceable& operator=(Emplaceable&& other) {
122 int_ = other.int_;
123 other.int_ = 0;
124 double_ = other.double_;
125 other.double_ = 0.0;
126 return *this;
127 }
128
129 friend bool operator==(const Emplaceable& lhs, const Emplaceable& rhs) {
130 return std::tie(lhs.int_, lhs.double_) == std::tie(rhs.int_, rhs.double_);
131 }
132
133 friend bool operator<(const Emplaceable& lhs, const Emplaceable& rhs) {
134 return std::tie(lhs.int_, lhs.double_) < std::tie(rhs.int_, rhs.double_);
135 }
136
137 private:
138 int int_;
139 double double_;
140
141 DISALLOW_COPY_AND_ASSIGN(Emplaceable);
142 };
143
144 class NonDefaultConstructibleCompare {
145 public:
146 NonDefaultConstructibleCompare(int) {}
danakj 2017/02/03 20:30:49 can this be explicit?
dyaroshev 2017/02/04 00:59:16 Done.
147
148 template <typename T>
149 bool operator()(const T& lhs, const T& rhs) {
150 return std::less<T>()(lhs, rhs);
151 }
152 };
153
154 template <typename T>
155 std::string ToString(const T& val) {
156 return std::to_string(val);
157 }
158
159 std::string ToString(const MoveOnly& val) {
160 return std::to_string(val.data());
161 }
162
163 template <typename LhsRange, typename RhsRange>
164 // Requires:
165 // LhsRange is ForwardRange
danakj 2017/02/03 20:30:49 in these comments just use a - or a * or something
dyaroshev 2017/02/04 00:59:16 Not applicable.
166 // RhsRange is ForwardRange
167 // ValueType<LhsRange> is EqualityComparable to ValueType<RhsRange>
168 bool EqualRange(const LhsRange& lhs_range, const RhsRange& rhs_range) {
169 return (std::distance(lhs_range.begin(), lhs_range.end()) ==
170 std::distance(rhs_range.begin(), rhs_range.end())) &&
171 std::equal(lhs_range.begin(), lhs_range.end(), rhs_range.begin());
172 }
173
174 template <typename Range>
175 // Requires:
176 // Range is InputRange
177 // ValueType<Range> is ConvertibleToString.
178 std::string RangeToString(const Range& range) {
179 std::string res = "[";
180 for (const auto& val : range)
181 res += ToString(val) + ", ";
182 res += ']';
183 return res;
184 }
185
186 template <typename TestedRange>
187 // Requires:
188 // TestedRange is ForwardRange
189 // ExpectedRange is ForwardRange
190 // ValueType<TestedRange> is EqualityComparable to int
191 // ValueType<TestedRange> is ConvertibleToString.
192 void ExpectRange(const TestedRange& tested_range,
193 const std::vector<int>& expected_range) {
194 EXPECT_TRUE(EqualRange(tested_range, expected_range))
195 << "Expected: " << RangeToString(expected_range)
196 << ", Actual: " << RangeToString(tested_range);
danakj 2017/02/03 20:30:49 perhaps "\n" in front of each of Expected and Actu
dyaroshev 2017/02/04 00:59:16 Turns out, GTEST has a pretty nice version of this
197 }
198
199 // Common test sets.
200 using IntSet = base::flat_set<int>;
201 using MoveOnlySet = base::flat_set<MoveOnly>;
202 using EmplaceableSet = base::flat_set<Emplaceable>;
203 using ReversedSet = base::flat_set<int, std::greater<int>>;
204
205 // TODO(dyaroshev): replace less<int> with less<>, once we have it
206 // crbug.com/682254.
207 using SetWithLess = base::flat_set<int, std::less<int>>;
208
209 using SetWithStrangeCompare =
210 base::flat_set<int, NonDefaultConstructibleCompare>;
211
212 } // namespace
213
214 // ----------------------------------------------------------------------------
215 // Class.
216
217 // Check that base::flat_set and its iterators can be instantiated with an
218 // incomplete type.
219
220 TEST(FlatSet, IncompleteType) {
221 struct A {
222 using Set = base::flat_set<A>;
223 int data;
224 Set set_with_incomplete_type;
225 Set::iterator it;
226 Set::const_iterator cit;
227
228 // We do not declare operator< because clang complains that it's unused.
229 };
230
231 A a;
232 }
233
234 TEST(FlatSet, Stability) {
235 using Pair = std::pair<int, int>;
236
237 struct LessByFirst {
238 bool operator()(const Pair& lhs, const Pair& rhs) {
239 return lhs.first < rhs.first;
240 }
241 };
242
243 using Set = base::flat_set<Pair, LessByFirst>;
244
245 // Constructors are not stable.
246 Set cont{{0, 0}, {1, 0}, {0, 1}, {2, 0}, {0, 2}, {1, 1}};
247
248 auto NoneOfSecondsAreTwo = [&cont] {
249 return std::none_of(cont.begin(), cont.end(),
250 [](const Pair& elem) { return elem.second == 2; });
251 };
252
253 // Should not replace existing.
254 cont.insert(Pair(0, 2));
255 cont.insert(Pair(1, 2));
256 cont.insert(Pair(2, 2));
257
258 EXPECT_TRUE(NoneOfSecondsAreTwo())
259 << "insert should be stable with respect to constructor";
260
261 cont.insert(Pair(3, 0));
262 cont.insert(Pair(3, 2));
263
264 EXPECT_TRUE(NoneOfSecondsAreTwo())
265 << "insert should be stable with respect to previous insert";
266 }
267
268 // ----------------------------------------------------------------------------
269 // Types.
270
271 // key_type
272 // key_compare
273 // value_type
274 // value_compare
275 // pointer
276 // const_pointer
277 // reference
278 // const_reference
279 // size_type
280 // difference_type
281 // iterator
282 // const_iterator
283 // reverse_iterator
284 // const_reverse_iterator
285
286 TEST(FlatSet, Types) {
287 // These are guaranteed to be portable.
288 static_assert((std::is_same<int, IntSet::key_type>::value), "");
289 static_assert((std::is_same<int, IntSet::value_type>::value), "");
290 static_assert((std::is_same<std::less<int>, IntSet::key_compare>::value), "");
291 static_assert((std::is_same<std::less<int>, IntSet::value_compare>::value),
292 "");
293 static_assert((std::is_same<int&, IntSet::reference>::value), "");
294 static_assert((std::is_same<const int&, IntSet::const_reference>::value), "");
295 static_assert((std::is_same<int*, IntSet::pointer>::value), "");
296 static_assert((std::is_same<const int*, IntSet::const_pointer>::value), "");
297 }
298
299 // ----------------------------------------------------------------------------
300 // Lifetime.
301
302 // flat_set()
303 // flat_set(const Compare& comp)
304
305 TEST(FlatSet, DefaultConstructor) {
306 {
307 IntSet cont;
308 ExpectRange(cont, {});
danakj 2017/02/03 20:30:49 Can you ensure there is a SCOPED_TRACE at each cal
dyaroshev 2017/02/04 00:59:16 Not applicable
309 }
310
311 {
312 SetWithStrangeCompare cont(NonDefaultConstructibleCompare(0));
313 ExpectRange(cont, {});
314 }
315 }
316
317 // flat_set(InputIterator first,
318 // InputIterator last,
319 // const Compare& comp = Compare())
320
321 TEST(FlatSet, RangeConstructor) {
322 {
323 IntSet::value_type input_vals[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
324
325 IntSet cont(MakeInputIterator(std::begin(input_vals)),
326 MakeInputIterator(std::end(input_vals)));
327 ExpectRange(cont, {1, 2, 3});
328 }
329 {
330 SetWithStrangeCompare::value_type input_vals[] = {1, 1, 1, 2, 2,
331 2, 3, 3, 3};
332
333 SetWithStrangeCompare cont(MakeInputIterator(std::begin(input_vals)),
334 MakeInputIterator(std::end(input_vals)),
335 NonDefaultConstructibleCompare(0));
336 ExpectRange(cont, {1, 2, 3});
337 }
338 }
339
340 // flat_set(const flat_set& x)
341
342 TEST(FlatSet, CopyConstructor) {
343 IntSet original{1, 2, 3, 4};
344 IntSet copied(original);
345
346 ExpectRange(copied, {1, 2, 3, 4});
347 ExpectRange(original, {1, 2, 3, 4});
348 EXPECT_EQ(original, copied);
349 }
350
351 // flat_set(flat_set&& x)
352
353 TEST(FlatSet, MoveConstructor) {
354 int input_range[] = {1, 2, 3, 4};
355
356 MoveOnlySet original(std::begin(input_range), std::end(input_range));
357 MoveOnlySet moved(std::move(original));
358
359 ExpectRange(moved, {1, 2, 3, 4});
360 }
361
362 // flat_set(std::initializer_list<value_type> ilist,
363 // const Compare& comp = Compare())
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&)
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&&)
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> ilist)
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 new_capacity)
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& val);
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&& val);
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_hint, const value_type& val);
606
607 TEST(FlatSet, InsertIterCV) {
danakj 2017/02/03 20:30:49 no acronyms please, write out what CV means, same
dyaroshev 2017/02/04 00:59:16 I changed it somehow. Works for you?
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_hint, value_type&& val);
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... Args>
658 // pair<iterator, bool> emplace(Args&&... args);
659
660 TEST(FlatSet, Emplace) {
661 {
662 EmplaceableSet cont;
663
664 std::pair<EmplaceableSet::iterator, bool> result = cont.emplace();
665 EXPECT_TRUE(result.second);
666 EXPECT_EQ(cont.begin(), result.first);
667 EXPECT_EQ(1U, cont.size());
668 EXPECT_EQ(Emplaceable(), *cont.begin());
669
670 result = cont.emplace(2, 3.5);
671 EXPECT_TRUE(result.second);
672 EXPECT_EQ(std::next(cont.begin()), result.first);
673 EXPECT_EQ(2U, cont.size());
674 EXPECT_EQ(Emplaceable(2, 3.5), *result.first);
675
676 result = cont.emplace(2, 3.5);
677 EXPECT_FALSE(result.second);
678 EXPECT_EQ(std::next(cont.begin()), result.first);
679 EXPECT_EQ(2U, cont.size());
680 EXPECT_EQ(Emplaceable(2, 3.5), *result.first);
681 }
682 {
683 IntSet cont;
684
685 std::pair<IntSet::iterator, bool> result = cont.emplace(2);
686 EXPECT_TRUE(result.second);
687 EXPECT_EQ(cont.begin(), result.first);
688 EXPECT_EQ(1U, cont.size());
689 EXPECT_EQ(2, *result.first);
690 }
691 }
692
693 // template <class... Args>
694 // iterator emplace_hint(const_iterator position_hint, Args&&... args);
695
696 TEST(FlatSet, EmplaceHint) {
697 {
698 EmplaceableSet cont;
699
700 EmplaceableSet::iterator result = cont.emplace_hint(cont.cend());
701 EXPECT_EQ(cont.begin(), result);
702 EXPECT_EQ(1U, cont.size());
703 EXPECT_EQ(Emplaceable(), *cont.begin());
704
705 result = cont.emplace_hint(cont.cend(), 2, 3.5);
706 EXPECT_EQ(std::next(cont.begin()), result);
707 EXPECT_EQ(2U, cont.size());
708 EXPECT_EQ(Emplaceable(2, 3.5), *result);
709
710 result = cont.emplace_hint(cont.cbegin(), 2, 3.5);
711 EXPECT_EQ(std::next(cont.begin()), result);
712 EXPECT_EQ(2U, cont.size());
713 EXPECT_EQ(Emplaceable(2, 3.5), *result);
714 }
715 {
716 IntSet cont;
717
718 IntSet::iterator result = cont.emplace_hint(cont.cend(), 2);
719 EXPECT_EQ(cont.begin(), result);
720 EXPECT_EQ(1U, cont.size());
721 EXPECT_EQ(2, *result);
722 }
723 }
724
725 // ----------------------------------------------------------------------------
726 // Erase operations.
727
728 // iterator erase(const_iterator position_hint);
729
730 TEST(FlatSet, EraseCIter) {
danakj 2017/02/03 20:30:49 write out what C is
dyaroshev 2017/02/04 00:59:16 Done.
731 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8};
732
733 IntSet::iterator it = cont.erase(std::next(cont.cbegin(), 3));
734 EXPECT_EQ(std::next(cont.begin(), 3), it);
735 ExpectRange(cont, {1, 2, 3, 5, 6, 7, 8});
736
737 it = cont.erase(std::next(cont.cbegin(), 0));
738 EXPECT_EQ(cont.begin(), it);
739 ExpectRange(cont, {2, 3, 5, 6, 7, 8});
740
741 it = cont.erase(std::next(cont.cbegin(), 5));
742 EXPECT_EQ(cont.end(), it);
743 ExpectRange(cont, {2, 3, 5, 6, 7});
744
745 it = cont.erase(std::next(cont.cbegin(), 1));
746 EXPECT_EQ(std::next(cont.begin()), it);
747 ExpectRange(cont, {2, 5, 6, 7});
748
749 it = cont.erase(std::next(cont.cbegin(), 2));
750 EXPECT_EQ(std::next(cont.begin(), 2), it);
751 ExpectRange(cont, {2, 5, 7});
752
753 it = cont.erase(std::next(cont.cbegin(), 2));
754 EXPECT_EQ(std::next(cont.begin(), 2), it);
755 ExpectRange(cont, {2, 5});
756
757 it = cont.erase(std::next(cont.cbegin(), 0));
758 EXPECT_EQ(std::next(cont.begin(), 0), it);
759 ExpectRange(cont, {5});
760
761 it = cont.erase(cont.cbegin());
762 EXPECT_EQ(cont.begin(), it);
763 EXPECT_EQ(cont.end(), it);
764 }
765
766 // iterator erase(const_iterator first, const_iterator last);
767
768 TEST(FlatSet, EraseCIterCIter) {
769 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8};
770
771 IntSet::iterator it =
772 cont.erase(std::next(cont.cbegin(), 5), std::next(cont.cbegin(), 5));
773 EXPECT_EQ(std::next(cont.begin(), 5), it);
774 ExpectRange(cont, {1, 2, 3, 4, 5, 6, 7, 8});
775
776 it = cont.erase(std::next(cont.cbegin(), 3), std::next(cont.cbegin(), 4));
777 EXPECT_EQ(std::next(cont.begin(), 3), it);
778 ExpectRange(cont, {1, 2, 3, 5, 6, 7, 8});
779
780 it = cont.erase(std::next(cont.cbegin(), 2), std::next(cont.cbegin(), 5));
781 EXPECT_EQ(std::next(cont.begin(), 2), it);
782 ExpectRange(cont, {1, 2, 7, 8});
783
784 it = cont.erase(std::next(cont.cbegin(), 0), std::next(cont.cbegin(), 2));
785 EXPECT_EQ(std::next(cont.begin(), 0), it);
786 ExpectRange(cont, {7, 8});
787
788 it = cont.erase(cont.cbegin(), cont.cend());
789 EXPECT_EQ(cont.begin(), it);
790 EXPECT_EQ(cont.end(), it);
791 }
792
793 // size_type erase(const key_type& key);
794
795 TEST(FlatSet, EraseKey) {
796 IntSet cont{1, 2, 3, 4, 5, 6, 7, 8};
797
798 EXPECT_EQ(0U, cont.erase(9));
799 ExpectRange(cont, {1, 2, 3, 4, 5, 6, 7, 8});
800
801 EXPECT_EQ(1U, cont.erase(4));
802 ExpectRange(cont, {1, 2, 3, 5, 6, 7, 8});
803
804 EXPECT_EQ(1U, cont.erase(1));
805 ExpectRange(cont, {2, 3, 5, 6, 7, 8});
806
807 EXPECT_EQ(1U, cont.erase(8));
808 ExpectRange(cont, {2, 3, 5, 6, 7});
809
810 EXPECT_EQ(1U, cont.erase(3));
811 ExpectRange(cont, {2, 5, 6, 7});
812
813 EXPECT_EQ(1U, cont.erase(6));
814 ExpectRange(cont, {2, 5, 7});
815
816 EXPECT_EQ(1U, cont.erase(7));
817 ExpectRange(cont, {2, 5});
818
819 EXPECT_EQ(1U, cont.erase(2));
820 ExpectRange(cont, {5});
821
822 EXPECT_EQ(1U, cont.erase(5));
823 ExpectRange(cont, {});
824 }
825
826 // ----------------------------------------------------------------------------
827 // Comparators.
828
829 // key_compare key_comp() const
830
831 TEST(FlatSet, KeyComp) {
832 ReversedSet cont{1, 2, 3, 4, 5};
833
834 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp()));
835 int new_elements[] = {6, 7, 8, 9, 10};
836 std::copy(std::begin(new_elements), std::end(new_elements),
837 std::inserter(cont, cont.end()));
838 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp()));
839 }
840
841 // value_compare value_comp() const
842
843 TEST(FlatSet, ValueComp) {
844 ReversedSet cont{1, 2, 3, 4, 5};
845
846 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp()));
847 int new_elements[] = {6, 7, 8, 9, 10};
848 std::copy(std::begin(new_elements), std::end(new_elements),
849 std::inserter(cont, cont.end()));
850 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp()));
851 }
852
853 // ----------------------------------------------------------------------------
854 // Search operations.
855
856 // size_type count(const key_type& key) const;
857
858 TEST(FlatSet, Count) {
859 {
860 const IntSet cont{5, 6, 7, 8, 9, 10, 11, 12};
861
862 EXPECT_EQ(1U, cont.count(5));
863 EXPECT_EQ(1U, cont.count(6));
864 EXPECT_EQ(1U, cont.count(7));
865 EXPECT_EQ(1U, cont.count(8));
866 EXPECT_EQ(1U, cont.count(9));
867 EXPECT_EQ(1U, cont.count(10));
868 EXPECT_EQ(1U, cont.count(11));
869 EXPECT_EQ(1U, cont.count(12));
870 EXPECT_EQ(0U, cont.count(4));
871 }
872 {
873 const SetWithLess cont{5, 6, 7, 8, 9, 10, 11, 12};
874
875 EXPECT_EQ(1U, cont.count(5));
876 EXPECT_EQ(1U, cont.count(6));
877 EXPECT_EQ(1U, cont.count(7));
878 EXPECT_EQ(1U, cont.count(8));
879 EXPECT_EQ(1U, cont.count(9));
880 EXPECT_EQ(1U, cont.count(10));
881 EXPECT_EQ(1U, cont.count(11));
882 EXPECT_EQ(1U, cont.count(12));
883 EXPECT_EQ(0U, cont.count(4));
884 }
885 }
886
887 // iterator find(const key_type& key);
888 // const_iterator find(const key_type& key) const;
889
890 TEST(FlatSet, Find) {
891 {
892 IntSet cont{5, 6, 7, 8, 9, 10, 11, 12};
893
894 EXPECT_EQ(cont.begin(), cont.find(5));
895 EXPECT_EQ(std::next(cont.begin()), cont.find(6));
896 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7));
897 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8));
898 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9));
899 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10));
900 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11));
901 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12));
902 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4));
903 }
904 {
905 const IntSet cont{5, 6, 7, 8, 9, 10, 11, 12};
906
907 EXPECT_EQ(cont.begin(), cont.find(5));
908 EXPECT_EQ(std::next(cont.begin()), cont.find(6));
909 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7));
910 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8));
911 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9));
912 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10));
913 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11));
914 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12));
915 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4));
916 }
917 {
918 SetWithLess cont{5, 6, 7, 8, 9, 10, 11, 12};
919
920 EXPECT_EQ(cont.begin(), cont.find(5));
921 EXPECT_EQ(std::next(cont.begin()), cont.find(6));
922 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7));
923 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8));
924 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9));
925 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10));
926 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11));
927 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12));
928 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4));
929 }
930 }
931
932 // pair<iterator,iterator> equal_range(const key_type& key);
933 // pair<const_iterator,const_iterator> equal_range(const key_type& key) const;
934
935 TEST(FlatSet, EqualRange) {
936 {
937 IntSet cont{5, 7, 9, 11, 13, 15, 17, 19};
938
939 std::pair<IntSet::iterator, IntSet::iterator> result = cont.equal_range(5);
940 EXPECT_EQ(std::next(cont.begin(), 0), result.first);
941 EXPECT_EQ(std::next(cont.begin(), 1), result.second);
942 result = cont.equal_range(7);
943 EXPECT_EQ(std::next(cont.begin(), 1), result.first);
944 EXPECT_EQ(std::next(cont.begin(), 2), result.second);
945 result = cont.equal_range(9);
946 EXPECT_EQ(std::next(cont.begin(), 2), result.first);
947 EXPECT_EQ(std::next(cont.begin(), 3), result.second);
948 result = cont.equal_range(11);
949 EXPECT_EQ(std::next(cont.begin(), 3), result.first);
950 EXPECT_EQ(std::next(cont.begin(), 4), result.second);
951 result = cont.equal_range(13);
952 EXPECT_EQ(std::next(cont.begin(), 4), result.first);
953 EXPECT_EQ(std::next(cont.begin(), 5), result.second);
954 result = cont.equal_range(15);
955 EXPECT_EQ(std::next(cont.begin(), 5), result.first);
956 EXPECT_EQ(std::next(cont.begin(), 6), result.second);
957 result = cont.equal_range(17);
958 EXPECT_EQ(std::next(cont.begin(), 6), result.first);
959 EXPECT_EQ(std::next(cont.begin(), 7), result.second);
960 result = cont.equal_range(19);
961 EXPECT_EQ(std::next(cont.begin(), 7), result.first);
962 EXPECT_EQ(std::next(cont.begin(), 8), result.second);
963 result = cont.equal_range(4);
964 EXPECT_EQ(std::next(cont.begin(), 0), result.first);
965 EXPECT_EQ(std::next(cont.begin(), 0), result.second);
966 result = cont.equal_range(6);
967 EXPECT_EQ(std::next(cont.begin(), 1), result.first);
968 EXPECT_EQ(std::next(cont.begin(), 1), result.second);
969 result = cont.equal_range(8);
970 EXPECT_EQ(std::next(cont.begin(), 2), result.first);
971 EXPECT_EQ(std::next(cont.begin(), 2), result.second);
972 result = cont.equal_range(10);
973 EXPECT_EQ(std::next(cont.begin(), 3), result.first);
974 EXPECT_EQ(std::next(cont.begin(), 3), result.second);
975 result = cont.equal_range(12);
976 EXPECT_EQ(std::next(cont.begin(), 4), result.first);
977 EXPECT_EQ(std::next(cont.begin(), 4), result.second);
978 result = cont.equal_range(14);
979 EXPECT_EQ(std::next(cont.begin(), 5), result.first);
980 EXPECT_EQ(std::next(cont.begin(), 5), result.second);
981 result = cont.equal_range(16);
982 EXPECT_EQ(std::next(cont.begin(), 6), result.first);
983 EXPECT_EQ(std::next(cont.begin(), 6), result.second);
984 result = cont.equal_range(18);
985 EXPECT_EQ(std::next(cont.begin(), 7), result.first);
986 EXPECT_EQ(std::next(cont.begin(), 7), result.second);
987 result = cont.equal_range(20);
988 EXPECT_EQ(std::next(cont.begin(), 8), result.first);
989 EXPECT_EQ(std::next(cont.begin(), 8), result.second);
990 }
991 {
992 const IntSet cont{5, 7, 9, 11, 13, 15, 17, 19};
993
994 std::pair<IntSet::const_iterator, IntSet::const_iterator> result =
995 cont.equal_range(5);
996 EXPECT_EQ(std::next(cont.begin(), 0), result.first);
997 EXPECT_EQ(std::next(cont.begin(), 1), result.second);
998 result = cont.equal_range(7);
999 EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1000 EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1001 result = cont.equal_range(9);
1002 EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1003 EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1004 result = cont.equal_range(11);
1005 EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1006 EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1007 result = cont.equal_range(13);
1008 EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1009 EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1010 result = cont.equal_range(15);
1011 EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1012 EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1013 result = cont.equal_range(17);
1014 EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1015 EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1016 result = cont.equal_range(19);
1017 EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1018 EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1019 result = cont.equal_range(4);
1020 EXPECT_EQ(std::next(cont.begin(), 0), result.first);
1021 EXPECT_EQ(std::next(cont.begin(), 0), result.second);
1022 result = cont.equal_range(6);
1023 EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1024 EXPECT_EQ(std::next(cont.begin(), 1), result.second);
1025 result = cont.equal_range(8);
1026 EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1027 EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1028 result = cont.equal_range(10);
1029 EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1030 EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1031 result = cont.equal_range(12);
1032 EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1033 EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1034 result = cont.equal_range(14);
1035 EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1036 EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1037 result = cont.equal_range(16);
1038 EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1039 EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1040 result = cont.equal_range(18);
1041 EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1042 EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1043 result = cont.equal_range(20);
1044 EXPECT_EQ(std::next(cont.begin(), 8), result.first);
1045 EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1046 }
1047 {
1048 SetWithLess cont{5, 7, 9, 11, 13, 15, 17, 19};
1049
1050 std::pair<SetWithLess::iterator, SetWithLess::iterator> result =
1051 cont.equal_range(5);
1052 EXPECT_EQ(std::next(cont.begin(), 0), result.first);
1053 EXPECT_EQ(std::next(cont.begin(), 1), result.second);
1054 result = cont.equal_range(7);
1055 EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1056 EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1057 result = cont.equal_range(9);
1058 EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1059 EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1060 result = cont.equal_range(11);
1061 EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1062 EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1063 result = cont.equal_range(13);
1064 EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1065 EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1066 result = cont.equal_range(15);
1067 EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1068 EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1069 result = cont.equal_range(17);
1070 EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1071 EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1072 result = cont.equal_range(19);
1073 EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1074 EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1075 result = cont.equal_range(4);
1076 EXPECT_EQ(std::next(cont.begin(), 0), result.first);
1077 EXPECT_EQ(std::next(cont.begin(), 0), result.second);
1078 result = cont.equal_range(6);
1079 EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1080 EXPECT_EQ(std::next(cont.begin(), 1), result.second);
1081 result = cont.equal_range(8);
1082 EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1083 EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1084 result = cont.equal_range(10);
1085 EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1086 EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1087 result = cont.equal_range(12);
1088 EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1089 EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1090 result = cont.equal_range(14);
1091 EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1092 EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1093 result = cont.equal_range(16);
1094 EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1095 EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1096 result = cont.equal_range(18);
1097 EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1098 EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1099 result = cont.equal_range(20);
1100 EXPECT_EQ(std::next(cont.begin(), 8), result.first);
1101 EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1102 }
1103 }
1104
1105 // iterator lower_bound(const key_type& key);
1106 // const_iterator lower_bound(const key_type& key) const;
1107
1108 TEST(FlatSet, LowerBound) {
1109 {
1110 IntSet cont{5, 7, 9, 11, 13, 15, 17, 19};
1111
1112 EXPECT_EQ(cont.begin(), cont.lower_bound(5));
1113 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7));
1114 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9));
1115 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11));
1116 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13));
1117 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15));
1118 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17));
1119 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19));
1120 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4));
1121 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6));
1122 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8));
1123 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10));
1124 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12));
1125 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14));
1126 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16));
1127 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18));
1128 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20));
1129 }
1130 {
1131 const IntSet cont{5, 7, 9, 11, 13, 15, 17, 19};
1132
1133 EXPECT_EQ(cont.begin(), cont.lower_bound(5));
1134 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7));
1135 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9));
1136 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11));
1137 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13));
1138 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15));
1139 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17));
1140 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19));
1141 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4));
1142 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6));
1143 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8));
1144 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10));
1145 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12));
1146 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14));
1147 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16));
1148 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18));
1149 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20));
1150 }
1151 {
1152 SetWithLess cont{5, 7, 9, 11, 13, 15, 17, 19};
1153
1154 EXPECT_EQ(cont.begin(), cont.lower_bound(5));
1155 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7));
1156 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9));
1157 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11));
1158 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13));
1159 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15));
1160 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17));
1161 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19));
1162 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4));
1163 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6));
1164 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8));
1165 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10));
1166 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12));
1167 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14));
1168 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16));
1169 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18));
1170 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20));
1171 }
1172 }
1173
1174 // iterator upper_bound(const key_type& key);
1175 // const_iterator upper_bound(const key_type& key) const;
1176
1177 TEST(FlatSet, UpperBound) {
1178 {
1179 IntSet cont{5, 7, 9, 11, 13, 15, 17, 19};
1180
1181 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5));
1182 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7));
1183 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9));
1184 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11));
1185 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13));
1186 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15));
1187 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17));
1188 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19));
1189 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4));
1190 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6));
1191 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8));
1192 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10));
1193 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12));
1194 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14));
1195 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16));
1196 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18));
1197 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20));
1198 }
1199 {
1200 const IntSet cont{5, 7, 9, 11, 13, 15, 17, 19};
1201
1202 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5));
1203 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7));
1204 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9));
1205 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11));
1206 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13));
1207 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15));
1208 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17));
1209 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19));
1210 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4));
1211 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6));
1212 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8));
1213 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10));
1214 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12));
1215 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14));
1216 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16));
1217 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18));
1218 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20));
1219 }
1220 {
1221 SetWithLess cont{5, 7, 9, 11, 13, 15, 17, 19};
1222
1223 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5));
1224 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7));
1225 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9));
1226 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11));
1227 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13));
1228 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15));
1229 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17));
1230 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19));
1231 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4));
1232 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6));
1233 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8));
1234 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10));
1235 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12));
1236 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14));
1237 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16));
1238 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18));
1239 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20));
1240 }
1241 }
1242
1243 // ----------------------------------------------------------------------------
1244 // General operations.
1245
1246 // void swap(flat_set& other)
1247 // void swap(flat_set& lhs, flat_set& rhs)
1248
1249 TEST(FlatSetOurs, Swap) {
1250 IntSet x{1, 2, 3};
1251 IntSet y{4};
1252 swap(x, y);
1253 ExpectRange(x, {4});
1254 ExpectRange(y, {1, 2, 3});
1255
1256 y.swap(x);
1257 ExpectRange(x, {1, 2, 3});
1258 ExpectRange(y, {4});
1259 }
1260
1261 // bool operator==(const flat_set& lhs, const flat_set& rhs)
1262 // bool operator!=(const flat_set& lhs, const flat_set& rhs)
1263 // bool operator<(const flat_set& lhs, const flat_set& rhs)
1264 // bool operator>(const flat_set& lhs, const flat_set& rhs)
1265 // bool operator<=(const flat_set& lhs, const flat_set& rhs)
1266 // bool operator>=(const flat_set& lhs, const flat_set& rhs)
1267
1268 TEST(FlatSet, Comparison) {
1269 // Provided comparator does not participate in comparison.
1270 ReversedSet biggest{3};
1271 ReversedSet smallest{1};
1272 ReversedSet middle{1, 2};
1273
1274 EXPECT_EQ(biggest, biggest);
1275 EXPECT_NE(biggest, smallest);
1276 EXPECT_LT(smallest, middle);
1277 EXPECT_LE(smallest, middle);
1278 EXPECT_LE(middle, middle);
1279 EXPECT_GT(biggest, middle);
1280 EXPECT_GE(biggest, middle);
1281 EXPECT_GE(biggest, biggest);
1282 }
OLDNEW
« base/containers/flat_set.h ('K') | « base/containers/flat_set.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698