|
|
|
|
|
|
1 |
// Bug 1021379 - Rename typed arrays' move method to copyWithin, |
| 2 |
// fix up to ES6 semantics |
| 3 |
// Tests for TypedArray#copyWithin |
| 4 |
|
| 5 |
load(libdir + "asserts.js"); |
| 6 |
|
| 7 |
const constructors = [ |
| 8 |
Int8Array, |
| 9 |
Uint8Array, |
| 10 |
Uint8ClampedArray, |
| 11 |
Int16Array, |
| 12 |
Uint16Array, |
| 13 |
Int32Array, |
| 14 |
Uint32Array, |
| 15 |
Float32Array, |
| 16 |
Float64Array |
| 17 |
]; |
| 18 |
|
| 19 |
for (constructor of constructors) { |
| 20 |
|
| 21 |
assertEq(constructor.prototype.copyWithin.length, 2); |
| 22 |
|
| 23 |
// works with two arguments |
| 24 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0, 3), |
| 25 |
new constructor([4, 5, 3, 4, 5])); |
| 26 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(1, 3), |
| 27 |
new constructor([1, 4, 5, 4, 5])); |
| 28 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(1, 2), |
| 29 |
new constructor([1, 3, 4, 5, 5])); |
| 30 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(2, 2), |
| 31 |
new constructor([1, 2, 3, 4, 5])); |
| 32 |
|
| 33 |
// works with three arguments |
| 34 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0, 3, 4), |
| 35 |
new constructor([4, 2, 3, 4, 5])); |
| 36 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(1, 3, 4), |
| 37 |
new constructor([1, 4, 3, 4, 5])); |
| 38 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(1, 2, 4), |
| 39 |
new constructor([1, 3, 4, 4, 5])); |
| 40 |
|
| 41 |
// works with negative arguments |
| 42 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0, -2), |
| 43 |
new constructor([4, 5, 3, 4, 5])); |
| 44 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0, -2, -1), |
| 45 |
new constructor([4, 2, 3, 4, 5])); |
| 46 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(-4, -3, -2), |
| 47 |
new constructor([1, 3, 3, 4, 5])); |
| 48 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(-4, -3, -1), |
| 49 |
new constructor([1, 3, 4, 4, 5])); |
| 50 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(-4, -3), |
| 51 |
new constructor([1, 3, 4, 5, 5])); |
| 52 |
|
| 53 |
// throws on null/undefined values |
| 54 |
assertThrowsInstanceOf(function() { |
| 55 |
constructor.prototype.copyWithin.call(null, 0, 3); |
| 56 |
}, TypeError, "Assert that copyWithin fails if this value is null"); |
| 57 |
|
| 58 |
assertThrowsInstanceOf(function() { |
| 59 |
constructor.prototype.copyWithin.call(undefined, 0, 3); |
| 60 |
}, TypeError, "Assert that copyWithin fails if this value is undefined"); |
| 61 |
|
| 62 |
// test with this value as string |
| 63 |
assertThrowsInstanceOf(function() { |
| 64 |
constructor.prototype.copyWithin.call("hello world", 0, 3); |
| 65 |
}, TypeError, "Assert that copyWithin fails if this value is string"); |
| 66 |
|
| 67 |
// if arguments object is sloppy, copyWithin must move the arguments around |
| 68 |
function f(a, b, c, d, e) { |
| 69 |
[].copyWithin.call(arguments, 1, 3); |
| 70 |
return new constructor([a, b, c, d, e]); |
| 71 |
} |
| 72 |
assertDeepEq(f(1, 2, 3, 4, 5), (new constructor([1, 4, 5, 4, 5]))); |
| 73 |
|
| 74 |
// test with target > start on 2 arguments |
| 75 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(3, 0), |
| 76 |
(new constructor([1, 2, 3, 1, 2]))); |
| 77 |
|
| 78 |
// test with target > start on 3 arguments |
| 79 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(3, 0, 4), |
| 80 |
(new constructor([1, 2, 3, 1, 2]))); |
| 81 |
|
| 82 |
// test on array with holes |
| 83 |
var arr = new constructor(6); |
| 84 |
for (var i = 0; i < arr.length; i += 2) { |
| 85 |
arr[i] = i; |
| 86 |
} |
| 87 |
assertDeepEq(arr.copyWithin(0, 3), (new constructor([, 4, , , 4, , ]))); |
| 88 |
|
| 89 |
// test on fractional arguments |
| 90 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0.2, 3.9), |
| 91 |
(new constructor([4, 5, 3, 4, 5]))); |
| 92 |
|
| 93 |
// test with -0 |
| 94 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(-0, 3), |
| 95 |
(new constructor([4, 5, 3, 4, 5]))); |
| 96 |
|
| 97 |
// test with arguments more than this.length |
| 98 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0, 7), |
| 99 |
(new constructor([1, 2, 3, 4, 5]))); |
| 100 |
|
| 101 |
// test with arguments less than -this.length |
| 102 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(-7, 0), |
| 103 |
(new constructor([1, 2, 3, 4, 5]))); |
| 104 |
|
| 105 |
// test with arguments equal to -this.length |
| 106 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(-5, 0), |
| 107 |
(new constructor([1, 2, 3, 4, 5]))); |
| 108 |
|
| 109 |
// test on empty constructor |
| 110 |
assertDeepEq((new constructor([])).copyWithin(0, 3), (new constructor([]))); |
| 111 |
|
| 112 |
// test with target range being shorter than end - start |
| 113 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(2, 1, 4), |
| 114 |
(new constructor([1, 2, 2, 3, 4]))); |
| 115 |
|
| 116 |
// test overlapping ranges |
| 117 |
arr = new constructor([1, 2, 3, 4, 5]); |
| 118 |
arr.copyWithin(2, 1, 4); |
| 119 |
assertDeepEq(arr.copyWithin(2, 1, 4), (new constructor([1, 2, 2, 2, 3]))); |
| 120 |
|
| 121 |
// undefined as third argument |
| 122 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0, 3, undefined), |
| 123 |
(new constructor([4, 5, 3, 4, 5]))); |
| 124 |
|
| 125 |
// test that this.length is never called |
| 126 |
arr = new constructor([0, 1, 2, 3, 5]); |
| 127 |
Object.defineProperty(arr, "length", { |
| 128 |
get: function () { throw new Error("length accessor called"); } |
| 129 |
}); |
| 130 |
arr.copyWithin(1, 3); |
| 131 |
|
| 132 |
var large = 10000; |
| 133 |
|
| 134 |
// test on a large constructor |
| 135 |
arr = new constructor(large); |
| 136 |
assertDeepEq(arr.copyWithin(45, 900), arr); |
| 137 |
|
| 138 |
// test on floating point numbers |
| 139 |
for (var i = 0; i < large; i++) { |
| 140 |
arr[i] = Math.random(); |
| 141 |
} |
| 142 |
arr.copyWithin(45, 900); |
| 143 |
|
| 144 |
// test on constructor of objects |
| 145 |
for (var i = 0; i < large; i++) { |
| 146 |
arr[i] = { num: Math.random() }; |
| 147 |
} |
| 148 |
arr.copyWithin(45, 900); |
| 149 |
|
| 150 |
// test constructor length remains same |
| 151 |
assertEq(arr.length, large); |
| 152 |
|
| 153 |
// test null on third argument is handled correctly |
| 154 |
assertDeepEq((new constructor([1, 2, 3, 4, 5])).copyWithin(0, 3, null), |
| 155 |
(new constructor([1, 2, 3, 4, 5]))); |
| 156 |
|
| 157 |
// test with a proxy object |
| 158 |
var proxyObj = { |
| 159 |
get: function(recipient, name) { |
| 160 |
return recipient[name] + 2; |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
var p = new Proxy(new constructor([1, 2, 3, 4, 5]), proxyObj); |
| 165 |
|
| 166 |
assertThrowsInstanceOf(function() { |
| 167 |
constructor.prototype.copyWithin.call(p, 0, 3); |
| 168 |
}, TypeError, "Assert that copyWithin fails if used with a proxy object"); |
| 169 |
|
| 170 |
} |