1 //=== llvm/unittest/ADT/EquivalenceClassesTest.cpp - the structure tests --===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/EquivalenceClasses.h"
10 #include "gtest/gtest.h"
16 TEST(EquivalenceClassesTest
, NoMerges
) {
17 EquivalenceClasses
<int> EqClasses
;
18 // Until we merged any sets, check that every element is only equivalent to
20 for (int i
= 0; i
< 3; i
++)
21 for (int j
= 0; j
< 3; j
++)
23 EXPECT_TRUE(EqClasses
.isEquivalent(i
, j
));
25 EXPECT_FALSE(EqClasses
.isEquivalent(i
, j
));
28 TEST(EquivalenceClassesTest
, SimpleMerge1
) {
29 EquivalenceClasses
<int> EqClasses
;
30 // Check that once we merge (A, B), (B, C), (C, D), then all elements belong
32 EqClasses
.unionSets(0, 1);
33 EqClasses
.unionSets(1, 2);
34 EqClasses
.unionSets(2, 3);
35 for (int i
= 0; i
< 4; ++i
)
36 for (int j
= 0; j
< 4; ++j
)
37 EXPECT_TRUE(EqClasses
.isEquivalent(i
, j
));
40 TEST(EquivalenceClassesTest
, SimpleMerge2
) {
41 EquivalenceClasses
<int> EqClasses
;
42 // Check that once we merge (A, B), (C, D), (A, C), then all elements belong
44 EqClasses
.unionSets(0, 1);
45 EqClasses
.unionSets(2, 3);
46 EqClasses
.unionSets(0, 2);
47 for (int i
= 0; i
< 4; ++i
)
48 for (int j
= 0; j
< 4; ++j
)
49 EXPECT_TRUE(EqClasses
.isEquivalent(i
, j
));
52 TEST(EquivalenceClassesTest
, TwoSets
) {
53 EquivalenceClasses
<int> EqClasses
;
54 // Form sets of odd and even numbers, check that we split them into these
55 // two sets correcrly.
56 for (int i
= 0; i
< 30; i
+= 2)
57 EqClasses
.unionSets(0, i
);
58 for (int i
= 1; i
< 30; i
+= 2)
59 EqClasses
.unionSets(1, i
);
61 for (int i
= 0; i
< 30; i
++)
62 for (int j
= 0; j
< 30; j
++)
64 EXPECT_TRUE(EqClasses
.isEquivalent(i
, j
));
66 EXPECT_FALSE(EqClasses
.isEquivalent(i
, j
));
69 // Type-parameterized tests: Run the same test cases with different element
71 template <typename T
> class ParameterizedTest
: public testing::Test
{};
73 TYPED_TEST_SUITE_P(ParameterizedTest
);
75 TYPED_TEST_P(ParameterizedTest
, MultipleSets
) {
77 // Split numbers from [0, 100) into sets so that values in the same set have
78 // equal remainders (mod 17).
79 for (int i
= 0; i
< 100; i
++)
80 EqClasses
.unionSets(i
% 17, i
);
82 for (int i
= 0; i
< 100; i
++)
83 for (int j
= 0; j
< 100; j
++)
85 EXPECT_TRUE(EqClasses
.isEquivalent(i
, j
));
87 EXPECT_FALSE(EqClasses
.isEquivalent(i
, j
));
91 // A dummy struct for testing EquivalenceClasses with a comparator.
93 TestStruct(int value
) : value(value
) {}
95 bool operator==(const TestStruct
&other
) const {
96 return value
== other
.value
;
101 // Comparator to be used in test case.
102 struct TestStructComparator
{
103 bool operator()(const TestStruct
&lhs
, const TestStruct
&rhs
) const {
104 return lhs
.value
< rhs
.value
;
109 REGISTER_TYPED_TEST_SUITE_P(ParameterizedTest
, MultipleSets
);
111 testing::Types
<EquivalenceClasses
<int>,
112 EquivalenceClasses
<TestStruct
, TestStructComparator
>>;
113 INSTANTIATE_TYPED_TEST_SUITE_P(EquivalenceClassesTest
, ParameterizedTest
,