• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-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 */
15
16function test_multiply(): void {
17    assertEQ(new BigInt(10).operatorMultiply(new BigInt(10)), 100n)
18    assertEQ(new BigInt(0).operatorMultiply(new BigInt(50)), 0n)
19    assertEQ(new BigInt(1).operatorMultiply(new BigInt(50)), 50n)
20    assertEQ(new BigInt(50).operatorMultiply(new BigInt(5)), 250n)
21    assertEQ(new BigInt(50).operatorMultiply(new BigInt(-5)), -250n)
22    assertEQ(new BigInt(-1).operatorMultiply(new BigInt(-5)), 5n)
23    assertEQ(new BigInt(0).operatorMultiply(new BigInt(0)), 0n)
24    assertEQ(new BigInt(123).operatorMultiply(new BigInt(1)), 123n)
25    assertEQ(new BigInt(1234).operatorMultiply(new BigInt(987)), 1217958n)
26    assertEQ(new BigInt(3241847031247230147213740214703214721047312n).operatorMultiply(new BigInt(412343124123421347812304712431421204731024n)), 1336753332794721625246945391107220242430725631478413717131017736872102322242538207488n)
27    assertEQ(new BigInt(-3241847031247230147213740214703214721047312n).operatorMultiply(new BigInt(-412343124123421347812304712431421204731024n)), 1336753332794721625246945391107220242430725631478413717131017736872102322242538207488n)
28    assertEQ(new BigInt(-3241847031247230147213740214703214721047312n).operatorMultiply(new BigInt(412343124123421347812304712431421204731024n)), -1336753332794721625246945391107220242430725631478413717131017736872102322242538207488n)
29    assertEQ(new BigInt(3241847031247230147213740214703214721047312n).operatorMultiply(new BigInt(-412343124123421347812304712431421204731024n)), -1336753332794721625246945391107220242430725631478413717131017736872102322242538207488n)
30    assertEQ(new BigInt(256).operatorMultiply(new BigInt(256)), 65536n)
31}
32
33function test_multiplication(): void {
34    const a = 23443495146314363289895841n
35    const b = 245000234343499329134n
36    const c = -245000234343499329134n
37
38    /* Multiplication testing (*) */
39    assertEQ(978667632325344545n * 4534000101n, 4437279143808543031889799045n)
40    assertEQ(a * b, 5743661804677708098900659843374372544236731694n)
41    assertEQ(a * c, -5743661804677708098900659843374372544236731694n)
42    assertEQ(a * 0n, 0n)
43    assertEQ(c * 0n, 0n)
44
45    /* Division testing (/) */
46    assertEQ(39735235034886462n / 89221422n, 445355321n)
47    assertEQ(a / b, 95687n)
48    assertEQ(a / c, -95687n)
49    assertEQ(0n / a, 0n)
50
51    let err = false;
52    try {
53        a / 0n
54    } catch (e) {
55        if (e instanceof Error) {
56            err = true
57        }
58    }
59    assertTrue(err)
60
61    /* Remainder of the division (%) */
62    assertEQ(493433405047004109n % 111114444n, 18100749n)
63    assertEQ(a % b, a % c)
64    assertEQ(0n % a, 0n)
65
66    err = false;
67    try {
68        a % 0n
69    } catch (e) {
70        if (e instanceof Error) {
71            err = true
72        }
73    }
74    assertTrue(err)
75}
76
77function main() {
78    test_multiply()
79    test_multiplication()
80}
81
82