bigint-tobigint.js (2886B)
1 // Copyright (C) 2017 Josh Wolfe. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 description: BigInt.asUintN type coercion for bigint parameter 5 esid: sec-bigint.asuintn 6 info: | 7 BigInt.asUintN ( bits, bigint ) 8 9 2. Let bigint ? ToBigInt(bigint). 10 features: [BigInt] 11 ---*/ 12 13 assert.sameValue(BigInt.asUintN(2, 0n), 0n); 14 assert.sameValue(BigInt.asUintN(2, -0n), 0n); 15 assert.sameValue(BigInt.asUintN(2, false), 0n, "ToBigInt: false => 0n"); 16 assert.sameValue(BigInt.asUintN(2, true), 1n, "ToBigInt: true => 1n"); 17 assert.sameValue(BigInt.asUintN(2, "1"), 1n, "ToBigInt: parse BigInt"); 18 assert.sameValue(BigInt.asUintN(2, "-0"), 0n, "ToBigInt: parse BigInt"); 19 assert.sameValue(BigInt.asUintN(2, ""), 0n, "ToBigInt: empty String => 0n"); 20 assert.sameValue(BigInt.asUintN(2, " "), 0n, "ToBigInt: String with only whitespace => 0n"); 21 assert.sameValue(BigInt.asUintN(2, []), 0n, "ToBigInt: .toString() => empty String => 0n"); 22 assert.sameValue(BigInt.asUintN(2, [1]), 1n, "ToBigInt: .toString() => parse BigInt"); 23 assert.sameValue(BigInt.asUintN(3, 10n), 2n); 24 assert.sameValue(BigInt.asUintN(3, "10"), 2n, "ToBigInt: parse BigInt"); 25 assert.sameValue(BigInt.asUintN(3, "0b1010"), 2n, "ToBigInt: parse BigInt binary"); 26 assert.sameValue(BigInt.asUintN(3, "0o12"), 2n, "ToBigInt: parse BigInt octal"); 27 assert.sameValue(BigInt.asUintN(3, "0xa"), 2n, "ToBigInt: parse BigInt hex"); 28 assert.sameValue(BigInt.asUintN(3, " 0xa "), 2n, 29 "ToBigInt: parse BigInt ignore leading/trailing whitespace"); 30 assert.sameValue(BigInt.asUintN(3, " 10 "), 2n, 31 "ToBigInt: parse BigInt ignore leading/trailing whitespace"); 32 assert.sameValue(BigInt.asUintN(3, [10n]), 2n, "ToBigInt: .toString() => parse BigInt"); 33 assert.sameValue(BigInt.asUintN(3, ["10"]), 2n, "ToBigInt: .toString() => parse BigInt"); 34 assert.sameValue(BigInt.asUintN(4, 12345678901234567890003n), 3n); 35 assert.sameValue(BigInt.asUintN(4, "12345678901234567890003"), 3n, "ToBigInt: parse BigInt"); 36 assert.sameValue(BigInt.asUintN(4, 37 "0b10100111010100001010110110010011100111011001110001010000100100010001010011"), 3n, 38 "ToBigInt: parse BigInt binary"); 39 assert.sameValue(BigInt.asUintN(4, "0o2472412662347316120442123"), 3n, 40 "ToBigInt: parse BigInt octal"); 41 assert.sameValue(BigInt.asUintN(4, "0x29d42b64e7671424453"), 3n, "ToBigInt: parse BigInt hex"); 42 assert.sameValue(BigInt.asUintN(4, " 0x29d42b64e7671424453 "), 3n, 43 "ToBigInt: parse BigInt ignore leading/trailing whitespace"); 44 assert.sameValue(BigInt.asUintN(4, " 12345678901234567890003 "), 3n, 45 "ToBigInt: parse BigInt ignore leading/trailing whitespace"); 46 assert.sameValue(BigInt.asUintN(4, [12345678901234567890003n]), 3n, 47 "ToBigInt: .toString() => parse BigInt"); 48 assert.sameValue(BigInt.asUintN(4, ["12345678901234567890003"]), 3n, 49 "ToBigInt: .toString() => parse BigInt"); 50 51 reportCompare(0, 0);