Skip to content

Commit 687fc08

Browse files
[ADT] Use UnorderedElementsAre in SetOperationsTest.cpp (NFC) (#99596)
1 parent f554dd7 commit 687fc08

File tree

1 file changed

+65
-101
lines changed

1 file changed

+65
-101
lines changed

llvm/unittests/ADT/SetOperationsTest.cpp

Lines changed: 65 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -25,50 +25,43 @@ namespace {
2525
TEST(SetOperationsTest, SetUnion) {
2626
std::set<int> Set1 = {1, 2, 3, 4};
2727
std::set<int> Set2 = {5, 6, 7, 8};
28-
// Set1 should be the union of input sets Set1 and Set2.
29-
std::set<int> ExpectedSet1 = {1, 2, 3, 4, 5, 6, 7, 8};
30-
// Set2 should not be touched.
31-
std::set<int> ExpectedSet2 = Set2;
3228

3329
set_union(Set1, Set2);
34-
EXPECT_EQ(ExpectedSet1, Set1);
35-
EXPECT_EQ(ExpectedSet2, Set2);
30+
// Set1 should be the union of input sets Set1 and Set2.
31+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
32+
// Set2 should not be touched.
33+
EXPECT_THAT(Set2, UnorderedElementsAre(5, 6, 7, 8));
3634

3735
Set1.clear();
3836
Set2 = {1, 2};
37+
38+
set_union(Set1, Set2);
3939
// Set1 should be the union of input sets Set1 and Set2, which in this case
4040
// will be Set2.
41-
ExpectedSet1 = Set2;
41+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2));
4242
// Set2 should not be touched.
43-
ExpectedSet2 = Set2;
44-
45-
set_union(Set1, Set2);
46-
EXPECT_EQ(ExpectedSet1, Set1);
47-
EXPECT_EQ(ExpectedSet2, Set2);
43+
EXPECT_THAT(Set2, UnorderedElementsAre(1, 2));
4844
}
4945

