Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-arrays | |
| 6 | |
| 7 assertEquals(Array.prototype.copyWithin.length, 2); | |
| 8 | |
| 9 // works with two arguments | |
| 10 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3)); | |
| 11 assertArrayEquals([1, 4, 5, 4, 5], [1, 2, 3, 4, 5].copyWithin(1, 3)); | |
| 12 assertArrayEquals([1, 3, 4, 5, 5], [1, 2, 3, 4, 5].copyWithin(1, 2)); | |
| 13 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(2, 2)); | |
| 14 | |
| 15 // works with three arguments | |
| 16 assertArrayEquals([1, 2, 3, 4, 5].copyWithin(0, 3, 4), [4, 2, 3, 4, 5]); | |
| 17 assertArrayEquals([1, 2, 3, 4, 5].copyWithin(1, 3, 4), [1, 4, 3, 4, 5]); | |
| 18 assertArrayEquals([1, 2, 3, 4, 5].copyWithin(1, 2, 4), [1, 3, 4, 4, 5]); | |
| 19 | |
| 20 // works with negative arguments | |
| 21 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, -2)); | |
| 22 assertArrayEquals([4, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, -2, -1)); | |
| 23 assertArrayEquals([1, 3, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3, -2)); | |
| 24 assertArrayEquals([1, 3, 4, 4, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3, -1)); | |
| 25 assertArrayEquals([1, 3, 4, 5, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3)); | |
| 26 | |
| 27 var args = (function () { return Array.prototype.slice.call(arguments); }(1, 2, 3)); | |
| 28 var argsClass = Object.prototype.toString.call(args); | |
| 29 assertArrayEquals([1, 2, 3], args); | |
| 30 Array.prototype.copyWithin.call(args, -2, 0); | |
| 31 assertArrayEquals([1, 1, 2], args); | |
| 32 assertArrayEquals(argsClass, Object.prototype.toString.call(args)); | |
| 33 | |
| 34 // throws on null/undefined values | |
| 35 assertThrows('Array.prototype.copyWithin.call(null, 0, 3);', TypeError); | |
| 36 | |
| 37 assertThrows('Array.prototype.copyWithin.call(undefined, 0, 3);', TypeError); | |
| 38 | |
| 39 // test with this value as string | |
| 40 // FIXME: Should this throw? | |
| 41 // assertThrows('Array.prototype.copyWithin.call("hello world", 0, 3);', TypeErr or); | |
|
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
| |
| 42 | |
| 43 // test with this value as number | |
| 44 assertArrayEquals(new Number(34), Array.prototype.copyWithin.call(34, 0, 3)); | |
| 45 | |
| 46 // test with this value as TypedArray | |
| 47 var buffer = new ArrayBuffer(16); | |
| 48 var int32View = new Int32Array(buffer); | |
| 49 for (var i=0; i<int32View.length; i++) { | |
| 50 int32View[i] = i*2; | |
| 51 } | |
| 52 assertArrayEquals(new Int32Array([2, 4, 6, 6]), Array.prototype.copyWithin.call( int32View, 0, 1)); | |
| 53 | |
| 54 // if arguments object is sloppy, copyWithin must move the arguments around | |
| 55 function f(a, b, c, d, e) { | |
| 56 [].copyWithin.call(arguments, 1, 3); | |
| 57 return [a, b, c, d, e]; | |
| 58 } | |
| 59 assertArrayEquals([1, 4, 5, 4, 5], f(1, 2, 3, 4, 5)); | |
| 60 | |
| 61 // test with target > start on 2 arguments | |
| 62 assertArrayEquals([1, 2, 3, 1, 2], [1, 2, 3, 4, 5].copyWithin(3, 0)); | |
| 63 | |
| 64 // test with target > start on 3 arguments | |
| 65 assertArrayEquals([1, 2, 3, 1, 2], [1, 2, 3, 4, 5].copyWithin(3, 0, 4)); | |
| 66 | |
| 67 // test on array with holes | |
| 68 var arr = new Array(6); | |
| 69 for (var i = 0; i < arr.length; i += 2) { | |
| 70 arr[i] = i; | |
| 71 } | |
| 72 assertArrayEquals([, 4, , , 4, , ], arr.copyWithin(0, 3)); | |
| 73 | |
| 74 // test on fractional arguments | |
| 75 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0.2, 3.9)); | |
| 76 | |
| 77 // test with -0 | |
| 78 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-0, 3)); | |
| 79 | |
| 80 // test with arguments more than this.length | |
| 81 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 7)); | |
| 82 | |
| 83 // test with arguments less than -this.length | |
| 84 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-7, 0)); | |
| 85 | |
| 86 // test with arguments equal to -this.length | |
| 87 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-5, 0)); | |
| 88 | |
| 89 // test on empty array | |
| 90 assertArrayEquals([], [].copyWithin(0, 3)); | |
| 91 | |
| 92 // test with target range being shorter than end - start | |
| 93 assertArrayEquals([1, 2, 2, 3, 4], [1, 2, 3, 4, 5].copyWithin(2, 1, 4)); | |
| 94 | |
| 95 // test overlapping ranges | |
| 96 arr = [1, 2, 3, 4, 5]; | |
| 97 arr.copyWithin(2, 1, 4); | |
| 98 assertArrayEquals([1, 2, 2, 2, 3], arr.copyWithin(2, 1, 4)); | |
| 99 | |
| 100 // 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
| |
| 101 arr = [1, , 3, , 4, 5]; | |
| 102 Object.freeze(arr); | |
| 103 assertThrows('arr.copyWithin(2, 1, 4);', TypeError); | |
| 104 | |
| 105 // test with a proxy object | |
| 106 var proxyObj = { | |
| 107 get: function(recipient, name) { | |
| 108 return recipient[name] + 2; | |
| 109 } | |
| 110 }; | |
| 111 | |
| 112 // 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
| |
| 113 // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-proxy-constructor-f unction | |
| 114 // var p = new Proxy([1, 2, 3, 4, 5], proxyObj); | |
| 115 // assertArrayEquals(Array.prototype.copyWithin.call(p, 0, 3), [6, 7, , , 5]); | |
| 116 | |
| 117 // test if we throw in between | |
| 118 arr = [1, 2, 3, 4, 5]; | |
| 119 Object.defineProperty(arr, 1, { | |
| 120 set: function () { | |
| 121 throw new Error("Boom!"); | |
| 122 } | |
| 123 }); | |
| 124 assertThrows('arr.copyWithin(1, 3);', Error); | |
| 125 assertArrayEquals([1, undefined, 3, 4, 5], arr); | |
| 126 | |
| 127 // undefined as third argument | |
| 128 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3, undefined)); | |
| 129 | |
| 130 // test that this.length is called only once | |
| 131 arr = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5}; | |
| 132 var count = 0; | |
| 133 Object.defineProperty(arr, "length", { | |
| 134 get: function () { | |
| 135 count++; | |
| 136 } | |
| 137 }); | |
| 138 Array.prototype.copyWithin.call(arr, 1, 3); | |
| 139 assertEquals(1, count); | |
| 140 | |
| 141 count = 0; | |
| 142 Array.prototype.copyWithin.call(arr, 1, 3, 4); | |
| 143 assertEquals(1, count); | |
| 144 | |
| 145 var large = 10000; | |
| 146 | |
| 147 // test on a large array | |
| 148 arr = new Array(large); | |
| 149 assertArrayEquals(arr, arr.copyWithin(45, 900)); | |
| 150 | |
| 151 // test on floating point numbers | |
| 152 for (var i = 0; i < large; i++) { | |
| 153 arr[i] = Math.random(); | |
| 154 } | |
| 155 arr.copyWithin(45, 900); | |
| 156 | |
| 157 // test on array of objects | |
| 158 for (var i = 0; i < large; i++) { | |
| 159 arr[i] = { num: Math.random() }; | |
| 160 } | |
| 161 arr.copyWithin(45, 900); | |
| 162 | |
| 163 // test array length remains same | |
| 164 assertEquals(large, arr.length); | |
| 165 | |
| 166 // test null on third argument is handled correctly | |
| 167 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3, null)); | |
| 168 | |
| 169 // tamper the global Object prototype and test this works | |
| 170 Object.prototype[2] = 1; | |
| 171 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
| |
| OLD | NEW |