2 * Copyright 2012 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.
18 // Author: andrei.alexandrescu@fb.com
20 #include "folly/Traits.h"
21 #include "folly/Random.h"
22 #include "folly/FBString.h"
23 #include "folly/FBVector.h"
24 #include "folly/Benchmark.h"
26 #include <gflags/gflags.h>
28 #include <gtest/gtest.h>
31 #include <boost/random.hpp>
34 using namespace folly;
36 auto static const seed = randomNumberSeed();
37 typedef boost::mt19937 RandomT;
38 static RandomT rng(seed);
39 static const size_t maxString = 100;
40 static const bool avoidAliasing = true;
42 template <class Integral1, class Integral2>
43 Integral2 random(Integral1 low, Integral2 up) {
44 boost::uniform_int<> range(low, up);
48 template <class String>
49 void randomString(String* toFill, unsigned int maxSize = 1000) {
51 toFill->resize(random(0, maxSize));
52 FOR_EACH (i, *toFill) {
53 *i = random('a', 'z');
57 template <class String, class Integral>
58 void Num2String(String& str, Integral n) {
60 // ultoa((unsigned long)n, &str[0], 10);
61 sprintf(&str[0], "%ul", 10);
62 str.resize(strlen(str.c_str()));
65 std::list<char> RandomList(unsigned int maxSize) {
66 std::list<char> lst(random(0u, maxSize));
67 std::list<char>::iterator i = lst.begin();
68 for (; i != lst.end(); ++i) {
69 *i = random('a', 'z');
74 template<class T> T randomObject();
76 template<> int randomObject<int>() {
77 return random(0, 1024);
80 template<> folly::fbstring randomObject<folly::fbstring>() {
81 folly::fbstring result;
82 randomString(&result);
86 ////////////////////////////////////////////////////////////////////////////////
88 ////////////////////////////////////////////////////////////////////////////////
90 TEST(fbvector, clause_23_3_6_1_3_ambiguity) {
91 fbvector<int> v(10, 20);
92 EXPECT_EQ(v.size(), 10);
98 TEST(fbvector, clause_23_3_6_1_11_ambiguity) {
101 EXPECT_EQ(v.size(), 10);
107 TEST(fbvector, clause_23_3_6_2_6) {
109 auto const n = random(0U, 10000U);
111 auto const n1 = random(0U, 10000U);
112 auto const obj = randomObject<int>();
115 // Nothing to verify except that the call made it through
118 TEST(fbvector, clause_23_3_6_4_ambiguity) {
120 fbvector<int>::const_iterator i = v.end();
122 EXPECT_EQ(v.size(), 10);
128 TEST(fbvector, composition) {
129 fbvector< fbvector<double> > matrix(100, fbvector<double>(100));
132 TEST(fbvector, works_with_std_string) {
133 fbvector<std::string> v(10, "hello");
134 EXPECT_EQ(v.size(), 10);
135 v.push_back("world");
139 class UserDefinedType { int whatevs_; };
142 FOLLY_ASSUME_FBVECTOR_COMPATIBLE(UserDefinedType);
144 TEST(fbvector, works_with_user_defined_type) {
145 fbvector<UserDefinedType> v(10);
146 EXPECT_EQ(v.size(), 10);
147 v.push_back(UserDefinedType());
150 TEST(fbvector, move_construction) {
151 fbvector<int> v1(100, 100);
153 EXPECT_EQ(v1.size(), 100);
154 EXPECT_EQ(v1.front(), 100);
155 EXPECT_EQ(v2.size(), 0);
157 EXPECT_EQ(v1.size(), 0);
158 EXPECT_EQ(v2.size(), 100);
159 EXPECT_EQ(v2.front(), 100);
162 auto other = std::move(v1);
163 EXPECT_EQ(v1.size(), 0);
164 EXPECT_EQ(other.size(), 100);
165 EXPECT_EQ(other.front(), 100);
168 TEST(fbvector, emplace) {
169 fbvector<std::string> s(12, "asd");
170 EXPECT_EQ(s.size(), 12);
171 EXPECT_EQ(s.front(), "asd");
172 s.emplace_back("funk");
173 EXPECT_EQ(s.back(), "funk");
176 TEST(fbvector, initializer_lists) {
177 fbvector<int> vec = { 1, 2, 3 };
178 EXPECT_EQ(vec.size(), 3);
179 EXPECT_EQ(vec[0], 1);
180 EXPECT_EQ(vec[1], 2);
181 EXPECT_EQ(vec[2], 3);
183 vec = { 0, 0, 12, 16 };
184 EXPECT_EQ(vec.size(), 4);
185 EXPECT_EQ(vec[0], 0);
186 EXPECT_EQ(vec[1], 0);
187 EXPECT_EQ(vec[2], 12);
188 EXPECT_EQ(vec[3], 16);
190 vec.insert(vec.begin() + 1, { 23, 23 });
191 EXPECT_EQ(vec.size(), 6);
192 EXPECT_EQ(vec[0], 0);
193 EXPECT_EQ(vec[1], 23);
194 EXPECT_EQ(vec[2], 23);
195 EXPECT_EQ(vec[3], 0);
196 EXPECT_EQ(vec[4], 12);
197 EXPECT_EQ(vec[5], 16);
200 TEST(fbvector, unique_ptr) {
201 fbvector<std::unique_ptr<int> > v(12);
202 std::unique_ptr<int> p(new int(12));
203 v.push_back(std::move(p));
204 EXPECT_EQ(*v.back(), 12);
207 EXPECT_FALSE(v[0].get());
208 v[0].reset(new int(32));
209 std::unique_ptr<int> somePtr;
210 v.insert(v.begin(), std::move(somePtr));
211 EXPECT_EQ(*v[1], 32);
214 TEST(FBVector, task858056) {
215 fbvector<fbstring> cycle;
216 cycle.push_back("foo");
217 cycle.push_back("bar");
218 cycle.push_back("baz");
219 fbstring message("Cycle detected: ");
220 FOR_EACH_R (node_name, cycle) {
222 message += *node_name;
225 EXPECT_EQ("Cycle detected: [baz] [bar] [foo] ", message);
228 #define CONCAT(A, B) CONCAT_HELPER(A, B)
229 #define CONCAT_HELPER(A, B) A##B
230 #define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR))
231 #define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR))
233 typedef vector<int> IntVector;
234 typedef fbvector<int> IntFBVector;
235 typedef vector<folly::fbstring> FBStringVector;
236 typedef fbvector<folly::fbstring> FBStringFBVector;
238 #define VECTOR IntVector
239 #include "folly/test/FBVectorTestBenchmarks.cpp.h"
241 #define VECTOR IntFBVector
242 #include "folly/test/FBVectorTestBenchmarks.cpp.h"
244 #define VECTOR FBStringVector
245 #include "folly/test/FBVectorTestBenchmarks.cpp.h"
247 #define VECTOR FBStringFBVector
248 #include "folly/test/FBVectorTestBenchmarks.cpp.h"
251 int main(int argc, char** argv) {
252 testing::InitGoogleTest(&argc, argv);
253 google::ParseCommandLineFlags(&argc, &argv, true);
254 auto ret = RUN_ALL_TESTS();
255 if (!ret && FLAGS_benchmark) {
256 folly::runBenchmarks();