DenseSetTest.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "gtest/gtest.h"
  10. #include "llvm/ADT/DenseSet.h"
  11. using namespace llvm;
  12. namespace {
  13. // Test fixture
  14. class DenseSetTest : public testing::Test {
  15. };
  16. // Test hashing with a set of only two entries.
  17. TEST_F(DenseSetTest, DoubleEntrySetTest) {
  18. llvm::DenseSet<unsigned> set(2);
  19. set.insert(0);
  20. set.insert(1);
  21. // Original failure was an infinite loop in this call:
  22. EXPECT_EQ(0u, set.count(2));
  23. }
  24. struct TestDenseSetInfo {
  25. static inline unsigned getEmptyKey() { return ~0; }
  26. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  27. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  28. static unsigned getHashValue(const char* Val) {
  29. return (unsigned)(Val[0] - 'a') * 37U;
  30. }
  31. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  32. return LHS == RHS;
  33. }
  34. static bool isEqual(const char* LHS, const unsigned& RHS) {
  35. return (unsigned)(LHS[0] - 'a') == RHS;
  36. }
  37. };
  38. TEST(DenseSetCustomTest, FindAsTest) {
  39. DenseSet<unsigned, TestDenseSetInfo> set;
  40. set.insert(0);
  41. set.insert(1);
  42. set.insert(2);
  43. // Size tests
  44. EXPECT_EQ(3u, set.size());
  45. // Normal lookup tests
  46. EXPECT_EQ(1u, set.count(1));
  47. EXPECT_EQ(0u, *set.find(0));
  48. EXPECT_EQ(1u, *set.find(1));
  49. EXPECT_EQ(2u, *set.find(2));
  50. EXPECT_TRUE(set.find(3) == set.end());
  51. // find_as() tests
  52. EXPECT_EQ(0u, *set.find_as("a"));
  53. EXPECT_EQ(1u, *set.find_as("b"));
  54. EXPECT_EQ(2u, *set.find_as("c"));
  55. EXPECT_TRUE(set.find_as("d") == set.end());
  56. }
  57. }