proxygen
sorted_vector_test.cpp File Reference
#include <folly/sorted_vector_types.h>
#include <iterator>
#include <list>
#include <memory>
#include <string>
#include <folly/Range.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

struct  Movable
 

Functions

 TEST (SortedVectorTypes, SetAssignmentInitListTest)
 
 TEST (SortedVectorTypes, MapAssignmentInitListTest)
 
 TEST (SortedVectorTypes, SimpleSetTest)
 
 TEST (SortedVectorTypes, TransparentSetTest)
 
 TEST (SortedVectorTypes, BadHints)
 
 TEST (SortedVectorTypes, SimpleMapTest)
 
 TEST (SortedVectorTypes, TransparentMapTest)
 
 TEST (SortedVectorTypes, Sizes)
 
 TEST (SortedVectorTypes, InitializerLists)
 
 TEST (SortedVectorTypes, CustomCompare)
 
 TEST (SortedVectorTypes, GrowthPolicy)
 
 TEST (SortedVectorTest, EmptyTest)
 
 TEST (SortedVectorTest, MoveTest)
 
 TEST (SortedVectorTest, ShrinkTest)
 
 TEST (SortedVectorTypes, EraseTest)
 
 TEST (SortedVectorTypes, EraseTest2)
 
std::vector< int > extractValues (sorted_vector_set< CountCopyCtor > const &in)
 
template<typename T , typename S >
std::vector< TmakeVectorOfWrappers (std::vector< S > ss)
 
 TEST (SortedVectorTypes, TestSetBulkInsertionSortMerge)
 
 TEST (SortedVectorTypes, TestSetBulkInsertionMiddleValuesEqualDuplication)
 
 TEST (SortedVectorTypes, TestSetBulkInsertionSortMergeDups)
 
 TEST (SortedVectorTypes, TestSetInsertionDupsOneByOne)
 
 TEST (SortedVectorTypes, TestSetBulkInsertionSortNoMerge)
 
 TEST (SortedVectorTypes, TestSetBulkInsertionNoSortMerge)
 
 TEST (SortedVectorTypes, TestSetBulkInsertionNoSortNoMerge)
 
 TEST (SortedVectorTypes, TestSetBulkInsertionEmptyRange)
 
 TEST (SortedVectorTypes, TestBulkInsertionUncopyableTypes)
 
 TEST (SortedVectorTypes, TestBulkInsertionMovableTypes)
 
 TEST (SortedVectorTypes, TestSetCreationFromVector)
 
 TEST (SortedVectorTypes, TestMapCreationFromVector)
 
 TEST (SortedVectorTypes, TestBulkInsertionWithDuplicatesIntoEmptySet)
 
 TEST (SortedVectorTypes, TestDataPointsToFirstElement)
 

Function Documentation

std::vector<int> extractValues ( sorted_vector_set< CountCopyCtor > const &  in)

Definition at line 570 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::begin(), c, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::end(), and folly::pushmi::operators::transform.

Referenced by TEST().

570  {
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 }
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
char c
template<typename T , typename S >
std::vector<T> makeVectorOfWrappers ( std::vector< S ss)

Definition at line 581 of file sorted_vector_test.cpp.

References s.

581  {
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 }
static set< string > s
TEST ( SortedVectorTypes  ,
SetAssignmentInitListTest   
)

Definition at line 101 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_THAT, and s.

101  {
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 }
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
TEST ( SortedVectorTypes  ,
MapAssignmentInitListTest   
)

Definition at line 110 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_THAT, and m.

110  {
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 }
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
static map< string, int > m
#define EXPECT_THAT(value, matcher)
TEST ( SortedVectorTypes  ,
SimpleSetTest   
)

