• 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 
20 #include "helpers/helpers_runtime.h"
21 #include "helpers/helpers.h"
22 
23 #include <gtest/gtest.h>
24 
25 namespace libabckit::test {
26 
27 namespace {
28 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 
TransformSetStringIrStatic(AbckitGraph * graph,AbckitFile * file)34 void TransformSetStringIrStatic(AbckitGraph *graph, AbckitFile *file)
35 {
36     auto *loadStr = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_LOADSTRING);
37     ASSERT_NE(loadStr, nullptr);
38     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
39 
40     auto *newStr = g_implM->createString(file, "STRING", strlen("STRING"));
41     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
42 
43     g_implG->iSetString(loadStr, newStr);
44     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
45 }
46 
CheckGetStringIrStatic(AbckitGraph * graph)47 void CheckGetStringIrStatic(AbckitGraph *graph)
48 {
49     auto *loadStr = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_LOADSTRING);
50     ASSERT_NE(loadStr, nullptr);
51     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
52 
53     auto *str = g_implG->iGetString(loadStr);
54     ASSERT_NE(str, nullptr);
55     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
56 
57     ASSERT_EQ(helpers::AbckitStringToString(str), "string");
58 }
59 
60 }  // namespace
61 
62 class LibAbcKitStringStaticTest : public ::testing::Test {};
63 
64 // Test: test-kind=api, api=GraphApiImpl::iSetString, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitStringStaticTest,LibAbcKitTestSetString)65 TEST_F(LibAbcKitStringStaticTest, LibAbcKitTestSetString)
66 {
67     auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/string/string_static.abc",
68                                             "string_static/ETSGLOBAL", "main");
69     EXPECT_TRUE(helpers::Match(output, "string\n"));
70 
71     helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/string/string_static.abc",
72                              ABCKIT_ABC_DIR "ut/ir_core/string/string_static_modified.abc", "foo",
73                              [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
74                                  TransformSetStringIrStatic(graph, file);
75                              });
76 
77     output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/string/string_static_modified.abc",
78                                        "string_static/ETSGLOBAL", "main");
79     EXPECT_TRUE(helpers::Match(output, "STRING\n"));
80 }
81 
82 // Test: test-kind=api, api=GraphApiImpl::iGetString, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitStringStaticTest,LibAbcKitTestGetString)83 TEST_F(LibAbcKitStringStaticTest, LibAbcKitTestGetString)
84 {
85     helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/string/string_static.abc",
86                              ABCKIT_ABC_DIR "ut/ir_core/string/string_static_modified.abc", "foo",
87                              [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
88                                  CheckGetStringIrStatic(graph);
89                              });
90 }
91 
92 }  // namespace libabckit::test
93