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