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