aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/DenseMapTest.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2015-06-24 10:06:29 +0000
committerChandler Carruth <chandlerc@gmail.com>2015-06-24 10:06:29 +0000
commit595348228569c587e14bf8cea1c4fdf1b503c104 (patch)
treef4fc8854cae28a3380264d33eed1b5ddcec734a0 /llvm/unittests/ADT/DenseMapTest.cpp
parente0cf3b9a3ca8d80d313d1e2e5b8c70d73d10e933 (diff)
downloadllvm-595348228569c587e14bf8cea1c4fdf1b503c104.zip
llvm-595348228569c587e14bf8cea1c4fdf1b503c104.tar.gz
llvm-595348228569c587e14bf8cea1c4fdf1b503c104.tar.bz2
[ADT] Teach DenseMap to support StringRef keys.
While often you want to use something specialized like StringMap, when the strings already have persistent storage a normal densemap over them can be more efficient. This can't go into StringRef.h because of really obnoxious header chains from the hashing code to the endian detection code to CPU feature detection code to StringMap. llvm-svn: 240528
Diffstat (limited to 'llvm/unittests/ADT/DenseMapTest.cpp')
-rw-r--r--llvm/unittests/ADT/DenseMapTest.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index 9780777..2c6fe35 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -323,6 +323,31 @@ TYPED_TEST(DenseMapTest, ConstIteratorTest) {
EXPECT_TRUE(cit == cit2);
}
+// Make sure DenseMap works with StringRef keys.
+TEST(DenseMapCustomTest, StringRefTest) {
+ DenseMap<StringRef, int> M;
+
+ M["a"] = 1;
+ M["b"] = 2;
+ M["c"] = 3;
+
+ EXPECT_EQ(3u, M.size());
+ EXPECT_EQ(1, M.lookup("a"));
+ EXPECT_EQ(2, M.lookup("b"));
+ EXPECT_EQ(3, M.lookup("c"));
+
+ EXPECT_EQ(0, M.lookup("q"));
+
+ // Test the empty string, spelled various ways.
+ EXPECT_EQ(0, M.lookup(""));
+ EXPECT_EQ(0, M.lookup(StringRef()));
+ EXPECT_EQ(0, M.lookup(StringRef("a", 0)));
+ M[""] = 42;
+ EXPECT_EQ(42, M.lookup(""));
+ EXPECT_EQ(42, M.lookup(StringRef()));
+ EXPECT_EQ(42, M.lookup(StringRef("a", 0)));
+}
+
// Key traits that allows lookup with either an unsigned or char* key;
// In the latter case, "a" == 0, "b" == 1 and so on.
struct TestDenseMapInfo {