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 <unordered_set>
17
18 #include "util/abstract_index.h"
19
20 #include "util/tests/verifier_test.h"
21
22 #include <gtest/gtest.h>
23
24 namespace panda::verifier::test {
25
26 namespace {
27
28 class Wrap final {
29 public:
Wrap()30 Wrap() : default_index1 {}, default_index2 {4}
31 {
32 EXPECT_FALSE(default_index1.IsValid());
33
34 default_index1 = AbstractIndex<int, Wrap>(0x7);
35 EXPECT_TRUE(default_index1.IsValid());
36
37 int number = default_index1;
38 EXPECT_EQ(number, 0x7);
39
40 number = *default_index1;
41 EXPECT_EQ(number, 0x7);
42
43 default_index1 = 0x5;
44 EXPECT_TRUE(default_index1.IsValid());
45 EXPECT_EQ(static_cast<int>(default_index1), 0x5);
46
47 default_index1.Invalidate();
48 EXPECT_FALSE(default_index1.IsValid());
49
50 #ifndef NDEBUG
51 EXPECT_DEATH(number = default_index1, "");
52 #endif
53
54 EXPECT_TRUE(default_index2.IsValid());
55 EXPECT_EQ(static_cast<int>(default_index2), 0x4);
56
57 default_index1 = std::move(default_index2);
58 EXPECT_TRUE(default_index1.IsValid());
59 EXPECT_EQ(static_cast<int>(default_index1), 0x4);
60 EXPECT_FALSE(default_index2.IsValid());
61
62 default_index2 = default_index1;
63 EXPECT_TRUE(default_index1.IsValid());
64 EXPECT_EQ(static_cast<int>(default_index1), 0x4);
65 EXPECT_TRUE(default_index2.IsValid());
66 EXPECT_EQ(static_cast<int>(default_index2), 0x4);
67
68 EXPECT_TRUE(static_cast<bool>(default_index1));
69 default_index1.Invalidate();
70 EXPECT_FALSE(static_cast<bool>(default_index1));
71
72 default_index1 = 0x4;
73 default_index2 = 0x5;
74
75 std::unordered_set<AbstractIndex<int, Wrap>> i_set; // containers mustn't contain invalid index
76 i_set.insert(default_index1);
77 i_set.insert(default_index2);
78 EXPECT_EQ(i_set.size(), 0x2);
79 EXPECT_EQ(i_set.count(0x4), 1);
80 EXPECT_EQ(i_set.count(0x5), 1);
81 EXPECT_EQ(i_set.count(0x6), 0);
82 }
83
index1() const84 AbstractIndex<int, Wrap> index1() const
85 {
86 return default_index1;
87 }
index2() const88 AbstractIndex<int, Wrap> index2() const
89 {
90 return default_index2;
91 }
92
93 private:
94 AbstractIndex<int, Wrap> default_index1;
95 AbstractIndex<int, Wrap> default_index2;
96 };
97 } // namespace
98
TEST_F(VerifierTest,abstract_index)99 TEST_F(VerifierTest, abstract_index)
100 {
101 Wrap wr;
102
103 AbstractIndex<int, Wrap> index1 = wr.index1();
104 AbstractIndex<int, Wrap> index2 = wr.index2();
105
106 EXPECT_TRUE(index1.IsValid());
107 EXPECT_TRUE(index2.IsValid());
108 EXPECT_FALSE(index1 == index2);
109 EXPECT_TRUE(index1 != index2);
110 EXPECT_TRUE(index1 < index2);
111 EXPECT_TRUE(index1 <= index2);
112 EXPECT_TRUE(index1 <= index2);
113 }
114
115 } // namespace panda::verifier::test