• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <utility>
16 #include <vector>
17 
18 #include "gmock/gmock.h"
19 #include "source/util/small_vector.h"
20 
21 namespace spvtools {
22 namespace utils {
23 namespace {
24 
25 using SmallVectorTest = ::testing::Test;
26 
TEST(SmallVectorTest,Initialize_default)27 TEST(SmallVectorTest, Initialize_default) {
28   SmallVector<uint32_t, 2> vec;
29 
30   EXPECT_TRUE(vec.empty());
31   EXPECT_EQ(vec.size(), 0);
32   EXPECT_EQ(vec.begin(), vec.end());
33 }
34 
TEST(SmallVectorTest,Initialize_list1)35 TEST(SmallVectorTest, Initialize_list1) {
36   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
37 
38   EXPECT_FALSE(vec.empty());
39   EXPECT_EQ(vec.size(), 4);
40 
41   uint32_t result[] = {0, 1, 2, 3};
42   for (uint32_t i = 0; i < vec.size(); ++i) {
43     EXPECT_EQ(vec[i], result[i]);
44   }
45 }
46 
TEST(SmallVectorTest,Initialize_list2)47 TEST(SmallVectorTest, Initialize_list2) {
48   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
49 
50   EXPECT_FALSE(vec.empty());
51   EXPECT_EQ(vec.size(), 4);
52 
53   uint32_t result[] = {0, 1, 2, 3};
54   for (uint32_t i = 0; i < vec.size(); ++i) {
55     EXPECT_EQ(vec[i], result[i]);
56   }
57 }
58 
TEST(SmallVectorTest,Initialize_list3)59 TEST(SmallVectorTest, Initialize_list3) {
60   std::vector<uint32_t> result = {0, 1, 2, 3};
61   SmallVector<uint32_t, 6> vec(result.begin(), result.end());
62 
63   EXPECT_FALSE(vec.empty());
64   EXPECT_EQ(vec.size(), 4);
65 
66   for (uint32_t i = 0; i < vec.size(); ++i) {
67     EXPECT_EQ(vec[i], result[i]);
68   }
69 }
70 
TEST(SmallVectorTest,Initialize_copy1)71 TEST(SmallVectorTest, Initialize_copy1) {
72   SmallVector<uint32_t, 6> vec1 = {0, 1, 2, 3};
73   SmallVector<uint32_t, 6> vec2(vec1);
74 
75   EXPECT_EQ(vec2.size(), 4);
76 
77   uint32_t result[] = {0, 1, 2, 3};
78   for (uint32_t i = 0; i < vec2.size(); ++i) {
79     EXPECT_EQ(vec2[i], result[i]);
80   }
81 
82   EXPECT_EQ(vec1, vec2);
83 }
84 
TEST(SmallVectorTest,Initialize_copy2)85 TEST(SmallVectorTest, Initialize_copy2) {
86   SmallVector<uint32_t, 2> vec1 = {0, 1, 2, 3};
87   SmallVector<uint32_t, 2> vec2(vec1);
88 
89   EXPECT_EQ(vec2.size(), 4);
90 
91   uint32_t result[] = {0, 1, 2, 3};
92   for (uint32_t i = 0; i < vec2.size(); ++i) {
93     EXPECT_EQ(vec2[i], result[i]);
94   }
95 
96   EXPECT_EQ(vec1, vec2);
97 }
98 
TEST(SmallVectorTest,Initialize_copy_vec1)99 TEST(SmallVectorTest, Initialize_copy_vec1) {
100   std::vector<uint32_t> vec1 = {0, 1, 2, 3};
101   SmallVector<uint32_t, 6> vec2(vec1);
102 
103   EXPECT_EQ(vec2.size(), 4);
104 
105   uint32_t result[] = {0, 1, 2, 3};
106   for (uint32_t i = 0; i < vec2.size(); ++i) {
107     EXPECT_EQ(vec2[i], result[i]);
108   }
109 
110   EXPECT_EQ(vec1, vec2);
111 }
112 
TEST(SmallVectorTest,Initialize_copy_vec2)113 TEST(SmallVectorTest, Initialize_copy_vec2) {
114   std::vector<uint32_t> vec1 = {0, 1, 2, 3};
115   SmallVector<uint32_t, 2> vec2(vec1);
116 
117   EXPECT_EQ(vec2.size(), 4);
118 
119   uint32_t result[] = {0, 1, 2, 3};
120   for (uint32_t i = 0; i < vec2.size(); ++i) {
121     EXPECT_EQ(vec2[i], result[i]);
122   }
123 
124   EXPECT_EQ(vec1, vec2);
125 }
126 
TEST(SmallVectorTest,Initialize_move1)127 TEST(SmallVectorTest, Initialize_move1) {
128   SmallVector<uint32_t, 6> vec1 = {0, 1, 2, 3};
129   SmallVector<uint32_t, 6> vec2(std::move(vec1));
130 
131   EXPECT_EQ(vec2.size(), 4);
132 
133   uint32_t result[] = {0, 1, 2, 3};
134   for (uint32_t i = 0; i < vec2.size(); ++i) {
135     EXPECT_EQ(vec2[i], result[i]);
136   }
137   EXPECT_TRUE(vec1.empty());
138 }
139 
TEST(SmallVectorTest,Initialize_move2)140 TEST(SmallVectorTest, Initialize_move2) {
141   SmallVector<uint32_t, 2> vec1 = {0, 1, 2, 3};
142   SmallVector<uint32_t, 2> vec2(std::move(vec1));
143 
144   EXPECT_EQ(vec2.size(), 4);
145 
146   uint32_t result[] = {0, 1, 2, 3};
147   for (uint32_t i = 0; i < vec2.size(); ++i) {
148     EXPECT_EQ(vec2[i], result[i]);
149   }
150   EXPECT_TRUE(vec1.empty());
151 }
152 
TEST(SmallVectorTest,Initialize_move_vec1)153 TEST(SmallVectorTest, Initialize_move_vec1) {
154   std::vector<uint32_t> vec1 = {0, 1, 2, 3};
155   SmallVector<uint32_t, 6> vec2(std::move(vec1));
156 
157   EXPECT_EQ(vec2.size(), 4);
158 
159   uint32_t result[] = {0, 1, 2, 3};
160   for (uint32_t i = 0; i < vec2.size(); ++i) {
161     EXPECT_EQ(vec2[i], result[i]);
162   }
163   EXPECT_TRUE(vec1.empty());
164 }
165 
TEST(SmallVectorTest,Initialize_move_vec2)166 TEST(SmallVectorTest, Initialize_move_vec2) {
167   std::vector<uint32_t> vec1 = {0, 1, 2, 3};
168   SmallVector<uint32_t, 2> vec2(std::move(vec1));
169 
170   EXPECT_EQ(vec2.size(), 4);
171 
172   uint32_t result[] = {0, 1, 2, 3};
173   for (uint32_t i = 0; i < vec2.size(); ++i) {
174     EXPECT_EQ(vec2[i], result[i]);
175   }
176   EXPECT_TRUE(vec1.empty());
177 }
178 
TEST(SmallVectorTest,Initialize_iterators1)179 TEST(SmallVectorTest, Initialize_iterators1) {
180   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
181 
182   EXPECT_EQ(vec.size(), 4);
183   uint32_t result[] = {0, 1, 2, 3};
184 
185   uint32_t i = 0;
186   for (uint32_t p : vec) {
187     EXPECT_EQ(p, result[i]);
188     i++;
189   }
190 }
191 
TEST(SmallVectorTest,Initialize_iterators2)192 TEST(SmallVectorTest, Initialize_iterators2) {
193   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
194 
195   EXPECT_EQ(vec.size(), 4);
196   uint32_t result[] = {0, 1, 2, 3};
197 
198   uint32_t i = 0;
199   for (uint32_t p : vec) {
200     EXPECT_EQ(p, result[i]);
201     i++;
202   }
203 }
204 
TEST(SmallVectorTest,Initialize_iterators3)205 TEST(SmallVectorTest, Initialize_iterators3) {
206   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
207 
208   EXPECT_EQ(vec.size(), 4);
209   uint32_t result[] = {0, 1, 2, 3};
210 
211   uint32_t i = 0;
212   for (SmallVector<uint32_t, 2>::iterator it = vec.begin(); it != vec.end();
213        ++it) {
214     EXPECT_EQ(*it, result[i]);
215     i++;
216   }
217 }
218 
TEST(SmallVectorTest,Initialize_iterators4)219 TEST(SmallVectorTest, Initialize_iterators4) {
220   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
221 
222   EXPECT_EQ(vec.size(), 4);
223   uint32_t result[] = {0, 1, 2, 3};
224 
225   uint32_t i = 0;
226   for (SmallVector<uint32_t, 6>::iterator it = vec.begin(); it != vec.end();
227        ++it) {
228     EXPECT_EQ(*it, result[i]);
229     i++;
230   }
231 }
232 
TEST(SmallVectorTest,Initialize_iterators_write1)233 TEST(SmallVectorTest, Initialize_iterators_write1) {
234   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
235 
236   EXPECT_EQ(vec.size(), 4);
237   for (SmallVector<uint32_t, 6>::iterator it = vec.begin(); it != vec.end();
238        ++it) {
239     *it *= 2;
240   }
241 
242   uint32_t result[] = {0, 2, 4, 6};
243 
244   uint32_t i = 0;
245   for (SmallVector<uint32_t, 6>::iterator it = vec.begin(); it != vec.end();
246        ++it) {
247     EXPECT_EQ(*it, result[i]);
248     i++;
249   }
250 }
251 
TEST(SmallVectorTest,Initialize_iterators_write2)252 TEST(SmallVectorTest, Initialize_iterators_write2) {
253   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
254 
255   EXPECT_EQ(vec.size(), 4);
256   for (SmallVector<uint32_t, 2>::iterator it = vec.begin(); it != vec.end();
257        ++it) {
258     *it *= 2;
259   }
260 
261   uint32_t result[] = {0, 2, 4, 6};
262 
263   uint32_t i = 0;
264   for (SmallVector<uint32_t, 2>::iterator it = vec.begin(); it != vec.end();
265        ++it) {
266     EXPECT_EQ(*it, result[i]);
267     i++;
268   }
269 }
270 
TEST(SmallVectorTest,Initialize_front)271 TEST(SmallVectorTest, Initialize_front) {
272   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
273 
274   EXPECT_EQ(vec.front(), 0);
275   for (SmallVector<uint32_t, 2>::iterator it = vec.begin(); it != vec.end();
276        ++it) {
277     *it += 2;
278   }
279   EXPECT_EQ(vec.front(), 2);
280 }
281 
TEST(SmallVectorTest,Erase_element_front1)282 TEST(SmallVectorTest, Erase_element_front1) {
283   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
284 
285   EXPECT_EQ(vec.front(), 0);
286   EXPECT_EQ(vec.size(), 4);
287   vec.erase(vec.begin());
288   EXPECT_EQ(vec.front(), 1);
289   EXPECT_EQ(vec.size(), 3);
290 }
291 
TEST(SmallVectorTest,Erase_element_front2)292 TEST(SmallVectorTest, Erase_element_front2) {
293   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
294 
295   EXPECT_EQ(vec.front(), 0);
296   EXPECT_EQ(vec.size(), 4);
297   vec.erase(vec.begin());
298   EXPECT_EQ(vec.front(), 1);
299   EXPECT_EQ(vec.size(), 3);
300 }
301 
TEST(SmallVectorTest,Erase_element_back1)302 TEST(SmallVectorTest, Erase_element_back1) {
303   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
304   SmallVector<uint32_t, 2> result = {0, 1, 2};
305 
306   EXPECT_EQ(vec[3], 3);
307   EXPECT_EQ(vec.size(), 4);
308   vec.erase(vec.begin() + 3);
309   EXPECT_EQ(vec.size(), 3);
310   EXPECT_EQ(vec, result);
311 }
312 
TEST(SmallVectorTest,Erase_element_back2)313 TEST(SmallVectorTest, Erase_element_back2) {
314   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
315   SmallVector<uint32_t, 6> result = {0, 1, 2};
316 
317   EXPECT_EQ(vec[3], 3);
318   EXPECT_EQ(vec.size(), 4);
319   vec.erase(vec.begin() + 3);
320   EXPECT_EQ(vec.size(), 3);
321   EXPECT_EQ(vec, result);
322 }
323 
TEST(SmallVectorTest,Erase_element_middle1)324 TEST(SmallVectorTest, Erase_element_middle1) {
325   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
326   SmallVector<uint32_t, 2> result = {0, 1, 3};
327 
328   EXPECT_EQ(vec.size(), 4);
329   vec.erase(vec.begin() + 2);
330   EXPECT_EQ(vec.size(), 3);
331   EXPECT_EQ(vec, result);
332 }
333 
TEST(SmallVectorTest,Erase_element_middle2)334 TEST(SmallVectorTest, Erase_element_middle2) {
335   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
336   SmallVector<uint32_t, 6> result = {0, 1, 3};
337 
338   EXPECT_EQ(vec.size(), 4);
339   vec.erase(vec.begin() + 2);
340   EXPECT_EQ(vec.size(), 3);
341   EXPECT_EQ(vec, result);
342 }
343 
TEST(SmallVectorTest,Erase_range_1)344 TEST(SmallVectorTest, Erase_range_1) {
345   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
346   SmallVector<uint32_t, 6> result = {};
347 
348   EXPECT_EQ(vec.size(), 4);
349   vec.erase(vec.begin(), vec.end());
350   EXPECT_EQ(vec.size(), 0);
351   EXPECT_EQ(vec, result);
352 }
353 
TEST(SmallVectorTest,Erase_range_2)354 TEST(SmallVectorTest, Erase_range_2) {
355   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
356   SmallVector<uint32_t, 2> result = {};
357 
358   EXPECT_EQ(vec.size(), 4);
359   vec.erase(vec.begin(), vec.end());
360   EXPECT_EQ(vec.size(), 0);
361   EXPECT_EQ(vec, result);
362 }
363 
TEST(SmallVectorTest,Erase_range_3)364 TEST(SmallVectorTest, Erase_range_3) {
365   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
366   SmallVector<uint32_t, 6> result = {2, 3};
367 
368   EXPECT_EQ(vec.size(), 4);
369   vec.erase(vec.begin(), vec.begin() + 2);
370   EXPECT_EQ(vec.size(), 2);
371   EXPECT_EQ(vec, result);
372 }
373 
TEST(SmallVectorTest,Erase_range_4)374 TEST(SmallVectorTest, Erase_range_4) {
375   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
376   SmallVector<uint32_t, 2> result = {2, 3};
377 
378   EXPECT_EQ(vec.size(), 4);
379   vec.erase(vec.begin(), vec.begin() + 2);
380   EXPECT_EQ(vec.size(), 2);
381   EXPECT_EQ(vec, result);
382 }
383 
TEST(SmallVectorTest,Erase_range_5)384 TEST(SmallVectorTest, Erase_range_5) {
385   SmallVector<uint32_t, 6> vec = {0, 1, 2, 3};
386   SmallVector<uint32_t, 6> result = {0, 3};
387 
388   EXPECT_EQ(vec.size(), 4);
389   vec.erase(vec.begin() + 1, vec.begin() + 3);
390   EXPECT_EQ(vec.size(), 2);
391   EXPECT_EQ(vec, result);
392 }
393 
TEST(SmallVectorTest,Erase_range_6)394 TEST(SmallVectorTest, Erase_range_6) {
395   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
396   SmallVector<uint32_t, 2> result = {0, 3};
397 
398   EXPECT_EQ(vec.size(), 4);
399   vec.erase(vec.begin() + 1, vec.begin() + 3);
400   EXPECT_EQ(vec.size(), 2);
401   EXPECT_EQ(vec, result);
402 }
403 
TEST(SmallVectorTest,Push_back)404 TEST(SmallVectorTest, Push_back) {
405   SmallVector<uint32_t, 2> vec;
406   SmallVector<uint32_t, 2> result = {0, 1, 2, 3};
407 
408   EXPECT_EQ(vec.size(), 0);
409   vec.push_back(0);
410   EXPECT_EQ(vec.size(), 1);
411   vec.push_back(1);
412   EXPECT_EQ(vec.size(), 2);
413   vec.push_back(2);
414   EXPECT_EQ(vec.size(), 3);
415   vec.push_back(3);
416   EXPECT_EQ(vec.size(), 4);
417   EXPECT_EQ(vec, result);
418 }
419 
TEST(SmallVectorTest,Emplace_back)420 TEST(SmallVectorTest, Emplace_back) {
421   SmallVector<uint32_t, 2> vec;
422   SmallVector<uint32_t, 2> result = {0, 1, 2, 3};
423 
424   EXPECT_EQ(vec.size(), 0);
425   vec.emplace_back(0);
426   EXPECT_EQ(vec.size(), 1);
427   vec.emplace_back(1);
428   EXPECT_EQ(vec.size(), 2);
429   vec.emplace_back(2);
430   EXPECT_EQ(vec.size(), 3);
431   vec.emplace_back(3);
432   EXPECT_EQ(vec.size(), 4);
433   EXPECT_EQ(vec, result);
434 }
435 
TEST(SmallVectorTest,Clear)436 TEST(SmallVectorTest, Clear) {
437   SmallVector<uint32_t, 2> vec = {0, 1, 2, 3};
438   SmallVector<uint32_t, 2> result = {};
439 
440   EXPECT_EQ(vec.size(), 4);
441   vec.clear();
442   EXPECT_EQ(vec.size(), 0);
443   EXPECT_EQ(vec, result);
444 }
445 
TEST(SmallVectorTest,Insert1)446 TEST(SmallVectorTest, Insert1) {
447   SmallVector<uint32_t, 2> vec = {};
448   SmallVector<uint32_t, 2> insert_values = {10, 11};
449   SmallVector<uint32_t, 2> result = {10, 11};
450 
451   EXPECT_EQ(vec.size(), 0);
452   auto ret =
453       vec.insert(vec.begin(), insert_values.begin(), insert_values.end());
454   EXPECT_EQ(vec.size(), 2);
455   EXPECT_EQ(vec, result);
456   EXPECT_EQ(*ret, 10);
457 }
458 
TEST(SmallVectorTest,Insert2)459 TEST(SmallVectorTest, Insert2) {
460   SmallVector<uint32_t, 2> vec = {};
461   SmallVector<uint32_t, 2> insert_values = {10, 11, 12};
462   SmallVector<uint32_t, 2> result = {10, 11, 12};
463 
464   EXPECT_EQ(vec.size(), 0);
465   auto ret =
466       vec.insert(vec.begin(), insert_values.begin(), insert_values.end());
467   EXPECT_EQ(vec.size(), 3);
468   EXPECT_EQ(vec, result);
469   EXPECT_EQ(*ret, 10);
470 }
471 
TEST(SmallVectorTest,Insert3)472 TEST(SmallVectorTest, Insert3) {
473   SmallVector<uint32_t, 2> vec = {0};
474   SmallVector<uint32_t, 2> insert_values = {10, 11, 12};
475   SmallVector<uint32_t, 2> result = {10, 11, 12, 0};
476 
477   EXPECT_EQ(vec.size(), 1);
478   auto ret =
479       vec.insert(vec.begin(), insert_values.begin(), insert_values.end());
480   EXPECT_EQ(vec.size(), 4);
481   EXPECT_EQ(vec, result);
482   EXPECT_EQ(*ret, 10);
483 }
484 
TEST(SmallVectorTest,Insert4)485 TEST(SmallVectorTest, Insert4) {
486   SmallVector<uint32_t, 6> vec = {0};
487   SmallVector<uint32_t, 6> insert_values = {10, 11, 12};
488   SmallVector<uint32_t, 6> result = {10, 11, 12, 0};
489 
490   EXPECT_EQ(vec.size(), 1);
491   auto ret =
492       vec.insert(vec.begin(), insert_values.begin(), insert_values.end());
493   EXPECT_EQ(vec.size(), 4);
494   EXPECT_EQ(vec, result);
495   EXPECT_EQ(*ret, 10);
496 }
497 
TEST(SmallVectorTest,Insert5)498 TEST(SmallVectorTest, Insert5) {
499   SmallVector<uint32_t, 2> vec = {0, 1, 2};
500   SmallVector<uint32_t, 2> insert_values = {10, 11, 12};
501   SmallVector<uint32_t, 2> result = {0, 1, 2, 10, 11, 12};
502 
503   EXPECT_EQ(vec.size(), 3);
504   auto ret = vec.insert(vec.end(), insert_values.begin(), insert_values.end());
505   EXPECT_EQ(vec.size(), 6);
506   EXPECT_EQ(vec, result);
507   EXPECT_EQ(*ret, 10);
508 }
509 
TEST(SmallVectorTest,Insert6)510 TEST(SmallVectorTest, Insert6) {
511   SmallVector<uint32_t, 6> vec = {0, 1, 2};
512   SmallVector<uint32_t, 6> insert_values = {10, 11, 12};
513   SmallVector<uint32_t, 6> result = {0, 1, 2, 10, 11, 12};
514 
515   EXPECT_EQ(vec.size(), 3);
516   auto ret = vec.insert(vec.end(), insert_values.begin(), insert_values.end());
517   EXPECT_EQ(vec.size(), 6);
518   EXPECT_EQ(vec, result);
519   EXPECT_EQ(*ret, 10);
520 }
521 
TEST(SmallVectorTest,Insert7)522 TEST(SmallVectorTest, Insert7) {
523   SmallVector<uint32_t, 2> vec = {0, 1, 2};
524   SmallVector<uint32_t, 2> insert_values = {10, 11, 12};
525   SmallVector<uint32_t, 2> result = {0, 10, 11, 12, 1, 2};
526 
527   EXPECT_EQ(vec.size(), 3);
528   auto ret =
529       vec.insert(vec.begin() + 1, insert_values.begin(), insert_values.end());
530   EXPECT_EQ(vec.size(), 6);
531   EXPECT_EQ(vec, result);
532   EXPECT_EQ(*ret, 10);
533 }
534 
TEST(SmallVectorTest,Insert8)535 TEST(SmallVectorTest, Insert8) {
536   SmallVector<uint32_t, 6> vec = {0, 1, 2};
537   SmallVector<uint32_t, 6> insert_values = {10, 11, 12};
538   SmallVector<uint32_t, 6> result = {0, 10, 11, 12, 1, 2};
539 
540   EXPECT_EQ(vec.size(), 3);
541   auto ret =
542       vec.insert(vec.begin() + 1, insert_values.begin(), insert_values.end());
543   EXPECT_EQ(vec.size(), 6);
544   EXPECT_EQ(vec, result);
545   EXPECT_EQ(*ret, 10);
546 }
547 
TEST(SmallVectorTest,Resize1)548 TEST(SmallVectorTest, Resize1) {
549   SmallVector<uint32_t, 2> vec = {0, 1, 2};
550   SmallVector<uint32_t, 2> result = {0, 1, 2, 10, 10, 10};
551 
552   EXPECT_EQ(vec.size(), 3);
553   vec.resize(6, 10);
554   EXPECT_EQ(vec.size(), 6);
555   EXPECT_EQ(vec, result);
556 }
557 
TEST(SmallVectorTest,Resize2)558 TEST(SmallVectorTest, Resize2) {
559   SmallVector<uint32_t, 8> vec = {0, 1, 2};
560   SmallVector<uint32_t, 8> result = {0, 1, 2, 10, 10, 10};
561 
562   EXPECT_EQ(vec.size(), 3);
563   vec.resize(6, 10);
564   EXPECT_EQ(vec.size(), 6);
565   EXPECT_EQ(vec, result);
566 }
567 
TEST(SmallVectorTest,Resize3)568 TEST(SmallVectorTest, Resize3) {
569   SmallVector<uint32_t, 4> vec = {0, 1, 2};
570   SmallVector<uint32_t, 4> result = {0, 1, 2, 10, 10, 10};
571 
572   EXPECT_EQ(vec.size(), 3);
573   vec.resize(6, 10);
574   EXPECT_EQ(vec.size(), 6);
575   EXPECT_EQ(vec, result);
576 }
577 
TEST(SmallVectorTest,Resize4)578 TEST(SmallVectorTest, Resize4) {
579   SmallVector<uint32_t, 4> vec = {0, 1, 2, 10, 10, 10};
580   SmallVector<uint32_t, 4> result = {0, 1, 2};
581 
582   EXPECT_EQ(vec.size(), 6);
583   vec.resize(3, 10);
584   EXPECT_EQ(vec.size(), 3);
585   EXPECT_EQ(vec, result);
586 }
587 
TEST(SmallVectorTest,Resize5)588 TEST(SmallVectorTest, Resize5) {
589   SmallVector<uint32_t, 2> vec = {0, 1, 2, 10, 10, 10};
590   SmallVector<uint32_t, 2> result = {0, 1, 2};
591 
592   EXPECT_EQ(vec.size(), 6);
593   vec.resize(3, 10);
594   EXPECT_EQ(vec.size(), 3);
595   EXPECT_EQ(vec, result);
596 }
597 
TEST(SmallVectorTest,Resize6)598 TEST(SmallVectorTest, Resize6) {
599   SmallVector<uint32_t, 8> vec = {0, 1, 2, 10, 10, 10};
600   SmallVector<uint32_t, 8> result = {0, 1, 2};
601 
602   EXPECT_EQ(vec.size(), 6);
603   vec.resize(3, 10);
604   EXPECT_EQ(vec.size(), 3);
605   EXPECT_EQ(vec, result);
606 }
607 
TEST(SmallVectorTest,Pop_back)608 TEST(SmallVectorTest, Pop_back) {
609   SmallVector<uint32_t, 8> vec = {0, 1, 2, 10, 10, 10};
610   SmallVector<uint32_t, 8> result = {0, 1, 2};
611 
612   EXPECT_EQ(vec.size(), 6);
613   vec.pop_back();
614   vec.pop_back();
615   vec.pop_back();
616   EXPECT_EQ(vec.size(), 3);
617   EXPECT_EQ(vec, result);
618 }
619 
TEST(SmallVectorTest,Pop_back_TestDestructor)620 TEST(SmallVectorTest, Pop_back_TestDestructor) {
621   // Tracks number of constructions and destructions to ensure they are called.
622   struct TracksDtor {
623     TracksDtor& operator=(TracksDtor&&) = delete;
624     TracksDtor& operator=(const TracksDtor&) = delete;
625 
626     TracksDtor(int& num_ctors, int& num_dtors)
627         : num_ctors_(num_ctors), num_dtors_(num_dtors) {
628       num_ctors_++;
629     }
630     TracksDtor(const TracksDtor& that)
631         : TracksDtor(that.num_ctors_, that.num_dtors_) {}
632     TracksDtor(TracksDtor&& that)
633         : TracksDtor(that.num_ctors_, that.num_dtors_) {}
634     ~TracksDtor() { num_dtors_++; }
635 
636     int& num_ctors_;
637     int& num_dtors_;
638   };
639 
640   constexpr int capacity = 4;
641   SmallVector<TracksDtor, capacity> vec;
642 
643   int num_ctors = 0;
644   int num_dtors = 0;
645 
646   // Make sure it works when staying within the smallvector capacity
647   for (int i = 0; i < capacity; ++i) {
648     vec.emplace_back(num_ctors, num_dtors);
649   }
650 
651   EXPECT_EQ(num_ctors, capacity);
652   while (!vec.empty()) {
653     vec.pop_back();
654   }
655 
656   EXPECT_EQ(num_ctors, capacity);
657   EXPECT_EQ(num_dtors, num_ctors);
658 
659   // And when larger than builtin capacity
660   for (int i = 0; i < capacity * 2; ++i) {
661     vec.emplace_back(num_ctors, num_dtors);
662   }
663 
664   while (!vec.empty()) {
665     vec.pop_back();
666   }
667   EXPECT_EQ(num_dtors, num_ctors);
668 }
669 
670 }  // namespace
671 }  // namespace utils
672 }  // namespace spvtools
673