[llvm] c072580 - [ADT] Add unit tests for set_subtract (#99561)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 18:06:21 PDT 2024


Author: Kazu Hirata
Date: 2024-07-18T18:06:19-07:00
New Revision: c0725804e6bc0f1b418db096099897e7fb029a0e

URL: https://github.com/llvm/llvm-project/commit/c0725804e6bc0f1b418db096099897e7fb029a0e
DIFF: https://github.com/llvm/llvm-project/commit/c0725804e6bc0f1b418db096099897e7fb029a0e.diff

LOG: [ADT] Add unit tests for set_subtract (#99561)

This patch adds a couple of unit tests:

- SetSubtractSmallPtrSet exercises the code path involving remove_if,
  added in d772cdd6279de1e578dfdfca7432327a1806c659.  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.

Added: 
    

Modified: 
    llvm/unittests/ADT/SetOperationsTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index b3d931cbfd479..da84c8afbd2ba 100644
--- a/llvm/unittests/ADT/SetOperationsTest.cpp
+++ b/llvm/unittests/ADT/SetOperationsTest.cpp
@@ -8,6 +8,8 @@
 
 #include "llvm/ADT/SetOperations.h"
 #include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SmallVector.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
@@ -16,6 +18,7 @@
 using namespace llvm;
 
 using testing::IsEmpty;
+using testing::UnorderedElementsAre;
 
 namespace {
 
@@ -199,6 +202,38 @@ TEST(SetOperationsTest, SetSubtract) {
   EXPECT_EQ(ExpectedSet2, Set2);
 }
 
+TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
+  int A[4];
+
+  // Set1.size() < Set2.size()
+  SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
+  SmallPtrSet<int *, 4> Set2 = {&A[1], &A[2], &A[3]};
+  set_subtract(Set1, Set2);
+  EXPECT_THAT(Set1, UnorderedElementsAre(&A[0]));
+
+  // Set1.size() > Set2.size()
+  Set1 = {&A[0], &A[1], &A[2]};
+  Set2 = {&A[0], &A[2]};
+  set_subtract(Set1, Set2);
+  EXPECT_THAT(Set1, UnorderedElementsAre(&A[1]));
+}
+
+TEST(SetOperationsTest, SetSubtractSmallVector) {
+  int A[4];
+
+  // Set1.size() < Set2.size()
+  SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
+  SmallVector<int *> Set2 = {&A[1], &A[2], &A[3]};
+  set_subtract(Set1, Set2);
+  EXPECT_THAT(Set1, UnorderedElementsAre(&A[0]));
+
+  // Set1.size() > Set2.size()
+  Set1 = {&A[0], &A[1], &A[2]};
+  Set2 = {&A[0], &A[2]};
+  set_subtract(Set1, Set2);
+  EXPECT_THAT(Set1, UnorderedElementsAre(&A[1]));
+}
+
 TEST(SetOperationsTest, SetSubtractRemovedRemaining) {
   std::set<int> Removed, Remaining;
 


        


More information about the llvm-commits mailing list