diff options
author | David Blaikie <dblaikie@gmail.com> | 2014-06-19 20:08:56 +0000 |
---|---|---|
committer | David Blaikie <dblaikie@gmail.com> | 2014-06-19 20:08:56 +0000 |
commit | 37700dc057ab65c68d86667da1c3d2741f142026 (patch) | |
tree | d103a7bfd19de72d630e421d84f72a6d90a3ef87 /llvm/unittests/ADT/StringMapTest.cpp | |
parent | Since we're using DW_AT_string rather than DW_AT_strp for debug_info (diff) | |
download | llvm-project-37700dc057ab65c68d86667da1c3d2741f142026.tar.gz llvm-project-37700dc057ab65c68d86667da1c3d2741f142026.tar.bz2 llvm-project-37700dc057ab65c68d86667da1c3d2741f142026.zip |
Add StringMap::insert(pair) consistent with the standard associative container concept.
Patch by Agustín Bergé.
llvm-svn: 211309
Diffstat (limited to 'llvm/unittests/ADT/StringMapTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/StringMapTest.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index 70eec873ed23..215d3dfa02e5 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -10,6 +10,7 @@ #include "gtest/gtest.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/DataTypes.h" +#include <tuple> using namespace llvm; namespace { @@ -203,6 +204,43 @@ TEST_F(StringMapTest, InsertTest) { assertSingleItemMap(); } +// Test insert(pair<K, V>) method +TEST_F(StringMapTest, InsertPairTest) { + bool Inserted; + StringMap<uint32_t>::iterator NewIt; + std::tie(NewIt, Inserted) = + testMap.insert(std::make_pair(testKeyFirst, testValue)); + EXPECT_EQ(1u, testMap.size()); + EXPECT_EQ(testValue, testMap[testKeyFirst]); + EXPECT_EQ(testKeyFirst, NewIt->first()); + EXPECT_EQ(testValue, NewIt->second); + EXPECT_TRUE(Inserted); + + StringMap<uint32_t>::iterator ExistingIt; + std::tie(ExistingIt, Inserted) = + testMap.insert(std::make_pair(testKeyFirst, testValue + 1)); + EXPECT_EQ(1u, testMap.size()); + EXPECT_EQ(testValue, testMap[testKeyFirst]); + EXPECT_FALSE(Inserted); + EXPECT_EQ(NewIt, ExistingIt); +} + +// Test insert(pair<K, V>) method when rehashing occurs +TEST_F(StringMapTest, InsertRehashingPairTest) { + // Check that the correct iterator is returned when the inserted element is + // moved to a different bucket during internal rehashing. This depends on + // the particular key, and the implementation of StringMap and HashString. + // Changes to those might result in this test not actually checking that. + StringMap<uint32_t> t(1); + EXPECT_EQ(1u, t.getNumBuckets()); + + StringMap<uint32_t>::iterator It = + t.insert(std::make_pair("abcdef", 42)).first; + EXPECT_EQ(2u, t.getNumBuckets()); + EXPECT_EQ("abcdef", It->first()); + EXPECT_EQ(42u, It->second); +} + // Create a non-default constructable value struct StringMapTestStruct { StringMapTestStruct(int i) : i(i) {} |