/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2011-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
#include <folly/small_vector.h>
+#include <folly/sorted_vector_types.h>
#include <iostream>
#include <iterator>
namespace {
+template <typename Key, typename Value, size_t N>
+using small_sorted_vector_map = folly::sorted_vector_map<
+ Key,
+ Value,
+ std::less<Key>,
+ std::allocator<std::pair<Key, Value>>,
+ void,
+ folly::small_vector<std::pair<Key, Value>, N>>;
+
+template <typename Key, typename Value, size_t N>
+using noheap_sorted_vector_map = folly::sorted_vector_map<
+ Key,
+ Value,
+ std::less<Key>,
+ std::allocator<std::pair<Key, Value>>,
+ void,
+ folly::small_vector<
+ std::pair<Key, Value>,
+ N,
+ folly::small_vector_policy::NoHeap>>;
+
+template <typename T, size_t N>
+using small_sorted_vector_set = folly::sorted_vector_set<
+ T,
+ std::less<T>,
+ std::allocator<T>,
+ void,
+ folly::small_vector<T, N>>;
+
+template <typename T, size_t N>
+using noheap_sorted_vector_set = folly::sorted_vector_set<
+ T,
+ std::less<T>,
+ std::allocator<T>,
+ void,
+ folly::small_vector<T, N, folly::small_vector_policy::NoHeap>>;
+
struct NontrivialType {
static int ctored;
explicit NontrivialType() : a(0) {}
throwCounter = 1000;
}
- template<class Operation>
+ template <class Operation>
void operator()(int insertCount, Operation const& op) {
bool done = false;
std::unique_ptr<folly::small_vector<Thrower,3> > workingVec;
for (int counter = 1; !done; ++counter) {
throwCounter = 1000;
- workingVec.reset(new folly::small_vector<Thrower,3>(vec));
+ workingVec = std::make_unique<folly::small_vector<Thrower, 3>>(vec);
throwCounter = counter;
EXPECT_EQ(Thrower::alive, prepopulate * 2);
try {
}
};
-}
+} // namespace
TEST(small_vector, BasicGuarantee) {
for (int prepop = 1; prepop < 30; ++prepop) {
return *this;
}
};
-}
+} // namespace
TEST(small_vector, EmplaceBackEfficiency) {
small_vector<Counter, 2> test;
EXPECT_EQ(test.size() - 1, counts.moveCount);
EXPECT_LT(test.size(), test.capacity());
}
+
+TEST(small_vector, StorageForSortedVectorMap) {
+ small_sorted_vector_map<int32_t, int32_t, 2> test;
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(20, 10));
+ EXPECT_EQ(test.size(), 2);
+ test.insert(std::make_pair(30, 10));
+ EXPECT_EQ(test.size(), 3);
+}
+
+TEST(small_vector, NoHeapStorageForSortedVectorMap) {
+ noheap_sorted_vector_map<int32_t, int32_t, 2> test;
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(20, 10));
+ EXPECT_EQ(test.size(), 2);
+ EXPECT_THROW(test.insert(std::make_pair(30, 10)), std::length_error);
+ EXPECT_EQ(test.size(), 2);
+}
+
+TEST(small_vector, StorageForSortedVectorSet) {
+ small_sorted_vector_set<int32_t, 2> test;
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(20);
+ EXPECT_EQ(test.size(), 2);
+ test.insert(30);
+ EXPECT_EQ(test.size(), 3);
+}
+
+TEST(small_vector, NoHeapStorageForSortedVectorSet) {
+ noheap_sorted_vector_set<int32_t, 2> test;
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(20);
+ EXPECT_EQ(test.size(), 2);
+ EXPECT_THROW(test.insert(30), std::length_error);
+ EXPECT_EQ(test.size(), 2);
+}