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/static_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::StaticVector;
30
31 // Keep in sync with example usage in header file.
TEST(StaticVector,Example)32 TEST(StaticVector, Example) {
33 ftl::StaticVector<char, 3> vector;
34 EXPECT_TRUE(vector.empty());
35
36 vector = {'a', 'b'};
37 EXPECT_EQ(vector.size(), 2u);
38
39 vector.push_back('c');
40 EXPECT_TRUE(vector.full());
41
42 EXPECT_FALSE(vector.push_back('d'));
43 EXPECT_EQ(vector.size(), 3u);
44
45 vector.unstable_erase(vector.begin());
46 EXPECT_EQ(vector, (ftl::StaticVector{'c', 'b'}));
47
48 vector.pop_back();
49 EXPECT_EQ(vector.back(), 'c');
50
51 const char array[] = "hi";
52 vector = ftl::StaticVector(array);
53 EXPECT_EQ(vector, (ftl::StaticVector{'h', 'i', '\0'}));
54
55 ftl::StaticVector strings = ftl::init::list<std::string>("abc")("123456", 3u)(3u, '?');
56 ASSERT_EQ(strings.size(), 3u);
57
58 EXPECT_EQ(strings[0], "abc");
59 EXPECT_EQ(strings[1], "123");
60 EXPECT_EQ(strings[2], "???");
61 }
62
TEST(StaticVector,Construct)63 TEST(StaticVector, Construct) {
64 {
65 // Default constructor.
66 StaticVector<std::string, 2> vector;
67 EXPECT_TRUE(vector.empty());
68 }
69 {
70 // Array constructor.
71 const float floats[] = {.1f, .2f, .3f};
72 StaticVector vector(floats);
73 EXPECT_EQ(vector, (StaticVector{.1f, .2f, .3f}));
74 }
75 {
76 // Iterator constructor.
77 const char chars[] = "abcdef";
78 std::string string(chars);
79 StaticVector<char, sizeof(chars)> vector(string.begin(), string.end());
80
81 EXPECT_STREQ(vector.begin(), chars);
82 }
83 {
84 // Variadic constructor with same types.
85 StaticVector vector = {1, 2, 3};
86
87 static_assert(std::is_same_v<decltype(vector), StaticVector<int, 3>>);
88 EXPECT_EQ(vector, (StaticVector{1, 2, 3}));
89 }
90 {
91 // Variadic constructor with different types.
92 const auto copy = "quince"s;
93 auto move = "tart"s;
94 StaticVector vector = {copy, std::move(move)};
95
96 static_assert(std::is_same_v<decltype(vector), StaticVector<std::string, 2>>);
97 EXPECT_EQ(vector, (StaticVector{"quince"s, "tart"s}));
98 }
99 {
100 // In-place constructor with same types.
101 StaticVector vector =
102 ftl::init::list<std::string>("redolent", 3u)("velveteen", 6u)("cakewalk", 4u);
103
104 static_assert(std::is_same_v<decltype(vector), StaticVector<std::string, 3>>);
105 EXPECT_EQ(vector, (StaticVector{"red"s, "velvet"s, "cake"s}));
106 }
107 {
108 // In-place constructor with different types.
109 const auto copy = "red"s;
110 auto move = "velvet"s;
111 std::initializer_list<char> list = {'c', 'a', 'k', 'e'};
112 StaticVector vector = ftl::init::list<std::string>(copy.c_str())(std::move(move))(list);
113
114 static_assert(std::is_same_v<decltype(vector), StaticVector<std::string, 3>>);
115 EXPECT_TRUE(move.empty());
116 EXPECT_EQ(vector, (StaticVector{"red"s, "velvet"s, "cake"s}));
117 }
118 {
119 struct String {
120 explicit String(const char* str) : str(str) {}
121 explicit String(const char** ptr) : str(*ptr) {}
122 const char* str;
123 };
124
125 const char* strings[] = {"a", "b", "c", "d"};
126
127 {
128 // Two iterator-like elements.
129 StaticVector<String, 3> vector(strings, strings + 3);
130 ASSERT_EQ(vector.size(), 2u);
131
132 EXPECT_STREQ(vector[0].str, "a");
133 EXPECT_STREQ(vector[1].str, "d");
134 }
135 {
136 // Disambiguating iterator constructor.
137 StaticVector<String, 3> vector(ftl::kIteratorRange, strings, strings + 3);
138 ASSERT_EQ(vector.size(), 3u);
139
140 EXPECT_STREQ(vector[0].str, "a");
141 EXPECT_STREQ(vector[1].str, "b");
142 EXPECT_STREQ(vector[2].str, "c");
143 }
144 }
145 }
146
TEST(StaticVector,String)147 TEST(StaticVector, String) {
148 StaticVector<char, 10> chars;
149 char c = 'a';
150 std::generate_n(std::back_inserter(chars), chars.max_size(), [&c] { return c++; });
151 chars.back() = '\0';
152
153 EXPECT_STREQ(chars.begin(), "abcdefghi");
154
155 // Constructor takes iterator range.
156 const char numbers[] = "123456";
157 StaticVector<char, 10> string(std::begin(numbers), std::end(numbers));
158
159 EXPECT_STREQ(string.begin(), "123456");
160 EXPECT_EQ(string.size(), 7u);
161
162 // Similar to emplace, but replaces rather than inserts.
163 string.replace(string.begin() + 5, '\0');
164 EXPECT_STREQ(string.begin(), "12345");
165
166 swap(chars, string);
167
168 EXPECT_STREQ(chars.begin(), "12345");
169 EXPECT_STREQ(string.begin(), "abcdefghi");
170 }
171
TEST(StaticVector,CopyableElement)172 TEST(StaticVector, CopyableElement) {
173 struct Pair {
174 const int a, b;
175 bool operator==(Pair p) const { return p.a == a && p.b == b; }
176 };
177
178 StaticVector<Pair, 5> pairs;
179
180 EXPECT_TRUE(pairs.empty());
181 EXPECT_EQ(pairs.max_size(), 5u);
182
183 for (size_t i = 0; i < pairs.max_size(); ++i) {
184 EXPECT_EQ(pairs.size(), i);
185
186 const int a = static_cast<int>(i) * 2;
187 const auto it = pairs.emplace_back(a, a + 1);
188 ASSERT_NE(it, pairs.end());
189 EXPECT_EQ(*it, (Pair{a, a + 1}));
190 }
191
192 EXPECT_TRUE(pairs.full());
193 EXPECT_EQ(pairs.size(), 5u);
194
195 // Insertion fails if the vector is full.
196 const auto it = pairs.emplace_back(10, 11);
197 EXPECT_EQ(it, pairs.end());
198
199 EXPECT_EQ(pairs, (StaticVector{Pair{0, 1}, Pair{2, 3}, Pair{4, 5}, Pair{6, 7}, Pair{8, 9}}));
200
201 // Constructor takes at most N elements.
202 StaticVector<int, 6> sums = {0, 0, 0, 0, 0, -1};
203 EXPECT_TRUE(sums.full());
204
205 // Random-access iterators comply with standard.
206 std::transform(pairs.begin(), pairs.end(), sums.begin(), [](Pair p) { return p.a + p.b; });
207 EXPECT_EQ(sums, (StaticVector{1, 5, 9, 13, 17, -1}));
208
209 sums.pop_back();
210 std::reverse(sums.begin(), sums.end());
211
212 EXPECT_EQ(sums, (StaticVector{17, 13, 9, 5, 1}));
213 }
214
TEST(StaticVector,MovableElement)215 TEST(StaticVector, MovableElement) {
216 // Construct std::string elements in place from per-element arguments.
217 StaticVector strings = ftl::init::list<std::string>()()()("cake")("velvet")("red")();
218 strings.pop_back();
219
220 EXPECT_EQ(strings.max_size(), 7u);
221 EXPECT_EQ(strings.size(), 6u);
222
223 // Erase "cake" and append a substring copy.
224 {
225 auto it =
226 std::find_if(strings.begin(), strings.end(), [](const auto& s) { return !s.empty(); });
227 ASSERT_FALSE(it == strings.end());
228 EXPECT_EQ(*it, "cake");
229
230 strings.unstable_erase(it);
231
232 // Construct std::string from first 4 characters of string literal.
233 it = strings.emplace_back("cakewalk", 4u);
234 ASSERT_NE(it, strings.end());
235 EXPECT_EQ(*it, "cake"s);
236 }
237
238 strings[1] = "quince"s;
239
240 // Replace last empty string with "tart".
241 {
242 const auto rit = std::find(strings.rbegin(), strings.rend(), std::string());
243 ASSERT_FALSE(rit == strings.rend());
244
245 std::initializer_list<char> list = {'t', 'a', 'r', 't'};
246 strings.replace(rit.base() - 1, list);
247 }
248
249 strings.front().assign("pie");
250
251 EXPECT_EQ(strings, (StaticVector{"pie"s, "quince"s, "tart"s, "red"s, "velvet"s, "cake"s}));
252 }
253
TEST(StaticVector,Replace)254 TEST(StaticVector, Replace) {
255 // Replacing does not require a copy/move assignment operator.
256 struct Word {
257 explicit Word(std::string str) : str(std::move(str)) {}
258 const std::string str;
259 };
260
261 StaticVector words = ftl::init::list<Word>("red")("velour")("cake");
262
263 // The replaced element can be referenced by the replacement.
264 const auto it = words.begin() + 1;
265 const Word& word = words.replace(it, it->str.substr(0, 3) + "vet");
266 EXPECT_EQ(word.str, "velvet");
267 }
268
TEST(StaticVector,ReverseTruncate)269 TEST(StaticVector, ReverseTruncate) {
270 StaticVector<std::string, 10> strings("pie", "quince", "tart", "red", "velvet", "cake");
271 EXPECT_FALSE(strings.full());
272
273 for (auto it = strings.begin(); it != strings.end(); ++it) {
274 strings.replace(it, strings.back());
275 strings.pop_back();
276 }
277
278 EXPECT_EQ(strings, (StaticVector{"cake"s, "velvet"s, "red"s}));
279 }
280
TEST(StaticVector,Sort)281 TEST(StaticVector, Sort) {
282 StaticVector<std::string, 7> strings("pie", "quince", "tart", "red", "velvet", "cake");
283 EXPECT_FALSE(strings.full());
284
285 auto sorted = std::move(strings);
286 EXPECT_TRUE(strings.empty());
287
288 std::sort(sorted.begin(), sorted.end());
289 EXPECT_EQ(sorted, (StaticVector{"cake"s, "pie"s, "quince"s, "red"s, "tart"s, "velvet"s}));
290
291 // Constructor takes array reference.
292 {
293 const char* array[] = {"cake", "lie"};
294 strings = StaticVector(array);
295 }
296
297 EXPECT_GT(sorted, strings);
298 swap(sorted, strings);
299 EXPECT_LT(sorted, strings);
300
301 // Append remaining elements, such that "pie" is the only difference.
302 for (const char* str : {"quince", "red", "tart", "velvet"}) {
303 sorted.emplace_back(str);
304 }
305
306 EXPECT_NE(sorted, strings);
307
308 // Replace second element with "pie".
309 const auto it = sorted.begin() + 1;
310 EXPECT_EQ(sorted.replace(it, 'p' + it->substr(1)), "pie");
311
312 EXPECT_EQ(sorted, strings);
313 }
314
315 namespace {
316
317 struct DestroyCounts {
DestroyCountsandroid::test::__anon7e315b240411::DestroyCounts318 DestroyCounts(int& live, int& dead) : counts{live, dead} {}
DestroyCountsandroid::test::__anon7e315b240411::DestroyCounts319 DestroyCounts(const DestroyCounts& other) : counts(other.counts) {}
DestroyCountsandroid::test::__anon7e315b240411::DestroyCounts320 DestroyCounts(DestroyCounts&& other) : counts(other.counts) { other.alive = false; }
~DestroyCountsandroid::test::__anon7e315b240411::DestroyCounts321 ~DestroyCounts() { ++(alive ? counts.live : counts.dead); }
322
323 struct {
324 int& live;
325 int& dead;
326 } counts;
327
328 bool alive = true;
329 };
330
swap(DestroyCounts & lhs,DestroyCounts & rhs)331 void swap(DestroyCounts& lhs, DestroyCounts& rhs) {
332 std::swap(lhs.alive, rhs.alive);
333 }
334
335 } // namespace
336
TEST(StaticVector,Destroy)337 TEST(StaticVector, Destroy) {
338 int live = 0;
339 int dead = 0;
340
341 { StaticVector<DestroyCounts, 5> counts; }
342 EXPECT_EQ(0, live);
343 EXPECT_EQ(0, dead);
344
345 {
346 StaticVector<DestroyCounts, 5> counts;
347 counts.emplace_back(live, dead);
348 counts.emplace_back(live, dead);
349 counts.emplace_back(live, dead);
350 }
351 EXPECT_EQ(3, live);
352 EXPECT_EQ(0, dead);
353
354 live = 0;
355 {
356 StaticVector<DestroyCounts, 5> counts;
357 counts.emplace_back(live, dead);
358 counts.emplace_back(live, dead);
359 counts.emplace_back(live, dead);
360
361 auto copy = counts;
362 }
363 EXPECT_EQ(6, live);
364 EXPECT_EQ(0, dead);
365
366 live = 0;
367 {
368 StaticVector<DestroyCounts, 5> counts;
369 counts.emplace_back(live, dead);
370 counts.emplace_back(live, dead);
371 counts.emplace_back(live, dead);
372
373 auto move = std::move(counts);
374 }
375 EXPECT_EQ(3, live);
376 EXPECT_EQ(3, dead);
377
378 live = dead = 0;
379 {
380 StaticVector<DestroyCounts, 5> counts1;
381 counts1.emplace_back(live, dead);
382 counts1.emplace_back(live, dead);
383 counts1.emplace_back(live, dead);
384
385 StaticVector<DestroyCounts, 5> counts2;
386 counts2.emplace_back(live, dead);
387
388 swap(counts1, counts2);
389
390 EXPECT_EQ(0, live);
391 EXPECT_EQ(2, dead);
392
393 dead = 0;
394 }
395 EXPECT_EQ(4, live);
396 EXPECT_EQ(0, dead);
397 }
398
399 } // namespace android::test
400