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"
11 #include "llvm/ADT/DenseSet.h"
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.
TEST_F(DenseSetTest,DoubleEntrySetTest)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:
27 EXPECT_EQ(0u, set.count(2));
28 }
29
30 struct TestDenseSetInfo {
getEmptyKey__anon7cd309510111::TestDenseSetInfo31 static inline unsigned getEmptyKey() { return ~0; }
getTombstoneKey__anon7cd309510111::TestDenseSetInfo32 static inline unsigned getTombstoneKey() { return ~0U - 1; }
getHashValue__anon7cd309510111::TestDenseSetInfo33 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
getHashValue__anon7cd309510111::TestDenseSetInfo34 static unsigned getHashValue(const char* Val) {
35 return (unsigned)(Val[0] - 'a') * 37U;
36 }
isEqual__anon7cd309510111::TestDenseSetInfo37 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
38 return LHS == RHS;
39 }
isEqual__anon7cd309510111::TestDenseSetInfo40 static bool isEqual(const char* LHS, const unsigned& RHS) {
41 return (unsigned)(LHS[0] - 'a') == RHS;
42 }
43 };
44
TEST(DenseSetCustomTest,FindAsTest)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
68 }
69