1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "mem/arena.h"
17 #include "mem/arena_allocator.h"
18 #include "mem/arena_allocator_stl_adapter.h"
19 #include "mem/pool_manager.h"
20 #include "mem/mem.h"
21 #include "mem/mem_config.h"
22
23 #include "utils/arena_containers.h"
24
25 #include "gtest/gtest.h"
26 #include "utils/logger.h"
27
28 #include <string>
29 #include <array>
30 #include <limits>
31 #include <cstdint>
32 #include <ctime>
33
34 namespace ark {
35
36 constexpr const int64_t DEFAULT_SEED = 123456;
37
38 class ArenaAllocatorTest : public testing::Test {
39 public:
ArenaAllocatorTest()40 ArenaAllocatorTest()
41 {
42 #ifdef PANDA_NIGHTLY_TEST_ON
43 seed_ = std::time(NULL);
44 #else
45 seed_ = DEFAULT_SEED;
46 #endif
47 }
48
49 ~ArenaAllocatorTest() override = default;
50
51 NO_MOVE_SEMANTIC(ArenaAllocatorTest);
52 NO_COPY_SEMANTIC(ArenaAllocatorTest);
53
54 protected:
55 static constexpr size_t MIN_LOG_ALIGN_SIZE_T = static_cast<size_t>(LOG_ALIGN_MIN);
56 static constexpr size_t MAX_LOG_ALIGN_SIZE_T = static_cast<size_t>(LOG_ALIGN_MAX);
57 static constexpr size_t ARRAY_SIZE = 1024;
58
59 template <class T>
MaxValue()60 static constexpr T MaxValue()
61 {
62 return std::numeric_limits<T>::max();
63 }
64
IsAligned(const void * ptr,size_t alignment)65 static bool IsAligned(const void *ptr, size_t alignment)
66 {
67 return reinterpret_cast<uintptr_t>(ptr) % alignment == 0;
68 }
69
SetUp()70 void SetUp() override
71 {
72 static constexpr size_t INTERNAL_SIZE = 128_MB;
73 ark::mem::MemConfig::Initialize(0U, INTERNAL_SIZE, 0U, 0U, 0U, 0U);
74 PoolManager::Initialize();
75 }
76
GetRand() const77 int GetRand() const
78 {
79 // NOLINTNEXTLINE(cert-msc50-cpp)
80 return rand();
81 }
82
83 template <class T>
AllocateWithAlignment() const84 void AllocateWithAlignment() const
85 {
86 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
87
88 for (Alignment align = LOG_ALIGN_MIN; align <= LOG_ALIGN_MAX;
89 align = static_cast<Alignment>(static_cast<size_t>(align) + 1)) {
90 std::array<T *, ARRAY_SIZE> arr {};
91
92 size_t mask = GetAlignmentInBytes(align) - 1L;
93
94 // Allocations
95 srand(seed_);
96 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
97 arr[i] = static_cast<T *>(aa.Alloc(sizeof(T), align));
98 *arr[i] = GetRand() % MaxValue<T>();
99 }
100
101 // Allocations checking
102 srand(seed_);
103 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
104 ASSERT_NE(arr[i], nullptr) << "value of i: " << i << ", align: " << align;
105 ASSERT_EQ(reinterpret_cast<size_t>(arr[i]) & mask, 0U) << "value of i: " << i << ", align: " << align;
106 ASSERT_EQ(*arr[i], GetRand() % MaxValue<T>()) << "value of i: " << i << ", align: " << align;
107 }
108 }
109 }
110
111 template <class T>
AllocateWithDiffAlignment() const112 void AllocateWithDiffAlignment() const
113 {
114 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
115
116 std::array<T *, ARRAY_SIZE> arr {};
117
118 // Allocations
119 srand(seed_);
120 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
121 auto randomValue = GetRand();
122 size_t randAlign = MIN_LOG_ALIGN_SIZE_T + randomValue % (MAX_LOG_ALIGN_SIZE_T - MIN_LOG_ALIGN_SIZE_T);
123 arr[i] = static_cast<T *>(aa.Alloc(sizeof(T), static_cast<Alignment>(randAlign)));
124 *arr[i] = randomValue % MaxValue<T>();
125 }
126
127 // Allocations checking
128 srand(seed_);
129 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
130 auto randomValue = GetRand();
131 size_t align = MIN_LOG_ALIGN_SIZE_T + randomValue % (MAX_LOG_ALIGN_SIZE_T - MIN_LOG_ALIGN_SIZE_T);
132 size_t mask = GetAlignmentInBytes(static_cast<Alignment>(align)) - 1L;
133
134 ASSERT_NE(arr[i], nullptr);
135 ASSERT_EQ(reinterpret_cast<size_t>(arr[i]) & mask, 0U) << "value of i: " << i << ", align: " << align;
136 ASSERT_EQ(*arr[i], randomValue % MaxValue<T>()) << "value of i: " << i;
137 }
138 }
139
TearDown()140 void TearDown() override
141 {
142 PoolManager::Finalize();
143 ark::mem::MemConfig::Finalize();
144 }
145
GetSeed()146 unsigned int GetSeed()
147 {
148 return seed_;
149 }
150
151 private:
152 unsigned int seed_;
153 };
154
155 class ComplexClass final {
156 public:
ComplexClass()157 ComplexClass() : value_(0U), strValue_("0") {}
ComplexClass(size_t value)158 explicit ComplexClass(size_t value) : value_(value), strValue_(std::to_string(value_)) {}
ComplexClass(size_t value,std::string strValue)159 ComplexClass(size_t value, std::string strValue) : value_(value), strValue_(std::move(strValue)) {}
160 ComplexClass(const ComplexClass &other) = default;
161 ComplexClass(ComplexClass &&other) noexcept = default;
162
163 ComplexClass &operator=(const ComplexClass &other) = default;
164 ComplexClass &operator=(ComplexClass &&other) = default;
165
GetValue() const166 size_t GetValue() const noexcept
167 {
168 return value_;
169 }
GetString() const170 std::string GetString() const noexcept
171 {
172 return strValue_;
173 }
174
SetValue(size_t value)175 void SetValue(size_t value)
176 {
177 value_ = value;
178 strValue_ = std::to_string(value);
179 }
180
181 ~ComplexClass() = default;
182
183 private:
184 size_t value_;
185 std::string strValue_;
186 };
187
TEST_F(ArenaAllocatorTest,AllocateTest)188 TEST_F(ArenaAllocatorTest, AllocateTest)
189 {
190 static constexpr size_t FIRST_ALLOC_SIZE = 24;
191 static constexpr size_t TEMP_ALLOC_SIZE = 1024;
192 static constexpr char STASHED_VALUE = 33;
193 void *addr;
194 void *tmp;
195 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
196
197 addr = aa.Alloc(FIRST_ALLOC_SIZE);
198 ASSERT_NE(addr, nullptr);
199 ASSERT_TRUE(IsAligned(addr, GetAlignmentInBytes(DEFAULT_ARENA_ALIGNMENT)));
200 addr = aa.Alloc(4U);
201 ASSERT_NE(addr, nullptr);
202 ASSERT_TRUE(IsAligned(addr, GetAlignmentInBytes(DEFAULT_ARENA_ALIGNMENT)));
203 tmp = aa.AllocArray<int>(TEMP_ALLOC_SIZE);
204 ASSERT_NE(tmp, nullptr);
205 // Make sure that we force to using dynamic pool if STACK pool enabled
206 for (size_t i = 0; i < 5U; ++i) {
207 void *mem = nullptr;
208 mem = aa.Alloc(DEFAULT_ARENA_SIZE / 2U);
209 ASSERT_NE(mem, nullptr);
210 *(static_cast<char *>(mem)) = STASHED_VALUE; // Try to catch segfault just in case something wrong
211 }
212 ASSERT_NE(tmp = aa.Alloc(DEFAULT_ARENA_SIZE - AlignUp(sizeof(Arena), GetAlignmentInBytes(DEFAULT_ARENA_ALIGNMENT))),
213 nullptr);
214 size_t maxAlignDrift =
215 (DEFAULT_ALIGNMENT_IN_BYTES > alignof(Arena)) ? (DEFAULT_ALIGNMENT_IN_BYTES - alignof(Arena)) : 0U;
216 ASSERT_EQ(tmp = aa.Alloc(DEFAULT_ARENA_SIZE + maxAlignDrift + 1U), nullptr);
217 }
218
TEST_F(ArenaAllocatorTest,AllocateVectorTest)219 TEST_F(ArenaAllocatorTest, AllocateVectorTest)
220 {
221 constexpr size_t SIZE = 2048;
222 constexpr size_t SMALL_MAGIC_CONSTANT = 3;
223
224 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
225 ArenaVector<unsigned> vec(aa.Adapter());
226
227 for (size_t i = 0; i < SIZE; ++i) {
228 vec.push_back(i * SMALL_MAGIC_CONSTANT);
229 }
230
231 ASSERT_EQ(SIZE, vec.size());
232 vec.shrink_to_fit();
233 ASSERT_EQ(SIZE, vec.size());
234
235 for (size_t i = 0; i < SIZE; ++i) {
236 ASSERT_EQ(i * SMALL_MAGIC_CONSTANT, vec[i]) << "value of i: " << i;
237 }
238 }
239
TEST_F(ArenaAllocatorTest,AllocateVectorWithComplexTypeTest)240 TEST_F(ArenaAllocatorTest, AllocateVectorWithComplexTypeTest)
241 {
242 constexpr size_t SIZE = 512;
243 constexpr size_t MAGIC_CONSTANT_1 = std::numeric_limits<size_t>::max() / (SIZE + 2U);
244 srand(GetSeed());
245 size_t magicConstant2 = GetRand() % SIZE;
246
247 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
248 ArenaVector<ComplexClass> vec(aa.Adapter());
249
250 // Allocate SIZE objects
251 for (size_t i = 0; i < SIZE; ++i) {
252 vec.emplace_back(i * MAGIC_CONSTANT_1 + magicConstant2, std::to_string(i));
253 }
254
255 // Size checking
256 ASSERT_EQ(SIZE, vec.size());
257
258 // Allocations checking
259 for (size_t i = 0; i < SIZE; ++i) {
260 ASSERT_EQ(vec[i].GetValue(), i * MAGIC_CONSTANT_1 + magicConstant2) << "value of i: " << i;
261 ASSERT_EQ(vec[i].GetString(), std::to_string(i)) << i;
262 }
263 Span<ComplexClass> dataPtr(vec.data(), SIZE);
264 for (size_t i = 0; i < SIZE; ++i) {
265 ASSERT_EQ(dataPtr[i].GetValue(), i * MAGIC_CONSTANT_1 + magicConstant2) << "value of i: " << i;
266 ASSERT_EQ(dataPtr[i].GetString(), std::to_string(i)) << "value of i: " << i;
267 }
268
269 // Resizing and new elements assignment
270 constexpr size_t SIZE_2 = SIZE << 1U;
271 vec.assign(SIZE_2, ComplexClass(1U, "1"));
272
273 // Size checking
274 ASSERT_EQ(SIZE_2, vec.size());
275 vec.shrink_to_fit();
276 ASSERT_EQ(SIZE_2, vec.size());
277
278 // Allocations and assignment checking
279 for (size_t i = 0; i < SIZE_2; ++i) {
280 ASSERT_EQ(vec[i].GetValue(), 1U) << "value of i: " << i;
281 ASSERT_EQ(vec[i].GetString(), "1") << "value of i: " << i;
282 }
283
284 // Increasing size
285 constexpr size_t SIZE_4 = SIZE_2 << 1U;
286 vec.resize(SIZE_4, ComplexClass());
287
288 // Size checking
289 ASSERT_EQ(SIZE_4, vec.size());
290
291 // Allocations checking
292 for (size_t i = 0; i < SIZE_4 / 2U; ++i) {
293 ASSERT_EQ(vec[i].GetValue(), 1U) << "value of i: " << i;
294 ASSERT_EQ(vec[i].GetString(), "1") << "value of i: " << i;
295 }
296 for (size_t i = SIZE_4 / 2U; i < SIZE_4; ++i) {
297 ASSERT_EQ(vec[i].GetValue(), 0U) << "value of i: " << i;
298 ASSERT_EQ(vec[i].GetString(), "0") << "value of i: " << i;
299 }
300
301 // Decreasing size
302 vec.resize(SIZE);
303
304 // Size checking
305 ASSERT_EQ(SIZE, vec.size());
306 vec.shrink_to_fit();
307 ASSERT_EQ(SIZE, vec.size());
308
309 // Allocations checking
310 for (size_t i = 0; i < SIZE; ++i) {
311 ASSERT_EQ(vec[i].GetValue(), 1U) << "value of i: " << i;
312 ASSERT_EQ(vec[i].GetString(), "1") << "value of i: " << i;
313 }
314 }
315
TEST_F(ArenaAllocatorTest,AllocateDequeWithComplexTypeTest)316 TEST_F(ArenaAllocatorTest, AllocateDequeWithComplexTypeTest)
317 {
318 constexpr size_t SIZE = 2048;
319 constexpr size_t MAGIC_CONSTANT_1 = std::numeric_limits<size_t>::max() / (SIZE + 2U);
320 srand(GetSeed());
321 size_t magicConstant2 = GetRand() % SIZE;
322
323 size_t i;
324
325 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
326 ArenaDeque<ComplexClass> deq(aa.Adapter());
327
328 // Allocate SIZE objects
329 for (size_t j = 0; j < SIZE; ++j) {
330 deq.emplace_back(j * MAGIC_CONSTANT_1 + magicConstant2, std::to_string(j));
331 }
332
333 // Size checking
334 ASSERT_EQ(SIZE, deq.size());
335
336 // Allocations checking
337 i = 0;
338 for (auto it = deq.cbegin(); it != deq.cend(); ++it, ++i) {
339 ASSERT_EQ(it->GetValue(), i * MAGIC_CONSTANT_1 + magicConstant2) << "value of i: " << i;
340 ASSERT_EQ(it->GetString(), std::to_string(i)) << "value of i: " << i;
341 }
342
343 // Resizing and new elements assignment
344 constexpr size_t SIZE_2 = SIZE << 1U;
345 deq.assign(SIZE_2, ComplexClass(1U, "1"));
346
347 // Size checking
348 ASSERT_EQ(SIZE_2, deq.size());
349 deq.shrink_to_fit();
350 ASSERT_EQ(SIZE_2, deq.size());
351
352 // Allocations and assignment checking
353 i = SIZE_2 - 1L;
354 for (auto it = deq.crbegin(); it != deq.crend(); ++it, - -i) {
355 ASSERT_EQ(it->GetValue(), 1U) << "value of i: " << i;
356 ASSERT_EQ(it->GetString(), "1") << "value of i: " << i;
357 }
358
359 // Increasing size
360 constexpr size_t SIZE_4 = SIZE_2 << 1U;
361 deq.resize(SIZE_4, ComplexClass());
362
363 // Size checking
364 ASSERT_EQ(SIZE_4, deq.size());
365
366 // Allocations checking
367 auto it = deq.cbegin();
368 for (size_t j = 0; j < SIZE_4 / 2U; ++j, ++it) {
369 ASSERT_EQ(it->GetValue(), 1U) << "value of i: " << j;
370 ASSERT_EQ(it->GetString(), "1") << "value of i: " << j;
371 }
372 for (size_t j = SIZE_4 / 2U; j < SIZE_4; ++j, ++it) {
373 ASSERT_EQ(it->GetValue(), 0U) << "value of i: " << j;
374 ASSERT_EQ(it->GetString(), "0") << "value of i: " << j;
375 }
376
377 // Decreasing size
378 deq.resize(SIZE);
379
380 // Size checking
381 ASSERT_EQ(SIZE, deq.size());
382 deq.shrink_to_fit();
383 ASSERT_EQ(SIZE, deq.size());
384
385 // Allocations checking
386 i = 0;
387 for (auto tIt = deq.cbegin(); tIt != deq.cend(); ++tIt, ++i) {
388 ASSERT_EQ(tIt->GetValue(), 1U) << "value of i: " << i;
389 ASSERT_EQ(tIt->GetString(), "1") << "value of i: " << i;
390 }
391 }
392
TEST_F(ArenaAllocatorTest,LongRandomTest)393 TEST_F(ArenaAllocatorTest, LongRandomTest)
394 {
395 constexpr size_t SIZE = 3250000;
396 constexpr size_t HALF_SIZE = SIZE >> 1U;
397 constexpr size_t DOUBLE_SIZE = SIZE << 1U;
398 constexpr auto MAX_VAL = MaxValue<uint32_t>();
399
400 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
401 ArenaDeque<uint32_t> st(aa.Adapter());
402 size_t i = 0;
403
404 srand(GetSeed());
405
406 // Allocations
407 for (size_t j = 0; j < SIZE; ++j) {
408 st.push_back(GetRand() % MAX_VAL);
409 }
410
411 // Size checking
412 ASSERT_EQ(st.size(), SIZE);
413
414 // Allocations checking
415 srand(GetSeed());
416 i = 0;
417 for (unsigned int &tIt : st) {
418 ASSERT_EQ(tIt, GetRand() % MAX_VAL) << "value of i: " << i;
419 }
420
421 // Decreasing size
422 st.resize(HALF_SIZE);
423
424 // Size chcking
425 ASSERT_EQ(st.size(), HALF_SIZE);
426
427 // Allocations checking
428 srand(GetSeed());
429 i = 0;
430 for (unsigned int &tIt : st) {
431 ASSERT_EQ(tIt, GetRand() % MAX_VAL) << "value of i: " << i;
432 }
433
434 // Increasing size
435 st.resize(DOUBLE_SIZE, GetRand() % MAX_VAL);
436
437 // Allocations checking
438 srand(GetSeed());
439 auto it = st.cbegin();
440 for (i = 0; i < HALF_SIZE; ++it, ++i) {
441 ASSERT_EQ(*it, GetRand() % MAX_VAL) << "value of i: " << i;
442 }
443 for (uint32_t value = GetRand() % MAX_VAL; it != st.cend(); ++it, ++i) {
444 ASSERT_EQ(*it, value) << "value of i: " << i;
445 }
446
447 // Change values
448 srand(GetSeed() >> 1U);
449 for (unsigned int &tIt : st) {
450 tIt = GetRand() % MAX_VAL;
451 }
452
453 // Changes checking
454 srand(GetSeed() >> 1U);
455 i = 0;
456 for (auto tIt = st.cbegin(); tIt != st.cend(); ++tIt, ++i) {
457 ASSERT_EQ(*tIt, GetRand() % MAX_VAL) << "value of i: " << i;
458 }
459 }
460
TEST_F(ArenaAllocatorTest,LogAlignmentSmallSizesTest)461 TEST_F(ArenaAllocatorTest, LogAlignmentSmallSizesTest)
462 {
463 constexpr size_t MAX_SMALL_SIZE = 32;
464
465 for (size_t size = 1; size < MAX_SMALL_SIZE; ++size) {
466 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
467
468 for (Alignment align = LOG_ALIGN_MIN; align <= LOG_ALIGN_MAX;
469 align = static_cast<Alignment>(static_cast<size_t>(align) + 1)) {
470 void *ptr = aa.Alloc(size, align);
471 size_t mask = GetAlignmentInBytes(align) - 1L;
472
473 ASSERT_NE(ptr, nullptr);
474 ASSERT_EQ(reinterpret_cast<size_t>(ptr) & mask, 0U)
475 << "alignment: " << align << "addr: " << reinterpret_cast<size_t>(ptr);
476 }
477 }
478 }
479
TEST_F(ArenaAllocatorTest,LogAlignmentBigSizeTest)480 TEST_F(ArenaAllocatorTest, LogAlignmentBigSizeTest)
481 {
482 constexpr size_t SIZE = 0.3_KB;
483 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
484
485 for (Alignment align = LOG_ALIGN_MIN; align <= LOG_ALIGN_MAX;
486 align = static_cast<Alignment>(static_cast<size_t>(align) + 1)) {
487 void *ptr = aa.Alloc(SIZE, align);
488 size_t mask = GetAlignmentInBytes(align) - 1L;
489
490 ASSERT_NE(ptr, nullptr);
491 ASSERT_EQ(reinterpret_cast<size_t>(ptr) & mask, 0U)
492 << "alignment: " << align << "addr: " << reinterpret_cast<size_t>(ptr);
493 }
494 }
495
TEST_F(ArenaAllocatorTest,ArrayUINT16AlignmentTest)496 TEST_F(ArenaAllocatorTest, ArrayUINT16AlignmentTest)
497 {
498 AllocateWithAlignment<uint16_t>();
499 }
500
TEST_F(ArenaAllocatorTest,ArrayUINT32AlignmentTest)501 TEST_F(ArenaAllocatorTest, ArrayUINT32AlignmentTest)
502 {
503 AllocateWithAlignment<uint32_t>();
504 }
505
TEST_F(ArenaAllocatorTest,ArrayUINT64AlignmentTest)506 TEST_F(ArenaAllocatorTest, ArrayUINT64AlignmentTest)
507 {
508 AllocateWithAlignment<uint64_t>();
509 }
510
TEST_F(ArenaAllocatorTest,ArrayUINT16WithDiffAlignmentTest)511 TEST_F(ArenaAllocatorTest, ArrayUINT16WithDiffAlignmentTest)
512 {
513 AllocateWithDiffAlignment<uint16_t>();
514 }
515
TEST_F(ArenaAllocatorTest,ArrayUINT32WithDiffAlignmentTest)516 TEST_F(ArenaAllocatorTest, ArrayUINT32WithDiffAlignmentTest)
517 {
518 AllocateWithDiffAlignment<uint32_t>();
519 }
520
TEST_F(ArenaAllocatorTest,ArrayUINT64WithDiffAlignmentTest)521 TEST_F(ArenaAllocatorTest, ArrayUINT64WithDiffAlignmentTest)
522 {
523 AllocateWithDiffAlignment<uint64_t>();
524 }
525
TEST_F(ArenaAllocatorTest,FunctionNewTest)526 TEST_F(ArenaAllocatorTest, FunctionNewTest)
527 {
528 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
529 std::array<ComplexClass *, ARRAY_SIZE> arr {};
530
531 // Allocations
532 srand(GetSeed());
533 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
534 arr[i] = aa.New<ComplexClass>(GetRand() % MaxValue<size_t>());
535 }
536
537 // Allocations checking
538 srand(GetSeed());
539 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
540 ASSERT_NE(arr[i], nullptr);
541 size_t randomValue = GetRand() % MaxValue<size_t>();
542 ASSERT_EQ(arr[i]->GetValue(), randomValue);
543 ASSERT_EQ(arr[i]->GetString(), std::to_string(randomValue));
544 }
545
546 // Change values
547 srand(GetSeed() >> 1U);
548 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
549 arr[i]->SetValue(GetRand() % MaxValue<size_t>());
550 }
551
552 // Changes checking
553 srand(GetSeed() >> 1U);
554 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
555 size_t randomValue = GetRand() % MaxValue<size_t>();
556 ASSERT_EQ(arr[i]->GetValue(), randomValue);
557 ASSERT_EQ(arr[i]->GetString(), std::to_string(randomValue));
558 }
559 }
560
TEST_F(ArenaAllocatorTest,ResizeTest)561 TEST_F(ArenaAllocatorTest, ResizeTest)
562 {
563 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
564 static constexpr size_t ALLOC_COUNT = 1000;
565 static constexpr size_t INIT_VAL = 0xdeadbeef;
566 size_t *firstVar = aa.New<size_t>(INIT_VAL);
567 {
568 size_t initSize = aa.GetAllocatedSize();
569 for (size_t i = 0; i < ALLOC_COUNT; i++) {
570 [[maybe_unused]] void *tmp = aa.Alloc(sizeof(size_t));
571 }
572 EXPECT_DEATH(aa.Resize(aa.GetAllocatedSize() + 1U), "");
573 aa.Resize(initSize);
574 ASSERT_EQ(aa.GetAllocatedSize(), initSize);
575 }
576 ASSERT_EQ(*firstVar, INIT_VAL);
577 }
578
TEST_F(ArenaAllocatorTest,ResizeWrapperTest)579 TEST_F(ArenaAllocatorTest, ResizeWrapperTest)
580 {
581 static constexpr size_t VECTOR_SIZE = 1000;
582 ArenaAllocator aa(SpaceType::SPACE_TYPE_INTERNAL);
583 size_t oldSize = aa.GetAllocatedSize();
584 {
585 ArenaResizeWrapper<false> wrapper(&aa);
586 ArenaVector<size_t> vector(aa.Adapter());
587 for (size_t i = 0; i < VECTOR_SIZE; i++) {
588 vector.push_back(i);
589 }
590 }
591 ASSERT_EQ(oldSize, aa.GetAllocatedSize());
592 }
593
594 } // namespace ark
595