es6toLength.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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: "Concat toLength tests",
  9. body: function ()
  10. {
  11. var c = [];
  12. c[0] = 1;
  13. c[100] = 2;
  14. var oNeg = { length : -1, 0 : 3, 1: 4, [Symbol.isConcatSpreadable] : true};
  15. c = c.concat(oNeg);
  16. assert.areEqual(1, c[0], "confirm indices of array concated to did not change")
  17. assert.areEqual(2, c[100], "confirm indices of array concated to did not change");
  18. assert.areEqual(undefined, c[101], "Length of oNeg is coerced to 0 nothing is concated here");
  19. }
  20. },
  21. {
  22. name: "IndexOf toLength tests",
  23. body: function ()
  24. {
  25. var a = [];
  26. a[4294967294] = 2;
  27. a[4294967295] = 3;
  28. a[4294967296] = 4;
  29. var o32 = { length : 4294967296, 4294967294 : 2, 4294967295: 3 };
  30. var maxLength64 = Math.pow(2,53)-1;
  31. var o64 = { length : maxLength64, [maxLength64-2] : 2, [maxLength64-1]: 3 };
  32. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  33. assert.areEqual(4294967294,a.indexOf(2, 4294967290), "confirm we can find value 2 in the index range given");
  34. assert.areEqual(-1,a.indexOf(3, 4294967290), "indexOf on an array is bound by array max length");
  35. assert.areEqual(-1,a.indexOf(4, 4294967290), "indexOf on an array is bound by array max length");
  36. assert.areEqual(4294967294,Array.prototype.indexOf.call(o32, 2, 4294967290), "confirm we can find value 2 in the index range given");
  37. assert.areEqual(4294967295,Array.prototype.indexOf.call(o32, 3, 4294967290), "objects don't have a bounded length so we should find the index given any length");
  38. var o32 = { length : 4294967297, 4294967294 : 2, 4294967295: 3, 4294967296: 4};
  39. assert.areEqual(4294967296,Array.prototype.indexOf.call(o32, 4, 4294967290), "objects don't have a bounded length so we should find the index given any length");
  40. assert.areEqual(maxLength64-2,Array.prototype.indexOf.call(o64, 2, maxLength64-10), "confirm indexOf can have an index up to length set to the max integer without loss of precision");
  41. assert.areEqual(maxLength64-2,Array.prototype.indexOf.call(o64, 2, maxLength64-10), "confirm indexOf can have an index up to length set to the max integer without loss of precision");
  42. assert.areEqual(maxLength64-1,Array.prototype.indexOf.call(o64, 3, maxLength64-10), "confirm indexOf can have an index up to length set to the max integer without loss of precision");
  43. assert.areEqual(maxLength64-1,Array.prototype.indexOf.call(o64, 3, maxLength64-10), "confirm indexOf can have an index up to length set to the max integer without loss of precision");
  44. assert.areEqual(-1, Array.prototype.indexOf.call(oNeg,2), "confirm negative lengths are coerced to 0, so we should not find any of these indices");
  45. //Note a.indexOf(2) will spin for a very long time, we should probably Consider enumerating instead of walking all indices
  46. }
  47. },
  48. {
  49. name: "Reverse toLength tests",
  50. body: function ()
  51. {
  52. var a = [];
  53. a[0] = 0;
  54. a[4294967294] = 1;
  55. a.reverse();
  56. a[4294967295] = 2;
  57. a[4294967296] = 3;
  58. assert.areEqual(1, a[0], " confirm reverse still happens on the bounds of an array");
  59. assert.areEqual(0, a[4294967294], " confirm reverse still happens on the bounds of an array");
  60. a.reverse();
  61. assert.areEqual(0, a[0]," confirm reverse still happens on the bounds of an array");
  62. assert.areEqual(1, a[4294967294], " confirm reverse still happens on the bounds of an array");
  63. assert.areEqual(2, a[4294967295], "index 4294967295 is beyond the bounds of an array so it does not reverse");
  64. assert.areEqual(3, a[4294967296], "index 4294967296 is beyond the bounds of an array so it does not reverse");
  65. var o32 = { length : -1, 4294967294 : 1, 0: 0 };
  66. Array.prototype.reverse.call(o32);
  67. assert.areEqual(0, o32[0], "confirm length does not get converted to 4294967295");
  68. assert.areEqual(1, o32[4294967294],"confirm length does not get converted to 4294967295");
  69. assert.areEqual(-1,o32.length, "length returns -1");
  70. // Note it is not practical to reverse an object length 4294967295 or larger since we will hit an
  71. // Out of memory exception before computation ever completes. As a result we will have a test coverage hole,
  72. // but at the moment it is not a real world scenario.
  73. }
  74. },
  75. {
  76. name: "Shift toLength tests",
  77. body: function ()
  78. {
  79. var a = [];
  80. a[0] = 0;
  81. a[4294967294] = 2;
  82. a[4294967295] = 3;
  83. a[4294967296] = 4;
  84. var o32 = { length : -1, 4294967294 : 1, 0: 0 };
  85. var shifted = a.shift();
  86. assert.areEqual(0, shifted);
  87. assert.areEqual(undefined, a[0]);
  88. assert.areEqual(undefined, a[4294967294], "confirm 4294967294 is now empty after shift");
  89. assert.areEqual(2, a[4294967293], "confirm 4294967293 now has contents of index 4294967294 after shift");
  90. assert.areEqual(3, a[4294967295], "confirm index 4294967295 does not shift because it is past array length max");
  91. assert.areEqual(4, a[4294967296], "confirm index 4294967296 does not shift because it is past array length max");
  92. Array.prototype.shift.call(o32);
  93. assert.areEqual(0, o32[0], "confirm length does not get converted to 4294967295");
  94. assert.areEqual(1, o32[4294967294],"confirm length does not get converted to 4294967295");
  95. // Note it is not practical to shift an object length 4294967295 or larger since we will hit an
  96. // Out of memory exception before computation ever completes. As a result we will have a test coverage hole,
  97. // but at the moment it is not a real world scenario.
  98. }
  99. },
  100. {
  101. name: "UnShift toLength tests",
  102. body: function ()
  103. {
  104. /*var o = {
  105. 0 : 0,
  106. 4294967294 : 2,
  107. 4294967295 : 3,
  108. 4294967296 : 4,
  109. length : 4294967297
  110. }//consider property enumeration*/
  111. var o32 = { length : -1, 4294967294 : 1, 0: 0 };
  112. Array.prototype.unshift.call(o32, -1);
  113. assert.areEqual(-1, o32[0], "confirm length does not get converted to 4294967295");
  114. assert.areEqual(undefined, o32[1], "since length was negative, we can not account for any indicies we over write and so 0 does not shift down");
  115. assert.areEqual(1, o32["length"], "confirm length does not get converted to 4294967295 and instead is updated after an unshift");
  116. assert.areEqual(1, o32[4294967294],"length will not account this because we added it when length was invalid");
  117. // Note it is not practical to unshift an object length 4294967295 or larger since we will hit an
  118. // Out of memory exception before computation ever completes. As a result we will have a test coverage hole,
  119. // but at the moment it is not a real world scenario.
  120. }
  121. },
  122. {
  123. name: "Push toLength tests",
  124. body: function ()
  125. {
  126. var o = {
  127. 0 : 0,
  128. 4294967294 : 2,
  129. 4294967295 : 3,
  130. 4294967296 : 4,
  131. length : 4294967297
  132. }
  133. assert.areEqual(4294967297, o.length, "confirm length is 4294967297");
  134. Array.prototype.push.call(o,5);
  135. assert.areEqual(5, o[4294967297]);
  136. assert.areEqual(4294967298, o.length, "confirm length incremented by 1");
  137. Array.prototype.push.call(o,6,7);
  138. assert.areEqual(6, o[4294967298]);
  139. assert.areEqual(7, o[4294967299]);
  140. assert.areEqual(4294967300, o.length, "confirm length incremented by 2");
  141. }
  142. },
  143. {
  144. name: "Pop toLength tests",
  145. body: function ()
  146. {
  147. var o = {
  148. 0 : 0,
  149. 4294967294 : 2,
  150. 4294967295 : 3,
  151. 4294967296 : 4,
  152. length : 4294967297
  153. }
  154. var popped = Array.prototype.pop.call(o);
  155. assert.areEqual(4, popped);
  156. assert.areEqual(4294967296, o.length, "confirm length decremented by 1");
  157. var popped = Array.prototype.pop.call(o);
  158. assert.areEqual(3, popped);
  159. assert.areEqual(4294967295, o.length, "confirm length decremented by 1");
  160. var popped = Array.prototype.pop.call(o);
  161. assert.areEqual(2, popped);
  162. assert.areEqual(4294967294, o.length, "confirm length decremented by 1");
  163. var popped = Array.prototype.pop.call(o);
  164. assert.areEqual(undefined, popped, "pop decrements by one so and we already popped off the top part of this sparse object");
  165. assert.areEqual(4294967293, o.length, "confirm length decremented by 1");
  166. var o32 = { length : -1, 4294967294 : 1, 0: 0 };
  167. var popped = Array.prototype.pop.call(o32);
  168. assert.areEqual(undefined,popped, "confirm we were unable to pop anything because -1 length no longer converts to uint max and instead is coerced to 0");
  169. assert.areEqual(0, o32[0], "nothing was popped because of invalid length");
  170. assert.areEqual(1, o32[4294967294], "nothing was popped because of invalid length");
  171. assert.areEqual(0, o32.length, "length should get set to 0");
  172. var a = [0]
  173. a[4294967294] = 2;
  174. a[4294967295] = 3;
  175. a[4294967296] = 4;
  176. assert.areEqual(4294967295, a.length, "length is at max");
  177. var popped = a.pop();
  178. assert.areEqual(2, popped, "confirm we start popping at index 4294967294 and get value 2 from it");
  179. assert.areEqual(4294967294, a.length, "confirm length decremented by 1");
  180. }
  181. },
  182. {
  183. name: "Slice toLength tests",
  184. body: function ()
  185. {
  186. var a = [];
  187. a[0] = 0;
  188. a[4294967294/2] = 1;
  189. a[4294967294] = 2;
  190. a[4294967295] = 3;
  191. a[4294967296] = 4;
  192. var o32 = { length : 4294967296, 4294967294 : 2, 4294967295: 3 };
  193. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  194. var aCopy = a.slice();
  195. var aFront = a.slice(0,4294967294/2 +1);
  196. var aBack = a.slice(4294967294/2,4294967295);
  197. assert.areEqual(a[0], aCopy[0]);
  198. assert.areEqual(a[4294967294/2], aCopy[4294967294/2]);
  199. assert.areEqual(a[4294967294], aCopy[4294967294]);
  200. assert.areEqual(undefined, aCopy[4294967295], "slice only copies indices up to uint32max");
  201. assert.areEqual(undefined, aCopy[4294967296], "slice only copies indices up to uint32max");
  202. assert.areEqual(a[0], aFront[0]);
  203. assert.areEqual(a[4294967294/2], aFront[4294967294/2]);
  204. assert.areEqual(a[4294967294/2], aBack[0]);
  205. assert.areEqual(a[4294967294], aBack[4294967294/2]);
  206. assert.throws(function() { Array.prototype.slice.call(o32); }, RangeError, "slice can't make an array larger than the array max length", "Array length must be a finite positive integer");
  207. assert.areEqual([], Array.prototype.slice.call(oNeg), "negative length get converted to 0, so slice returns an empty array");
  208. // If we change the Array species it does not throw but its to slow to test
  209. /*
  210. class MyArray extends Array {
  211. // Overwrite species to the parent Array constructor
  212. static get [Symbol.species]() { return Object; }
  213. }
  214. var myArr = new MyArray();
  215. myArr[0] = 0;
  216. myArr[4294967294] = 1;
  217. myArr[4294967295] = 2;
  218. Array.prototype.slice.call(myArr);*/
  219. }
  220. },
  221. {
  222. name: "Splice toLength tests",
  223. body: function ()
  224. {
  225. //TODO when we change splice
  226. }
  227. },
  228. {
  229. name: "Every toLength tests",
  230. body: function ()
  231. {
  232. var a = [];
  233. a[0] = 0;
  234. a[4294967294/2] = 1;
  235. a[4294967294] = 2;
  236. a[4294967295] = 3;
  237. function isEven(element, index, array)
  238. {
  239. return element %2 == 0;
  240. }
  241. //a.every(isEven); // Note perf issue: spec says callback is invoked only for indexes of the array which have
  242. // assigned values; it is not invoked for indexes which have been deleted or
  243. // which have never been assigned values.
  244. // Sounds like we should do some kind of sparse array optimization or
  245. // enumerating instead of walking all indices
  246. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  247. assert.areEqual(true, Array.prototype.every.call(oNeg, isEven), "oNeg has length coerced 0, so we never find an index that proves our comparison false");
  248. // Given that is not practical to write tests for arrays with boundary numbers I'm not going to bother with typed Arrays and other objects
  249. }
  250. },
  251. {
  252. name: "Some toLength tests",
  253. body: function ()
  254. {
  255. var a = [];
  256. a[0] = 1;
  257. a[4294967294/2] = 1;
  258. a[4294967294] = 2;
  259. a[4294967295] = 3;
  260. function isEven(element, index, array)
  261. {
  262. return element %2 == 0;
  263. }
  264. //a.some(isEven); // same issue as Map, ForEach, Filter, & Every
  265. // not as bad as Array.prototype.every because we can quit as soon as we find a true case
  266. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  267. assert.areEqual(false, Array.prototype.some.call(oNeg, isEven), "oNeg has length coerced 0, so we never find an index that proves our comparison true");
  268. // Given that is not practical to write tests for arrays with boundary numbers I'm not going to bother with typed Arrays and other objects
  269. }
  270. },
  271. {
  272. name: "ForEach toLength tests",
  273. body: function ()
  274. {
  275. var a = [];
  276. a[0] = 1;
  277. a[4294967294/2] = 2;
  278. a[4294967294] = 3;
  279. a[4294967295] = 4;
  280. var sum = 0;
  281. function summation(element, index, array) {
  282. sum += element;
  283. }
  284. //a.forEach(summation); // same issue as Map, Filter, Some, & Every
  285. //assert.areEqual(6,sum);
  286. sum = 0;
  287. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  288. Array.prototype.forEach.call(oNeg, summation);
  289. assert.areEqual(0,sum,"oNeg has length coerced 0, so we never perform a summation");
  290. // Given that is not practical to write tests for arrays with boundary numbers I'm not going to bother with typed Arrays and other objects
  291. }
  292. },
  293. {
  294. name: "Map toLength tests",
  295. body: function ()
  296. {
  297. var a = [];
  298. a[0] = 1;
  299. a[4294967294/2] = 4;
  300. a[4294967294] = 9;
  301. a[4294967295] = 16;
  302. //var b = a.map(Math.sqrt); // same issue as ForEach, Some, & Every
  303. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  304. assert.areEqual([], Array.prototype.map.call(oNeg, Math.sqrt));
  305. var o32 = { length : 4294967296, 4294967294 : 4, 4294967295: 9 };
  306. assert.throws(function() { Array.prototype.map.call(o32, Math.sqrt); }, RangeError, "Map can't make an array larger than the array max length", "Array length must be a finite positive integer");
  307. // If we change the Array species it does not throw but its to slow to test
  308. /*class MyArray extends Array {
  309. // Overwrite species to the parent Array constructor
  310. static get [Symbol.species]() { return Object; }
  311. }
  312. var myArr = new MyArray();
  313. myArr[0] = 0;
  314. myArr[4294967294] = 1;
  315. myArr[4294967295] = 2;
  316. Array.prototype.map.call(myArr, Math.sqrt);*/
  317. }
  318. },
  319. {
  320. name: "Filter toLength tests",
  321. body: function ()
  322. {
  323. var a = [];
  324. a[0] = 1;
  325. a[4294967294/2] = 4;
  326. a[4294967294] = 9;
  327. a[4294967295] = 16;
  328. function biggerThanFive(element)
  329. {
  330. return element > 5;
  331. }
  332. //a.filter(biggerThanFive); // same issue as Map, ForEach, Some, & Every
  333. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  334. assert.areEqual([], Array.prototype.filter.call(oNeg, biggerThanFive));
  335. }
  336. },
  337. {
  338. name: "Reduce toLength tests",
  339. body: function ()
  340. {
  341. var a = [];
  342. a[0] = 1;
  343. a[4294967294/2] = 2;
  344. a[4294967294] = 3;
  345. a[4294967295] = 4;
  346. var sum = function(a, b) {
  347. return a + b;
  348. }
  349. // a.reduce(sum); // same issue as Map, ForEach, Filter, Some, & Every
  350. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  351. assert.throws(function() { Array.prototype.reduce.call(oNeg, sum)},TypeError,"Reduce needs a length greater than 0","Object doesn't support this action");
  352. }
  353. },
  354. {
  355. name: "ReduceRight toLength tests",
  356. body: function ()
  357. {
  358. var a = [];
  359. a[0] = 1;
  360. a[4294967294/2] = 2;
  361. a[4294967294] = 3;
  362. a[4294967295] = 4;
  363. var sum = function(a, b) {
  364. return a + b;
  365. }
  366. // a.reduceRight(sum); // same issue as Reduce, Map, ForEach, Filter, Some, & Every
  367. var oNeg = { length : -1, [-5] : 2, [-2]: 3 };
  368. assert.throws(function() { Array.prototype.reduceRight.call(oNeg, sum)},TypeError,"Reduce needs a length greater than 0","Object doesn't support this action");
  369. }
  370. },
  371. ];
  372. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });