diff options
Diffstat (limited to 'llvm/unittests/ADT/StringMapTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/StringMapTest.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index 4038d4d..5f0c83d 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -75,6 +75,16 @@ const char* StringMapTest::testKeyFirst = testKey; size_t StringMapTest::testKeyLength = sizeof(testKey) - 1; const std::string StringMapTest::testKeyStr(testKey); +struct CountCopyAndMove { + CountCopyAndMove() = default; + CountCopyAndMove(const CountCopyAndMove &) { copy = 1; } + CountCopyAndMove(CountCopyAndMove &&) { move = 1; } + void operator=(const CountCopyAndMove &) { ++copy; } + void operator=(CountCopyAndMove &&) { ++move; } + int copy = 0; + int move = 0; +}; + // Empty map tests. TEST_F(StringMapTest, EmptyMapTest) { assertEmptyMap(); @@ -269,6 +279,27 @@ TEST_F(StringMapTest, InsertRehashingPairTest) { EXPECT_EQ(42u, It->second); } +TEST_F(StringMapTest, InsertOrAssignTest) { + struct A : CountCopyAndMove { + A(int v) : v(v) {} + int v; + }; + StringMap<A> t(0); + + auto try1 = t.insert_or_assign("A", A(1)); + EXPECT_TRUE(try1.second); + EXPECT_EQ(1, try1.first->second.v); + EXPECT_EQ(1, try1.first->second.move); + + auto try2 = t.insert_or_assign("A", A(2)); + EXPECT_FALSE(try2.second); + EXPECT_EQ(2, try2.first->second.v); + EXPECT_EQ(2, try1.first->second.move); + + EXPECT_EQ(try1.first, try2.first); + EXPECT_EQ(0, try1.first->second.copy); +} + TEST_F(StringMapTest, IterMapKeys) { StringMap<int> Map; Map["A"] = 1; |