2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/sorted_vector_types.h>
23 #include <folly/portability/GMock.h>
24 #include <folly/portability/GTest.h>
26 using folly::sorted_vector_set;
27 using folly::sorted_vector_map;
33 bool operator()(const T& a, const T& b) const {
38 template<class Container>
39 void check_invariant(Container& c) {
46 for (; it != end; ++it, ++prev) {
47 EXPECT_TRUE(c.value_comp()(*prev, *it));
51 struct OneAtATimePolicy {
52 template<class Container>
53 void increase_capacity(Container& c) {
54 if (c.size() == c.capacity()) {
55 c.reserve(c.size() + 1);
60 struct CountCopyCtor {
61 explicit CountCopyCtor() : val_(0) {}
63 explicit CountCopyCtor(int val) : val_(val), count_(0) {}
65 CountCopyCtor(const CountCopyCtor& c)
67 , count_(c.count_ + 1)
70 bool operator<(const CountCopyCtor& o) const {
80 TEST(SortedVectorTypes, SimpleSetTest) {
81 sorted_vector_set<int> s;
82 EXPECT_TRUE(s.empty());
83 for (int i = 0; i < 1000; ++i) {
84 s.insert(rand() % 100000);
86 EXPECT_FALSE(s.empty());
89 sorted_vector_set<int> s2;
90 s2.insert(s.begin(), s.end());
94 auto it = s2.lower_bound(32);
97 it = s2.lower_bound(32);
100 auto oldSz = s2.size();
102 EXPECT_TRUE(s2.size() == oldSz + 1);
105 const sorted_vector_set<int>& cs2 = s2;
106 auto range = cs2.equal_range(32);
107 auto lbound = cs2.lower_bound(32);
108 auto ubound = cs2.upper_bound(32);
109 EXPECT_TRUE(range.first == lbound);
110 EXPECT_TRUE(range.second == ubound);
111 EXPECT_TRUE(range.first != cs2.end());
112 EXPECT_TRUE(range.second != cs2.end());
113 EXPECT_TRUE(cs2.count(32) == 1);
114 EXPECT_FALSE(cs2.find(32) == cs2.end());
117 s2.insert(s2.begin() + 3, 33);
118 EXPECT_TRUE(s2.find(33) != s2.begin());
119 EXPECT_TRUE(s2.find(33) != s2.end());
125 EXPECT_FALSE(it == s2.end());
127 EXPECT_TRUE(s2.size() == oldSz);
130 sorted_vector_set<int> cpy(s);
131 check_invariant(cpy);
132 EXPECT_TRUE(cpy == s);
133 sorted_vector_set<int> cpy2(s);
135 EXPECT_TRUE(cpy2 != cpy);
136 EXPECT_TRUE(cpy2 != s);
137 check_invariant(cpy2);
138 EXPECT_TRUE(cpy2.count(100001) == 1);
140 check_invariant(cpy2);
142 EXPECT_TRUE(s != cpy);
143 EXPECT_TRUE(s != cpy2);
144 EXPECT_TRUE(cpy2 == cpy);
147 TEST(SortedVectorTypes, SimpleMapTest) {
148 sorted_vector_map<int,float> m;
149 for (int i = 0; i < 1000; ++i) {
156 EXPECT_TRUE(m.count(32) == 1);
157 EXPECT_DOUBLE_EQ(100.0, m.at(32));
158 EXPECT_FALSE(m.find(32) == m.end());
160 EXPECT_TRUE(m.find(32) == m.end());
162 EXPECT_THROW(m.at(32), std::out_of_range);
164 sorted_vector_map<int,float> m2 = m;
165 EXPECT_TRUE(m2 == m);
166 EXPECT_FALSE(m2 != m);
167 auto it = m2.lower_bound(1 << 20);
168 EXPECT_TRUE(it == m2.end());
169 m2.insert(it, std::make_pair(1 << 20, 10.0f));
171 EXPECT_TRUE(m2.count(1 << 20) == 1);
173 EXPECT_TRUE(m <= m2);
175 const sorted_vector_map<int,float>& cm = m;
176 auto range = cm.equal_range(42);
177 auto lbound = cm.lower_bound(42);
178 auto ubound = cm.upper_bound(42);
179 EXPECT_TRUE(range.first == lbound);
180 EXPECT_TRUE(range.second == ubound);
181 EXPECT_FALSE(range.first == cm.end());
182 EXPECT_FALSE(range.second == cm.end());
183 m.erase(m.lower_bound(42));
186 sorted_vector_map<int,float> m3;
187 m3.insert(m2.begin(), m2.end());
189 EXPECT_TRUE(m3 == m2);
190 EXPECT_FALSE(m3 == m);
192 EXPECT_TRUE(m != m2);
193 EXPECT_TRUE(m2 == m3);
194 EXPECT_TRUE(m3 != m);
199 EXPECT_TRUE(m3 != m2);
200 EXPECT_TRUE(m3 != m);
201 EXPECT_TRUE(m == m2);
204 m.insert(m.begin() + 3, std::make_pair(1 << 15, 1.0f));
208 TEST(SortedVectorTypes, Sizes) {
209 EXPECT_EQ(sizeof(sorted_vector_set<int>),
210 sizeof(std::vector<int>));
211 EXPECT_EQ(sizeof(sorted_vector_map<int,int>),
212 sizeof(std::vector<std::pair<int,int> >));
214 typedef sorted_vector_set<int,std::less<int>,
215 std::allocator<int>,OneAtATimePolicy> SetT;
216 typedef sorted_vector_map<int,int,std::less<int>,
217 std::allocator<std::pair<int,int>>,OneAtATimePolicy> MapT;
219 EXPECT_EQ(sizeof(SetT), sizeof(std::vector<int>));
220 EXPECT_EQ(sizeof(MapT), sizeof(std::vector<std::pair<int,int> >));
223 TEST(SortedVectorTypes, InitializerLists) {
224 sorted_vector_set<int> empty_initialized_set{};
225 EXPECT_TRUE(empty_initialized_set.empty());
227 sorted_vector_set<int> singleton_initialized_set{1};
228 EXPECT_EQ(1, singleton_initialized_set.size());
229 EXPECT_EQ(1, *singleton_initialized_set.begin());
231 sorted_vector_set<int> forward_initialized_set{1, 2};
232 sorted_vector_set<int> backward_initialized_set{2, 1};
233 EXPECT_EQ(2, forward_initialized_set.size());
234 EXPECT_EQ(1, *forward_initialized_set.begin());
235 EXPECT_EQ(2, *forward_initialized_set.rbegin());
236 EXPECT_TRUE(forward_initialized_set == backward_initialized_set);
238 sorted_vector_map<int,int> empty_initialized_map{};
239 EXPECT_TRUE(empty_initialized_map.empty());
241 sorted_vector_map<int,int> singleton_initialized_map{{1,10}};
242 EXPECT_EQ(1, singleton_initialized_map.size());
243 EXPECT_EQ(10, singleton_initialized_map[1]);
245 sorted_vector_map<int,int> forward_initialized_map{{1,10}, {2,20}};
246 sorted_vector_map<int,int> backward_initialized_map{{2,20}, {1,10}};
247 EXPECT_EQ(2, forward_initialized_map.size());
248 EXPECT_EQ(10, forward_initialized_map[1]);
249 EXPECT_EQ(20, forward_initialized_map[2]);
250 EXPECT_TRUE(forward_initialized_map == backward_initialized_map);
253 TEST(SortedVectorTypes, CustomCompare) {
254 sorted_vector_set<int,less_invert<int> > s;
255 for (int i = 0; i < 200; ++i)
259 sorted_vector_map<int,float,less_invert<int> > m;
260 for (int i = 0; i < 200; ++i)
265 TEST(SortedVectorTypes, GrowthPolicy) {
266 typedef sorted_vector_set<CountCopyCtor,
267 std::less<CountCopyCtor>,
268 std::allocator<CountCopyCtor>,
273 for (int i = 0; i < 20; ++i) {
274 a.insert(CountCopyCtor(i));
277 SetT::iterator it = a.begin();
278 EXPECT_FALSE(it == a.end());
280 EXPECT_EQ(it->val_, 0);
281 // 1 copy for the initial insertion, 19 more for reallocs on the
282 // additional insertions.
283 EXPECT_EQ(it->count_, 20);
286 std::list<CountCopyCtor> v;
287 for (int i = 0; i < 20; ++i) {
288 v.emplace_back(20 + i);
290 a.insert(v.begin(), v.end());
294 EXPECT_FALSE(it == a.end());
296 EXPECT_EQ(it->val_, 0);
297 // Should be only 1 more copy for inserting this above range.
298 EXPECT_EQ(it->count_, 21);
302 TEST(SortedVectorTest, EmptyTest) {
303 sorted_vector_set<int> emptySet;
304 EXPECT_TRUE(emptySet.lower_bound(10) == emptySet.end());
305 EXPECT_TRUE(emptySet.find(10) == emptySet.end());
307 sorted_vector_map<int,int> emptyMap;
308 EXPECT_TRUE(emptyMap.lower_bound(10) == emptyMap.end());
309 EXPECT_TRUE(emptyMap.find(10) == emptyMap.end());
310 EXPECT_THROW(emptyMap.at(10), std::out_of_range);
313 TEST(SortedVectorTest, MoveTest) {
314 sorted_vector_set<std::unique_ptr<int>> s;
315 s.insert(std::unique_ptr<int>(new int(5)));
316 s.insert(s.end(), std::unique_ptr<int>(new int(10)));
317 EXPECT_EQ(s.size(), 2);
319 for (const auto& p : s) {
320 EXPECT_TRUE(*p == 5 || *p == 10);
323 sorted_vector_map<int, std::unique_ptr<int>> m;
324 m.insert(std::make_pair(5, std::unique_ptr<int>(new int(5))));
325 m.insert(m.end(), std::make_pair(10, std::unique_ptr<int>(new int(10))));
328 EXPECT_EQ(*m[10], 10);
331 TEST(SortedVectorTest, ShrinkTest) {
332 sorted_vector_set<int> s;
334 // Hopefully your resize policy doubles when capacity is full, or this will
336 while (s.capacity() == s.size()) {
340 // The standard does not actually enforce that this be true, but assume that
341 // vector::shrink_to_fit respects the caller.
342 EXPECT_EQ(s.capacity(), s.size());
345 TEST(SortedVectorTypes, EraseTest) {
346 sorted_vector_set<int> s1;
348 sorted_vector_set<int> s2(s1);
349 EXPECT_EQ(0, s1.erase(0));
353 std::vector<int> extractValues(sorted_vector_set<CountCopyCtor> const& in) {
354 std::vector<int> ret;
358 std::back_inserter(ret),
359 [](const CountCopyCtor& c) { return c.val_; });
363 template <typename T, typename S>
364 std::vector<T> makeVectorOfWrappers(std::vector<S> ss) {
366 ts.reserve(ss.size());
367 for (auto const& s : ss) {
373 TEST(SortedVectorTypes, TestSetBulkInsertionSortMerge) {
374 auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
376 sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
377 check_invariant(vset);
379 // Add an unsorted range that will have to be merged in.
380 s = makeVectorOfWrappers<CountCopyCtor, int>({10, 7, 5, 1});
382 vset.insert(s.begin(), s.end());
383 check_invariant(vset);
384 EXPECT_EQ(vset.rbegin()->count_, 1);
388 testing::ElementsAreArray({1, 2, 4, 5, 6, 7, 8, 10}));
391 TEST(SortedVectorTypes, TestSetBulkInsertionSortNoMerge) {
392 auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
394 sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
395 check_invariant(vset);
397 // Add an unsorted range that will not have to be merged in.
398 s = makeVectorOfWrappers<CountCopyCtor, int>({20, 15, 16, 13});
400 vset.insert(s.begin(), s.end());
401 check_invariant(vset);
402 EXPECT_EQ(vset.rbegin()->count_, 1);
405 testing::ElementsAreArray({2, 4, 6, 8, 13, 15, 16, 20}));
408 TEST(SortedVectorTypes, TestSetBulkInsertionNoSortMerge) {
409 auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
411 sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
412 check_invariant(vset);
414 // Add a sorted range that will have to be merged in.
415 s = makeVectorOfWrappers<CountCopyCtor, int>({1, 3, 5, 9});
417 vset.insert(s.begin(), s.end());
418 check_invariant(vset);
419 EXPECT_EQ(vset.rbegin()->count_, 1);
421 extractValues(vset), testing::ElementsAreArray({1, 2, 3, 4, 5, 6, 8, 9}));
424 TEST(SortedVectorTypes, TestSetBulkInsertionNoSortNoMerge) {
425 auto s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
427 sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
428 check_invariant(vset);
430 // Add a sorted range that will not have to be merged in.
431 s = makeVectorOfWrappers<CountCopyCtor, int>({21, 22, 23, 24});
433 vset.insert(s.begin(), s.end());
434 check_invariant(vset);
435 EXPECT_EQ(vset.rbegin()->count_, 1);
438 testing::ElementsAreArray({2, 4, 6, 8, 21, 22, 23, 24}));
441 TEST(SortedVectorTypes, TestSetBulkInsertionEmptyRange) {
442 std::vector<CountCopyCtor> s;
443 EXPECT_TRUE(s.empty());
445 // insertion of empty range into empty container.
446 sorted_vector_set<CountCopyCtor> vset(s.begin(), s.end());
447 check_invariant(vset);
449 s = makeVectorOfWrappers<CountCopyCtor, int>({6, 4, 8, 2});
451 vset.insert(s.begin(), s.end());
453 // insertion of empty range into non-empty container.
455 vset.insert(s.begin(), s.end());
456 check_invariant(vset);
458 EXPECT_THAT(extractValues(vset), testing::ElementsAreArray({2, 4, 6, 8}));
461 // This is a test of compilation - the behavior has already been tested
462 // extensively above.
463 TEST(SortedVectorTypes, TestBulkInsertionUncopyableTypes) {
464 std::vector<std::pair<int, std::unique_ptr<int>>> s;
465 s.emplace_back(1, std::make_unique<int>(0));
467 sorted_vector_map<int, std::unique_ptr<int>> vmap(
468 std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
471 s.emplace_back(3, std::make_unique<int>(0));
473 std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
476 // A moveable and copyable struct, which we use to make sure that no copy
477 // operations are performed during bulk insertion if moving is an option.
480 explicit Movable(int x) : x_(x) {}
481 Movable(const Movable&) {
482 ADD_FAILURE() << "Copy ctor should not be called";
484 Movable& operator=(const Movable&) {
485 ADD_FAILURE() << "Copy assignment should not be called";
489 Movable(Movable&&) = default;
490 Movable& operator=(Movable&&) = default;
493 TEST(SortedVectorTypes, TestBulkInsertionMovableTypes) {
494 std::vector<std::pair<int, Movable>> s;
495 s.emplace_back(3, Movable(2));
496 s.emplace_back(1, Movable(0));
498 sorted_vector_map<int, Movable> vmap(
499 std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));
502 s.emplace_back(4, Movable(3));
503 s.emplace_back(2, Movable(1));
505 std::make_move_iterator(s.begin()), std::make_move_iterator(s.end()));