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 #include "libabckit/include/c/statuses.h"
24
25 #include <gtest/gtest.h>
26
27 // NOLINTBEGIN(readability-magic-numbers)
28 namespace libabckit::test {
29
30 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35
36 class LibAbcKitCreateDynDefineField : public ::testing::Test {};
37
38 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateDefinefieldbyname, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynDefineField,IcreateDefinefieldbyname_1)39 TEST_F(LibAbcKitCreateDynDefineField, IcreateDefinefieldbyname_1)
40 {
41 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
42 "definefieldbyname_dynamic");
43 EXPECT_TRUE(helpers::Match(output, "2\nundefined\n"));
44
45 helpers::TransformMethod(
46 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
47 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc", "instance_initializer",
48 [&](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
49 auto *name = g_implM->createString(file, "b", strlen("b"));
50 ASSERT_NE(name, nullptr);
51 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
52
53 auto *constant = g_implG->gFindOrCreateConstantI32(graph, 4);
54 ASSERT_NE(constant, nullptr);
55 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
56
57 auto *par = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
58 ASSERT_NE(par, nullptr);
59 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
60
61 auto *field = g_dynG->iCreateDefinefieldbyname(graph, constant, name, par);
62 ASSERT_NE(field, nullptr);
63 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
64
65 auto *mainBB = g_implG->gGetBasicBlock(graph, 0);
66 ASSERT_NE(mainBB, nullptr);
67 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
68
69 g_implG->bbAddInstFront(mainBB, field);
70 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
71 },
72 [&]([[maybe_unused]] AbckitGraph *graph) {});
73
74 output = helpers::ExecuteDynamicAbc(
75 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc", "definefieldbyname_dynamic");
76 EXPECT_TRUE(helpers::Match(output, "2\n4\n"));
77 }
78
79 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateDefinefieldbyname, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynDefineField,IcreateDefinefieldbyname_2)80 TEST_F(LibAbcKitCreateDynDefineField, IcreateDefinefieldbyname_2)
81 {
82 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
83 "definefieldbyname_dynamic");
84 EXPECT_TRUE(helpers::Match(output, "2\nundefined\n"));
85
86 helpers::TransformMethod(
87 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
88 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc", "instance_initializer",
89 [&](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
90 auto *name = g_implM->createString(file, "b", strlen("b"));
91 ASSERT_NE(name, nullptr);
92 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
93
94 auto *str = g_implM->createString(file, "Hello", strlen("Hello"));
95 ASSERT_NE(str, nullptr);
96 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
97
98 auto *ldstr = g_dynG->iCreateLoadString(graph, str);
99 ASSERT_NE(ldstr, nullptr);
100 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
101
102 auto *par = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
103 ASSERT_NE(par, nullptr);
104 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
105
106 auto *field = g_dynG->iCreateDefinefieldbyname(graph, ldstr, name, par);
107 ASSERT_NE(field, nullptr);
108 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
109
110 auto *mainBB = g_implG->gGetBasicBlock(graph, 0);
111 ASSERT_NE(mainBB, nullptr);
112 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
113
114 g_implG->bbAddInstFront(mainBB, field);
115 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
116 g_implG->bbAddInstFront(mainBB, ldstr);
117 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
118 },
119 [&]([[maybe_unused]] AbckitGraph *graph) {});
120
121 output = helpers::ExecuteDynamicAbc(
122 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc", "definefieldbyname_dynamic");
123 EXPECT_TRUE(helpers::Match(output, "2\nHello\n"));
124 }
125
126 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateDefinepropertybyname, abc-kind=ArkTS1, category=positive,
127 // extension=c
TEST_F(LibAbcKitCreateDynDefineField,IcreateDefinepropertybyname_1)128 TEST_F(LibAbcKitCreateDynDefineField, IcreateDefinepropertybyname_1)
129 {
130 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
131 "definefieldbyname_dynamic");
132 EXPECT_TRUE(helpers::Match(output, "2\nundefined\n"));
133
134 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
135 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc",
136 "instance_initializer",
137 [&](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
138 auto *name = g_implM->createString(file, "b", strlen("b"));
139 ASSERT_NE(name, nullptr);
140 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
141
142 auto *constant = g_implG->gFindOrCreateConstantI32(graph, 4);
143 ASSERT_NE(constant, nullptr);
144 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
145
146 auto *par = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
147 ASSERT_NE(par, nullptr);
148 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
149
150 auto *prop = g_dynG->iCreateDefinepropertybyname(graph, constant, name, par);
151 ASSERT_NE(prop, nullptr);
152 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
153
154 auto *mainBB = g_implG->gGetBasicBlock(graph, 0);
155 ASSERT_NE(mainBB, nullptr);
156 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
157
158 g_implG->bbAddInstFront(mainBB, prop);
159 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
160 });
161
162 output = helpers::ExecuteDynamicAbc(
163 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc", "definefieldbyname_dynamic");
164 EXPECT_TRUE(helpers::Match(output, "2\n4\n"));
165 }
166
167 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateDefinepropertybyname, abc-kind=ArkTS1, category=positive,
168 // extension=c
TEST_F(LibAbcKitCreateDynDefineField,IcreateDefinepropertybyname_2)169 TEST_F(LibAbcKitCreateDynDefineField, IcreateDefinepropertybyname_2)
170 {
171 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
172 "definefieldbyname_dynamic");
173 EXPECT_TRUE(helpers::Match(output, "2\nundefined\n"));
174
175 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic.abc",
176 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc",
177 "instance_initializer",
178 [&](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
179 auto *name = g_implM->createString(file, "b", strlen("b"));
180 ASSERT_NE(name, nullptr);
181 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
182
183 auto *str = g_implM->createString(file, "World", strlen("World"));
184 ASSERT_NE(str, nullptr);
185 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
186
187 auto *ldstr = g_dynG->iCreateLoadString(graph, str);
188 ASSERT_NE(ldstr, nullptr);
189 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
190
191 auto *par = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
192 ASSERT_NE(par, nullptr);
193 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
194
195 auto *prop = g_dynG->iCreateDefinepropertybyname(graph, ldstr, name, par);
196 ASSERT_NE(prop, nullptr);
197 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
198
199 auto *mainBB = g_implG->gGetBasicBlock(graph, 0);
200 ASSERT_NE(mainBB, nullptr);
201 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
202
203 g_implG->bbAddInstFront(mainBB, prop);
204 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
205 g_implG->bbAddInstFront(mainBB, ldstr);
206 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
207 });
208
209 output = helpers::ExecuteDynamicAbc(
210 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefieldbyname_dynamic_modified.abc", "definefieldbyname_dynamic");
211 EXPECT_TRUE(helpers::Match(output, "2\nWorld\n"));
212 }
213
214 } // namespace libabckit::test
215 // NOLINTEND(readability-magic-numbers)
216