5046
TEST(SetOperationsTest, SetIntersect) {
5147
std::set<int> Set1 = {1, 2, 3, 4};
5248
std::set<int> Set2 = {3, 4, 5, 6};
53-
// Set1 should be the intersection of sets Set1 and Set2.
54-
std::set<int> ExpectedSet1 = {3, 4};
55-
// Set2 should not be touched.
56-
std::set<int> ExpectedSet2 = Set2;
5749

5850
set_intersect(Set1, Set2);
59-
EXPECT_EQ(ExpectedSet1, Set1);
60-
EXPECT_EQ(ExpectedSet2, Set2);
51+
// Set1 should be the intersection of sets Set1 and Set2.
52+
EXPECT_THAT(Set1, UnorderedElementsAre(3, 4));
53+
// Set2 should not be touched.
54+
EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
6155

6256
Set1 = {1, 2, 3, 4};
6357
Set2 = {5, 6};
64-
// Set2 should not be touched.
65-
ExpectedSet2 = Set2;
6658

6759
set_intersect(Set1, Set2);
6860
// Set1 should be the intersection of sets Set1 and Set2, which
6961
// is empty as they are non-overlapping.
7062
EXPECT_THAT(Set1, IsEmpty());
71-
EXPECT_EQ(ExpectedSet2, Set2);
63+
// Set2 should not be touched.
64+
EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
7265

7366
// Check that set_intersect works on SetVector via remove_if.
7467
SmallSetVector<int, 4> SV;
@@ -85,121 +78,101 @@ TEST(SetOperationsTest, SetIntersection) {
8578
std::set<int> Set1 = {1, 2, 3, 4};
8679
std::set<int> Set2 = {3, 4, 5, 6};
8780
std::set<int> Result;
88-
// Result should be the intersection of sets Set1 and Set2.
89-
std::set<int> ExpectedResult = {3, 4};
90-
// Set1 and Set2 should not be touched.
91-
std::set<int> ExpectedSet1 = Set1;
92-
std::set<int> ExpectedSet2 = Set2;
9381

9482
Result = set_intersection(Set1, Set2);
95-
EXPECT_EQ(ExpectedResult, Result);
96-
EXPECT_EQ(ExpectedSet1, Set1);
97-
EXPECT_EQ(ExpectedSet2, Set2);
83+
// Result should be the intersection of sets Set1 and Set2.
84+
EXPECT_THAT(Result, UnorderedElementsAre(3, 4));
85+
// Set1 and Set2 should not be touched.
86+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
87+
EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
9888

9989
Set1 = {1, 2, 3, 4};
10090
Set2 = {5, 6};
101-
// Set1 and Set2 should not be touched.
102-
ExpectedSet1 = Set1;
103-
ExpectedSet2 = Set2;
10491

10592
Result = set_intersection(Set1, Set2);
10693
// Result should be the intersection of sets Set1 and Set2, which
10794
// is empty as they are non-overlapping.
10895
EXPECT_THAT(Result, IsEmpty());
109-
EXPECT_EQ(ExpectedSet1, Set1);
110-
EXPECT_EQ(ExpectedSet2, Set2);
96+
// Set1 and Set2 should not be touched.
97+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
98+
EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
11199

112100
Set1 = {5, 6};
113101
Set2 = {1, 2, 3, 4};
114-
// Set1 and Set2 should not be touched.
115-
ExpectedSet1 = Set1;
116-
ExpectedSet2 = Set2;
117102

118103
Result = set_intersection(Set1, Set2);
119104
// Result should be the intersection of sets Set1 and Set2, which
120105
// is empty as they are non-overlapping. Test this again with the input sets
121106
// reversed, since the code takes a different path depending on which input
122107
// set is smaller.
123108
EXPECT_THAT(Result, IsEmpty());
124-
EXPECT_EQ(ExpectedSet1, Set1);
125-
EXPECT_EQ(ExpectedSet2, Set2);
109+
// Set1 and Set2 should not be touched.
110+
EXPECT_THAT(Set1, UnorderedElementsAre(5, 6));
111+
EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
126112
}
127113

128114
TEST(SetOperationsTest, SetDifference) {
129115
std::set<int> Set1 = {1, 2, 3, 4};
130116
std::set<int> Set2 = {3, 4, 5, 6};
131117
std::set<int> Result;
132-
// Result should be Set1 - Set2, leaving only {1, 2}.
133-
std::set<int> ExpectedResult = {1, 2};
134-
// Set1 and Set2 should not be touched.
135-
std::set<int> ExpectedSet1 = Set1;
136-
std::set<int> ExpectedSet2 = Set2;
137118

138119
Result = set_difference(Set1, Set2);
139-
EXPECT_EQ(ExpectedResult, Result);
140-
EXPECT_EQ(ExpectedSet1, Set1);
141-
EXPECT_EQ(ExpectedSet2, Set2);
120+
// Result should be Set1 - Set2, leaving only {1, 2}.
121+
EXPECT_THAT(Result, UnorderedElementsAre(1, 2));
122+
// Set1 and Set2 should not be touched.
123+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
124+
EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
142125

143126
Set1 = {1, 2, 3, 4};
144127
Set2 = {1, 2, 3, 4};
145-
// Set1 and Set2 should not be touched.
146-
ExpectedSet1 = Set1;
147-
ExpectedSet2 = Set2;
148128

149129
Result = set_difference(Set1, Set2);
150130
// Result should be Set1 - Set2, which should be empty.
151131
EXPECT_THAT(Result, IsEmpty());
152-
EXPECT_EQ(ExpectedSet1, Set1);
153-
EXPECT_EQ(ExpectedSet2, Set2);
132+
// Set1 and Set2 should not be touched.
133+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
134+
EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
154135

155136
Set1 = {1, 2, 3, 4};
156137
Set2 = {5, 6};
138+
139+
Result = set_difference(Set1, Set2);
157140
// Result should be Set1 - Set2, which should be Set1 as they are
158141
// non-overlapping.
159-
ExpectedResult = Set1;
142+
EXPECT_THAT(Result, UnorderedElementsAre(1, 2, 3, 4));
160143
// Set1 and Set2 should not be touched.
161-
ExpectedSet1 = Set1;
162-
ExpectedSet2 = Set2;
163-
164-
Result = set_difference(Set1, Set2);
165-
EXPECT_EQ(ExpectedResult, Result);
166-
EXPECT_EQ(ExpectedSet1, Set1);
167-
EXPECT_EQ(ExpectedSet2, Set2);
144+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
145+
EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
168146
}
169147

170148
TEST(SetOperationsTest, SetSubtract) {
171149
std::set<int> Set1 = {1, 2, 3, 4};
172150
std::set<int> Set2 = {3, 4, 5, 6};
173-
// Set1 should get Set1 - Set2, leaving only {1, 2}.
174-
std::set<int> ExpectedSet1 = {1, 2};
175-
// Set2 should not be touched.
176-
std::set<int> ExpectedSet2 = Set2;
177151

178152
set_subtract(Set1, Set2);
179-
EXPECT_EQ(ExpectedSet1, Set1);
180-
EXPECT_EQ(ExpectedSet2, Set2);
153+
// Set1 should get Set1 - Set2, leaving only {1, 2}.
154+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2));
155+
// Set2 should not be touched.
156+
EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
181157

182158
Set1 = {1, 2, 3, 4};
183159
Set2 = {1, 2, 3, 4};
184-
// Set2 should not be touched.
185-
ExpectedSet2 = Set2;
186160

