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 <vector>
17 #include <array>
18 #include <map>
19
20 #include "libpandabase/utils/utils.h"
21 #include "util/struct_field.h"
22
23 #include "util/tests/verifier_test.h"
24
25 #include <gtest/gtest.h>
26
27 namespace ark::verifier::test {
28
29 struct MyArr final {
30 // NOLINTBEGIN(misc-non-private-member-variables-in-classes,readability-magic-numbers)
31 int32_t num0 = 0;
32 int32_t num1 = 1;
33 int32_t num2 = 2;
34 int32_t num3 = 3;
35 int32_t num4 = 4;
36 int32_t num5 = 5;
37
38 using Offset = StructField<int32_t, int32_t>;
39
40 std::map<char, Offset> elemMap = {{'0', Offset {0}}, {'1', Offset {4}}, {'2', Offset {8}},
41 {'3', Offset {12}}, {'4', Offset {16}}, {'5', Offset {20}}};
42 // NOLINTEND(misc-non-private-member-variables-in-classes,readability-magic-numbers)
43
Accessark::verifier::test::MyArr44 int32_t &Access(char id)
45 {
46 auto it = elemMap.find(id);
47 ASSERT(it != elemMap.end());
48 return it->second.Of(num0);
49 }
50 };
51
TEST_F(VerifierTest,struct_field)52 TEST_F(VerifierTest, struct_field)
53 {
54 std::vector<int32_t> vec(5U);
55 vec[3U] = 5_I;
56 int32_t &pos1 = vec[1U];
57 StructField<int32_t, int32_t> sF1 {8U};
58 int32_t &pos2 = sF1.Of(pos1);
59 EXPECT_EQ(pos2, 5_I);
60
61 std::array<int64_t, 3U> arr {};
62 arr[2U] = 5L;
63 int64_t &pos3 = arr[0];
64 // NOLINTNEXTLINE(readability-magic-numbers)
65 StructField<int64_t, int64_t> sF2 {16U};
66 int64_t &pos4 = sF2.Of(pos3);
67 EXPECT_EQ(pos4, 5U);
68
69 MyArr myArr;
70 EXPECT_EQ(myArr.Access('3'), 3_I);
71 // NOLINTNEXTLINE(readability-magic-numbers)
72 myArr.Access('4') = 44_I;
73 EXPECT_EQ(myArr.num4, 44_I);
74 }
75
76 } // namespace ark::verifier::test
77