| 1 | /* Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <algorithm> |
| 26 | #include <memory> |
| 27 | #include <random> |
| 28 | |
| 29 | #include "prealloced_array.h" |
| 30 | |
| 31 | namespace prealloced_array_unittest { |
| 32 | |
| 33 | class PreallocedArrayTest : public ::testing::Test { |
| 34 | public: |
| 35 | PreallocedArrayTest() : int_10(PSI_NOT_INSTRUMENTED) {} |
| 36 | |
| 37 | protected: |
| 38 | Prealloced_array<int, 10> int_10; |
| 39 | int some_integer; |
| 40 | }; |
| 41 | |
| 42 | TEST_F(PreallocedArrayTest, Empty) { |
| 43 | EXPECT_EQ(10U, int_10.capacity()); |
| 44 | EXPECT_EQ(sizeof(int), int_10.element_size()); |
| 45 | EXPECT_TRUE(int_10.empty()); |
| 46 | EXPECT_EQ(0U, int_10.size()); |
| 47 | } |
| 48 | |
| 49 | #if !defined(NDEBUG) |
| 50 | // Google Test recommends DeathTest suffix for classes used in death tests. |
| 51 | typedef PreallocedArrayTest PreallocedArrayDeathTest; |
| 52 | |
| 53 | TEST_F(PreallocedArrayDeathTest, OutOfBoundsRead) { |
| 54 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 55 | EXPECT_DEATH_IF_SUPPORTED(some_integer = int_10[5], |
| 56 | ".*Assertion .*n < size.*"); |
| 57 | } |
| 58 | |
| 59 | TEST_F(PreallocedArrayDeathTest, OutOfBoundsWrite) { |
| 60 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 61 | EXPECT_DEATH_IF_SUPPORTED(int_10[5] = some_integer, |
| 62 | ".*Assertion .*n < size.*"); |
| 63 | } |
| 64 | |
| 65 | TEST_F(PreallocedArrayDeathTest, EmptyBack) { |
| 66 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 67 | EXPECT_DEATH_IF_SUPPORTED(int_10.back() = 42, ".*Assertion .*n < size.*"); |
| 68 | } |
| 69 | |
| 70 | TEST_F(PreallocedArrayDeathTest, EmptyPopBack) { |
| 71 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 72 | EXPECT_DEATH_IF_SUPPORTED(int_10.pop_back(), ".*Assertion .*!empty.*"); |
| 73 | } |
| 74 | |
| 75 | TEST_F(PreallocedArrayDeathTest, EmptyErase) { |
| 76 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 77 | size_t ix = 0; |
| 78 | EXPECT_DEATH_IF_SUPPORTED(int_10.erase(ix), ".*Assertion .*ix < size.*"); |
| 79 | } |
| 80 | |
| 81 | #endif // NDEBUG |
| 82 | |
| 83 | TEST_F(PreallocedArrayTest, Insert5) { |
| 84 | for (int ix = 0; ix < 5; ++ix) int_10.push_back(ix); |
| 85 | for (int ix = 0; ix < 5; ++ix) EXPECT_EQ(ix, int_10[ix]); |
| 86 | for (int ix = 0; ix < 5; ++ix) int_10[ix] = ix; |
| 87 | EXPECT_EQ(5U, int_10.size()); |
| 88 | EXPECT_EQ(10U, int_10.capacity()); |
| 89 | } |
| 90 | |
| 91 | TEST_F(PreallocedArrayTest, Insert15) { |
| 92 | for (int ix = 0; ix < 15; ++ix) int_10.push_back(ix); |
| 93 | for (int ix = 0; ix < 15; ++ix) EXPECT_EQ(ix, int_10[ix]); |
| 94 | for (int ix = 0; ix < 15; ++ix) int_10[ix] = ix; |
| 95 | EXPECT_EQ(15U, int_10.size()); |
| 96 | EXPECT_LE(15U, int_10.capacity()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(PreallocedArrayTest, Sort) { |
| 100 | for (int ix = 20; ix >= 0; --ix) int_10.push_back(ix); |
| 101 | std::sort(int_10.begin(), int_10.end()); |
| 102 | for (int ix = 0; ix <= 20; ++ix) EXPECT_EQ(ix, int_10[ix]); |
| 103 | } |
| 104 | |
| 105 | TEST_F(PreallocedArrayTest, Back) { |
| 106 | for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix); |
| 107 | EXPECT_EQ(15, int_10.back()); |
| 108 | int_10.back() = 42; |
| 109 | EXPECT_EQ(42, int_10.back()); |
| 110 | } |
| 111 | |
| 112 | TEST_F(PreallocedArrayTest, PopBack) { |
| 113 | for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix); |
| 114 | for (int ix = 15; ix >= 0; --ix) { |
| 115 | EXPECT_EQ(ix, int_10.back()); |
| 116 | int_10.pop_back(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | TEST_F(PreallocedArrayTest, EraseFirst) { |
| 121 | for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix); |
| 122 | EXPECT_EQ(0, int_10[0]); |
| 123 | EXPECT_EQ(16U, int_10.size()); |
| 124 | int_10.erase(int_10.begin()); |
| 125 | EXPECT_EQ(15U, int_10.size()); |
| 126 | for (int ix = 0; ix < static_cast<int>(int_10.size()); ++ix) { |
| 127 | EXPECT_EQ(ix + 1, int_10[ix]); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | TEST_F(PreallocedArrayTest, EraseLast) { |
| 132 | for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix); |
| 133 | EXPECT_EQ(15, int_10.back()); |
| 134 | EXPECT_EQ(15, int_10.at(15)); |
| 135 | int_10.erase(15); |
| 136 | EXPECT_EQ(14, int_10.back()); |
| 137 | EXPECT_EQ(14, int_10.at(14)); |
| 138 | } |
| 139 | |
| 140 | TEST_F(PreallocedArrayTest, EraseMiddle) { |
| 141 | for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix); |
| 142 | EXPECT_EQ(6, int_10[6]); |
| 143 | EXPECT_EQ(7, int_10[7]); |
| 144 | EXPECT_EQ(16U, int_10.size()); |
| 145 | int_10.erase(7); |
| 146 | EXPECT_EQ(6, int_10[6]); |
| 147 | EXPECT_EQ(8, int_10[7]); |
| 148 | EXPECT_EQ(9, int_10[8]); |
| 149 | EXPECT_EQ(15U, int_10.size()); |
| 150 | } |
| 151 | |
| 152 | TEST_F(PreallocedArrayTest, ResizeSame) { |
| 153 | for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix); |
| 154 | EXPECT_EQ(16U, int_10.size()); |
| 155 | int_10.resize(16U); |
| 156 | EXPECT_EQ(16U, int_10.size()); |
| 157 | } |
| 158 | |
| 159 | TEST_F(PreallocedArrayTest, ResizeGrow) { |
| 160 | int_10.push_back(1); |
| 161 | int_10.resize(20); |
| 162 | EXPECT_EQ(1, int_10[0]); |
| 163 | EXPECT_EQ(0, int_10[1]); |
| 164 | EXPECT_EQ(20U, int_10.size()); |
| 165 | EXPECT_GE(int_10.capacity(), 20U); |
| 166 | } |
| 167 | |
| 168 | TEST_F(PreallocedArrayTest, ResizeGrowVal) { |
| 169 | int_10.resize(20, 42); |
| 170 | EXPECT_EQ(42, int_10[0]); |
| 171 | EXPECT_EQ(42, int_10[19]); |
| 172 | EXPECT_EQ(20U, int_10.size()); |
| 173 | EXPECT_GE(int_10.capacity(), 20U); |
| 174 | } |
| 175 | |
| 176 | TEST_F(PreallocedArrayTest, ResizeShrink) { |
| 177 | for (int ix = 0; ix <= 15; ++ix) int_10.push_back(ix); |
| 178 | EXPECT_EQ(16U, int_10.size()); |
| 179 | int_10.resize(10); |
| 180 | EXPECT_EQ(10U, int_10.size()); |
| 181 | } |
| 182 | |
| 183 | TEST_F(PreallocedArrayTest, InsertUnique) { |
| 184 | for (int ix = 0; ix < 10; ++ix) { |
| 185 | int_10.push_back(ix); |
| 186 | int_10.push_back(ix); |
| 187 | } |
| 188 | std::random_device rng; |
| 189 | std::mt19937 urng(rng()); |
| 190 | std::shuffle(int_10.begin(), int_10.end(), urng); |
| 191 | Prealloced_array<int, 1> unique_arr(PSI_NOT_INSTRUMENTED); |
| 192 | for (int *pi = int_10.begin(); pi != int_10.end(); ++pi) { |
| 193 | unique_arr.insert_unique(*pi); |
| 194 | EXPECT_EQ(1U, unique_arr.count_unique(*pi)); |
| 195 | } |
| 196 | EXPECT_EQ(10U, unique_arr.size()); |
| 197 | // Duplicates should have been ignored, and the result should be sorted. |
| 198 | for (int ix = 0; ix < static_cast<int>(unique_arr.size()); ++ix) { |
| 199 | EXPECT_EQ(ix, unique_arr[ix]); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | TEST_F(PreallocedArrayTest, EraseUnique) { |
| 204 | for (int ix = 0; ix < 20; ++ix) int_10.push_back(ix); |
| 205 | |
| 206 | // The array should be sorted by default. |
| 207 | for (int ix = 0; ix < 20; ++ix) EXPECT_EQ(ix, int_10[ix]); |
| 208 | |
| 209 | // Now remove all even numbers. |
| 210 | for (int ix = 0; ix < 10; ++ix) EXPECT_EQ(1U, int_10.erase_unique(2 * ix)); |
| 211 | |
| 212 | // 10 numbers should remain. |
| 213 | EXPECT_EQ(10U, int_10.size()); |
| 214 | |
| 215 | // Removing non-existing numbers should return 0. |
| 216 | for (int ix = 0; ix < 10; ++ix) { |
| 217 | EXPECT_EQ(0U, int_10.count_unique(2 * ix)); |
| 218 | EXPECT_EQ(0U, int_10.erase_unique(2 * ix)); |
| 219 | } |
| 220 | |
| 221 | // 10 numbers should still remain. |
| 222 | EXPECT_EQ(10U, int_10.size()); |
| 223 | |
| 224 | // The array should still be sorted and contain odd numbers. |
| 225 | for (int ix = 0; ix < 10; ++ix) EXPECT_EQ(2 * ix + 1, int_10[ix]); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | A simple class for testing that object copying and destruction is done |
| 230 | properly when we have to expand the array a few times, |
| 231 | and has_trivial_destructor == false. |
| 232 | */ |
| 233 | class IntWrap { |
| 234 | public: |
| 235 | IntWrap() { m_int = new int(0); } |
| 236 | explicit IntWrap(int arg) { m_int = new int(arg); } |
| 237 | IntWrap(const IntWrap &other) { m_int = new int(other.getval()); } |
| 238 | ~IntWrap() { delete m_int; } |
| 239 | IntWrap &operator=(const IntWrap &rhs) { |
| 240 | *m_int = rhs.getval(); |
| 241 | return *this; |
| 242 | } |
| 243 | |
| 244 | int getval() const { return *m_int; } |
| 245 | |
| 246 | private: |
| 247 | int *m_int; |
| 248 | }; |
| 249 | |
| 250 | /* |
| 251 | To verify that there are no leaks, do: |
| 252 | valgrind ./prealloced_array-t --gtest_filter="-*DeathTest*" |
| 253 | */ |
| 254 | TEST_F(PreallocedArrayTest, NoMemLeaksAssignAt) { |
| 255 | Prealloced_array<IntWrap, 10> array(PSI_NOT_INSTRUMENTED, 0); |
| 256 | EXPECT_EQ(0U, array.size()); |
| 257 | |
| 258 | array.assign_at(3, IntWrap(3)); |
| 259 | EXPECT_EQ(4U, array.size()); |
| 260 | EXPECT_EQ(3, array[3].getval()); |
| 261 | EXPECT_EQ(0, array[0].getval()); |
| 262 | |
| 263 | array.assign_at(0, IntWrap(42)); |
| 264 | EXPECT_EQ(4U, array.size()); |
| 265 | EXPECT_EQ(42, array[0].getval()); |
| 266 | |
| 267 | array.assign_at(4, IntWrap(4)); |
| 268 | EXPECT_EQ(5U, array.size()); |
| 269 | EXPECT_EQ(4, array[4].getval()); |
| 270 | |
| 271 | array.assign_at(42, IntWrap(42)); |
| 272 | EXPECT_EQ(43U, array.size()); |
| 273 | EXPECT_EQ(42, array[42].getval()); |
| 274 | |
| 275 | array.assign_at(0, IntWrap(0)); |
| 276 | EXPECT_EQ(0, array[0].getval()); |
| 277 | } |
| 278 | |
| 279 | TEST_F(PreallocedArrayTest, NoMemLeaksInitializing) { |
| 280 | const size_t initial_capacity = 10; |
| 281 | Prealloced_array<IntWrap, initial_capacity> array1(PSI_NOT_INSTRUMENTED, 0); |
| 282 | EXPECT_EQ(0U, array1.size()); |
| 283 | |
| 284 | Prealloced_array<IntWrap, initial_capacity> array2(PSI_NOT_INSTRUMENTED, |
| 285 | initial_capacity / 2); |
| 286 | EXPECT_EQ(5U, array2.size()); |
| 287 | |
| 288 | Prealloced_array<IntWrap, 10> array3(PSI_NOT_INSTRUMENTED, initial_capacity); |
| 289 | EXPECT_EQ(10U, array3.size()); |
| 290 | |
| 291 | Prealloced_array<IntWrap, 10> array4(PSI_NOT_INSTRUMENTED, |
| 292 | 2 * initial_capacity); |
| 293 | EXPECT_EQ(20U, array4.size()); |
| 294 | } |
| 295 | |
| 296 | TEST_F(PreallocedArrayTest, NoMemLeaksPushing) { |
| 297 | Prealloced_array<IntWrap, 1> array(PSI_NOT_INSTRUMENTED); |
| 298 | for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix)); |
| 299 | for (int ix = 0; ix < 42; ++ix) EXPECT_EQ(ix, array[ix].getval()); |
| 300 | } |
| 301 | |
| 302 | TEST_F(PreallocedArrayTest, NoMemLeaksPopping) { |
| 303 | Prealloced_array<IntWrap, 1> array(PSI_NOT_INSTRUMENTED); |
| 304 | for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix)); |
| 305 | while (!array.empty()) array.pop_back(); |
| 306 | } |
| 307 | |
| 308 | TEST_F(PreallocedArrayTest, NoMemLeaksErasing) { |
| 309 | Prealloced_array<IntWrap, 1> array(PSI_NOT_INSTRUMENTED); |
| 310 | for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix)); |
| 311 | for (int ix = 0; !array.empty(); ++ix) { |
| 312 | EXPECT_EQ(ix, array[0].getval()); |
| 313 | array.erase(array.begin()); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | TEST_F(PreallocedArrayTest, NoMemLeaksClearing) { |
| 318 | Prealloced_array<IntWrap, 1> array(PSI_NOT_INSTRUMENTED); |
| 319 | for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix)); |
| 320 | array.clear(); |
| 321 | EXPECT_EQ(0U, array.size()); |
| 322 | } |
| 323 | |
| 324 | TEST_F(PreallocedArrayTest, NoMemLeaksResizing) { |
| 325 | Prealloced_array<IntWrap, 1> array(PSI_NOT_INSTRUMENTED); |
| 326 | for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix)); |
| 327 | array.resize(0); |
| 328 | EXPECT_EQ(0U, array.size()); |
| 329 | } |
| 330 | |
| 331 | TEST_F(PreallocedArrayTest, NoMemLeaksAssigning) { |
| 332 | Prealloced_array<IntWrap, 1> array1(PSI_NOT_INSTRUMENTED); |
| 333 | for (int ix = 0; ix < 42; ++ix) array1.push_back(IntWrap(ix)); |
| 334 | Prealloced_array<IntWrap, 1> array2(PSI_NOT_INSTRUMENTED); |
| 335 | for (int ix = 0; ix < 10; ++ix) array2.push_back(IntWrap(ix + 100)); |
| 336 | array2 = array1; |
| 337 | EXPECT_EQ(array1.size(), array2.size()); |
| 338 | for (size_t ix = 0; ix < array1.size(); ++ix) |
| 339 | EXPECT_EQ(array1[ix].getval(), array2[ix].getval()); |
| 340 | } |
| 341 | |
| 342 | TEST_F(PreallocedArrayTest, NoMemLeaksEraseAll) { |
| 343 | Prealloced_array<IntWrap, 1> array(PSI_NOT_INSTRUMENTED); |
| 344 | for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix)); |
| 345 | array.erase(array.begin(), array.end()); |
| 346 | EXPECT_EQ(0U, array.size()); |
| 347 | } |
| 348 | |
| 349 | TEST_F(PreallocedArrayTest, NoMemLeaksEraseMiddle) { |
| 350 | Prealloced_array<IntWrap, 1> array(PSI_NOT_INSTRUMENTED); |
| 351 | for (int ix = 0; ix < 42; ++ix) array.push_back(IntWrap(ix)); |
| 352 | array.erase(array.begin() + 1, array.end() - 1); |
| 353 | EXPECT_EQ(2U, array.size()); |
| 354 | EXPECT_EQ(0, array[0].getval()); |
| 355 | EXPECT_EQ(41, array[1].getval()); |
| 356 | } |
| 357 | |
| 358 | TEST_F(PreallocedArrayTest, NoMemLeaksEraseSwap) { |
| 359 | Prealloced_array<IntWrap, 1> array1(PSI_NOT_INSTRUMENTED); |
| 360 | for (int ix = 0; ix < 42; ++ix) array1.push_back(IntWrap(ix)); |
| 361 | Prealloced_array<IntWrap, 1> array2(PSI_NOT_INSTRUMENTED); |
| 362 | for (int ix = 0; ix < 10; ++ix) array2.push_back(IntWrap(ix + 100)); |
| 363 | array1.swap(array2); |
| 364 | EXPECT_EQ(10U, array1.size()); |
| 365 | EXPECT_EQ(42U, array2.size()); |
| 366 | Prealloced_array<IntWrap, 1>(PSI_NOT_INSTRUMENTED).swap(array1); |
| 367 | EXPECT_EQ(0U, array1.size()); |
| 368 | } |
| 369 | |
| 370 | TEST_F(PreallocedArrayTest, NoMemLeaksMySwap) { |
| 371 | Prealloced_array<IntWrap, 2> array1(PSI_NOT_INSTRUMENTED); |
| 372 | Prealloced_array<IntWrap, 2> array2(PSI_NOT_INSTRUMENTED); |
| 373 | array1.push_back(IntWrap(1)); |
| 374 | array2.push_back(IntWrap(2)); |
| 375 | array2.push_back(IntWrap(22)); |
| 376 | array1.swap(array2); |
| 377 | EXPECT_EQ(2U, array1.size()); |
| 378 | EXPECT_EQ(1U, array2.size()); |
| 379 | EXPECT_EQ(2, array1[0].getval()); |
| 380 | EXPECT_EQ(22, array1[1].getval()); |
| 381 | EXPECT_EQ(1, array2[0].getval()); |
| 382 | } |
| 383 | |
| 384 | TEST_F(PreallocedArrayTest, NoMemLeaksStdSwap) { |
| 385 | Prealloced_array<IntWrap, 1> array1(PSI_NOT_INSTRUMENTED); |
| 386 | for (int ix = 0; ix < 42; ++ix) array1.push_back(IntWrap(ix)); |
| 387 | Prealloced_array<IntWrap, 1> array2(PSI_NOT_INSTRUMENTED, array1.begin(), |
| 388 | array1.begin() + 10); |
| 389 | EXPECT_EQ(10U, array2.size()); |
| 390 | IntWrap *p1 = array1.begin(); |
| 391 | IntWrap *p2 = array2.begin(); |
| 392 | array1.swap(array2); |
| 393 | EXPECT_EQ(10U, array1.size()); |
| 394 | EXPECT_EQ(42U, array2.size()); |
| 395 | // We expect a buffer swap here. |
| 396 | EXPECT_EQ(p1, array2.begin()); |
| 397 | EXPECT_EQ(p2, array1.begin()); |
| 398 | } |
| 399 | |
| 400 | TEST_F(PreallocedArrayTest, NoMemLeaksShrinkToFitMalloc) { |
| 401 | Prealloced_array<IntWrap, 1> array1(PSI_NOT_INSTRUMENTED); |
| 402 | for (int ix = 0; ix < 42; ++ix) array1.push_back(IntWrap(ix)); |
| 403 | IntWrap *p1 = array1.begin(); |
| 404 | array1.shrink_to_fit(); |
| 405 | EXPECT_EQ(42U, array1.size()); |
| 406 | EXPECT_EQ(42U, array1.capacity()); |
| 407 | EXPECT_NE(p1, array1.begin()); |
| 408 | } |
| 409 | |
| 410 | TEST_F(PreallocedArrayTest, NoMemLeaksShrinkToFitSameSize) { |
| 411 | Prealloced_array<IntWrap, 10> array1(PSI_NOT_INSTRUMENTED); |
| 412 | for (int ix = 0; ix < 42; ++ix) array1.push_back(IntWrap(ix)); |
| 413 | for (int ix = 0; array1.size() != array1.capacity(); ++ix) |
| 414 | array1.push_back(IntWrap(ix)); |
| 415 | IntWrap *p1 = array1.begin(); |
| 416 | array1.shrink_to_fit(); |
| 417 | EXPECT_EQ(p1, array1.begin()); |
| 418 | } |
| 419 | |
| 420 | TEST_F(PreallocedArrayTest, NoMemLeaksShrinkToFitPrealloc) { |
| 421 | Prealloced_array<IntWrap, 100> array1(PSI_NOT_INSTRUMENTED); |
| 422 | for (int ix = 0; ix < 42; ++ix) array1.push_back(IntWrap(ix)); |
| 423 | IntWrap *p1 = array1.begin(); |
| 424 | array1.shrink_to_fit(); |
| 425 | EXPECT_EQ(42U, array1.size()); |
| 426 | EXPECT_EQ(100U, array1.capacity()); |
| 427 | EXPECT_EQ(p1, array1.begin()); |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | A simple class to verify that Prealloced_array also works for |
| 432 | classes which have their own operator new/delete. |
| 433 | */ |
| 434 | class TestAlloc { |
| 435 | public: |
| 436 | explicit TestAlloc(int val) : m_int(val) {} |
| 437 | |
| 438 | int getval() const { return m_int; } |
| 439 | |
| 440 | private: |
| 441 | int m_int; |
| 442 | |
| 443 | static void *operator new(size_t) { throw std::bad_alloc(); } |
| 444 | }; |
| 445 | |
| 446 | /* |
| 447 | There is no THD and no mem-root available for the execution of this test. |
| 448 | This shows that the memory management of Prealloced_array works OK for |
| 449 | classes with custom new/delete. |
| 450 | */ |
| 451 | TEST_F(PreallocedArrayTest, CustomNewDelete) { |
| 452 | Prealloced_array<TestAlloc, 1> array(PSI_NOT_INSTRUMENTED); |
| 453 | for (int ix = 0; ix < 42; ++ix) array.push_back(TestAlloc(ix)); |
| 454 | for (int ix = 0; ix < 42; ++ix) EXPECT_EQ(ix, array[ix].getval()); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | A simple class to verify that Prealloced_array also works for |
| 459 | dynamically allocated objects on heap. |
| 460 | */ |
| 461 | class TestAllocHeap { |
| 462 | public: |
| 463 | TestAllocHeap(int val) : m_int(val) {} |
| 464 | int getval() const { return m_int; } |
| 465 | |
| 466 | private: |
| 467 | int m_int; |
| 468 | }; |
| 469 | |
| 470 | TEST_F(PreallocedArrayTest, CustomNewDeletePointer) { |
| 471 | Prealloced_array<TestAllocHeap *, 1> array(PSI_NOT_INSTRUMENTED); |
| 472 | for (int ix = 0; ix < 42; ++ix) array.push_back(new TestAllocHeap(ix)); |
| 473 | for (int ix = 0; ix < 42; ++ix) EXPECT_EQ(ix, array[ix]->getval()); |
| 474 | for (int ix = 0; ix < 42; ++ix) delete array[ix]; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | A class that wraps an integer. Objects of this class can be moved, |
| 479 | but cannot be copied. |
| 480 | */ |
| 481 | class IntWrapMove { |
| 482 | std::unique_ptr<int> m_i; |
| 483 | |
| 484 | public: |
| 485 | explicit IntWrapMove(int i) : m_i(new int(i)) {} |
| 486 | IntWrapMove(const IntWrapMove &) = delete; |
| 487 | IntWrapMove &operator=(const IntWrapMove &) = delete; |
| 488 | IntWrapMove(IntWrapMove &&) = default; |
| 489 | IntWrapMove &operator=(IntWrapMove &&) = default; |
| 490 | int getval() const { return *m_i; } |
| 491 | }; |
| 492 | |
| 493 | /* |
| 494 | Test that a Prealloced_array can hold objects that cannot be copied. |
| 495 | */ |
| 496 | TEST_F(PreallocedArrayTest, Move) { |
| 497 | using IntArray = Prealloced_array<IntWrapMove, 1>; |
| 498 | IntArray array(PSI_NOT_INSTRUMENTED); |
| 499 | |
| 500 | // Test that we can add non-copyable elements to the array. |
| 501 | for (int i = 0; i < 5; ++i) array.push_back(IntWrapMove(i)); |
| 502 | for (int i = 5; i < 10; ++i) array.emplace_back(i); |
| 503 | for (int i = 0; i < 10; ++i) EXPECT_EQ(i, array[i].getval()); |
| 504 | array.insert(array.begin(), IntWrapMove(100)); |
| 505 | array.emplace(array.begin() + 1, IntWrapMove(101)); |
| 506 | EXPECT_EQ(12U, array.size()); |
| 507 | EXPECT_EQ(100, array[0].getval()); |
| 508 | EXPECT_EQ(101, array[1].getval()); |
| 509 | EXPECT_EQ(0, array[2].getval()); |
| 510 | |
| 511 | // Test that we can remove non-copyable elements from the array. |
| 512 | IntArray::iterator it = array.erase(1); |
| 513 | EXPECT_EQ(1, it - array.begin()); |
| 514 | EXPECT_EQ(0, it->getval()); |
| 515 | it = array.erase(array.cbegin() + 1); |
| 516 | EXPECT_EQ(1, it - array.begin()); |
| 517 | EXPECT_EQ(1, it->getval()); |
| 518 | it = array.erase(array.cbegin() + 2, array.cend() - 2); |
| 519 | EXPECT_EQ(8, it->getval()); |
| 520 | EXPECT_EQ(2, array.end() - it); |
| 521 | EXPECT_EQ(2, it - array.begin()); |
| 522 | EXPECT_EQ(4U, array.size()); |
| 523 | EXPECT_EQ(100, array[0].getval()); |
| 524 | EXPECT_EQ(1, array[1].getval()); |
| 525 | EXPECT_EQ(8, array[2].getval()); |
| 526 | EXPECT_EQ(9, array[3].getval()); |
| 527 | } |
| 528 | |
| 529 | TEST_F(PreallocedArrayTest, ShrinkToFit) { |
| 530 | Prealloced_array<int, 1> array(PSI_NOT_INSTRUMENTED); |
| 531 | array.push_back(1); |
| 532 | array.push_back(2); |
| 533 | array.push_back(3); |
| 534 | size_t capacity = array.capacity(); |
| 535 | EXPECT_LE(3U, capacity); |
| 536 | |
| 537 | // After clear(), array is empty, but the capacity is unchanged. |
| 538 | array.clear(); |
| 539 | EXPECT_TRUE(array.empty()); |
| 540 | EXPECT_EQ(capacity, array.capacity()); |
| 541 | |
| 542 | // After shrink_to_fit(), the capacity should shrink to the prealloc size. |
| 543 | array.shrink_to_fit(); |
| 544 | EXPECT_EQ(1U, array.capacity()); |
| 545 | } |
| 546 | |
| 547 | // https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=vs-2019#implicit-conversion-of-integral-constant-expressions-to-null-pointer |
| 548 | TEST_F(PreallocedArrayTest, CorrectOverloadIsChosen) { |
| 549 | Prealloced_array<int *, 4> array{PSI_NOT_INSTRUMENTED}; |
| 550 | EXPECT_EQ(0, array.size()); |
| 551 | |
| 552 | int x = 2; |
| 553 | Prealloced_array<int *, 4> array2{&x}; |
| 554 | EXPECT_EQ(1, array2.size()); |
| 555 | |
| 556 | Prealloced_array<int64_t, 4> array3{x}; |
| 557 | EXPECT_EQ(1, array3.size()); |
| 558 | } |
| 559 | |
| 560 | } // namespace prealloced_array_unittest |