Dominik Laskowski | 0bacf27 | 2020-10-22 14:08:27 -0700 | [diff] [blame] | 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/SmallVector.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. |
| 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 | |
| 57 | TEST(SmallVector, Construct) { |
| 58 | { |
| 59 | // Default constructor. |
| 60 | SmallVector<std::string, 2> vector; |
| 61 | |
| 62 | EXPECT_TRUE(vector.empty()); |
| 63 | EXPECT_FALSE(vector.dynamic()); |
| 64 | } |
| 65 | { |
| 66 | // Array constructor. |
| 67 | const float kFloats[] = {.1f, .2f, .3f}; |
| 68 | SmallVector vector(kFloats); |
| 69 | |
| 70 | EXPECT_EQ(vector, (SmallVector{.1f, .2f, .3f})); |
| 71 | EXPECT_FALSE(vector.dynamic()); |
| 72 | } |
| 73 | { |
| 74 | // Iterator constructor. |
| 75 | const char chars[] = "abcdef"; |
| 76 | std::string string(chars); |
| 77 | SmallVector<char, sizeof(chars)> vector(string.begin(), string.end()); |
| 78 | |
| 79 | EXPECT_STREQ(vector.begin(), chars); |
| 80 | EXPECT_FALSE(vector.dynamic()); |
| 81 | } |
| 82 | { |
| 83 | // Variadic constructor with same types. |
| 84 | SmallVector vector = {1, 2, 3}; |
| 85 | |
| 86 | static_assert(std::is_same_v<decltype(vector), SmallVector<int, 3>>); |
| 87 | EXPECT_EQ(vector, (SmallVector{1, 2, 3})); |
| 88 | EXPECT_FALSE(vector.dynamic()); |
| 89 | } |
| 90 | { |
| 91 | // Variadic constructor with different types. |
| 92 | const auto copy = "quince"s; |
| 93 | auto move = "tart"s; |
| 94 | SmallVector vector = {copy, std::move(move)}; |
| 95 | |
| 96 | static_assert(std::is_same_v<decltype(vector), SmallVector<std::string, 2>>); |
| 97 | EXPECT_EQ(vector, (SmallVector{"quince"s, "tart"s})); |
| 98 | EXPECT_FALSE(vector.dynamic()); |
| 99 | } |
| 100 | { |
| 101 | // In-place constructor with same types. |
| 102 | SmallVector vector(std::in_place_type<std::string>, "red", "velvet", "cake"); |
| 103 | |
| 104 | static_assert(std::is_same_v<decltype(vector), SmallVector<std::string, 3>>); |
| 105 | EXPECT_EQ(vector, (SmallVector{"red"s, "velvet"s, "cake"s})); |
| 106 | EXPECT_FALSE(vector.dynamic()); |
| 107 | } |
| 108 | { |
| 109 | // In-place constructor with different types. |
| 110 | const auto copy = "red"s; |
| 111 | auto move = "velvet"s; |
| 112 | std::initializer_list<char> list = {'c', 'a', 'k', 'e'}; |
| 113 | SmallVector vector(std::in_place_type<std::string>, copy.c_str(), std::move(move), list); |
| 114 | |
| 115 | static_assert(std::is_same_v<decltype(vector), SmallVector<std::string, 3>>); |
| 116 | EXPECT_EQ(vector, (SmallVector{"red"s, "velvet"s, "cake"s})); |
| 117 | EXPECT_FALSE(vector.dynamic()); |
| 118 | } |
| 119 | { |
| 120 | // Conversion from StaticVector. |
| 121 | ftl::StaticVector doubles = {.1, .2, .3}; |
| 122 | SmallVector vector = std::move(doubles); |
| 123 | EXPECT_TRUE(doubles.empty()); |
| 124 | |
| 125 | static_assert(std::is_same_v<decltype(vector), SmallVector<double, 3>>); |
| 126 | EXPECT_EQ(vector, (SmallVector{.1, .2, .3})); |
| 127 | EXPECT_FALSE(vector.dynamic()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | TEST(SmallVector, String) { |
| 132 | SmallVector<char, 10> chars; |
| 133 | char c = 'a'; |
| 134 | std::generate_n(std::back_inserter(chars), chars.max_size(), [&c] { return c++; }); |
| 135 | chars.push_back('\0'); |
| 136 | |
| 137 | EXPECT_TRUE(chars.dynamic()); |
| 138 | EXPECT_EQ(chars.size(), 11u); |
| 139 | EXPECT_STREQ(chars.begin(), "abcdefghij"); |
| 140 | |
| 141 | // Constructor takes iterator range. |
| 142 | const char kString[] = "123456"; |
| 143 | SmallVector<char, 10> string(std::begin(kString), std::end(kString)); |
| 144 | |
| 145 | EXPECT_FALSE(string.dynamic()); |
| 146 | EXPECT_STREQ(string.begin(), "123456"); |
| 147 | EXPECT_EQ(string.size(), 7u); |
| 148 | |
| 149 | // Similar to emplace, but replaces rather than inserts. |
| 150 | string.replace(string.begin() + 5, '\0'); |
| 151 | EXPECT_STREQ(string.begin(), "12345"); |
| 152 | |
| 153 | swap(chars, string); |
| 154 | |
| 155 | EXPECT_STREQ(chars.begin(), "12345"); |
| 156 | EXPECT_STREQ(string.begin(), "abcdefghij"); |
| 157 | |
| 158 | EXPECT_FALSE(chars.dynamic()); |
| 159 | EXPECT_TRUE(string.dynamic()); |
| 160 | } |
| 161 | |
| 162 | TEST(SmallVector, CopyableElement) { |
| 163 | struct Pair { |
| 164 | // Needed because std::vector emplace does not use uniform initialization. |
| 165 | Pair(int a, int b) : a(a), b(b) {} |
| 166 | |
| 167 | const int a, b; |
| 168 | bool operator==(Pair p) const { return p.a == a && p.b == b; } |
| 169 | }; |
| 170 | |
| 171 | SmallVector<Pair, 5> pairs; |
| 172 | |
| 173 | EXPECT_TRUE(pairs.empty()); |
| 174 | EXPECT_EQ(pairs.max_size(), 5u); |
| 175 | |
| 176 | for (size_t i = 0; i < pairs.max_size(); ++i) { |
| 177 | EXPECT_EQ(pairs.size(), i); |
| 178 | |
| 179 | const int a = static_cast<int>(i) * 2; |
| 180 | EXPECT_EQ(pairs.emplace_back(a, a + 1), Pair(a, a + 1)); |
| 181 | } |
| 182 | |
| 183 | EXPECT_EQ(pairs.size(), 5u); |
| 184 | EXPECT_FALSE(pairs.dynamic()); |
| 185 | |
| 186 | // The vector is promoted when full. |
| 187 | EXPECT_EQ(pairs.emplace_back(10, 11), Pair(10, 11)); |
| 188 | EXPECT_TRUE(pairs.dynamic()); |
| 189 | |
| 190 | EXPECT_EQ(pairs, |
| 191 | (SmallVector{Pair{0, 1}, Pair{2, 3}, Pair{4, 5}, Pair{6, 7}, Pair{8, 9}, |
| 192 | Pair{10, 11}})); |
| 193 | |
| 194 | // Constructor takes at most N elements. |
| 195 | SmallVector<int, 6> sums = {0, 0, 0, 0, 0, 0}; |
| 196 | EXPECT_FALSE(sums.dynamic()); |
| 197 | |
| 198 | // Random-access iterators comply with standard. |
| 199 | std::transform(pairs.begin(), pairs.end(), sums.begin(), [](Pair p) { return p.a + p.b; }); |
| 200 | EXPECT_EQ(sums, (SmallVector{1, 5, 9, 13, 17, 21})); |
| 201 | |
| 202 | sums.pop_back(); |
| 203 | std::reverse(sums.begin(), sums.end()); |
| 204 | |
| 205 | EXPECT_EQ(sums, (SmallVector{17, 13, 9, 5, 1})); |
| 206 | } |
| 207 | |
| 208 | TEST(SmallVector, MovableElement) { |
| 209 | // Construct std::string elements in-place from C-style strings. Without std::in_place_type, the |
| 210 | // element type would be deduced from the first element, i.e. const char*. |
| 211 | SmallVector strings(std::in_place_type<std::string>, "", "", "", "cake", "velvet", "red", ""); |
| 212 | strings.pop_back(); |
| 213 | |
| 214 | EXPECT_EQ(strings.max_size(), 7u); |
| 215 | EXPECT_EQ(strings.size(), 6u); |
| 216 | |
| 217 | // Erase "cake" and append a substring copy. |
| 218 | { |
| 219 | const auto it = std::find_if(strings.begin(), strings.end(), |
| 220 | [](const auto& s) { return !s.empty(); }); |
| 221 | ASSERT_FALSE(it == strings.end()); |
| 222 | EXPECT_EQ(*it, "cake"); |
| 223 | |
| 224 | strings.unstable_erase(it); |
| 225 | EXPECT_EQ(strings.emplace_back("cakewalk", 4u), "cake"s); |
| 226 | } |
| 227 | |
| 228 | strings[1] = "quince"s; |
| 229 | |
| 230 | // Replace last empty string with "tart". |
| 231 | { |
| 232 | const auto rit = std::find(strings.rbegin(), strings.rend(), std::string()); |
| 233 | ASSERT_FALSE(rit == strings.rend()); |
| 234 | |
| 235 | std::initializer_list<char> list = {'t', 'a', 'r', 't'}; |
| 236 | strings.replace(rit.base() - 1, list); |
| 237 | } |
| 238 | |
| 239 | strings.front().assign("pie"); |
| 240 | |
| 241 | EXPECT_EQ(strings, (SmallVector{"pie"s, "quince"s, "tart"s, "red"s, "velvet"s, "cake"s})); |
| 242 | |
| 243 | strings.push_back("nougat"); |
| 244 | strings.push_back("oreo"); |
| 245 | EXPECT_TRUE(strings.dynamic()); |
| 246 | |
| 247 | std::rotate(strings.begin(), strings.end() - 2, strings.end()); |
| 248 | |
| 249 | EXPECT_EQ(strings, |
| 250 | (SmallVector{"nougat"s, "oreo"s, "pie"s, "quince"s, "tart"s, "red"s, "velvet"s, |
| 251 | "cake"s})); |
| 252 | } |
| 253 | |
| 254 | TEST(SmallVector, 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 | bool operator==(const Word& other) const { return other.str == str; } |
| 261 | }; |
| 262 | |
| 263 | SmallVector words(std::in_place_type<Word>, "colored", "velour"); |
| 264 | |
| 265 | // The replaced element can be referenced by the replacement. |
| 266 | { |
| 267 | const Word& word = words.replace(words.last(), words.back().str.substr(0, 3) + "vet"); |
| 268 | EXPECT_EQ(word, Word("velvet")); |
| 269 | } |
| 270 | |
| 271 | // The vector is not promoted if replacing while full. |
| 272 | EXPECT_FALSE(words.dynamic()); |
| 273 | |
| 274 | words.emplace_back("cake"); |
| 275 | EXPECT_TRUE(words.dynamic()); |
| 276 | |
| 277 | { |
| 278 | const Word& word = words.replace(words.begin(), words.front().str.substr(4)); |
| 279 | EXPECT_EQ(word, Word("red")); |
| 280 | } |
| 281 | |
| 282 | EXPECT_EQ(words, (SmallVector{Word("red"), Word("velvet"), Word("cake")})); |
| 283 | } |
| 284 | |
| 285 | TEST(SmallVector, ReverseAppend) { |
| 286 | SmallVector strings = {"red"s, "velvet"s, "cake"s}; |
| 287 | EXPECT_FALSE(strings.dynamic()); |
| 288 | |
| 289 | auto rit = strings.rbegin(); |
| 290 | while (rit != strings.rend()) { |
| 291 | // Iterator and reference are invalidated on insertion. |
| 292 | const auto i = std::distance(strings.begin(), rit.base()); |
| 293 | std::string s = *rit; |
| 294 | |
| 295 | strings.push_back(std::move(s)); |
| 296 | rit = std::make_reverse_iterator(strings.begin() + i) + 1; |
| 297 | } |
| 298 | |
| 299 | EXPECT_EQ(strings, (SmallVector{"red"s, "velvet"s, "cake"s, "cake"s, "velvet"s, "red"s})); |
| 300 | EXPECT_TRUE(strings.dynamic()); |
| 301 | } |
| 302 | |
| 303 | TEST(SmallVector, Sort) { |
| 304 | SmallVector strings(std::in_place_type<std::string>, "pie", "quince", "tart", "red", "velvet"); |
| 305 | strings.push_back("cake"s); |
| 306 | |
| 307 | auto sorted = std::move(strings); |
| 308 | EXPECT_TRUE(strings.empty()); |
| 309 | |
| 310 | EXPECT_TRUE(sorted.dynamic()); |
| 311 | EXPECT_TRUE(strings.dynamic()); |
| 312 | |
| 313 | std::sort(sorted.begin(), sorted.end()); |
| 314 | EXPECT_EQ(sorted, (SmallVector{"cake"s, "pie"s, "quince"s, "red"s, "tart"s, "velvet"s})); |
| 315 | |
| 316 | // Constructor takes array reference. |
| 317 | { |
| 318 | const char* kStrings[] = {"cake", "lie"}; |
| 319 | strings = SmallVector(kStrings); |
| 320 | EXPECT_FALSE(strings.dynamic()); |
| 321 | } |
| 322 | |
| 323 | EXPECT_GT(sorted, strings); |
| 324 | swap(sorted, strings); |
| 325 | EXPECT_LT(sorted, strings); |
| 326 | |
| 327 | EXPECT_FALSE(sorted.dynamic()); |
| 328 | EXPECT_TRUE(strings.dynamic()); |
| 329 | |
| 330 | // Append remaining elements, such that "pie" is the only difference. |
| 331 | for (const char* str : {"quince", "red", "tart", "velvet"}) { |
| 332 | sorted.emplace_back(str); |
| 333 | } |
| 334 | EXPECT_TRUE(sorted.dynamic()); |
| 335 | |
| 336 | EXPECT_NE(sorted, strings); |
| 337 | |
| 338 | // Replace second element with "pie". |
| 339 | const auto it = sorted.begin() + 1; |
| 340 | EXPECT_EQ(sorted.replace(it, 'p' + it->substr(1)), "pie"); |
| 341 | |
| 342 | EXPECT_EQ(sorted, strings); |
| 343 | } |
| 344 | |
| 345 | namespace { |
| 346 | |
| 347 | struct DestroyCounts { |
| 348 | DestroyCounts(int& live, int& dead) : counts{live, dead} {} |
| 349 | DestroyCounts(const DestroyCounts& other) : counts(other.counts) {} |
| 350 | DestroyCounts(DestroyCounts&& other) : counts(other.counts) { other.alive = false; } |
| 351 | ~DestroyCounts() { ++(alive ? counts.live : counts.dead); } |
| 352 | |
| 353 | struct { |
| 354 | int& live; |
| 355 | int& dead; |
| 356 | } counts; |
| 357 | |
| 358 | bool alive = true; |
| 359 | }; |
| 360 | |
| 361 | void swap(DestroyCounts& lhs, DestroyCounts& rhs) { |
| 362 | std::swap(lhs.alive, rhs.alive); |
| 363 | } |
| 364 | |
| 365 | } // namespace |
| 366 | |
| 367 | TEST(SmallVector, Destroy) { |
| 368 | int live = 0; |
| 369 | int dead = 0; |
| 370 | |
| 371 | { SmallVector<DestroyCounts, 3> counts; } |
| 372 | EXPECT_EQ(0, live); |
| 373 | EXPECT_EQ(0, dead); |
| 374 | |
| 375 | { |
| 376 | SmallVector<DestroyCounts, 3> counts; |
| 377 | counts.emplace_back(live, dead); |
| 378 | counts.emplace_back(live, dead); |
| 379 | counts.emplace_back(live, dead); |
| 380 | |
| 381 | EXPECT_FALSE(counts.dynamic()); |
| 382 | } |
| 383 | EXPECT_EQ(3, live); |
| 384 | EXPECT_EQ(0, dead); |
| 385 | |
| 386 | live = 0; |
| 387 | { |
| 388 | SmallVector<DestroyCounts, 3> counts; |
| 389 | counts.emplace_back(live, dead); |
| 390 | counts.emplace_back(live, dead); |
| 391 | counts.emplace_back(live, dead); |
| 392 | counts.emplace_back(live, dead); |
| 393 | |
| 394 | EXPECT_TRUE(counts.dynamic()); |
| 395 | } |
| 396 | EXPECT_EQ(4, live); |
| 397 | EXPECT_EQ(3, dead); |
| 398 | |
| 399 | live = dead = 0; |
| 400 | { |
| 401 | SmallVector<DestroyCounts, 2> counts; |
| 402 | counts.emplace_back(live, dead); |
| 403 | counts.emplace_back(live, dead); |
| 404 | counts.emplace_back(live, dead); |
| 405 | |
| 406 | auto copy = counts; |
| 407 | EXPECT_TRUE(copy.dynamic()); |
| 408 | } |
| 409 | EXPECT_EQ(6, live); |
| 410 | EXPECT_EQ(2, dead); |
| 411 | |
| 412 | live = dead = 0; |
| 413 | { |
| 414 | SmallVector<DestroyCounts, 2> counts; |
| 415 | counts.emplace_back(live, dead); |
| 416 | counts.emplace_back(live, dead); |
| 417 | counts.emplace_back(live, dead); |
| 418 | |
| 419 | auto move = std::move(counts); |
| 420 | EXPECT_TRUE(move.dynamic()); |
| 421 | } |
| 422 | EXPECT_EQ(3, live); |
| 423 | EXPECT_EQ(2, dead); |
| 424 | |
| 425 | live = dead = 0; |
| 426 | { |
| 427 | SmallVector<DestroyCounts, 2> counts1; |
| 428 | counts1.emplace_back(live, dead); |
| 429 | counts1.emplace_back(live, dead); |
| 430 | counts1.emplace_back(live, dead); |
| 431 | |
| 432 | EXPECT_TRUE(counts1.dynamic()); |
| 433 | EXPECT_EQ(2, dead); |
| 434 | dead = 0; |
| 435 | |
| 436 | SmallVector<DestroyCounts, 2> counts2; |
| 437 | counts2.emplace_back(live, dead); |
| 438 | |
| 439 | EXPECT_FALSE(counts2.dynamic()); |
| 440 | |
| 441 | swap(counts1, counts2); |
| 442 | |
| 443 | EXPECT_FALSE(counts1.dynamic()); |
| 444 | EXPECT_TRUE(counts2.dynamic()); |
| 445 | |
| 446 | EXPECT_EQ(0, live); |
| 447 | EXPECT_EQ(1, dead); |
| 448 | |
| 449 | dead = 0; |
| 450 | } |
| 451 | EXPECT_EQ(4, live); |
| 452 | EXPECT_EQ(0, dead); |
| 453 | } |
| 454 | |
| 455 | } // namespace android::test |