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 var typedArrayConstructors = [ | |
8 Uint8Array, | |
9 Int8Array, | |
10 Uint16Array, | |
11 Int16Array, | |
12 Uint32Array, | |
13 Int32Array, | |
14 Uint8ClampedArray, | |
15 Float32Array, | |
16 Float64Array]; | |
17 | |
18 function CheckEachTypedArray(fn) { | |
19 typedArrayConstructors.forEach(fn); | |
20 } | |
21 | |
22 CheckEachTypedArray(function copyWithinArity(constructor) { | |
23 assertEquals(new constructor([]).copyWithin.length, 2); | |
24 }); | |
25 | |
26 | |
27 CheckEachTypedArray(function copyWithinTargetAndStart(constructor) { | |
28 // works with two arguments | |
29 assertArrayEquals([4, 5, 3, 4, 5], | |
30 new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3)); | |
31 assertArrayEquals([1, 4, 5, 4, 5], | |
32 new constructor([1, 2, 3, 4, 5]).copyWithin(1, 3)); | |
33 assertArrayEquals([1, 3, 4, 5, 5], | |
34 new constructor([1, 2, 3, 4, 5]).copyWithin(1, 2)); | |
35 assertArrayEquals([1, 2, 3, 4, 5], | |
36 new constructor([1, 2, 3, 4, 5]).copyWithin(2, 2)); | |
37 }); | |
38 | |
39 | |
40 CheckEachTypedArray(function copyWithinTargetStartAndEnd(constructor) { | |
41 // works with three arguments | |
42 assertArrayEquals(new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, 4), | |
43 [4, 2, 3, 4, 5]); | |
44 assertArrayEquals(new constructor([1, 2, 3, 4, 5]).copyWithin(1, 3, 4), | |
45 [1, 4, 3, 4, 5]); | |
46 assertArrayEquals(new constructor([1, 2, 3, 4, 5]).copyWithin(1, 2, 4), | |
47 [1, 3, 4, 4, 5]); | |
48 }); | |
49 | |
50 | |
51 CheckEachTypedArray(function copyWithinNegativeRelativeOffsets(constructor) { | |
52 // works with negative arguments | |
53 assertArrayEquals([4, 5, 3, 4, 5], | |
54 new constructor([1, 2, 3, 4, 5]).copyWithin(0, -2)); | |
55 assertArrayEquals([4, 2, 3, 4, 5], | |
56 new constructor([1, 2, 3, 4, 5]).copyWithin(0, -2, -1)); | |
57 assertArrayEquals([1, 3, 3, 4, 5], | |
58 new constructor([1, 2, 3, 4, 5]).copyWithin(-4, -3, -2)); | |
59 assertArrayEquals([1, 3, 4, 4, 5], | |
60 new constructor([1, 2, 3, 4, 5]).copyWithin(-4, -3, -1)); | |
61 assertArrayEquals([1, 3, 4, 5, 5], | |
62 new constructor([1, 2, 3, 4, 5]).copyWithin(-4, -3)); | |
63 // test with arguments equal to -this.length | |
64 assertArrayEquals([1, 2, 3, 4, 5], | |
65 new constructor([1, 2, 3, 4, 5]).copyWithin(-5, 0)); | |
66 }); | |
67 | |
68 | |
69 CheckEachTypedArray(function mustBeTypedArray(constructor) { | |
70 // throws on non-TypedArray values | |
71 assertThrows(function() { | |
72 return constructor.prototype.copyWithin.call(null, 0, 3); | |
73 }, TypeError); | |
74 assertThrows(function() { | |
75 return constructor.prototype.copyWithin.call(undefined, 0, 3); | |
76 }, TypeError); | |
77 assertThrows(function() { | |
78 return constructor.prototype.copyWithin.call(34, 0, 3); | |
79 }, TypeError); | |
80 assertThrows(function() { | |
81 return constructor.prototype.copyWithin.call([1, 2, 3, 4, 5], 0, 3); | |
82 }, TypeError); | |
83 }); | |
84 | |
85 | |
86 CheckEachTypedArray(function copyWithinStartLessThanTarget(constructor) { | |
87 // test with target > start on 2 arguments | |
88 assertArrayEquals([1, 2, 3, 1, 2], | |
89 new constructor([1, 2, 3, 4, 5]).copyWithin(3, 0)); | |
90 | |
91 // test with target > start on 3 arguments | |
92 assertArrayEquals([1, 2, 3, 1, 2], | |
93 new constructor([1, 2, 3, 4, 5]).copyWithin(3, 0, 4)); | |
94 }); | |
95 | |
96 CheckEachTypedArray(function copyWithinNonIntegerRelativeOffsets(constructor) { | |
97 // test on fractional arguments | |
98 assertArrayEquals([4, 5, 3, 4, 5], | |
99 new constructor([1, 2, 3, 4, 5]).copyWithin(0.2, 3.9)); | |
100 }); | |
101 | |
102 | |
103 CheckEachTypedArray(function copyWithinNegativeZeroTarget(constructor) { | |
104 // test with -0 | |
105 assertArrayEquals([4, 5, 3, 4, 5], | |
106 new constructor([1, 2, 3, 4, 5]).copyWithin(-0, 3)); | |
107 }); | |
108 | |
109 | |
110 CheckEachTypedArray(function copyWithinTargetOutsideStart(constructor) { | |
111 // test with arguments more than this.length | |
112 assertArrayEquals([1, 2, 3, 4, 5], | |
113 new constructor([1, 2, 3, 4, 5]).copyWithin(0, 7)); | |
114 | |
115 // test with arguments less than -this.length | |
116 assertArrayEquals([1, 2, 3, 4, 5], | |
117 new constructor([1, 2, 3, 4, 5]).copyWithin(-7, 0)); | |
118 }); | |
119 | |
120 | |
121 CheckEachTypedArray(function copyWithinEmptyArray(constructor) { | |
122 // test on empty array | |
123 assertArrayEquals([], new constructor([]).copyWithin(0, 3)); | |
124 }); | |
125 | |
126 | |
127 CheckEachTypedArray(function copyWithinTargetCutOff(constructor) { | |
128 // test with target range being shorter than end - start | |
129 assertArrayEquals([1, 2, 2, 3, 4], [1, 2, 3, 4, 5].copyWithin(2, 1, 4)); | |
130 }); | |
131 | |
132 | |
133 CheckEachTypedArray(function copyWithinOverlappingRanges(constructor) { | |
134 // test overlapping ranges | |
135 var arr = [1, 2, 3, 4, 5]; | |
136 arr.copyWithin(2, 1, 4); | |
137 assertArrayEquals([1, 2, 2, 2, 3], arr.copyWithin(2, 1, 4)); | |
138 }); | |
139 | |
140 | |
141 CheckEachTypedArray(function copyWithinStrictSet(constructor) { | |
142 // check that [[Set]] is strict (non-extensible via freeze) | |
143 assertThrows(function() { | |
144 return Object.freeze(new constructor([1, 2, 3, 4, 5])).copyWithin(0, 3); | |
145 }, TypeError); | |
146 | |
147 if (0) { | |
caitp (gmail)
2015/05/07 20:13:20
Leftover code?
| |
148 // check that [[Set]] is strict (non-extensible via seal) | |
149 assertThrows(function() { | |
150 return Object.seal([, 2, 3, 4, 5]).copyWithin(0, 3); | |
151 }, TypeError); | |
152 | |
153 // check that [[Set]] is strict (non-extensible via preventExtensions) | |
154 assertThrows(function() { | |
155 return Object.preventExtensions([ , 2, 3, 4, 5]).copyWithin(0, 3); | |
156 }, TypeError); | |
157 } | |
158 }); | |
159 | |
160 | |
161 CheckEachTypedArray(function copyWithinDefaultEnd(constructor) { | |
162 // undefined as third argument | |
163 assertArrayEquals([4, 5, 3, 4, 5], | |
164 new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, undefined) ); | |
165 }); | |
166 | |
167 | |
168 CheckEachTypedArray(function copyWithinLargeArray(constructor) { | |
169 var large = 10000; | |
170 | |
171 // test on a large array | |
172 var arr = new constructor(large); | |
173 assertArrayEquals(arr, arr.copyWithin(45, 9000)); | |
174 | |
175 var expected = new Array(large); | |
176 // test on numbers | |
177 for (var i = 0; i < large; i++) { | |
178 arr[i] = Math.random() * 100; // May be cast to an int | |
179 expected[i] = arr[i]; | |
180 if (i >= 9000) { | |
181 expected[(i - 9000) + 45] = arr[i]; | |
182 } | |
183 } | |
184 assertArrayEquals(expected, arr.copyWithin(45, 9000)); | |
185 | |
186 // test array length remains same | |
187 assertEquals(large, arr.length); | |
188 }); | |
189 | |
190 | |
191 CheckEachTypedArray(function copyWithinNullEnd(constructor) { | |
192 // test null on third argument is converted to +0 | |
193 assertArrayEquals([1, 2, 3, 4, 5], | |
194 new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, null)); | |
195 }); | |
OLD | NEW |