• 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 <gtest/gtest.h>
17 
18 #include "libabckit/include/c/abckit.h"
19 #include "helpers/helpers.h"
20 
21 namespace libabckit::test {
22 
23 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
24 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
25 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
26 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
27 
28 class LibAbcKitMemoryHandling : public ::testing::Test {};
29 
30 // Test: test-kind=api, api=ApiImpl::openAbc, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitMemoryHandling,OpenAbcStatic)31 TEST_F(LibAbcKitMemoryHandling, OpenAbcStatic)
32 {
33     constexpr auto PATH1 = ABCKIT_ABC_DIR "internal/mem_manager/abc_static_1.abc";
34     constexpr auto PATH2 = ABCKIT_ABC_DIR "internal/mem_manager/abc_static_2.abc";
35     auto *file = g_impl->openAbc(PATH1, strlen(PATH1));
36     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
37     auto *ctxI2 = g_impl->openAbc(PATH2, strlen(PATH2));
38     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
39 
40     auto *main = helpers::FindMethodByName(file, "main");
41     ASSERT_NE(main, nullptr);
42     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
43     auto *main2 = helpers::FindMethodByName(ctxI2, "main");
44     ASSERT_NE(main2, nullptr);
45     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
46 
47     auto transformMain = [](AbckitCoreFunction *method) {
48         auto *graph = g_implI->createGraphFromFunction(method);
49         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
50         g_implM->functionSetGraph(method, graph);
51         g_impl->destroyGraph(graph);
52         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
53     };
54 
55     transformMain(main);
56     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
57     transformMain(main2);
58     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
59 
60     g_impl->writeAbc(file, PATH1, strlen(PATH1));
61     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
62     g_impl->closeFile(file);
63     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
64     g_impl->writeAbc(ctxI2, PATH2, strlen(PATH2));
65     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
66     g_impl->closeFile(ctxI2);
67     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
68 }
69 
70 // Test: test-kind=api, api=ApiImpl::openAbc, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitMemoryHandling,OpenAbcDynamic)71 TEST_F(LibAbcKitMemoryHandling, OpenAbcDynamic)
72 {
73     constexpr auto PATH1 = ABCKIT_ABC_DIR "internal/mem_manager/abc_dynamic_1.abc";
74     constexpr auto PATH2 = ABCKIT_ABC_DIR "internal/mem_manager/abc_dynamic_2.abc";
75     auto *file = g_impl->openAbc(PATH1, strlen(PATH1));
76     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
77     auto *ctxI2 = g_impl->openAbc(PATH2, strlen(PATH2));
78     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
79 
80     auto *main = helpers::FindMethodByName(file, "func_main_0");
81     ASSERT_NE(main, nullptr);
82     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
83     auto *main2 = helpers::FindMethodByName(ctxI2, "func_main_0");
84     ASSERT_NE(main2, nullptr);
85     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
86 
87     auto transformMain = [](AbckitCoreFunction *method) {
88         auto *graph = g_implI->createGraphFromFunction(method);
89         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
90         g_implM->functionSetGraph(method, graph);
91         g_impl->destroyGraph(graph);
92         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
93     };
94 
95     transformMain(main);
96     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
97     transformMain(main2);
98     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
99 
100     g_impl->writeAbc(file, PATH1, strlen(PATH1));
101     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
102     g_impl->closeFile(file);
103     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
104     g_impl->writeAbc(ctxI2, PATH2, strlen(PATH2));
105     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
106     g_impl->closeFile(ctxI2);
107     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
108 }
109 
110 }  // namespace libabckit::test
111