diff --git a/llvm/unittests/ADT/EquivalenceClassesTest.cpp b/llvm/unittests/ADT/EquivalenceClassesTest.cpp --- a/llvm/unittests/ADT/EquivalenceClassesTest.cpp +++ b/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 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