• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-2025 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/abckit.h"
19 
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22 
23 #include <gtest/gtest.h>
24 
25 // NOLINTBEGIN(readability-magic-numbers)
26 namespace libabckit::test {
27 
28 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 
34 class LibAbcKitCreateNullPtrInstTest : public ::testing::Test {};
35 
TransformIrCreateNullPtrInstValid(AbckitGraph * graph)36 static void TransformIrCreateNullPtrInstValid(AbckitGraph *graph)
37 {
38     AbckitInst *mainInst = g_statG->gCreateNullPtr(graph);
39     ASSERT_NE(mainInst, nullptr);
40     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
41 
42     auto callOp = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC);
43     g_implG->iSetInput(callOp, mainInst, 0);
44     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
45 }
46 
47 // Test: test-kind=api, api=IsaApiStaticImpl::gCreateNullPtr, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateNullPtrInstTest,CreateNullPtrValid)48 TEST_F(LibAbcKitCreateNullPtrInstTest, CreateNullPtrValid)
49 {
50     auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/create_nullptr/create_nullptr_static.abc",
51                                             "create_nullptr_static/ETSGLOBAL", "main");
52     EXPECT_TRUE(helpers::Match(output, "10\n"));
53 
54     helpers::TransformMethod(
55         ABCKIT_ABC_DIR "ut/isa/isa_static/create_nullptr/create_nullptr_static.abc",
56         ABCKIT_ABC_DIR "ut/isa/isa_static/create_nullptr/create_nullptr_static_modified.abc", "foo",
57         [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
58             TransformIrCreateNullPtrInstValid(graph);
59         },
60         [&](AbckitGraph *graph) {
61             std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
62                 {{{},
63                   {1},
64                   {
65                       {0, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
66                       {1, ABCKIT_ISA_API_STATIC_OPCODE_NULLPTR, {}},
67                   }},
68                  {{0},
69                   {2},
70                   {{2, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC, {1}},
71                    {3, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}}}},
72                  {{1}, {}, {}}});
73             helpers::VerifyGraph(graph, bbSchemas);
74         });
75 
76     output =
77         helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/create_nullptr/create_nullptr_static_modified.abc",
78                                   "create_nullptr_static/ETSGLOBAL", "main");
79     EXPECT_TRUE(helpers::Match(output, "undefined\n"));
80 }
81 
82 }  // namespace libabckit::test
83 // NOLINTEND(readability-magic-numbers)
84