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