• 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/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_static.h"
21 
22 #include "helpers/helpers.h"
23 #include "helpers/helpers_runtime.h"
24 
25 #include <gtest/gtest.h>
26 #include <cstddef>
27 #include <cstdint>
28 
29 // NOLINTBEGIN(readability-magic-numbers)
30 
31 namespace libabckit::test {
32 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37 
38 static constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static.abc";
39 
40 class LibAbcKitBasicBlocksTest : public ::testing::Test {};
41 
42 // Test: test-kind=api, api=GraphApiImpl::bbCreateEmpty, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBcreateEmptyBlock_1)43 TEST_F(LibAbcKitBasicBlocksTest, BBcreateEmptyBlock_1)
44 {
45     helpers::TransformMethod(
46         INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
47         [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
48             auto *start = g_implG->gGetStartBasicBlock(graph);
49             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
50             auto *main = helpers::BBgetSuccBlocks(start)[0];
51             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
52             auto *bb = g_implG->bbCreateEmpty(graph);
53             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54 
55             g_implG->bbDisconnectSuccBlock(start, 0);
56             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
57             g_implG->bbInsertSuccBlock(start, bb, 0);
58             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
59             g_implG->bbInsertSuccBlock(bb, main, 0);
60             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
61 
62             std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
63                 {{{}, {1}, {{1, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}}},
64                  {{0}, {2}, {}},
65                  {{1},
66                   {3},
67                   {
68                       {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
69                   }},
70                  {{2}, {}, {}}});
71             helpers::VerifyGraph(graph, bbSchemas);
72 
73             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
74         },
75         []([[maybe_unused]] AbckitGraph *graph) {});
76 }
77 
78 // Test: test-kind=api, api=GraphApiImpl::bbDisconnectSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBdisconnectSuccBlock)79 TEST_F(LibAbcKitBasicBlocksTest, BBdisconnectSuccBlock)
80 {
81     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
82         auto *start = g_implG->gGetStartBasicBlock(graph);
83         auto *next = helpers::BBgetSuccBlocks(start)[0];
84         g_implG->bbDisconnectSuccBlock(start, 0);
85         ASSERT_TRUE(helpers::BBgetSuccBlocks(start).empty());
86         ASSERT_TRUE(helpers::BBgetPredBlocks(next).empty());
87         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
88         g_implG->bbAppendSuccBlock(start, next);
89         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
90         std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
91             {{{}, {1}, {{3, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}}},
92              {{0},
93               {2},
94               {
95                   {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
96               }},
97              {{1}, {}, {}}});
98         // CC-OFFNXT(G.FMT.02)
99         helpers::VerifyGraph(graph, bbSchemas);
100     };
101     helpers::InspectMethod(INPUT_PATH, "test", cb);
102 }
103 
104 // Test: test-kind=api, api=GraphApiImpl::bbGetGraph, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetGraph_1)105 TEST_F(LibAbcKitBasicBlocksTest, BBgetGraph_1)
106 {
107     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
108         auto *start = g_implG->gGetStartBasicBlock(graph);
109         auto *bb = g_implG->bbCreateEmpty(graph);
110         auto *end = g_implG->gGetEndBasicBlock(graph);
111 
112         g_implG->bbAppendSuccBlock(start, bb);
113         g_implG->bbAppendSuccBlock(bb, end);
114         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
115         std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
116             {{{}, {2, 1}, {{3, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}}},
117              {{0}, {3}, {}},
118              {{0},
119               {3},
120               {
121                   {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
122               }},
123              {{2, 1}, {}, {}}});
124         helpers::VerifyGraph(graph, bbSchemas);
125         ASSERT_EQ(graph, graph);
126     };
127     helpers::InspectMethod(INPUT_PATH, "test", cb);
128 }
129 
130 // Test: test-kind=api, api=GraphApiImpl::bbGetFirstInst, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetFirstInst_2)131 TEST_F(LibAbcKitBasicBlocksTest, BBgetFirstInst_2)
132 {
133     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
134         auto *start = g_implG->gGetStartBasicBlock(graph);
135         auto *bb = helpers::BBgetSuccBlocks(start)[0];
136         auto *inst = g_implG->bbGetFirstInst(bb);
137 
138         ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID);
139     };
140     helpers::InspectMethod(INPUT_PATH, "test", cb);
141 }
142 
143 // Test: test-kind=api, api=GraphApiImpl::bbGetLastInst, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetLastInst_1)144 TEST_F(LibAbcKitBasicBlocksTest, BBgetLastInst_1)
145 {
146     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
147         auto *start = g_implG->gGetStartBasicBlock(graph);
148         auto *bb = helpers::BBgetSuccBlocks(start)[0];
149         auto *inst = g_implG->bbGetLastInst(bb);
150 
151         ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID);
152     };
153     helpers::InspectMethod(INPUT_PATH, "test", cb);
154 }
155 
156 // Test: test-kind=api, api=GraphApiImpl::bbGetTrueBranch, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetTrueBranch_1)157 TEST_F(LibAbcKitBasicBlocksTest, BBgetTrueBranch_1)
158 {
159     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
160         auto *start = g_implG->gGetStartBasicBlock(graph);
161         auto *cmpB = helpers::BBgetSuccBlocks(start)[0];
162         auto *trueB = g_implG->bbGetTrueBranch(cmpB);
163         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
164         auto *inst = g_implG->bbGetFirstInst(trueB);
165 
166         ASSERT_NE(inst, nullptr);
167         ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN);
168 
169         std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
170             {{{},
171               {1},
172               {
173                   {9, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
174                   {1, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
175                   {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
176                   {5, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
177                   {7, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
178               }},
179              {{0},
180               {3, 2},
181               {
182                   {4, ABCKIT_ISA_API_STATIC_OPCODE_IF, {3, 1}},
183               }},
184              {{1},
185               {4},
186               {
187                   {6, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {5}},
188               }},
189              {{1},
190               {4},
191               {
192                   {8, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {7}},
193               }},
194              {{2, 3}, {}, {}}});
195         helpers::VerifyGraph(graph, bbSchemas);
196     };
197     helpers::InspectMethod(INPUT_PATH, "test2", cb);
198 }
199 
200 // Test: test-kind=api, api=GraphApiImpl::bbGetFalseBranch, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetFalseBranch_1)201 TEST_F(LibAbcKitBasicBlocksTest, BBgetFalseBranch_1)
202 {
203     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
204         auto *start = g_implG->gGetStartBasicBlock(graph);
205         auto *cmpB = helpers::BBgetSuccBlocks(start)[0];
206         auto *trueB = g_implG->bbGetFalseBranch(cmpB);
207         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
208         auto *inst = g_implG->bbGetFirstInst(trueB);
209 
210         ASSERT_NE(inst, nullptr);
211         ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_RETURN);
212 
213         std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
214             {{{},
215               {1},
216               {
217                   {9, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
218                   {1, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
219                   {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
220                   {5, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
221                   {7, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
222               }},
223              {{0},
224               {3, 2},
225               {
226                   {4, ABCKIT_ISA_API_STATIC_OPCODE_IF, {3, 1}},
227               }},
228              {{1},
229               {4},
230               {
231                   {6, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {5}},
232               }},
233              {{1},
234               {4},
235               {
236                   {8, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {7}},
237               }},
238              {{2, 3}, {}, {}}});
239         helpers::VerifyGraph(graph, bbSchemas);
240     };
241     helpers::InspectMethod(INPUT_PATH, "test2", cb);
242 }
243 
244 // Test: test-kind=api, api=GraphApiImpl::bbAddInstFront, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBaddInstFront_4)245 TEST_F(LibAbcKitBasicBlocksTest, BBaddInstFront_4)
246 {
247     helpers::TransformMethod(
248         INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
249         [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
250             auto *start = g_implG->gGetStartBasicBlock(graph);
251             auto *bb = helpers::BBgetSuccBlocks(start)[0];
252             auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT);
253             auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
254             auto *negInst = g_statG->iCreateNeg(graph, newInst);
255             g_implG->bbAddInstFront(bb, negInst);
256             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
257             std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
258                 {{{},
259                   {1},
260                   {{5, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
261                  {{0},
262                   {2},
263                   {
264                       {4, ABCKIT_ISA_API_STATIC_OPCODE_NEG, {3}},
265                       {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
266                   }},
267                  {{1}, {}, {}}});
268             helpers::VerifyGraph(graph, bbSchemas);
269 
270             ASSERT_EQ(inst, g_implG->iGetNext(newInst));
271         },
272         []([[maybe_unused]] AbckitGraph *graph) {});
273 }
274 
275 // Test: test-kind=api, api=GraphApiImpl::bbAddInstBack, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBaddInstBack_2)276 TEST_F(LibAbcKitBasicBlocksTest, BBaddInstBack_2)
277 {
278     helpers::TransformMethod(
279         INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
280         [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
281             auto *start = g_implG->gGetStartBasicBlock(graph);
282             auto *bb = helpers::BBgetSuccBlocks(start)[0];
283             auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT);
284             auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
285             auto *negInst = g_statG->iCreateNeg(graph, newInst);
286 
287             g_implG->bbDisconnectSuccBlock(start, 0);
288             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
289             auto *empty = g_implG->bbCreateEmpty(graph);
290             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
291             g_implG->bbAppendSuccBlock(start, empty);
292             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
293             g_implG->bbAppendSuccBlock(empty, bb);
294             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
295             g_implG->bbAddInstBack(empty, negInst);
296             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
297             std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
298                 {{{},
299                   {1},
300                   {{5, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
301                  {{0},
302                   {2},
303                   {
304                       {4, ABCKIT_ISA_API_STATIC_OPCODE_NEG, {3}},
305                   }},
306                  {{1},
307                   {3},
308                   {
309                       {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
310                   }},
311                  {{2}, {}, {}}});
312             helpers::VerifyGraph(graph, bbSchemas);
313 
314             ASSERT_EQ(inst, g_implG->iGetNext(newInst));
315         },
316         []([[maybe_unused]] AbckitGraph *graph) {});
317 }
318 
319 // Test: test-kind=api, api=GraphApiImpl::bbAddInstBack, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBaddInstBack_4)320 TEST_F(LibAbcKitBasicBlocksTest, BBaddInstBack_4)
321 {
322     helpers::TransformMethod(
323         INPUT_PATH, ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
324         [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
325             auto *start = g_implG->gGetStartBasicBlock(graph);
326             auto *newIns = g_implG->gFindOrCreateConstantI64(graph, 1);
327 
328             ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
329             std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
330                 {{{},
331                   {1},
332                   {{4, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
333                  {{0},
334                   {2},
335                   {
336                       {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
337                   }},
338                  {{1}, {}, {}}});
339             helpers::VerifyGraph(graph, bbSchemas);
340 
341             ASSERT_EQ(newIns, g_implG->bbGetLastInst(start));
342         },
343         []([[maybe_unused]] AbckitGraph *graph) {});
344 }
345 
346 // Test: test-kind=api, api=GraphApiImpl::bbVisitSuccBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitSuccBlocks_1)347 TEST_F(LibAbcKitBasicBlocksTest, BBvisitSuccBlocks_1)
348 {
349     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
350         auto *start = g_implG->gGetStartBasicBlock(graph);
351         auto *bb = helpers::BBgetSuccBlocks(start)[0];
352 
353         uint32_t counter = 0;
354         auto cb = [](AbckitBasicBlock *, void *data) {
355             (*(reinterpret_cast<uint32_t *>(data)))++;
356             return true;
357         };
358         g_implG->bbVisitSuccBlocks(bb, &counter, cb);
359         // CC-OFFNXT(G.FMT.02)
360         ASSERT_EQ(counter, 1);
361         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
362     };
363     helpers::InspectMethod(INPUT_PATH, "test", cb);
364 }
365 
366 // Test: test-kind=api, api=GraphApiImpl::bbVisitPredBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitPredBlocks_1)367 TEST_F(LibAbcKitBasicBlocksTest, BBvisitPredBlocks_1)
368 {
369     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
370         auto *start = g_implG->gGetStartBasicBlock(graph);
371         auto *bb = helpers::BBgetSuccBlocks(start)[0];
372 
373         uint32_t counter = 0;
374         auto cb = [](AbckitBasicBlock *, void *data) {
375             (*(reinterpret_cast<uint32_t *>(data)))++;
376             return true;
377         };
378         g_implG->bbVisitPredBlocks(bb, &counter, cb);
379 
380         ASSERT_EQ(counter, 1);
381         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
382     };
383 
384     helpers::InspectMethod(INPUT_PATH, "test", cb);
385 }
386 
387 // Test: test-kind=api, api=GraphApiImpl::bbVisitDominatedBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitDominatedBlocks_1)388 TEST_F(LibAbcKitBasicBlocksTest, BBvisitDominatedBlocks_1)
389 {
390     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
391     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
392     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
393         auto *start = g_implG->gGetStartBasicBlock(graph);
394         auto *bb = helpers::BBgetSuccBlocks(start)[0];
395 
396         uint32_t counter = 0;
397         auto cb = [](AbckitBasicBlock *, void *data) {
398             (*(reinterpret_cast<uint32_t *>(data)))++;
399             return true;
400         };
401         g_implG->bbVisitDominatedBlocks(bb, &counter, cb);
402 
403         ASSERT_EQ(counter, 1);
404         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
405     });
406     g_impl->closeFile(file);
407 }
408 
409 // Test: test-kind=api, api=GraphApiImpl::bbGetSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetSuccBlock_1)410 TEST_F(LibAbcKitBasicBlocksTest, BBgetSuccBlock_1)
411 {
412     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
413     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
414     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
415         auto *start = g_implG->gGetStartBasicBlock(graph);
416         auto *bb = g_implG->bbGetSuccBlock(start, 0);
417 
418         ASSERT_EQ(bb, helpers::BBgetSuccBlocks(start)[0]);
419         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
420     });
421     g_impl->closeFile(file);
422 }
423 
424 // Test: test-kind=api, api=GraphApiImpl::bbIsStart, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisStart_1)425 TEST_F(LibAbcKitBasicBlocksTest, BBisStart_1)
426 {
427     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
428     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
429     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
430         auto *start = g_implG->gGetStartBasicBlock(graph);
431 
432         ASSERT_TRUE(g_implG->bbIsStart(start));
433         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
434     });
435     g_impl->closeFile(file);
436 }
437 
438 // Test: test-kind=api, api=GraphApiImpl::bbIsEnd, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisEnd_1)439 TEST_F(LibAbcKitBasicBlocksTest, BBisEnd_1)
440 {
441     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
442     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
443     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
444         auto *end = g_implG->gGetEndBasicBlock(graph);
445 
446         ASSERT_TRUE(g_implG->bbIsEnd(end));
447         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
448     });
449     g_impl->closeFile(file);
450 }
451 
452 // Test: test-kind=api, api=GraphApiImpl::bbGetImmediateDominator, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetImmediateDominator_1)453 TEST_F(LibAbcKitBasicBlocksTest, BBgetImmediateDominator_1)
454 {
455     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
456     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
457     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
458         auto *start = g_implG->gGetStartBasicBlock(graph);
459         auto *bb = helpers::BBgetSuccBlocks(start)[0];
460         g_implG->bbGetImmediateDominator(bb);
461         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
462     });
463     g_impl->closeFile(file);
464 }
465 
466 // Test: test-kind=api, api=GraphApiImpl::bbGetImmediateDominator, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetImmediateDominator_2)467 TEST_F(LibAbcKitBasicBlocksTest, BBgetImmediateDominator_2)
468 {
469     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
470     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
471     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
472         auto *start = g_implG->gGetStartBasicBlock(graph);
473         auto bb = g_implG->bbGetImmediateDominator(start);
474         ASSERT_EQ(bb, nullptr);
475     });
476     g_impl->closeFile(file);
477 }
478 
479 // Test: test-kind=api, api=GraphApiImpl::bbGetSuccBlockCount, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetSuccBlockCount_1)480 TEST_F(LibAbcKitBasicBlocksTest, BBgetSuccBlockCount_1)
481 {
482     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
483     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
484     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
485         auto *start = g_implG->gGetStartBasicBlock(graph);
486 
487         ASSERT_EQ(g_implG->bbGetSuccBlockCount(start), 1);
488         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
489     });
490     g_impl->closeFile(file);
491 }
492 
493 // Test: test-kind=api, api=GraphApiImpl::bbGetPredBlockCount, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetPredBlockCount_1)494 TEST_F(LibAbcKitBasicBlocksTest, BBgetPredBlockCount_1)
495 {
496     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
497     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
498     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
499         auto *start = g_implG->gGetStartBasicBlock(graph);
500         auto *bb = helpers::BBgetSuccBlocks(start)[0];
501 
502         ASSERT_EQ(g_implG->bbGetPredBlockCount(bb), 1);
503         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
504     });
505     g_impl->closeFile(file);
506 }
507 
508 // Test: test-kind=api, api=GraphApiImpl::bbDump, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBdump_1)509 TEST_F(LibAbcKitBasicBlocksTest, BBdump_1)
510 {
511     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
512     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
513     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
514         auto *start = g_implG->gGetStartBasicBlock(graph);
515         LIBABCKIT_LOG_DUMP(g_implG->bbDump(start, 0), DEBUG);
516         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
517     });
518     g_impl->closeFile(file);
519 }
520 
521 // Test: test-kind=api, api=GraphApiImpl::bbGetNumberOfInstructions, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetNumberOfInstructions_1)522 TEST_F(LibAbcKitBasicBlocksTest, BBgetNumberOfInstructions_1)
523 {
524     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
525     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
526     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
527         auto *start = g_implG->gGetStartBasicBlock(graph);
528         auto *bb = helpers::BBgetSuccBlocks(start)[0];
529         ASSERT_EQ(g_implG->bbGetNumberOfInstructions(bb), 1);
530         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
531     });
532     g_impl->closeFile(file);
533 }
534 
535 // Test: test-kind=api, api=GraphApiImpl::gGetBasicBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetBasicBlock_1)536 TEST_F(LibAbcKitBasicBlocksTest, GgetBasicBlock_1)
537 {
538     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
539     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
540     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
541         auto *bb = g_implG->gGetBasicBlock(graph, 1);
542         ASSERT_EQ(bb, g_implG->gGetStartBasicBlock(graph));
543         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
544     });
545     g_impl->closeFile(file);
546 }
547 
548 // Test: test-kind=api, api=GraphApiImpl::gGetBasicBlock, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitBasicBlocksTest,GgetBasicBlockBigId)549 TEST_F(LibAbcKitBasicBlocksTest, GgetBasicBlockBigId)
550 {
551     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
552     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
553     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
554         auto bbVectorSize = g_implG->gGetNumberOfBasicBlocks(graph);
555         auto *bb = g_implG->gGetBasicBlock(graph, bbVectorSize + 1);
556         ASSERT_EQ(bb, nullptr);
557         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
558     });
559     g_impl->closeFile(file);
560 }
561 
562 // Test: test-kind=api, api=GraphApiImpl::gGetParameter, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetParameter_1)563 TEST_F(LibAbcKitBasicBlocksTest, GgetParameter_1)
564 {
565     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
566     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
567     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
568         auto *start = g_implG->gGetStartBasicBlock(graph);
569         auto *inst = g_implG->bbGetFirstInst(start);
570         ASSERT_EQ(inst, g_implG->gGetParameter(graph, 0));
571         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
572     });
573     g_impl->closeFile(file);
574 }
575 
576 // Test: test-kind=api, api=GraphApiImpl::gGetParameter, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitBasicBlocksTest,GgetParameterBigId)577 TEST_F(LibAbcKitBasicBlocksTest, GgetParameterBigId)
578 {
579     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
580     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
581     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
582         auto size = g_implG->gGetNumberOfParameters(graph);
583         auto *inst = g_implG->gGetParameter(graph, size + 1);
584         ASSERT_EQ(inst, nullptr);
585         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
586     });
587     g_impl->closeFile(file);
588 }
589 
590 // Test: test-kind=api, api=GraphApiImpl::gGetNumberOfParameters, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetNumberOfParameters)591 TEST_F(LibAbcKitBasicBlocksTest, GgetNumberOfParameters)
592 {
593     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
594     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
595     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
596         uint32_t num = g_implG->gGetNumberOfParameters(graph);
597         ASSERT_EQ(num, 2U);
598         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
599     });
600     g_impl->closeFile(file);
601 }
602 
603 // Test: test-kind=api, api=GraphApiImpl::gGetNumberOfBasicBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,GgetNumberOfBasicBlocks_1)604 TEST_F(LibAbcKitBasicBlocksTest, GgetNumberOfBasicBlocks_1)
605 {
606     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
607     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
608     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
609         g_implG->gGetNumberOfBasicBlocks(graph);
610         ASSERT_EQ(g_implG->gGetNumberOfBasicBlocks(graph), 5U);
611         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
612     });
613     g_impl->closeFile(file);
614 }
615 
616 // Test: test-kind=api, api=GraphApiImpl::bbGetId, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetId_1)617 TEST_F(LibAbcKitBasicBlocksTest, BBgetId_1)
618 {
619     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
620     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
621     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
622         auto *start = g_implG->gGetStartBasicBlock(graph);
623         ASSERT_EQ(g_implG->bbGetId(start), 3U);
624         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
625     });
626     g_impl->closeFile(file);
627 }
628 
629 // Test: test-kind=api, api=GraphApiImpl::bbGetPredBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBgetPredBlock_1)630 TEST_F(LibAbcKitBasicBlocksTest, BBgetPredBlock_1)
631 {
632     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
633     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
634     helpers::TransformMethod(file, "test", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
635         auto *start = g_implG->gGetStartBasicBlock(graph);
636         auto *bb = g_implG->bbGetSuccBlock(start, 0U);
637         ASSERT_EQ(g_implG->bbGetPredBlock(bb, 0U), helpers::BBgetPredBlocks(bb)[0U]);
638         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
639     });
640     g_impl->closeFile(file);
641 }
642 
643 // Test: test-kind=api, api=GraphApiImpl::bbInsertSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBinsertSuccBlock_1)644 TEST_F(LibAbcKitBasicBlocksTest, BBinsertSuccBlock_1)
645 {
646     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
647     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
648     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
649         auto *start = g_implG->gGetStartBasicBlock(graph);
650         auto *bb = g_implG->bbGetSuccBlock(start, 0U);
651         auto *empty = g_implG->bbCreateEmpty(graph);
652         g_implG->bbInsertSuccBlock(bb, empty, 2U);
653         ASSERT_EQ(g_implG->bbGetSuccBlock(bb, 2U), helpers::BBgetSuccBlocks(bb)[2U]);
654         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
655         g_implG->bbDisconnectSuccBlock(bb, 2U);
656     });
657     g_impl->closeFile(file);
658 }
659 
660 // Test: test-kind=api, api=GraphApiImpl::bbAppendSuccBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBappendSuccBlock_1)661 TEST_F(LibAbcKitBasicBlocksTest, BBappendSuccBlock_1)
662 {
663     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
664     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
665     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
666         auto *start = g_implG->gGetStartBasicBlock(graph);
667         auto *bb = g_implG->bbGetSuccBlock(start, 0U);
668         auto *empty = g_implG->bbCreateEmpty(graph);
669         g_implG->bbAppendSuccBlock(bb, empty);
670         ASSERT_EQ(g_implG->bbGetSuccBlock(bb, 2U), helpers::BBgetSuccBlocks(bb)[2U]);
671         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
672         g_implG->bbDisconnectSuccBlock(bb, g_implG->bbGetSuccBlockCount(bb) - 1U);
673     });
674     g_impl->closeFile(file);
675 }
676 
677 // Test: test-kind=api, api=GraphApiImpl::bbRemoveAllInsts, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,bbRemoveAllInsts_1)678 TEST_F(LibAbcKitBasicBlocksTest, bbRemoveAllInsts_1)
679 {
680     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
681     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
682     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
683         auto *empty = g_implG->bbCreateEmpty(graph);
684         auto *constInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
685         auto *newInst = g_statG->iCreateNeg(graph, constInst);
686         g_implG->bbAddInstFront(empty, newInst);
687         ASSERT_EQ(g_implG->bbGetNumberOfInstructions(empty), 1);
688         g_implG->bbRemoveAllInsts(empty);
689         ASSERT_EQ(g_implG->bbGetNumberOfInstructions(empty), 0);
690         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
691     });
692     g_impl->closeFile(file);
693 }
694 
695 // Test: test-kind=api, api=GraphApiImpl::bbIsLoopPrehead, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisLoopPrehead_1)696 TEST_F(LibAbcKitBasicBlocksTest, BBisLoopPrehead_1)
697 {
698     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
699     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
700     helpers::TransformMethod(file, "test4", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
701         auto *start = g_implG->gGetStartBasicBlock(graph);
702         auto *bb = g_implG->bbGetSuccBlock(start, 0);
703         ASSERT_TRUE(g_implG->bbIsLoopPrehead(bb));
704         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
705     });
706     g_impl->closeFile(file);
707 }
708 
709 // Test: test-kind=api, api=GraphApiImpl::bbIsLoopHead, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisLoopHead_1)710 TEST_F(LibAbcKitBasicBlocksTest, BBisLoopHead_1)
711 {
712     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
713     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
714     helpers::TransformMethod(file, "test4", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
715         auto *start = g_implG->gGetStartBasicBlock(graph);
716         auto *bb = g_implG->bbGetSuccBlock(start, 0);
717         auto *bb2 = g_implG->bbGetSuccBlock(bb, 0);
718         ASSERT_TRUE(g_implG->bbIsLoopHead(bb2));
719         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
720     });
721     g_impl->closeFile(file);
722 }
723 
724 // Test: test-kind=api, api=GraphApiImpl::bbSplitBlockAfterInstruction, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,DISABLED_BBsplitBlockAfterInstruction_1)725 TEST_F(LibAbcKitBasicBlocksTest, DISABLED_BBsplitBlockAfterInstruction_1)
726 {
727     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
728     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
729     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
730         auto *start = g_implG->gGetStartBasicBlock(graph);
731         auto *bb = g_implG->bbGetSuccBlock(start, 0);
732         auto *bb2 = g_implG->bbGetSuccBlock(bb, 0);
733         auto *inst = g_implG->bbGetFirstInst(bb2);
734         ASSERT_EQ(g_implG->gGetNumberOfBasicBlocks(graph), 5U);
735         auto *newBb = g_implG->bbSplitBlockAfterInstruction(bb2, inst, false);
736         ASSERT_EQ(g_implG->gGetNumberOfBasicBlocks(graph), 6U);
737         g_implG->bbAppendSuccBlock(bb2, newBb);
738         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
739     });
740     g_impl->closeFile(file);
741 }
742 
743 // Test: test-kind=api, api=GraphApiImpl::bbCheckDominance, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBcheckDominance_1)744 TEST_F(LibAbcKitBasicBlocksTest, BBcheckDominance_1)
745 {
746     AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
747     ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
748     helpers::TransformMethod(file, "test2", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
749         auto *start = g_implG->gGetStartBasicBlock(graph);
750         auto *bb = g_implG->bbGetSuccBlock(start, 0);
751         ASSERT_TRUE(g_implG->bbCheckDominance(bb, start));
752         ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
753     });
754     g_impl->closeFile(file);
755 }
756 
757 // Test: test-kind=api, api=GraphApiImpl::bbVisitSuccBlocks, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBvisitSuccBlocksStatic_2)758 TEST_F(LibAbcKitBasicBlocksTest, BBvisitSuccBlocksStatic_2)
759 {
760     auto output = helpers::ExecuteStaticAbc(INPUT_PATH, "basic_blocks_static/ETSGLOBAL", "visit_call");
761     EXPECT_TRUE(helpers::Match(output, "0\n1\n"));
762     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
763         AbckitBasicBlock *startBB = g_implG->gGetStartBasicBlock(graph);
764         AbckitBasicBlock *ifBB = g_implG->bbGetSuccBlock(startBB, 0);
765         std::vector<AbckitBasicBlock *> succBBs;
766         struct VisitData {
767             const AbckitIsaApiStatic *gStatG;
768             const AbckitGraphApi *implG;
769             AbckitGraph *graph;
770             int newPrintInt;
771         } data {g_statG, g_implG, graph, 42};
772         g_implG->bbVisitSuccBlocks(ifBB, &data, [](AbckitBasicBlock *succBasicBlock, void *d) -> bool {
773             auto *gStatG = static_cast<struct VisitData *>(d)->gStatG;
774             auto *gImplG = static_cast<struct VisitData *>(d)->implG;
775             auto *graph = static_cast<struct VisitData *>(d)->graph;
776             int newPrintInt = static_cast<struct VisitData *>(d)->newPrintInt++;
777             auto *curInst = gImplG->bbGetFirstInst(succBasicBlock);
778             while (curInst != nullptr && gStatG->iGetOpcode(curInst) != ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC) {
779                 curInst = gImplG->iGetNext(curInst);
780             }
781             if (curInst == nullptr) {
782                 return false;
783             }
784             gImplG->iSetInput(curInst, gImplG->gFindOrCreateConstantU64(graph, newPrintInt), 1);
785             return true;
786         });
787     };
788     auto abcIn = INPUT_PATH;
789     auto abcOut = ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_visit.abc";
790     helpers::TransformMethod(abcIn, abcOut, "visit_succ_blocks", cb);
791     output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_visit.abc",
792                                        "basic_blocks_static/ETSGLOBAL", "visit_call");
793     EXPECT_TRUE(helpers::Match(output, "43\n42\n"));
794 }
795 
796 // Test: test-kind=api, api=GraphApiImpl::bbIsTryBegin, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisTryBegin)797 TEST_F(LibAbcKitBasicBlocksTest, BBisTryBegin)
798 {
799     helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
800         auto *startBB = g_implG->gGetStartBasicBlock(graph);
801         AbckitBasicBlock *tryBegin = g_implG->bbGetSuccBlock(startBB, 0);
802         ASSERT_TRUE(g_implG->bbIsTryBegin(tryBegin));
803     });
804 }
805 
806 // Test: test-kind=api, api=GraphApiImpl::bbIsTryEnd, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisTryEnd)807 TEST_F(LibAbcKitBasicBlocksTest, BBisTryEnd)
808 {
809     helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
810         auto *endBB = g_implG->gGetEndBasicBlock(graph);
811         AbckitBasicBlock *tryEnd = g_implG->bbGetPredBlock(g_implG->bbGetPredBlock(endBB, 2), 0);
812         ASSERT_TRUE(g_implG->bbIsTryEnd(tryEnd));
813     });
814 }
815 
816 // Test: test-kind=api, api=GraphApiImpl::bbIsCatchBegin, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisCatchBegin)817 TEST_F(LibAbcKitBasicBlocksTest, BBisCatchBegin)
818 {
819     helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
820         auto *startBB = g_implG->gGetStartBasicBlock(graph);
821         AbckitBasicBlock *tryBegin = g_implG->bbGetSuccBlock(startBB, 0);
822         AbckitBasicBlock *catchBegin = g_implG->bbGetSuccBlock(tryBegin, 1);
823         ASSERT_TRUE(g_implG->bbIsCatchBegin(catchBegin));
824     });
825 }
826 
827 // Test: test-kind=api, api=GraphApiImpl::bbIsCatch, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisCatch)828 TEST_F(LibAbcKitBasicBlocksTest, BBisCatch)
829 {
830     helpers::InspectMethod(INPUT_PATH, "test3", [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
831         auto *endBB = g_implG->gGetEndBasicBlock(graph);
832         AbckitBasicBlock *catchBB = g_implG->bbGetPredBlock(endBB, 1);
833         ASSERT_TRUE(g_implG->bbIsCatch(catchBB));
834     });
835 }
836 
837 // Test: test-kind=api, api=GraphApiImpl::bbIsTry, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitBasicBlocksTest,BBisTry)838 TEST_F(LibAbcKitBasicBlocksTest, BBisTry)
839 {
840     auto cb = [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
841         auto *startBB = g_implG->gGetStartBasicBlock(graph);
842         AbckitBasicBlock *tryBB = g_implG->bbGetSuccBlock(g_implG->bbGetSuccBlock(startBB, 0), 0);
843         // CC-OFFNXT(G.FMT.02)
844         ASSERT_TRUE(g_implG->bbIsTry(tryBB));
845     };
846     helpers::InspectMethod(INPUT_PATH, "test3", cb);
847 }
848 
849 }  // namespace libabckit::test
850 
851 // NOLINTEND(readability-magic-numbers)
852