2 * Copyright 2016 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 <gtest/gtest.h>
30 #include <boost/random.hpp>
33 using namespace folly;
37 auto static const seed = randomNumberSeed();
38 typedef boost::mt19937 RandomT;
39 static RandomT rng(seed);
41 template <class Integral1, class Integral2>
42 Integral2 random(Integral1 low, Integral2 up) {
43 boost::uniform_int<> range(low, up);
47 template <class String>
48 void randomString(String* toFill, unsigned int maxSize = 1000) {
50 toFill->resize(random(0, maxSize));
51 FOR_EACH (i, *toFill) {
52 *i = random('a', 'z');
56 template <class String, class Integral>
57 void Num2String(String& str, Integral /* n */) {
59 sprintf(&str[0], "%ul", 10);
60 str.resize(strlen(str.c_str()));
63 template<class T> T randomObject();
65 template<> int randomObject<int>() {
66 return random(0, 1024);
70 ////////////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////////////
74 TEST(fbvector, clause_23_3_6_1_3_ambiguity) {
75 fbvector<int> v(10, 20);
76 EXPECT_EQ(v.size(), 10);
82 TEST(fbvector, clause_23_3_6_1_11_ambiguity) {
85 EXPECT_EQ(v.size(), 10);
91 TEST(fbvector, clause_23_3_6_2_6) {
93 auto const n = random(0U, 10000U);
95 auto const n1 = random(0U, 10000U);
96 auto const obj = randomObject<int>();
99 // Nothing to verify except that the call made it through
102 TEST(fbvector, clause_23_3_6_4_ambiguity) {
104 fbvector<int>::const_iterator i = v.end();
106 EXPECT_EQ(v.size(), 10);
112 TEST(fbvector, composition) {
113 fbvector< fbvector<double> > matrix(100, fbvector<double>(100));
116 TEST(fbvector, works_with_std_string) {
117 fbvector<std::string> v(10, "hello");
118 EXPECT_EQ(v.size(), 10);
119 v.push_back("world");
123 class UserDefinedType { int whatevs_; };
126 FOLLY_ASSUME_FBVECTOR_COMPATIBLE(UserDefinedType);
128 TEST(fbvector, works_with_user_defined_type) {
129 fbvector<UserDefinedType> v(10);
130 EXPECT_EQ(v.size(), 10);
131 v.push_back(UserDefinedType());
134 TEST(fbvector, move_construction) {
135 fbvector<int> v1(100, 100);
137 EXPECT_EQ(v1.size(), 100);
138 EXPECT_EQ(v1.front(), 100);
139 EXPECT_EQ(v2.size(), 0);
141 EXPECT_EQ(v1.size(), 0);
142 EXPECT_EQ(v2.size(), 100);
143 EXPECT_EQ(v2.front(), 100);
146 auto other = std::move(v1);
147 EXPECT_EQ(v1.size(), 0);
148 EXPECT_EQ(other.size(), 100);
149 EXPECT_EQ(other.front(), 100);
152 TEST(fbvector, emplace) {
153 fbvector<std::string> s(12, "asd");
154 EXPECT_EQ(s.size(), 12);
155 EXPECT_EQ(s.front(), "asd");
156 s.emplace_back("funk");
157 EXPECT_EQ(s.back(), "funk");
160 TEST(fbvector, initializer_lists) {
161 fbvector<int> vec = { 1, 2, 3 };
162 EXPECT_EQ(vec.size(), 3);
163 EXPECT_EQ(vec[0], 1);
164 EXPECT_EQ(vec[1], 2);
165 EXPECT_EQ(vec[2], 3);
167 vec = { 0, 0, 12, 16 };
168 EXPECT_EQ(vec.size(), 4);
169 EXPECT_EQ(vec[0], 0);
170 EXPECT_EQ(vec[1], 0);
171 EXPECT_EQ(vec[2], 12);
172 EXPECT_EQ(vec[3], 16);
174 vec.insert(vec.begin() + 1, { 23, 23 });
175 EXPECT_EQ(vec.size(), 6);
176 EXPECT_EQ(vec[0], 0);
177 EXPECT_EQ(vec[1], 23);
178 EXPECT_EQ(vec[2], 23);
179 EXPECT_EQ(vec[3], 0);
180 EXPECT_EQ(vec[4], 12);
181 EXPECT_EQ(vec[5], 16);
184 TEST(fbvector, unique_ptr) {
185 fbvector<std::unique_ptr<int> > v(12);
186 std::unique_ptr<int> p(new int(12));
187 v.push_back(std::move(p));
188 EXPECT_EQ(*v.back(), 12);
191 EXPECT_FALSE(v[0].get());
192 v[0].reset(new int(32));
193 std::unique_ptr<int> somePtr;
194 v.insert(v.begin(), std::move(somePtr));
195 EXPECT_EQ(*v[1], 32);
198 TEST(FBVector, task858056) {
199 fbvector<fbstring> cycle;
200 cycle.push_back("foo");
201 cycle.push_back("bar");
202 cycle.push_back("baz");
203 fbstring message("Cycle detected: ");
204 FOR_EACH_R (node_name, cycle) {
206 message += *node_name;
209 EXPECT_EQ("Cycle detected: [baz] [bar] [foo] ", message);
212 TEST(FBVector, move_iterator) {
213 fbvector<int> base = { 0, 1, 2 };
216 fbvector<int> fbvi1(std::make_move_iterator(cp1.begin()),
217 std::make_move_iterator(cp1.end()));
218 EXPECT_EQ(fbvi1, base);
222 fbvi2.assign(std::make_move_iterator(cp2.begin()),
223 std::make_move_iterator(cp2.end()));
224 EXPECT_EQ(fbvi2, base);
228 fbvi3.insert(fbvi3.end(),
229 std::make_move_iterator(cp3.begin()),
230 std::make_move_iterator(cp3.end()));
231 EXPECT_EQ(fbvi3, base);
234 TEST(FBVector, reserve_consistency) {
235 struct S { int64_t a, b, c, d; };
238 for (size_t i = 0; i < 1000; ++i) {
240 EXPECT_EQ(fb1.size(), 0);
245 TEST(FBVector, vector_of_maps) {
246 fbvector<std::map<std::string, std::string>> v;
248 v.push_back(std::map<std::string, std::string>());
249 v.push_back(std::map<std::string, std::string>());
251 EXPECT_EQ(2, v.size());
253 v[1]["hello"] = "world";
254 EXPECT_EQ(0, v[0].size());
255 EXPECT_EQ(1, v[1].size());
258 EXPECT_EQ(1, v[0].size());
259 EXPECT_EQ(1, v[1].size());
262 TEST(FBVector, shrink_to_fit_after_clear) {
268 EXPECT_EQ(fb1.size(), 0);
269 EXPECT_EQ(fb1.capacity(), 0);