1/* 2 * Copyright (c) 2023-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 16function test_unary_minus(): void { 17 assert new BigInt(10).negate() == (-10n); 18 assert new BigInt(1000).negate() == (-1000n); 19 assert new BigInt(-1).negate() == (1n); 20 assert new BigInt(-10).negate() == (10n); 21 assert new BigInt(-100).negate() == (100n); 22 assert new BigInt(0).negate() == (0n); 23} 24 25function test_unary(): void { 26 let n = -128n 27 assert (n == -128n) 28 29 let a = 123n; 30 assert(a == 123n); 31 assert(a.toString() == "123"); 32 33 a = -123n; 34 assert(a == -123n); 35 assert(a.toString() == "-123"); 36 37 a = 123n 38 assert(a == 123n); 39 assert(a.toString() == "123"); 40 41 a = -a; 42 assert(a == -123n); 43 assert(a.toString() == "-123"); 44 45 a = -a; 46 assert(a == 123n); 47 assert(a.toString() == "123"); 48 49 let e: bigint; 50 assert(a == 123n); 51 assert(a.toString() == "123"); 52 e = -a; 53 54 // Check that original value does not change 55 assert(a == 123n); 56 assert(a.toString() == "123"); 57 assert(e == -123n); 58 assert(e.toString() == "-123"); 59 e = a; 60 assert(e == 123n); 61 assert(e.toString() == "123"); 62 63 let b = -123n; 64 assert(b == -123n) 65 assert(b.toString() == "-123"); 66 67 let c = -(123n); 68 assert(c == -123n) 69 assert(c.toString() == "-123"); 70 71 // Double unary minus 72 let d = -(-123n); 73 assert(d == 123n); 74 assert(d.toString() == "123"); 75 76 // Triple unary minux 77 let f = -(-(-123n)); 78 assert(f == -123n); 79 assert(f.toString() == "-123"); 80 81 a = new BigInt(321); 82 assert(a.toString() == "321") 83 assert(a == 321n) 84 85 b = -a; 86 assert(a.toString() == "321") 87 assert(a == 321n) 88 assert(b == -321n); 89 assert(b.toString() == "-321"); 90} 91 92function main() : void { 93 test_unary() 94 test_unary_minus() 95} 96