• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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 */
15import * as bigint_new from "bigint_new";
16import * as bigint_test from "bigint_test";
17import {BusinessError} from "@ohos.base";
18
19loadLibrary("ani_bigint");
20
21function main() {
22    console.info("bigint_test run start...");
23    const suite = new ArkTestsuite("bigint_test ut")
24    suite.addTest("testBigint01", testBigint01);
25    suite.addTest("testBigint02", testBigint02);
26    suite.addTest("testBigint03", testBigint03);
27    suite.addTest("testBigint04", testBigint04);
28    suite.addTest("testBigint05", testBigint05);
29    suite.addTest("testBigint06", testBigint06);
30
31    suite.addTest("testProcessBigInt_1", testProcessBigInt_1);
32    suite.addTest("testProcessBigInt_2", testProcessBigInt_2);
33    suite.addTest("testProcessBigInt_3", testProcessBigInt_3);
34    suite.addTest("testProcessBigInt_4", testProcessBigInt_4);
35    suite.addTest("testProcessBigInt_5", testProcessBigInt_5);
36    suite.addTest("testProcessBigInt_6", testProcessBigInt_6);
37
38    suite.addTest("test_bigint_with_printBigInt", test_bigint_with_printBigInt);
39    suite.addTest("test_bigint_with_createBigInt", test_bigint_with_createBigInt);
40    suite.addTest("test_bigint_with_setAndgetBigInt", test_bigint_with_setAndgetBigInt);
41    suite.addTest("test_bigint_with_setupStructAndPrint", test_bigint_with_setupStructAndPrint);
42    suite.addTest("test_bigint_with_getBigIntOptional_1", test_bigint_with_getBigIntOptional_1);
43    suite.addTest("test_bigint_with_getBigIntOptional_2", test_bigint_with_getBigIntOptional_2);
44    suite.addTest("test_bigint_with_mapBigInt", test_bigint_with_mapBigInt);
45    suite.addTest("test_bigint_with_union", test_bigint_with_union);
46
47    exit(suite.run());
48    console.info("bigint_test run end...");
49}
50
51function testProcessBigInt_1() {
52    let arg: bigint = (1n << 63n) - 1n;
53    let res: bigint = bigint_new.processBigInt(arg);
54    assertEQ(res, arg << 64n);
55}
56
57function testProcessBigInt_2() {
58    let arg: bigint = 1n << 63n;
59    let res: bigint = bigint_new.processBigInt(arg);
60    assertEQ(res, arg << 64n);
61}
62
63function testProcessBigInt_3() {
64    let arg: bigint = (1n << 64n) - 1n;
65    let res: bigint = bigint_new.processBigInt(arg);
66    assertEQ(res, arg << 64n);
67}
68
69function testProcessBigInt_4() {
70    let arg: bigint = 1n << 64n;
71    let res: bigint = bigint_new.processBigInt(arg);
72    assertEQ(res, arg << 64n);
73}
74
75function testProcessBigInt_5() {
76    let arg: bigint = -1n << 63n;
77    let res: bigint = bigint_new.processBigInt(arg);
78    assertEQ(res, arg << 64n);
79}
80
81function testProcessBigInt_6() {
82    let arg: bigint = -1n << 64n;
83    let res: bigint = bigint_new.processBigInt(arg);
84    assertEQ(res, arg << 64n);
85}
86
87// test function Bigint01(a : number, b : BigInt) : boolean
88function testBigint01() {
89    let a: number = 666666;
90    let b: bigint = 77777777777777777777777777n;
91    let result: boolean = bigint_test.bigInt001(a, b);
92    assertEQ(result, true);
93}
94
95// test function Bigint02(a: number) : number
96function testBigint02() {
97    let a: number = 77777777777;
98    let result: number = bigint_test.bigInt002(a);
99    assertEQ(result, a);
100}
101
102// test function Bigint03(a : BigInt) : BigInt
103function testBigint03() {
104    let a: bigint = 8888888888888888888888888888n;
105    let result: bigint = bigint_test.bigInt003(a);
106    assertEQ(result, a);
107}
108
109// test function Bigint04(a : BigInt) : void
110function testBigint04() {
111    let a: bigint = 8888888888888888888888888888n;
112    bigint_test.bigInt004(a);
113}
114
115// test function Bigint05(a : number, b : BigInt) : BigInt
116function testBigint05() {
117    let a: number = 1;
118    let b: bigint = 8888888888888888888888888888n;
119    let result: bigint = bigint_test.bigInt005(a, b);
120    assertEQ(result, b);
121}
122
123// test function Bigint06(a : number, b : BigInt) : number
124function testBigint06() {
125    let a: number = 1;
126    let b: bigint = 8888888888888888888888888888n;
127    let result: number = bigint_test.bigInt006(a, b);
128    assertEQ(result, a);
129}
130
131
132// test function printBigInt(a: @bigint Array<u64>): void;
133function test_bigint_with_printBigInt() {
134    let num1: BigInt = 18446744073709551616n;
135    bigint_new.printBigInt(num1);
136    let num2: BigInt = -65535n;
137    bigint_new.printBigInt(num2);
138}
139
140//function createBigInt(a: @bigint Array<u64>): @bigint Array<u64>;
141function test_bigint_with_createBigInt() {
142    let num1: BigInt = 18446744073709551616n;
143    let res = bigint_new.createBigInt(num1);
144    let expect: BigInt = res as BigInt;
145    assertEQ(expect.toString(), num1.toString());
146}
147
148//BigInt set and get
149let want: bigint_new.Want = bigint_new.getWant();
150function test_bigint_with_setAndgetBigInt(){
151    want.bigInt = 123n;
152    let res = want.bigInt;
153    assertEQ(res, 123n);
154}
155
156//function setupStructAndPrint(v: BigInt1): void;
157function test_bigint_with_setupStructAndPrint(){
158    let a: bigint_new.BigInt1 = {a: 123n};
159    bigint_new.setupStructAndPrint(a);
160}
161
162//function getBigIntOptional(a: Optional<@bigint Array<u64>>): @bigint Array<u64>;
163function test_bigint_with_getBigIntOptional_1(){
164    let a: BigInt = 123n;
165    let res = bigint_new.getBigIntOptional(a);
166    console.log("test_bigint_with_getBigIntOptional_1 have Optional: ", res);
167}
168
169function test_bigint_with_getBigIntOptional_2(){
170    let res = bigint_new.getBigIntOptional(undefined);
171    console.log("test_bigint_with_getBigIntOptional_2 have no Optional: ", res);
172}
173
174//function mapBigInt(a: @record Map<String, @bigint Array<u64>>): @record Map<String, @bigint Array<u64>>;
175function test_bigint_with_mapBigInt(){
176    let data: Record<string, BigInt> = {"key1": 123n};
177    let res = bigint_new.mapBigInt(data);
178    console.log("test_bigint_with_mapBigInt result: ", res);
179}
180
181//union @bigint Array<u64> @null string
182function test_bigint_with_union(){
183    //union bigInt
184    let bigIntData = 1020847100762815390427017310442723737601n;
185    bigint_new.showMyUnion(bigIntData);
186    let bigIntResult = bigint_new.makeMyUnion(1) ;
187    console.log("bigIntResult: ", bigIntResult);
188    assertEQ(bigIntData, bigIntResult);
189
190    //union string
191    let strData = "this is string data";
192    bigint_new.showMyUnion(strData);
193    let strResult = bigint_new.makeMyUnion(2) ;
194    console.log("strResult: ", strResult);
195    assertEQ(strData, strResult);
196
197    //union null
198    bigint_new.showMyUnion(null);
199    let nullResult = bigint_new.makeMyUnion(0) ;
200    assertEQ(nullResult, null);
201}