1 //===- llvm/unittest/ADT/DenseSetTest.cpp - DenseSet unit tests --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/DenseSet.h"
18 class DenseSetTest : public testing::Test {
21 // Test hashing with a set of only two entries.
22 TEST_F(DenseSetTest, DoubleEntrySetTest) {
23 llvm::DenseSet<unsigned> set(2);
26 // Original failure was an infinite loop in this call:
27 EXPECT_EQ(0u, set.count(2));
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;
37 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
40 static bool isEqual(const char* LHS, const unsigned& RHS) {
41 return (unsigned)(LHS[0] - 'a') == RHS;
45 TEST(DenseSetCustomTest, FindAsTest) {
46 DenseSet<unsigned, TestDenseSetInfo> set;
52 EXPECT_EQ(3u, set.size());
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());
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());