Skip to content

Commit e6fab53

Browse files
committed
Add test
1 parent 7ff8b49 commit e6fab53

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

llvm/unittests/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ add_llvm_unittest(SupportTests
6969
PerThreadBumpPtrAllocatorTest.cpp
7070
ProcessTest.cpp
7171
ProgramTest.cpp
72+
RecyclerTest.cpp
7273
RegexTest.cpp
7374
ReverseIterationTest.cpp
7475
ReplaceFileTest.cpp
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- unittest/Support/RecyclerTest.cpp --------------------------------===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Support/Recycler.h"
10+
#include "llvm/Support/AllocatorBase.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace llvm;
14+
15+
namespace {
16+
17+
struct Object8 {
18+
char Data[8];
19+
};
20+
21+
class DecoratedMallocAllocator : public MallocAllocator {
22+
public:
23+
int DeallocCount = 0;
24+
25+
template <typename T> void Deallocate(T *Ptr) {
26+
DeallocCount++;
27+
MallocAllocator::Deallocate(Ptr);
28+
}
29+
};
30+
31+
TEST(RecyclerTest, MoveConstructor) {
32+
DecoratedMallocAllocator Allocator;
33+
Recycler<Object8> R;
34+
Object8 *A1 = R.Allocate(Allocator);
35+
Object8 *A2 = R.Allocate(Allocator);
36+
R.Deallocate(Allocator, A1);
37+
R.Deallocate(Allocator, A2);
38+
Recycler<Object8> R2(std::move(R));
39+
Object8 *A3 = R2.Allocate(Allocator);
40+
R2.Deallocate(Allocator, A3);
41+
R.clear(Allocator); // Should not deallocate anything as it was moved from.
42+
EXPECT_EQ(Allocator.DeallocCount, 0);
43+
R2.clear(Allocator);
44+
EXPECT_EQ(Allocator.DeallocCount, 2);
45+
}
46+
47+
} // end anonymous namespace

0 commit comments

Comments
 (0)