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/isa/isa_dynamic.h"
19 #include "libabckit/include/c/abckit.h"
20
21 #include "helpers/helpers.h"
22 #include "helpers/helpers_runtime.h"
23 #include "helpers_arithmetic.h"
24
25 #include <gtest/gtest.h>
26
27 // NOLINTBEGIN(readability-magic-numbers)
28 namespace libabckit::test {
29
30 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35
ValidTest(AbckitInst * (* unaryInstToCheck)(AbckitGraph * graph,AbckitInst * input0),AbckitIsaApiDynamicOpcode expectedOpcode,const std::string & expectedOutput)36 static void ValidTest(AbckitInst *(*unaryInstToCheck)(AbckitGraph *graph, AbckitInst *input0),
37 AbckitIsaApiDynamicOpcode expectedOpcode, const std::string &expectedOutput)
38 {
39 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arithmetic/unaryinst_dynamic.abc",
40 "unaryinst_dynamic");
41 EXPECT_TRUE(helpers::Match(output, "10\n"));
42
43 helpers::TransformMethod(
44 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arithmetic/unaryinst_dynamic.abc",
45 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arithmetic/unaryinst_dynamic_modified.abc", "foo",
46 [&](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
47 helpers::arithmetic::TransformIrUnaryInstValid(graph, unaryInstToCheck, 3);
48 },
49 [&](AbckitGraph *graph) {
50 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(
51 helpers::arithmetic::CreateBBSchemaForDynUnary(expectedOpcode));
52 helpers::VerifyGraph(graph, bbSchemas);
53 });
54
55 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arithmetic/unaryinst_dynamic_modified.abc",
56 "unaryinst_dynamic");
57 EXPECT_TRUE(helpers::Match(output, expectedOutput));
58 }
59
60 class LibAbcKitDynUnaryInstTest : public ::testing::Test {};
61
62 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateNot, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynNotValid)63 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynNotValid)
64 {
65 ValidTest(g_dynG->iCreateNot, ABCKIT_ISA_API_DYNAMIC_OPCODE_NOT, "-11\n");
66 }
67
68 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateNeg, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynNegValid)69 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynNegValid)
70 {
71 ValidTest(g_dynG->iCreateNeg, ABCKIT_ISA_API_DYNAMIC_OPCODE_NEG, "-10\n");
72 }
73
74 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateInc, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynIncValid)75 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynIncValid)
76 {
77 ValidTest(g_dynG->iCreateInc, ABCKIT_ISA_API_DYNAMIC_OPCODE_INC, "11\n");
78 }
79
80 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateDec, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynDecValid)81 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynDecValid)
82 {
83 ValidTest(g_dynG->iCreateDec, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEC, "9\n");
84 }
85
86 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateIstrue, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynIstrueValid)87 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynIstrueValid)
88 {
89 ValidTest(g_dynG->iCreateIstrue, ABCKIT_ISA_API_DYNAMIC_OPCODE_ISTRUE, "true\n");
90 }
91
92 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCallruntimeIstrue, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynCallruntimeIstrueValid)93 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynCallruntimeIstrueValid)
94 {
95 ValidTest(g_dynG->iCreateCallruntimeIstrue, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLRUNTIME_ISTRUE, "true\n");
96 }
97
98 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateIsfalse, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynIsfalseValid)99 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynIsfalseValid)
100 {
101 ValidTest(g_dynG->iCreateIsfalse, ABCKIT_ISA_API_DYNAMIC_OPCODE_ISFALSE, "false\n");
102 }
103
104 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCallruntimeIsfalse, abc-kind=ArkTS1, category=positive,
105 // extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynCallruntimeIsfalseValid)106 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynCallruntimeIsfalseValid)
107 {
108 ValidTest(g_dynG->iCreateCallruntimeIsfalse, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLRUNTIME_ISFALSE, "false\n");
109 }
110
111 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateTonumber, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynTonumberValid)112 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynTonumberValid)
113 {
114 ValidTest(g_dynG->iCreateTonumber, ABCKIT_ISA_API_DYNAMIC_OPCODE_TONUMBER, "10\n");
115 }
116
117 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateTonumeric, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynTonumericValid)118 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynTonumericValid)
119 {
120 ValidTest(g_dynG->iCreateTonumeric, ABCKIT_ISA_API_DYNAMIC_OPCODE_TONUMERIC, "10\n");
121 }
122
123 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateTypeof, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynUnaryInstTest,CreateDynTypeofValid)124 TEST_F(LibAbcKitDynUnaryInstTest, CreateDynTypeofValid)
125 {
126 ValidTest(g_dynG->iCreateTypeof, ABCKIT_ISA_API_DYNAMIC_OPCODE_TYPEOF, "number\n");
127 }
128
129 } // namespace libabckit::test
130 // NOLINTEND(readability-magic-numbers)
131