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 <vector>
17 #include <array>
18 #include <map>
19
20 #include "util/struct_field.h"
21
22 #include "util/tests/verifier_test.h"
23
24 #include <gtest/gtest.h>
25
26 namespace panda::verifier::test {
27
28 struct MyArr final {
29 int32_t num0 = 0;
30 int32_t num1 = 1;
31 int32_t num2 = 2;
32 int32_t num3 = 3;
33 int32_t num4 = 4;
34 int32_t num5 = 5;
35
36 using offset = struct_field<int32_t, int32_t>;
37
38 std::map<char, offset> elem_map = {{'0', offset {0}}, {'1', offset {4}}, {'2', offset {8}},
39 {'3', offset {12}}, {'4', offset {16}}, {'5', offset {20}}};
40
accesspanda::verifier::test::MyArr41 int32_t &access(char id)
42 {
43 auto it = elem_map.find(id);
44 ASSERT(it != elem_map.end());
45 return it->second.of(num0);
46 }
47 };
48
TEST_F(VerifierTest,struct_field)49 TEST_F(VerifierTest, struct_field)
50 {
51 std::vector<int32_t> vec(5);
52 vec[3] = 5;
53 int32_t &pos1 = vec[1];
54 struct_field<int32_t, int32_t> s_f1 {8};
55 int32_t &pos2 = s_f1.of(pos1);
56 EXPECT_EQ(pos2, 5);
57
58 std::array<int64_t, 3> arr;
59 arr[2] = 5;
60 int64_t &pos3 = arr[0];
61 struct_field<int64_t, int64_t> s_f2 {16};
62 int64_t &pos4 = s_f2.of(pos3);
63 EXPECT_EQ(pos4, 5);
64
65 MyArr my_arr;
66 EXPECT_EQ(my_arr.access('3'), 3);
67 my_arr.access('4') = 44;
68 EXPECT_EQ(my_arr.num4, 44);
69 }
70
71 } // namespace panda::verifier::test