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/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_dynamic.h"
21 #include "helpers/helpers_runtime.h"
22 #include "helpers/helpers.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_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34
35 class LibAbcKitIcreateArrayTest : public ::testing::Test {};
36
37 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCreatearraywithbuffer, abc-kind=ArkTS2, category=positive,
38 // extension=c
TEST_F(LibAbcKitIcreateArrayTest,IcreateDynCreatearraywithbuffer_1)39 TEST_F(LibAbcKitIcreateArrayTest, IcreateDynCreatearraywithbuffer_1)
40 {
41 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic.abc",
42 "create_array_dynamic");
43 EXPECT_TRUE(helpers::Match(output, "aa,10\n"));
44
45 helpers::TransformMethod(
46 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic.abc",
47 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic_modified.abc", "test",
48 [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
49 // NOTE(mredkin): Remove CreateLiteralU8, CreateLiteralU16, CreateLiteralU64, CreateLiteralFloat API
50 auto arr = std::vector<AbckitLiteral *>({
51 g_implM->createLiteralBool(file, true),
52 // g_implM->createLiteralU8(file, (uint8_t)((uint8_t)2 << 5) + 1),
53 // g_implM->createLiteralU16(file, (uint16_t)((uint16_t)2 << 14) + 1),
54 g_implM->createLiteralU32(file, (uint32_t)((uint32_t)2 << (uint32_t)30) + 1),
55 // g_implM->createLiteralU64(file, (uint64_t)((uint64_t)2 << 62) + 1),
56 // g_implM->createLiteralFloat(file, 3.21),
57 g_implM->createLiteralDouble(file, 3.22),
58 g_implM->createLiteralString(file, "asdf", strlen("asdf")),
59 });
60 auto litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
61
62 auto *inst = g_dynG->iCreateCreatearraywithbuffer(graph, litArr);
63 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
64 ASSERT_NE(inst, nullptr);
65 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
66
67 auto *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
68 ASSERT_NE(ret, nullptr);
69 g_implG->iInsertBefore(inst, ret);
70 g_implG->iSetInput(ret, inst, 0);
71 },
72 [&]([[maybe_unused]] AbckitGraph *graph) {});
73
74 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic_modified.abc",
75 "create_array_dynamic");
76 EXPECT_TRUE(helpers::Match(output, "true,2147483649,3.22,asdf\n"));
77 }
78
79 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateCreateemptyarray, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIcreateArrayTest,IcreateDynCreateemptyarray)80 TEST_F(LibAbcKitIcreateArrayTest, IcreateDynCreateemptyarray)
81 {
82 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic.abc",
83 "create_array_dynamic");
84 EXPECT_TRUE(helpers::Match(output, "aa,10\n"));
85
86 helpers::TransformMethod(
87 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic.abc",
88 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic_modified.abc", "test",
89 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
90 auto *inst = g_dynG->iCreateCreateemptyarray(graph);
91 ASSERT_NE(inst, nullptr);
92 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
93
94 auto *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
95 ASSERT_NE(ret, nullptr);
96 g_implG->iInsertBefore(inst, ret);
97 g_implG->iSetInput(ret, inst, 0);
98 },
99 [&](AbckitGraph *graph) {
100 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchema = {
101 {{},
102 {1},
103 {
104 {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
105 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
106 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
107 }},
108 {{0},
109 {2},
110 {
111 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEARRAYWITHBUFFER, {}},
112 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEEMPTYARRAY, {}},
113 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {5}},
114 }},
115 {{1}, {}, {}}};
116
117 helpers::VerifyGraph(graph, bbSchema);
118 });
119
120 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/arrays/create_array_dynamic_modified.abc",
121 "create_array_dynamic");
122 EXPECT_TRUE(helpers::Match(output, "\n"));
123 }
124
125 } // namespace libabckit::test
126 // NOLINTEND(readability-magic-numbers)
127