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/metadata_core.h"
17 #include "libabckit/include/c/ir_core.h"
18 #include "libabckit/include/c/isa/isa_dynamic.h"
19 #include "libabckit/include/c/abckit.h"
20
21 #include "helpers/helpers.h"
22 #include "helpers/helpers_runtime.h"
23
24 #include <gtest/gtest.h>
25
26 // NOLINTBEGIN(readability-magic-numbers)
27 namespace libabckit::test {
28
29 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34
35 class LibAbcKitCreateDynCreateIterResultObjInstTest : public ::testing::Test {};
36
TransformIr(AbckitGraph * graph)37 static void TransformIr(AbckitGraph *graph)
38 {
39 AbckitBasicBlock *startBB = g_implG->gGetStartBasicBlock(graph);
40 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
41
42 AbckitInst *curInst = g_implG->iGetPrev(g_implG->bbGetLastInst(startBB));
43 AbckitInst *firstInst = nullptr;
44 AbckitInst *secondInst = nullptr;
45 while (curInst != nullptr) {
46 AbckitIsaApiDynamicOpcode curOpcode = g_dynG->iGetOpcode(curInst);
47 if (curOpcode != ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER) {
48 curInst = g_implG->iGetNext(curInst);
49 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
50 continue;
51 }
52 firstInst = curInst;
53 curInst = g_implG->iGetNext(curInst);
54 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
55 if (curOpcode != ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER) {
56 curInst = g_implG->iGetNext(curInst);
57 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
58 continue;
59 }
60 secondInst = curInst;
61 break;
62 }
63
64 std::vector<AbckitBasicBlock *> succBBs = helpers::BBgetSuccBlocks(startBB);
65 AbckitBasicBlock *first = succBBs[0];
66
67 curInst = g_implG->bbGetFirstInst(first);
68 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
69
70 AbckitInst *inst = g_dynG->iCreateCreateiterresultobj(graph, firstInst, secondInst);
71 while (curInst != nullptr) {
72 AbckitIsaApiDynamicOpcode curOpcode = g_dynG->iGetOpcode(curInst);
73 if (curOpcode != ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN) {
74 curInst = g_implG->iGetNext(curInst);
75 continue;
76 }
77 g_implG->iInsertBefore(inst, curInst);
78 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
79
80 g_implG->iSetInput(curInst, inst, 0);
81 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
82
83 curInst = g_implG->iGetNext(curInst);
84 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
85 }
86 }
87
88 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCreateiterresultobj, abc-kind=ArkTS1, category=positive,
89 // extension=c
TEST_F(LibAbcKitCreateDynCreateIterResultObjInstTest,CreateDynCreateiterresultobjValid)90 TEST_F(LibAbcKitCreateDynCreateIterResultObjInstTest, CreateDynCreateiterresultobjValid)
91 {
92 auto output = helpers::ExecuteDynamicAbc(
93 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/iterators/createiterresultobj_dynamic.abc", "createiterresultobj_dynamic");
94 EXPECT_TRUE(helpers::Match(output, "1\n"));
95
96 helpers::TransformMethod(
97 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/iterators/createiterresultobj_dynamic.abc",
98 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/iterators/createiterresultobj_dynamic_modified.abc", "foo",
99 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
100 TransformIr(graph);
101 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
102 },
103 [&](AbckitGraph *graph) {
104 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(
105 {{{},
106 {1},
107 {
108 {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
109 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
110 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
111 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
112 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
113 }},
114 {{0},
115 {2},
116 {{5, ABCKIT_ISA_API_DYNAMIC_OPCODE_ADD2, {4, 3}},
117 {6, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEITERRESULTOBJ, {3, 4}},
118 {7, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {6}}}},
119 {{1}, {}, {}}});
120 helpers::VerifyGraph(graph, bbSchemas);
121 });
122
123 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR
124 "ut/isa/isa_dynamic/iterators/createiterresultobj_dynamic_modified.abc",
125 "createiterresultobj_dynamic");
126 EXPECT_TRUE(helpers::Match(output, "\\[object Object\\]\n"));
127 }
128
129 } // namespace libabckit::test
130 // NOLINTEND(readability-magic-numbers)
131