diff options
author | Talin <viridia@gmail.com> | 2012-01-30 06:55:43 +0000 |
---|---|---|
committer | Talin <viridia@gmail.com> | 2012-01-30 06:55:43 +0000 |
commit | babd5980d8a8b4aad9814212a269f6197ebf1f2e (patch) | |
tree | bb380373fd4dda5bb7209a58e477aa4d97be5bde /unittests | |
parent | 36c744ff10a0963d551cd6c4f439a4147bab8207 (diff) |
DenseMap::find_as() and unit tests.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149229 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/ADT/DenseMapTest.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/unittests/ADT/DenseMapTest.cpp b/unittests/ADT/DenseMapTest.cpp index afac651a6b..e0ee7782cc 100644 --- a/unittests/ADT/DenseMapTest.cpp +++ b/unittests/ADT/DenseMapTest.cpp @@ -176,4 +176,45 @@ TEST_F(DenseMapTest, ConstIteratorTest) { EXPECT_TRUE(cit == cit2); } +// 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 { + static inline unsigned getEmptyKey() { return ~0; } + static inline unsigned getTombstoneKey() { return ~0U - 1; } + static unsigned getHashValue(const unsigned& Val) { return Val * 37U; } + static unsigned getHashValue(const char* Val) { + return (unsigned)(Val[0] - 'a') * 37U; + } + static bool isEqual(const unsigned& LHS, const unsigned& RHS) { + return LHS == RHS; + } + static bool isEqual(const char* LHS, const unsigned& RHS) { + return (unsigned)(LHS[0] - 'a') == RHS; + } +}; + +// find_as() tests +TEST_F(DenseMapTest, FindAsTest) { + DenseMap<unsigned, unsigned, TestDenseMapInfo> map; + map[0] = 1; + map[1] = 2; + map[2] = 3; + + // Size tests + EXPECT_EQ(3u, map.size()); + + // Normal lookup tests + EXPECT_EQ(1, map.count(1)); + EXPECT_EQ(1u, map.find(0)->second); + EXPECT_EQ(2u, map.find(1)->second); + EXPECT_EQ(3u, map.find(2)->second); + EXPECT_TRUE(map.find(3) == map.end()); + + // find_as() tests + EXPECT_EQ(1u, map.find_as("a")->second); + EXPECT_EQ(2u, map.find_as("b")->second); + EXPECT_EQ(3u, map.find_as("c")->second); + EXPECT_TRUE(map.find_as("d") == map.end()); +} + } |