Skip to content

[ADT] Use UnorderedElementsAre in SetOperationsTest.cpp (NFC) #99596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@kazutakahirata kazutakahirata requested review from nikic and kuhar July 19, 2024 01:13
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@llvmbot
Copy link
Member

llvmbot commented Jul 19, 2024

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/99596.diff

1 Files Affected:

  • (modified) llvm/unittests/ADT/SetOperationsTest.cpp (+65-101)
diff --git a/llvm/unittests/ADT/SetOperationsTest.cpp b/llvm/unittests/ADT/SetOperationsTest.cpp
index da84c8afbd2ba..f99f5c9b2af10 100644
--- a/llvm/unittests/ADT/SetOperationsTest.cpp
+++ b/llvm/unittests/ADT/SetOperationsTest.cpp
@@ -25,50 +25,43 @@ namespace {
 TEST(SetOperationsTest, SetUnion) {
   std::set<int> Set1 = {1, 2, 3, 4};
   std::set<int> Set2 = {5, 6, 7, 8};
-  // Set1 should be the union of input sets Set1 and Set2.
-  std::set<int> ExpectedSet1 = {1, 2, 3, 4, 5, 6, 7, 8};
-  // Set2 should not be touched.
-  std::set<int> ExpectedSet2 = Set2;
 
   set_union(Set1, Set2);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set1 should be the union of input sets Set1 and Set2.
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
+  // Set2 should not be touched.
+  EXPECT_THAT(Set2, UnorderedElementsAre(5, 6, 7, 8));
 
   Set1.clear();
   Set2 = {1, 2};
+
+  set_union(Set1, Set2);
   // Set1 should be the union of input sets Set1 and Set2, which in this case
   // will be Set2.
-  ExpectedSet1 = Set2;
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2));
   // Set2 should not be touched.
