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 LibAbcKitCreateDynGetIteratorInstTest : 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 = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
43 AbckitInst *firstInst = nullptr;
44 while (curInst != nullptr) {
45 AbckitIsaApiDynamicOpcode curOpcode = g_dynG->iGetOpcode(curInst);
46 if (curOpcode != ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER) {
47 curInst = g_implG->iGetNext(curInst);
48 continue;
49 }
50 firstInst = curInst;
51 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
52 break;
53 }
54
55 std::vector<AbckitBasicBlock *> succBBs = helpers::BBgetSuccBlocks(startBB);
56 AbckitBasicBlock *first = succBBs[0];
57
58 curInst = g_implG->bbGetFirstInst(first);
59 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
60
61 AbckitInst *inst = g_dynG->iCreateGetiterator(graph, firstInst);
62 while (curInst != nullptr) {
63 AbckitIsaApiDynamicOpcode curOpcode = g_dynG->iGetOpcode(curInst);
64 if (curOpcode != ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN) {
65 curInst = g_implG->iGetNext(curInst);
66 continue;
67 }
68 g_implG->iInsertBefore(inst, curInst);
69 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
70
71 g_implG->iSetInput(curInst, inst, 0);
72 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
73
74 curInst = g_implG->iGetNext(curInst);
75 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
76 }
77 }
78
79 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateGetiterator, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynGetIteratorInstTest,CreateDynGetiteratorValid)80 TEST_F(LibAbcKitCreateDynGetIteratorInstTest, CreateDynGetiteratorValid)
81 {
82 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/iterators/getiterator_dynamic.abc",
83 "getiterator_dynamic");
84 EXPECT_TRUE(helpers::Match(output, "10\n"));
85
86 helpers::TransformMethod(
87 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/iterators/getiterator_dynamic.abc",
88 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/iterators/getiterator_dynamic_modified.abc", "foo",
89 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
90 TransformIr(graph);
91 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
92 },
93 [&](AbckitGraph *graph) {
94 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(
95 {{{},
96 {1},
97 {
98 {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
99 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
100 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
101 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
102 }},
103 {{0},
104 {2},
105 {{4, ABCKIT_ISA_API_DYNAMIC_OPCODE_GETITERATOR, {3}},
106 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {4}}}},
107 {{1}, {}, {}}});
108 helpers::VerifyGraph(graph, bbSchemas);
109 });
110
111 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/iterators/getiterator_dynamic_modified.abc",
112 "getiterator_dynamic");
113 EXPECT_TRUE(helpers::Match(output, "\\[object Array Iterator\\]\n"));
114 }
115
116 } // namespace libabckit::test
117 // NOLINTEND(readability-magic-numbers)
118