ImmutableSetTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //===----------- ImmutableSetTest.cpp - ImmutableSet unit tests ------------===//
  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/ImmutableSet.h"
  11. using namespace llvm;
  12. namespace {
  13. class ImmutableSetTest : public testing::Test {
  14. protected:
  15. // for callback tests
  16. static char buffer[10];
  17. struct MyIter {
  18. int counter;
  19. char *ptr;
  20. MyIter() : counter(0), ptr(buffer) {
  21. for (unsigned i=0; i<sizeof(buffer);++i) buffer[i]='\0';
  22. }
  23. void operator()(char c) {
  24. *ptr++ = c;
  25. ++counter;
  26. }
  27. };
  28. };
  29. char ImmutableSetTest::buffer[10];
  30. TEST_F(ImmutableSetTest, EmptyIntSetTest) {
  31. ImmutableSet<int>::Factory f;
  32. EXPECT_TRUE(f.getEmptySet() == f.getEmptySet());
  33. EXPECT_FALSE(f.getEmptySet() != f.getEmptySet());
  34. EXPECT_TRUE(f.getEmptySet().isEmpty());
  35. ImmutableSet<int> S = f.getEmptySet();
  36. EXPECT_EQ(0u, S.getHeight());
  37. EXPECT_TRUE(S.begin() == S.end());
  38. EXPECT_FALSE(S.begin() != S.end());
  39. }
  40. TEST_F(ImmutableSetTest, OneElemIntSetTest) {
  41. ImmutableSet<int>::Factory f;
  42. ImmutableSet<int> S = f.getEmptySet();
  43. ImmutableSet<int> S2 = f.add(S, 3);
  44. EXPECT_TRUE(S.isEmpty());
  45. EXPECT_FALSE(S2.isEmpty());
  46. EXPECT_FALSE(S == S2);
  47. EXPECT_TRUE(S != S2);
  48. EXPECT_FALSE(S.contains(3));
  49. EXPECT_TRUE(S2.contains(3));
  50. EXPECT_FALSE(S2.begin() == S2.end());
  51. EXPECT_TRUE(S2.begin() != S2.end());
  52. ImmutableSet<int> S3 = f.add(S, 2);
  53. EXPECT_TRUE(S.isEmpty());
  54. EXPECT_FALSE(S3.isEmpty());
  55. EXPECT_FALSE(S == S3);
  56. EXPECT_TRUE(S != S3);
  57. EXPECT_FALSE(S.contains(2));
  58. EXPECT_TRUE(S3.contains(2));
  59. EXPECT_FALSE(S2 == S3);
  60. EXPECT_TRUE(S2 != S3);
  61. EXPECT_FALSE(S2.contains(2));
  62. EXPECT_FALSE(S3.contains(3));
  63. }
  64. TEST_F(ImmutableSetTest, MultiElemIntSetTest) {
  65. ImmutableSet<int>::Factory f;
  66. ImmutableSet<int> S = f.getEmptySet();
  67. ImmutableSet<int> S2 = f.add(f.add(f.add(S, 3), 4), 5);
  68. ImmutableSet<int> S3 = f.add(f.add(f.add(S2, 9), 20), 43);
  69. ImmutableSet<int> S4 = f.add(S2, 9);
  70. EXPECT_TRUE(S.isEmpty());
  71. EXPECT_FALSE(S2.isEmpty());
  72. EXPECT_FALSE(S3.isEmpty());
  73. EXPECT_FALSE(S4.isEmpty());
  74. EXPECT_FALSE(S.contains(3));
  75. EXPECT_FALSE(S.contains(9));
  76. EXPECT_TRUE(S2.contains(3));
  77. EXPECT_TRUE(S2.contains(4));
  78. EXPECT_TRUE(S2.contains(5));
  79. EXPECT_FALSE(S2.contains(9));
  80. EXPECT_FALSE(S2.contains(0));
  81. EXPECT_TRUE(S3.contains(43));
  82. EXPECT_TRUE(S3.contains(20));
  83. EXPECT_TRUE(S3.contains(9));
  84. EXPECT_TRUE(S3.contains(3));
  85. EXPECT_TRUE(S3.contains(4));
  86. EXPECT_TRUE(S3.contains(5));
  87. EXPECT_FALSE(S3.contains(0));
  88. EXPECT_TRUE(S4.contains(9));
  89. EXPECT_TRUE(S4.contains(3));
  90. EXPECT_TRUE(S4.contains(4));
  91. EXPECT_TRUE(S4.contains(5));
  92. EXPECT_FALSE(S4.contains(20));
  93. EXPECT_FALSE(S4.contains(43));
  94. }
  95. TEST_F(ImmutableSetTest, RemoveIntSetTest) {
  96. ImmutableSet<int>::Factory f;
  97. ImmutableSet<int> S = f.getEmptySet();
  98. ImmutableSet<int> S2 = f.add(f.add(S, 4), 5);
  99. ImmutableSet<int> S3 = f.add(S2, 3);
  100. ImmutableSet<int> S4 = f.remove(S3, 3);
  101. EXPECT_TRUE(S3.contains(3));
  102. EXPECT_FALSE(S2.contains(3));
  103. EXPECT_FALSE(S4.contains(3));
  104. EXPECT_TRUE(S2 == S4);
  105. EXPECT_TRUE(S3 != S2);
  106. EXPECT_TRUE(S3 != S4);
  107. EXPECT_TRUE(S3.contains(4));
  108. EXPECT_TRUE(S3.contains(5));
  109. EXPECT_TRUE(S4.contains(4));
  110. EXPECT_TRUE(S4.contains(5));
  111. }
  112. TEST_F(ImmutableSetTest, CallbackCharSetTest) {
  113. ImmutableSet<char>::Factory f;
  114. ImmutableSet<char> S = f.getEmptySet();
  115. ImmutableSet<char> S2 = f.add(f.add(f.add(S, 'a'), 'e'), 'i');
  116. ImmutableSet<char> S3 = f.add(f.add(S2, 'o'), 'u');
  117. S3.foreach<MyIter>();
  118. ASSERT_STREQ("aeiou", buffer);
  119. }
  120. TEST_F(ImmutableSetTest, Callback2CharSetTest) {
  121. ImmutableSet<char>::Factory f;
  122. ImmutableSet<char> S = f.getEmptySet();
  123. ImmutableSet<char> S2 = f.add(f.add(f.add(S, 'b'), 'c'), 'd');
  124. ImmutableSet<char> S3 = f.add(f.add(f.add(S2, 'f'), 'g'), 'h');
  125. MyIter obj;
  126. S3.foreach<MyIter>(obj);
  127. ASSERT_STREQ("bcdfgh", buffer);
  128. ASSERT_EQ(6, obj.counter);
  129. MyIter obj2;
  130. S2.foreach<MyIter>(obj2);
  131. ASSERT_STREQ("bcd", buffer);
  132. ASSERT_EQ(3, obj2.counter);
  133. MyIter obj3;
  134. S.foreach<MyIter>(obj);
  135. ASSERT_STREQ("", buffer);
  136. ASSERT_EQ(0, obj3.counter);
  137. }
  138. TEST_F(ImmutableSetTest, IterLongSetTest) {
  139. ImmutableSet<long>::Factory f;
  140. ImmutableSet<long> S = f.getEmptySet();
  141. ImmutableSet<long> S2 = f.add(f.add(f.add(S, 0), 1), 2);
  142. ImmutableSet<long> S3 = f.add(f.add(f.add(S2, 3), 4), 5);
  143. int i = 0;
  144. for (ImmutableSet<long>::iterator I = S.begin(), E = S.end(); I != E; ++I) {
  145. ASSERT_EQ(i++, *I);
  146. }
  147. ASSERT_EQ(0, i);
  148. i = 0;
  149. for (ImmutableSet<long>::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
  150. ASSERT_EQ(i++, *I);
  151. }
  152. ASSERT_EQ(3, i);
  153. i = 0;
  154. for (ImmutableSet<long>::iterator I = S3.begin(), E = S3.end(); I != E; I++) {
  155. ASSERT_EQ(i++, *I);
  156. }
  157. ASSERT_EQ(6, i);
  158. }
  159. }