proxygen
sorted_vector_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <iterator>
20 #include <list>
21 #include <memory>
22 #include <string>
23 
24 #include <folly/Range.h>
27 
30 
31 namespace {
32 
33 template <class T>
34 struct less_invert {
35  bool operator()(const T& a, const T& b) const {
36  return b < a;
37  }
38 };
39 
40 template <class Container>
41 void check_invariant(Container& c) {
42  auto it = c.begin();
43  auto end = c.end();
44  if (it == end) {
45  return;
46  }
47  auto prev = it;
48  ++it;
49  for (; it != end; ++it, ++prev) {
50  EXPECT_TRUE(c.value_comp()(*prev, *it));
51  }
52 }
53 
54 struct OneAtATimePolicy {
55  template <class Container>
56  void increase_capacity(Container& c) {
57  if (c.size() == c.capacity()) {
58  c.reserve(c.size() + 1);
59  }
60  }
61 };
62 
63 struct CountCopyCtor {
64  explicit CountCopyCtor() : val_(0) {}
65 
66  explicit CountCopyCtor(int val) : val_(val), count_(0) {}
67 
68  CountCopyCtor(const CountCopyCtor& c) : val_(c.val_), count_(c.count_ + 1) {}
69 
70  bool operator<(const CountCopyCtor& o) const {
71  return val_ < o.val_;
72  }
73 
74  int val_;
75  int count_;
76 };
77 
78 struct Opaque {
79  int value;
80  friend bool operator==(Opaque a, Opaque b) {
81  return a.value == b.value;
82  }
83  friend bool operator<(Opaque a, Opaque b) {
84  return a.value < b.value;
85  }
86  struct Compare : std::less<int>, std::less<Opaque> {
87  using is_transparent = void;
88  using std::less<int>::operator();
89  using std::less<Opaque>::operator();
90  bool operator()(int a, Opaque b) const {
91  return std::less<int>::operator()(a, b.value);
92  }
93  bool operator()(Opaque a, int b) const {
94  return std::less<int>::operator()(a.value, b);
95  }
96  };
97 };
98 
99 } // namespace
100 
101 TEST(SortedVectorTypes, SetAssignmentInitListTest) {
102  sorted_vector_set<int> s{3, 4, 5};
104  s = {}; // empty ilist assignment
105  EXPECT_THAT(s, testing::IsEmpty());
106  s = {7, 8, 9}; // non-empty ilist assignment
108 }
109 
110 TEST(SortedVectorTypes, MapAssignmentInitListTest) {
111  using v = std::pair<int, const char*>;
112  v p = {3, "a"}, q = {4, "b"}, r = {5, "c"};
115  m = {}; // empty ilist assignment
116  EXPECT_THAT(m, testing::IsEmpty());
117  m = {p, q, r}; // non-empty ilist assignment
119 }
120 
121 TEST(SortedVectorTypes, SimpleSetTest) {
123  EXPECT_TRUE(s.empty());
124  for (int i = 0; i < 1000; ++i) {
125  s.insert(rand() % 100000);
126  }
127  EXPECT_FALSE(s.empty());
128  check_invariant(s);
129 
131  s2.insert(s.begin(), s.end());
132  check_invariant(s2);
133  EXPECT_TRUE(s == s2);
134 
135  auto it = s2.lower_bound(32);
136  if (*it == 32) {
137  s2.erase(it);
138  it = s2.lower_bound(32);
139  }
140  check_invariant(s2);
141  auto oldSz = s2.size();
142  s2.insert(it, 32);
143  EXPECT_TRUE(s2.size() == oldSz + 1);
144  check_invariant(s2);
145 
146  const sorted_vector_set<int>& cs2 = s2;
147  auto range = cs2.equal_range(32);
148  auto lbound = cs2.lower_bound(32);
149  auto ubound = cs2.upper_bound(32);
150  EXPECT_TRUE(range.first == lbound);
151  EXPECT_TRUE(range.second == ubound);
152  EXPECT_TRUE(range.first != cs2.end());
153  EXPECT_TRUE(range.second != cs2.end());
154  EXPECT_TRUE(cs2.count(32) == 1);
155  EXPECT_FALSE(cs2.find(32) == cs2.end());
156 
157  // Bad insert hint.
158  s2.insert(s2.begin() + 3, 33);
159  EXPECT_TRUE(s2.find(33) != s2.begin());
160  EXPECT_TRUE(s2.find(33) != s2.end());
161  check_invariant(s2);
162  s2.erase(33);
163  check_invariant(s2);
164 
165  it = s2.find(32);
166  EXPECT_FALSE(it == s2.end());
167  s2.erase(it);
168  EXPECT_TRUE(s2.size() == oldSz);
169  check_invariant(s2);
170 
171  sorted_vector_set<int> cpy(s);
172  check_invariant(cpy);
173  EXPECT_TRUE(cpy == s);
174  sorted_vector_set<int> cpy2(s);
175  cpy2.insert(100001);
176  EXPECT_TRUE(cpy2 != cpy);
177  EXPECT_TRUE(cpy2 != s);
178  check_invariant(cpy2);
179  EXPECT_TRUE(cpy2.count(100001) == 1);
180  s.swap(cpy2);
181  check_invariant(cpy2);
182  check_invariant(s);
183  EXPECT_TRUE(s != cpy);
184  EXPECT_TRUE(s != cpy2);
185  EXPECT_TRUE(cpy2 == cpy);
186 }
187 
188 TEST(SortedVectorTypes, TransparentSetTest) {
189  using namespace folly::string_piece_literals;
191 
192  constexpr auto buddy = "buddy"_sp;
193  constexpr auto hello = "hello"_sp;
194  constexpr auto stake = "stake"_sp;
195  constexpr auto world = "world"_sp;
196  constexpr auto zebra = "zebra"_sp;
197 
198  sorted_vector_set<std::string, Compare> const s({hello.str(), world.str()});
199 
200  // find
201  EXPECT_TRUE(s.end() == s.find(buddy));
202  EXPECT_EQ(hello, *s.find(hello));
203  EXPECT_TRUE(s.end() == s.find(stake));
204  EXPECT_EQ(world, *s.find(world));
205  EXPECT_TRUE(s.end() == s.find(zebra));
206 
207  // count
208  EXPECT_EQ(0, s.count(buddy));
209  EXPECT_EQ(1, s.count(hello));
210  EXPECT_EQ(0, s.count(stake));
211  EXPECT_EQ(1, s.count(world));
212  EXPECT_EQ(0, s.count(zebra));
213 
214  // lower_bound
215  EXPECT_TRUE(s.find(hello) == s.lower_bound(buddy));
216  EXPECT_TRUE(s.find(hello) == s.lower_bound(hello));
217  EXPECT_TRUE(s.find(world) == s.lower_bound(stake));
218  EXPECT_TRUE(s.find(world) == s.lower_bound(world));
219  EXPECT_TRUE(s.end() == s.lower_bound(zebra));
220 
221  // upper_bound
222  EXPECT_TRUE(s.find(hello) == s.upper_bound(buddy));
223  EXPECT_TRUE(s.find(world) == s.upper_bound(hello));
224  EXPECT_TRUE(s.find(world) == s.upper_bound(stake));
225  EXPECT_TRUE(s.end() == s.upper_bound(world));
226  EXPECT_TRUE(s.end() == s.upper_bound(zebra));
227 
228  // equal_range
229  for (auto value : {buddy, hello, stake, world, zebra}) {
230  EXPECT_TRUE(
231  std::make_pair(s.lower_bound(value), s.upper_bound(value)) ==
232  s.equal_range(value))
233  << value;
234  }
235 }
236 
237 TEST(SortedVectorTypes, BadHints) {
238  for (int toInsert = -1; toInsert <= 7; ++toInsert) {
239  for (int hintPos = 0; hintPos <= 4; ++hintPos) {
241  for (int i = 0; i <= 3; ++i) {
242  s.insert(i * 2);
243  }
244  s.insert(s.begin() + hintPos, toInsert);
245  size_t expectedSize = (toInsert % 2) == 0 ? 4 : 5;
246  EXPECT_EQ(s.size(), expectedSize);
247  check_invariant(s);
248  }
249  }
250 }
251 
252 TEST(SortedVectorTypes, SimpleMapTest) {
254  for (int i = 0; i < 1000; ++i) {
255  m[i] = i / 1000.0;
256  }
257  check_invariant(m);
258 
259  m[32] = 100.0;
260  check_invariant(m);
261  EXPECT_TRUE(m.count(32) == 1);
262  EXPECT_DOUBLE_EQ(100.0, m.at(32));
263  EXPECT_FALSE(m.find(32) == m.end());
264  m.erase(32);
265  EXPECT_TRUE(m.find(32) == m.end());
266  check_invariant(m);
267  EXPECT_THROW(m.at(32), std::out_of_range);
268 
270  EXPECT_TRUE(m2 == m);
271  EXPECT_FALSE(m2 != m);
272  auto it = m2.lower_bound(1 << 20);
273  EXPECT_TRUE(it == m2.end());
274  m2.insert(it, std::make_pair(1 << 20, 10.0f));
275  check_invariant(m2);
276  EXPECT_TRUE(m2.count(1 << 20) == 1);
277  EXPECT_TRUE(m < m2);
278  EXPECT_TRUE(m <= m2);
279 
280  const sorted_vector_map<int, float>& cm = m;
281  auto range = cm.equal_range(42);
282  auto lbound = cm.lower_bound(42);
283  auto ubound = cm.upper_bound(42);
284  EXPECT_TRUE(range.first == lbound);
285  EXPECT_TRUE(range.second == ubound);
286  EXPECT_FALSE(range.first == cm.end());
287  EXPECT_FALSE(range.second == cm.end());
288  m.erase(m.lower_bound(42));
289  check_invariant(m);
290 
292  m3.insert(m2.begin(), m2.end());
293  check_invariant(m3);
294  EXPECT_TRUE(m3 == m2);
295  EXPECT_FALSE(m3 == m);
296 
297  EXPECT_TRUE(m != m2);
298  EXPECT_TRUE(m2 == m3);
299  EXPECT_TRUE(m3 != m);
300  m.swap(m3);
301  check_invariant(m);
302  check_invariant(m2);
303  check_invariant(m3);
304  EXPECT_TRUE(m3 != m2);
305  EXPECT_TRUE(m3 != m);
306  EXPECT_TRUE(m == m2);
307 
308  // Bad insert hint.
309  m.insert(m.begin() + 3, std::make_pair(1 << 15, 1.0f));
310  check_invariant(m);
311 }
312 
313 TEST(SortedVectorTypes, TransparentMapTest) {
314  using namespace folly::string_piece_literals;
316 
317  constexpr auto buddy = "buddy"_sp;
318  constexpr auto hello = "hello"_sp;
319  constexpr auto stake = "stake"_sp;
320  constexpr auto world = "world"_sp;
321  constexpr auto zebra = "zebra"_sp;
322 
324  {{hello.str(), -1.}, {world.str(), +1.}});
325 
326  // find
327  EXPECT_TRUE(m.end() == m.find(buddy));
328  EXPECT_EQ(hello, m.find(hello)->first);
329  EXPECT_TRUE(m.end() == m.find(stake));
330  EXPECT_EQ(world, m.find(world)->first);
331  EXPECT_TRUE(m.end() == m.find(zebra));
332 
333  // count
334  EXPECT_EQ(0, m.count(buddy));
335  EXPECT_EQ(1, m.count(hello));
336  EXPECT_EQ(0, m.count(stake));
337  EXPECT_EQ(1, m.count(world));
338  EXPECT_EQ(0, m.count(zebra));
339 
340  // lower_bound
341  EXPECT_TRUE(m.find(hello) == m.lower_bound(buddy));
342  EXPECT_TRUE(m.find(hello) == m.lower_bound(hello));
343  EXPECT_TRUE(m.find(world) == m.lower_bound(stake));
344  EXPECT_TRUE(m.find(world) == m.lower_bound(world));
345  EXPECT_TRUE(m.end() == m.lower_bound(zebra));
346 
347  // upper_bound
348  EXPECT_TRUE(m.find(hello) == m.upper_bound(buddy));
349  EXPECT_TRUE(m.find(world) == m.upper_bound(hello));
350  EXPECT_TRUE(m.find(world) == m.upper_bound(stake));
351  EXPECT_TRUE(m.end() == m.upper_bound(world));
352  EXPECT_TRUE(m.end() == m.upper_bound(zebra));
353 
354  // equal_range
355  for (auto value : {buddy, hello, stake, world, zebra}) {
356  EXPECT_TRUE(
357  std::make_pair(m.lower_bound(value), m.upper_bound(value)) ==
358  m.equal_range(value))
359  << value;
360  }
361 }
362 
363 TEST(SortedVectorTypes, Sizes) {
364  EXPECT_EQ(sizeof(sorted_vector_set<int>), sizeof(std::vector<int>));
365  EXPECT_EQ(
367  sizeof(std::vector<std::pair<int, int>>));
368 
369  typedef sorted_vector_set<
370  int,
371  std::less<int>,
372  std::allocator<int>,
373  OneAtATimePolicy>
374  SetT;
375  typedef sorted_vector_map<
376  int,
377  int,
378  std::less<int>,
379  std::allocator<std::pair<int, int>>,
380  OneAtATimePolicy>
381  MapT;
382 
383  EXPECT_EQ(sizeof(SetT), sizeof(std::vector<int>));
384  EXPECT_EQ(sizeof(MapT), sizeof(std::vector<std::pair<int, int>>));
385 }
386 
387 TEST(SortedVectorTypes, InitializerLists) {
388  sorted_vector_set<int> empty_initialized_set{};
389  EXPECT_TRUE(empty_initialized_set.empty());
390 
391  sorted_vector_set<int> singleton_initialized_set{1};
392  EXPECT_EQ(1, singleton_initialized_set.size());
393  EXPECT_EQ(1, *singleton_initialized_set.begin());
394 
395  sorted_vector_set<int> forward_initialized_set{1, 2};
396  sorted_vector_set<int> backward_initialized_set{2, 1};
397  EXPECT_EQ(2, forward_initialized_set.size());
398  EXPECT_EQ(1, *forward_initialized_set.begin());
399  EXPECT_EQ(2, *forward_initialized_set.rbegin());
400  EXPECT_TRUE(forward_initialized_set == backward_initialized_set);
401 
402  sorted_vector_map<int, int> empty_initialized_map{};
403  EXPECT_TRUE(empty_initialized_map.empty());
404 
405  sorted_vector_map<int, int> singleton_initialized_map{{1, 10}};
406  EXPECT_EQ(1, singleton_initialized_map.size());
407  EXPECT_EQ(10, singleton_initialized_map[1]);
408 
409  sorted_vector_map<int, int> forward_initialized_map{{1, 10}, {2, 20}};
410  sorted_vector_map<int, int> backward_initialized_map{{2, 20}, {1, 10}};
411  EXPECT_EQ(2, forward_initialized_map.size());
412  EXPECT_EQ(10, forward_initialized_map[1]);
413  EXPECT_EQ(20, forward_initialized_map[2]);
414  EXPECT_TRUE(forward_initialized_map == backward_initialized_map);
415 }
416 
417 TEST(SortedVectorTypes, CustomCompare) {
419  for (int i = 0; i < 200; ++i) {
420  s.insert(i);
421  }
422  check_invariant(s);
423 
425  for (int i = 0; i < 200; ++i) {
426  m[i] = 12.0;
427  }
428  check_invariant(m);
429 }
430 
431 TEST(SortedVectorTypes, GrowthPolicy) {
432  typedef sorted_vector_set<
433  CountCopyCtor,
434  std::less<CountCopyCtor>,
435  std::allocator<CountCopyCtor>,
436  OneAtATimePolicy>
437  SetT;
438 
439  SetT a;
440  for (int i = 0; i < 20; ++i) {
441  a.insert(CountCopyCtor(i));
442  }
443  check_invariant(a);
444  SetT::iterator it = a.begin();
445  EXPECT_FALSE(it == a.end());
446  if (it != a.end()) {
447  EXPECT_EQ(it->val_, 0);
448  // 1 copy for the initial insertion, 19 more for reallocs on the
449  // additional insertions.
450  EXPECT_EQ(it->count_, 20);
451  }
452 
453  std::list<CountCopyCtor> v;
454  for (int i = 0; i < 20; ++i) {
455  v.emplace_back(20 + i);
456  }
457  a.insert(v.begin(), v.end());
458  check_invariant(a);
459 
460  it = a.begin();
461  EXPECT_FALSE(it == a.end());
462  if (it != a.end()) {
463  EXPECT_EQ(it->val_, 0);
464  // Should be only 1 more copy for inserting this above range.
465  EXPECT_EQ(it->count_, 21);
466  }
467 }
468 
469 TEST(SortedVectorTest, EmptyTest) {
470  sorted_vector_set<int> emptySet;
471  EXPECT_TRUE(emptySet.lower_bound(10) == emptySet.end());
472  EXPECT_TRUE(emptySet.find(10) == emptySet.end());
473 
475  EXPECT_TRUE(emptyMap.lower_bound(10) == emptyMap.end());
476  EXPECT_TRUE(emptyMap.find(10) == emptyMap.end());
477  EXPECT_THROW(emptyMap.at(10), std::out_of_range);
478 }
479 
480 TEST(SortedVectorTest, MoveTest) {
482  s.insert(std::make_unique<int>(5));
483  s.insert(s.end(), std::make_unique<int>(10));
484  EXPECT_EQ(s.size(), 2);
485 
486  for (const auto& p : s) {
487  EXPECT_TRUE(*p == 5 || *p == 10);
488  }
489 
491  m.insert(std::make_pair(5, std::make_unique<int>(5)));
492  m.insert(m.end(), std::make_pair(10, std::make_unique<int>(10)));
493 
494  EXPECT_EQ(*m[5], 5);
495  EXPECT_EQ(*m[10], 10);
496 }
497 
498 TEST(SortedVectorTest, ShrinkTest) {
500  int i = 0;
501  // Hopefully your resize policy doubles when capacity is full, or this will
502  // hang forever :(
503  while (s.capacity() == s.size()) {
504  s.insert(i++);
505  }
506  s.shrink_to_fit();
507  // The standard does not actually enforce that this be true, but assume that
508  // vector::shrink_to_fit respects the caller.
509  EXPECT_EQ(s.capacity(), s.size());
510 }
511 
512 TEST(SortedVectorTypes, EraseTest) {
514  s1.insert(1);
515  sorted_vector_set<int> s2(s1);
516  EXPECT_EQ(0, s1.erase(0));
517  EXPECT_EQ(s2, s1);
518 }
519 
520 TEST(SortedVectorTypes, EraseTest2) {
522  for (int i = 0; i < 1000; ++i) {
523  s.insert(i);
524  }
525 
526  auto it = s.lower_bound(32);
527  EXPECT_EQ(*it, 32);
528  it = s.erase(it);
529  EXPECT_NE(s.end(), it);
530  EXPECT_EQ(*it, 33);
531  it = s.erase(it, it + 5);
532  EXPECT_EQ(*it, 38);
533 
534  it = s.begin();
535  while (it != s.end()) {
536  if (*it >= 5) {
537  it = s.erase(it);
538  } else {
539  it++;
540  }
541  }
542  EXPECT_EQ(it, s.end());
543  EXPECT_EQ(s.size(), 5);
544 
546  for (int i = 0; i < 1000; ++i) {
547  m.insert(std::make_pair(i, i));
548  }
549 
550  auto it2 = m.lower_bound(32);
551  EXPECT_EQ(it2->first, 32);
552  it2 = m.erase(it2);
553  EXPECT_NE(m.end(), it2);
554  EXPECT_EQ(it2->first, 33);
555  it2 = m.erase(it2, it2 + 5);
556  EXPECT_EQ(it2->first, 38);
557 
558  it2 = m.begin();
559  while (it2 != m.end()) {
560  if (it2->first >= 5) {
561  it2 = m.erase(it2);
562  } else {
563  it2++;
564  }
565  }
566  EXPECT_EQ(it2, m.end());
567  EXPECT_EQ(m.size(), 5);
568 }
569 
570 std::vector<int> extractValues(sorted_vector_set<CountCopyCtor> const& in) {
571  std::vector<int> ret;
573  in.begin(),
574  in.end(),
575  std::back_inserter(ret),
576  [](const CountCopyCtor& c) { return c.val_; });
577  return ret;
578 }
579 
580 template <typename T, typename S>
581 std::vector<T> makeVectorOfWrappers(std::vector<S> ss) {
582  std::vector<T> ts;
583  ts.reserve(ss.size());
584  for (auto const& s : ss) {
585  ts.emplace_back(s);
586  }
587  return ts;
588 }
589 
590 TEST(SortedVectorTypes, TestSetBulkInsertionSortMerge) {
591  auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
592 
593  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
594  check_invariant(vset);
595 
596  // Add an unsorted range that will have to be merged in.
597  s = makeVectorOfWrappers<CountCopyCtor, int>({10, 7, 5, 1});
598 
599  vset.insert(s.begin(), s.end());
600  check_invariant(vset);
601  EXPECT_EQ(vset.rbegin()->count_, 1);
602 
603  EXPECT_THAT(
604  extractValues(vset),
605  testing::ElementsAreArray({1, 2, 4, 5, 6, 7, 8, 10}));
606 }
607 
608 TEST(SortedVectorTypes, TestSetBulkInsertionMiddleValuesEqualDuplication) {
609  auto s = makeVectorOfWrappers<CountCopyCtor, int>({4, 6, 8});
610 
611  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
612  check_invariant(vset);
613 
614  s = makeVectorOfWrappers<CountCopyCtor, int>({8, 10, 12});
615 
616  vset.insert(s.begin(), s.end());
617  check_invariant(vset);
618  EXPECT_EQ(vset.rbegin()->count_, 1);
619 
620  EXPECT_THAT(
621  extractValues(vset), testing::ElementsAreArray({4, 6, 8, 10, 12}));
622 }
623 
624 TEST(SortedVectorTypes, TestSetBulkInsertionSortMergeDups) {
625  auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
626 
627  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
628  check_invariant(vset);
629 
630  // Add an unsorted range that will have to be merged in.
631  s = makeVectorOfWrappers<CountCopyCtor, int>({10, 6, 5, 2});
632 
633  vset.insert(s.begin(), s.end());
634  check_invariant(vset);
635  EXPECT_EQ(vset.rbegin()->count_, 1);
636  EXPECT_THAT(
637  extractValues(vset), testing::ElementsAreArray({2, 4, 5, 6, 8, 10}));
638 }
639 
640 TEST(SortedVectorTypes, TestSetInsertionDupsOneByOne) {
641  auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
642 
643  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
644  check_invariant(vset);
645 
646  // Add an unsorted range that will have to be merged in.
647  s = makeVectorOfWrappers<CountCopyCtor, int>({10, 6, 5, 2});
648 
649  for (const auto& elem : s) {
650  vset.insert(elem);
651  }
652  check_invariant(vset);
653  EXPECT_EQ(vset.rbegin()->count_, 3);
654  EXPECT_THAT(
655  extractValues(vset), testing::ElementsAreArray({2, 4, 5, 6, 8, 10}));
656 }
657 
658 TEST(SortedVectorTypes, TestSetBulkInsertionSortNoMerge) {
659  auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
660 
661  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
662  check_invariant(vset);
663 
664  // Add an unsorted range that will not have to be merged in.
665  s = makeVectorOfWrappers<CountCopyCtor, int>({20, 15, 16, 13});
666 
667  vset.insert(s.begin(), s.end());
668  check_invariant(vset);
669  EXPECT_EQ(vset.rbegin()->count_, 1);
670  EXPECT_THAT(
671  extractValues(vset),
672  testing::ElementsAreArray({2, 4, 6, 8, 13, 15, 16, 20}));
673 }
674 
675 TEST(SortedVectorTypes, TestSetBulkInsertionNoSortMerge) {
676  auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
677 
678  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
679  check_invariant(vset);
680 
681  // Add a sorted range that will have to be merged in.
682  s = makeVectorOfWrappers<CountCopyCtor, int>({1, 3, 5, 9});
683 
684  vset.insert(s.begin(), s.end());
685  check_invariant(vset);
686  EXPECT_EQ(vset.rbegin()->count_, 1);
687  EXPECT_THAT(
688  extractValues(vset), testing::ElementsAreArray({1, 2, 3, 4, 5, 6, 8, 9}));
689 }
690 
691 TEST(SortedVectorTypes, TestSetBulkInsertionNoSortNoMerge) {
692  auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
693 
694  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
695  check_invariant(vset);
696 
697  // Add a sorted range that will not have to be merged in.
698  s = makeVectorOfWrappers<CountCopyCtor, int>({21, 22, 23, 24});
699 
700  vset.insert(s.begin(), s.end());
701  check_invariant(vset);
702  EXPECT_EQ(vset.rbegin()->count_, 1);
703  EXPECT_THAT(
704  extractValues(vset),
705  testing::ElementsAreArray({2, 4, 6, 8, 21, 22, 23, 24}));
706 }
707 
708 TEST(SortedVectorTypes, TestSetBulkInsertionEmptyRange) {
709  std::vector<CountCopyCtor> s;
710  EXPECT_TRUE(s.empty());
711 
712  // insertion of empty range into empty container.
713  sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
714  check_invariant(vset);
715 
716  s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
717 
718  vset.insert(s.begin(), s.end());
719 
720  // insertion of empty range into non-empty container.
721  s.clear();
722  vset.insert(s.begin(), s.end());
723  check_invariant(vset);
724 
726 }
727 
728 // This is a test of compilation - the behavior has already been tested
729 // extensively above.
730 TEST(SortedVectorTypes, TestBulkInsertionUncopyableTypes) {
731  std::vector<std::pair<int, std::unique_ptr<int>>> s;
732  s.emplace_back(1, std::make_unique<int>(0));
733 
735  std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
736 
737  s.clear();
738  s.emplace_back(3, std::make_unique<int>(0));
739  vmap.insert(
740  std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
741 }
742 
743 // A moveable and copyable struct, which we use to make sure that no copy
744 // operations are performed during bulk insertion if moving is an option.
745 struct Movable {
746  int x_;
747  explicit Movable(int x) : x_(x) {}
748  Movable(const Movable&) {
749  ADD_FAILURE() << "Copy ctor should not be called";
750  }
752  ADD_FAILURE() << "Copy assignment should not be called";
753  return *this;
754  }
755 
756  Movable(Movable&&) = default;
757  Movable& operator=(Movable&&) = default;
758 };
759 
760 TEST(SortedVectorTypes, TestBulkInsertionMovableTypes) {
761  std::vector<std::pair<int, Movable>> s;
762  s.emplace_back(3, Movable(2));
763  s.emplace_back(1, Movable(0));
764 
766  std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
767 
768  s.clear();
769  s.emplace_back(4, Movable(3));
770  s.emplace_back(2, Movable(1));
771  vmap.insert(
772  std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
773 }
774 
775 TEST(SortedVectorTypes, TestSetCreationFromVector) {
776  std::vector<int> vec = {3, 1, -1, 5, 0};
778  check_invariant(vset);
779  EXPECT_THAT(vset, testing::ElementsAreArray({-1, 0, 1, 3, 5}));
780 }
781 
782 TEST(SortedVectorTypes, TestMapCreationFromVector) {
783  std::vector<std::pair<int, int>> vec = {
784  {3, 1}, {1, 5}, {-1, 2}, {5, 3}, {0, 3}};
786  check_invariant(vmap);
787  auto contents = std::vector<std::pair<int, int>>(vmap.begin(), vmap.end());
788  auto expected_contents = std::vector<std::pair<int, int>>({
789  {-1, 2},
790  {0, 3},
791  {1, 5},
792  {3, 1},
793  {5, 3},
794  });
795  EXPECT_EQ(contents, expected_contents);
796 }
797 
798 TEST(SortedVectorTypes, TestBulkInsertionWithDuplicatesIntoEmptySet) {
800  {
801  std::vector<int> const vec = {0, 1, 0, 1};
802  set.insert(vec.begin(), vec.end());
803  }
805 }
806 
807 TEST(SortedVectorTypes, TestDataPointsToFirstElement) {
810 
811  set.insert(0);
812  map[0] = 0;
813  EXPECT_EQ(set.data(), &*set.begin());
814  EXPECT_EQ(map.data(), &*map.begin());
815 
816  set.insert(1);
817  map[1] = 1;
818  EXPECT_EQ(set.data(), &*set.begin());
819  EXPECT_EQ(map.data(), &*map.begin());
820 }
Definition: InvokeTest.cpp:58
bool operator==(const char *c, CStringRange::Sentinel)
#define T(v)
Definition: http_parser.c:233
auto f
auto v
size_type erase(const key_type &key)
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
std::pair< iterator, iterator > equal_range(const key_type &key)
char b
iterator lower_bound(const key_type &key)
std::pair< iterator, iterator > equal_range(const key_type &key)
mapped_type & at(const key_type &key)
size_type count(const key_type &key) const
size_type erase(const key_type &key)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
double val
Definition: String.cpp:273
std::pair< iterator, bool > insert(const value_type &value)
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
Movable & operator=(const Movable &)
iterator find(const key_type &key)
Gen range(Value begin, Value end)
Definition: Base.h:467
iterator lower_bound(const key_type &key)
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
std::pair< iterator, bool > insert(const value_type &value)
static Map map(mapCap)
static map< string, int > m
std::vector< T > makeVectorOfWrappers(std::vector< S > ss)
char a
Definition: Traits.h:588
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
#define EXPECT_DOUBLE_EQ(val1, val2)
Definition: gtest.h:2031
Movable(const Movable &)
iterator upper_bound(const key_type &key)
size_type count(const key_type &key) const
iterator upper_bound(const key_type &key)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
static set< string > s
void swap(sorted_vector_map &o)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
iterator find(const key_type &key)
#define ADD_FAILURE()
Definition: gtest.h:1808
char c
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
const value_type * data() const noexcept
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
void swap(sorted_vector_set &o)
std::enable_if< IsLessThanComparable< Value >::value, bool >::type operator<(const Expected< Value, Error > &lhs, const Expected< Value, Error > &rhs)
Definition: Expected.h:1321
TEST(SortedVectorTypes, SetAssignmentInitListTest)