[PATCH] D113461: [ADT] Add unit test for EquivalanceClasses comparator

Matthias Springer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 8 22:20:21 PST 2021


springerm created this revision.
springerm added a reviewer: dblaikie.
springerm requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This unit tests tests new functionality added by D112052 <https://reviews.llvm.org/D112052>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113461

Files:
  llvm/unittests/ADT/EquivalenceClassesTest.cpp


Index: llvm/unittests/ADT/EquivalenceClassesTest.cpp
===================================================================
--- llvm/unittests/ADT/EquivalenceClassesTest.cpp
+++ llvm/unittests/ADT/EquivalenceClassesTest.cpp
@@ -81,4 +81,40 @@
         EXPECT_FALSE(EqClasses.isEquivalent(i, j));
 }
 
+namespace {
+struct TestStruct {
+  int value;
+
+  bool operator==(const TestStruct &other) const {
+    return value == other.value;
+  }
+};
+struct TestStructComparator {
+  bool operator()(const TestStruct &lhs, const TestStruct &rhs) const {
+    return lhs.value < rhs.value;
+  }
+};
+} // namespace
+
+TEST(EquivalenceClassesTest, Comparator) {
+  // This test is identical to MultipleSets, but uses `TestStruct` with a
+  // comparator instead of plain ints.
+
+  EquivalenceClasses<TestStruct, TestStructComparator> EqClasses;
+
+  TestStruct testObjs[100];
+  for (int i = 0; i < 100; i++)
+    testObjs[i].value = i;
+
+  for (int i = 0; i < 100; i++)
+    EqClasses.unionSets(testObjs[i % 17], testObjs[i]);
+
+  for (int i = 0; i < 100; i++)
+    for (int j = 0; j < 100; j++)
+      if (i % 17 == j % 17)
+        EXPECT_TRUE(EqClasses.isEquivalent(testObjs[i], testObjs[j]));
+      else
+        EXPECT_FALSE(EqClasses.isEquivalent(testObjs[i], testObjs[j]));
+}
+
 } // llvm


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113461.385700.patch
Type: text/x-patch
Size: 1293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211109/179d03b3/attachment.bin>


More information about the llvm-commits mailing list