1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "util/equiv_classes.h"
17
18 #include "util/tests/verifier_test.h"
19
20 #include <gtest/gtest.h>
21
22 namespace {
23
24 struct Obj {
25 int data = 0;
operator ==__anon0c8bbc550111::Obj26 bool operator==(const Obj &rhs) const
27 {
28 return data == rhs.data;
29 }
operator <__anon0c8bbc550111::Obj30 bool operator<(const Obj &rhs) const
31 {
32 return data < rhs.data;
33 }
34 };
35
36 } // namespace
37
38 namespace std {
39
40 template <>
41 struct hash<Obj> {
operator ()std::hash42 constexpr size_t operator()(const Obj &obj) const
43 {
44 return static_cast<size_t>(obj.data);
45 }
46 };
47
48 } // namespace std
49
50 namespace panda::verifier::test {
51
TEST_F(VerifierTest,ClassesOfEquivalence)52 TEST_F(VerifierTest, ClassesOfEquivalence)
53 {
54 EqClass<Obj> eqc;
55
56 Obj o1 {1};
57 Obj o2 {2};
58 Obj o3 {3};
59 Obj o4 {4};
60 Obj o5 {5};
61 Obj o6 {6};
62 Obj o7 {7};
63 Obj o8 {8};
64
65 eqc.Equate({o1, o2, o3});
66 eqc.Equate({o4, o5, o6});
67 eqc.Equate({o7, o8});
68
69 EXPECT_EQ(eqc.ClassSizeOf(o1), 3);
70 EXPECT_EQ(eqc.ClassSizeOf(o5), 3);
71 EXPECT_EQ(eqc.ClassSizeOf(o8), 2);
72
73 EXPECT_TRUE(eqc.IsAllEqual({o1, o3}));
74 EXPECT_TRUE(eqc.IsAllEqual({o2, o1}));
75 EXPECT_TRUE(eqc.IsAllEqual({o2, o3}));
76
77 EXPECT_FALSE(eqc.IsAllEqual({o1, o4}));
78 EXPECT_FALSE(eqc.IsAllEqual({o5, o8}));
79
80 EXPECT_TRUE(eqc.IsAllEqual({o4, o6}));
81
82 EXPECT_TRUE(eqc.IsAllEqual({o7, o8}));
83
84 EXPECT_FALSE(eqc.IsAllEqual({o5, o7}));
85
86 eqc.Equate({o3, o8});
87
88 EXPECT_EQ(eqc.ClassSizeOf(o2), 5);
89 EXPECT_EQ(eqc.ClassSizeOf(o7), 5);
90
91 EXPECT_TRUE(eqc.IsAllEqual({o1, o7}));
92
93 eqc.Equate({o2, o4});
94
95 EXPECT_EQ(eqc.ClassSizeOf(o8), 8);
96
97 EXPECT_TRUE(eqc.IsAllEqual({o3, o5, o8}));
98 }
99
100 } // namespace panda::verifier::test
101