-  ExpectedSet2 = Set2;
-
-  set_union(Set1, Set2);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  EXPECT_THAT(Set2, UnorderedElementsAre(1, 2));
 }
 
 TEST(SetOperationsTest, SetIntersect) {
   std::set<int> Set1 = {1, 2, 3, 4};
   std::set<int> Set2 = {3, 4, 5, 6};
-  // Set1 should be the intersection of sets Set1 and Set2.
-  std::set<int> ExpectedSet1 = {3, 4};
-  // Set2 should not be touched.
-  std::set<int> ExpectedSet2 = Set2;
 
   set_intersect(Set1, Set2);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set1 should be the intersection of sets Set1 and Set2.
+  EXPECT_THAT(Set1, UnorderedElementsAre(3, 4));
+  // Set2 should not be touched.
+  EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
 
   Set1 = {1, 2, 3, 4};
   Set2 = {5, 6};
-  // Set2 should not be touched.
-  ExpectedSet2 = Set2;
 
   set_intersect(Set1, Set2);
   // Set1 should be the intersection of sets Set1 and Set2, which
   // is empty as they are non-overlapping.
   EXPECT_THAT(Set1, IsEmpty());
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set2 should not be touched.
+  EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
 
   // Check that set_intersect works on SetVector via remove_if.
   SmallSetVector<int, 4> SV;
@@ -85,35 +78,27 @@ TEST(SetOperationsTest, SetIntersection) {
   std::set<int> Set1 = {1, 2, 3, 4};
   std::set<int> Set2 = {3, 4, 5, 6};
   std::set<int> Result;
-  // Result should be the intersection of sets Set1 and Set2.
-  std::set<int> ExpectedResult = {3, 4};
-  // Set1 and Set2 should not be touched.
-  std::set<int> ExpectedSet1 = Set1;
-  std::set<int> ExpectedSet2 = Set2;
 
   Result = set_intersection(Set1, Set2);
-  EXPECT_EQ(ExpectedResult, Result);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Result should be the intersection of sets Set1 and Set2.
+  EXPECT_THAT(Result, UnorderedElementsAre(3, 4));
+  // Set1 and Set2 should not be touched.
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
+  EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
 
   Set1 = {1, 2, 3, 4};
   Set2 = {5, 6};
-  // Set1 and Set2 should not be touched.
-  ExpectedSet1 = Set1;
-  ExpectedSet2 = Set2;
 
   Result = set_intersection(Set1, Set2);
   // Result should be the intersection of sets Set1 and Set2, which
   // is empty as they are non-overlapping.
   EXPECT_THAT(Result, IsEmpty());
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set1 and Set2 should not be touched.
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
+  EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
 
   Set1 = {5, 6};
   Set2 = {1, 2, 3, 4};
-  // Set1 and Set2 should not be touched.
-  ExpectedSet1 = Set1;
-  ExpectedSet2 = Set2;
 
   Result = set_intersection(Set1, Set2);
   // Result should be the intersection of sets Set1 and Set2, which
@@ -121,85 +106,73 @@ TEST(SetOperationsTest, SetIntersection) {
   // reversed, since the code takes a different path depending on which input
   // set is smaller.
   EXPECT_THAT(Result, IsEmpty());
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set1 and Set2 should not be touched.
+  EXPECT_THAT(Set1, UnorderedElementsAre(5, 6));
+  EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
 }
 
 TEST(SetOperationsTest, SetDifference) {
   std::set<int> Set1 = {1, 2, 3, 4};
   std::set<int> Set2 = {3, 4, 5, 6};
   std::set<int> Result;
-  // Result should be Set1 - Set2, leaving only {1, 2}.
-  std::set<int> ExpectedResult = {1, 2};
-  // Set1 and Set2 should not be touched.
-  std::set<int> ExpectedSet1 = Set1;
-  std::set<int> ExpectedSet2 = Set2;
 
   Result = set_difference(Set1, Set2);
-  EXPECT_EQ(ExpectedResult, Result);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Result should be Set1 - Set2, leaving only {1, 2}.
+  EXPECT_THAT(Result, UnorderedElementsAre(1, 2));
+  // Set1 and Set2 should not be touched.
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
+  EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
 
   Set1 = {1, 2, 3, 4};
   Set2 = {1, 2, 3, 4};
-  // Set1 and Set2 should not be touched.
-  ExpectedSet1 = Set1;
-  ExpectedSet2 = Set2;
 
   Result = set_difference(Set1, Set2);
   // Result should be Set1 - Set2, which should be empty.
   EXPECT_THAT(Result, IsEmpty());
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set1 and Set2 should not be touched.
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
+  EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
 
   Set1 = {1, 2, 3, 4};
   Set2 = {5, 6};
+
+  Result = set_difference(Set1, Set2);
   // Result should be Set1 - Set2, which should be Set1 as they are
   // non-overlapping.
-  ExpectedResult = Set1;
+  EXPECT_THAT(Result, UnorderedElementsAre(1, 2, 3, 4));
   // Set1 and Set2 should not be touched.
-  ExpectedSet1 = Set1;
-  ExpectedSet2 = Set2;
-
-  Result = set_difference(Set1, Set2);
-  EXPECT_EQ(ExpectedResult, Result);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
+  EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
 }
 
 TEST(SetOperationsTest, SetSubtract) {
   std::set<int> Set1 = {1, 2, 3, 4};
   std::set<int> Set2 = {3, 4, 5, 6};
-  // Set1 should get Set1 - Set2, leaving only {1, 2}.
-  std::set<int> ExpectedSet1 = {1, 2};
-  // Set2 should not be touched.
-  std::set<int> ExpectedSet2 = Set2;
 
   set_subtract(Set1, Set2);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set1 should get Set1 - Set2, leaving only {1, 2}.
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2));
+  // Set2 should not be touched.
+  EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
 
   Set1 = {1, 2, 3, 4};
   Set2 = {1, 2, 3, 4};
-  // Set2 should not be touched.
-  ExpectedSet2 = Set2;
 
   set_subtract(Set1, Set2);
   // Set1 should get Set1 - Set2, which should be empty.
   EXPECT_THAT(Set1, IsEmpty());
-  EXPECT_EQ(ExpectedSet2, Set2);
+  // Set2 should not be touched.
+  EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
 
   Set1 = {1, 2, 3, 4};
   Set2 = {5, 6};
+
+  set_subtract(Set1, Set2);
   // Set1 should get Set1 - Set2, which should be Set1 as they are
   // non-overlapping.
-  ExpectedSet1 = Set1;
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
   // Set2 should not be touched.
-  ExpectedSet2 = Set2;
-
-  set_subtract(Set1, Set2);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
+  EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
 }
 
 TEST(SetOperationsTest, SetSubtractSmallPtrSet) {
@@ -239,56 +212,47 @@ TEST(SetOperationsTest, SetSubtractRemovedRemaining) {
 
   std::set<int> Set1 = {1, 2, 3, 4};
   std::set<int> Set2 = {3, 4, 5, 6};
+
+  set_subtract(Set1, Set2, Removed, Remaining);
   // Set1 should get Set1 - Set2, leaving only {1, 2}.
-  std::set<int> ExpectedSet1 = {1, 2};
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2));
   // Set2 should not be touched.
