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/index.h"
19
20 #include "util/tests/verifier_test.h"
21
22 #include <gtest/gtest.h>
23
24 namespace panda::verifier::test {
25
TEST_F(VerifierTest,index)26 TEST_F(VerifierTest, index)
27 {
28 Index<int> default_index;
29 EXPECT_FALSE(default_index.IsValid());
30
31 default_index = Index<int>(7);
32 EXPECT_TRUE(default_index.IsValid());
33
34 int number = default_index;
35 EXPECT_EQ(number, 7);
36
37 number = *default_index;
38 EXPECT_EQ(number, 7);
39
40 default_index = 5;
41 ASSERT_TRUE(default_index.IsValid());
42 EXPECT_EQ(static_cast<int>(default_index), 5);
43
44 default_index.Invalidate();
45 EXPECT_FALSE(default_index.IsValid());
46
47 #ifndef NDEBUG
48 EXPECT_DEATH(number = default_index, "");
49 #endif
50
51 Index<int> default_index1 {4};
52 EXPECT_TRUE(default_index1.IsValid());
53 EXPECT_EQ(static_cast<int>(default_index1), 4);
54 EXPECT_FALSE(default_index == default_index1);
55 EXPECT_TRUE(default_index != default_index1);
56
57 default_index = std::move(default_index1);
58 ASSERT_TRUE(default_index.IsValid());
59 EXPECT_EQ(static_cast<int>(default_index), 4);
60 EXPECT_FALSE(default_index1.IsValid());
61
62 default_index1 = default_index;
63 ASSERT_TRUE(default_index.IsValid());
64 EXPECT_EQ(static_cast<int>(default_index), 4);
65 ASSERT_TRUE(default_index1.IsValid());
66 EXPECT_EQ(static_cast<int>(default_index1), 4);
67
68 EXPECT_TRUE(static_cast<bool>(default_index));
69 default_index.Invalidate();
70 EXPECT_FALSE(static_cast<bool>(default_index));
71
72 Index<int, 9> custom_index;
73 EXPECT_FALSE(custom_index.IsValid());
74
75 #ifndef NDEBUG
76 EXPECT_DEATH(custom_index = 9, "");
77 #endif
78
79 Index<int, 9> custom_index1 {std::move(custom_index)};
80 EXPECT_FALSE(custom_index.IsValid());
81 EXPECT_FALSE(custom_index1.IsValid());
82
83 custom_index = 5;
84 ASSERT_TRUE(custom_index.IsValid());
85 EXPECT_EQ(static_cast<int>(custom_index), 5);
86 EXPECT_EQ(static_cast<double>(custom_index), 5.0);
87 }
88
TEST_F(VerifierTest,index_hash)89 TEST_F(VerifierTest, index_hash)
90 {
91 std::unordered_set<Index<int, 8>> i_set; // containers mustn't contain invalid index
92 i_set.emplace(5);
93 i_set.insert(Index<int, 8> {7});
94 EXPECT_EQ(i_set.size(), 2);
95 EXPECT_EQ(i_set.count(5), 1);
96 EXPECT_EQ(i_set.count(6), 0);
97 EXPECT_EQ(i_set.count(7), 1);
98 }
99
100 } // namespace panda::verifier::test