Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/DenseSetTest.cpp - DenseSet unit tests --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "gtest/gtest.h" |
Craig Topper | bc40d7e | 2012-09-15 18:45:38 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/DenseSet.h" |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace llvm; |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // Test fixture |
| 18 | class DenseSetTest : public testing::Test { |
| 19 | }; |
| 20 | |
| 21 | // Test hashing with a set of only two entries. |
| 22 | TEST_F(DenseSetTest, DoubleEntrySetTest) { |
| 23 | llvm::DenseSet<unsigned> set(2); |
| 24 | set.insert(0); |
| 25 | set.insert(1); |
| 26 | // Original failure was an infinite loop in this call: |
David Blaikie | 7c8d139 | 2014-06-20 19:54:13 +0000 | [diff] [blame] | 27 | EXPECT_EQ(0u, set.count(2)); |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Lang Hames | b27a3b0 | 2014-10-19 19:36:33 +0000 | [diff] [blame] | 30 | struct TestDenseSetInfo { |
| 31 | static inline unsigned getEmptyKey() { return ~0; } |
| 32 | static inline unsigned getTombstoneKey() { return ~0U - 1; } |
| 33 | static unsigned getHashValue(const unsigned& Val) { return Val * 37U; } |
| 34 | static unsigned getHashValue(const char* Val) { |
| 35 | return (unsigned)(Val[0] - 'a') * 37U; |
| 36 | } |
| 37 | static bool isEqual(const unsigned& LHS, const unsigned& RHS) { |
| 38 | return LHS == RHS; |
| 39 | } |
| 40 | static bool isEqual(const char* LHS, const unsigned& RHS) { |
| 41 | return (unsigned)(LHS[0] - 'a') == RHS; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | TEST(DenseSetCustomTest, FindAsTest) { |
| 46 | DenseSet<unsigned, TestDenseSetInfo> set; |
| 47 | set.insert(0); |
| 48 | set.insert(1); |
| 49 | set.insert(2); |
| 50 | |
| 51 | // Size tests |
| 52 | EXPECT_EQ(3u, set.size()); |
| 53 | |
| 54 | // Normal lookup tests |
| 55 | EXPECT_EQ(1u, set.count(1)); |
| 56 | EXPECT_EQ(0u, *set.find(0)); |
| 57 | EXPECT_EQ(1u, *set.find(1)); |
| 58 | EXPECT_EQ(2u, *set.find(2)); |
| 59 | EXPECT_TRUE(set.find(3) == set.end()); |
| 60 | |
| 61 | // find_as() tests |
| 62 | EXPECT_EQ(0u, *set.find_as("a")); |
| 63 | EXPECT_EQ(1u, *set.find_as("b")); |
| 64 | EXPECT_EQ(2u, *set.find_as("c")); |
| 65 | EXPECT_TRUE(set.find_as("d") == set.end()); |
| 66 | } |
| 67 | |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 68 | } |