-  std::set<int> ExpectedSet2 = Set2;
+  EXPECT_THAT(Set2, UnorderedElementsAre(3, 4, 5, 6));
   // We should get back that {3, 4} from Set2 were removed from Set1, and {5, 6}
   // were not removed from Set1.
-  std::set<int> ExpectedRemoved = {3, 4};
-  std::set<int> ExpectedRemaining = {5, 6};
-
-  set_subtract(Set1, Set2, Removed, Remaining);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
-  EXPECT_EQ(ExpectedRemoved, Removed);
-  EXPECT_EQ(ExpectedRemaining, Remaining);
+  EXPECT_THAT(Removed, UnorderedElementsAre(3, 4));
+  EXPECT_THAT(Remaining, UnorderedElementsAre(5, 6));
 
   Set1 = {1, 2, 3, 4};
   Set2 = {1, 2, 3, 4};
   Removed.clear();
   Remaining.clear();
-  // Set2 should not be touched.
-  ExpectedSet2 = Set2;
-  // Set should get back that all of Set2 was removed from Set1, and nothing
-  // left in Set2 was not removed from Set1.
-  ExpectedRemoved = Set2;
 
   set_subtract(Set1, Set2, Removed, Remaining);
   // Set1 should get Set1 - Set2, which should be empty.
   EXPECT_THAT(Set1, IsEmpty());
-  EXPECT_EQ(ExpectedSet2, Set2);
-  EXPECT_EQ(ExpectedRemoved, Removed);
+  // Set2 should not be touched.
+  EXPECT_THAT(Set2, UnorderedElementsAre(1, 2, 3, 4));
+  // Set should get back that all of Set2 was removed from Set1, and nothing
+  // left in Set2 was not removed from Set1.
+  EXPECT_THAT(Removed, UnorderedElementsAre(1, 2, 3, 4));
   EXPECT_THAT(Remaining, IsEmpty());
 
   Set1 = {1, 2, 3, 4};
   Set2 = {5, 6};
   Removed.clear();
   Remaining.clear();
+
+  set_subtract(Set1, Set2, Removed, Remaining);
   // Set1 should get Set1 - Set2, which should be Set1 as they are
   // non-overlapping.
-  ExpectedSet1 = {1, 2, 3, 4};
+  EXPECT_THAT(Set1, UnorderedElementsAre(1, 2, 3, 4));
   // Set2 should not be touched.
-  ExpectedSet2 = Set2;
+  EXPECT_THAT(Set2, UnorderedElementsAre(5, 6));
+  EXPECT_THAT(Removed, IsEmpty());
   // Set should get back that none of Set2 was removed from Set1, and all
   // of Set2 was not removed from Set1.
-  ExpectedRemaining = Set2;
-
-  set_subtract(Set1, Set2, Removed, Remaining);
-  EXPECT_EQ(ExpectedSet1, Set1);
-  EXPECT_EQ(ExpectedSet2, Set2);
-  EXPECT_THAT(Removed, IsEmpty());
-  EXPECT_EQ(ExpectedRemaining, Remaining);
+  EXPECT_THAT(Remaining, UnorderedElementsAre(5, 6));
 }
 
 TEST(SetOperationsTest, SetIsSubset) {

@kazutakahirata kazutakahirata merged commit 687fc08 into llvm:main Jul 19, 2024
7 of 9 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_SetOperations_UnorderedElementsAre branch July 19, 2024 04:29
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary: 

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251554
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants