1 /**
2 * Copyright (c) 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 "libabckit/include/c/abckit.h"
17 #include "libabckit/src/include_v2/c/isa/isa_static.h"
18 #include "libabckit/include/c/metadata_core.h"
19
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22 #include "libabckit/include/c/statuses.h"
23 #include "statuses_impl.h"
24
25 #include <gtest/gtest.h>
26
27 // NOLINTBEGIN(readability-magic-numbers)
28 namespace libabckit::test {
29
30 namespace {
31 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36
TransformIrIcreateCmp(AbckitGraph * graph,AbckitInst * firstConst,AbckitInst * secondConst)37 void TransformIrIcreateCmp(AbckitGraph *graph, AbckitInst *firstConst, AbckitInst *secondConst)
38 {
39 AbckitBasicBlock *startBB = g_implG->gGetStartBasicBlock(graph);
40 std::vector<AbckitBasicBlock *> succBBs = helpers::BBgetSuccBlocks(startBB);
41 AbckitBasicBlock *firstBB = succBBs[0];
42 AbckitInst *cmp = g_statG->iCreateCmp(graph, firstConst, secondConst);
43 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
44 g_implG->bbAddInstFront(firstBB, cmp);
45 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
46 AbckitInst *ret = g_implG->bbGetLastInst(firstBB);
47 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
48 g_implG->iSetInput(ret, cmp, 0);
49 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
50 }
51 } // namespace
52
53 class LibAbcKitCreateCmpStaticTest : public ::testing::Test {};
54
55 template <bool IS_EQ_TEST>
GetSchema()56 static std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> GetSchema()
57 {
58 size_t firstConstIdx = 1;
59 size_t secondConstIdx = 2;
60 size_t cmpIdx = 3;
61 size_t retIdx = 4;
62 if constexpr (IS_EQ_TEST) {
63 secondConstIdx = firstConstIdx;
64 cmpIdx--;
65 retIdx--;
66 }
67 std::vector<helpers::InstSchema<AbckitIsaApiStaticOpcode>> constInsts = {
68 {0, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {firstConstIdx, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}};
69 if constexpr (!IS_EQ_TEST) {
70 constInsts.push_back({secondConstIdx, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}});
71 }
72 return {{{}, {1}, {constInsts}},
73 {{0},
74 {2},
75 {
76 {cmpIdx, ABCKIT_ISA_API_STATIC_OPCODE_CMP, {firstConstIdx, secondConstIdx}},
77 {retIdx, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {cmpIdx}},
78 }},
79 {{1}, {}, {}}};
80 }
81
82 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateCmp, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateCmpStaticTest,LibAbcKitTestCmpI1)83 TEST_F(LibAbcKitCreateCmpStaticTest, LibAbcKitTestCmpI1)
84 {
85 helpers::TransformMethod(
86 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static.abc",
87 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc", "foo",
88 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
89 AbckitInst *firstConst = g_implG->gFindOrCreateConstantI64(graph, 10);
90 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
91 AbckitInst *secondConst = g_implG->gFindOrCreateConstantI64(graph, 7);
92 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
93 TransformIrIcreateCmp(graph, firstConst, secondConst);
94 },
95 [](AbckitGraph *graph) { helpers::VerifyGraph(graph, GetSchema<false>()); });
96
97 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc",
98 "create_cmp_static/ETSGLOBAL", "main");
99 EXPECT_TRUE(helpers::Match(output, "1\n"));
100 }
101
102 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateCmp, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateCmpStaticTest,LibAbcKitTestCmpI2)103 TEST_F(LibAbcKitCreateCmpStaticTest, LibAbcKitTestCmpI2)
104 {
105 helpers::TransformMethod(
106 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static.abc",
107 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc", "foo",
108 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
109 AbckitInst *firstConst = g_implG->gFindOrCreateConstantI64(graph, 0);
110 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
111 AbckitInst *secondConst = g_implG->gFindOrCreateConstantI64(graph, 24);
112 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
113 TransformIrIcreateCmp(graph, firstConst, secondConst);
114 },
115 [](AbckitGraph *graph) { helpers::VerifyGraph(graph, GetSchema<false>()); });
116
117 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc",
118 "create_cmp_static/ETSGLOBAL", "main");
119 EXPECT_TRUE(helpers::Match(output, "-1\n"));
120 }
121
122 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateCmp, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateCmpStaticTest,LibAbcKitTestCmpI3)123 TEST_F(LibAbcKitCreateCmpStaticTest, LibAbcKitTestCmpI3)
124 {
125 helpers::TransformMethod(
126 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static.abc",
127 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc", "foo",
128 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
129 AbckitInst *firstConst = g_implG->gFindOrCreateConstantI64(graph, 7);
130 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
131 TransformIrIcreateCmp(graph, firstConst, firstConst);
132 },
133 [](AbckitGraph *graph) { helpers::VerifyGraph(graph, GetSchema<true>()); });
134
135 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc",
136 "create_cmp_static/ETSGLOBAL", "main");
137 EXPECT_TRUE(helpers::Match(output, "0\n"));
138 }
139
140 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateCmp, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateCmpStaticTest,LibAbcKitTestCmpD1)141 TEST_F(LibAbcKitCreateCmpStaticTest, LibAbcKitTestCmpD1)
142 {
143 helpers::TransformMethod(
144 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static.abc",
145 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc", "foo",
146 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
147 AbckitInst *firstConst = g_implG->gFindOrCreateConstantF64(graph, 12.4);
148 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
149 AbckitInst *secondConst = g_implG->gFindOrCreateConstantF64(graph, 4.5);
150 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
151 TransformIrIcreateCmp(graph, firstConst, secondConst);
152 },
153 [](AbckitGraph *graph) { helpers::VerifyGraph(graph, GetSchema<false>()); });
154
155 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc",
156 "create_cmp_static/ETSGLOBAL", "main");
157 EXPECT_TRUE(helpers::Match(output, "1\n"));
158 }
159
160 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateCmp, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateCmpStaticTest,LibAbcKitTestCmpD2)161 TEST_F(LibAbcKitCreateCmpStaticTest, LibAbcKitTestCmpD2)
162 {
163 helpers::TransformMethod(
164 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static.abc",
165 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc", "foo",
166 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
167 AbckitInst *firstConst = g_implG->gFindOrCreateConstantF64(graph, 4.3);
168 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
169 AbckitInst *secondConst = g_implG->gFindOrCreateConstantF64(graph, 13.6);
170 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
171 TransformIrIcreateCmp(graph, firstConst, secondConst);
172 },
173 [](AbckitGraph *graph) { helpers::VerifyGraph(graph, GetSchema<false>()); });
174
175 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc",
176 "create_cmp_static/ETSGLOBAL", "main");
177 EXPECT_TRUE(helpers::Match(output, "-1\n"));
178 }
179
180 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateCmp, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateCmpStaticTest,LibAbcKitTestCmpD3)181 TEST_F(LibAbcKitCreateCmpStaticTest, LibAbcKitTestCmpD3)
182 {
183 helpers::TransformMethod(
184 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static.abc",
185 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc", "foo",
186 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
187 AbckitInst *firstConst = g_implG->gFindOrCreateConstantF64(graph, 7.6);
188 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
189 TransformIrIcreateCmp(graph, firstConst, firstConst);
190 },
191 [](AbckitGraph *graph) { helpers::VerifyGraph(graph, GetSchema<true>()); });
192
193 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc",
194 "create_cmp_static/ETSGLOBAL", "main");
195 EXPECT_TRUE(helpers::Match(output, "0\n"));
196 }
197
198 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateCmp, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitCreateCmpStaticTest,LibAbcKitTestCmpNegative)199 TEST_F(LibAbcKitCreateCmpStaticTest, LibAbcKitTestCmpNegative)
200 {
201 helpers::TransformMethod(
202 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static.abc",
203 ABCKIT_ABC_DIR "ut/isa/isa_static/cmp/create_cmp_static_modified_set.abc", "foo",
204 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
205 AbckitInst *firstConst = g_implG->gFindOrCreateConstantI64(graph, 10);
206 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
207 AbckitInst *secondConst = g_implG->gFindOrCreateConstantF64(graph, 7.2);
208 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_NO_ERROR);
209 AbckitInst *cmp = g_statG->iCreateCmp(graph, firstConst, secondConst);
210 EXPECT_TRUE(cmp == nullptr);
211 EXPECT_TRUE(g_impl->getLastError() == ABCKIT_STATUS_BAD_ARGUMENT);
212 },
213 []([[maybe_unused]] AbckitGraph *graph) {});
214 }
215
216 } // namespace libabckit::test
217 // NOLINTEND(readability-magic-numbers)
218