2 * Copyright 2015 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/Foreach.h>
21 #include <folly/Traits.h>
22 #include <folly/Random.h>
23 #include <folly/FBString.h>
24 #include <folly/FBVector.h>
26 #include <gflags/gflags.h>
28 #include <gtest/gtest.h>
32 #include <boost/random.hpp>
35 using namespace folly;
37 auto static const seed = randomNumberSeed();
38 typedef boost::mt19937 RandomT;
39 static RandomT rng(seed);
40 static const size_t maxString = 100;
41 static const bool avoidAliasing = true;
43 template <class Integral1, class Integral2>
44 Integral2 random(Integral1 low, Integral2 up) {
45 boost::uniform_int<> range(low, up);
49 template <class String>
50 void randomString(String* toFill, unsigned int maxSize = 1000) {
52 toFill->resize(random(0, maxSize));
53 FOR_EACH (i, *toFill) {
54 *i = random('a', 'z');
58 template <class String, class Integral>
59 void Num2String(String& str, Integral n) {
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 TEST(FBVector, move_iterator) {
229 fbvector<int> base = { 0, 1, 2 };
232 fbvector<int> fbvi1(std::make_move_iterator(cp1.begin()),
233 std::make_move_iterator(cp1.end()));
234 EXPECT_EQ(fbvi1, base);
238 fbvi2.assign(std::make_move_iterator(cp2.begin()),
239 std::make_move_iterator(cp2.end()));
240 EXPECT_EQ(fbvi2, base);
244 fbvi3.insert(fbvi3.end(),
245 std::make_move_iterator(cp3.begin()),
246 std::make_move_iterator(cp3.end()));
247 EXPECT_EQ(fbvi3, base);
250 TEST(FBVector, reserve_consistency) {
251 struct S { int64_t a, b, c, d; };
254 for (size_t i = 0; i < 1000; ++i) {
256 EXPECT_EQ(fb1.size(), 0);
261 TEST(FBVector, vector_of_maps) {
262 fbvector<std::map<std::string, std::string>> v;
264 v.push_back(std::map<std::string, std::string>());
265 v.push_back(std::map<std::string, std::string>());
267 EXPECT_EQ(2, v.size());
269 v[1]["hello"] = "world";
270 EXPECT_EQ(0, v[0].size());
271 EXPECT_EQ(1, v[1].size());
274 EXPECT_EQ(1, v[0].size());
275 EXPECT_EQ(1, v[1].size());
278 TEST(FBVector, shrink_to_fit_after_clear) {
284 EXPECT_EQ(fb1.size(), 0);
285 EXPECT_EQ(fb1.capacity(), 0);
288 int main(int argc, char** argv) {
289 testing::InitGoogleTest(&argc, argv);
290 gflags::ParseCommandLineFlags(&argc, &argv, true);
291 return RUN_ALL_TESTS();