expo.js 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. var tests = [
  7. {
  8. name: "Exponentiation operator (**) test cases",
  9. body: function () {
  10. assert.areEqual(8 , 2 ** 3, "2 to the power of 3 is 8");
  11. assert.areEqual(Math.pow(-2, -4), (-2) ** -4, "Exponentiation operator ** works as intended");
  12. assert.areEqual(Math.pow(-0, -4), (-0) ** -4, "Exponentiation operator ** works as intended");
  13. assert.areEqual(Math.pow(4, 0), 4 ** -0, "Exponentiation operator ** works as intended");
  14. assert.areEqual(Math.pow(0, -0), 0 ** -0, "Exponentiation operator ** works as intended");
  15. assert.areEqual(Math.pow(Infinity, 0), Infinity ** 0,"Exponentiation operator ** works as intended");
  16. assert.areEqual(Math.pow(Infinity, -Infinity), Infinity ** -Infinity, "Exponentiation operator ** works as intended");
  17. assert.areEqual(Math.pow(NaN, 2), NaN ** 2, "Exponentiation operator ** works as intended");
  18. assert.areEqual(Math.pow(3.4, 2.2), 3.4 ** 2.2,"Exponentiation operator ** works as intended");
  19. assert.areEqual(Math.pow({}, 2.2), ({}) ** 2.2, "Exponentiation operator ** works as intended");
  20. }
  21. },
  22. {
  23. name: "Right associativity",
  24. body: function () {
  25. assert.areEqual( Math.pow(2, Math.pow(3, 2)), 2 ** 3 ** 2, "** is right associative");
  26. }
  27. },
  28. {
  29. name: "Exponentiation operator assignment",
  30. body: function () {
  31. var a = 2;
  32. a**= 4;
  33. assert.areEqual(Math.pow(2,4), a, "Exponentiation assignment works as intended");
  34. var b = -2;
  35. b **= -4;
  36. assert.areEqual(Math.pow(-2,-4), b,"Exponentiation assignment works as intended");
  37. var c = -3;
  38. var d = 0;
  39. d = ++c**3;
  40. assert.areEqual(Math.pow(-2,3), d,"Exponentiation assignment works as intended with preincrement operator");
  41. c = -3;
  42. d = --c**3;
  43. assert.areEqual(Math.pow(-4,3), d,"Exponentiation assignment works as intended with predecrement operator");
  44. c = -3;
  45. d = c++**3;
  46. assert.areEqual(Math.pow(-3,3), d,"Exponentiation assignment works as intended with postincrement operator");
  47. c = -3;
  48. d = 2**++c;
  49. assert.areEqual(Math.pow(2,-2), d,"Exponentiation assignment works as intended with preincrement operator on the right hand side");
  50. c = -3;
  51. d = 2**c++;
  52. assert.areEqual(Math.pow(2,-3), d,"Exponentiation assignment works as intended with postincrement operator on the right hand side");
  53. }
  54. },
  55. {
  56. name: "Exponentiation operator must be evaluated before multiplication ",
  57. body: function () {
  58. assert.areEqual(2 * (3 ** 4), 2 * 3 ** 4, "Exponentiation operator has higher precedence than multiplicative operator *");
  59. assert.areEqual(2 * (3 ** 4) * 5 , 2 * 3 ** 4 * 5, "Exponentiation operator has higher precedence than multiplicative operator *");
  60. assert.areEqual(2 + (3 ** 2), 2 + 3 ** 2, "Exponentiation operator has higher precedence than additive operator +");
  61. }
  62. },
  63. {
  64. name: "Exponentiation syntax error cases ",
  65. body: function () {
  66. assert.throws(function () { eval("-2**3"); }, SyntaxError, "Exponentiation operator throws if the left hand side is an unary operator +", "Invalid unary operator on the left hand side of exponentiation (**) operator");
  67. assert.throws(function () { eval("+2**3"); }, SyntaxError, "Exponentiation operator throws if the left hand side is an unary operator -", "Invalid unary operator on the left hand side of exponentiation (**) operator");
  68. assert.throws(function () { eval("delete 2**3"); }, SyntaxError, "Exponentiation operator throws if the left hand side is an unary operator delete", "Invalid unary operator on the left hand side of exponentiation (**) operator");
  69. assert.throws(function () { eval("!2**3"); }, SyntaxError, "Exponentiation operator throws if the left hand side is an unary operator !", "Invalid unary operator on the left hand side of exponentiation (**) operator");
  70. }
  71. },
  72. {
  73. name: "Exponentiation and Ellipsis works",
  74. body: function () {
  75. Number.prototype[Symbol.iterator] = function* () {
  76. for (let i = 0; i < this; ++i) { yield i; }
  77. }
  78. assert.areEqual("0,1,2,3,4,5,6,7", [...2**3].toString(), "Exponentiation operator works as expected with ...");
  79. Number.prototype[Symbol.iterator] = null;
  80. }
  81. },
  82. ];
  83. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });