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 // NOLINTBEGIN(readability-magic-numbers)
26 namespace libabckit::test {
27
28 namespace {
29 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34
TransformEqualsIr(AbckitGraph * graph)35 void TransformEqualsIr(AbckitGraph *graph)
36 {
37 auto *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_RETURN);
38 ASSERT_NE(ret, nullptr);
39 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
40
41 auto *initObj = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_INITOBJECT);
42 ASSERT_NE(initObj, nullptr);
43 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
44
45 auto *initObj2 = g_implG->iGetNext(initObj);
46 ASSERT_NE(initObj2, nullptr);
47 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
48
49 auto *equals = g_statG->iCreateEquals(graph, initObj, initObj2);
50 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
51
52 g_implG->iInsertBefore(equals, ret);
53 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54
55 g_implG->iSetInput(ret, equals, 0);
56 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
57 }
58
59 } // namespace
60
61 class LibAbcKitEqualsStaticTest : public ::testing::Test {};
62
63 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateEquals, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitEqualsStaticTest,LibAbcKitTestEquals)64 TEST_F(LibAbcKitEqualsStaticTest, LibAbcKitTestEquals)
65 {
66 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/equals/equals_static.abc",
67 "equals_static/ETSGLOBAL", "main");
68 EXPECT_TRUE(helpers::Match(output, "true\n"));
69
70 helpers::TransformMethod(
71 ABCKIT_ABC_DIR "ut/isa/isa_static/equals/equals_static.abc",
72 ABCKIT_ABC_DIR "ut/isa/isa_static/equals/equals_static_modified.abc", "foo",
73 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) { TransformEqualsIr(graph); },
74 [](AbckitGraph *graph) {
75 helpers::BBSchema<AbckitIsaApiStaticOpcode> bb1({{}, {1}, {}});
76 std::vector<helpers::InstSchema<AbckitIsaApiStaticOpcode>> insts({
77 {0, ABCKIT_ISA_API_STATIC_OPCODE_INITOBJECT, {}},
78 {1, ABCKIT_ISA_API_STATIC_OPCODE_INITOBJECT, {}},
79 {3, ABCKIT_ISA_API_STATIC_OPCODE_EQUALS, {0, 1}},
80 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN, {3}},
81 });
82 helpers::BBSchema<AbckitIsaApiStaticOpcode> bb2({{0}, {2}, insts});
83 helpers::BBSchema<AbckitIsaApiStaticOpcode> bb3({{1}, {}, {}});
84 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas({bb1, bb2, bb3});
85 helpers::VerifyGraph(graph, bbSchemas);
86 });
87
88 output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/equals/equals_static_modified.abc",
89 "equals_static/ETSGLOBAL", "main");
90 EXPECT_TRUE(helpers::Match(output, "false\n"));
91 }
92
93 } // namespace libabckit::test
94 // NOLINTEND(readability-magic-numbers)
95