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 <array>
17 #include "gtest/gtest.h"
18 #include "utils/arch.h"
19
20 extern "C" int IrtocTestAddValues(int64_t, int64_t);
21 extern "C" int IrtocTestIncMaxValue(uint64_t, uint64_t);
22 extern "C" int IrtocTestIncMaxValueLabels(uint64_t, uint64_t);
23 extern "C" unsigned IrtocTestSeqLabels(uint64_t);
24 extern "C" uint64_t IrtocTestCfg(void *, uint64_t);
25 extern "C" uint64_t IrtocTestCfgLabels(void *, uint64_t);
26 extern "C" size_t IrtocTestLabels(size_t);
27 extern "C" size_t IrtocTestReturnBeforeLabel(size_t);
28
29 using TestCfgFunc = uint64_t (*)(void *, uint64_t);
30
31 namespace panda::test {
32
TEST(Irtoc,AddValues)33 TEST(Irtoc, AddValues)
34 {
35 ASSERT_EQ(IrtocTestAddValues(1, 2), 3);
36 ASSERT_EQ(IrtocTestAddValues(-1, -2), -3);
37 }
38
TEST(Irtoc,IncMaxValue)39 TEST(Irtoc, IncMaxValue)
40 {
41 ASSERT_EQ(IrtocTestIncMaxValue(10, 9), 11);
42 ASSERT_EQ(IrtocTestIncMaxValue(4, 8), 9);
43 ASSERT_EQ(IrtocTestIncMaxValue(4, 4), 5);
44 }
45
TEST(Irtoc,IncMaxValueLabels)46 TEST(Irtoc, IncMaxValueLabels)
47 {
48 ASSERT_EQ(IrtocTestIncMaxValueLabels(10, 9), 11);
49 ASSERT_EQ(IrtocTestIncMaxValueLabels(4, 8), 9);
50 ASSERT_EQ(IrtocTestIncMaxValueLabels(4, 4), 5);
51 }
52
53 template <size_t N>
ModifyArrayForLoopTest(std::array<uint64_t,N> * data)54 uint64_t ModifyArrayForLoopTest(std::array<uint64_t, N> *data)
55 {
56 uint64_t res = 0;
57 for (size_t i = 0; i < data->size(); i++) {
58 if ((i % 2) == 0) {
59 if (((*data)[i] % 2) == 0) {
60 (*data)[i] += 2;
61 res += 2;
62 } else {
63 (*data)[i] += 1;
64 res += 1;
65 }
66 } else {
67 (*data)[i] -= 1;
68 res -= 1;
69 }
70 }
71 return res;
72 }
73
TestLoop(TestCfgFunc func)74 void TestLoop(TestCfgFunc func)
75 {
76 std::array<uint64_t, 8> buf;
77 for (size_t i = 0; i < buf.size(); i++) {
78 buf[i] = i + i % 3;
79 }
80 std::array<uint64_t, 8> buf_expected = buf;
81 auto expected = ModifyArrayForLoopTest(&buf_expected);
82 uint64_t res = func(static_cast<void *>(buf.data()), buf.size());
83 ASSERT_EQ(expected, res);
84 ASSERT_EQ(buf_expected, buf);
85 }
86
TEST(Irtoc,Loop)87 TEST(Irtoc, Loop)
88 {
89 if constexpr (RUNTIME_ARCH == Arch::AARCH32) {
90 GTEST_SKIP();
91 }
92 TestLoop(IrtocTestCfg);
93 TestLoop(IrtocTestCfgLabels);
94 }
95
TEST(Irtoc,SeqLabels)96 TEST(Irtoc, SeqLabels)
97 {
98 if constexpr (RUNTIME_ARCH == Arch::AARCH32) {
99 GTEST_SKIP();
100 }
101 EXPECT_EQ(IrtocTestSeqLabels(0), 1);
102 EXPECT_EQ(IrtocTestSeqLabels(5), 6);
103 EXPECT_EQ(IrtocTestSeqLabels(10), 11);
104 EXPECT_EQ(IrtocTestSeqLabels(11), 13);
105 EXPECT_EQ(IrtocTestSeqLabels(55), 57);
106 EXPECT_EQ(IrtocTestSeqLabels(100), 102);
107 EXPECT_EQ(IrtocTestSeqLabels(101), 104);
108 EXPECT_EQ(IrtocTestSeqLabels(1010), 1013);
109 EXPECT_EQ(IrtocTestSeqLabels(54545), 54548);
110 }
111
112 extern "C" int IrtocTestRelocations(int);
TestCall(int n)113 extern "C" int TestCall(int n)
114 {
115 return n + 2;
116 }
117
TEST(Irtoc,Relocations)118 TEST(Irtoc, Relocations)
119 {
120 ASSERT_EQ(IrtocTestRelocations(1), 5);
121 ASSERT_EQ(IrtocTestRelocations(2), 10);
122 ASSERT_EQ(IrtocTestRelocations(3), 17);
123 }
124
IncrementInt(size_t n)125 extern "C" size_t IncrementInt(size_t n)
126 {
127 return n + 1;
128 }
129
IncrementFloat(double n)130 extern "C" double IncrementFloat(double n)
131 {
132 return n + 1;
133 }
134
135 extern "C" size_t IrtocTestRelocations2(size_t a0, size_t a1, double f0, size_t a2, size_t a3, size_t a4, double f1,
136 double f2, size_t a5, size_t a6, double f3, size_t a7, size_t a8, size_t a9,
137 double f4);
138
TEST(Irtoc,RelocationsParams)139 TEST(Irtoc, RelocationsParams)
140 {
141 if constexpr (RUNTIME_ARCH == Arch::AARCH32) {
142 GTEST_SKIP();
143 }
144 ASSERT_EQ(IrtocTestRelocations2(0, 1, 2.0, 3, 4, 5, 6.0, 7.0, 8, 9, 10.0, 11, 12, 13, 14.0), 120);
145 }
146
TEST(Irtoc,Labels)147 TEST(Irtoc, Labels)
148 {
149 EXPECT_EQ(IrtocTestLabels(0), 0);
150 EXPECT_EQ(IrtocTestLabels(1), 1);
151 EXPECT_EQ(IrtocTestLabels(2), 3);
152 EXPECT_EQ(IrtocTestLabels(3), 6);
153 EXPECT_EQ(IrtocTestLabels(4), 10);
154 EXPECT_EQ(IrtocTestLabels(5), 15);
155 EXPECT_EQ(IrtocTestLabels(100), 5050);
156 }
157
TEST(Irtoc,ReturnBeforeLabel)158 TEST(Irtoc, ReturnBeforeLabel)
159 {
160 EXPECT_EQ(IrtocTestReturnBeforeLabel(42), 2);
161 EXPECT_EQ(IrtocTestReturnBeforeLabel(146), 1);
162 }
163
164 } // namespace panda::test
165