• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #include "base/containers/flat_tree.h"
11 
12 // Following tests are ported and extended tests from libcpp for std::set.
13 // They can be found here:
14 // https://github.com/llvm/llvm-project/tree/main/libcxx/test/std/containers/associative/set
15 //
16 // Not ported tests:
17 // * No tests with PrivateConstructor and std::less<> changed to std::less<T>
18 //   These tests have to do with C++14 std::less<>
19 //   http://en.cppreference.com/w/cpp/utility/functional/less_void
20 //   and add support for templated versions of lookup functions.
21 //   Because we use same implementation, we figured that it's OK just to check
22 //   compilation and this is what we do in flat_set_unittest/flat_map_unittest.
23 // * No tests for max_size()
24 //   Has to do with allocator support.
25 // * No tests with DefaultOnly.
26 //   Standard containers allocate each element in the separate node on the heap
27 //   and then manipulate these nodes. Flat containers store their elements in
28 //   contiguous memory and move them around, type is required to be movable.
29 // * No tests for N3644.
30 //   This proposal suggests that all default constructed iterators compare
31 //   equal. Currently we use std::vector iterators and they don't implement
32 //   this.
33 // * No tests with min_allocator and no tests counting allocations.
34 //   Flat sets currently don't support allocators.
35 
36 #include <deque>
37 #include <forward_list>
38 #include <functional>
39 #include <iterator>
40 #include <list>
41 #include <string>
42 #include <vector>
43 
44 #include "base/ranges/algorithm.h"
45 #include "base/test/gtest_util.h"
46 #include "base/test/move_only_int.h"
47 #include "testing/gmock/include/gmock/gmock.h"
48 #include "testing/gtest/include/gtest/gtest.h"
49 
50 namespace base {
51 namespace internal {
52 
53 namespace {
54 
55 template <class It>
56 class InputIterator {
57  public:
58   using iterator_category = std::input_iterator_tag;
59   using value_type = typename std::iterator_traits<It>::value_type;
60   using difference_type = typename std::iterator_traits<It>::difference_type;
61   using pointer = It;
62   using reference = typename std::iterator_traits<It>::reference;
63 
InputIterator()64   InputIterator() : it_() {}
InputIterator(It it)65   explicit InputIterator(It it) : it_(it) {}
66 
operator *() const67   reference operator*() const { return *it_; }
operator ->() const68   pointer operator->() const { return it_; }
69 
operator ++()70   InputIterator& operator++() {
71     ++it_;
72     return *this;
73   }
operator ++(int)74   InputIterator operator++(int) {
75     InputIterator tmp(*this);
76     ++(*this);
77     return tmp;
78   }
79 
operator ==(const InputIterator & lhs,const InputIterator & rhs)80   friend bool operator==(const InputIterator& lhs, const InputIterator& rhs) {
81     return lhs.it_ == rhs.it_;
82   }
operator !=(const InputIterator & lhs,const InputIterator & rhs)83   friend bool operator!=(const InputIterator& lhs, const InputIterator& rhs) {
84     return !(lhs == rhs);
85   }
86 
87  private:
88   It it_;
89 };
90 
91 template <typename It>
MakeInputIterator(It it)92 InputIterator<It> MakeInputIterator(It it) {
93   return InputIterator<It>(it);
94 }
95 
96 class Emplaceable {
97  public:
Emplaceable()98   Emplaceable() : Emplaceable(0, 0.0) {}
Emplaceable(int i,double d)99   Emplaceable(int i, double d) : int_(i), double_(d) {}
Emplaceable(Emplaceable && other)100   Emplaceable(Emplaceable&& other) : int_(other.int_), double_(other.double_) {
101     other.int_ = 0;
102     other.double_ = 0.0;
103   }
104   Emplaceable(const Emplaceable&) = delete;
105   Emplaceable& operator=(const Emplaceable&) = delete;
106 
operator =(Emplaceable && other)107   Emplaceable& operator=(Emplaceable&& other) {
108     int_ = other.int_;
109     other.int_ = 0;
110     double_ = other.double_;
111     other.double_ = 0.0;
112     return *this;
113   }
114 
operator ==(const Emplaceable & lhs,const Emplaceable & rhs)115   friend bool operator==(const Emplaceable& lhs, const Emplaceable& rhs) {
116     return std::tie(lhs.int_, lhs.double_) == std::tie(rhs.int_, rhs.double_);
117   }
118 
operator <(const Emplaceable & lhs,const Emplaceable & rhs)119   friend bool operator<(const Emplaceable& lhs, const Emplaceable& rhs) {
120     return std::tie(lhs.int_, lhs.double_) < std::tie(rhs.int_, rhs.double_);
121   }
122 
123  private:
124   int int_;
125   double double_;
126 };
127 
128 struct TemplateConstructor {
129   template <typename T>
TemplateConstructorbase::internal::__anon990172030111::TemplateConstructor130   TemplateConstructor(const T&) {}
131 
operator <(const TemplateConstructor &,const TemplateConstructor &)132   friend bool operator<(const TemplateConstructor&,
133                         const TemplateConstructor&) {
134     return false;
135   }
136 };
137 
138 class NonDefaultConstructibleCompare {
139  public:
NonDefaultConstructibleCompare(int)140   explicit NonDefaultConstructibleCompare(int) {}
141 
142   template <typename T>
operator ()(const T & lhs,const T & rhs) const143   bool operator()(const T& lhs, const T& rhs) const {
144     return std::less<T>()(lhs, rhs);
145   }
146 };
147 
148 template <class PairType>
149 struct LessByFirst {
operator ()base::internal::__anon990172030111::LessByFirst150   bool operator()(const PairType& lhs, const PairType& rhs) const {
151     return lhs.first < rhs.first;
152   }
153 };
154 
155 // Common test trees.
156 template <typename ContainerT>
157 using TypedTree = flat_tree<typename ContainerT::value_type,
158                             std::identity,
159                             std::less<>,
160                             ContainerT>;
161 using IntTree = TypedTree<std::vector<int>>;
162 using IntPair = std::pair<int, int>;
163 using IntPairTree = flat_tree<IntPair,
164                               std::identity,
165                               LessByFirst<IntPair>,
166                               std::vector<IntPair>>;
167 using MoveOnlyTree = flat_tree<MoveOnlyInt,
168                                std::identity,
169                                std::less<>,
170                                std::vector<MoveOnlyInt>>;
171 using EmplaceableTree = flat_tree<Emplaceable,
172                                   std::identity,
173                                   std::less<>,
174                                   std::vector<Emplaceable>>;
175 using ReversedTree =
176     flat_tree<int, std::identity, std::greater<int>, std::vector<int>>;
177 
178 using TreeWithStrangeCompare = flat_tree<int,
179                                          std::identity,
180                                          NonDefaultConstructibleCompare,
181                                          std::vector<int>>;
182 
183 using ::testing::ElementsAre;
184 using ::testing::IsEmpty;
185 
186 }  // namespace
187 
188 template <typename T>
189 class FlatTreeTest : public testing::Test {};
190 TYPED_TEST_SUITE_P(FlatTreeTest);
191 
192 // Tests that the compiler generated move operators propagrate noexcept
193 // specifiers.
TEST(FlatTree,NoExcept)194 TEST(FlatTree, NoExcept) {
195   struct MoveThrows {
196     MoveThrows(MoveThrows&&) noexcept(false) {}
197     MoveThrows& operator=(MoveThrows&&) noexcept(false) { return *this; }
198   };
199 
200   using MoveThrowsTree = flat_tree<MoveThrows, std::identity, std::less<>,
201                                    std::array<MoveThrows, 1>>;
202 
203   static_assert(std::is_nothrow_move_constructible_v<IntTree>,
204                 "Error: IntTree is not nothrow move constructible");
205   static_assert(std::is_nothrow_move_assignable_v<IntTree>,
206                 "Error: IntTree is not nothrow move assignable");
207 
208   static_assert(!std::is_nothrow_move_constructible_v<MoveThrowsTree>,
209                 "Error: MoveThrowsTree is nothrow move constructible");
210   static_assert(!std::is_nothrow_move_assignable_v<MoveThrowsTree>,
211                 "Error: MoveThrowsTree is nothrow move assignable");
212 }
213 
214 // ----------------------------------------------------------------------------
215 // Class.
216 
217 // Check that flat_tree and its iterators can be instantiated with an
218 // incomplete type.
219 
TEST(FlatTree,IncompleteType)220 TEST(FlatTree, IncompleteType) {
221   struct A {
222     using Tree = flat_tree<A, std::identity, std::less<A>, std::vector<A>>;
223     int data;
224     Tree set_with_incomplete_type;
225     Tree::iterator it;
226     Tree::const_iterator cit;
227 
228     // We do not declare operator< because clang complains that it's unused.
229   };
230 
231   A a;
232 }
233 
TEST(FlatTree,Stability)234 TEST(FlatTree, Stability) {
235   using Pair = std::pair<int, int>;
236 
237   using Tree =
238       flat_tree<Pair, std::identity, LessByFirst<Pair>, std::vector<Pair>>;
239 
240   // Constructors are stable.
241   Tree cont({{0, 0}, {1, 0}, {0, 1}, {2, 0}, {0, 2}, {1, 1}});
242 
243   auto AllOfSecondsAreZero = [&cont] {
244     return ranges::all_of(cont,
245                           [](const Pair& elem) { return elem.second == 0; });
246   };
247 
248   EXPECT_TRUE(AllOfSecondsAreZero()) << "constructor should be stable";
249 
250   // Should not replace existing.
251   cont.insert(Pair(0, 2));
252   cont.insert(Pair(1, 2));
253   cont.insert(Pair(2, 2));
254 
255   EXPECT_TRUE(AllOfSecondsAreZero()) << "insert should be stable";
256 
257   cont.insert(Pair(3, 0));
258   cont.insert(Pair(3, 2));
259 
260   EXPECT_TRUE(AllOfSecondsAreZero()) << "insert should be stable";
261 }
262 
263 // ----------------------------------------------------------------------------
264 // Types.
265 
266 // key_type
267 // key_compare
268 // value_type
269 // value_compare
270 // pointer
271 // const_pointer
272 // reference
273 // const_reference
274 // size_type
275 // difference_type
276 // iterator
277 // const_iterator
278 // reverse_iterator
279 // const_reverse_iterator
280 
TEST(FlatTree,Types)281 TEST(FlatTree, Types) {
282   // These are guaranteed to be portable.
283   static_assert((std::is_same_v<int, IntTree::key_type>), "");
284   static_assert((std::is_same_v<int, IntTree::value_type>), "");
285   static_assert((std::is_same_v<std::less<>, IntTree::key_compare>), "");
286   static_assert((std::is_same_v<int&, IntTree::reference>), "");
287   static_assert((std::is_same_v<const int&, IntTree::const_reference>), "");
288   static_assert((std::is_same_v<int*, IntTree::pointer>), "");
289   static_assert((std::is_same_v<const int*, IntTree::const_pointer>), "");
290 }
291 
292 // ----------------------------------------------------------------------------
293 // Lifetime.
294 
295 // flat_tree()
296 // flat_tree(const Compare& comp)
297 
TYPED_TEST_P(FlatTreeTest,DefaultConstructor)298 TYPED_TEST_P(FlatTreeTest, DefaultConstructor) {
299   {
300     TypedTree<TypeParam> cont;
301     EXPECT_THAT(cont, ElementsAre());
302   }
303 
304   {
305     TreeWithStrangeCompare cont(NonDefaultConstructibleCompare(0));
306     EXPECT_THAT(cont, ElementsAre());
307   }
308 }
309 
310 // flat_tree(const flat_tree& x)
311 
TYPED_TEST_P(FlatTreeTest,CopyConstructor)312 TYPED_TEST_P(FlatTreeTest, CopyConstructor) {
313   TypedTree<TypeParam> original({1, 2, 3, 4});
314   TypedTree<TypeParam> copied(original);
315 
316   EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4));
317 
318   EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4));
319   EXPECT_THAT(original, ElementsAre(1, 2, 3, 4));
320   EXPECT_EQ(original, copied);
321 }
322 
323 // flat_tree(flat_tree&& x)
324 
TEST(FlatTree,MoveConstructor)325 TEST(FlatTree, MoveConstructor) {
326   int input_range[] = {1, 2, 3, 4};
327 
328   MoveOnlyTree original(std::begin(input_range), std::end(input_range));
329   MoveOnlyTree moved(std::move(original));
330 
331   EXPECT_EQ(1U, moved.count(MoveOnlyInt(1)));
332   EXPECT_EQ(1U, moved.count(MoveOnlyInt(2)));
333   EXPECT_EQ(1U, moved.count(MoveOnlyInt(3)));
334   EXPECT_EQ(1U, moved.count(MoveOnlyInt(4)));
335 }
336 
337 // flat_tree(InputIterator first,
338 //           InputIterator last,
339 //           const Compare& comp = Compare())
340 
TEST(FlatTree,RangeConstructor)341 TEST(FlatTree, RangeConstructor) {
342   {
343     IntPair input_vals[] = {{1, 1}, {1, 2}, {2, 1}, {2, 2}, {1, 3},
344                             {2, 3}, {3, 1}, {3, 2}, {3, 3}};
345 
346     IntPairTree first_of(MakeInputIterator(std::begin(input_vals)),
347                          MakeInputIterator(std::end(input_vals)));
348     EXPECT_THAT(first_of,
349                 ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1)));
350   }
351   {
352     TreeWithStrangeCompare::value_type input_vals[] = {1, 1, 1, 2, 2,
353                                                        2, 3, 3, 3};
354 
355     TreeWithStrangeCompare cont(MakeInputIterator(std::begin(input_vals)),
356                                 MakeInputIterator(std::end(input_vals)),
357                                 NonDefaultConstructibleCompare(0));
358     EXPECT_THAT(cont, ElementsAre(1, 2, 3));
359   }
360 }
361 
362 // flat_tree(const container_type&)
363 
TYPED_TEST_P(FlatTreeTest,ContainerCopyConstructor)364 TYPED_TEST_P(FlatTreeTest, ContainerCopyConstructor) {
365   TypeParam items = {1, 2, 3, 4};
366   TypedTree<TypeParam> tree(items);
367 
368   EXPECT_THAT(tree, ElementsAre(1, 2, 3, 4));
369   EXPECT_THAT(items, ElementsAre(1, 2, 3, 4));
370 }
371 
372 // flat_tree(container_type&&)
373 
TEST(FlatTree,ContainerMoveConstructor)374 TEST(FlatTree, ContainerMoveConstructor) {
375   using Pair = std::pair<int, MoveOnlyInt>;
376 
377   // Construct an unsorted vector with a duplicate item in it. Sorted by the
378   // first item, the second allows us to test for stability. Using a move
379   // only type to ensure the vector is not copied.
380   std::vector<Pair> storage;
381   storage.push_back(Pair(2, MoveOnlyInt(0)));
382   storage.push_back(Pair(1, MoveOnlyInt(0)));
383   storage.push_back(Pair(2, MoveOnlyInt(1)));
384 
385   using Tree =
386       flat_tree<Pair, std::identity, LessByFirst<Pair>, std::vector<Pair>>;
387   Tree tree(std::move(storage));
388 
389   // The list should be two items long, with only the first "2" saved.
390   ASSERT_EQ(2u, tree.size());
391   const Pair& zeroth = *tree.begin();
392   ASSERT_EQ(1, zeroth.first);
393   ASSERT_EQ(0, zeroth.second.data());
394 
395   const Pair& first = *(tree.begin() + 1);
396   ASSERT_EQ(2, first.first);
397   ASSERT_EQ(0, first.second.data());
398 }
399 
400 // flat_tree(std::initializer_list<value_type> ilist,
401 //           const Compare& comp = Compare())
402 
TYPED_TEST_P(FlatTreeTest,InitializerListConstructor)403 TYPED_TEST_P(FlatTreeTest, InitializerListConstructor) {
404   {
405     TypedTree<TypeParam> cont({1, 2, 3, 4, 5, 6, 10, 8});
406     EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10));
407   }
408   {
409     TypedTree<TypeParam> cont({1, 2, 3, 4, 5, 6, 10, 8});
410     EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10));
411   }
412   {
413     TreeWithStrangeCompare cont({1, 2, 3, 4, 5, 6, 10, 8},
414                                 NonDefaultConstructibleCompare(0));
415     EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10));
416   }
417   {
418     IntPairTree first_of({{1, 1}, {2, 1}, {1, 2}});
419     EXPECT_THAT(first_of, ElementsAre(IntPair(1, 1), IntPair(2, 1)));
420   }
421 }
422 
423 // flat_tree(sorted_unique_t,
424 //           InputIterator first,
425 //           InputIterator last,
426 //           const Compare& comp = Compare())
427 
TEST(FlatTree,SortedUniqueRangeConstructor)428 TEST(FlatTree, SortedUniqueRangeConstructor) {
429   {
430     IntPair input_vals[] = {{1, 1}, {2, 1}, {3, 1}};
431 
432     IntPairTree first_of(sorted_unique,
433                          MakeInputIterator(std::begin(input_vals)),
434                          MakeInputIterator(std::end(input_vals)));
435     EXPECT_THAT(first_of,
436                 ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1)));
437   }
438   {
439     TreeWithStrangeCompare::value_type input_vals[] = {1, 2, 3};
440 
441     TreeWithStrangeCompare cont(sorted_unique,
442                                 MakeInputIterator(std::begin(input_vals)),
443                                 MakeInputIterator(std::end(input_vals)),
444                                 NonDefaultConstructibleCompare(0));
445     EXPECT_THAT(cont, ElementsAre(1, 2, 3));
446   }
447 }
448 
449 // flat_tree(sorted_unique_t, const container_type&)
450 
TYPED_TEST_P(FlatTreeTest,SortedUniqueContainerCopyConstructor)451 TYPED_TEST_P(FlatTreeTest, SortedUniqueContainerCopyConstructor) {
452   TypeParam items = {1, 2, 3, 4};
453   TypedTree<TypeParam> tree(sorted_unique, items);
454 
455   EXPECT_THAT(tree, ElementsAre(1, 2, 3, 4));
456   EXPECT_THAT(items, ElementsAre(1, 2, 3, 4));
457 }
458 
459 // flat_tree(sorted_unique_t, std::vector<value_type>&&)
460 
TEST(FlatTree,SortedUniqueVectorMoveConstructor)461 TEST(FlatTree, SortedUniqueVectorMoveConstructor) {
462   using Pair = std::pair<int, MoveOnlyInt>;
463 
464   std::vector<Pair> storage;
465   storage.push_back(Pair(1, MoveOnlyInt(0)));
466   storage.push_back(Pair(2, MoveOnlyInt(0)));
467 
468   using Tree =
469       flat_tree<Pair, std::identity, LessByFirst<Pair>, std::vector<Pair>>;
470   Tree tree(sorted_unique, std::move(storage));
471 
472   ASSERT_EQ(2u, tree.size());
473   const Pair& zeroth = *tree.begin();
474   ASSERT_EQ(1, zeroth.first);
475   ASSERT_EQ(0, zeroth.second.data());
476 
477   const Pair& first = *(tree.begin() + 1);
478   ASSERT_EQ(2, first.first);
479   ASSERT_EQ(0, first.second.data());
480 }
481 
482 // flat_tree(sorted_unique_t,
483 //           std::initializer_list<value_type> ilist,
484 //           const Compare& comp = Compare())
485 
TYPED_TEST_P(FlatTreeTest,SortedUniqueInitializerListConstructor)486 TYPED_TEST_P(FlatTreeTest, SortedUniqueInitializerListConstructor) {
487   {
488     TypedTree<TypeParam> cont(sorted_unique, {1, 2, 3, 4, 5, 6, 8, 10});
489     EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10));
490   }
491   {
492     TypedTree<TypeParam> cont(sorted_unique, {1, 2, 3, 4, 5, 6, 8, 10});
493     EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10));
494   }
495   {
496     TreeWithStrangeCompare cont(sorted_unique, {1, 2, 3, 4, 5, 6, 8, 10},
497                                 NonDefaultConstructibleCompare(0));
498     EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10));
499   }
500   {
501     IntPairTree first_of(sorted_unique, {{1, 1}, {2, 1}});
502     EXPECT_THAT(first_of, ElementsAre(IntPair(1, 1), IntPair(2, 1)));
503   }
504 }
505 
506 // ----------------------------------------------------------------------------
507 // Assignments.
508 
509 // flat_tree& operator=(const flat_tree&)
510 
TYPED_TEST_P(FlatTreeTest,CopyAssignable)511 TYPED_TEST_P(FlatTreeTest, CopyAssignable) {
512   TypedTree<TypeParam> original({1, 2, 3, 4});
513   TypedTree<TypeParam> copied;
514   copied = original;
515 
516   EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4));
517   EXPECT_THAT(original, ElementsAre(1, 2, 3, 4));
518   EXPECT_EQ(original, copied);
519 }
520 
521 // flat_tree& operator=(flat_tree&&)
522 
TEST(FlatTree,MoveAssignable)523 TEST(FlatTree, MoveAssignable) {
524   int input_range[] = {1, 2, 3, 4};
525 
526   MoveOnlyTree original(std::begin(input_range), std::end(input_range));
527   MoveOnlyTree moved;
528   moved = std::move(original);
529 
530   EXPECT_EQ(1U, moved.count(MoveOnlyInt(1)));
531   EXPECT_EQ(1U, moved.count(MoveOnlyInt(2)));
532   EXPECT_EQ(1U, moved.count(MoveOnlyInt(3)));
533   EXPECT_EQ(1U, moved.count(MoveOnlyInt(4)));
534 }
535 
536 // flat_tree& operator=(std::initializer_list<value_type> ilist)
537 
TYPED_TEST_P(FlatTreeTest,InitializerListAssignable)538 TYPED_TEST_P(FlatTreeTest, InitializerListAssignable) {
539   TypedTree<TypeParam> cont({0});
540   cont = {1, 2, 3, 4, 5, 6, 10, 8};
541 
542   EXPECT_EQ(0U, cont.count(0));
543   EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10));
544 }
545 
546 // --------------------------------------------------------------------------
547 // Memory management.
548 
549 // void reserve(size_type new_capacity)
550 
TEST(FlatTreeTest,Reserve)551 TEST(FlatTreeTest, Reserve) {
552   IntTree cont({1, 2, 3});
553 
554   cont.reserve(5);
555   EXPECT_LE(5U, cont.capacity());
556 }
557 
558 // size_type capacity() const
559 
TEST(FlatTreeTest,Capacity)560 TEST(FlatTreeTest, Capacity) {
561   IntTree cont({1, 2, 3});
562 
563   EXPECT_LE(cont.size(), cont.capacity());
564   cont.reserve(5);
565   EXPECT_LE(cont.size(), cont.capacity());
566 }
567 
568 // void shrink_to_fit()
569 
TEST(FlatTreeTest,ShrinkToFit)570 TEST(FlatTreeTest, ShrinkToFit) {
571   IntTree cont({1, 2, 3});
572 
573   IntTree::size_type capacity_before = cont.capacity();
574   cont.shrink_to_fit();
575   EXPECT_GE(capacity_before, cont.capacity());
576 }
577 
578 // ----------------------------------------------------------------------------
579 // Size management.
580 
581 // void clear()
582 
TYPED_TEST_P(FlatTreeTest,Clear)583 TYPED_TEST_P(FlatTreeTest, Clear) {
584   TypedTree<TypeParam> cont({1, 2, 3, 4, 5, 6, 7, 8});
585   cont.clear();
586   EXPECT_THAT(cont, ElementsAre());
587 }
588 
589 // size_type size() const
590 
TYPED_TEST_P(FlatTreeTest,Size)591 TYPED_TEST_P(FlatTreeTest, Size) {
592   TypedTree<TypeParam> cont;
593 
594   EXPECT_EQ(0U, cont.size());
595   cont.insert(2);
596   EXPECT_EQ(1U, cont.size());
597   cont.insert(1);
598   EXPECT_EQ(2U, cont.size());
599   cont.insert(3);
600   EXPECT_EQ(3U, cont.size());
601   cont.erase(cont.begin());
602   EXPECT_EQ(2U, cont.size());
603   cont.erase(cont.begin());
604   EXPECT_EQ(1U, cont.size());
605   cont.erase(cont.begin());
606   EXPECT_EQ(0U, cont.size());
607 }
608 
609 // bool empty() const
610 
TYPED_TEST_P(FlatTreeTest,Empty)611 TYPED_TEST_P(FlatTreeTest, Empty) {
612   TypedTree<TypeParam> cont;
613 
614   EXPECT_TRUE(cont.empty());
615   cont.insert(1);
616   EXPECT_FALSE(cont.empty());
617   cont.clear();
618   EXPECT_TRUE(cont.empty());
619 }
620 
621 // ----------------------------------------------------------------------------
622 // Iterators.
623 
624 // iterator begin()
625 // const_iterator begin() const
626 // iterator end()
627 // const_iterator end() const
628 //
629 // reverse_iterator rbegin()
630 // const_reverse_iterator rbegin() const
631 // reverse_iterator rend()
632 // const_reverse_iterator rend() const
633 //
634 // const_iterator cbegin() const
635 // const_iterator cend() const
636 // const_reverse_iterator crbegin() const
637 // const_reverse_iterator crend() const
638 
TYPED_TEST_P(FlatTreeTest,Iterators)639 TYPED_TEST_P(FlatTreeTest, Iterators) {
640   TypedTree<TypeParam> cont({1, 2, 3, 4, 5, 6, 7, 8});
641 
642   auto size =
643       static_cast<typename TypedTree<TypeParam>::difference_type>(cont.size());
644 
645   EXPECT_EQ(size, std::distance(cont.begin(), cont.end()));
646   EXPECT_EQ(size, std::distance(cont.cbegin(), cont.cend()));
647   EXPECT_EQ(size, std::distance(cont.rbegin(), cont.rend()));
648   EXPECT_EQ(size, std::distance(cont.crbegin(), cont.crend()));
649 
650   {
651     auto it = cont.begin();
652     auto c_it = cont.cbegin();
653     EXPECT_EQ(it, c_it);
654     for (int j = 1; it != cont.end(); ++it, ++c_it, ++j) {
655       EXPECT_EQ(j, *it);
656       EXPECT_EQ(j, *c_it);
657     }
658   }
659   {
660     auto rit = cont.rbegin();
661     auto c_rit = cont.crbegin();
662     EXPECT_EQ(rit, c_rit);
663     for (int j = static_cast<int>(size); rit != cont.rend();
664          ++rit, ++c_rit, --j) {
665       EXPECT_EQ(j, *rit);
666       EXPECT_EQ(j, *c_rit);
667     }
668   }
669 }
670 
671 // ----------------------------------------------------------------------------
672 // Insert operations.
673 
674 // pair<iterator, bool> insert(const value_type& val)
675 
TYPED_TEST_P(FlatTreeTest,InsertLValue)676 TYPED_TEST_P(FlatTreeTest, InsertLValue) {
677   TypedTree<TypeParam> cont;
678 
679   int value = 2;
680   std::pair<typename TypedTree<TypeParam>::iterator, bool> result =
681       cont.insert(value);
682   EXPECT_TRUE(result.second);
683   EXPECT_EQ(cont.begin(), result.first);
684   EXPECT_EQ(1U, cont.size());
685   EXPECT_EQ(2, *result.first);
686 
687   value = 1;
688   result = cont.insert(value);
689   EXPECT_TRUE(result.second);
690   EXPECT_EQ(cont.begin(), result.first);
691   EXPECT_EQ(2U, cont.size());
692   EXPECT_EQ(1, *result.first);
693 
694   value = 3;
695   result = cont.insert(value);
696   EXPECT_TRUE(result.second);
697   EXPECT_EQ(std::prev(cont.end()), result.first);
698   EXPECT_EQ(3U, cont.size());
699   EXPECT_EQ(3, *result.first);
700 
701   value = 3;
702   result = cont.insert(value);
703   EXPECT_FALSE(result.second);
704   EXPECT_EQ(std::prev(cont.end()), result.first);
705   EXPECT_EQ(3U, cont.size());
706   EXPECT_EQ(3, *result.first);
707 }
708 
709 // pair<iterator, bool> insert(value_type&& val)
710 
TEST(FlatTree,InsertRValue)711 TEST(FlatTree, InsertRValue) {
712   MoveOnlyTree cont;
713 
714   std::pair<MoveOnlyTree::iterator, bool> result = cont.insert(MoveOnlyInt(2));
715   EXPECT_TRUE(result.second);
716   EXPECT_EQ(cont.begin(), result.first);
717   EXPECT_EQ(1U, cont.size());
718   EXPECT_EQ(2, result.first->data());
719 
720   result = cont.insert(MoveOnlyInt(1));
721   EXPECT_TRUE(result.second);
722   EXPECT_EQ(cont.begin(), result.first);
723   EXPECT_EQ(2U, cont.size());
724   EXPECT_EQ(1, result.first->data());
725 
726   result = cont.insert(MoveOnlyInt(3));
727   EXPECT_TRUE(result.second);
728   EXPECT_EQ(std::prev(cont.end()), result.first);
729   EXPECT_EQ(3U, cont.size());
730   EXPECT_EQ(3, result.first->data());
731 
732   result = cont.insert(MoveOnlyInt(3));
733   EXPECT_FALSE(result.second);
734   EXPECT_EQ(std::prev(cont.end()), result.first);
735   EXPECT_EQ(3U, cont.size());
736   EXPECT_EQ(3, result.first->data());
737 }
738 
739 // iterator insert(const_iterator position_hint, const value_type& val)
740 
TYPED_TEST_P(FlatTreeTest,InsertPositionLValue)741 TYPED_TEST_P(FlatTreeTest, InsertPositionLValue) {
742   TypedTree<TypeParam> cont;
743 
744   auto result = cont.insert(cont.cend(), 2);
745   EXPECT_EQ(cont.begin(), result);
746   EXPECT_EQ(1U, cont.size());
747   EXPECT_EQ(2, *result);
748 
749   result = cont.insert(cont.cend(), 1);
750   EXPECT_EQ(cont.begin(), result);
751   EXPECT_EQ(2U, cont.size());
752   EXPECT_EQ(1, *result);
753 
754   result = cont.insert(cont.cend(), 3);
755   EXPECT_EQ(std::prev(cont.end()), result);
756   EXPECT_EQ(3U, cont.size());
757   EXPECT_EQ(3, *result);
758 
759   result = cont.insert(cont.cend(), 3);
760   EXPECT_EQ(std::prev(cont.end()), result);
761   EXPECT_EQ(3U, cont.size());
762   EXPECT_EQ(3, *result);
763 }
764 
765 // iterator insert(const_iterator position_hint, value_type&& val)
766 
TEST(FlatTree,InsertPositionRValue)767 TEST(FlatTree, InsertPositionRValue) {
768   MoveOnlyTree cont;
769 
770   auto result = cont.insert(cont.cend(), MoveOnlyInt(2));
771   EXPECT_EQ(cont.begin(), result);
772   EXPECT_EQ(1U, cont.size());
773   EXPECT_EQ(2, result->data());
774 
775   result = cont.insert(cont.cend(), MoveOnlyInt(1));
776   EXPECT_EQ(cont.begin(), result);
777   EXPECT_EQ(2U, cont.size());
778   EXPECT_EQ(1, result->data());
779 
780   result = cont.insert(cont.cend(), MoveOnlyInt(3));
781   EXPECT_EQ(std::prev(cont.end()), result);
782   EXPECT_EQ(3U, cont.size());
783   EXPECT_EQ(3, result->data());
784 
785   result = cont.insert(cont.cend(), MoveOnlyInt(3));
786   EXPECT_EQ(std::prev(cont.end()), result);
787   EXPECT_EQ(3U, cont.size());
788   EXPECT_EQ(3, result->data());
789 }
790 
791 // template <class InputIterator>
792 //   void insert(InputIterator first, InputIterator last);
793 
TEST(FlatTree,InsertIterIter)794 TEST(FlatTree, InsertIterIter) {
795   struct GetKeyFromIntIntPair {
796     const int& operator()(const std::pair<int, int>& p) const {
797       return p.first;
798     }
799   };
800 
801   using IntIntMap = flat_tree<int, GetKeyFromIntIntPair, std::less<int>,
802                               std::vector<IntPair>>;
803 
804   {
805     IntIntMap cont;
806     IntPair int_pairs[] = {{3, 1}, {1, 1}, {4, 1}, {2, 1}};
807     UNSAFE_BUFFERS(cont.insert(std::begin(int_pairs), std::end(int_pairs)));
808     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
809                                   IntPair(4, 1)));
810   }
811 
812   {
813     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
814     std::vector<IntPair> int_pairs;
815     UNSAFE_BUFFERS(cont.insert(std::begin(int_pairs), std::end(int_pairs)));
816     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
817                                   IntPair(4, 1)));
818   }
819 
820   {
821     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
822     IntPair int_pairs[] = {{1, 1}};
823     UNSAFE_BUFFERS(cont.insert(std::begin(int_pairs), std::end(int_pairs)));
824     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
825                                   IntPair(4, 1)));
826   }
827 
828   {
829     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
830     IntPair int_pairs[] = {{5, 1}};
831     UNSAFE_BUFFERS(cont.insert(std::begin(int_pairs), std::end(int_pairs)));
832     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
833                                   IntPair(4, 1), IntPair(5, 1)));
834   }
835 
836   {
837     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
838     IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}};
839     UNSAFE_BUFFERS(cont.insert(std::begin(int_pairs), std::end(int_pairs)));
840     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
841                                   IntPair(4, 1)));
842   }
843 
844   {
845     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
846     IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}, {7, 2}, {6, 2},
847                            {8, 2}, {5, 2}, {5, 3}, {6, 3}, {7, 3}, {8, 3}};
848     UNSAFE_BUFFERS(cont.insert(std::begin(int_pairs), std::end(int_pairs)));
849     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
850                                   IntPair(4, 1), IntPair(5, 2), IntPair(6, 2),
851                                   IntPair(7, 2), IntPair(8, 2)));
852   }
853 }
854 
855 // template <class Range>
856 //   void insert_range(Range range);
857 
TEST(FlatTree,InsertRange)858 TEST(FlatTree, InsertRange) {
859   struct GetKeyFromIntIntPair {
860     const int& operator()(const std::pair<int, int>& p) const {
861       return p.first;
862     }
863   };
864 
865   using IntIntMap = flat_tree<int, GetKeyFromIntIntPair, std::less<int>,
866                               std::vector<IntPair>>;
867 
868   {
869     IntIntMap cont;
870     IntPair int_pairs[] = {{3, 1}, {1, 1}, {4, 1}, {2, 1}};
871     cont.insert_range(int_pairs);
872     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
873                                   IntPair(4, 1)));
874   }
875 
876   {
877     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
878     std::vector<IntPair> int_pairs;
879     cont.insert_range(int_pairs);
880     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
881                                   IntPair(4, 1)));
882   }
883 
884   {
885     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
886     IntPair int_pairs[] = {{1, 1}};
887     cont.insert_range(int_pairs);
888     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
889                                   IntPair(4, 1)));
890   }
891 
892   {
893     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
894     IntPair int_pairs[] = {{5, 1}};
895     cont.insert_range(int_pairs);
896     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
897                                   IntPair(4, 1), IntPair(5, 1)));
898   }
899 
900   {
901     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
902     IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}};
903     cont.insert_range(int_pairs);
904     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
905                                   IntPair(4, 1)));
906   }
907 
908   {
909     IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}});
910     IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}, {7, 2}, {6, 2},
911                            {8, 2}, {5, 2}, {5, 3}, {6, 3}, {7, 3}, {8, 3}};
912     cont.insert_range(int_pairs);
913     EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
914                                   IntPair(4, 1), IntPair(5, 2), IntPair(6, 2),
915                                   IntPair(7, 2), IntPair(8, 2)));
916   }
917 }
918 
919 // template <class... Args>
920 // pair<iterator, bool> emplace(Args&&... args)
921 
TYPED_TEST_P(FlatTreeTest,Emplace)922 TYPED_TEST_P(FlatTreeTest, Emplace) {
923   {
924     EmplaceableTree cont;
925 
926     std::pair<EmplaceableTree::iterator, bool> result = cont.emplace();
927     EXPECT_TRUE(result.second);
928     EXPECT_EQ(cont.begin(), result.first);
929     EXPECT_EQ(1U, cont.size());
930     EXPECT_EQ(Emplaceable(), *cont.begin());
931 
932     result = cont.emplace(2, 3.5);
933     EXPECT_TRUE(result.second);
934     EXPECT_EQ(std::next(cont.begin()), result.first);
935     EXPECT_EQ(2U, cont.size());
936     EXPECT_EQ(Emplaceable(2, 3.5), *result.first);
937 
938     result = cont.emplace(2, 3.5);
939     EXPECT_FALSE(result.second);
940     EXPECT_EQ(std::next(cont.begin()), result.first);
941     EXPECT_EQ(2U, cont.size());
942     EXPECT_EQ(Emplaceable(2, 3.5), *result.first);
943   }
944   {
945     TypedTree<TypeParam> cont;
946 
947     std::pair<typename TypedTree<TypeParam>::iterator, bool> result =
948         cont.emplace(2);
949     EXPECT_TRUE(result.second);
950     EXPECT_EQ(cont.begin(), result.first);
951     EXPECT_EQ(1U, cont.size());
952     EXPECT_EQ(2, *result.first);
953   }
954 }
955 
956 // template <class... Args>
957 // iterator emplace_hint(const_iterator position_hint, Args&&... args)
958 
TYPED_TEST_P(FlatTreeTest,EmplacePosition)959 TYPED_TEST_P(FlatTreeTest, EmplacePosition) {
960   {
961     EmplaceableTree cont;
962 
963     auto result = cont.emplace_hint(cont.cend());
964     EXPECT_EQ(cont.begin(), result);
965     EXPECT_EQ(1U, cont.size());
966     EXPECT_EQ(Emplaceable(), *cont.begin());
967 
968     result = cont.emplace_hint(cont.cend(), 2, 3.5);
969     EXPECT_EQ(std::next(cont.begin()), result);
970     EXPECT_EQ(2U, cont.size());
971     EXPECT_EQ(Emplaceable(2, 3.5), *result);
972 
973     result = cont.emplace_hint(cont.cbegin(), 2, 3.5);
974     EXPECT_EQ(std::next(cont.begin()), result);
975     EXPECT_EQ(2U, cont.size());
976     EXPECT_EQ(Emplaceable(2, 3.5), *result);
977   }
978   {
979     TypedTree<TypeParam> cont;
980 
981     auto result = cont.emplace_hint(cont.cend(), 2);
982     EXPECT_EQ(cont.begin(), result);
983     EXPECT_EQ(1U, cont.size());
984     EXPECT_EQ(2, *result);
985   }
986 }
987 
988 // ----------------------------------------------------------------------------
989 // Underlying type operations.
990 
991 // underlying_type extract() &&
TYPED_TEST_P(FlatTreeTest,Extract)992 TYPED_TEST_P(FlatTreeTest, Extract) {
993   TypedTree<TypeParam> cont;
994   cont.emplace(3);
995   cont.emplace(1);
996   cont.emplace(2);
997   cont.emplace(4);
998 
999   TypeParam body = std::move(cont).extract();
1000   EXPECT_THAT(cont, IsEmpty());
1001   EXPECT_THAT(body, ElementsAre(1, 2, 3, 4));
1002 }
1003 
1004 // replace(underlying_type&&)
TYPED_TEST_P(FlatTreeTest,Replace)1005 TYPED_TEST_P(FlatTreeTest, Replace) {
1006   TypeParam body = {1, 2, 3, 4};
1007   TypedTree<TypeParam> cont;
1008   cont.replace(std::move(body));
1009 
1010   EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4));
1011 }
1012 
1013 // ----------------------------------------------------------------------------
1014 // Erase operations.
1015 
1016 // iterator erase(const_iterator position_hint)
1017 
TYPED_TEST_P(FlatTreeTest,ErasePosition)1018 TYPED_TEST_P(FlatTreeTest, ErasePosition) {
1019   {
1020     TypedTree<TypeParam> cont({1, 2, 3, 4, 5, 6, 7, 8});
1021 
1022     auto it = cont.erase(std::next(cont.cbegin(), 3));
1023     EXPECT_EQ(std::next(cont.begin(), 3), it);
1024     EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8));
1025 
1026     it = cont.erase(std::next(cont.cbegin(), 0));
1027     EXPECT_EQ(cont.begin(), it);
1028     EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7, 8));
1029 
1030     it = cont.erase(std::next(cont.cbegin(), 5));
1031     EXPECT_EQ(cont.end(), it);
1032     EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7));
1033 
1034     it = cont.erase(std::next(cont.cbegin(), 1));
1035     EXPECT_EQ(std::next(cont.begin()), it);
1036     EXPECT_THAT(cont, ElementsAre(2, 5, 6, 7));
1037 
1038     it = cont.erase(std::next(cont.cbegin(), 2));
1039     EXPECT_EQ(std::next(cont.begin(), 2), it);
1040     EXPECT_THAT(cont, ElementsAre(2, 5, 7));
1041 
1042     it = cont.erase(std::next(cont.cbegin(), 2));
1043     EXPECT_EQ(std::next(cont.begin(), 2), it);
1044     EXPECT_THAT(cont, ElementsAre(2, 5));
1045 
1046     it = cont.erase(std::next(cont.cbegin(), 0));
1047     EXPECT_EQ(std::next(cont.begin(), 0), it);
1048     EXPECT_THAT(cont, ElementsAre(5));
1049 
1050     it = cont.erase(cont.cbegin());
1051     EXPECT_EQ(cont.begin(), it);
1052     EXPECT_EQ(cont.end(), it);
1053   }
1054   //  This is LWG #2059.
1055   //  There is a potential ambiguity between erase with an iterator and erase
1056   //  with a key, if key has a templated constructor.
1057   {
1058     using T = TemplateConstructor;
1059 
1060     flat_tree<T, std::identity, std::less<>, std::vector<T>> cont;
1061     T v(0);
1062 
1063     auto it = cont.find(v);
1064     if (it != cont.end())
1065       cont.erase(it);
1066   }
1067 }
1068 
1069 // iterator erase(const_iterator first, const_iterator last)
1070 
TYPED_TEST_P(FlatTreeTest,EraseRange)1071 TYPED_TEST_P(FlatTreeTest, EraseRange) {
1072   TypedTree<TypeParam> cont({1, 2, 3, 4, 5, 6, 7, 8});
1073 
1074   auto it =
1075       cont.erase(std::next(cont.cbegin(), 5), std::next(cont.cbegin(), 5));
1076   EXPECT_EQ(std::next(cont.begin(), 5), it);
1077   EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
1078 
1079   it = cont.erase(std::next(cont.cbegin(), 3), std::next(cont.cbegin(), 4));
1080   EXPECT_EQ(std::next(cont.begin(), 3), it);
1081   EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8));
1082 
1083   it = cont.erase(std::next(cont.cbegin(), 2), std::next(cont.cbegin(), 5));
1084   EXPECT_EQ(std::next(cont.begin(), 2), it);
1085   EXPECT_THAT(cont, ElementsAre(1, 2, 7, 8));
1086 
1087   it = cont.erase(std::next(cont.cbegin(), 0), std::next(cont.cbegin(), 2));
1088   EXPECT_EQ(std::next(cont.begin(), 0), it);
1089   EXPECT_THAT(cont, ElementsAre(7, 8));
1090 
1091   it = cont.erase(cont.cbegin(), cont.cend());
1092   EXPECT_EQ(cont.begin(), it);
1093   EXPECT_EQ(cont.end(), it);
1094 }
1095 
1096 // size_type erase(const key_type& key)
1097 
TYPED_TEST_P(FlatTreeTest,EraseKey)1098 TYPED_TEST_P(FlatTreeTest, EraseKey) {
1099   TypedTree<TypeParam> cont({1, 2, 3, 4, 5, 6, 7, 8});
1100 
1101   EXPECT_EQ(0U, cont.erase(9));
1102   EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
1103 
1104   EXPECT_EQ(1U, cont.erase(4));
1105   EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8));
1106 
1107   EXPECT_EQ(1U, cont.erase(1));
1108   EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7, 8));
1109 
1110   EXPECT_EQ(1U, cont.erase(8));
1111   EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7));
1112 
1113   EXPECT_EQ(1U, cont.erase(3));
1114   EXPECT_THAT(cont, ElementsAre(2, 5, 6, 7));
1115 
1116   EXPECT_EQ(1U, cont.erase(6));
1117   EXPECT_THAT(cont, ElementsAre(2, 5, 7));
1118 
1119   EXPECT_EQ(1U, cont.erase(7));
1120   EXPECT_THAT(cont, ElementsAre(2, 5));
1121 
1122   EXPECT_EQ(1U, cont.erase(2));
1123   EXPECT_THAT(cont, ElementsAre(5));
1124 
1125   EXPECT_EQ(1U, cont.erase(5));
1126   EXPECT_THAT(cont, ElementsAre());
1127 }
1128 
TYPED_TEST_P(FlatTreeTest,EraseEndDeath)1129 TYPED_TEST_P(FlatTreeTest, EraseEndDeath) {
1130   {
1131     TypedTree<TypeParam> tree;
1132     ASSERT_DEATH_IF_SUPPORTED(tree.erase(tree.cend()), "");
1133   }
1134 
1135   {
1136     TypedTree<TypeParam> tree = {1, 2, 3, 4};
1137     ASSERT_DEATH_IF_SUPPORTED(tree.erase(tree.find(5)), "");
1138   }
1139 }
1140 
1141 // ----------------------------------------------------------------------------
1142 // Comparators.
1143 
1144 // key_compare key_comp() const
1145 
TEST(FlatTree,KeyComp)1146 TEST(FlatTree, KeyComp) {
1147   ReversedTree cont({1, 2, 3, 4, 5});
1148 
1149   EXPECT_TRUE(ranges::is_sorted(cont, cont.key_comp()));
1150   int new_elements[] = {6, 7, 8, 9, 10};
1151   ranges::copy(new_elements, std::inserter(cont, cont.end()));
1152   EXPECT_TRUE(ranges::is_sorted(cont, cont.key_comp()));
1153 }
1154 
1155 // value_compare value_comp() const
1156 
TEST(FlatTree,ValueComp)1157 TEST(FlatTree, ValueComp) {
1158   ReversedTree cont({1, 2, 3, 4, 5});
1159 
1160   EXPECT_TRUE(ranges::is_sorted(cont, cont.value_comp()));
1161   int new_elements[] = {6, 7, 8, 9, 10};
1162   ranges::copy(new_elements, std::inserter(cont, cont.end()));
1163   EXPECT_TRUE(ranges::is_sorted(cont, cont.value_comp()));
1164 }
1165 
1166 // ----------------------------------------------------------------------------
1167 // Search operations.
1168 
1169 // size_type count(const key_type& key) const
1170 
TYPED_TEST_P(FlatTreeTest,Count)1171 TYPED_TEST_P(FlatTreeTest, Count) {
1172   const TypedTree<TypeParam> cont({5, 6, 7, 8, 9, 10, 11, 12});
1173 
1174   EXPECT_EQ(1U, cont.count(5));
1175   EXPECT_EQ(1U, cont.count(6));
1176   EXPECT_EQ(1U, cont.count(7));
1177   EXPECT_EQ(1U, cont.count(8));
1178   EXPECT_EQ(1U, cont.count(9));
1179   EXPECT_EQ(1U, cont.count(10));
1180   EXPECT_EQ(1U, cont.count(11));
1181   EXPECT_EQ(1U, cont.count(12));
1182   EXPECT_EQ(0U, cont.count(4));
1183 }
1184 
1185 // iterator find(const key_type& key)
1186 // const_iterator find(const key_type& key) const
1187 
TYPED_TEST_P(FlatTreeTest,Find)1188 TYPED_TEST_P(FlatTreeTest, Find) {
1189   {
1190     TypedTree<TypeParam> cont({5, 6, 7, 8, 9, 10, 11, 12});
1191 
1192     EXPECT_EQ(cont.begin(), cont.find(5));
1193     EXPECT_EQ(std::next(cont.begin()), cont.find(6));
1194     EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7));
1195     EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8));
1196     EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9));
1197     EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10));
1198     EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11));
1199     EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12));
1200     EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4));
1201   }
1202   {
1203     const TypedTree<TypeParam> cont({5, 6, 7, 8, 9, 10, 11, 12});
1204 
1205     EXPECT_EQ(cont.begin(), cont.find(5));
1206     EXPECT_EQ(std::next(cont.begin()), cont.find(6));
1207     EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7));
1208     EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8));
1209     EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9));
1210     EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10));
1211     EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11));
1212     EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12));
1213     EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4));
1214   }
1215 }
1216 
1217 // bool contains(const key_type& key) const
1218 
TYPED_TEST_P(FlatTreeTest,Contains)1219 TYPED_TEST_P(FlatTreeTest, Contains) {
1220   const TypedTree<TypeParam> cont({5, 6, 7, 8, 9, 10, 11, 12});
1221 
1222   EXPECT_TRUE(cont.contains(5));
1223   EXPECT_TRUE(cont.contains(6));
1224   EXPECT_TRUE(cont.contains(7));
1225   EXPECT_TRUE(cont.contains(8));
1226   EXPECT_TRUE(cont.contains(9));
1227   EXPECT_TRUE(cont.contains(10));
1228   EXPECT_TRUE(cont.contains(11));
1229   EXPECT_TRUE(cont.contains(12));
1230   EXPECT_FALSE(cont.contains(4));
1231 }
1232 
1233 // pair<iterator, iterator> equal_range(const key_type& key)
1234 // pair<const_iterator, const_iterator> equal_range(const key_type& key) const
1235 
TYPED_TEST_P(FlatTreeTest,EqualRange)1236 TYPED_TEST_P(FlatTreeTest, EqualRange) {
1237   {
1238     TypedTree<TypeParam> cont({5, 7, 9, 11, 13, 15, 17, 19});
1239 
1240     std::pair<typename TypedTree<TypeParam>::iterator,
1241               typename TypedTree<TypeParam>::iterator>
1242         result = cont.equal_range(5);
1243     EXPECT_EQ(std::next(cont.begin(), 0), result.first);
1244     EXPECT_EQ(std::next(cont.begin(), 1), result.second);
1245     result = cont.equal_range(7);
1246     EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1247     EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1248     result = cont.equal_range(9);
1249     EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1250     EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1251     result = cont.equal_range(11);
1252     EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1253     EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1254     result = cont.equal_range(13);
1255     EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1256     EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1257     result = cont.equal_range(15);
1258     EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1259     EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1260     result = cont.equal_range(17);
1261     EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1262     EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1263     result = cont.equal_range(19);
1264     EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1265     EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1266     result = cont.equal_range(4);
1267     EXPECT_EQ(std::next(cont.begin(), 0), result.first);
1268     EXPECT_EQ(std::next(cont.begin(), 0), result.second);
1269     result = cont.equal_range(6);
1270     EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1271     EXPECT_EQ(std::next(cont.begin(), 1), result.second);
1272     result = cont.equal_range(8);
1273     EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1274     EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1275     result = cont.equal_range(10);
1276     EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1277     EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1278     result = cont.equal_range(12);
1279     EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1280     EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1281     result = cont.equal_range(14);
1282     EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1283     EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1284     result = cont.equal_range(16);
1285     EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1286     EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1287     result = cont.equal_range(18);
1288     EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1289     EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1290     result = cont.equal_range(20);
1291     EXPECT_EQ(std::next(cont.begin(), 8), result.first);
1292     EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1293   }
1294   {
1295     const TypedTree<TypeParam> cont({5, 7, 9, 11, 13, 15, 17, 19});
1296 
1297     std::pair<typename TypedTree<TypeParam>::const_iterator,
1298               typename TypedTree<TypeParam>::const_iterator>
1299         result = cont.equal_range(5);
1300     EXPECT_EQ(std::next(cont.begin(), 0), result.first);
1301     EXPECT_EQ(std::next(cont.begin(), 1), result.second);
1302     result = cont.equal_range(7);
1303     EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1304     EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1305     result = cont.equal_range(9);
1306     EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1307     EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1308     result = cont.equal_range(11);
1309     EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1310     EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1311     result = cont.equal_range(13);
1312     EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1313     EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1314     result = cont.equal_range(15);
1315     EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1316     EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1317     result = cont.equal_range(17);
1318     EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1319     EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1320     result = cont.equal_range(19);
1321     EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1322     EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1323     result = cont.equal_range(4);
1324     EXPECT_EQ(std::next(cont.begin(), 0), result.first);
1325     EXPECT_EQ(std::next(cont.begin(), 0), result.second);
1326     result = cont.equal_range(6);
1327     EXPECT_EQ(std::next(cont.begin(), 1), result.first);
1328     EXPECT_EQ(std::next(cont.begin(), 1), result.second);
1329     result = cont.equal_range(8);
1330     EXPECT_EQ(std::next(cont.begin(), 2), result.first);
1331     EXPECT_EQ(std::next(cont.begin(), 2), result.second);
1332     result = cont.equal_range(10);
1333     EXPECT_EQ(std::next(cont.begin(), 3), result.first);
1334     EXPECT_EQ(std::next(cont.begin(), 3), result.second);
1335     result = cont.equal_range(12);
1336     EXPECT_EQ(std::next(cont.begin(), 4), result.first);
1337     EXPECT_EQ(std::next(cont.begin(), 4), result.second);
1338     result = cont.equal_range(14);
1339     EXPECT_EQ(std::next(cont.begin(), 5), result.first);
1340     EXPECT_EQ(std::next(cont.begin(), 5), result.second);
1341     result = cont.equal_range(16);
1342     EXPECT_EQ(std::next(cont.begin(), 6), result.first);
1343     EXPECT_EQ(std::next(cont.begin(), 6), result.second);
1344     result = cont.equal_range(18);
1345     EXPECT_EQ(std::next(cont.begin(), 7), result.first);
1346     EXPECT_EQ(std::next(cont.begin(), 7), result.second);
1347     result = cont.equal_range(20);
1348     EXPECT_EQ(std::next(cont.begin(), 8), result.first);
1349     EXPECT_EQ(std::next(cont.begin(), 8), result.second);
1350   }
1351 }
1352 
1353 //       iterator lower_bound(const key_type& key);
1354 // const_iterator lower_bound(const key_type& key) const;
1355 
TYPED_TEST_P(FlatTreeTest,LowerBound)1356 TYPED_TEST_P(FlatTreeTest, LowerBound) {
1357   {
1358     TypedTree<TypeParam> cont({5, 7, 9, 11, 13, 15, 17, 19});
1359 
1360     EXPECT_EQ(cont.begin(), cont.lower_bound(5));
1361     EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7));
1362     EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9));
1363     EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11));
1364     EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13));
1365     EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15));
1366     EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17));
1367     EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19));
1368     EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4));
1369     EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6));
1370     EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8));
1371     EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10));
1372     EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12));
1373     EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14));
1374     EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16));
1375     EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18));
1376     EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20));
1377   }
1378   {
1379     const TypedTree<TypeParam> cont({5, 7, 9, 11, 13, 15, 17, 19});
1380 
1381     EXPECT_EQ(cont.begin(), cont.lower_bound(5));
1382     EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7));
1383     EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9));
1384     EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11));
1385     EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13));
1386     EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15));
1387     EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17));
1388     EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19));
1389     EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4));
1390     EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6));
1391     EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8));
1392     EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10));
1393     EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12));
1394     EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14));
1395     EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16));
1396     EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18));
1397     EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20));
1398   }
1399 }
1400 
1401 // iterator upper_bound(const key_type& key)
1402 // const_iterator upper_bound(const key_type& key) const
1403 
TYPED_TEST_P(FlatTreeTest,UpperBound)1404 TYPED_TEST_P(FlatTreeTest, UpperBound) {
1405   {
1406     TypedTree<TypeParam> cont({5, 7, 9, 11, 13, 15, 17, 19});
1407 
1408     EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5));
1409     EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7));
1410     EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9));
1411     EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11));
1412     EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13));
1413     EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15));
1414     EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17));
1415     EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19));
1416     EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4));
1417     EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6));
1418     EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8));
1419     EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10));
1420     EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12));
1421     EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14));
1422     EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16));
1423     EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18));
1424     EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20));
1425   }
1426   {
1427     const TypedTree<TypeParam> cont({5, 7, 9, 11, 13, 15, 17, 19});
1428 
1429     EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5));
1430     EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7));
1431     EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9));
1432     EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11));
1433     EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13));
1434     EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15));
1435     EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17));
1436     EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19));
1437     EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4));
1438     EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6));
1439     EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8));
1440     EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10));
1441     EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12));
1442     EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14));
1443     EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16));
1444     EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18));
1445     EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20));
1446   }
1447 }
1448 
1449 // ----------------------------------------------------------------------------
1450 // General operations.
1451 
1452 // void swap(flat_tree& other)
1453 // void swap(flat_tree& lhs, flat_tree& rhs)
1454 
TYPED_TEST_P(FlatTreeTest,Swap)1455 TYPED_TEST_P(FlatTreeTest, Swap) {
1456   TypedTree<TypeParam> x({1, 2, 3});
1457   TypedTree<TypeParam> y({4});
1458   swap(x, y);
1459   EXPECT_THAT(x, ElementsAre(4));
1460   EXPECT_THAT(y, ElementsAre(1, 2, 3));
1461 
1462   y.swap(x);
1463   EXPECT_THAT(x, ElementsAre(1, 2, 3));
1464   EXPECT_THAT(y, ElementsAre(4));
1465 }
1466 
1467 // bool operator==(const flat_tree& lhs, const flat_tree& rhs)
1468 // bool operator!=(const flat_tree& lhs, const flat_tree& rhs)
1469 // bool operator<(const flat_tree& lhs, const flat_tree& rhs)
1470 // bool operator>(const flat_tree& lhs, const flat_tree& rhs)
1471 // bool operator<=(const flat_tree& lhs, const flat_tree& rhs)
1472 // bool operator>=(const flat_tree& lhs, const flat_tree& rhs)
1473 
TEST(FlatTree,Comparison)1474 TEST(FlatTree, Comparison) {
1475   // Provided comparator does not participate in comparison.
1476   ReversedTree biggest({3});
1477   ReversedTree smallest({1});
1478   ReversedTree middle({1, 2});
1479 
1480   EXPECT_EQ(biggest, biggest);
1481   EXPECT_NE(biggest, smallest);
1482   EXPECT_LT(smallest, middle);
1483   EXPECT_LE(smallest, middle);
1484   EXPECT_LE(middle, middle);
1485   EXPECT_GT(biggest, middle);
1486   EXPECT_GE(biggest, middle);
1487   EXPECT_GE(biggest, biggest);
1488 }
1489 
TYPED_TEST_P(FlatTreeTest,EraseIf)1490 TYPED_TEST_P(FlatTreeTest, EraseIf) {
1491   TypedTree<TypeParam> x;
1492   EXPECT_EQ(0u, base::EraseIf(x, [](int) { return false; }));
1493   EXPECT_THAT(x, ElementsAre());
1494 
1495   x = {1, 2, 3};
1496   EXPECT_EQ(1u, base::EraseIf(x, [](int elem) { return !(elem & 1); }));
1497   EXPECT_THAT(x, ElementsAre(1, 3));
1498 
1499   x = {1, 2, 3, 4};
1500   EXPECT_EQ(2u, base::EraseIf(x, [](int elem) { return elem & 1; }));
1501   EXPECT_THAT(x, ElementsAre(2, 4));
1502 }
1503 
1504 // Test unsorted containers or containers with repeated elements cause a DCHECK
1505 // if used with the sorted_unique tag.
TYPED_TEST_P(FlatTreeTest,SortedUniqueRangeConstructorDCHECKs)1506 TYPED_TEST_P(FlatTreeTest, SortedUniqueRangeConstructorDCHECKs) {
1507   int unsorted[] = {2, 1};
1508   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, std::begin(unsorted),
1509                                            std::end(unsorted)));
1510 
1511   int repeated[] = {1, 2, 2};
1512   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, std::begin(repeated),
1513                                            std::end(repeated)));
1514 }
1515 
TYPED_TEST_P(FlatTreeTest,SortedUniqueVectorCopyConstructorDCHECKs)1516 TYPED_TEST_P(FlatTreeTest, SortedUniqueVectorCopyConstructorDCHECKs) {
1517   TypeParam unsorted = {2, 1};
1518   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, unsorted));
1519 
1520   TypeParam repeated = {1, 2, 2};
1521   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, repeated));
1522 }
1523 
TYPED_TEST_P(FlatTreeTest,SortedUniqueVectorMoveConstructorDCHECKs)1524 TYPED_TEST_P(FlatTreeTest, SortedUniqueVectorMoveConstructorDCHECKs) {
1525   TypeParam unsorted = {2, 1};
1526   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, std::move(unsorted)));
1527 
1528   TypeParam repeated = {1, 2, 2};
1529   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, std::move(repeated)));
1530 }
1531 
TYPED_TEST_P(FlatTreeTest,SortedUniqueInitializerListConstructorDCHECKs)1532 TYPED_TEST_P(FlatTreeTest, SortedUniqueInitializerListConstructorDCHECKs) {
1533   std::initializer_list<int> unsorted = {2, 1};
1534   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, unsorted));
1535 
1536   std::initializer_list<int> repeated = {1, 2, 2};
1537   EXPECT_DCHECK_DEATH(TypedTree<TypeParam>(sorted_unique, repeated));
1538 }
1539 
TYPED_TEST_P(FlatTreeTest,ReplaceDCHECKs)1540 TYPED_TEST_P(FlatTreeTest, ReplaceDCHECKs) {
1541   TypedTree<TypeParam> tree;
1542   TypeParam unsorted = {2, 1};
1543   EXPECT_DCHECK_DEATH(tree.replace(std::move(unsorted)));
1544 
1545   TypeParam repeated = {1, 2, 2};
1546   EXPECT_DCHECK_DEATH(tree.replace(std::move(repeated)));
1547 }
1548 
1549 REGISTER_TYPED_TEST_SUITE_P(FlatTreeTest,
1550                             DefaultConstructor,
1551                             CopyConstructor,
1552                             ContainerCopyConstructor,
1553                             InitializerListConstructor,
1554                             SortedUniqueContainerCopyConstructor,
1555                             SortedUniqueInitializerListConstructor,
1556                             CopyAssignable,
1557                             InitializerListAssignable,
1558                             Clear,
1559                             Size,
1560                             Empty,
1561                             Iterators,
1562                             InsertLValue,
1563                             InsertPositionLValue,
1564                             Emplace,
1565                             EmplacePosition,
1566                             Extract,
1567                             Replace,
1568                             ErasePosition,
1569                             EraseRange,
1570                             EraseKey,
1571                             EraseEndDeath,
1572                             Count,
1573                             Find,
1574                             Contains,
1575                             EqualRange,
1576                             LowerBound,
1577                             UpperBound,
1578                             Swap,
1579                             EraseIf,
1580                             SortedUniqueRangeConstructorDCHECKs,
1581                             SortedUniqueVectorCopyConstructorDCHECKs,
1582                             SortedUniqueVectorMoveConstructorDCHECKs,
1583                             SortedUniqueInitializerListConstructorDCHECKs,
1584                             ReplaceDCHECKs);
1585 
1586 using IntSequenceContainers =
1587     ::testing::Types<std::deque<int>, std::vector<int>>;
1588 INSTANTIATE_TYPED_TEST_SUITE_P(My, FlatTreeTest, IntSequenceContainers);
1589 
1590 }  // namespace internal
1591 }  // namespace base
1592