diff options
Diffstat (limited to '')
-rw-r--r-- | js/src/tests/non262/BigInt/Number-conversion-rounding.js | 244 | ||||
-rw-r--r-- | js/src/tests/non262/BigInt/decimal.js | 30 | ||||
-rw-r--r-- | js/src/tests/non262/BigInt/large-bit-length.js | 33 | ||||
-rw-r--r-- | js/src/tests/non262/BigInt/mod.js | 8 | ||||
-rw-r--r-- | js/src/tests/non262/BigInt/property-name-guessed-name.js | 21 | ||||
-rw-r--r-- | js/src/tests/non262/BigInt/property-name.js | 194 |
6 files changed, 530 insertions, 0 deletions
diff --git a/js/src/tests/non262/BigInt/Number-conversion-rounding.js b/js/src/tests/non262/BigInt/Number-conversion-rounding.js new file mode 100644 index 0000000000..5daf68948d --- /dev/null +++ b/js/src/tests/non262/BigInt/Number-conversion-rounding.js @@ -0,0 +1,244 @@ +// Any copyright is dedicated to the Public Domain. +// https://creativecommons.org/licenses/publicdomain/ + +/** + * Edge-case behavior at Number.MAX_VALUE and beyond til overflow to Infinity. + */ +function maxMagnitudeTests(isNegative) +{ + var sign = isNegative ? -1 : +1; + var signBigInt = isNegative ? -1n : 1n; + + const MAX_VALUE = isNegative ? -Number.MAX_VALUE : +Number.MAX_VALUE; + + // 2**971+2**972+...+2**1022+2**1023 + var maxMagnitudeNumber = 0; + for (let i = 971; i < 1024; i++) + maxMagnitudeNumber += 2**i; + maxMagnitudeNumber *= sign; + assertEq(maxMagnitudeNumber, MAX_VALUE); + + // 2**971+2**972+...+2**1022+2**1023 + var maxMagnitudeNumberAsBigInt = 0n; + for (let i = 971n; i < 1024n; i++) + maxMagnitudeNumberAsBigInt += 2n**i; + maxMagnitudeNumberAsBigInt *= signBigInt; + var expectedMaxMagnitude = isNegative + ? -(2n**1024n) + 2n**971n + : 2n**1024n - 2n**971n; + assertEq(maxMagnitudeNumberAsBigInt, expectedMaxMagnitude); + + // Initial sanity tests. + assertEq(BigInt(maxMagnitudeNumber), maxMagnitudeNumberAsBigInt); + assertEq(maxMagnitudeNumber, Number(maxMagnitudeNumberAsBigInt)); + + // Test conversion of BigInt values above Number.MAX_VALUE. + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 1n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 3n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 4n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 5n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 6n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 7n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 8n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 9n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**20n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**400n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**800n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**900n), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**969n), MAX_VALUE); + + // For conversion purposes, rounding for values above Number.MAX_VALUE do + // rounding with respect to Number.MAX_VALUE and 2**1024 (which is treated as + // the "even" value -- so if the value to convert lies halfway between those two + // values, 2**1024 is selected). But if 2**1024 is the value that *would* have + // been chosen by this process, Infinity is substituted. + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * (2n**970n - 1n)), MAX_VALUE); + assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**970n), sign * Infinity); +} +maxMagnitudeTests(false); +maxMagnitudeTests(true); + +/** + * Simple single-Digit on x64, double-Digit on x86 tests. + */ + +assertEq(BigInt(Number(2n**53n - 2n)), 2n**53n - 2n); +assertEq(BigInt(Number(2n**53n - 1n)), 2n**53n - 1n); +assertEq(BigInt(Number(2n**53n)), 2n**53n); +assertEq(BigInt(Number(2n**53n + 1n)), 2n**53n); +assertEq(BigInt(Number(2n**53n + 2n)), 2n**53n + 2n); +assertEq(BigInt(Number(2n**53n + 3n)), 2n**53n + 4n); +assertEq(BigInt(Number(2n**53n + 4n)), 2n**53n + 4n); +assertEq(BigInt(Number(2n**53n + 5n)), 2n**53n + 4n); +assertEq(BigInt(Number(2n**53n + 6n)), 2n**53n + 6n); +assertEq(BigInt(Number(2n**53n + 7n)), 2n**53n + 8n); +assertEq(BigInt(Number(2n**53n + 8n)), 2n**53n + 8n); + +assertEq(BigInt(Number(2n**54n - 4n)), 2n**54n - 4n); +assertEq(BigInt(Number(2n**54n - 3n)), 2n**54n - 4n); +assertEq(BigInt(Number(2n**54n - 2n)), 2n**54n - 2n); +assertEq(BigInt(Number(2n**54n - 1n)), 2n**54n); +assertEq(BigInt(Number(2n**54n)), 2n**54n); +assertEq(BigInt(Number(2n**54n + 1n)), 2n**54n); +assertEq(BigInt(Number(2n**54n + 2n)), 2n**54n); +assertEq(BigInt(Number(2n**54n + 3n)), 2n**54n + 4n); +assertEq(BigInt(Number(2n**54n + 4n)), 2n**54n + 4n); +assertEq(BigInt(Number(2n**54n + 5n)), 2n**54n + 4n); +assertEq(BigInt(Number(2n**54n + 6n)), 2n**54n + 8n); +assertEq(BigInt(Number(2n**54n + 7n)), 2n**54n + 8n); +assertEq(BigInt(Number(2n**54n + 8n)), 2n**54n + 8n); + +assertEq(BigInt(Number(2n**55n - 8n)), 2n**55n - 8n); +assertEq(BigInt(Number(2n**55n - 7n)), 2n**55n - 8n); +assertEq(BigInt(Number(2n**55n - 6n)), 2n**55n - 8n); +assertEq(BigInt(Number(2n**55n - 5n)), 2n**55n - 4n); +assertEq(BigInt(Number(2n**55n - 4n)), 2n**55n - 4n); +assertEq(BigInt(Number(2n**55n - 3n)), 2n**55n - 4n); +assertEq(BigInt(Number(2n**55n - 2n)), 2n**55n); +assertEq(BigInt(Number(2n**55n - 1n)), 2n**55n); +assertEq(BigInt(Number(2n**55n)), 2n**55n); +assertEq(BigInt(Number(2n**55n + 1n)), 2n**55n); +assertEq(BigInt(Number(2n**55n + 2n)), 2n**55n); +assertEq(BigInt(Number(2n**55n + 3n)), 2n**55n); +assertEq(BigInt(Number(2n**55n + 4n)), 2n**55n); +assertEq(BigInt(Number(2n**55n + 5n)), 2n**55n + 8n); +assertEq(BigInt(Number(2n**55n + 6n)), 2n**55n + 8n); +assertEq(BigInt(Number(2n**55n + 7n)), 2n**55n + 8n); +assertEq(BigInt(Number(2n**55n + 8n)), 2n**55n + 8n); +assertEq(BigInt(Number(2n**55n + 9n)), 2n**55n + 8n); +assertEq(BigInt(Number(2n**55n + 10n)), 2n**55n + 8n); +assertEq(BigInt(Number(2n**55n + 11n)), 2n**55n + 8n); +assertEq(BigInt(Number(2n**55n + 12n)), 2n**55n + 16n); +assertEq(BigInt(Number(2n**55n + 13n)), 2n**55n + 16n); +assertEq(BigInt(Number(2n**55n + 14n)), 2n**55n + 16n); +assertEq(BigInt(Number(2n**55n + 15n)), 2n**55n + 16n); +assertEq(BigInt(Number(2n**55n + 16n)), 2n**55n + 16n); + + +/** + * Simple double-Digit on x64, triple-Digit on x86 tests. + */ + +// The tests below that aren't subtracting bits will have no bits in the +// ultimate significand from the most-significant digit (because of the implicit +// one being excluded). +assertEq(BigInt(Number(2n**64n - 2n**11n)), 2n**64n - 2n**11n); +assertEq(BigInt(Number(2n**64n - 2n**11n + 2n**10n - 1n)), 2n**64n - 2n**11n); +assertEq(BigInt(Number(2n**64n - 2n**11n + 2n**10n)), 2n**64n); +assertEq(BigInt(Number(2n**64n - 2n**10n)), 2n**64n); +assertEq(BigInt(Number(2n**64n)), 2n**64n); +assertEq(BigInt(Number(2n**64n + 1n)), 2n**64n); +assertEq(BigInt(Number(2n**64n + 2n**5n)), 2n**64n); +assertEq(BigInt(Number(2n**64n + 2n**10n)), 2n**64n); +assertEq(BigInt(Number(2n**64n + 2n**11n)), 2n**64n); +assertEq(BigInt(Number(2n**64n + 2n**11n + 1n)), 2n**64n + 2n**12n); +assertEq(BigInt(Number(2n**64n + 2n**12n)), 2n**64n + 2n**12n); +assertEq(BigInt(Number(2n**64n + 2n**12n + 1n)), 2n**64n + 2n**12n); +assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**5n)), 2n**64n + 2n**12n); +assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**10n)), 2n**64n + 2n**12n); +assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**11n - 1n)), 2n**64n + 2n**12n); +assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**11n)), 2n**64n + 2n**13n); +assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**11n + 1n)), 2n**64n + 2n**13n); + +// These tests *will* have a bit from the most-significant digit in the ultimate +// significand. +assertEq(BigInt(Number(2n**65n - 2n**12n)), 2n**65n - 2n**12n); +assertEq(BigInt(Number(2n**65n - 2n**12n + 2n**11n - 1n)), 2n**65n - 2n**12n); +assertEq(BigInt(Number(2n**65n - 2n**12n + 2n**11n)), 2n**65n); +assertEq(BigInt(Number(2n**65n - 2n**11n)), 2n**65n); +assertEq(BigInt(Number(2n**65n)), 2n**65n); +assertEq(BigInt(Number(2n**65n + 1n)), 2n**65n); +assertEq(BigInt(Number(2n**65n + 2n**5n)), 2n**65n); +assertEq(BigInt(Number(2n**65n + 2n**11n)), 2n**65n); +assertEq(BigInt(Number(2n**65n + 2n**12n)), 2n**65n); +assertEq(BigInt(Number(2n**65n + 2n**12n + 1n)), 2n**65n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**13n)), 2n**65n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**13n + 1n)), 2n**65n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**5n)), 2n**65n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**11n)), 2n**65n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**12n - 1n)), 2n**65n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**12n)), 2n**65n + 2n**14n); +assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**12n + 1n)), 2n**65n + 2n**14n); + +// ...and in these tests, the contributed bit from the most-significant digit +// is additionally nonzero. +assertEq(BigInt(Number(2n**65n + 2n**64n)), 2n**65n + 2n**64n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 1n)), 2n**65n + 2n**64n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**5n)), 2n**65n + 2n**64n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**11n)), 2n**65n + 2n**64n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**12n)), 2n**65n + 2n**64n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**12n + 1n)), 2n**65n + 2n**64n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n)), 2n**65n + 2n**64n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 1n)), 2n**65n + 2n**64n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**5n)), 2n**65n + 2n**64n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**11n)), 2n**65n + 2n**64n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**12n - 1n)), 2n**65n + 2n**64n + 2n**13n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**12n)), 2n**65n + 2n**64n + 2n**14n); +assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**12n + 1n)), 2n**65n + 2n**64n + 2n**14n); + +/** + * Versions of the testing above with all the high-order bits massively bumped + * upward to test that super-low bits, not just bits in high digits, are + * properly accounted for in rounding. + */ + +// The tests below that aren't subtracting bits will have no bits in the +// ultimate significand from the most-significant digit (because of the implicit +// one being excluded). +assertEq(BigInt(Number(2n**940n - 2n**887n + 1n)), 2n**940n - 2n**887n); +assertEq(BigInt(Number(2n**940n - 2n**887n + 2n**886n - 1n)), 2n**940n - 2n**887n); +assertEq(BigInt(Number(2n**940n - 2n**887n + 2n**886n)), 2n**940n); +assertEq(BigInt(Number(2n**940n - 2n**886n)), 2n**940n); +assertEq(BigInt(Number(2n**940n)), 2n**940n); +assertEq(BigInt(Number(2n**940n + 1n)), 2n**940n); +assertEq(BigInt(Number(2n**940n + 2n**880n)), 2n**940n); +assertEq(BigInt(Number(2n**940n + 2n**885n)), 2n**940n); +assertEq(BigInt(Number(2n**940n + 2n**887n)), 2n**940n); +assertEq(BigInt(Number(2n**940n + 2n**887n + 1n)), 2n**940n + 2n**888n); +assertEq(BigInt(Number(2n**940n + 2n**888n)), 2n**940n + 2n**888n); +assertEq(BigInt(Number(2n**940n + 2n**888n + 1n)), 2n**940n + 2n**888n); +assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**5n)), 2n**940n + 2n**888n); +assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**12n)), 2n**940n + 2n**888n); +assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**887n - 1n)), 2n**940n + 2n**888n); +assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**887n)), 2n**940n + 2n**889n); +assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**887n + 1n)), 2n**940n + 2n**889n); + +// These tests *will* have a bit from the most-significant digit in the ultimate +// significand. +assertEq(BigInt(Number(2n**941n - 2n**888n)), 2n**941n - 2n**888n); +assertEq(BigInt(Number(2n**941n - 2n**888n + 2n**887n - 1n)), 2n**941n - 2n**888n); +assertEq(BigInt(Number(2n**941n - 2n**888n + 2n**887n)), 2n**941n); +assertEq(BigInt(Number(2n**941n - 2n**887n)), 2n**941n); +assertEq(BigInt(Number(2n**941n)), 2n**941n); +assertEq(BigInt(Number(2n**941n + 1n)), 2n**941n); +assertEq(BigInt(Number(2n**941n + 2n**881n)), 2n**941n); +assertEq(BigInt(Number(2n**941n + 2n**886n)), 2n**941n); +assertEq(BigInt(Number(2n**941n + 2n**888n)), 2n**941n); +assertEq(BigInt(Number(2n**941n + 2n**888n + 1n)), 2n**941n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**889n)), 2n**941n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**889n + 1n)), 2n**941n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**5n)), 2n**941n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**12n)), 2n**941n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**888n - 1n)), 2n**941n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**888n)), 2n**941n + 2n**890n); +assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**888n + 1n)), 2n**941n + 2n**890n); + +// ...and in these tests, the contributed bit from the most-significant digit +// is additionally nonzero. +assertEq(BigInt(Number(2n**941n + 2n**940n)), 2n**941n + 2n**940n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 1n)), 2n**941n + 2n**940n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**881n)), 2n**941n + 2n**940n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**886n)), 2n**941n + 2n**940n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**888n)), 2n**941n + 2n**940n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**888n + 1n)), 2n**941n + 2n**940n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n)), 2n**941n + 2n**940n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 1n)), 2n**941n + 2n**940n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**5n)), 2n**941n + 2n**940n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**12n)), 2n**941n + 2n**940n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**888n - 1n)), 2n**941n + 2n**940n + 2n**889n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**888n)), 2n**941n + 2n**940n + 2n**890n); +assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**888n + 1n)), 2n**941n + 2n**940n + 2n**890n); + +if (typeof reportCompare === "function") + reportCompare(true, true); diff --git a/js/src/tests/non262/BigInt/decimal.js b/js/src/tests/non262/BigInt/decimal.js new file mode 100644 index 0000000000..f461307744 --- /dev/null +++ b/js/src/tests/non262/BigInt/decimal.js @@ -0,0 +1,30 @@ +// Any copyright is dedicated to the Public Domain. +// https://creativecommons.org/licenses/publicdomain/ + +// Check base-10 BigInt string conversion +const decimalTests = [ + [32n, -1n, 1n, "4294967295"], + [32n, -1n, -1n, "-4294967295"], + [32n, 0n, 1n, "4294967296"], + [32n, 0n, -1n, "-4294967296"], + [32n, 1n, 1n, "4294967297"], + [32n, 1n, -1n, "-4294967297"], + [64n, -1n, 1n, "18446744073709551615"], + [64n, -1n, -1n, "-18446744073709551615"], + [64n, 0n, 1n, "18446744073709551616"], + [64n, 0n, -1n, "-18446744073709551616"], + [64n, 1n, 1n, "18446744073709551617"], + [64n, 1n, -1n, "-18446744073709551617"], + [128n, -1n, 1n, "340282366920938463463374607431768211455"], + [128n, -1n, -1n, "-340282366920938463463374607431768211455"], + [128n, 0n, 1n, "340282366920938463463374607431768211456"], + [128n, 0n, -1n, "-340282366920938463463374607431768211456"], + [128n, 1n, 1n, "340282366920938463463374607431768211457"], + [128n, 1n, -1n, "-340282366920938463463374607431768211457"], +]; +for (const [power, offset, sign, result] of decimalTests) { + assertEq(((2n**power+offset)*sign).toString(), + result); +} + +reportCompare(true, true); diff --git a/js/src/tests/non262/BigInt/large-bit-length.js b/js/src/tests/non262/BigInt/large-bit-length.js new file mode 100644 index 0000000000..5f4617159b --- /dev/null +++ b/js/src/tests/non262/BigInt/large-bit-length.js @@ -0,0 +1,33 @@ +// Any copyright is dedicated to the Public Domain. +// https://creativecommons.org/licenses/publicdomain/ + +function test(thunk, result) { + let val, err; + try { + val = thunk(); + } catch (e) { + err = e; + } + if (err) { + assertEq(err instanceof RangeError, true); + } else { + assertEq(val, result); + } +} + +const UINT32_MAX = 2**32-1; + +// Check that BigInt.asIntN and BigInt.asUintN either return correct results or +// throw RangeErrors for large |bits| arguments. GMP uses a type equivalent to +// 'unsigned long' for bit counts, which may be too small to represent all JS +// integer indexes. +for (let bits of [UINT32_MAX-1, UINT32_MAX, UINT32_MAX+1, Number.MAX_SAFE_INTEGER]) { + test(() => BigInt.asIntN(bits, 1n), 1n); + test(() => BigInt.asIntN(bits, 0n), 0n); + test(() => BigInt.asIntN(bits, -1n), -1n); + test(() => BigInt.asUintN(bits, 1n), 1n); + test(() => BigInt.asUintN(bits, 0n), 0n); + // Skip testing asUintN with negative BigInts since it could OOM. +} + +reportCompare(true, true); diff --git a/js/src/tests/non262/BigInt/mod.js b/js/src/tests/non262/BigInt/mod.js new file mode 100644 index 0000000000..2d7bde0462 --- /dev/null +++ b/js/src/tests/non262/BigInt/mod.js @@ -0,0 +1,8 @@ +// Any copyright is dedicated to the Public Domain. +// https://creativecommons.org/licenses/publicdomain/ + +// Check that |x % x| returns zero when |x| contains multiple digits +assertEq(0x10000000000000000n % 0x10000000000000000n, 0n); +assertEq(-0x10000000000000000n % -0x10000000000000000n, 0n); + +reportCompare(true, true); diff --git a/js/src/tests/non262/BigInt/property-name-guessed-name.js b/js/src/tests/non262/BigInt/property-name-guessed-name.js new file mode 100644 index 0000000000..feb91345b2 --- /dev/null +++ b/js/src/tests/non262/BigInt/property-name-guessed-name.js @@ -0,0 +1,21 @@ +// |reftest| skip-if(!xulRuntime.shell) + +// BigInts currently don't participate when computing guessed function names. + +var p = {}; +p[1] = function(){}; +p[2n] = function(){}; + +assertEq(displayName(p[1]), "p[1]"); +assertEq(displayName(p[2]), ""); + +var q = { + 1: [function(){}], + 2n: [function(){}], +}; + +assertEq(displayName(q[1][0]), "q[1]<"); +assertEq(displayName(q[2][0]), "q<"); + +if (typeof reportCompare === "function") + reportCompare(true, true); diff --git a/js/src/tests/non262/BigInt/property-name.js b/js/src/tests/non262/BigInt/property-name.js new file mode 100644 index 0000000000..85addd550a --- /dev/null +++ b/js/src/tests/non262/BigInt/property-name.js @@ -0,0 +1,194 @@ +// BigInt literals as property keys. +{ + let o = { + 0n: "0", + 1n: "1", + + // 2**31 + 2147483647n: "2^31-1", + 2147483648n: "2^31", + 2147483649n: "2^31+1", + + // 2**32 + 4294967295n: "2^32-1", + 4294967296n: "2^32", + 4294967297n: "2^32+1", + + // 2n**63n + 9223372036854775807n: "2^63-1", + 9223372036854775808n: "2^63", + 9223372036854775809n: "2^63+1", + + // 2n**64n + 18446744073709551615n: "2^64-1", + 18446744073709551616n: "2^64", + 18446744073709551617n: "2^64+1", + }; + + assertEq(o[0], "0"); + assertEq(o[1], "1"); + + assertEq(o[2147483647], "2^31-1"); + assertEq(o[2147483648], "2^31"); + assertEq(o[2147483649], "2^31+1"); + + assertEq(o[4294967295], "2^32-1"); + assertEq(o[4294967296], "2^32"); + assertEq(o[4294967297], "2^32+1"); + + assertEq(o["9223372036854775807"], "2^63-1"); + assertEq(o["9223372036854775808"], "2^63"); + assertEq(o["9223372036854775809"], "2^63+1"); + + assertEq(o["18446744073709551615"], "2^64-1"); + assertEq(o["18446744073709551616"], "2^64"); + assertEq(o["18446744073709551617"], "2^64+1"); +} + +// With non-decimal different base. +{ + let o = { + 0b1n: "1", + 0o2n: "2", + 0x3n: "3", + }; + + assertEq(o[1], "1"); + assertEq(o[2], "2"); + assertEq(o[3], "3"); +} + +// With numeric separators. +{ + let o = { + 1_2_3n: "123", + }; + + assertEq(o[123], "123"); +} + +// BigInt literals as method property names. +{ + let o = { + 1n() {}, + *2n() {}, + async 3n() {}, + async* 4n() {}, + get 5n() {}, + set 6n(x) {}, + }; + + assertEqArray(Object.getOwnPropertyNames(o), [ + "1", "2", "3", "4", "5", "6", + ]); + + assertEq(o[1].name, "1"); + assertEq(o[2].name, "2"); + assertEq(o[3].name, "3"); + assertEq(o[4].name, "4"); + assertEq(Object.getOwnPropertyDescriptor(o, 5).get.name, "get 5"); + assertEq(Object.getOwnPropertyDescriptor(o, 6).set.name, "set 6"); +} + +// BigInt literals as class method property names. +{ + class C { + 1n() {} + *2n() {} + async 3n() {} + async* 4n() {} + get 5n() {} + set 6n(x) {} + } + let o = C.prototype; + + assertEqArray(Object.getOwnPropertyNames(o), [ + "1", "2", "3", "4", "5", "6", + "constructor", + ]); + + assertEq(o[1].name, "1"); + assertEq(o[2].name, "2"); + assertEq(o[3].name, "3"); + assertEq(o[4].name, "4"); + assertEq(Object.getOwnPropertyDescriptor(o, 5).get.name, "get 5"); + assertEq(Object.getOwnPropertyDescriptor(o, 6).set.name, "set 6"); +} + +// BigInt literals as static class method property names. +{ + class C { + static 1n() {} + static *2n() {} + static async 3n() {} + static async* 4n() {} + static get 5n() {} + static set 6n(x) {} + } + let o = C; + + // NB: Sort names because lazily resolved "length" and "name" properties are + // inserted in the wrong order. + assertEqArray(Object.getOwnPropertyNames(o).sort(), [ + "1", "2", "3", "4", "5", "6", + "length", "name", "prototype", + ]); + + assertEq(o[1].name, "1"); + assertEq(o[2].name, "2"); + assertEq(o[3].name, "3"); + assertEq(o[4].name, "4"); + assertEq(Object.getOwnPropertyDescriptor(o, 5).get.name, "get 5"); + assertEq(Object.getOwnPropertyDescriptor(o, 6).set.name, "set 6"); +} + +// BigInt literals as class field property names. +{ + let o = new class { + 1n; + 2n = "ok"; + }; + + assertEq(o[1], undefined); + assertEq(o[2], "ok"); +} + +// In binding destructuring contexts. +{ + let {0n: a} = ["ok"]; + assertEq(a, "ok"); +} + +// In binding destructuring contexts with object rest pattern. +{ + let {0n: a, ...b} = ["ok", "test"]; + assertEq(a, "ok"); + assertEqArray(Object.getOwnPropertyNames(b), ["1"]); +} + +// In assignment destructuring contexts. +{ + let a; + ({0n: a} = ["ok"]); + assertEq(a, "ok"); +} + +// In assignment destructuring contexts with object rest pattern. +{ + let a, b; + ({0n: a, ...b} = ["ok", "test"]); + assertEq(a, "ok"); + assertEqArray(Object.getOwnPropertyNames(b), ["1"]); +} + +// BigInt literals as inferred names. +{ + let o = { + 0xan: function(){}, + }; + + assertEq(o[10].name, "10"); +} + +if (typeof reportCompare === "function") + reportCompare(true, true); |