BigInt.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. load("test-common.js");
  2. try {
  3. assert(BigInt.length === 1);
  4. assert(BigInt.name === "BigInt");
  5. assert(BigInt(0) === 0n);
  6. assert(BigInt(1) === 1n);
  7. assert(BigInt(+1) === 1n);
  8. assert(BigInt(-1) === -1n);
  9. assert(BigInt("") === 0n);
  10. assert(BigInt("0") === 0n);
  11. assert(BigInt("1") === 1n);
  12. assert(BigInt("+1") === 1n);
  13. assert(BigInt("-1") === -1n);
  14. assert(BigInt("-1") === -1n);
  15. assert(BigInt([]) === 0n);
  16. assert(BigInt("42") === 42n);
  17. assert(BigInt(" \n 00100 \n ") === 100n);
  18. assert(BigInt(123n) === 123n);
  19. assert(BigInt("3323214327642987348732109829832143298746432437532197321") === 3323214327642987348732109829832143298746432437532197321n);
  20. assertThrowsError(() => {
  21. new BigInt();
  22. }, {
  23. error: TypeError,
  24. message: "BigInt is not a constructor"
  25. });
  26. [null, undefined, Symbol()].forEach(value => {
  27. assertThrowsError(() => {
  28. BigInt(value);
  29. }, {
  30. error: TypeError,
  31. message: typeof value === "symbol"
  32. ? "Cannot convert symbol to BigInt"
  33. : `Cannot convert ${value} to BigInt`
  34. });
  35. });
  36. ["foo", "123n", "1+1", {}, function () { }].forEach(value => {
  37. assertThrowsError(() => {
  38. BigInt(value);
  39. }, {
  40. error: SyntaxError,
  41. message: `Invalid value for BigInt: ${value}`
  42. });
  43. });
  44. [1.23, Infinity, -Infinity, NaN].forEach(value => {
  45. assertThrowsError(() => {
  46. BigInt(value);
  47. }, {
  48. error: RangeError,
  49. message: "BigInt argument must be an integer"
  50. });
  51. });
  52. console.log("PASS");
  53. } catch (e) {
  54. console.log("FAIL: " + e);
  55. }