• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/metadata_core.h"
18 #include "libabckit/include/c/ir_core.h"
19 #include "libabckit/include/c/isa/isa_dynamic.h"
20 
21 #include "helpers/helpers_runtime.h"
22 #include "helpers/helpers.h"
23 
24 #include <gtest/gtest.h>
25 
26 // NOLINTBEGIN(readability-magic-numbers)
27 
28 namespace libabckit::test {
29 
30 namespace {
31 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 
TransformSetSetCallMethod(AbckitGraph * graph,AbckitCoreFunction * foo)37 void TransformSetSetCallMethod(AbckitGraph *graph, AbckitCoreFunction *foo)
38 {
39     auto *defineProp = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEPROPERTYBYNAME);
40     ASSERT_NE(defineProp, nullptr);
41     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
42 
43     auto *defineFunc = g_implG->iGetPrev(defineProp);
44     ASSERT_NE(defineFunc, nullptr);
45     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
46     ASSERT_EQ(g_dynG->iGetOpcode(defineFunc), ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
47 
48     g_implG->iSetFunction(defineFunc, foo);
49     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
50 }
51 }  // namespace
52 
53 class LibAbcKitMethodDynamicTest : public ::testing::Test {};
54 
55 // Test: test-kind=api, api=GraphApiImpl::iSetFunction, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitMethodDynamicTest,LibAbcKitTestSetCallMethod)56 TEST_F(LibAbcKitMethodDynamicTest, LibAbcKitTestSetCallMethod)
57 {
58     auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/method/method_dynamic.abc", "method_dynamic");
59     EXPECT_TRUE(helpers::Match(output, "function\n"));
60 
61     helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/method/method_dynamic.abc",
62                              ABCKIT_ABC_DIR "ut/ir_core/method/method_dynamic_modified.abc", "func_main_0",
63                              [](AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph *graph) {
64                                  auto *bar = helpers::FindMethodByName(g_implI->functionGetFile(method), "foo");
65                                  ASSERT_NE(bar, nullptr);
66                                  TransformSetSetCallMethod(graph, bar);
67                              });
68 
69     output =
70         helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/method/method_dynamic_modified.abc", "method_dynamic");
71     EXPECT_TRUE(helpers::Match(output, "foo\n"));
72 }
73 
74 }  // namespace libabckit::test
75 
76 // NOLINTEND(readability-magic-numbers)
77