Skip to content

Commit 74ea9a8

Browse files
committed
refactor the test and fix alignment issue
1 parent e9f39e8 commit 74ea9a8

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

llvm/unittests/ADT/CountCopyAndMove.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using namespace llvm;
1212

13+
int CountCopyAndMove::Constructions = 0;
1314
int CountCopyAndMove::CopyConstructions = 0;
1415
int CountCopyAndMove::CopyAssignments = 0;
1516
int CountCopyAndMove::MoveConstructions = 0;

llvm/unittests/ADT/CountCopyAndMove.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
namespace llvm {
1313

1414
struct CountCopyAndMove {
15+
static int Constructions;
1516
static int CopyConstructions;
1617
static int CopyAssignments;
1718
static int MoveConstructions;
1819
static int MoveAssignments;
1920
static int Destructions;
2021
int val;
2122

22-
CountCopyAndMove() = default;
23-
explicit CountCopyAndMove(int val) : val(val) {}
23+
CountCopyAndMove() { ++Constructions; }
24+
explicit CountCopyAndMove(int val) : val(val) { ++Constructions; }
2425
CountCopyAndMove(const CountCopyAndMove &other) : val(other.val) {
2526
++CopyConstructions;
2627
}
@@ -40,13 +41,18 @@ struct CountCopyAndMove {
4041
~CountCopyAndMove() { ++Destructions; }
4142

4243
static void ResetCounts() {
44+
Constructions = 0;
4345
CopyConstructions = 0;
4446
CopyAssignments = 0;
4547
MoveConstructions = 0;
4648
MoveAssignments = 0;
4749
Destructions = 0;
4850
}
4951

52+
static int TotalConstructions() {
53+
return Constructions + MoveConstructions + CopyConstructions;
54+
}
55+
5056
static int TotalCopies() { return CopyConstructions + CopyAssignments; }
5157

5258
static int TotalMoves() { return MoveConstructions + MoveAssignments; }

llvm/unittests/ADT/FunctionExtrasTest.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/ADT/FunctionExtras.h"
10+
#include "CountCopyAndMove.h"
1011
#include "gtest/gtest.h"
1112

1213
#include <memory>
@@ -332,20 +333,15 @@ TEST(UniqueFunctionTest, InlineStorageWorks) {
332333
// Check that the moved-from captured state is properly destroyed during
333334
// move construction/assignment.
334335
TEST(UniqueFunctionTest, MovedFromStateIsDestroyedCorrectly) {
335-
static int NumOfMovesCalled = 0;
336-
static int NumOfDestructorsCalled = 0;
337-
struct State {
338-
State() = default;
339-
State(State &&) { ++NumOfMovesCalled; }
340-
~State() { ++NumOfDestructorsCalled; }
341-
};
336+
CountCopyAndMove::ResetCounts();
342337
{
343-
unique_function<void()> CapturingFunction{[state = State{}] {}};
338+
unique_function<void()> CapturingFunction{
339+
[Counter = CountCopyAndMove{}] {}};
344340
unique_function<void()> CapturingFunctionMoved{
345341
std::move(CapturingFunction)};
346342
}
347-
printf("%i, %i\n", NumOfMovesCalled, NumOfDestructorsCalled);
348-
EXPECT_EQ(NumOfDestructorsCalled, 1 + NumOfMovesCalled);
343+
EXPECT_EQ(CountCopyAndMove::TotalConstructions(),
344+
CountCopyAndMove::Destructions);
349345
}
350346

351347
} // anonymous namespace

0 commit comments

Comments
 (0)