Index: test/mjsunit/harmony/array-copywithin.js |
diff --git a/test/mjsunit/harmony/array-copywithin.js b/test/mjsunit/harmony/array-copywithin.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5adbde437ba5f77d25ccea2dadde403bbb5ff40a |
--- /dev/null |
+++ b/test/mjsunit/harmony/array-copywithin.js |
@@ -0,0 +1,171 @@ |
+// Copyright 2014 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Flags: --harmony-arrays |
+ |
+assertEquals(Array.prototype.copyWithin.length, 2); |
+ |
+// works with two arguments |
+assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3)); |
+assertArrayEquals([1, 4, 5, 4, 5], [1, 2, 3, 4, 5].copyWithin(1, 3)); |
+assertArrayEquals([1, 3, 4, 5, 5], [1, 2, 3, 4, 5].copyWithin(1, 2)); |
+assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(2, 2)); |
+ |
+// works with three arguments |
+assertArrayEquals([1, 2, 3, 4, 5].copyWithin(0, 3, 4), [4, 2, 3, 4, 5]); |
+assertArrayEquals([1, 2, 3, 4, 5].copyWithin(1, 3, 4), [1, 4, 3, 4, 5]); |
+assertArrayEquals([1, 2, 3, 4, 5].copyWithin(1, 2, 4), [1, 3, 4, 4, 5]); |
+ |
+// works with negative arguments |
+assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, -2)); |
+assertArrayEquals([4, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, -2, -1)); |
+assertArrayEquals([1, 3, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3, -2)); |
+assertArrayEquals([1, 3, 4, 4, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3, -1)); |
+assertArrayEquals([1, 3, 4, 5, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3)); |
+ |
+var args = (function () { return Array.prototype.slice.call(arguments); }(1, 2, 3)); |
+var argsClass = Object.prototype.toString.call(args); |
+assertArrayEquals([1, 2, 3], args); |
+Array.prototype.copyWithin.call(args, -2, 0); |
+assertArrayEquals([1, 1, 2], args); |
+assertArrayEquals(argsClass, Object.prototype.toString.call(args)); |
+ |
+// throws on null/undefined values |
+assertThrows('Array.prototype.copyWithin.call(null, 0, 3);', TypeError); |
+ |
+assertThrows('Array.prototype.copyWithin.call(undefined, 0, 3);', TypeError); |
+ |
+// test with this value as string |
+// FIXME: Should this throw? |
+// assertThrows('Array.prototype.copyWithin.call("hello world", 0, 3);', TypeError); |
+ |
+// test with this value as number |
+assertArrayEquals(new Number(34), Array.prototype.copyWithin.call(34, 0, 3)); |
+ |
+// test with this value as TypedArray |
+var buffer = new ArrayBuffer(16); |
+var int32View = new Int32Array(buffer); |
+for (var i=0; i<int32View.length; i++) { |
+ int32View[i] = i*2; |
+} |
+assertArrayEquals(new Int32Array([2, 4, 6, 6]), Array.prototype.copyWithin.call(int32View, 0, 1)); |
+ |
+// if arguments object is sloppy, copyWithin must move the arguments around |
+function f(a, b, c, d, e) { |
+ [].copyWithin.call(arguments, 1, 3); |
+ return [a, b, c, d, e]; |
+} |
+assertArrayEquals([1, 4, 5, 4, 5], f(1, 2, 3, 4, 5)); |
+ |
+// test with target > start on 2 arguments |
+assertArrayEquals([1, 2, 3, 1, 2], [1, 2, 3, 4, 5].copyWithin(3, 0)); |
+ |
+// test with target > start on 3 arguments |
+assertArrayEquals([1, 2, 3, 1, 2], [1, 2, 3, 4, 5].copyWithin(3, 0, 4)); |
+ |
+// test on array with holes |
+var arr = new Array(6); |
+for (var i = 0; i < arr.length; i += 2) { |
+ arr[i] = i; |
+} |
+assertArrayEquals([, 4, , , 4, , ], arr.copyWithin(0, 3)); |
+ |
+// test on fractional arguments |
+assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0.2, 3.9)); |
+ |
+// test with -0 |
+assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-0, 3)); |
+ |
+// test with arguments more than this.length |
+assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 7)); |
+ |
+// test with arguments less than -this.length |
+assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-7, 0)); |
+ |
+// test with arguments equal to -this.length |
+assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-5, 0)); |
+ |
+// test on empty array |
+assertArrayEquals([], [].copyWithin(0, 3)); |
+ |
+// test with target range being shorter than end - start |
+assertArrayEquals([1, 2, 2, 3, 4], [1, 2, 3, 4, 5].copyWithin(2, 1, 4)); |
+ |
+// test overlapping ranges |
+arr = [1, 2, 3, 4, 5]; |
+arr.copyWithin(2, 1, 4); |
+assertArrayEquals([1, 2, 2, 2, 3], arr.copyWithin(2, 1, 4)); |
+ |
+// check that delete is strict |
+arr = [1, , 3, , 4, 5]; |
+Object.freeze(arr); |
+assertThrows('arr.copyWithin(2, 1, 4);', TypeError); |
+ |
+// test with a proxy object |
+var proxyObj = { |
+ get: function(recipient, name) { |
+ return recipient[name] + 2; |
+ } |
+}; |
+ |
+// FIXME: Support Proxies |
+// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-proxy-constructor-function |
+// var p = new Proxy([1, 2, 3, 4, 5], proxyObj); |
+// assertArrayEquals(Array.prototype.copyWithin.call(p, 0, 3), [6, 7, , , 5]); |
+ |
+// test if we throw in between |
+arr = [1, 2, 3, 4, 5]; |
+Object.defineProperty(arr, 1, { |
+ set: function () { |
+ throw new Error("Boom!"); |
+ } |
+}); |
+assertThrows('arr.copyWithin(1, 3);', Error); |
+assertArrayEquals([1, undefined, 3, 4, 5], arr); |
+ |
+// undefined as third argument |
+assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3, undefined)); |
+ |
+// test that this.length is called only once |
+arr = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5}; |
+var count = 0; |
+Object.defineProperty(arr, "length", { |
+ get: function () { |
+ count++; |
+ } |
+}); |
+Array.prototype.copyWithin.call(arr, 1, 3); |
+assertEquals(1, count); |
+ |
+count = 0; |
+Array.prototype.copyWithin.call(arr, 1, 3, 4); |
+assertEquals(1, count); |
+ |
+var large = 10000; |
+ |
+// test on a large array |
+arr = new Array(large); |
+assertArrayEquals(arr, arr.copyWithin(45, 900)); |
+ |
+// test on floating point numbers |
+for (var i = 0; i < large; i++) { |
+ arr[i] = Math.random(); |
+} |
+arr.copyWithin(45, 900); |
+ |
+// test on array of objects |
+for (var i = 0; i < large; i++) { |
+ arr[i] = { num: Math.random() }; |
+} |
+arr.copyWithin(45, 900); |
+ |
+// test array length remains same |
+assertEquals(large, arr.length); |
+ |
+// test null on third argument is handled correctly |
+assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3, null)); |
+ |
+// tamper the global Object prototype and test this works |
+Object.prototype[2] = 1; |
+assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3)); |