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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 18:03:27 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/4] [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/4] 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);
 

>From b0e9359983bd7869e9cebf4619113ee38a02e67b Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 18 Jul 2024 16:05:05 -0700
Subject: [PATCH 3/4] Use testing::UnorderedElementsAre.

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

diff --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index 59a58b0d0054e..ff0d0d3832f1b 100644
--- a/llvm/unittests/ADT/SetOperationsTest.cpp
+++ b/llvm/unittests/ADT/SetOperationsTest.cpp
@@ -207,16 +207,14 @@ TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
   // Set1.size() < Set2.size()
   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);
+  EXPECT_THAT(Set1, testing::UnorderedElementsAre(&A[0]));
 
   // 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);
+  EXPECT_THAT(Set1, testing::UnorderedElementsAre(&A[1]));
 }
 
 TEST(SetOperationsTest, SetSubtractSmallVector) {
@@ -225,16 +223,14 @@ TEST(SetOperationsTest, SetSubtractSmallVector) {
   // Set1.size() < Set2.size()
   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);
+  EXPECT_THAT(Set1, testing::UnorderedElementsAre(&A[0]));
 
   // 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);
+  EXPECT_THAT(Set1, testing::UnorderedElementsAre(&A[1]));
 }
 
 TEST(SetOperationsTest, SetSubtractRemovedRemaining) {

>From 30f8001183437754222337677726e807e8a8b3b2 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 18 Jul 2024 17:35:18 -0700
Subject: [PATCH 4/4] Add "using testing::UnorderedElementsAre;".

---
 llvm/unittests/ADT/SetOperationsTest.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index ff0d0d3832f1b..da84c8afbd2ba 100644
--- a/llvm/unittests/ADT/SetOperationsTest.cpp
+++ b/llvm/unittests/ADT/SetOperationsTest.cpp
@@ -18,6 +18,7 @@
 using namespace llvm;
 
 using testing::IsEmpty;
+using testing::UnorderedElementsAre;
 
 namespace {
 
@@ -208,13 +209,13 @@ TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
   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, testing::UnorderedElementsAre(&A[0]));
+  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, testing::UnorderedElementsAre(&A[1]));
+  EXPECT_THAT(Set1, UnorderedElementsAre(&A[1]));
 }
 
 TEST(SetOperationsTest, SetSubtractSmallVector) {
@@ -224,13 +225,13 @@ TEST(SetOperationsTest, SetSubtractSmallVector) {
   SmallPtrSet<int *, 4> Set1 = {&A[0], &A[1]};
   SmallVector<int *> Set2 = {&A[1], &A[2], &A[3]};
   set_subtract(Set1, Set2);
-  EXPECT_THAT(Set1, testing::UnorderedElementsAre(&A[0]));
+  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, testing::UnorderedElementsAre(&A[1]));
+  EXPECT_THAT(Set1, UnorderedElementsAre(&A[1]));
 }
 
 TEST(SetOperationsTest, SetSubtractRemovedRemaining) {



More information about the llvm-commits mailing list