parseInt.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. test("basic parseInt() functionality", () => {
  2. expect(parseInt("0")).toBe(0);
  3. expect(parseInt("100")).toBe(100);
  4. expect(parseInt("1000", 16)).toBe(4096);
  5. expect(parseInt("0xF", 16)).toBe(15);
  6. expect(parseInt("F", 16)).toBe(15);
  7. expect(parseInt("17", 8)).toBe(15);
  8. expect(parseInt(021, 8)).toBe(15);
  9. expect(parseInt("015", 10)).toBe(15);
  10. expect(parseInt(15.99, 10)).toBe(15);
  11. expect(parseInt("15,123", 10)).toBe(15);
  12. expect(parseInt("FXX123", 16)).toBe(15);
  13. expect(parseInt("1111", 2)).toBe(15);
  14. expect(parseInt("15 * 3", 10)).toBe(15);
  15. expect(parseInt("15e2", 10)).toBe(15);
  16. expect(parseInt("15px", 10)).toBe(15);
  17. expect(parseInt("12", 13)).toBe(15);
  18. expect(parseInt("Hello", 8)).toBeNaN();
  19. expect(parseInt("546", 2)).toBeNaN();
  20. expect(parseInt("-F", 16)).toBe(-15);
  21. expect(parseInt("-0F", 16)).toBe(-15);
  22. expect(parseInt("-0XF", 16)).toBe(-15);
  23. expect(parseInt(-15.1, 10)).toBe(-15);
  24. expect(parseInt("-17", 8)).toBe(-15);
  25. expect(parseInt("-15", 10)).toBe(-15);
  26. expect(parseInt("-1111", 2)).toBe(-15);
  27. expect(parseInt("-15e1", 10)).toBe(-15);
  28. expect(parseInt("-12", 13)).toBe(-15);
  29. expect(parseInt(4.7, 10)).toBe(4);
  30. expect(parseInt("0e0", 16)).toBe(224);
  31. expect(parseInt("123_456")).toBe(123);
  32. expect(parseInt("UVWXYZ", 36)).toBe(1867590395);
  33. expect(parseInt(4.7 * 1e22, 10)).toBe(4);
  34. expect(parseInt(0.00000000000434, 10)).toBe(4);
  35. expect(parseInt(0.0000001, 11)).toBe(1);
  36. expect(parseInt(0.000000124, 10)).toBe(1);
  37. expect(parseInt(1e-7, 10)).toBe(1);
  38. expect(parseInt(1000000000000100000000, 10)).toBe(1);
  39. expect(parseInt(123000000000010000000000, 10)).toBe(1);
  40. expect(parseInt(1e21, 10)).toBe(1);
  41. // FIXME: expect(parseInt('900719925474099267n')).toBe(900719925474099300)
  42. });
  43. test("parseInt() radix is coerced to a number", () => {
  44. const obj = {
  45. valueOf() {
  46. return 8;
  47. },
  48. };
  49. expect(parseInt("11", obj)).toBe(9);
  50. obj.valueOf = function () {
  51. return 1;
  52. };
  53. expect(parseInt("11", obj)).toBeNaN();
  54. obj.valueOf = function () {
  55. return Infinity;
  56. };
  57. expect(parseInt("11", obj)).toBe(11);
  58. });