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/include/c/statuses.h"
18 #include "libabckit/include/c/metadata_core.h"
19 #include "libabckit/include/c/ir_core.h"
20 #include "libabckit/include/c/isa/isa_dynamic.h"
21 #include "helpers/helpers_runtime.h"
22 #include "helpers/helpers.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 LibAbcKitObjectsTest : public ::testing::Test {};
36
37 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCreateemptyobject, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitObjectsTest,IcreateDynCreateemptyobject)38 TEST_F(LibAbcKitObjectsTest, IcreateDynCreateemptyobject)
39 {
40 auto output =
41 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/objects/objects_dynamic.abc", "objects_dynamic");
42 EXPECT_TRUE(helpers::Match(output, "\n"));
43
44 helpers::TransformMethod(
45 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/objects/objects_dynamic.abc",
46 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/objects/objects_dynamic_modified.abc", "test",
47 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
48 auto *callarg0 = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG0);
49 ASSERT_NE(callarg0, nullptr);
50 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
51
52 auto *print = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_TRYLDGLOBALBYNAME);
53 ASSERT_NE(print, nullptr);
54 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
55
56 g_implG->iRemove(callarg0);
57 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
58
59 auto *emptyobj = g_dynG->iCreateCreateemptyobject(graph);
60 ASSERT_NE(emptyobj, nullptr);
61 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
62
63 auto *callarg1 = g_dynG->iCreateCallarg1(graph, print, emptyobj);
64 ASSERT_NE(callarg1, nullptr);
65
66 g_implG->iInsertAfter(callarg1, print);
67 g_implG->iInsertBefore(emptyobj, print);
68 },
69 [&](AbckitGraph *graph) {
70 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchema = {
71 {{},
72 {1},
73 {
74 {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
75 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
76 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
77 }},
78 {{0},
79 {2},
80 {
81 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEEMPTYOBJECT, {}},
82 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_TRYLDGLOBALBYNAME, {}},
83 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1, {4, 3}},
84 {6, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED, {}},
85 }},
86 {{1}, {}, {}}};
87
88 helpers::VerifyGraph(graph, bbSchema);
89 });
90
91 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/objects/objects_dynamic_modified.abc",
92 "objects_dynamic");
93 EXPECT_TRUE(helpers::Match(output, "\\[object Object\\]\n"));
94 }
95
96 } // namespace libabckit::test
97 // NOLINTEND(readability-magic-numbers)
98