187161
set_subtract(Set1, Set2);
188162
// Set1 should get Set1 - Set2, which should be empty.
189163
EXPECT_THAT(Set1, IsEmpty());
190-
EXPECT_EQ(ExpectedSet2, Set2);
164+
// Set2 should not be touched.
165+
EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
191166

192167
Set1 = {1, 2, 3, 4};
193168
Set2 = {5, 6};
169+
170+
set_subtract(Set1, Set2);
194171
// Set1 should get Set1 - Set2, which should be Set1 as they are
195172
// non-overlapping.
196-
ExpectedSet1 = Set1;
173+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
197174
// Set2 should not be touched.
198-
ExpectedSet2 = Set2;
199-
200-
set_subtract(Set1, Set2);
201-
EXPECT_EQ(ExpectedSet1, Set1);
202-
EXPECT_EQ(ExpectedSet2, Set2);
175+
EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
203176
}
204177

205178
TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
@@ -239,56 +212,47 @@ TEST(SetOperationsTest, SetSubtractRemovedRemaining) {
239212

240213
std::set<int> Set1 = {1, 2, 3, 4};
241214
std::set<int> Set2 = {3, 4, 5, 6};
215+
216+
set_subtract(Set1, Set2, Removed, Remaining);
242217
// Set1 should get Set1 - Set2, leaving only {1, 2}.
243-
std::set<int> ExpectedSet1 = {1, 2};
218+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2));
244219
// Set2 should not be touched.
245-
std::set<int> ExpectedSet2 = Set2;
220+
EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
246221
// We should get back that {3, 4} from Set2 were removed from Set1, and {5, 6}
247222
// were not removed from Set1.
248-
std::set<int> ExpectedRemoved = {3, 4};
249-
std::set<int> ExpectedRemaining = {5, 6};
250-
251-
set_subtract(Set1, Set2, Removed, Remaining);
252-
EXPECT_EQ(ExpectedSet1, Set1);
253-
EXPECT_EQ(ExpectedSet2, Set2);
254-
EXPECT_EQ(ExpectedRemoved, Removed);
255-
EXPECT_EQ(ExpectedRemaining, Remaining);
223+
EXPECT_THAT(Removed, UnorderedElementsAre(3, 4));
224+
EXPECT_THAT(Remaining, UnorderedElementsAre(5, 6));
256225

257226
Set1 = {1, 2, 3, 4};
258227
Set2 = {1, 2, 3, 4};
259228
Removed.clear();
260229
Remaining.clear();
261-
// Set2 should not be touched.
262-
ExpectedSet2 = Set2;
263-
// Set should get back that all of Set2 was removed from Set1, and nothing
264-
// left in Set2 was not removed from Set1.
265-
ExpectedRemoved = Set2;
266230

267231
set_subtract(Set1, Set2, Removed, Remaining);
268232
// Set1 should get Set1 - Set2, which should be empty.
269233
EXPECT_THAT(Set1, IsEmpty());
270-
EXPECT_EQ(ExpectedSet2, Set2);
271-
EXPECT_EQ(ExpectedRemoved, Removed);
234+
// Set2 should not be touched.
235+
EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
236+
// Set should get back that all of Set2 was removed from Set1, and nothing
237+
// left in Set2 was not removed from Set1.
238+
EXPECT_THAT(Removed, UnorderedElementsAre(1, 2, 3, 4));
272239
EXPECT_THAT(Remaining, IsEmpty());
273240

274241
Set1 = {1, 2, 3, 4};
275242
Set2 = {5, 6};
276243
Removed.clear();
277244
Remaining.clear();
245+
246+
set_subtract(Set1, Set2, Removed, Remaining);
278247
// Set1 should get Set1 - Set2, which should be Set1 as they are
279248
// non-overlapping.
280-
ExpectedSet1 = {1, 2, 3, 4};
249+
EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
281250
// Set2 should not be touched.
282-
ExpectedSet2 = Set2;
251+
EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
252+
EXPECT_THAT(Removed, IsEmpty());
283253
// Set should get back that none of Set2 was removed from Set1, and all
284254
// of Set2 was not removed from Set1.
285-
ExpectedRemaining = Set2;
286-
287-
set_subtract(Set1, Set2, Removed, Remaining);
288-
EXPECT_EQ(ExpectedSet1, Set1);
289-
EXPECT_EQ(ExpectedSet2, Set2);
290-
EXPECT_THAT(Removed, IsEmpty());
291-
EXPECT_EQ(ExpectedRemaining, Remaining);
255+
EXPECT_THAT(Remaining, UnorderedElementsAre(5, 6));
292256
}
293257

294258
TEST(SetOperationsTest, SetIsSubset) {

0 commit comments

Comments
 (0)