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::iCreateLdbigint, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateLdbigint)41 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateLdbigint)
42 {
43 auto output =
44 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/bigint/load_bigint.abc", "load_bigint");
45 auto expected = "123456789012345678901234567890\n";
46 EXPECT_TRUE(helpers::Match(output, expected));
47
48 auto cb = [](AbckitFile *file, AbckitCoreFunction *, AbckitGraph *graph) {
49 AbckitString *value =
50 g_implM->createString(file, "987654321987654321987654321", strlen("987654321987654321987654321"));
51 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
52 ASSERT_NE(value, nullptr);
53
54 AbckitInst *call1 = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1);
55 // CC-OFFNXT(G.FMT.02) project code style
56 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
57 ASSERT_NE(call1, nullptr);
58
59 AbckitInst *load = g_dynG->iCreateLdbigint(graph, value);
60 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
61 ASSERT_NE(load, nullptr);
62
63 g_implG->iInsertBefore(load, call1);
64 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
65 g_implG->iSetInput(call1, load, 1);
66
67 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
68 };
69
70 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/bigint/load_bigint.abc",
71 ABCKIT_ABC_DIR "ut/extensions/js/modify_api/bigint/load_bigint_modified.abc",
72 "load_bigint.func_main_0", cb);
73 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/bigint/load_bigint_modified.abc",
74 "load_bigint");
75 expected = "987654321987654321987654321\n";
76 EXPECT_TRUE(helpers::Match(output, expected));
77 }
78
79 } // namespace libabckit::test
80