| 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 (function copyWithinArity() { | |
| 8 assertEquals(Array.prototype.copyWithin.length, 2); | |
| 9 })(); | |
| 10 | |
| 11 | |
| 12 (function copyWithinTargetAndStart() { | |
| 13 // works with two arguemnts | |
| 14 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3)); | |
| 15 assertArrayEquals([1, 4, 5, 4, 5], [1, 2, 3, 4, 5].copyWithin(1, 3)); | |
| 16 assertArrayEquals([1, 3, 4, 5, 5], [1, 2, 3, 4, 5].copyWithin(1, 2)); | |
| 17 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(2, 2)); | |
| 18 })(); | |
| 19 | |
| 20 | |
| 21 (function copyWithinTargetStartAndEnd() { | |
| 22 // works with three arguments | |
| 23 assertArrayEquals([1, 2, 3, 4, 5].copyWithin(0, 3, 4), [4, 2, 3, 4, 5]); | |
| 24 assertArrayEquals([1, 2, 3, 4, 5].copyWithin(1, 3, 4), [1, 4, 3, 4, 5]); | |
| 25 assertArrayEquals([1, 2, 3, 4, 5].copyWithin(1, 2, 4), [1, 3, 4, 4, 5]); | |
| 26 })(); | |
| 27 | |
| 28 | |
| 29 (function copyWithinNegativeRelativeOffsets() { | |
| 30 // works with negative arguments | |
| 31 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, -2)); | |
| 32 assertArrayEquals([4, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, -2, -1)); | |
| 33 assertArrayEquals([1, 3, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3, -2)); | |
| 34 assertArrayEquals([1, 3, 4, 4, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3, -1)); | |
| 35 assertArrayEquals([1, 3, 4, 5, 5], [1, 2, 3, 4, 5].copyWithin(-4, -3)); | |
| 36 // test with arguments equal to -this.length | |
| 37 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-5, 0)); | |
| 38 })(); | |
| 39 | |
| 40 | |
| 41 (function copyWithinArrayLikeValues() { | |
| 42 // works with array-like values | |
| 43 var args = (function () { return arguments; }(1, 2, 3)); | |
| 44 Array.prototype.copyWithin.call(args, -2, 0); | |
| 45 assertArrayEquals([1, 1, 2], Array.prototype.slice.call(args)); | |
| 46 | |
| 47 // [[Class]] does not change | |
| 48 assertArrayEquals("[object Arguments]", Object.prototype.toString.call(args)); | |
| 49 })(); | |
| 50 | |
| 51 | |
| 52 (function copyWithinNullThis() { | |
| 53 // throws on null/undefined values | |
| 54 assertThrows(function() { | |
| 55 return Array.prototype.copyWithin.call(null, 0, 3); | |
| 56 }, TypeError); | |
| 57 })(); | |
| 58 | |
| 59 | |
| 60 (function copyWithinUndefinedThis() { | |
| 61 assertThrows(function() { | |
| 62 return Array.prototype.copyWithin.call(undefined, 0, 3); | |
| 63 }, TypeError); | |
| 64 })(); | |
| 65 | |
| 66 | |
| 67 // TODO(caitp): indexed properties of String are read-only and setting them | |
| 68 // should throw in strict mode. See bug v8:4042 | |
| 69 // (function copyWithinStringThis() { | |
| 70 // // test with this value as string | |
| 71 // assertThrows(function() { | |
| 72 // return Array.prototype.copyWithin.call("hello world", 0, 3); | |
| 73 // }, TypeError); | |
| 74 // })(); | |
| 75 | |
| 76 | |
| 77 (function copyWithinNumberThis() { | |
| 78 // test with this value as number | |
| 79 assertEquals(34, Array.prototype.copyWithin.call(34, 0, 3).valueOf()); | |
| 80 })(); | |
| 81 | |
| 82 | |
| 83 (function copyWithinSymbolThis() { | |
| 84 // test with this value as number | |
| 85 var sym = Symbol("test"); | |
| 86 assertEquals(sym, Array.prototype.copyWithin.call(sym, 0, 3).valueOf()); | |
| 87 })(); | |
| 88 | |
| 89 | |
| 90 (function copyyWithinTypedArray() { | |
| 91 // test with this value as TypedArray | |
| 92 var buffer = new ArrayBuffer(16); | |
| 93 var int32View = new Int32Array(buffer); | |
| 94 for (var i=0; i<int32View.length; i++) { | |
| 95 int32View[i] = i*2; | |
| 96 } | |
| 97 assertArrayEquals(new Int32Array([2, 4, 6, 6]), | |
| 98 Array.prototype.copyWithin.call(int32View, 0, 1)); | |
| 99 })(); | |
| 100 | |
| 101 | |
| 102 (function copyWithinSloppyArguments() { | |
| 103 // if arguments object is sloppy, copyWithin must move the arguments around | |
| 104 function f(a, b, c, d, e) { | |
| 105 [].copyWithin.call(arguments, 1, 3); | |
| 106 return [a, b, c, d, e]; | |
| 107 } | |
| 108 assertArrayEquals([1, 4, 5, 4, 5], f(1, 2, 3, 4, 5)); | |
| 109 })(); | |
| 110 | |
| 111 | |
| 112 (function copyWithinStartLessThanTarget() { | |
| 113 // test with target > start on 2 arguments | |
| 114 assertArrayEquals([1, 2, 3, 1, 2], [1, 2, 3, 4, 5].copyWithin(3, 0)); | |
| 115 | |
| 116 // test with target > start on 3 arguments | |
| 117 assertArrayEquals([1, 2, 3, 1, 2], [1, 2, 3, 4, 5].copyWithin(3, 0, 4)); | |
| 118 })(); | |
| 119 | |
| 120 | |
| 121 (function copyWithinArrayWithHoles() { | |
| 122 // test on array with holes | |
| 123 var arr = new Array(6); | |
| 124 for (var i = 0; i < arr.length; i += 2) { | |
| 125 arr[i] = i; | |
| 126 } | |
| 127 assertArrayEquals([, 4, , , 4, , ], arr.copyWithin(0, 3)); | |
| 128 })(); | |
| 129 | |
| 130 | |
| 131 (function copyWithinArrayLikeWithHoles() { | |
| 132 // test on array-like object with holes | |
| 133 assertArrayEquals({ | |
| 134 length: 6, | |
| 135 1: 4, | |
| 136 4: 4 | |
| 137 }, Array.prototype.copyWithin.call({ | |
| 138 length: 6, | |
| 139 0: 0, | |
| 140 2: 2, | |
| 141 4: 4 | |
| 142 }, 0, 3)); | |
| 143 })(); | |
| 144 | |
| 145 | |
| 146 (function copyWithinNonIntegerRelativeOffsets() { | |
| 147 // test on fractional arguments | |
| 148 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0.2, 3.9)); | |
| 149 })(); | |
| 150 | |
| 151 | |
| 152 (function copyWithinNegativeZeroTarget() { | |
| 153 // test with -0 | |
| 154 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-0, 3)); | |
| 155 })(); | |
| 156 | |
| 157 | |
| 158 (function copyWithinTargetOutsideStart() { | |
| 159 // test with arguments more than this.length | |
| 160 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 7)); | |
| 161 | |
| 162 // test with arguments less than -this.length | |
| 163 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(-7, 0)); | |
| 164 })(); | |
| 165 | |
| 166 | |
| 167 (function copyWithinEmptyArray() { | |
| 168 // test on empty array | |
| 169 assertArrayEquals([], [].copyWithin(0, 3)); | |
| 170 })(); | |
| 171 | |
| 172 | |
| 173 (function copyWithinTargetCutOff() { | |
| 174 // test with target range being shorter than end - start | |
| 175 assertArrayEquals([1, 2, 2, 3, 4], [1, 2, 3, 4, 5].copyWithin(2, 1, 4)); | |
| 176 })(); | |
| 177 | |
| 178 | |
| 179 (function copyWithinOverlappingRanges() { | |
| 180 // test overlapping ranges | |
| 181 var arr = [1, 2, 3, 4, 5]; | |
| 182 arr.copyWithin(2, 1, 4); | |
| 183 assertArrayEquals([1, 2, 2, 2, 3], arr.copyWithin(2, 1, 4)); | |
| 184 })(); | |
| 185 | |
| 186 | |
| 187 (function copyWithinStrictDelete() { | |
| 188 // check that [[Delete]] is strict (non-extensible via freeze) | |
| 189 assertThrows(function() { | |
| 190 return Object.freeze([1, , 3, , 4, 5]).copyWithin(2, 1, 4); | |
| 191 }, TypeError); | |
| 192 | |
| 193 // check that [[Delete]] is strict (non-extensible via seal) | |
| 194 assertThrows(function() { | |
| 195 return Object.seal([1, , 3, , 4, 5]).copyWithin(2, 1, 4); | |
| 196 }, TypeError); | |
| 197 | |
| 198 // check that [[Delete]] is strict (non-extensible via preventExtensions) | |
| 199 assertThrows(function() { | |
| 200 return Object.preventExtensions([1, , 3, , 4, 5]).copyWithin(2, 1, 4); | |
| 201 }, TypeError); | |
| 202 })(); | |
| 203 | |
| 204 | |
| 205 (function copyWithinStrictSet() { | |
| 206 // check that [[Set]] is strict (non-extensible via freeze) | |
| 207 assertThrows(function() { | |
| 208 return Object.freeze([1, 2, 3, 4, 5]).copyWithin(0, 3); | |
| 209 }, TypeError); | |
| 210 | |
| 211 // check that [[Set]] is strict (non-extensible via seal) | |
| 212 assertThrows(function() { | |
| 213 return Object.seal([, 2, 3, 4, 5]).copyWithin(0, 3); | |
| 214 }, TypeError); | |
| 215 | |
| 216 // check that [[Set]] is strict (non-extensible via preventExtensions) | |
| 217 assertThrows(function() { | |
| 218 return Object.preventExtensions([ , 2, 3, 4, 5]).copyWithin(0, 3); | |
| 219 }, TypeError); | |
| 220 })(); | |
| 221 | |
| 222 | |
| 223 (function copyWithinSetterThrows() { | |
| 224 function Boom() {} | |
| 225 // test if we throw in between | |
| 226 var arr = Object.defineProperty([1, 2, 3, 4, 5], 1, { | |
| 227 set: function () { | |
| 228 throw new Boom(); | |
| 229 } | |
| 230 }); | |
| 231 | |
| 232 assertThrows(function() { | |
| 233 return arr.copyWithin(1, 3); | |
| 234 }, Boom); | |
| 235 | |
| 236 assertArrayEquals([1, , 3, 4, 5], arr); | |
| 237 })(); | |
| 238 | |
| 239 | |
| 240 (function copyWithinDefaultEnd() { | |
| 241 // undefined as third argument | |
| 242 assertArrayEquals( | |
| 243 [4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3, undefined)); | |
| 244 })(); | |
| 245 | |
| 246 | |
| 247 (function copyWithinGetLengthOnce() { | |
| 248 // test that this.length is called only once | |
| 249 var count = 0; | |
| 250 var arr = Object.defineProperty({ 0: 1, 1: 2, 2: 3, 3: 4, 4: 5 }, "length", { | |
| 251 get: function () { | |
| 252 count++; | |
| 253 return 5; | |
| 254 } | |
| 255 }); | |
| 256 Array.prototype.copyWithin.call(arr, 1, 3); | |
| 257 assertEquals(1, count); | |
| 258 | |
| 259 Array.prototype.copyWithin.call(arr, 1, 3, 4); | |
| 260 assertEquals(2, count); | |
| 261 })(); | |
| 262 | |
| 263 | |
| 264 (function copyWithinLargeArray() { | |
| 265 var large = 10000; | |
| 266 | |
| 267 // test on a large array | |
| 268 var arr = new Array(large); | |
| 269 assertArrayEquals(arr, arr.copyWithin(45, 9000)); | |
| 270 | |
| 271 var expected = new Array(large); | |
| 272 // test on floating point numbers | |
| 273 for (var i = 0; i < large; i++) { | |
| 274 arr[i] = Math.random(); | |
| 275 expected[i] = arr[i]; | |
| 276 if (i >= 9000) { | |
| 277 expected[(i - 9000) + 45] = arr[i]; | |
| 278 } | |
| 279 } | |
| 280 assertArrayEquals(expected, arr.copyWithin(45, 9000)); | |
| 281 | |
| 282 // test on array of objects | |
| 283 for (var i = 0; i < large; i++) { | |
| 284 arr[i] = { num: Math.random() }; | |
| 285 } + 45 | |
| 286 arr.copyWithin(45, 9000); | |
| 287 | |
| 288 // test copied by reference | |
| 289 for (var i = 9000; i < large; ++i) { | |
| 290 assertSame(arr[(i - 9000) + 45], arr[i]); | |
| 291 } | |
| 292 | |
| 293 // test array length remains same | |
| 294 assertEquals(large, arr.length); | |
| 295 })(); | |
| 296 | |
| 297 | |
| 298 (function copyWithinSuperLargeLength() { | |
| 299 // 2^53 - 1 is the maximum value returned from ToLength() | |
| 300 var large = Math.pow(2, 53) - 1; | |
| 301 var object = { length: large }; | |
| 302 | |
| 303 // Initialize last 10 entries | |
| 304 for (var i = 1; i <= 10; ++i) { | |
| 305 object[(large - 11) + i] = { num: i }; | |
| 306 } | |
| 307 | |
| 308 Array.prototype.copyWithin.call(object, 1, large - 10); | |
| 309 | |
| 310 // Test copied values | |
| 311 for (var i = 1; i <= 10; ++i) { | |
| 312 var old_ref = object[(large - 11) + i]; | |
| 313 var new_ref = object[i]; | |
| 314 assertSame(old_ref, new_ref); | |
| 315 assertSame(new_ref.num, i); | |
| 316 } | |
| 317 | |
| 318 // Assert length has not changed | |
| 319 assertEquals(large, object.length); | |
| 320 })(); | |
| 321 | |
| 322 | |
| 323 (function copyWithinNullEnd() { | |
| 324 // test null on third argument is converted to +0 | |
| 325 assertArrayEquals([1, 2, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3, null)); | |
| 326 })(); | |
| 327 | |
| 328 | |
| 329 (function copyWithinElementsInObjectsPrototype() { | |
| 330 // tamper the global Object prototype and test this works | |
| 331 Object.prototype[2] = 1; | |
| 332 assertArrayEquals([4, 5, 3, 4, 5], [1, 2, 3, 4, 5].copyWithin(0, 3)); | |
| 333 delete Object.prototype[2]; | |
| 334 | |
| 335 Object.prototype[3] = "FAKE"; | |
| 336 assertArrayEquals(["FAKE", 5, 3, "FAKE", 5], [1, 2, 3, , 5].copyWithin(0, 3)); | |
| 337 delete Object.prototype[3]; | |
| 338 })(); | |
| OLD | NEW |