proxygen
FBVectorTests.cpp.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
22 TESTFUN(clause_23_3_6_1_1) {
23  VECTOR v;
24  EXPECT_TRUE(v.empty());
25  VECTOR::allocator_type a;
26  VECTOR v1(a);
27  EXPECT_TRUE(v1.empty());
28 }
29 
30 TESTFUN(clause_23_3_6_1_3) {
31  auto const n = random(0U, 10000U);
32  VECTOR v(n);
33  EXPECT_EQ(v.size(), n);
34  FOR_EACH (i, v) { EXPECT_EQ(*i, VECTOR::value_type()); }
35 }
36 
37 TESTFUN(clause_23_3_6_1_9) {
38  // Insert with iterators
39  list<VECTOR::value_type> lst;
40  auto const n = random(0U, 10000U);
41  FOR_EACH_RANGE (i, 0, n) {
42  lst.push_back(randomObject<VECTOR::value_type>());
43  }
44  VECTOR v(lst.begin(), lst.end());
45  EXPECT_EQ(v.size(), lst.size());
46  size_t j = 0;
47  FOR_EACH (i, lst) {
48  EXPECT_EQ(v[j], *i);
49  j++;
50  }
51 }
52 
53 TESTFUN(clause_23_3_6_1_11) {
54  // assign with iterators
55  list<VECTOR::value_type> lst;
56  auto const n = random(0U, 10000U);
57  FOR_EACH_RANGE (i, 0, n) {
58  lst.push_back(randomObject<VECTOR::value_type>());
59  }
60  VECTOR v;
61  v.assign(lst.begin(), lst.end());
62  EXPECT_EQ(v.size(), lst.size());
63  size_t j = 0;
64  FOR_EACH (i, lst) {
65  EXPECT_EQ(v[j], *i);
66  j++;
67  }
68 
69  // aliased assign
70  v.assign(v.begin(), v.begin() + v.size() / 2);
71  EXPECT_EQ(v.size(), lst.size() / 2);
72  j = 0;
73  FOR_EACH (i, lst) {
74  if (j == v.size()) {
75  break;
76  }
77  EXPECT_EQ(v[j], *i);
78  j++;
79  }
80 }
81 
82 TESTFUN(clause_23_3_6_1_12) {
83  VECTOR v;
84  auto const n = random(0U, 10000U);
85  auto const obj = randomObject<VECTOR::value_type>();
86  v.assign(n, obj);
87  EXPECT_EQ(v.size(), n);
88  FOR_EACH (i, v) { EXPECT_EQ(*i, obj); }
89 }
90 
91 TESTFUN(clause_23_3_6_2_1) {
92  VECTOR v;
93  auto const n = random(0U, 10000U);
94  v.reserve(n);
95  EXPECT_GE(v.capacity(), n);
96 }
97 
98 TESTFUN(clause_23_3_6_2_7) {
99  auto const n1 = random(0U, 10000U);
100  auto const n2 = random(0U, 10000U);
101  auto const obj1 = randomObject<VECTOR::value_type>();
102  auto const obj2 = randomObject<VECTOR::value_type>();
103  VECTOR v1(n1, obj1), v2(n2, obj2);
104  v1.swap(v2);
105  EXPECT_EQ(v1.size(), n2);
106  EXPECT_EQ(v2.size(), n1);
107  FOR_EACH (i, v1) { EXPECT_EQ(*i, obj2); }
108  FOR_EACH (i, v2) { EXPECT_EQ(*i, obj1); }
109 }
110 
111 TESTFUN(clause_23_3_6_2_9) {
112  VECTOR v;
113  auto const n1 = random(0U, 10000U);
114  v.resize(n1);
115  FOR_EACH (i, v) { EXPECT_EQ(*i, VECTOR::value_type()); }
116  FOR_EACH (i, v) { EXPECT_EQ(*i, VECTOR::value_type()); }
117 }
118 
119 TESTFUN(clause_23_3_6_2_11) {
120  VECTOR v;
121  auto const n1 = random(0U, 10000U);
122  auto const obj1 = randomObject<VECTOR::value_type>();
123  v.resize(n1, obj1);
124  FOR_EACH (i, v) { EXPECT_EQ(*i, obj1); }
125  auto const n2 = random(0U, 10000U);
126  auto const obj2 = randomObject<VECTOR::value_type>();
127  v.resize(n2, obj2);
128  if (n1 < n2) {
129  FOR_EACH_RANGE (i, n1, n2) { EXPECT_EQ(v[i], obj2); }
130  }
131 }
132 
133 TESTFUN(clause_absent_element_access) {
134  VECTOR v;
135  auto const n1 = random(1U, 10000U);
136  auto const obj1 = randomObject<VECTOR::value_type>();
137  v.resize(n1, obj1);
138  auto const n = random(0U, v.size() - 1);
139  EXPECT_EQ(v[n], v.at(n));
140  auto const obj2 = randomObject<VECTOR::value_type>();
141  v[n] = obj2;
142  EXPECT_EQ(v[n], v.at(n));
143  EXPECT_EQ(v[n], obj2);
144  auto const obj3 = randomObject<VECTOR::value_type>();
145  v.at(n) = obj3;
146  EXPECT_EQ(v[n], v.at(n));
147  EXPECT_EQ(v[n], obj3);
148 }
149 
150 TESTFUN(clause_23_3_6_3_1) {
151  VECTOR v;
152  auto const n1 = random(1U, 10000U);
153  auto const obj1 = randomObject<VECTOR::value_type>();
154  v.resize(n1, obj1);
155  EXPECT_EQ(v.data(), &v.front());
156 }
157 
158 TESTFUN(clause_23_3_6_4_1_a) {
159  VECTOR v, w;
160  auto const n1 = random(1U, 10000U);
161  FOR_EACH_RANGE (i, 0, n1) {
162  auto const obj1 = randomObject<VECTOR::value_type>();
163  v.push_back(obj1);
164  w.push_back(obj1);
165  }
166  auto const n2 = random(0U, n1 - 1);
167  auto pos = v.begin() + n2;
168  auto const obj2 = randomObject<VECTOR::value_type>();
169 
170  auto r = v.insert(pos, obj2);
171 
172  EXPECT_EQ(v.size(), w.size() + 1);
173  EXPECT_EQ(r - v.begin(), n2);
174  EXPECT_EQ(*r, obj2);
175  FOR_EACH_RANGE (i, 0, r - v.begin()) { EXPECT_EQ(v[i], w[i]); }
176  FOR_EACH_RANGE (i, r - v.begin() + 1, v.size()) { EXPECT_EQ(v[i], w[i - 1]); }
177 }
178 
179 TESTFUN(clause_23_3_6_4_1_c) {
180  // This test only works for fbvector
181  fbvector<VECTOR::value_type> v, w;
182  auto const n1 = random(1U, 10000U);
183  FOR_EACH_RANGE (i, 0, n1) {
184  auto const obj1 = randomObject<VECTOR::value_type>();
185  v.push_back(obj1);
186  w.push_back(obj1);
187  }
188  auto const n2 = random(0U, n1 - 1);
189  auto pos = v.begin() + n2;
190  auto const obj2 = randomObject<VECTOR::value_type>();
191  auto const n3 = random(0U, 10000U);
192 
193  auto r = v.insert(pos, n3, obj2);
194 
195  EXPECT_EQ(v.size(), w.size() + n3);
196  EXPECT_EQ(r - v.begin(), n2);
197  FOR_EACH_RANGE (i, 0, r - v.begin()) { EXPECT_EQ(v[i], w[i]); }
198  FOR_EACH_RANGE (i, r - v.begin(), r - v.begin() + n3) {
199  EXPECT_EQ(v[i], obj2);
200  }
201  FOR_EACH_RANGE (i, r - v.begin() + n3, v.size()) {
202  EXPECT_EQ(v[i], w[i - n3]);
203  }
204 }
205 
206 TESTFUN(clause_23_3_6_4_1_d) {
207  VECTOR v, w;
208  auto const n1 = random(0U, 10000U);
209  FOR_EACH_RANGE (i, 0, n1) {
210  auto const obj1 = randomObject<VECTOR::value_type>();
211  v.push_back(obj1);
212  w.push_back(obj1);
213  }
214  EXPECT_EQ(v.size(), n1);
215 
216  auto const obj2 = randomObject<VECTOR::value_type>();
217  v.push_back(obj2);
218  EXPECT_EQ(v.back(), obj2);
219  EXPECT_EQ(v.size(), w.size() + 1);
220 
221  FOR_EACH_RANGE (i, 0, w.size()) { EXPECT_EQ(v[i], w[i]); }
222 }
223 
224 TESTFUN(clause_23_3_6_4_3) {
225  VECTOR v, w;
226  auto const n1 = random(1U, 10000U);
227  FOR_EACH_RANGE (i, 0, n1) {
228  auto const obj1 = randomObject<VECTOR::value_type>();
229  v.push_back(obj1);
230  w.push_back(obj1);
231  }
232  EXPECT_EQ(v.size(), n1);
233 
234  auto const n2 = random(0U, n1 - 1);
235  auto it = v.erase(v.begin() + n2);
236  EXPECT_EQ(v.size() + 1, w.size());
237 
238  FOR_EACH_RANGE (i, 0, it - v.begin()) { EXPECT_EQ(v[i], w[i]); }
239 
240  FOR_EACH_RANGE (i, it - v.begin(), v.size()) { EXPECT_EQ(v[i], w[i + 1]); }
241 }
242 
243 TESTFUN(clause_23_3_6_4_4) {
244  VECTOR v, w;
245  auto const n1 = random(1U, 10000U);
246  FOR_EACH_RANGE (i, 0, n1) {
247  auto const obj1 = randomObject<VECTOR::value_type>();
248  v.push_back(obj1);
249  w.push_back(obj1);
250  }
251  EXPECT_EQ(v.size(), n1);
252 
253  auto const n2 = random(0U, n1 - 1);
254  auto const n3 = random(n2, n1 - 1);
255  auto it = v.erase(v.begin() + n2, v.begin() + n3);
256  EXPECT_EQ(v.size() + (n3 - n2), w.size());
257 
258  FOR_EACH_RANGE (i, 0, it - v.begin()) { EXPECT_EQ(v[i], w[i]); }
259 
260  FOR_EACH_RANGE (i, it - v.begin(), v.size()) {
261  EXPECT_EQ(v[i], w[i + (n3 - n2)]);
262  }
263 }
264 
265 TESTFUN(clause_23_3_6_4_clear) {
266  VECTOR v;
267  v.clear();
268  EXPECT_TRUE(v.empty());
269  v.resize(random(0U, 10000U));
270  auto c = v.capacity();
271  v.clear();
272  EXPECT_TRUE(v.empty());
273  EXPECT_EQ(v.capacity(), c);
274 }
Integral2 random(Integral1 low, Integral2 up)
auto v
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
TESTFUN(clause_23_3_6_1_1)
char a
#define VECTOR
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define FOR_EACH(i, c)
Definition: Foreach.h:143
char c