Chromium Code Reviews| 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..f9143e17c4d788375b792ae1352b022b15d99de5 |
| --- /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); |
|
adamk
2015/04/17 22:34:17
This throws because the indices are not writable.
caitp (gmail)
2015/04/17 22:36:50
I think these tests are all wrong, since a target
caitp (gmail)
2015/04/18 04:50:30
I apparently forgot how copyWithin() worked when I
|
| + |
| +// 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 |
|
adamk
2015/04/17 22:34:17
Also want to check that Set() is strict (your orig
caitp (gmail)
2015/04/18 04:50:30
Done, with some various [[Extensible]]-related tes
|
| +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 |
|
adamk
2015/04/17 22:34:17
You can probably test this with our existing imple
caitp (gmail)
2015/04/18 04:50:30
IMO, better to just get rid of the test for now. e
|
| +// 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)); |
|
adamk
2015/04/17 22:34:17
I think this test is probably more interesting if
|