[llvm] [ADT] Add C++17-style insert_or_assign for DenseMap (PR #94151)
David Blaikie via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 4 11:14:33 PDT 2024
================
@@ -484,21 +461,55 @@ TEST(DenseMapCustomTest, ReserveTest) {
DenseMap<int, CountCopyAndMove> Map;
Map.reserve(Size);
unsigned MemorySize = Map.getMemorySize();
- CountCopyAndMove::Copy = 0;
- CountCopyAndMove::Move = 0;
+ CountCopyAndMove::ResetCounts();
for (int i = 0; i < Size; ++i)
Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct,
std::forward_as_tuple(i),
std::forward_as_tuple()));
// Check that we didn't grow
EXPECT_EQ(MemorySize, Map.getMemorySize());
// Check that move was called the expected number of times
- EXPECT_EQ(Size, CountCopyAndMove::Move);
+ EXPECT_EQ(Size, CountCopyAndMove::TotalMoves());
// Check that no copy occurred
- EXPECT_EQ(0, CountCopyAndMove::Copy);
+ EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
}
}
+TEST(DenseMapCustomTest, InsertOrAssignTest) {
+ DenseMap<int, CountCopyAndMove> Map;
+ CountCopyAndMove::ResetCounts();
+
+ CountCopyAndMove val1(1);
+ auto try0 = Map.insert_or_assign(0, val1);
+ EXPECT_TRUE(try0.second);
+ EXPECT_EQ(0, CountCopyAndMove::TotalMoves());
+ EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
+ EXPECT_EQ(0, CountCopyAndMove::CopyAssignments);
+
+ auto try1 = Map.insert_or_assign(0, val1);
+ EXPECT_FALSE(try1.second);
+ EXPECT_EQ(0, CountCopyAndMove::TotalMoves());
+ EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
+ EXPECT_EQ(1, CountCopyAndMove::CopyAssignments);
+
+ int key2 = 2;
+ CountCopyAndMove val2(2);
+ auto try2 = Map.insert_or_assign(key2, std::move(val2));
+ EXPECT_TRUE(try2.second);
+ EXPECT_EQ(1, CountCopyAndMove::MoveConstructions);
+ EXPECT_EQ(0, CountCopyAndMove::MoveAssignments);
+ EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
+ EXPECT_EQ(1, CountCopyAndMove::CopyAssignments);
+
+ CountCopyAndMove val3(3);
+ auto try3 = Map.insert_or_assign(key2, std::move(val3));
+ EXPECT_FALSE(try3.second);
+ EXPECT_EQ(1, CountCopyAndMove::MoveConstructions);
+ EXPECT_EQ(1, CountCopyAndMove::MoveAssignments);
+ EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
+ EXPECT_EQ(1, CountCopyAndMove::CopyAssignments);
+}
----------------
dwblaikie wrote:
Probably easier to read if the counts are reset between each portion of the test? so it's clear the expectations are about what the immediate previous operation did, rather than the total of all the things that came before?
https://github.com/llvm/llvm-project/pull/94151
More information about the llvm-commits
mailing list