summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/ADT/StringMapTest.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2017-03-21 20:27:36 +0000
committerZachary Turner <zturner@google.com>2017-03-21 20:27:36 +0000
commite0c2fec12594e3b829a3646b0be34e4b0e6f817b (patch)
tree932c881a2315a6cf3c16dc2eb1d6baa9dc84c10a /llvm/unittests/ADT/StringMapTest.cpp
parentadb561d7284b88891bb0f44293b6af56ff8d4662 (diff)
downloadbcm5719-llvm-e0c2fec12594e3b829a3646b0be34e4b0e6f817b.tar.gz
bcm5719-llvm-e0c2fec12594e3b829a3646b0be34e4b0e6f817b.zip
Improve StringMap iterator support.
StringMap's iterators did not support LLVM's iterator_facade_base, which made it unusable in various STL algorithms or with some of our range adapters. This patch makes both StringMapConstIterator as well as StringMapIterator support iterator_facade_base. With this in place, it is easy to make an iterator adapter that iterates over only keys, and whose value_type is StringRef. So I add StringMapKeyIterator as well, and provide the method StringMap::keys() that returns a range that can be iterated. Differential Revision: https://reviews.llvm.org/D31171 llvm-svn: 298436
Diffstat (limited to 'llvm/unittests/ADT/StringMapTest.cpp')
-rw-r--r--llvm/unittests/ADT/StringMapTest.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp
index 911c72d7496..d2b1c31805e 100644
--- a/llvm/unittests/ADT/StringMapTest.cpp
+++ b/llvm/unittests/ADT/StringMapTest.cpp
@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/DataTypes.h"
#include "gtest/gtest.h"
@@ -269,6 +270,34 @@ TEST_F(StringMapTest, InsertRehashingPairTest) {
EXPECT_EQ(42u, It->second);
}
+TEST_F(StringMapTest, IterMapKeys) {
+ StringMap<int> Map;
+ Map["A"] = 1;
+ Map["B"] = 2;
+ Map["C"] = 3;
+ Map["D"] = 3;
+
+ auto Keys = to_vector<4>(Map.keys());
+ std::sort(Keys.begin(), Keys.end());
+
+ SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
+ EXPECT_EQ(Expected, Keys);
+}
+
+TEST_F(StringMapTest, IterSetKeys) {
+ StringSet<> Set;
+ Set.insert("A");
+ Set.insert("B");
+ Set.insert("C");
+ Set.insert("D");
+
+ auto Keys = to_vector<4>(Set.keys());
+ std::sort(Keys.begin(), Keys.end());
+
+ SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
+ EXPECT_EQ(Expected, Keys);
+}
+
// Create a non-default constructable value
struct StringMapTestStruct {
StringMapTestStruct(int i) : i(i) {}
OpenPOWER on IntegriCloud