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/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 #include "ut/isa/isa_dynamic/arithmetic/helpers_arithmetic.h"
23
24 #include <gtest/gtest.h>
25
26 // NOLINTBEGIN(readability-magic-numbers)
27 namespace libabckit::test {
28
29 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34
ValidTest(AbckitInst * (* binaryInstToCheck)(AbckitGraph * graph,AbckitInst * input0,AbckitInst * input1),AbckitIsaApiStaticOpcode expectedOpcode,const std::string & expectedOutput)35 static void ValidTest(AbckitInst *(*binaryInstToCheck)(AbckitGraph *graph, AbckitInst *input0, AbckitInst *input1),
36 AbckitIsaApiStaticOpcode expectedOpcode, const std::string &expectedOutput)
37 {
38 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_static.abc",
39 "bininst_static/ETSGLOBAL", "main");
40 EXPECT_TRUE(helpers::Match(output, "60\n"));
41
42 helpers::TransformMethod(
43 ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_static.abc",
44 ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_static_modified.abc", "foo",
45 [&](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
46 helpers::arithmetic::TransformIrBinInstrValid(graph, binaryInstToCheck);
47 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
48 },
49 [&](AbckitGraph *graph) {
50 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
51 helpers::arithmetic::CreateBBSchemaForBinary(expectedOpcode));
52 helpers::VerifyGraph(graph, bbSchemas);
53 });
54
55 output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_static_modified.abc",
56 "bininst_static/ETSGLOBAL", "main");
57 EXPECT_TRUE(helpers::Match(output, expectedOutput));
58 }
59
60 class LibAbcKitCreateBinInstTest : public ::testing::Test {};
61
62 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateAdd, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstTest,CreateAddValid)63 TEST_F(LibAbcKitCreateBinInstTest, CreateAddValid)
64 {
65 ValidTest(g_statG->iCreateAdd, ABCKIT_ISA_API_STATIC_OPCODE_ADD, "12\n");
66 }
67
68 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateMul, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstTest,CreateMulValid)69 TEST_F(LibAbcKitCreateBinInstTest, CreateMulValid)
70 {
71 ValidTest(g_statG->iCreateMul, ABCKIT_ISA_API_STATIC_OPCODE_MUL, "20\n");
72 }
73
74 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateDiv, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstTest,CreateDivValid)75 TEST_F(LibAbcKitCreateBinInstTest, CreateDivValid)
76 {
77 ValidTest(g_statG->iCreateDiv, ABCKIT_ISA_API_STATIC_OPCODE_DIV, "5\n");
78 }
79
80 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateMod, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstTest,CreateModValid)81 TEST_F(LibAbcKitCreateBinInstTest, CreateModValid)
82 {
83 ValidTest(g_statG->iCreateMod, ABCKIT_ISA_API_STATIC_OPCODE_MOD, "0\n");
84 }
85
86 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateSub, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstTest,CreateSubValid)87 TEST_F(LibAbcKitCreateBinInstTest, CreateSubValid)
88 {
89 ValidTest(g_statG->iCreateSub, ABCKIT_ISA_API_STATIC_OPCODE_SUB, "8\n");
90 }
91
92 } // namespace libabckit::test
93 // NOLINTEND(readability-magic-numbers)
94