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 <gtest/gtest.h>
17 #include <cstring>
18
19 #include "libabckit/include/c/abckit.h"
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22 #include "libabckit/include/c/metadata_core.h"
23 #include "libabckit/include/c/extensions/js/metadata_js.h"
24 #include "ir_impl.h"
25 #include "libabckit/include/c/isa/isa_dynamic.h"
26 #include "libabckit/include/c/statuses.h"
27
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_implJsI = AbckitGetJsInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_implJsM = AbckitGetJsModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37
38 class LibAbcKitJSModifyApiModulesTest : public ::testing::Test {};
39
40 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateStarrayspread, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateStarrayspread)41 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateStarrayspread)
42 {
43 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/arrays/starrayspread.abc",
44 "starrayspread");
45 auto expected = "helloworld\n1\n";
46 EXPECT_TRUE(helpers::Match(output, expected));
47
48 helpers::TransformMethod(
49 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/arrays/starrayspread.abc",
50 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/arrays/starrayspread_modified.abc", "starrayspread.func_main_0",
51 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
52 AbckitInst *ind = g_implG->gFindOrCreateConstantI32(graph, 0);
53 AbckitInst *str = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_LOADSTRING);
54 AbckitInst *emptyArray = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEEMPTYARRAY);
55 AbckitInst *spread = g_dynG->iCreateStarrayspread(graph, str, emptyArray, ind);
56
57 AbckitInst *oldStown = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_STOWNBYINDEX);
58 g_implG->iRemove(oldStown);
59 g_implG->iInsertAfter(spread, emptyArray);
60 });
61 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/arrays/starrayspread_modified.abc",
62 "starrayspread");
63 expected = "helloworld\n10\n";
64 EXPECT_TRUE(helpers::Match(output, expected));
65 }
66
67 } // namespace libabckit::test
68