#include "llvm/ADT/EquivalenceClasses.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace llvm {
TEST(EquivalenceClassesTest, NoMerges) {
EquivalenceClasses<int> EqClasses;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (i == j)
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
else
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
}
TEST(EquivalenceClassesTest, SimpleMerge1) {
EquivalenceClasses<int> EqClasses;
EqClasses.unionSets(0, 1);
EqClasses.unionSets(1, 2);
EqClasses.unionSets(2, 3);
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
}
TEST(EquivalenceClassesTest, SimpleMerge2) {
EquivalenceClasses<int> EqClasses;
EqClasses.unionSets(0, 1);
EqClasses.unionSets(2, 3);
EqClasses.unionSets(0, 2);
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
}
TEST(EquivalenceClassesTest, TwoSets) {
EquivalenceClasses<int> EqClasses;
for (int i = 0; i < 30; i += 2)
EqClasses.unionSets(0, i);
for (int i = 1; i < 30; i += 2)
EqClasses.unionSets(1, i);
for (int i = 0; i < 30; i++)
for (int j = 0; j < 30; j++)
if (i % 2 == j % 2)
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
else
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
}
template <typename T> class ParameterizedTest : public testing::Test {};
TYPED_TEST_SUITE_P(ParameterizedTest);
TYPED_TEST_P(ParameterizedTest, MultipleSets) {
TypeParam EqClasses;
for (int i = 0; i < 100; i++)
EqClasses.unionSets(i % 17, i);
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
if (i % 17 == j % 17)
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
else
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
}
namespace {
struct TestStruct {
TestStruct(int value) : value(value) {}
bool operator==(const TestStruct &other) const {
return value == other.value;
}
int value;
};
struct TestStructComparator {
bool operator()(const TestStruct &lhs, const TestStruct &rhs) const {
return lhs.value < rhs.value;
}
};
}
REGISTER_TYPED_TEST_SUITE_P(ParameterizedTest, MultipleSets);
using ParamTypes =
testing::Types<EquivalenceClasses<int>,
EquivalenceClasses<TestStruct, TestStructComparator>>;
INSTANTIATE_TYPED_TEST_SUITE_P(EquivalenceClassesTest, ParameterizedTest,
ParamTypes, );
}