Debian Codegraph @

js/src/jit-test/tests/basic/typed-array-copyWithin.js

raw
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 Float16Array,
16 Float32Array,
17 Float64Array
18 ];
19
20 for (var constructor of constructors) {
21
22 assertEq(constructor.prototype.copyWithin.length, 2);
23
24 // works with two arguments
25 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3),
26 new constructor([4, 5, 3, 4, 5]));
27 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(1, 3),
28 new constructor([1, 4, 5, 4, 5]));
29 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(1, 2),
30 new constructor([1, 3, 4, 5, 5]));
31 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(2, 2),
32 new constructor([1, 2, 3, 4, 5]));
33
34 // works with three arguments
35 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, 4),
36 new constructor([4, 2, 3, 4, 5]));
37 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(1, 3, 4),
38 new constructor([1, 4, 3, 4, 5]));
39 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(1, 2, 4),
40 new constructor([1, 3, 4, 4, 5]));
41
42 // works with negative arguments
43 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0, -2),
44 new constructor([4, 5, 3, 4, 5]));
45 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0, -2, -1),
46 new constructor([4, 2, 3, 4, 5]));
47 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(-4, -3, -2),
48 new constructor([1, 3, 3, 4, 5]));
49 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(-4, -3, -1),
50 new constructor([1, 3, 4, 4, 5]));
51 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(-4, -3),
52 new constructor([1, 3, 4, 5, 5]));
53
54 // throws on null/undefined values
55 assertThrowsInstanceOf(function() {
56 constructor.prototype.copyWithin.call(null, 0, 3);
57 }, TypeError, "Assert that copyWithin fails if this value is null");
58
59 assertThrowsInstanceOf(function() {
60 var throw42 = { valueOf: function() { throw 42; } };
61 constructor.prototype.copyWithin.call(null, throw42, throw42, throw42);
62 }, TypeError, "Assert that copyWithin fails if this value is null");
63
64 assertThrowsInstanceOf(function() {
65 var throw42 = { valueOf: function() { throw 42; } };
66 constructor.prototype.copyWithin.call(undefined, throw42, throw42, throw42);
67 }, TypeError, "Assert that copyWithin fails if this value is undefined");
68
69 assertThrowsInstanceOf(function() {
70 constructor.prototype.copyWithin.call(undefined, 0, 3);
71 }, TypeError, "Assert that copyWithin fails if this value is undefined");
72
73 // test with this value as string
74 assertThrowsInstanceOf(function() {
75 constructor.prototype.copyWithin.call("hello world", 0, 3);
76 }, TypeError, "Assert that copyWithin fails if this value is string");
77
78 assertThrowsInstanceOf(function() {
79 var throw42 = { valueOf: function() { throw 42; } };
80 constructor.prototype.copyWithin.call("hello world", throw42, throw42, throw42);
81 }, TypeError, "Assert that copyWithin fails if this value is string");
82
83 // test with target > start on 2 arguments
84 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(3, 0),
85 new constructor([1, 2, 3, 1, 2]));
86
87 // test with target > start on 3 arguments
88 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(3, 0, 4),
89 new constructor([1, 2, 3, 1, 2]));
90
91 // test on fractional arguments
92 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0.2, 3.9),
93 new constructor([4, 5, 3, 4, 5]));
94
95 // test with -0
96 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(-0, 3),
97 new constructor([4, 5, 3, 4, 5]));
98
99 // test with arguments more than this.length
100 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0, 7),
101 new constructor([1, 2, 3, 4, 5]));
102
103 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(7, 0),
104 new constructor([1, 2, 3, 4, 5]));
105
106 // test with arguments less than -this.length
107 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(-7, 0),
108 new constructor([1, 2, 3, 4, 5]));
109
110 // test with arguments equal to -this.length
111 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(-5, 0),
112 new constructor([1, 2, 3, 4, 5]));
113
114 // test on empty constructor
115 assertDeepEq(new constructor([]).copyWithin(0, 3),
116 new constructor([]));
117
118 // test with target range being shorter than end - start
119 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(2, 1, 4),
120 new constructor([1, 2, 2, 3, 4]));
121
122 // test overlapping ranges
123 arr = new constructor([1, 2, 3, 4, 5]);
124 arr.copyWithin(2, 1, 4);
125 assertDeepEq(arr.copyWithin(2, 1, 4),
126 new constructor([1, 2, 2, 2, 3]));
127
128 // undefined as third argument
129 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, undefined),
130 new constructor([4, 5, 3, 4, 5]));
131
132 // test that this.length is never called
133 arr = new constructor([0, 1, 2, 3, 5]);
134 Object.defineProperty(arr, "length", {
135 get: function () { throw new Error("length accessor called"); }
136 });
137 arr.copyWithin(1, 3);
138
139 var large = 10000;
140
141 // test on a large constructor
142 arr = new constructor(large);
143 assertDeepEq(arr.copyWithin(45, 900), arr);
144
145 // test on floating point numbers
146 for (var i = 0; i < large; i++) {
147 arr[i] = Math.random();
148 }
149 arr.copyWithin(45, 900);
150
151 // test on constructor of objects
152 for (var i = 0; i < large; i++) {
153 arr[i] = { num: Math.random() };
154 }
155 arr.copyWithin(45, 900);
156
157 // test constructor length remains same
158 assertEq(arr.length, large);
159
160 // test null on third argument is handled correctly
161 assertDeepEq(new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, null),
162 new constructor([1, 2, 3, 4, 5]));
163
164 //Any argument referenced, but not provided, has the value |undefined|
165 var tarray = new constructor(8);
166 try
167 {
168 tarray.copyWithin({ valueOf: function() { throw 42; } });
169 throw new Error("expected to throw");
170 }
171 catch (e)
172 {
173 assertEq(e, 42, "should have failed converting target to index");
174 }
175
176 function detachAndConvertTo(x) {
177 return { valueOf() {
178 detachArrayBuffer(tarray.buffer);
179 return x;
180 } };
181 }
182
183 // Detaching during argument processing triggers a TypeError.
184 tarray = new constructor([1, 2, 3, 4, 5]);
185 try
186 {
187 tarray.copyWithin(0, 3, detachAndConvertTo(4));
188 throw new Error("expected to throw");
189 }
190 catch (e)
191 {
192 assertEq(e instanceof TypeError, true,
193 "expected throw with detached buffer during set");
194 }
195
196 // ...unless no elements are to be copied.
197 tarray = new constructor([1, 2, 3, 4, 5]);
198 assertDeepEq(tarray.copyWithin(0, 3, detachAndConvertTo(3)),
199 new constructor([]));
200
201 /* // fails, unclear whether it should, disabling for now
202 // test with a proxy object
203 var handler = {
204 get: function(recipient, name) {
205 return recipient[name] + 2;
206 }
207 };
208
209 var p = new Proxy(new constructor([1, 2, 3, 4, 5]), handler);
210
211 assertThrowsInstanceOf(function() {
212 constructor.prototype.copyWithin.call(p, 0, 3);
213 }, TypeError, "Assert that copyWithin fails if used with a proxy object");
214 */
215 }