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