OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 copyWithinDefaultEnd(constructor) { | |
142 // undefined as third argument | |
143 assertArrayEquals([4, 5, 3, 4, 5], | |
144 new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, undefined)
); | |
145 }); | |
146 | |
147 | |
148 CheckEachTypedArray(function copyWithinLargeArray(constructor) { | |
149 var large = 10000; | |
150 | |
151 // test on a large array | |
152 var arr = new constructor(large); | |
153 assertArrayEquals(arr, arr.copyWithin(45, 9000)); | |
154 | |
155 var expected = new Array(large); | |
156 // test on numbers | |
157 for (var i = 0; i < large; i++) { | |
158 arr[i] = Math.random() * 100; // May be cast to an int | |
159 expected[i] = arr[i]; | |
160 if (i >= 9000) { | |
161 expected[(i - 9000) + 45] = arr[i]; | |
162 } | |
163 } | |
164 assertArrayEquals(expected, arr.copyWithin(45, 9000)); | |
165 | |
166 // test array length remains same | |
167 assertEquals(large, arr.length); | |
168 }); | |
169 | |
170 | |
171 CheckEachTypedArray(function copyWithinNullEnd(constructor) { | |
172 // test null on third argument is converted to +0 | |
173 assertArrayEquals([1, 2, 3, 4, 5], | |
174 new constructor([1, 2, 3, 4, 5]).copyWithin(0, 3, null)); | |
175 }); | |
OLD | NEW |