ES6ArrayAPI.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. // ES6 Array extension tests -- verifies the API shape and basic functionality
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var tests = [
  8. {
  9. name: "Array constructor has correct functions",
  10. body: function() {
  11. assert.isTrue(Array.hasOwnProperty('from'), "Array.hasOwnProperty('from');");
  12. assert.areEqual('function', typeof Array.from, "typeof Array.from === 'function'");
  13. assert.areEqual(1, Array.from.length, "Array.from.length === 0");
  14. assert.isTrue(Array.hasOwnProperty('of'), "Array.hasOwnProperty('of');");
  15. assert.areEqual('function', typeof Array.of, "typeof Array.of === 'function'");
  16. assert.areEqual(0, Array.of.length, "Array.of.length === 0");
  17. }
  18. },
  19. {
  20. name: "[0].indexOf(-0.0) should return 0",
  21. body: function() {
  22. assert.areEqual(0, [0].indexOf(-0.0), "[0].indexOf(-0.0) should return 0");
  23. }
  24. },
  25. {
  26. name: "Array.from basic behavior",
  27. body: function() {
  28. assert.areEqual([], Array.from([]), "Array.from simplest usage is copying empty array");
  29. assert.areEqual([], Array.from([], undefined), "Array.from disables mapping function when the param is explicitly passed as undefined");
  30. assert.areEqual([0,1,2,3], Array.from([0,1,2,3]), "Array.from basic behavior with an iterable object");
  31. assert.areEqual([0,1,2,3], Array.from({ 0: 0, 1: 1, 2: 2, 3: 3, length: 4 }), "Array.from basic behavior with an object with length but no iterator");
  32. }
  33. },
  34. {
  35. name: "Array.from special behaviors",
  36. body: function() {
  37. var fromFnc = Array.from;
  38. var b = fromFnc.call(Array, "string");
  39. assert.areEqual('object', typeof b, "Array.from.call(Array, 'string') returns an array");
  40. assert.areEqual(['s','t','r','i','n','g'], b, "Array.from.call(Array, 'string') == ['s','t','r','i','n','g']");
  41. assert.areEqual(6, b.length, "Array.from.call(Array, 'string').length === 6");
  42. assert.isFalse(ArrayBuffer.isView(b), "Array.from.call(Array, 'string') is not a TypedArray");
  43. var b = fromFnc.call(String, [0,1,2,3]);
  44. assert.areEqual('object', typeof b, "Array.from.call(String, [0,1,2,3]) returns a String object");
  45. assert.areEqual('', b.toString(), "Array.from.call(String, [0,1,2,3]).toString() == '4'");
  46. assert.areEqual(0, b.length, "Array.from.call(String, [0,1,2,3]).length === 1");
  47. assert.isFalse(ArrayBuffer.isView(b), "Array.from.call(String, [0,1,2,3]) is not a TypedArray");
  48. assert.areEqual(0, b[0], "Integer-indexed properties are still added to the string");
  49. assert.areEqual(1, b[1], "Integer-indexed properties are still added to the string");
  50. assert.areEqual(2, b[2], "Integer-indexed properties are still added to the string");
  51. assert.areEqual(3, b[3], "Integer-indexed properties are still added to the string");
  52. var a = { 0: 0, 1: 1, 2: 2, 3: 3, length: 4 };
  53. var b = fromFnc.call(String, a);
  54. assert.areEqual('object', typeof b, "Array.from.call(String, objectLikeArray) returns a String object");
  55. assert.areEqual('4', b.toString(), "Array.from.call(String, objectLikeArray).toString() == '4'");
  56. assert.areEqual(1, b.length, "Array.from.call(String, objectLikeArray).length === 1");
  57. assert.isFalse(ArrayBuffer.isView(b), "Array.from.call(String, objectLikeArray) is not a TypedArray");
  58. assert.areEqual(1, b[1], "Integer-indexed properties are still added to the string");
  59. assert.areEqual(2, b[2], "Integer-indexed properties are still added to the string");
  60. assert.areEqual(3, b[3], "Integer-indexed properties are still added to the string");
  61. assert.areEqual('4', b[0], "Zero-th property of the string is set to the string value, can't overwrite this via put");
  62. assert.throws(function() { fromFnc.call(Uint8Array, { 0: 0, 1: 1, 2: 2, length: 5 }); }, TypeError, "Array.from tries to set length of the object returned from the constructor which will throw for TypedArrays", "Cannot define property: object is not extensible");
  63. var a = { 0: 0, 1: 1, 3: 3, length: 5 };
  64. var b = fromFnc.call(Array, a);
  65. assert.areEqual('object', typeof b, "Array.from.call(Array, objectWithLengthProperty) returns an object");
  66. assert.areEqual('0,1,,3,', b.toString(), "Array.from.call(String, [0,1,2,3]).toString() == '4'");
  67. assert.areEqual(5, b.length, "Array.from.call(Array, objectWithLengthProperty) returns a new array with length = a.length");
  68. assert.isFalse(ArrayBuffer.isView(b), "Array.from.call(Array, objectWithLengthProperty) is not a TypedArray (ArrayBuffer.isView)");
  69. assert.areEqual([0,1,undefined,3,undefined], b, "Array.from.call(Array, objectWithLengthProperty) has missing values set to undefined");
  70. var a = { 0: 0, 1: 1 };
  71. var b = fromFnc.call(Array, a);
  72. assert.areEqual('object', typeof b, "Array.from.call(Array, objectWithLengthNoProperty) returns an object");
  73. assert.areEqual(0, b.length, "Array.from.call(Array, objectWithLengthNoProperty) returns a new array with length = 0");
  74. assert.isFalse(ArrayBuffer.isView(b), "Array.from.call(Array, objectWithLengthNoProperty) is not a TypedArray (ArrayBuffer.isView)");
  75. assert.areEqual([], b, "Array.from.call(Array, objectWithLengthNoProperty) is an empty array");
  76. assert.areEqual([0,1,2], fromFnc.call(undefined, [0,1,2]), "Calling Array.from with undefined this argument produces an array");
  77. assert.areEqual([0,1,2], fromFnc.call(null, [0,1,2]), "Calling Array.from with null this argument produces an array");
  78. assert.areEqual([0,1,2], fromFnc.call({}, [0,1,2]), "Calling Array.from with a non-function this argument produces an array");
  79. assert.areEqual([0,1,2], fromFnc.call(Math.sin, [0,1,2]), "Calling Array.from with a non-constructor function this argument produces an array");
  80. }
  81. },
  82. {
  83. name: "Array.from error conditions",
  84. body: function() {
  85. assert.throws(function () { Array.from(); }, TypeError, "Calling Array.from with non-object items argument throws TypeError", "Array.from: argument is not an Object");
  86. assert.throws(function () { Array.from(undefined); }, TypeError, "Calling Array.from with non-object items argument throws TypeError", "Array.from: argument is not an Object");
  87. assert.throws(function () { Array.from(null); }, TypeError, "Calling Array.from with non-object items argument throws TypeError", "Array.from: argument is not an Object");
  88. assert.throws(function () { Array.from({}, null); }, TypeError, "Calling Array.from with non-object mapFn argument throws TypeError", "Array.from: argument is not a Function object");
  89. assert.throws(function () { Array.from({}, 'string'); }, TypeError, "Calling Array.from with non-object mapFn argument throws TypeError", "Array.from: argument is not a Function object");
  90. assert.throws(function () { Array.from({}, {}); }, TypeError, "Calling Array.from with non-function mapFn argument throws TypeError", "Array.from: argument is not a Function object");
  91. }
  92. },
  93. {
  94. name: "Array.from behavior with a map function",
  95. body: function() {
  96. var i = 0;
  97. function mapFunction(val, k) {
  98. assert.areEqual(i, k, "Array.from called with a mapping function, we should get the elements in order. Setting item[" + k + "] = " + val);
  99. assert.areEqual(val, k, "Array.from called with a mapping function, Value and index should be same for this test");
  100. assert.areEqual(2, arguments.length, "Array.from called with a mapping function, only 2 elements should be passed as arguments");
  101. // increment expected index
  102. i++;
  103. }
  104. var objectWithoutIterator = {
  105. 0: 0,
  106. 1: 1,
  107. 2: 2,
  108. 3: 3,
  109. length: 4
  110. };
  111. // Verify mapFunction gets called with correct arguments
  112. Array.from(objectWithoutIterator, mapFunction);
  113. }
  114. },
  115. {
  116. name: "Array.from behavior with a map function passing this argument",
  117. body: function() {
  118. var thisArg = 'thisArg';
  119. function mapFunction(val, k) {
  120. // this will be wrapped as an Object
  121. assert.areEqual(Object(thisArg), this, "thisArg passed into Array.from should flow into mapFunction");
  122. }
  123. var objectWithoutIterator = {
  124. 0: 0,
  125. 1: 1,
  126. 2: 2,
  127. 3: 3,
  128. length: 4
  129. };
  130. // Verify mapFunction gets called with thisArg passed as this
  131. Array.from(objectWithoutIterator, mapFunction, thisArg);
  132. }
  133. },
  134. {
  135. name: "Array.from behavior with a map function that mutates source object",
  136. body: function() {
  137. var objectWithoutIterator = {
  138. 0: 0,
  139. 1: 1,
  140. 2: 2,
  141. 3: 3,
  142. 4: 4,
  143. length: 5
  144. };
  145. function mapFunction(val, k) {
  146. switch (k) {
  147. case 0:
  148. // change the objectWithoutIterator length value - we should still fetch the rest of the indexed-elements anyway
  149. objectWithoutIterator.length = 0;
  150. return val;
  151. case 1:
  152. // change the value of the next indexed value - the new value should end up in the return object
  153. objectWithoutIterator[2] = 200;
  154. return val;
  155. case 2:
  156. // change the value of a previous indexed value - the old value should end up in the return object
  157. objectWithoutIterator[0] = -100;
  158. return val;
  159. case 3:
  160. // delete the next indexed value - return object should have undefined there
  161. delete objectWithoutIterator[4];
  162. return val;
  163. }
  164. // otherwise
  165. return val;
  166. }
  167. assert.areEqual([0,1,200,3,undefined], Array.from(objectWithoutIterator, mapFunction), "Array.from called with a map function that mutates the source object");
  168. }
  169. },
  170. {
  171. name: "Array.from behavior with iterator and a map function",
  172. body: function() {
  173. var j = 0;
  174. var checkThisArg = false;
  175. var thisArg = 'string';
  176. var objectWithIterator = {
  177. [Symbol.iterator]: function() {
  178. return {
  179. i: 0,
  180. next: function () {
  181. return {
  182. done: this.i == 5,
  183. value: this.i++
  184. };
  185. }
  186. };
  187. }
  188. };
  189. function mapFunction(val, k) {
  190. assert.areEqual(j, val, "Array.from called with a mapping function, we should get the elements in order. Setting item[" + j + "] = " + val);
  191. assert.areEqual(val, k, "Array.from called with a mapping function, index should match the value passed in");
  192. assert.areEqual(2, arguments.length, "Array.from called with a mapping function, only 2 elements should be passed as arguments");
  193. // increment expected value
  194. j++;
  195. if (checkThisArg) {
  196. // this will be wrapped as an Object
  197. assert.areEqual(Object(thisArg), this, "thisArg passed into Array.from should flow into mapFunction");
  198. }
  199. }
  200. // Verify mapFunction gets called with correct arguments
  201. Array.from(objectWithIterator, mapFunction);
  202. j = 0;
  203. checkThisArg = true;
  204. // Verify mapFunction gets called with thisArg passed as this
  205. Array.from(objectWithIterator, mapFunction, thisArg);
  206. }
  207. },
  208. {
  209. name: "Array.from behavior with iterator and a map function which mutates the iterator state",
  210. body: function() {
  211. var iterator_val = 0;
  212. var objectWithIterator = {
  213. [Symbol.iterator]: function() {
  214. return {
  215. next: function () {
  216. return {
  217. done: iterator_val == 5,
  218. value: iterator_val++
  219. };
  220. }
  221. };
  222. }
  223. };
  224. var reset = false;
  225. function mapFunction(val, k) {
  226. if (val == 2 && !reset)
  227. {
  228. reset = true;
  229. iterator_val = 0;
  230. }
  231. return val;
  232. }
  233. assert.areEqual([0,1,2,0,1,2,3,4], Array.from(objectWithIterator, mapFunction), "Array.from called with map function which alters iterator state");
  234. }
  235. },
  236. {
  237. name: "Array.from behavior with badly formed iterator objects",
  238. body: function() {
  239. var objectWithIteratorThatIsNotAFunction = { [Symbol.iterator]: 'a string' };
  240. var objectWithIteratorWhichDoesNotReturnObjects = { [Symbol.iterator]: function() { return undefined; } };
  241. var objectWithIteratorNextIsNotAFunction = { [Symbol.iterator]: function() { return { next: undefined }; } };
  242. var objectWithIteratorNextDoesNotReturnObjects = { [Symbol.iterator]: function() { return { next: function() { return undefined; } }; } };
  243. assert.throws(function() { Array.from(objectWithIteratorThatIsNotAFunction); }, TypeError, "obj[@@iterator] must be a function", "Function expected");
  244. assert.throws(function() { Array.from(objectWithIteratorWhichDoesNotReturnObjects); }, TypeError, "obj[@@iterator] must return an object", "Object expected");
  245. assert.throws(function() { Array.from(objectWithIteratorNextIsNotAFunction); }, TypeError, "obj[@@iterator].next must be a function", "Function expected");
  246. assert.throws(function() { Array.from(objectWithIteratorNextDoesNotReturnObjects); }, TypeError, "obj[@@iterator].next must return an object", "Object expected");
  247. var objectWithUndefinedIterator = { 0: "a", 1: "b", length: 2, [Symbol.iterator]: undefined };
  248. var objectWithNullIterator = { 0: "a", 1: "b", length: 2, [Symbol.iterator]: null };
  249. assert.areEqual([ "a", "b" ], Array.from(objectWithUndefinedIterator), "'@@iterator: undefined' is ignored");
  250. assert.areEqual([ "a", "b" ], Array.from(objectWithNullIterator), "'@@iterator: null' is ignored");
  251. }
  252. },
  253. {
  254. name: "Array.of basic behavior",
  255. body: function() {
  256. assert.areEqual([], Array.of(), "Array.of basic behavior with no arguments");
  257. assert.areEqual([3], Array.of(3), "Array.of basic behavior with a single argument");
  258. assert.areEqual([0,1,2,3], Array.of(0, 1, 2, 3), "Array.of basic behavior with a list of arguments");
  259. }
  260. },
  261. {
  262. name: "Array.of extended behavior",
  263. body: function() {
  264. var ofFnc = Array.of;
  265. assert.throws(function() { ofFnc.call(Uint8ClampedArray, 0, -1, 2, 300, 4); }, TypeError, "Array.of tries to set length of the object returned from the constructor which will throw for TypedArrays", "Cannot define property: object is not extensible");
  266. var b = ofFnc.call(Array, 'string', 'other string', 5, { 0: 'string val',length:10 });
  267. assert.areEqual('object', typeof b, "Array.of.call(Array, ...someStringsAndObjects) returns an array");
  268. assert.areEqual(['string','other string',5,{ 0: 'string val',length:10 }], b, "Array.of.call(Array, ...someStringsAndObjects) == ['string','other string',5,{ 0: 'string val',length:10 }]");
  269. assert.areEqual(4, b.length, "Array.of.call(Array, ...someStringsAndObjects).length === 4");
  270. assert.isFalse(ArrayBuffer.isView(b), "Array.of.call(Array, ...someStringsAndObjects) is not a TypedArray");
  271. var b = ofFnc.call(String, 0, 1, 2, 3);
  272. assert.areEqual('object', typeof b, "Array.of.call(String, 0, 1, 2, 3) returns a string object");
  273. assert.areEqual(1, b.length, "Array.of.call(String, 0, 1, 2, 3) returns a string object with length 1");
  274. assert.areEqual('4', b.toString(), "Array.of.call(String, 0, 1, 2, 3) returns a string object with value == '4'");
  275. assert.areEqual('4', b[0], "Array.of.call(String, 0, 1, 2, 3) returns a string object s. s[0] == '4'");
  276. assert.areEqual(1, b[1], "Array.of.call(String, 0, 1, 2, 3) returns a string object s. s[1] == 1");
  277. assert.areEqual(2, b[2], "Array.of.call(String, 0, 1, 2, 3) returns a string object s. s[2] == 2");
  278. assert.areEqual(3, b[3], "Array.of.call(String, 0, 1, 2, 3) returns a string object s. s[3] == 3");
  279. assert.areEqual([], ofFnc.call(Array), "Array.of.call(Array) returns empty array");
  280. assert.areEqual([], ofFnc.call(), "Array.of.call() returns empty array");
  281. assert.areEqual(new String(0), ofFnc.call(String), "Array.of.call(String) returns empty string");
  282. assert.areEqual("0", ofFnc.call(String).toString(), "Array.of.call(String) returns empty string");
  283. }
  284. },
  285. {
  286. name: "OS:840217 - Array.from, Array#fill, Array#lastIndexOf should use ToLength instead of ToUint32 on their parameter's length property",
  287. body: function() {
  288. // ToLength(-1) should be 0 which means we won't execute any iterations in the below calls.
  289. Array.from({length: -1});
  290. Array.prototype.fill.call({length: -1}, 'a');
  291. Array.prototype.lastIndexOf.call({length: -1}, 'a');
  292. }
  293. },
  294. {
  295. name: "Array.from called with an items object that has length > 2^32-1 and is not iterable",
  296. body: function() {
  297. var o = {length: 4294967301};
  298. assert.throws(function() { Array.from(o); }, RangeError, "Array.from uses abstract operation ArrayCreate which throws RangeError when requested length > 2^32-1", "Array length must be a finite positive integer");
  299. }
  300. },
  301. {
  302. name: "Array.from doesn't get @@iterator twice",
  303. body: function () {
  304. let count = 0;
  305. Array.from({
  306. get [Symbol.iterator]() {
  307. count++;
  308. return [][Symbol.iterator];
  309. }
  310. });
  311. assert.areEqual(count, 1, "Array.from calls @@iterator getter once");
  312. count = 0;
  313. Array.from(new Proxy({}, {
  314. get(target, property) {
  315. if (property === Symbol.iterator) {
  316. count++;
  317. return [][Symbol.iterator];
  318. }
  319. return Reflect.get(target, property);
  320. }
  321. }));
  322. assert.areEqual(count, 1, "Array.from calls proxy's getter with @@iterator as parameter only once");
  323. }
  324. },
  325. {
  326. name: "Array#fill called with an object that has length > 2^32-1",
  327. body: function() {
  328. var e = {length: 4294967301, 4294967297: 1234};
  329. Array.prototype.fill.call(e, 5678, 4294967298, 4294967300);
  330. assert.areEqual(1234, e[4294967297], "Array.prototype.fill called on an object with length > 2^32 and a fill range existing completely past 2^32 does not fill elements outside the request range");
  331. assert.areEqual(5678, e[4294967298], "Array.prototype.fill called on an object with length > 2^32 and a fill range existing completely past 2^32 is able to fill elements with indices > 2^32");
  332. assert.areEqual(5678, e[4294967299], "Array.prototype.fill called on an object with length > 2^32 and a fill range existing completely past 2^32 is able to fill elements with indices > 2^32");
  333. assert.areEqual(undefined, e[4294967300], "Array.prototype.fill called on an object with length > 2^32 and a fill range existing completely past 2^32 does not fill elements outside the request range");
  334. var e = {length: 4294967301, 4294967292: 1234};
  335. Array.prototype.fill.call(e, 5678, 4294967293, 4294967300);
  336. assert.areEqual(1234, e[4294967292], "Array.prototype.fill called on an object with length > 2^32 and a fill range starting before 2^32 but ending after 2^32 does not fill elements outside the request range");
  337. assert.areEqual(5678, e[4294967293], "Array.prototype.fill called on an object with length > 2^32 and a fill range starting before 2^32 but ending after 2^32 is able to fill elements with indices > 2^32");
  338. assert.areEqual(5678, e[4294967294], "Array.prototype.fill called on an object with length > 2^32 and a fill range starting before 2^32 but ending after 2^32 is able to fill elements with indices > 2^32");
  339. assert.areEqual(5678, e[4294967295], "Array.prototype.fill called on an object with length > 2^32 and a fill range starting before 2^32 but ending after 2^32 is able to fill elements with indices > 2^32");
  340. assert.areEqual(5678, e[4294967299], "Array.prototype.fill called on an object with length > 2^32 and a fill range starting before 2^32 but ending after 2^32 is able to fill elements with indices > 2^32");
  341. assert.areEqual(undefined, e[4294967300], "Array.prototype.fill called on an object with length > 2^32 and a fill range starting before 2^32 but ending after 2^32 does not fill elements outside the request range");
  342. }
  343. },
  344. {
  345. name: "Array#lastIndexOf called with an object that has length > 2^32-1",
  346. body: function() {
  347. var e = {length: 4294967301, 4294967291: 1234, 4294967294: 5678, 4294967295: 5678, 4294967296: 5678, 4294967297: 9};
  348. assert.areEqual(4294967291, Array.prototype.lastIndexOf.call(e, 1234, 4294967300), "Array.prototype.lastIndexOf called on an object with length > 2^32 and a fromIndex also > 2^32 finds the element when expected index is < 2^32");
  349. assert.areEqual(4294967296, Array.prototype.lastIndexOf.call(e, 5678, 4294967300), "Array.prototype.lastIndexOf called on an object with length > 2^32 and a fromIndex also > 2^32 finds the element when expected index is > 2^32");
  350. var e = [1,2,3,4];
  351. assert.areEqual(0, Array.prototype.lastIndexOf.call(e, 1, 4294967296), "Array.prototype.lastIndexOf is able to find the element when it exists at index 0 when fromIndex > 2^32");
  352. assert.areEqual(0, Array.prototype.lastIndexOf.call(e, 1), "Array.prototype.lastIndexOf is able to find the element when it exists at index 0 when fromIndex not defined");
  353. }
  354. },
  355. {
  356. name: "Array#copyWithin called with an object that has length > 2^32-1 and parameters also > 2^32-1",
  357. body: function() {
  358. var e = {length: 4294967301, 4294967292: 4294967292, 4294967293: 4294967293, 4294967294: 4294967294};
  359. Array.prototype.copyWithin.call(e, 5, 4294967292, 4294967294);
  360. assert.areEqual(4294967292, e[5], "Array.prototype.copyWithin called on an object with length > 2^32 and a source and destination range completely < 2^32");
  361. assert.areEqual(4294967293, e[6], "Array.prototype.copyWithin called on an object with length > 2^32 and a source and destination range completely < 2^32");
  362. var e = {length: 4294967304, 4294967300: 4294967300, 4294967301: 4294967301, 4294967302: 4294967302, 4294967303: 4294967303};
  363. Array.prototype.copyWithin.call(e, 4294967297, 4294967300, 4294967303);
  364. assert.areEqual(4294967300, e[4294967297], "Array.prototype.copyWithin called on an object with length > 2^32 and a source and destination range completely > 2^32");
  365. assert.areEqual(4294967301, e[4294967298], "Array.prototype.copyWithin called on an object with length > 2^32 and a source and destination range completely > 2^32");
  366. assert.areEqual(4294967302, e[4294967299], "Array.prototype.copyWithin called on an object with length > 2^32 and a source and destination range completely > 2^32");
  367. var e = {length: 4294967301, 4294967297: 4294967297, 4294967298: 4294967298, 4294967299: 4294967299};
  368. Array.prototype.copyWithin.call(e, 5, 4294967297, 4294967300);
  369. assert.areEqual(4294967297, e[5], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range completely < 2^32");
  370. assert.areEqual(4294967298, e[6], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range completely < 2^32");
  371. assert.areEqual(4294967299, e[7], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range completely < 2^32");
  372. var e = {length: 4294967301, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4};
  373. Array.prototype.copyWithin.call(e, 4294967297, 0, 5);
  374. assert.areEqual(0, e[4294967297], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range completely > 2^32");
  375. assert.areEqual(1, e[4294967298], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range completely > 2^32");
  376. assert.areEqual(2, e[4294967299], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range completely > 2^32");
  377. assert.areEqual(3, e[4294967300], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range completely > 2^32");
  378. var e = {length: 4294967301, 4294967292: 4294967292, 4294967293: 4294967293, 4294967294: 4294967294, 4294967295: 4294967295, 4294967296: 4294967296, 4294967297: 4294967297, 4294967298: 4294967298, 4294967299: 4294967299, 4294967300: 4294967300};
  379. Array.prototype.copyWithin.call(e, 5, 4294967292, 4294967301);
  380. assert.areEqual(4294967292, e[5], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  381. assert.areEqual(4294967293, e[6], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  382. assert.areEqual(4294967294, e[7], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  383. assert.areEqual(4294967295, e[8], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  384. assert.areEqual(4294967296, e[9], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  385. assert.areEqual(4294967297, e[10], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  386. assert.areEqual(4294967298, e[11], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  387. assert.areEqual(4294967299, e[12], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  388. assert.areEqual(4294967300, e[13], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely < 2^32");
  389. var e = {length: 4294967400, 4294967292: 4294967292, 4294967293: 4294967293, 4294967294: 4294967294, 4294967295: 4294967295, 4294967296: 4294967296, 4294967297: 4294967297, 4294967298: 4294967298, 4294967299: 4294967299, 4294967300: 4294967300};
  390. Array.prototype.copyWithin.call(e, 4294967350, 4294967292, 4294967301);
  391. assert.areEqual(4294967292, e[4294967350], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  392. assert.areEqual(4294967293, e[4294967351], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  393. assert.areEqual(4294967294, e[4294967352], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  394. assert.areEqual(4294967295, e[4294967353], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  395. assert.areEqual(4294967296, e[4294967354], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  396. assert.areEqual(4294967297, e[4294967355], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  397. assert.areEqual(4294967298, e[4294967356], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  398. assert.areEqual(4294967299, e[4294967357], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  399. assert.areEqual(4294967300, e[4294967358], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range crossing 2^32 and destination range completely > 2^32");
  400. var e = {length: 4294967301, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4};
  401. Array.prototype.copyWithin.call(e, 4294967293, 0, 5);
  402. assert.areEqual(0, e[4294967293], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range crossing 2^32");
  403. assert.areEqual(1, e[4294967294], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range crossing 2^32");
  404. assert.areEqual(2, e[4294967295], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range crossing 2^32");
  405. assert.areEqual(3, e[4294967296], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range crossing 2^32");
  406. assert.areEqual(4, e[4294967297], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely < 2^32 and destination range crossing 2^32");
  407. var e = {length: 4294967420, 4294967400: 4294967400, 4294967401: 4294967401, 4294967402: 4294967402, 4294967403: 4294967403, 4294967404: 4294967404};
  408. Array.prototype.copyWithin.call(e, 4294967293, 4294967400, 4294967405);
  409. assert.areEqual(4294967400, e[4294967293], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range crossing 2^32");
  410. assert.areEqual(4294967401, e[4294967294], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range crossing 2^32");
  411. assert.areEqual(4294967402, e[4294967295], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range crossing 2^32");
  412. assert.areEqual(4294967403, e[4294967296], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range crossing 2^32");
  413. assert.areEqual(4294967404, e[4294967297], "Array.prototype.copyWithin called on an object with length > 2^32 and a source range completely > 2^32 and destination range crossing 2^32");
  414. }
  415. },
  416. {
  417. name: "Array#lastIndexOf called with a fromIndex < 0 && abs(fromIndex) > length (OS #1185913)",
  418. body: function() {
  419. var a = [0, 1];
  420. var r = a.lastIndexOf(0, -3);
  421. assert.areEqual(-1, r, "Array.prototype.lastIndexOf returns -1 when the search element is not found");
  422. }
  423. },
  424. {
  425. name: "Array.of() called with the a bound function without a constructor should not throw an exception",
  426. body: function () {
  427. var val = Math.cos.bind(Math);
  428. assert.isTrue(Array.isArray(Array.of.call(val)));
  429. }
  430. },
  431. {
  432. name: "Array.of() should not invoke setter",
  433. body: function () {
  434. function Bag() {}
  435. Bag.of = Array.of;
  436. Object.defineProperty(Bag.prototype, "0", {set: function(v) { /* no-op */ }});
  437. assert.areEqual(1, Bag.of(1)[0]);
  438. }
  439. },
  440. {
  441. name: "Array.from() should not invoke setter in iterable case",
  442. body: function () {
  443. function Bag() {}
  444. Bag.from = Array.from;
  445. Object.defineProperty(Bag.prototype, "0", {set: function(v) { throw "Fail"; }});
  446. var a = [1,2,3];
  447. assert.areEqual(1, Bag.from(a)[0]);
  448. }
  449. },
  450. {
  451. name: "Array.from() should not invoke setter in array like case",
  452. body: function () {
  453. function Bag() {}
  454. Bag.from = Array.from;
  455. Object.defineProperty(Bag.prototype, "0", {set: function(v) { throw "Fail"; }});
  456. var a = {};
  457. a[0] = 1;
  458. a[1] = 2;
  459. a[2] = 3;
  460. a.length = 3;
  461. assert.areEqual(1, Bag.from(a)[0]);
  462. }
  463. },
  464. {
  465. name: "Array.filter() should not invoke setter even with substituted constructor",
  466. body: function () {
  467. var a = [1,2,3];
  468. a.constructor = function()
  469. {
  470. function Bag() {};
  471. Object.defineProperty(Bag.prototype, "0", { set: function(v){ throw "Fail"; } });
  472. return new Bag();
  473. };
  474. assert.areEqual(1, a.filter(function(v){return v % 2 == 1;})[0]);
  475. }
  476. },
  477. {
  478. name: "Array.map() should not invoke setter even with substituted constructor",
  479. body: function () {
  480. var a = [1,2,3];
  481. a.constructor = function()
  482. {
  483. function Bag() {};
  484. Object.defineProperty(Bag.prototype, "0", { set: function(v){ throw "Fail"; } });
  485. return new Bag();
  486. };
  487. assert.areEqual(1, a.map(function(v){return v % 2;})[0]);
  488. }
  489. },
  490. {
  491. name: "Array.slice() should not invoke setter even with substituted constructor",
  492. body: function () {
  493. var a = [1,2,3];
  494. a.constructor = function()
  495. {
  496. function Bag() {};
  497. Object.defineProperty(Bag.prototype, "0", { set: function(v){ throw "Fail"; } });
  498. return new Bag();
  499. };
  500. assert.areEqual(2, a.slice(1, 3)[0]);
  501. }
  502. },
  503. {
  504. name: "Array.splice() should not invoke setter even with substituted constructor",
  505. body: function () {
  506. var a = [1,2,3];
  507. a.constructor = function()
  508. {
  509. function Bag() {};
  510. Object.defineProperty(Bag.prototype, "0", { set: function(v){ throw "Fail"; } });
  511. return new Bag();
  512. };
  513. assert.areEqual(1, a.splice(0, 1, 'x')[0]);
  514. }
  515. },
  516. {
  517. name: "Array.fill() should throw when applied on frozen array",
  518. body: function () {
  519. var x = [0];
  520. Object.freeze(x);
  521. assert.throws(function() { Array.prototype.fill.call(x) }, TypeError, "We should get a TypeError when fill is applied to a frozen array");
  522. }
  523. },
  524. {
  525. name: "Array.copyWithin() should throw when applied on frozen array",
  526. body: function () {
  527. var x = [1,2,3,4,5];
  528. Object.freeze(x);
  529. assert.throws(function() { Array.prototype.fill.copyWithin(x, 1, 2) }, TypeError, "We should get a TypeError when fill is applied to a frozen array");
  530. }
  531. },
  532. {
  533. name: "Array.concat() should always box the first item",
  534. body: function () {
  535. assert.isTrue(typeof Array.prototype.concat.call(101)[0] === "object");
  536. }
  537. },
  538. {
  539. name: "Boolean primitive should never be considered concat spreadable",
  540. body: function () {
  541. try
  542. {
  543. Boolean.prototype[Symbol.isConcatSpreadable] = true;
  544. Boolean.prototype[0] = 1;
  545. Boolean.prototype[1] = 2;
  546. Boolean.prototype[2] = 3;
  547. Boolean.prototype.length = 3;
  548. assert.isTrue([].concat(true).length === 1); /** True is added to the array as an literal, not spreaded */
  549. }
  550. finally
  551. {
  552. delete Boolean.prototype[Symbol.isConcatSpreadable];
  553. delete Boolean.prototype[0];
  554. delete Boolean.prototype[1];
  555. delete Boolean.prototype[2];
  556. delete Boolean.prototype.length;
  557. }
  558. }
  559. },
  560. {
  561. name: "String primitive should never be considered concat spreadable",
  562. body: function () {
  563. try
  564. {
  565. String.prototype[Symbol.isConcatSpreadable] = true;
  566. String.prototype[0] = 1;
  567. String.prototype[1] = 2;
  568. String.prototype[2] = 3;
  569. String.prototype.length = 3;
  570. assert.isTrue([].concat("Hello").length === 1); /** True is added to the array as an literal, not spreaded */
  571. }
  572. finally
  573. {
  574. delete String.prototype[Symbol.isConcatSpreadable];
  575. delete String.prototype[0];
  576. delete String.prototype[1];
  577. delete String.prototype[2];
  578. delete String.prototype.length;
  579. }
  580. }
  581. }
  582. ];
  583. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });