max.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // No arguments
  6. check(-Infinity, Math.max(), "max()");
  7. // NaN
  8. check(NaN, Math.max(NaN), "max(NaN)");
  9. // Other non-numbers
  10. check(NaN, Math.max({}), "max({})");
  11. check(NaN, Math.max({}, {}), "max({}, {})");
  12. check(NaN, Math.max({}, 42), "max({}, 42)");
  13. // null
  14. check(0, Math.max(null), "max(null)");
  15. // +0 / -0
  16. check(0, Math.max(+0,-0), "max(+0,-0)");
  17. check(0, Math.max(-0,+0), "max(-0,+0)");
  18. check(0, Math.max(+0, -5, -0), "max(+0, -5, -0)");
  19. check(0, Math.max(-0, -5, +0), "max(-0, -5, +0)");
  20. // Length property
  21. check(2, Math.max.length, "max.length");
  22. var values = [0, -0, -1, 1, -2, 2, 3.14159, 1000.123, 0x1fffffff, -0x20000000, Infinity, -Infinity];
  23. for(var i=0; i < values.length; ++i)
  24. {
  25. // single value
  26. check(values[i], Math.max(values[i]), "max(" + values[i] + ")");
  27. // Infinitys
  28. check(Infinity, Math.max(values[i], Infinity), "max(" + values[i] + ", Infinity)");
  29. check(values[i], Math.max(values[i], -Infinity), "max(" + values[i] + ", -Infinity)");
  30. }
  31. // check max is the first/last value in the array
  32. check(5, Math.max(5, 1, 2, 3, 4), "max is the first value");
  33. check(5, Math.max(4, 1, 2, 3, 5), "max is the last value");
  34. check(57000.4, Math.max(1.3, 1, 57000.4, 3, 4), "max is the first value");
  35. // check max with 50 nearby double values
  36. check(3.0981503795899998000e+007,
  37. Math.max(
  38. 3.0981503795899998000e+007,
  39. 3.0981503795899995000e+007,
  40. 3.0981503795899991000e+007,
  41. 3.0981503795899987000e+007,
  42. 3.0981503795899983000e+007,
  43. 3.0981503795899980000e+007,
  44. 3.0981503795899976000e+007,
  45. 3.0981503795899972000e+007,
  46. 3.0981503795899969000e+007,
  47. 3.0981503795899965000e+007,
  48. 3.0981503795899961000e+007,
  49. 3.0981503795899957000e+007,
  50. 3.0981503795899954000e+007,
  51. 3.0981503795899950000e+007,
  52. 3.0981503795899946000e+007,
  53. 3.0981503795899943000e+007,
  54. 3.0981503795899939000e+007,
  55. 3.0981503795899935000e+007,
  56. 3.0981503795899931000e+007,
  57. 3.0981503795899928000e+007,
  58. 3.0981503795899924000e+007,
  59. 3.0981503795899920000e+007,
  60. 3.0981503795899916000e+007,
  61. 3.0981503795899913000e+007,
  62. 3.0981503795899909000e+007,
  63. 3.0981503795899905000e+007,
  64. 3.0981503795899902000e+007,
  65. 3.0981503795899898000e+007,
  66. 3.0981503795899894000e+007,
  67. 3.0981503795899890000e+007,
  68. 3.0981503795899887000e+007,
  69. 3.0981503795899883000e+007,
  70. 3.0981503795899879000e+007,
  71. 3.0981503795899875000e+007,
  72. 3.0981503795899872000e+007,
  73. 3.0981503795899868000e+007,
  74. 3.0981503795899864000e+007,
  75. 3.0981503795899861000e+007,
  76. 3.0981503795899857000e+007,
  77. 3.0981503795899853000e+007,
  78. 3.0981503795899849000e+007,
  79. 3.0981503795899846000e+007,
  80. 3.0981503795899842000e+007,
  81. 3.0981503795899838000e+007,
  82. 3.0981503795899834000e+007,
  83. 3.0981503795899831000e+007,
  84. 3.0981503795899827000e+007,
  85. 3.0981503795899823000e+007,
  86. 3.0981503795899820000e+007,
  87. 3.0981503795899816000e+007
  88. ), "many values");
  89. // special cases on -0.0, infinity, etc
  90. WScript.Echo(Number.POSITIVE_INFINITY);
  91. WScript.Echo(Number.NEGATIVE_INFINITY);
  92. var opD = +0.0;
  93. var onD = -0.0;
  94. var op = 1 / Number.POSITIVE_INFINITY
  95. var on = 1 / Number.NEGATIVE_INFINITY;
  96. var infp = 1 / op;
  97. var infn = 1 / on;
  98. var infpD = 1 / (+0.0);
  99. var infnD = 1 / (-0.0);
  100. WScript.Echo("opD = " + opD + " , onD = " + onD + " op = " + op + " , on = " + on + " , infp = " + infp + " , infn = " + infn + " , infpD = " + infpD + " , infnD = " + infnD);
  101. WScript.Echo("Math.max(+0.0, -0.0) " + Math.max(+0.0, -0.0));
  102. if (1 / Math.max(+0.0, -0.0) < 0) {
  103. WScript.Echo("Check (1 / Math.max(+0.0, -0.0) < 0) - true ");
  104. }
  105. else {
  106. WScript.Echo("Check (1 / Math.max(+0.0, -0.0) < 0) - false ")
  107. }
  108. check(5, Math.max(5, Number.NEGATIVE_INFINITY), "max is not the negative infinity");
  109. check(Number.POSITIVE_INFINITY, Math.max(5, Number.POSITIVE_INFINITY), "max is the positive infinity");
  110. check(Number.POSITIVE_INFINITY, Math.max(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY), "max is the positive infinity");
  111. WScript.Echo("done");
  112. function check(actual, expected, str)
  113. {
  114. if (isNaN(expected))
  115. {
  116. if (actual.toString() !== expected.toString())
  117. {
  118. WScript.Echo("fail: " + str);
  119. }
  120. }
  121. else
  122. {
  123. if (actual !== expected)
  124. {
  125. WScript.Echo("fail: " + str);
  126. }
  127. }
  128. }
  129. //negative zero
  130. function foo(a,b)
  131. {
  132. var c = Math.max(a,b);
  133. return 1/c;
  134. }
  135. WScript.Echo(foo(-0,0));
  136. WScript.Echo(foo(-0,0));
  137. WScript.Echo(foo(0,-0));
  138. WScript.Echo(foo(-0,-0));
  139. function BLUE143505(a,b)
  140. {
  141. var c;
  142. if ((Math.max(-4, undefined))) {
  143. c = false;
  144. }
  145. return c;
  146. };
  147. WScript.Echo(BLUE143505());
  148. WScript.Echo(BLUE143505());
  149. function findMaxInArray()
  150. {
  151. var arr = new Array(3);
  152. prop1 = 0;
  153. arr[1] = 1;
  154. arr[0] = 1;
  155. WScript.Echo(Math.max.apply(Math, arr));
  156. Object.prototype[2] = prop1;
  157. }
  158. findMaxInArray();
  159. findMaxInArray();