BigInt.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. describe("correct behavior", () => {
  2. test("basic functionality", () => {
  3. expect(BigInt).toHaveLength(1);
  4. expect(BigInt.name).toBe("BigInt");
  5. });
  6. test("constructor with numbers", () => {
  7. expect(BigInt(0)).toBe(0n);
  8. expect(BigInt(1)).toBe(1n);
  9. expect(BigInt(+1)).toBe(1n);
  10. expect(BigInt(-1)).toBe(-1n);
  11. expect(BigInt(123n)).toBe(123n);
  12. });
  13. test("constructor with strings", () => {
  14. expect(BigInt("")).toBe(0n);
  15. expect(BigInt("0")).toBe(0n);
  16. expect(BigInt("1")).toBe(1n);
  17. expect(BigInt("+1")).toBe(1n);
  18. expect(BigInt("-1")).toBe(-1n);
  19. expect(BigInt("-1")).toBe(-1n);
  20. expect(BigInt("42")).toBe(42n);
  21. expect(BigInt(" \n 00100 \n ")).toBe(100n);
  22. expect(BigInt("3323214327642987348732109829832143298746432437532197321")).toBe(
  23. 3323214327642987348732109829832143298746432437532197321n
  24. );
  25. });
  26. test("constructor with objects", () => {
  27. expect(BigInt([])).toBe(0n);
  28. });
  29. test("base-2 strings", () => {
  30. expect(BigInt("0b0")).toBe(0n);
  31. expect(BigInt("0B0")).toBe(0n);
  32. expect(BigInt("0b1")).toBe(1n);
  33. expect(BigInt("0B1")).toBe(1n);
  34. expect(BigInt("0b10")).toBe(2n);
  35. expect(BigInt("0B10")).toBe(2n);
  36. expect(BigInt(`0b${"1".repeat(100)}`)).toBe(1267650600228229401496703205375n);
  37. });
  38. test("base-8 strings", () => {
  39. expect(BigInt("0o0")).toBe(0n);
  40. expect(BigInt("0O0")).toBe(0n);
  41. expect(BigInt("0o1")).toBe(1n);
  42. expect(BigInt("0O1")).toBe(1n);
  43. expect(BigInt("0o7")).toBe(7n);
  44. expect(BigInt("0O7")).toBe(7n);
  45. expect(BigInt("0o10")).toBe(8n);
  46. expect(BigInt("0O10")).toBe(8n);
  47. expect(BigInt(`0o1${"7".repeat(33)}`)).toBe(1267650600228229401496703205375n);
  48. });
  49. test("base-16 strings", () => {
  50. expect(BigInt("0x0")).toBe(0n);
  51. expect(BigInt("0X0")).toBe(0n);
  52. expect(BigInt("0x1")).toBe(1n);
  53. expect(BigInt("0X1")).toBe(1n);
  54. expect(BigInt("0xf")).toBe(15n);
  55. expect(BigInt("0Xf")).toBe(15n);
  56. expect(BigInt("0x10")).toBe(16n);
  57. expect(BigInt("0X10")).toBe(16n);
  58. expect(BigInt(`0x${"f".repeat(25)}`)).toBe(1267650600228229401496703205375n);
  59. });
  60. });
  61. describe("errors", () => {
  62. test('cannot be constructed with "new"', () => {
  63. expect(() => {
  64. new BigInt();
  65. }).toThrowWithMessage(TypeError, "BigInt is not a constructor");
  66. });
  67. test("invalid arguments", () => {
  68. expect(() => {
  69. BigInt(null);
  70. }).toThrowWithMessage(TypeError, "Cannot convert null to BigInt");
  71. expect(() => {
  72. BigInt(undefined);
  73. }).toThrowWithMessage(TypeError, "Cannot convert undefined to BigInt");
  74. expect(() => {
  75. BigInt(Symbol());
  76. }).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
  77. ["foo", "123n", "1+1", {}, function () {}].forEach(value => {
  78. expect(() => {
  79. BigInt(value);
  80. }).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
  81. });
  82. });
  83. test("invalid numeric arguments", () => {
  84. [1.23, Infinity, -Infinity, NaN].forEach(value => {
  85. expect(() => {
  86. BigInt(value);
  87. }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
  88. });
  89. });
  90. test("invalid string for base", () => {
  91. ["0b", "0b2", "0B02", "-0b1", "-0B1"].forEach(value => {
  92. expect(() => {
  93. BigInt(value);
  94. }).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
  95. });
  96. ["0o", "0o8", "0O08", "-0o1", "-0O1"].forEach(value => {
  97. expect(() => {
  98. BigInt(value);
  99. }).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
  100. });
  101. ["0x", "0xg", "0X0g", "-0x1", "-0X1"].forEach(value => {
  102. expect(() => {
  103. BigInt(value);
  104. }).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
  105. });
  106. ["a", "-1a"].forEach(value => {
  107. expect(() => {
  108. BigInt(value);
  109. }).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
  110. });
  111. });
  112. });