1 //===--- unittest/Support/ArrayRecyclerTest.cpp ---------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/ArrayRecycler.h"
10 #include "llvm/Support/Allocator.h"
11 #include "gtest/gtest.h"
22 typedef ArrayRecycler
<Object
> ARO
;
24 TEST(ArrayRecyclerTest
, Capacity
) {
25 // Capacity size should never be 0.
26 ARO::Capacity Cap
= ARO::Capacity::get(0);
27 EXPECT_LT(0u, Cap
.getSize());
29 size_t PrevSize
= Cap
.getSize();
30 for (unsigned N
= 1; N
!= 100; ++N
) {
31 Cap
= ARO::Capacity::get(N
);
32 EXPECT_LE(N
, Cap
.getSize());
34 EXPECT_EQ(PrevSize
, Cap
.getSize());
36 EXPECT_LT(PrevSize
, Cap
.getSize());
37 PrevSize
= Cap
.getSize();
40 // Check that the buckets are monotonically increasing.
41 Cap
= ARO::Capacity::get(0);
42 PrevSize
= Cap
.getSize();
43 for (unsigned N
= 0; N
!= 20; ++N
) {
45 EXPECT_LT(PrevSize
, Cap
.getSize());
46 PrevSize
= Cap
.getSize();
50 TEST(ArrayRecyclerTest
, Basics
) {
51 BumpPtrAllocator Allocator
;
52 ArrayRecycler
<Object
> DUT
;
54 ARO::Capacity Cap
= ARO::Capacity::get(8);
55 Object
*A1
= DUT
.allocate(Cap
, Allocator
);
59 Object
*A2
= DUT
.allocate(Cap
, Allocator
);
63 Object
*A3
= DUT
.allocate(Cap
, Allocator
);
67 EXPECT_EQ(21, A1
[0].Num
);
68 EXPECT_EQ(17, A1
[7].Num
);
69 EXPECT_EQ(121, A2
[0].Num
);
70 EXPECT_EQ(117, A2
[7].Num
);
71 EXPECT_EQ(221, A3
[0].Num
);
72 EXPECT_EQ(217, A3
[7].Num
);
74 DUT
.deallocate(Cap
, A2
);
76 // Check that deallocation didn't clobber anything.
77 EXPECT_EQ(21, A1
[0].Num
);
78 EXPECT_EQ(17, A1
[7].Num
);
79 EXPECT_EQ(221, A3
[0].Num
);
80 EXPECT_EQ(217, A3
[7].Num
);
83 Object
*A2x
= DUT
.allocate(Cap
, Allocator
);
86 DUT
.deallocate(Cap
, A2x
);
87 DUT
.deallocate(Cap
, A1
);
88 DUT
.deallocate(Cap
, A3
);
90 // Objects are not required to be recycled in reverse deallocation order, but
91 // that is what the current implementation does.
92 Object
*A3x
= DUT
.allocate(Cap
, Allocator
);
94 Object
*A1x
= DUT
.allocate(Cap
, Allocator
);
96 Object
*A2y
= DUT
.allocate(Cap
, Allocator
);
99 // Back to allocation from the BumpPtrAllocator.
100 Object
*A4
= DUT
.allocate(Cap
, Allocator
);
105 DUT
.clear(Allocator
);
108 } // end anonymous namespace