Definition at line 121 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::begin(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::count(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::empty(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::end(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::equal_range(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::erase(), EXPECT_FALSE, EXPECT_TRUE, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::find(), i, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::insert(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::lower_bound(), folly::gen::range(), s, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::size(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::swap(), and folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::upper_bound().

121  {
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 }
size_type erase(const key_type &key)
std::pair< iterator, iterator > equal_range(const key_type &key)
size_type count(const key_type &key) const
std::pair< iterator, bool > insert(const value_type &value)
iterator find(const key_type &key)
Gen range(Value begin, Value end)
Definition: Base.h:467
iterator lower_bound(const key_type &key)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
iterator upper_bound(const key_type &key)
static set< string > s
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
void swap(sorted_vector_set &o)
TEST ( SortedVectorTypes  ,
TransparentSetTest   
)

Definition at line 188 of file sorted_vector_test.cpp.

References EXPECT_EQ, EXPECT_TRUE, s, and value.

188  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static set< string > s
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
TEST ( SortedVectorTypes  ,
BadHints   
)

Definition at line 237 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::begin(), EXPECT_EQ, i, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::insert(), s, and folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::size().

237  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::pair< iterator, bool > insert(const value_type &value)
static set< string > s
TEST ( SortedVectorTypes  ,
SimpleMapTest   
)

Definition at line 252 of file sorted_vector_test.cpp.

References folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::at(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::begin(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::count(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::end(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::equal_range(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::erase(), EXPECT_DOUBLE_EQ, EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, f, folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::find(), i, folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::insert(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::lower_bound(), m, folly::gen::range(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::swap(), and folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::upper_bound().

252  {
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 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
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 erase(const key_type &key)
Gen range(Value begin, Value end)
Definition: Base.h:467
std::pair< iterator, bool > insert(const value_type &value)
static map< string, int > m
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_DOUBLE_EQ(val1, val2)
Definition: gtest.h:2031
size_type count(const key_type &key) const
iterator upper_bound(const key_type &key)
void swap(sorted_vector_map &o)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
iterator find(const key_type &key)
TEST ( SortedVectorTypes  ,
TransparentMapTest   
)

Definition at line 313 of file sorted_vector_test.cpp.

References EXPECT_EQ, EXPECT_TRUE, m, and value.

313  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static map< string, int > m
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
TEST ( SortedVectorTypes  ,
Sizes   
)

Definition at line 363 of file sorted_vector_test.cpp.

References EXPECT_EQ.

363  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( SortedVectorTypes  ,
InitializerLists   
)

Definition at line 387 of file sorted_vector_test.cpp.

References EXPECT_EQ, and EXPECT_TRUE.

387  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( SortedVectorTypes  ,
CustomCompare   
)

Definition at line 417 of file sorted_vector_test.cpp.

References i, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::insert(), m, and s.

417  {
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 }
std::pair< iterator, bool > insert(const value_type &value)
static map< string, int > m
static set< string > s
TEST ( SortedVectorTypes  ,
GrowthPolicy   
)

Definition at line 431 of file sorted_vector_test.cpp.

References a, EXPECT_EQ, EXPECT_FALSE, i, and v.

431  {
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 }
auto v
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( SortedVectorTest  ,
EmptyTest   
)

Definition at line 469 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::end(), EXPECT_THROW, EXPECT_TRUE, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::find(), and folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::lower_bound().

469  {
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 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
iterator find(const key_type &key)
iterator lower_bound(const key_type &key)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( SortedVectorTest  ,
MoveTest   
)

Definition at line 480 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::end(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::end(), EXPECT_EQ, EXPECT_TRUE, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::insert(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::insert(), m, s, and folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::size().

480  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::pair< iterator, bool > insert(const value_type &value)
std::pair< iterator, bool > insert(const value_type &value)
static map< string, int > m
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static set< string > s
TEST ( SortedVectorTest  ,
ShrinkTest   
)

Definition at line 498 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::capacity(), EXPECT_EQ, i, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::insert(), s, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::shrink_to_fit(), and folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::size().

498  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::pair< iterator, bool > insert(const value_type &value)
static set< string > s
TEST ( SortedVectorTypes  ,
EraseTest   
)

Definition at line 512 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::erase(), EXPECT_EQ, and folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::insert().

512  {
514  s1.insert(1);
515  sorted_vector_set<int> s2(s1);
516  EXPECT_EQ(0, s1.erase(0));
517  EXPECT_EQ(s2, s1);
518 }
size_type erase(const key_type &key)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::pair< iterator, bool > insert(const value_type &value)
TEST ( SortedVectorTypes  ,
EraseTest2   
)

Definition at line 520 of file sorted_vector_test.cpp.

References folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::begin(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::end(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::erase(), EXPECT_EQ, EXPECT_NE, i, folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::insert(), folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::lower_bound(), m, s, and folly::sorted_vector_set< T, Compare, Allocator, GrowthPolicy, Container >::size().

520  {
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 }
size_type erase(const key_type &key)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::pair< iterator, bool > insert(const value_type &value)
iterator lower_bound(const key_type &key)
static map< string, int > m
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
static set< string > s
TEST ( SortedVectorTypes  ,
TestSetBulkInsertionSortMerge   
)

Definition at line 590 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_THAT, extractValues(), and s.

590  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestSetBulkInsertionMiddleValuesEqualDuplication   
)

Definition at line 608 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_THAT, extractValues(), and s.

608  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestSetBulkInsertionSortMergeDups   
)

Definition at line 624 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_THAT, extractValues(), and s.

624  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestSetInsertionDupsOneByOne   
)

Definition at line 640 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_THAT, extractValues(), and s.

640  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestSetBulkInsertionSortNoMerge   
)

Definition at line 658 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_THAT, extractValues(), and s.

658  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestSetBulkInsertionNoSortMerge   
)

Definition at line 675 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_THAT, extractValues(), and s.

675  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestSetBulkInsertionNoSortNoMerge   
)

Definition at line 691 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, EXPECT_THAT, extractValues(), and s.

691  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestSetBulkInsertionEmptyRange   
)

Definition at line 708 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_THAT, EXPECT_TRUE, extractValues(), and s.

708  {
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 }
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
static set< string > s
std::vector< int > extractValues(sorted_vector_set< CountCopyCtor > const &in)
TEST ( SortedVectorTypes  ,
TestBulkInsertionUncopyableTypes   
)

Definition at line 730 of file sorted_vector_test.cpp.

References folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::insert(), and s.

730  {
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 }
static set< string > s
TEST ( SortedVectorTypes  ,
TestBulkInsertionMovableTypes   
)

Definition at line 760 of file sorted_vector_test.cpp.

References folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::insert(), and s.

760  {
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 }
static set< string > s
TEST ( SortedVectorTypes  ,
TestSetCreationFromVector   
)

Definition at line 775 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), EXPECT_THAT, and folly::gen::move.

775  {
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 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
Definition: Traits.h:588
#define EXPECT_THAT(value, matcher)
TEST ( SortedVectorTypes  ,
TestMapCreationFromVector   
)

Definition at line 782 of file sorted_vector_test.cpp.

References folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::begin(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::end(), EXPECT_EQ, and folly::gen::move.

782  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Definition: Traits.h:588
TEST ( SortedVectorTypes  ,
TestBulkInsertionWithDuplicatesIntoEmptySet   
)

Definition at line 798 of file sorted_vector_test.cpp.

References testing::ElementsAreArray(), and EXPECT_THAT.

798  {
800  {
801  std::vector<int> const vec = {0, 1, 0, 1};
802  set.insert(vec.begin(), vec.end());
803  }
805 }
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
Definition: Traits.h:588
#define EXPECT_THAT(value, matcher)
TEST ( SortedVectorTypes  ,
TestDataPointsToFirstElement   
)

Definition at line 807 of file sorted_vector_test.cpp.

References folly::test::begin(), folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::begin(), data, folly::sorted_vector_map< Key, Value, Compare, Allocator, GrowthPolicy, Container >::data(), EXPECT_EQ, and map().

807  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
static Map map(mapCap)
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
const value_type * data() const noexcept