diff options
author | Lang Hames <lhames@gmail.com> | 2014-10-19 19:36:33 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2014-10-19 19:36:33 +0000 |
commit | b27a3b0d43adbff9bb3ecb9afaa87ab420032735 (patch) | |
tree | 05e70be7f4c47d03c226f04fc5c1717396525f0c /llvm/unittests/ADT/DenseSetTest.cpp | |
parent | 0c4b230b32ba04494deeabc4415f7248aa455068 (diff) | |
download | bcm5719-llvm-b27a3b0d43adbff9bb3ecb9afaa87ab420032735.tar.gz bcm5719-llvm-b27a3b0d43adbff9bb3ecb9afaa87ab420032735.zip |
[ADT] Add a 'find_as' operation to DenseSet.
This operation is analogous to its counterpart in DenseMap: It allows lookup
via cheap-to-construct keys (provided that getHashValue and isEqual are
implemented for the cheap key-type in the DenseMapInfo specialization).
Thanks to Chandler for the review.
llvm-svn: 220168
Diffstat (limited to 'llvm/unittests/ADT/DenseSetTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/DenseSetTest.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp index 154c5892d5f..5952353034f 100644 --- a/llvm/unittests/ADT/DenseSetTest.cpp +++ b/llvm/unittests/ADT/DenseSetTest.cpp @@ -27,4 +27,42 @@ TEST_F(DenseSetTest, DoubleEntrySetTest) { EXPECT_EQ(0u, set.count(2)); } +struct TestDenseSetInfo { + 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; + } +}; + +TEST(DenseSetCustomTest, FindAsTest) { + DenseSet<unsigned, TestDenseSetInfo> set; + set.insert(0); + set.insert(1); + set.insert(2); + + // Size tests + EXPECT_EQ(3u, set.size()); + + // Normal lookup tests + EXPECT_EQ(1u, set.count(1)); + EXPECT_EQ(0u, *set.find(0)); + EXPECT_EQ(1u, *set.find(1)); + EXPECT_EQ(2u, *set.find(2)); + EXPECT_TRUE(set.find(3) == set.end()); + + // find_as() tests + EXPECT_EQ(0u, *set.find_as("a")); + EXPECT_EQ(1u, *set.find_as("b")); + EXPECT_EQ(2u, *set.find_as("c")); + EXPECT_TRUE(set.find_as("d") == set.end()); +} + } |