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 LibAbcKitCreateDynCreateobjectwithbuffer : public ::testing::Test {};
36
37 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCreateobjectwithbuffer, abc-kind=ArkTS1, category=positive,
38 // extension=c
TEST_F(LibAbcKitCreateDynCreateobjectwithbuffer,IcreateCreateobjectwithbuffer_1)39 TEST_F(LibAbcKitCreateDynCreateobjectwithbuffer, IcreateCreateobjectwithbuffer_1)
40 {
41 auto output =
42 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic.abc",
43 "createobjectwithbuffer_dynamic");
44 EXPECT_TRUE(helpers::Match(output, ""));
45
46 helpers::TransformMethod(
47 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic.abc",
48 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic_modified.abc", "func_main_0",
49 [&](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
50 auto arr = std::vector<AbckitLiteral *>();
51 arr.emplace_back(g_implM->createLiteralString(file, "a", strlen("a")));
52 arr.emplace_back(g_implM->createLiteralU32(file, 1));
53 arr.emplace_back(g_implM->createLiteralString(file, "b", strlen("b")));
54 arr.emplace_back(g_implM->createLiteralString(file, "str", strlen("str")));
55 auto *litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
56
57 auto *createobjectwithbuffer = g_dynG->iCreateCreateobjectwithbuffer(graph, litArr);
58 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
59 auto *stringPrint = g_implM->createString(file, "print", strlen("print"));
60 auto *tryldglobalbyname = g_dynG->iCreateTryldglobalbyname(graph, stringPrint);
61 auto *stringA = g_implM->createString(file, "a", strlen("a"));
62 auto *ldobjbynameA = g_dynG->iCreateLdobjbyname(graph, createobjectwithbuffer, stringA);
63 auto *stringB = g_implM->createString(file, "b", strlen("b"));
64 auto *ldobjbynameB = g_dynG->iCreateLdobjbyname(graph, createobjectwithbuffer, stringB);
65 auto *callarg10 = g_dynG->iCreateCallarg1(graph, tryldglobalbyname, ldobjbynameA);
66 auto *callarg11 = g_dynG->iCreateCallarg1(graph, tryldglobalbyname, ldobjbynameB);
67
68 auto *returnundef = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
69 g_implG->iInsertBefore(createobjectwithbuffer, returnundef);
70 g_implG->iInsertBefore(tryldglobalbyname, returnundef);
71 g_implG->iInsertBefore(ldobjbynameA, returnundef);
72 g_implG->iInsertBefore(ldobjbynameB, returnundef);
73 g_implG->iInsertBefore(callarg10, returnundef);
74 g_implG->iInsertBefore(callarg11, returnundef);
75 });
76
77 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR
78 "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic_modified.abc",
79 "createobjectwithbuffer_dynamic");
80 EXPECT_TRUE(helpers::Match(output, "1\nstr\n"));
81 }
82
83 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCreateobjectwithbuffer, abc-kind=ArkTS1, category=positive,
84 // extension=c
TEST_F(LibAbcKitCreateDynCreateobjectwithbuffer,IcreateCreateobjectwithbuffer_2)85 TEST_F(LibAbcKitCreateDynCreateobjectwithbuffer, IcreateCreateobjectwithbuffer_2)
86 {
87 auto output =
88 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic.abc",
89 "createobjectwithbuffer_dynamic");
90 EXPECT_TRUE(helpers::Match(output, ""));
91
92 helpers::TransformMethod(
93 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic.abc",
94 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic_modified.abc", "func_main_0",
95 [&](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
96 auto arr = std::vector<AbckitLiteral *>();
97 arr.emplace_back(g_implM->createLiteralString(file, "toString", strlen("toString")));
98 arr.emplace_back(g_implM->createLiteralMethod(file, helpers::FindMethodByName(file, "toString")));
99 arr.emplace_back(g_implM->createLiteralMethodAffiliate(file, 0));
100 auto *litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
101
102 auto *createobjectwithbuffer = g_dynG->iCreateCreateobjectwithbuffer(graph, litArr);
103 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
104 auto *stringPrint = g_implM->createString(file, "print", strlen("print"));
105 auto *tryldglobalbyname = g_dynG->iCreateTryldglobalbyname(graph, stringPrint);
106 auto *callarg1 = g_dynG->iCreateCallarg1(graph, tryldglobalbyname, createobjectwithbuffer);
107
108 auto *returnundef = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
109 g_implG->iInsertBefore(createobjectwithbuffer, returnundef);
110 g_implG->iInsertBefore(tryldglobalbyname, returnundef);
111 g_implG->iInsertBefore(callarg1, returnundef);
112 });
113
114 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR
115 "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic_modified.abc",
116 "createobjectwithbuffer_dynamic");
117 EXPECT_TRUE(helpers::Match(output, "objA\n"));
118 }
119
120 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCreateobjectwithbuffer, abc-kind=ArkTS1, category=positive,
121 // extension=c
TEST_F(LibAbcKitCreateDynCreateobjectwithbuffer,IcreateCreateobjectwithbuffer_3)122 TEST_F(LibAbcKitCreateDynCreateobjectwithbuffer, IcreateCreateobjectwithbuffer_3)
123 {
124 auto output =
125 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic.abc",
126 "createobjectwithbuffer_dynamic");
127 EXPECT_TRUE(helpers::Match(output, ""));
128
129 helpers::TransformMethod(
130 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic.abc",
131 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic_modified.abc", "func_main_0",
132 [&](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
133 auto arr = std::vector<AbckitLiteral *>();
134 arr.emplace_back(g_implM->createLiteralString(file, "foo", strlen("foo")));
135 arr.emplace_back(g_implM->createLiteralMethod(file, helpers::FindMethodByName(file, "foo")));
136 arr.emplace_back(g_implM->createLiteralMethodAffiliate(file, 0));
137 arr.emplace_back(g_implM->createLiteralString(file, "bar", strlen("bar")));
138 arr.emplace_back(g_implM->createLiteralMethod(file, helpers::FindMethodByName(file, "bar")));
139 arr.emplace_back(g_implM->createLiteralMethodAffiliate(file, 1));
140 auto *litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
141
142 auto *createobjectwithbuffer = g_dynG->iCreateCreateobjectwithbuffer(graph, litArr);
143 auto *stringFoo = g_implM->createString(file, "foo", strlen("foo"));
144 auto *stringBar = g_implM->createString(file, "bar", strlen("bar"));
145 auto *ldobjbynameFoo = g_dynG->iCreateLdobjbyname(graph, createobjectwithbuffer, stringFoo);
146 auto *ldobjbynameBar = g_dynG->iCreateLdobjbyname(graph, createobjectwithbuffer, stringBar);
147 auto *callthisFoo = g_dynG->iCreateCallthis0(graph, ldobjbynameFoo, createobjectwithbuffer);
148 auto *callthisBar = g_dynG->iCreateCallthis1(graph, ldobjbynameBar, createobjectwithbuffer,
149 g_implG->gFindOrCreateConstantU64(graph, 777));
150
151 auto *returnundef = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
152 g_implG->iInsertBefore(createobjectwithbuffer, returnundef);
153 g_implG->iInsertBefore(ldobjbynameFoo, returnundef);
154 g_implG->iInsertBefore(ldobjbynameBar, returnundef);
155 g_implG->iInsertBefore(callthisFoo, returnundef);
156 g_implG->iInsertBefore(callthisBar, returnundef);
157 });
158
159 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR
160 "ut/isa/isa_dynamic/create/createobjectwithbuffer_dynamic_modified.abc",
161 "createobjectwithbuffer_dynamic");
162 EXPECT_TRUE(helpers::Match(output, "foo\nbar 777\n"));
163 }
164 } // namespace libabckit::test
165 // NOLINTEND(readability-magic-numbers)
166