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