Skip to content

Commit c072580

Browse files
[ADT] Add unit tests for set_subtract (llvm#99561)
This patch adds a couple of unit tests: - SetSubtractSmallPtrSet exercises the code path involving remove_if, added in d772cdd. Note that SmallPtrSet supports remove_if. - SetSubtractSmallVector exercises the code path involving S1.erase(*SI) and ensures that set_subtract continues to accept S2 being a vector, which does not have contains.
1 parent f304b88 commit c072580

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

llvm/unittests/ADT/SetOperationsTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "llvm/ADT/SetOperations.h"
1010
#include "llvm/ADT/SetVector.h"
11+
#include "llvm/ADT/SmallPtrSet.h"
12+
#include "llvm/ADT/SmallVector.h"
1113
#include "gmock/gmock.h"
1214
#include "gtest/gtest.h"
1315

@@ -16,6 +18,7 @@
1618
using namespace llvm;
1719

1820
using testing::IsEmpty;
21+
using testing::UnorderedElementsAre;
1922

2023
namespace {
2124

@@ -199,6 +202,38 @@ TEST(SetOperationsTest, SetSubtract) {
199202
EXPECT_EQ(ExpectedSet2, Set2);
200203
}
201204

205+
TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
206+
int A[4];
207+
208+
// Set1.size() < Set2.size()
209+
SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
210+
SmallPtrSet<int *, 4> Set2 = {&A[1], &A[2], &A[3]};
211+
set_subtract(Set1, Set2);
212+
EXPECT_THAT(Set1, UnorderedElementsAre(&A[0]));
213+
214+
// Set1.size() > Set2.size()
215+
Set1 = {&A[0], &A[1], &A[2]};
216+
Set2 = {&A[0], &A[2]};
217+
set_subtract(Set1, Set2);
218+
EXPECT_THAT(Set1, UnorderedElementsAre(&A[1]));
219+
}
220+
221+
TEST(SetOperationsTest, SetSubtractSmallVector) {
222+
int A[4];
223+
224+
// Set1.size() < Set2.size()
225+
SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
226+
SmallVector<int *> Set2 = {&A[1], &A[2], &A[3]};
227+
set_subtract(Set1, Set2);
228+
EXPECT_THAT(Set1, UnorderedElementsAre(&A[0]));
229+
230+
// Set1.size() > Set2.size()
231+
Set1 = {&A[0], &A[1], &A[2]};
232+
Set2 = {&A[0], &A[2]};
233+
set_subtract(Set1, Set2);
234+
EXPECT_THAT(Set1, UnorderedElementsAre(&A[1]));
235+
}
236+
202237
TEST(SetOperationsTest, SetSubtractRemovedRemaining) {
203238
std::set<int> Removed, Remaining;
204239

0 commit comments

Comments
 (0)