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_create_empty_bigint(): void { 17 let a = new BigInt() 18 assertTrue(a.positive()) 19 assertEQ(a, 0n) 20 21 let b = new BigInt() 22 assertTrue(b.positive()) 23 assertEQ(b, 0n) 24} 25 26function test_invalid_bigint(): void { 27 // NOTE(kkonsw): implement validation 28} 29 30function test_bigint_as_string(): void { 31 assertEQ(new BigInt("10").toString(), "10") 32 assertEQ(new BigInt("1000").toString(), "1000") 33 assertEQ(new BigInt("-1000").toString(), "-1000") 34 assertEQ(new BigInt("-1").toString(), "-1") 35 assertEQ(new BigInt("-10").toString(), "-10") 36 assertEQ(new BigInt("-100").toString(), "-100") 37 assertEQ(new BigInt("-100000000000000").toString(), "-100000000000000") 38 assertEQ(new BigInt("0").toString(), "0") 39} 40 41function test_type(): void { 42 let num0 = 0n; 43 let num1 = 100_100_100_100_100_100n 44 let num2 = -57896044618658097711785492504343953926634992332820282019728792003956564819967n 45 46 assertTrue(num0 instanceof bigint) 47 assertTrue(num1 instanceof bigint) 48 assertTrue(num2 instanceof bigint) 49} 50 51function test_assignment(): void { 52 let a = -24059059045444224545405903904190343043049209420234290480n 53 let b = a 54 assertTrue(a instanceof bigint) 55 assertTrue(b instanceof bigint) 56 assertEQ(a, b) 57 58 a = 123n; 59 assertTrue(a instanceof bigint) 60 assertEQ(a.toString(), "123") 61 assertEQ(a, 123n) 62 63 const zero = 0n; 64 let c = zero; 65 assertTrue(zero instanceof bigint) 66 assertTrue(c instanceof bigint) 67 assertEQ(zero, c) 68} 69 70function test_compare(): void { 71 const a = 24400569094091093912039019089543850580328542852805043n 72 const b = 34034240244909504590902901119302940942904944029040950n 73 74 assertTrue(44493059209094029409209402940924902n < 140044940590459049067274048929058908989042385n) 75 assertTrue(44493059209094029409209402940924902n < a) 76 assertTrue(a < 34034240244909504590902901119302940942904944029040950n) 77 assertTrue(a < b) 78 79 assertTrue(44493059209094029409209402940924902n <= 140044940590459049067274048929058908989042385n) 80 assertTrue(44493059209094029409209402940924902n <= a) 81 assertTrue(a <= 34034240244909504590902901119302940942904944029040950n) 82 assertTrue(a <= b) 83 84 assertTrue(44493059209094029409209402940924902n <= 44493059209094029409209402940924902n) 85 assertTrue(24400569094091093912039019089543850580328542852805043n <= a) 86 assertTrue(a <= 24400569094091093912039019089543850580328542852805043n) 87 assertTrue(a <= a) 88 89 assertTrue(40044940590459049067274048929058908989042385n > 44493059209094029409209402940924902n) 90 assertTrue(34034240244909504590902901119302940942904944029040950n > a) 91 assertTrue(a > 140044940590459049067274048929058908989042385n) 92 assertTrue(b > a) 93 94 assertTrue(40044940590459049067274048929058908989042385n >= 44493059209094029409209402940924902n) 95 assertTrue(34034240244909504590902901119302940942904944029040950n >= a) 96 assertTrue(a >= 140044940590459049067274048929058908989042385n) 97 assertTrue(b >= a) 98 99 assertTrue(44493059209094029409209402940924902n <= 44493059209094029409209402940924902n) 100 assertTrue(24400569094091093912039019089543850580328542852805043n <= a) 101 assertTrue(a <= 24400569094091093912039019089543850580328542852805043n) 102 assertTrue(a <= a) 103} 104 105function test_literals() : void { 106 let num0 = 0n 107 assertEQ(num0.toString(), "0") 108 109 let num1 = 127n 110 assertEQ(num1.toString(), "127") 111 112 let num2 = 32767n 113 assertEQ(num2.toString(), "32767") 114 115 let num3 = 2147483647n 116 assertEQ(num3.toString(), "2147483647") 117 118 let num4 = 9223372036854775807n 119 assertEQ(num4.toString(), "9223372036854775807") 120 121 let num5 = 170141183460469231731687303715884105727n 122 assertEQ(num5.toString(), "170141183460469231731687303715884105727") 123 124 let num6 = 57896044618658097711785492504343953926634992332820282019728792003956564819967n 125 assertEQ(num6.toString(), "57896044618658097711785492504343953926634992332820282019728792003956564819967") 126 127 let num1_n = -128n 128 assertEQ(num1_n.toString(), "-128") 129 130 let num2_n = -32768n 131 assertEQ(num2_n.toString(), "-32768") 132 133 let num3_n = -2147483648n 134 assertEQ(num3_n.toString(), "-2147483648") 135 136 let num4_n = -9223372036854775808n 137 assertEQ(num4_n.toString(), "-9223372036854775808") 138 139 let num5_n = -170141183460469231731687303715884105728n 140 assertEQ(num5_n.toString(), "-170141183460469231731687303715884105728") 141 142 let num6_n = -57896044618658097711785492504343953926634992332820282019728792003956564819968n 143 assertEQ(num6_n.toString(), "-57896044618658097711785492504343953926634992332820282019728792003956564819968") 144 145 let num1_sep = 1_991_653_125_841_217_555_434419_9091_123000000_3_3313_5775_3282_29n 146 assertEQ(num1_sep.toString(), "19916531258412175554344199091123000000333135775328229") 147 148 let num2_sep = -422_12_3333_9844_3333_3443_34111_43434_1111_11_1_3_3_411909_990081n 149 assertEQ(num2_sep.toString(), "-4221233339844333334433411143434111111133411909990081") 150 151 let num0_t: bigint = 0n 152 assertEQ(num0_t.toString(), "0") 153 154 let num1_t: bigint = 57896044618658097711785492504343953926634992332820282019728792003956564819967n 155 assertEQ(num1_t.toString(), "57896044618658097711785492504343953926634992332820282019728792003956564819967") 156 157 let num2_t: bigint = -9223372036854775808n 158 assertEQ(num2_t.toString(), "-9223372036854775808") 159 160 let num3_t: bigint = 1_991_653_125_841_217_555_434419_9091_123000000_3_3313_5775_3282_29n 161 assertEQ(num3_t.toString(), "19916531258412175554344199091123000000333135775328229") 162 163 let num4_t: bigint = -422_12_3333_9844_3333_3443_34111_43434_1111_11_1_3_3_411909_990081n 164 assertEQ(num4_t.toString(), "-4221233339844333334433411143434111111133411909990081") 165 166 const num0_c = 0n 167 assertEQ(num0_c.toString(), "0") 168 169 const num1_c = 1267650600228229401496703205376n 170 assertEQ(num1_c.toString(), "1267650600228229401496703205376") 171 172 const num2_c = -1427247692705959881058285969449495136382746624n 173 assertEQ(num2_c.toString(), "-1427247692705959881058285969449495136382746624") 174 175 const num3_c = 4_000_000_000_000_000_000_000_100n 176 assertEQ(num3_c.toString(), "4000000000000000000000100") 177 178 const num4_c: bigint = -7777777_666666_55555_4444_333_22_1n 179 assertEQ(num4_c.toString(), "-7777777666666555554444333221") 180} 181 182function test_cast(): void { 183 const v = 1559053 184 const b: byte = 44 185 const s: short = -17600 186 const i: int = 1150483640 187 const l: long = -8223372036854775808 188 189 // NOTE(kkonsw): casts currently do not work 190} 191 192function test_bigint_methods(): void { 193 const b: byte = 44 194 const s: short = -17600 195 const i: int = 1150483640 196 const l: long = -8223372036854775808 197 198 /* Testing BigInt constructor */ 199 let n0 = new BigInt(0) 200 assertEQ(n0, 0n) 201 assertEQ(n0.toString(), "0") 202 203 let n1 = new BigInt(654093) 204 assertEQ(n1.toString(), "654093") 205 assertEQ(n1, 654093n) 206 207 let n2 = new BigInt(b) 208 assertEQ(n2.toString(), "44") 209 assertEQ(n2, 44n) 210 211 let n3 = new BigInt(s) 212 assertEQ(n3.toString(), "-17600") 213 assertEQ(n3, -17600n) 214 215 let n4 = new BigInt(i) 216 assertEQ(n4.toString(), "1150483640") 217 assertEQ(n4, 1150483640n) 218 219 let n5 = new BigInt(l) 220 assertEQ(n5.toString(), "-8223372036854775808") 221 assertEQ(n5, -8223372036854775808n) 222 223 let dec = new BigInt("-12392320390239294724747283477947923471101032") 224 assertEQ(dec, -12392320390239294724747283477947923471101032n) 225 226 const n7 = 12392320390239294724747283477947923471101032n 227 228 /* Testing asIntN() static method */ 229 assertEQ(BigInt.asIntN(0, n7), 0n) 230 assertEQ(BigInt.asIntN(8, n7), 104n) 231 assertEQ(BigInt.asIntN(16, n7), 27752n) 232 assertEQ(BigInt.asIntN(32, n7), -737317784n) 233 assertEQ(BigInt.asIntN(64, n7), -7098331616643290008n) 234 235 /* Testing asUintN() static method */ 236 assertEQ(BigInt.asUintN(0, n7), 0n) 237 assertEQ(BigInt.asUintN(8, n7), 104n) 238 assertEQ(BigInt.asUintN(16, n7), 27752n) 239 assertEQ(BigInt.asUintN(32, n7), 3557649512n) 240 assertEQ(BigInt.asUintN(64, n7), 11348412457066261608n) 241} 242 243function test_shift(): void { 244 const a = 245599210405555256299145n 245 /* Testing left shift (<<) */ 246 assertEQ(a << 100n, 311333986486181324779687697000809288883015536628203520n) 247 assertEQ(a << 0n, a) 248 249 /* Testing right shift (>>) */ 250 assertEQ(a >> 60n, 213023n) 251 assertEQ(a >> 0n, a) 252} 253 254function test_scientific(): void { 255 assertEQ(new BigInt(0.0e0).toString(), "0", "BigInt(0.0e0)") 256 assertEQ(new BigInt(0.0e+0).toString(), "0", "BigInt(0.0e+0)") 257 assertEQ(new BigInt(0.0e-0).toString(), "0", "BigInt(0.0e-0).toString()") 258 assertEQ(new BigInt(-0.0e0).toString(), "0", "BigInt(-0.0e0).toString()") 259 assertEQ(new BigInt(1e23).toString(), "100000000000000000000000", "BigInt(1e22)") 260 assertEQ(new BigInt(1e+23).toString(), "100000000000000000000000", "BigInt(1e+22)") 261 assertEQ(new BigInt(-1e23).toString(), "-100000000000000000000000", "BigInt(-1e22)") 262 assertEQ(new BigInt(-1e+23).toString(), "-100000000000000000000000", "BigInt(-1e+22)") 263 264 assertEQ(new BigInt(1.234567e10).toString(), "12345670000", "BigInt(1.234567e10).toString()") 265 assertEQ(new BigInt(1.234567e20).toString(), "123456700000000000000", "BigInt(1.234567e20).toString()") 266 267 assertEQ(new BigInt(1.2345678912e21).toString(), "1234567891200000000000", "BigInt(1.2345678912e21)") 268 assertEQ(new BigInt(1.2345678912e+21).toString(), "1234567891200000000000", "BigInt(1.2345678912e+21)") 269 assertEQ(new BigInt(-1.2345678912e21).toString(), "-1234567891200000000000", "BigInt(-1.2345678912e21)") 270} 271 272function test_double(): void { 273 let bigIntFromDouble = (x: number): boolean => { 274 try { 275 let b = new BigInt(x); 276 } catch (e) { 277 return e instanceof Error; 278 } 279 return false 280 }; 281 282 assertTrue(bigIntFromDouble(0.1), "BigInt(0.1)") 283 assertTrue(bigIntFromDouble(-0.1), "BigInt(-0.1)") 284 assertTrue(bigIntFromDouble(42.1234567), "BigInt(42.1234567)") 285 assertTrue(bigIntFromDouble(1.234567e2), "BigInt(1.234567e2)") 286 assertTrue(bigIntFromDouble(1e-22), "BigInt(1e-22)") 287 assertTrue(bigIntFromDouble(-1e-22), "BigInt(-1e-22)") 288 assertTrue(bigIntFromDouble(1.88e-20), "BigInt(1.88e-20)") 289 assertTrue(bigIntFromDouble(1.2345678848e-21), "BigInt(1.2345678484e-21)") 290} 291 292function test_sign(): void { 293 assertEQ(new BigInt().negative() , false , 'BigInt() is negative') 294 assertEQ(new BigInt(0).negative() , false , 'BigInt(0) is negative') 295 assertEQ(new BigInt(-0).negative() , false , 'BigInt(-0) is negative') 296 assertEQ(new BigInt(0.0).negative() , false , 'BigInt(0.0) is negative') 297 assertEQ(new BigInt(-0.0).negative(), false , 'BigInt(-0.0) is negative') 298 assertEQ(new BigInt("0").negative() , false , 'BigInt("0") is negative') 299 assertEQ(new BigInt("-0").negative(), false , 'BigInt("-0") is negative') 300 301 assertEQ(new BigInt().positive() , true , 'BigInt() is not positive') 302 assertEQ(new BigInt(0).positive() , true , 'BigInt(0) is not positive') 303 assertEQ(new BigInt(-0).positive() , true , 'BigInt(-0) is not positive') 304 assertEQ(new BigInt(0.0).positive() , true , 'BigInt(0.0) is not positive') 305 assertEQ(new BigInt(-0.0).positive(), true , 'BigInt(-0.0) is not positive') 306 assertEQ(new BigInt("0").positive() , true , 'BigInt("0") is not positive') 307 assertEQ(new BigInt("-0").positive(), true , 'BigInt("-0") is not positive') 308 309 assertEQ(new BigInt("0").negate().positive() , true , 'BigInt("0").negate() is not positive') 310 assertEQ(new BigInt("-0").negate().positive(), true , 'BigInt("-0").negate() is not positive') 311 312 assertTrue((new BigInt("1") + new BigInt("-1")).positive() , 'BigInt(1 + -1) is not positive') 313 assertTrue((new BigInt("-1") + new BigInt("1")).positive() , 'BigInt(-1 + 1) is not positive') 314 assertTrue((new BigInt("1") - new BigInt("1")).positive() , 'BigInt(1 - 1) is not positive') 315 assertTrue((new BigInt("-1") - new BigInt("-1")).positive() , 'BigInt(-1 - -1) is not positive') 316 assertTrue((new BigInt("1") * new BigInt("0")).positive() , 'BigInt(1 * 0) is not positive') 317 assertTrue((new BigInt("-1") * new BigInt("0")).positive() , 'BigInt(-1 * 0) is not positive') 318 assertTrue((new BigInt("0") * new BigInt("1")).positive() , 'BigInt(0 * 1) is not positive') 319 assertTrue((new BigInt("0") * new BigInt("-1")).positive() , 'BigInt(0 * -1) is not positive') 320 assertTrue((new BigInt("0") / new BigInt("1")).positive() , 'BigInt(0 / 1) is not positive') 321 assertTrue((new BigInt("0") / new BigInt("-1")).positive() , 'BigInt(0 / -1) is not positive') 322 assertTrue((new BigInt("4") % new BigInt("2")).positive() , 'BigInt(4 % 2) is not positive') 323 assertTrue((new BigInt("4") % new BigInt("-2")).positive() , 'BigInt(4 % -2) is not positive') 324 assertTrue((new BigInt("-4") % new BigInt("2")).positive() , 'BigInt(-4 % 2) is not positive') 325 assertTrue((new BigInt("-4") % new BigInt("-2")).positive() , 'BigInt(-4 % -2) is not positive') 326} 327 328function main() : void { 329 test_create_empty_bigint(); 330 test_bigint_as_string(); 331 test_invalid_bigint(); 332 test_type(); 333 test_assignment(); 334 test_compare(); 335 test_literals(); 336 test_cast(); 337 test_bigint_methods(); 338 test_shift(); 339 test_double(); 340 test_sign(); 341 // NOTE(aakmaev): Enable after fix #17683. 342 // test_scientific(); 343} 344