1 /*
2 * Copyright 2020 The Android Open Source Project
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
17 #include <ftl/small_vector.h>
18 #include <gtest/gtest.h>
19
20 #include <algorithm>
21 #include <iterator>
22 #include <string>
23 #include <utility>
24
25 using namespace std::string_literals;
26
27 namespace android::test {
28
29 using ftl::SmallVector;
30
31 // Keep in sync with example usage in header file.
TEST(SmallVector,Example)32 TEST(SmallVector, Example) {
33 ftl::SmallVector<char, 3> vector;
34 EXPECT_TRUE(vector.empty());
35 EXPECT_FALSE(vector.dynamic());
36
37 vector = {'a', 'b', 'c'};
38 EXPECT_EQ(vector.size(), 3u);
39 EXPECT_FALSE(vector.dynamic());
40
41 vector.push_back('d');
42 EXPECT_TRUE(vector.dynamic());
43
44 vector.unstable_erase(vector.begin());
45 EXPECT_EQ(vector, (ftl::SmallVector{'d', 'b', 'c'}));
46
47 vector.pop_back();
48 EXPECT_EQ(vector.back(), 'b');
49 EXPECT_TRUE(vector.dynamic());
50
51 const char array[] = "hi";
52 vector = ftl::SmallVector(array);
53 EXPECT_EQ(vector, (ftl::SmallVector{'h', 'i', '\0'}));
54 EXPECT_FALSE(vector.dynamic());
55
56 ftl::SmallVector strings = ftl::init::list<std::string>("abc")("123456", 3u)(3u, '?');
57 ASSERT_EQ(strings.size(), 3u);
58 EXPECT_FALSE(strings.dynamic());
59
60 EXPECT_EQ(strings[0], "abc");
61 EXPECT_EQ(strings[1], "123");
62 EXPECT_EQ(strings[2], "???");
63 }
64
TEST(SmallVector,Construct)65 TEST(SmallVector, Construct) {
66 {
67 // Default constructor.
68 SmallVector<std::string, 2> vector;
69
70 EXPECT_TRUE(vector.empty());
71 EXPECT_FALSE(vector.dynamic());
72 }
73 {
74 // Array constructor.
75 const float floats[] = {.1f, .2f, .3f};
76 SmallVector vector(floats);
77
78 EXPECT_EQ(vector, (SmallVector{.1f, .2f, .3f}));
79 EXPECT_FALSE(vector.dynamic());
80 }
81 {
82 // Iterator constructor.
83 const char chars[] = "abcdef";
84 std::string string(chars);
85 SmallVector<char, sizeof(chars)> vector(string.begin(), string.end());
86
87 EXPECT_STREQ(vector.begin(), chars);
88 EXPECT_FALSE(vector.dynamic());
89 }
90 {
91 // Variadic constructor with same types.
92 SmallVector vector = {1, 2, 3};
93
94 static_assert(std::is_same_v<decltype(vector), SmallVector<int, 3>>);
95 EXPECT_EQ(vector, (SmallVector{1, 2, 3}));
96 EXPECT_FALSE(vector.dynamic());
97 }
98 {
99 // Variadic constructor with different types.
100 const auto copy = "quince"s;
101 auto move = "tart"s;
102 SmallVector vector = {copy, std::move(move)};
103
104 static_assert(std::is_same_v<decltype(vector), SmallVector<std::string, 2>>);
105 EXPECT_EQ(vector, (SmallVector{"quince"s, "tart"s}));
106 EXPECT_FALSE(vector.dynamic());
107 }
108 {
109 // In-place constructor with same types.
110 SmallVector vector =
111 ftl::init::list<std::string>("redolent", 3u)("velveteen", 6u)("cakewalk", 4u);
112
113 static_assert(std::is_same_v<decltype(vector), SmallVector<std::string, 3>>);
114 EXPECT_EQ(vector, (SmallVector{"red"s, "velvet"s, "cake"s}));
115 EXPECT_FALSE(vector.dynamic());
116 }
117 {
118 // In-place constructor with different types.
119 const auto copy = "red"s;
120 auto move = "velvet"s;
121 std::initializer_list<char> list = {'c', 'a', 'k', 'e'};
122 SmallVector vector = ftl::init::list<std::string>(copy.c_str())(std::move(move))(list);
123
124 static_assert(std::is_same_v<decltype(vector), SmallVector<std::string, 3>>);
125 EXPECT_TRUE(move.empty());
126 EXPECT_EQ(vector, (SmallVector{"red"s, "velvet"s, "cake"s}));
127 EXPECT_FALSE(vector.dynamic());
128 }
129 {
130 // Conversion from StaticVector.
131 ftl::StaticVector doubles = {.1, .2, .3};
132 SmallVector vector = std::move(doubles);
133 EXPECT_TRUE(doubles.empty());
134
135 static_assert(std::is_same_v<decltype(vector), SmallVector<double, 3>>);
136 EXPECT_EQ(vector, (SmallVector{.1, .2, .3}));
137 EXPECT_FALSE(vector.dynamic());
138 }
139 }
140
TEST(SmallVector,Copy)141 TEST(SmallVector, Copy) {
142 {
143 // Same capacity.
144 const SmallVector vector = {"snow"s, "cone"s};
145
146 SmallVector<const std::string, 2> copy(vector);
147 EXPECT_EQ(copy, vector);
148
149 // The vector is assignable even if T is const.
150 const SmallVector<std::string, 2> other = {"tiramisu"s};
151 copy = other;
152 EXPECT_EQ(copy, other);
153 }
154 {
155 // From smaller capacity.
156 const SmallVector vector = {"snow"s, "cone"s};
157
158 SmallVector<const std::string, 3> copy(vector);
159 EXPECT_EQ(copy, vector);
160
161 // The vector is assignable even if T is const.
162 const SmallVector other = {"tiramisu"s};
163 copy = other;
164 EXPECT_EQ(copy, other);
165 }
166 {
167 // To zero capacity.
168 const SmallVector vector = {"snow"s, "cone"s};
169
170 SmallVector<const std::string, 0> copy(vector);
171 EXPECT_EQ(copy, vector);
172
173 // The vector is assignable even if T is const.
174 const SmallVector other = {"tiramisu"s};
175 copy = other;
176 EXPECT_EQ(copy, other);
177 }
178 {
179 // From/to zero capacity.
180 const SmallVector<std::string, 0> vector = {"snow"s, "cone"s};
181
182 SmallVector<const std::string, 0> copy(vector);
183 EXPECT_EQ(copy, vector);
184
185 // The vector is assignable even if T is const.
186 const SmallVector<std::string, 0> other = {"tiramisu"s};
187 copy = other;
188 EXPECT_EQ(copy, other);
189 }
190 }
191
TEST(SmallVector,Move)192 TEST(SmallVector, Move) {
193 {
194 // Same capacity.
195 SmallVector vector = {"snow"s, "cone"s};
196
197 SmallVector<const std::string, 2> move(std::move(vector));
198 EXPECT_TRUE(vector.empty());
199 EXPECT_EQ(move, (SmallVector{"snow"s, "cone"s}));
200
201 // The vector is assignable even if T is const.
202 SmallVector<std::string, 2> other = {"tiramisu"s};
203 move = std::move(other);
204 EXPECT_TRUE(other.empty());
205 EXPECT_EQ(move, (SmallVector{"tiramisu"s}));
206 }
207 {
208 // From smaller capacity.
209 SmallVector vector = {"snow"s, "cone"s};
210
211 SmallVector<const std::string, 3> move(std::move(vector));
212 EXPECT_TRUE(vector.empty());
213 EXPECT_EQ(move, (SmallVector{"snow"s, "cone"s}));
214
215 // The vector is assignable even if T is const.
216 SmallVector other = {"tiramisu"s};
217 move = std::move(other);
218 EXPECT_TRUE(other.empty());
219 EXPECT_EQ(move, (SmallVector{"tiramisu"s}));
220 }
221 {
222 // To zero capacity.
223 SmallVector vector = {"snow"s, "cone"s};
224
225 SmallVector<const std::string, 0> move(std::move(vector));
226 EXPECT_TRUE(vector.empty());
227 EXPECT_EQ(move, (SmallVector{"snow"s, "cone"s}));
228
229 // The vector is assignable even if T is const.
230 SmallVector other = {"tiramisu"s};
231 move = std::move(other);
232 EXPECT_TRUE(other.empty());
233 EXPECT_EQ(move, (SmallVector{"tiramisu"s}));
234 }
235 {
236 // From/to zero capacity.
237 SmallVector<std::string, 0> vector = {"snow"s, "cone"s};
238
239 SmallVector<const std::string, 0> move(std::move(vector));
240 EXPECT_TRUE(vector.empty());
241 EXPECT_EQ(move, (SmallVector{"snow"s, "cone"s}));
242
243 // The vector is assignable even if T is const.
244 SmallVector<std::string, 0> other = {"tiramisu"s};
245 move = std::move(other);
246 EXPECT_TRUE(other.empty());
247 EXPECT_EQ(move, (SmallVector{"tiramisu"s}));
248 }
249 }
250
TEST(SmallVector,String)251 TEST(SmallVector, String) {
252 SmallVector<char, 10> chars;
253 char c = 'a';
254 std::generate_n(std::back_inserter(chars), chars.max_size(), [&c] { return c++; });
255 chars.push_back('\0');
256
257 EXPECT_TRUE(chars.dynamic());
258 EXPECT_EQ(chars.size(), 11u);
259 EXPECT_STREQ(chars.begin(), "abcdefghij");
260
261 // Constructor takes iterator range.
262 const char numbers[] = "123456";
263 SmallVector<char, 10> string(std::begin(numbers), std::end(numbers));
264
265 EXPECT_FALSE(string.dynamic());
266 EXPECT_STREQ(string.begin(), "123456");
267 EXPECT_EQ(string.size(), 7u);
268
269 // Similar to emplace, but replaces rather than inserts.
270 string.replace(string.begin() + 5, '\0');
271 EXPECT_STREQ(string.begin(), "12345");
272
273 swap(chars, string);
274
275 EXPECT_STREQ(chars.begin(), "12345");
276 EXPECT_STREQ(string.begin(), "abcdefghij");
277
278 EXPECT_FALSE(chars.dynamic());
279 EXPECT_TRUE(string.dynamic());
280 }
281
TEST(SmallVector,CopyableElement)282 TEST(SmallVector, CopyableElement) {
283 struct Pair {
284 // Needed because std::vector does not use list initialization to emplace.
285 Pair(int a, int b) : a(a), b(b) {}
286
287 const int a, b;
288 bool operator==(Pair p) const { return p.a == a && p.b == b; }
289 };
290
291 SmallVector<Pair, 5> pairs;
292
293 EXPECT_TRUE(pairs.empty());
294 EXPECT_EQ(pairs.max_size(), 5u);
295
296 for (size_t i = 0; i < pairs.max_size(); ++i) {
297 EXPECT_EQ(pairs.size(), i);
298
299 const int a = static_cast<int>(i) * 2;
300 EXPECT_EQ(pairs.emplace_back(a, a + 1), Pair(a, a + 1));
301 }
302
303 EXPECT_EQ(pairs.size(), 5u);
304 EXPECT_FALSE(pairs.dynamic());
305
306 // The vector is promoted when full.
307 EXPECT_EQ(pairs.emplace_back(10, 11), Pair(10, 11));
308 EXPECT_TRUE(pairs.dynamic());
309
310 EXPECT_EQ(pairs, (SmallVector{Pair{0, 1}, Pair{2, 3}, Pair{4, 5}, Pair{6, 7}, Pair{8, 9},
311 Pair{10, 11}}));
312
313 // Constructor takes at most N elements.
314 SmallVector<int, 6> sums = {0, 0, 0, 0, 0, 0};
315 EXPECT_FALSE(sums.dynamic());
316
317 // Random-access iterators comply with standard.
318 std::transform(pairs.begin(), pairs.end(), sums.begin(), [](Pair p) { return p.a + p.b; });
319 EXPECT_EQ(sums, (SmallVector{1, 5, 9, 13, 17, 21}));
320
321 sums.pop_back();
322 std::reverse(sums.begin(), sums.end());
323
324 EXPECT_EQ(sums, (SmallVector{17, 13, 9, 5, 1}));
325 }
326
TEST(SmallVector,MovableElement)327 TEST(SmallVector, MovableElement) {
328 // Construct std::string elements in place from per-element arguments.
329 SmallVector strings = ftl::init::list<std::string>()()()("cake")("velvet")("red")();
330 strings.pop_back();
331
332 EXPECT_EQ(strings.max_size(), 7u);
333 EXPECT_EQ(strings.size(), 6u);
334
335 // Erase "cake" and append a substring copy.
336 {
337 const auto it =
338 std::find_if(strings.begin(), strings.end(), [](const auto& s) { return !s.empty(); });
339 ASSERT_FALSE(it == strings.end());
340 EXPECT_EQ(*it, "cake");
341
342 // Construct std::string from first 4 characters of string literal.
343 strings.unstable_erase(it);
344 EXPECT_EQ(strings.emplace_back("cakewalk", 4u), "cake"s);
345 }
346
347 strings[1] = "quince"s;
348
349 // Replace last empty string with "tart".
350 {
351 const auto rit = std::find(strings.rbegin(), strings.rend(), std::string());
352 ASSERT_FALSE(rit == strings.rend());
353
354 std::initializer_list<char> list = {'t', 'a', 'r', 't'};
355 strings.replace(rit.base() - 1, list);
356 }
357
358 strings.front().assign("pie");
359
360 EXPECT_EQ(strings, (SmallVector{"pie"s, "quince"s, "tart"s, "red"s, "velvet"s, "cake"s}));
361
362 strings.push_back("nougat");
363 strings.push_back("oreo");
364 EXPECT_TRUE(strings.dynamic());
365
366 std::rotate(strings.begin(), strings.end() - 2, strings.end());
367
368 EXPECT_EQ(strings, (SmallVector{"nougat"s, "oreo"s, "pie"s, "quince"s, "tart"s, "red"s, "velvet"s,
369 "cake"s}));
370 }
371
TEST(SmallVector,Replace)372 TEST(SmallVector, Replace) {
373 // Replacing does not require a copy/move assignment operator.
374 struct Word {
375 explicit Word(std::string str) : str(std::move(str)) {}
376 const std::string str;
377
378 bool operator==(const Word& other) const { return other.str == str; }
379 };
380
381 SmallVector words = ftl::init::list<Word>("colored")("velour");
382
383 // The replaced element can be referenced by the replacement.
384 {
385 const Word& word = words.replace(words.last(), words.back().str.substr(0, 3) + "vet");
386 EXPECT_EQ(word, Word("velvet"));
387 }
388
389 // The vector is not promoted if replacing while full.
390 EXPECT_FALSE(words.dynamic());
391
392 words.emplace_back("cake");
393 EXPECT_TRUE(words.dynamic());
394
395 {
396 const Word& word = words.replace(words.begin(), words.front().str.substr(4));
397 EXPECT_EQ(word, Word("red"));
398 }
399
400 EXPECT_EQ(words, (SmallVector{Word("red"), Word("velvet"), Word("cake")}));
401 }
402
TEST(SmallVector,ReverseAppend)403 TEST(SmallVector, ReverseAppend) {
404 SmallVector strings = {"red"s, "velvet"s, "cake"s};
405 EXPECT_FALSE(strings.dynamic());
406
407 auto rit = strings.rbegin();
408 while (rit != strings.rend()) {
409 // Iterator and reference are invalidated on insertion.
410 const auto i = std::distance(strings.begin(), rit.base());
411 std::string s = *rit;
412
413 strings.push_back(std::move(s));
414 rit = std::make_reverse_iterator(strings.begin() + i) + 1;
415 }
416
417 EXPECT_EQ(strings, (SmallVector{"red"s, "velvet"s, "cake"s, "cake"s, "velvet"s, "red"s}));
418 EXPECT_TRUE(strings.dynamic());
419 }
420
TEST(SmallVector,Sort)421 TEST(SmallVector, Sort) {
422 SmallVector strings = ftl::init::list<std::string>("pie")("quince")("tart")("red")("velvet");
423 strings.push_back("cake"s);
424
425 auto sorted = std::move(strings);
426 EXPECT_TRUE(strings.empty());
427
428 EXPECT_TRUE(sorted.dynamic());
429 EXPECT_TRUE(strings.dynamic());
430
431 std::sort(sorted.begin(), sorted.end());
432 EXPECT_EQ(sorted, (SmallVector{"cake"s, "pie"s, "quince"s, "red"s, "tart"s, "velvet"s}));
433
434 // Constructor takes array reference.
435 {
436 const char* array[] = {"cake", "lie"};
437 strings = SmallVector(array);
438 EXPECT_FALSE(strings.dynamic());
439 }
440
441 EXPECT_GT(sorted, strings);
442 swap(sorted, strings);
443 EXPECT_LT(sorted, strings);
444
445 EXPECT_FALSE(sorted.dynamic());
446 EXPECT_TRUE(strings.dynamic());
447
448 // Append remaining elements, such that "pie" is the only difference.
449 for (const char* str : {"quince", "red", "tart", "velvet"}) {
450 sorted.emplace_back(str);
451 }
452 EXPECT_TRUE(sorted.dynamic());
453
454 EXPECT_NE(sorted, strings);
455
456 // Replace second element with "pie".
457 const auto it = sorted.begin() + 1;
458 EXPECT_EQ(sorted.replace(it, 'p' + it->substr(1)), "pie");
459
460 EXPECT_EQ(sorted, strings);
461 }
462
463 namespace {
464
465 struct DestroyCounts {
DestroyCountsandroid::test::__anonaba3364e0411::DestroyCounts466 DestroyCounts(int& live, int& dead) : counts{live, dead} {}
DestroyCountsandroid::test::__anonaba3364e0411::DestroyCounts467 DestroyCounts(const DestroyCounts& other) : counts(other.counts) {}
DestroyCountsandroid::test::__anonaba3364e0411::DestroyCounts468 DestroyCounts(DestroyCounts&& other) : counts(other.counts) { other.alive = false; }
~DestroyCountsandroid::test::__anonaba3364e0411::DestroyCounts469 ~DestroyCounts() { ++(alive ? counts.live : counts.dead); }
470
471 struct {
472 int& live;
473 int& dead;
474 } counts;
475
476 bool alive = true;
477 };
478
479 } // namespace
480
TEST(SmallVector,Destroy)481 TEST(SmallVector, Destroy) {
482 int live = 0;
483 int dead = 0;
484 {
485 // Empty.
486 SmallVector<DestroyCounts, 3> counts;
487 }
488 EXPECT_EQ(0, live);
489 EXPECT_EQ(0, dead);
490
491 {
492 // Static.
493 SmallVector<DestroyCounts, 3> counts;
494 counts.emplace_back(live, dead);
495 counts.emplace_back(live, dead);
496 counts.emplace_back(live, dead);
497
498 EXPECT_FALSE(counts.dynamic());
499 }
500 EXPECT_EQ(3, live);
501 EXPECT_EQ(0, dead);
502
503 live = 0;
504 {
505 // Dynamic.
506 SmallVector<DestroyCounts, 3> counts;
507 counts.emplace_back(live, dead);
508 counts.emplace_back(live, dead);
509 counts.emplace_back(live, dead);
510 counts.emplace_back(live, dead);
511
512 EXPECT_TRUE(counts.dynamic());
513 }
514 EXPECT_EQ(4, live);
515 EXPECT_EQ(3, dead);
516
517 live = dead = 0;
518 {
519 // Copy.
520 SmallVector<DestroyCounts, 2> counts;
521 counts.emplace_back(live, dead);
522 counts.emplace_back(live, dead);
523 counts.emplace_back(live, dead);
524
525 const auto copy = counts;
526 EXPECT_TRUE(copy.dynamic());
527 }
528 EXPECT_EQ(6, live);
529 EXPECT_EQ(2, dead);
530
531 live = dead = 0;
532 {
533 // Move.
534 SmallVector<DestroyCounts, 2> counts;
535 counts.emplace_back(live, dead);
536 counts.emplace_back(live, dead);
537 counts.emplace_back(live, dead);
538
539 const auto move = std::move(counts);
540 EXPECT_TRUE(move.dynamic());
541 }
542 EXPECT_EQ(3, live);
543 EXPECT_EQ(2, dead);
544
545 live = dead = 0;
546 {
547 // Swap.
548 SmallVector<DestroyCounts, 2> counts1;
549 counts1.emplace_back(live, dead);
550 counts1.emplace_back(live, dead);
551 counts1.emplace_back(live, dead);
552
553 EXPECT_TRUE(counts1.dynamic());
554 EXPECT_EQ(2, dead);
555 dead = 0;
556
557 SmallVector<DestroyCounts, 2> counts2;
558 counts2.emplace_back(live, dead);
559
560 EXPECT_FALSE(counts2.dynamic());
561
562 swap(counts1, counts2);
563
564 EXPECT_EQ(1u, counts1.size());
565 EXPECT_FALSE(counts1.dynamic());
566
567 EXPECT_EQ(3u, counts2.size());
568 EXPECT_TRUE(counts2.dynamic());
569
570 EXPECT_EQ(0, live);
571 EXPECT_EQ(1, dead);
572
573 dead = 0;
574 }
575 EXPECT_EQ(4, live);
576 EXPECT_EQ(0, dead);
577 }
578
TEST(SmallVector,Clear)579 TEST(SmallVector, Clear) {
580 int live = 0;
581 int dead = 0;
582
583 SmallVector<DestroyCounts, 2> counts;
584 {
585 // Static.
586 counts.emplace_back(live, dead);
587 counts.emplace_back(live, dead);
588
589 counts.clear();
590
591 EXPECT_TRUE(counts.empty());
592 EXPECT_FALSE(counts.dynamic());
593 }
594 EXPECT_EQ(2, live);
595 EXPECT_EQ(0, dead);
596
597 live = 0;
598 {
599 // Dynamic.
600 counts.emplace_back(live, dead);
601 counts.emplace_back(live, dead);
602 counts.emplace_back(live, dead);
603
604 counts.clear();
605
606 EXPECT_TRUE(counts.empty());
607 EXPECT_TRUE(counts.dynamic());
608 }
609 EXPECT_EQ(3, live);
610 EXPECT_EQ(2, dead);
611 }
612
TEST(SmallVector,Promote)613 TEST(SmallVector, Promote) {
614 {
615 const std::vector vector = {"snow"s, "cone"s};
616 EXPECT_EQ(vector, SmallVector("snow"s, "cone"s).promote());
617 EXPECT_EQ(vector, (SmallVector<std::string, 0>({"snow"s, "cone"s}).promote()));
618 }
619
620 int live = 0;
621 int dead = 0;
622
623 std::vector<DestroyCounts> vector;
624 {
625 // Static.
626 SmallVector<DestroyCounts, 3> counts;
627 counts.emplace_back(live, dead);
628 counts.emplace_back(live, dead);
629
630 vector = std::move(counts).promote();
631
632 ASSERT_EQ(2u, vector.size());
633 EXPECT_TRUE(vector[0].alive);
634 EXPECT_TRUE(vector[1].alive);
635 }
636 EXPECT_EQ(0, live);
637 EXPECT_EQ(2, dead);
638
639 vector.clear();
640 live = dead = 0;
641 {
642 // Dynamic.
643 SmallVector<DestroyCounts, 2> counts;
644 counts.emplace_back(live, dead);
645 counts.emplace_back(live, dead);
646 counts.emplace_back(live, dead);
647
648 EXPECT_EQ(2, dead);
649 dead = 0;
650
651 vector = std::move(counts).promote();
652
653 ASSERT_EQ(3u, vector.size());
654 EXPECT_TRUE(vector[0].alive);
655 EXPECT_TRUE(vector[1].alive);
656 EXPECT_TRUE(vector[2].alive);
657 }
658 EXPECT_EQ(0, live);
659 EXPECT_EQ(0, dead);
660 }
661
662 } // namespace android::test
663