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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 14:23:45 PDT 2024


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/99561

>From f630f6b07e2060855a76faf743b955077160311e Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 18 Jul 2024 13:09:30 -0700
Subject: [PATCH 1/2] [ADT] Add unit tests for set_subtract

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.
---
 llvm/unittests/ADT/SetOperationsTest.cpp | 38 ++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index b3d931cbfd479..7696e32570d21 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"
 
@@ -199,6 +201,42 @@ TEST(SetOperationsTest, SetSubtract) {
   EXPECT_EQ(ExpectedSet2, Set2);
 }
 
+TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
+  int A[4];
+
+  // Set1.size() < Set2.size()
+  llvm::SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
+  llvm::SmallPtrSet<int *, 4> Set2 = {&A[1], &A[2], &A[3]};
+  llvm::SmallPtrSet<int *, 4> ExpectedSet1 = {&A[0]};
+  set_subtract(Set1, Set2);
+  EXPECT_EQ(ExpectedSet1, Set1);
+
+  // Set1.size() > Set2.size()
+  Set1 = {&A[0], &A[1], &A[2]};
+  Set2 = {&A[0], &A[2]};
+  ExpectedSet1 = {&A[1]};
+  set_subtract(Set1, Set2);
+  EXPECT_EQ(ExpectedSet1, Set1);
+}
+
+TEST(SetOperationsTest, SetSubtractSmallVector) {
+  int A[4];
+
+  // Set1.size() < Set2.size()
+  llvm::SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
+  llvm::SmallVector<int *> Set2 = {&A[1], &A[2], &A[3]};
+  llvm::SmallPtrSet<int *, 4> ExpectedSet1 = {&A[0]};
+  set_subtract(Set1, Set2);
+  EXPECT_EQ(ExpectedSet1, Set1);
+
+  // Set1.size() > Set2.size()
+  Set1 = {&A[0], &A[1], &A[2]};
+  Set2 = {&A[0], &A[2]};
+  ExpectedSet1 = {&A[1]};
+  set_subtract(Set1, Set2);
+  EXPECT_EQ(ExpectedSet1, Set1);
+}
+
 TEST(SetOperationsTest, SetSubtractRemovedRemaining) {
   std::set<int> Removed, Remaining;
 

>From a85fd4b62b626213c12861f813a48d8168e20d44 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 18 Jul 2024 14:21:20 -0700
Subject: [PATCH 2/2] Drop llvm:: prefixes.

---
 llvm/unittests/ADT/SetOperationsTest.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index 7696e32570d21..59a58b0d0054e 100644
--- a/llvm/unittests/ADT/SetOperationsTest.cpp
+++ b/llvm/unittests/ADT/SetOperationsTest.cpp
@@ -205,9 +205,9 @@ TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
   int A[4];
 
   // Set1.size() < Set2.size()
-  llvm::SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
-  llvm::SmallPtrSet<int *, 4> Set2 = {&A[1], &A[2], &A[3]};
-  llvm::SmallPtrSet<int *, 4> ExpectedSet1 = {&A[0]};
+  SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
+  SmallPtrSet<int *, 4> Set2 = {&A[1], &A[2], &A[3]};
+  SmallPtrSet<int *, 4> ExpectedSet1 = {&A[0]};
   set_subtract(Set1, Set2);
   EXPECT_EQ(ExpectedSet1, Set1);
 
@@ -223,9 +223,9 @@ TEST(SetOperationsTest, SetSubtractSmallVector) {
   int A[4];
 
   // Set1.size() < Set2.size()
-  llvm::SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
-  llvm::SmallVector<int *> Set2 = {&A[1], &A[2], &A[3]};
-  llvm::SmallPtrSet<int *, 4> ExpectedSet1 = {&A[0]};
+  SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
+  SmallVector<int *> Set2 = {&A[1], &A[2], &A[3]};
+  SmallPtrSet<int *, 4> ExpectedSet1 = {&A[0]};
   set_subtract(Set1, Set2);
   EXPECT_EQ(ExpectedSet1, Set1);
 



More information about the llvm-commits mailing list