/
SetOperationsTest.cpp
266 lines (223 loc) · 7.56 KB
/
SetOperationsTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//===- SetOperations.cpp - Unit tests for set operations ------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SetOperations.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <set>
using namespace llvm;
using testing::IsEmpty;
namespace {
TEST(SetOperationsTest, SetUnion) {
std::set<int> Set1 = {1, 2, 3, 4};
std::set<int> Set2 = {5, 6, 7, 8};
// Set1 should be the union of input sets Set1 and Set2.
std::set<int> ExpectedSet1 = {1, 2, 3, 4, 5, 6, 7, 8};
// Set2 should not be touched.
std::set<int> ExpectedSet2 = Set2;
set_union(Set1, Set2);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
Set1.clear();
Set2 = {1, 2};
// Set1 should be the union of input sets Set1 and Set2, which in this case
// will be Set2.
ExpectedSet1 = Set2;
// Set2 should not be touched.
ExpectedSet2 = Set2;
set_union(Set1, Set2);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
}
TEST(SetOperationsTest, SetIntersect) {
std::set<int> Set1 = {1, 2, 3, 4};
std::set<int> Set2 = {3, 4, 5, 6};
// Set1 should be the intersection of sets Set1 and Set2.
std::set<int> ExpectedSet1 = {3, 4};
// Set2 should not be touched.
std::set<int> ExpectedSet2 = Set2;
set_intersect(Set1, Set2);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
Set1 = {1, 2, 3, 4};
Set2 = {5, 6};
// Set2 should not be touched.
ExpectedSet2 = Set2;
set_intersect(Set1, Set2);
// Set1 should be the intersection of sets Set1 and Set2, which
// is empty as they are non-overlapping.
EXPECT_THAT(Set1, IsEmpty());
EXPECT_EQ(ExpectedSet2, Set2);
}
TEST(SetOperationsTest, SetIntersection) {
std::set<int> Set1 = {1, 2, 3, 4};
std::set<int> Set2 = {3, 4, 5, 6};
std::set<int> Result;
// Result should be the intersection of sets Set1 and Set2.
std::set<int> ExpectedResult = {3, 4};
// Set1 and Set2 should not be touched.
std::set<int> ExpectedSet1 = Set1;
std::set<int> ExpectedSet2 = Set2;
Result = set_intersection(Set1, Set2);
EXPECT_EQ(ExpectedResult, Result);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
Set1 = {1, 2, 3, 4};
Set2 = {5, 6};
// Set1 and Set2 should not be touched.
ExpectedSet1 = Set1;
ExpectedSet2 = Set2;
Result = set_intersection(Set1, Set2);
// Result should be the intersection of sets Set1 and Set2, which
// is empty as they are non-overlapping.
EXPECT_THAT(Result, IsEmpty());
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
Set1 = {5, 6};
Set2 = {1, 2, 3, 4};
// Set1 and Set2 should not be touched.
ExpectedSet1 = Set1;
ExpectedSet2 = Set2;
Result = set_intersection(Set1, Set2);
// Result should be the intersection of sets Set1 and Set2, which
// is empty as they are non-overlapping. Test this again with the input sets
// reversed, since the code takes a different path depending on which input
// set is smaller.
EXPECT_THAT(Result, IsEmpty());
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
}
TEST(SetOperationsTest, SetDifference) {
std::set<int> Set1 = {1, 2, 3, 4};
std::set<int> Set2 = {3, 4, 5, 6};
std::set<int> Result;
// Result should be Set1 - Set2, leaving only {1, 2}.
std::set<int> ExpectedResult = {1, 2};
// Set1 and Set2 should not be touched.
std::set<int> ExpectedSet1 = Set1;
std::set<int> ExpectedSet2 = Set2;
Result = set_difference(Set1, Set2);
EXPECT_EQ(ExpectedResult, Result);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
Set1 = {1, 2, 3, 4};
Set2 = {1, 2, 3, 4};
// Set1 and Set2 should not be touched.
ExpectedSet1 = Set1;
ExpectedSet2 = Set2;
Result = set_difference(Set1, Set2);
// Result should be Set1 - Set2, which should be empty.
EXPECT_THAT(Result, IsEmpty());
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
Set1 = {1, 2, 3, 4};
Set2 = {5, 6};
// Result should be Set1 - Set2, which should be Set1 as they are
// non-overlapping.
ExpectedResult = Set1;
// Set1 and Set2 should not be touched.
ExpectedSet1 = Set1;
ExpectedSet2 = Set2;
Result = set_difference(Set1, Set2);
EXPECT_EQ(ExpectedResult, Result);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
}
TEST(SetOperationsTest, SetSubtract) {
std::set<int> Set1 = {1, 2, 3, 4};
std::set<int> Set2 = {3, 4, 5, 6};
// Set1 should get Set1 - Set2, leaving only {1, 2}.
std::set<int> ExpectedSet1 = {1, 2};
// Set2 should not be touched.
std::set<int> ExpectedSet2 = Set2;
set_subtract(Set1, Set2);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
Set1 = {1, 2, 3, 4};
Set2 = {1, 2, 3, 4};
// Set2 should not be touched.
ExpectedSet2 = Set2;
set_subtract(Set1, Set2);
// Set1 should get Set1 - Set2, which should be empty.
EXPECT_THAT(Set1, IsEmpty());
EXPECT_EQ(ExpectedSet2, Set2);
Set1 = {1, 2, 3, 4};
Set2 = {5, 6};
// Set1 should get Set1 - Set2, which should be Set1 as they are
// non-overlapping.
ExpectedSet1 = Set1;
// Set2 should not be touched.
ExpectedSet2 = Set2;
set_subtract(Set1, Set2);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
}
TEST(SetOperationsTest, SetSubtractRemovedRemaining) {
std::set<int> Removed, Remaining;
std::set<int> Set1 = {1, 2, 3, 4};
std::set<int> Set2 = {3, 4, 5, 6};
// Set1 should get Set1 - Set2, leaving only {1, 2}.
std::set<int> ExpectedSet1 = {1, 2};
// Set2 should not be touched.
std::set<int> ExpectedSet2 = Set2;
// We should get back that {3, 4} from Set2 were removed from Set1, and {5, 6}
// were not removed from Set1.
std::set<int> ExpectedRemoved = {3, 4};
std::set<int> ExpectedRemaining = {5, 6};
set_subtract(Set1, Set2, Removed, Remaining);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
EXPECT_EQ(ExpectedRemoved, Removed);
EXPECT_EQ(ExpectedRemaining, Remaining);
Set1 = {1, 2, 3, 4};
Set2 = {1, 2, 3, 4};
Removed.clear();
Remaining.clear();
// Set2 should not be touched.
ExpectedSet2 = Set2;
// Set should get back that all of Set2 was removed from Set1, and nothing
// left in Set2 was not removed from Set1.
ExpectedRemoved = Set2;
set_subtract(Set1, Set2, Removed, Remaining);
// Set1 should get Set1 - Set2, which should be empty.
EXPECT_THAT(Set1, IsEmpty());
EXPECT_EQ(ExpectedSet2, Set2);
EXPECT_EQ(ExpectedRemoved, Removed);
EXPECT_THAT(Remaining, IsEmpty());
Set1 = {1, 2, 3, 4};
Set2 = {5, 6};
Removed.clear();
Remaining.clear();
// Set1 should get Set1 - Set2, which should be Set1 as they are
// non-overlapping.
ExpectedSet1 = {1, 2, 3, 4};
// Set2 should not be touched.
ExpectedSet2 = Set2;
// Set should get back that none of Set2 was removed from Set1, and all
// of Set2 was not removed from Set1.
ExpectedRemaining = Set2;
set_subtract(Set1, Set2, Removed, Remaining);
EXPECT_EQ(ExpectedSet1, Set1);
EXPECT_EQ(ExpectedSet2, Set2);
EXPECT_THAT(Removed, IsEmpty());
EXPECT_EQ(ExpectedRemaining, Remaining);
}
TEST(SetOperationsTest, SetIsSubset) {
std::set<int> Set1 = {1, 2, 3, 4};
std::set<int> Set2 = {3, 4};
EXPECT_FALSE(set_is_subset(Set1, Set2));
Set1 = {1, 2, 3, 4};
Set2 = {1, 2, 3, 4};
EXPECT_TRUE(set_is_subset(Set1, Set2));
Set1 = {1, 2};
Set2 = {1, 2, 3, 4};
EXPECT_TRUE(set_is_subset(Set1, Set2));
Set1 = {1, 2};
Set2 = {3, 4};
EXPECT_FALSE(set_is_subset(Set1, Set2));
}
} // namespace