exponentiation-basic.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. test("regular exponentiation", () => {
  2. expect(2 ** 0).toBe(1);
  3. expect(2 ** 1).toBe(2);
  4. expect(2 ** 2).toBe(4);
  5. expect(2 ** 3).toBe(8);
  6. expect(3 ** 2).toBe(9);
  7. expect(0 ** 0).toBe(1);
  8. expect(2 ** (3 ** 2)).toBe(512);
  9. expect(2 ** (3 ** 2)).toBe(512);
  10. expect((2 ** 3) ** 2).toBe(64);
  11. });
  12. test("exponentiation with negatives", () => {
  13. expect(2 ** -3).toBe(0.125);
  14. expect((-2) ** 3).toBe(-8);
  15. expect("-2 ** 3").not.toEval();
  16. });
  17. test("exponentiation with PlusPlus and MinusMinus", () => {
  18. let value = 5;
  19. // prettier-ignore
  20. expect(++value ** 2).toBe(36);
  21. value = 5;
  22. expect((++value) ** 2).toBe(36);
  23. value = 5;
  24. // prettier-ignore
  25. expect(--value ** 2).toBe(16);
  26. value = 5;
  27. expect((--value) ** 2).toBe(16);
  28. expect("++5 ** 2").not.toEval();
  29. expect("--5 ** 2").not.toEval();
  30. });
  31. test("exponentiation with non-numeric primitives", () => {
  32. expect("2" ** "3").toBe(8);
  33. expect("" ** []).toBe(1);
  34. expect([] ** null).toBe(1);
  35. expect(null ** null).toBe(1);
  36. expect(undefined ** null).toBe(1);
  37. });
  38. test("exponentiation that produces NaN", () => {
  39. expect(NaN ** 2).toBeNaN();
  40. expect(2 ** NaN).toBeNaN();
  41. expect(undefined ** 2).toBeNaN();
  42. expect(2 ** undefined).toBeNaN();
  43. expect(null ** undefined).toBeNaN();
  44. expect(2 ** "foo").toBeNaN();
  45. expect("foo" ** 2).toBeNaN();
  46. });
  47. test("exponentiation with infinities", () => {
  48. expect((-1) ** Infinity).toBeNaN();
  49. expect(0 ** Infinity).toBe(0);
  50. expect(1 ** Infinity).toBeNaN();
  51. expect((-1) ** -Infinity).toBeNaN();
  52. expect(0 ** -Infinity).toBe(Infinity);
  53. expect(1 ** -Infinity).toBeNaN();
  54. expect(Infinity ** -1).toBe(0);
  55. expect(Infinity ** 0).toBe(1);
  56. expect(Infinity ** 1).toBe(Infinity);
  57. expect((-Infinity) ** -1).toBe(-0);
  58. expect((-Infinity) ** 0).toBe(1);
  59. expect((-Infinity) ** 1).toBe(-Infinity);
  60. });
  61. test("unary expression before exponentiation with brackets", () => {
  62. expect((!1) ** 2).toBe(0);
  63. expect((~5) ** 2).toBe(36);
  64. expect((+5) ** 2).toBe(25);
  65. expect((-5) ** 2).toBe(25);
  66. });