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 // Tests for standard TypedArray array iteration functions. | |
8 | |
9 var typedArrayConstructors = [ | |
10 Uint8Array, | |
11 Int8Array, | |
12 Uint16Array, | |
13 Int16Array, | |
14 Uint32Array, | |
15 Int32Array, | |
16 Uint8ClampedArray, | |
17 Float32Array, | |
18 Float64Array | |
19 ]; | |
20 | |
21 function assertArrayLikeEquals(expected, value, type) { | |
22 assertEquals(value.__proto__, type.prototype); | |
23 assertEquals(expected.length, value.length); | |
24 for (var i = 0; i < value.length; ++i) { | |
25 assertEquals(expected[i], value[i]); | |
26 } | |
27 } | |
28 | |
29 for (var constructor of typedArrayConstructors) { | |
30 (function TypedArrayFilterTest() { | |
31 // Simple use. | |
32 var a = new constructor([0, 1]); | |
33 assertArrayLikeEquals([0], a.filter(function(n) { return n == 0; }), | |
34 constructor); | |
35 assertArrayLikeEquals([0, 1], a, constructor); | |
36 | |
37 // Use specified object as this object when calling the function. | |
38 var o = { value: 42 } | |
39 a = new constructor([1, 42, 3, 42, 4]); | |
40 assertArrayLikeEquals([42, 42], a.filter(function(n) { | |
41 return this.value == n | |
42 }, o), constructor); | |
43 | |
44 // Modify original array. | |
45 a = new constructor([1, 42, 3, 42, 4]); | |
46 assertArrayLikeEquals([42, 42], a.filter(function(n, index, array) { | |
47 array[index] = 43; return 42 == n; | |
48 }), constructor); | |
49 assertArrayLikeEquals([43, 43, 43, 43, 43], a, constructor); | |
50 | |
51 // Create a new object in each function call when receiver is a | |
52 // primitive value. See ECMA-262, Annex C. | |
53 a = []; | |
54 new constructor([1, 2]).filter(function() { a.push(this) }, ''); | |
55 assertTrue(a[0] !== a[1]); | |
56 | |
57 // Do not create a new object otherwise. | |
58 a = []; | |
59 new constructor([1, 2]).filter(function() { a.push(this) }, {}); | |
60 assertEquals(a[0], a[1]); | |
61 | |
62 // In strict mode primitive values should not be coerced to an object. | |
63 a = []; | |
64 new constructor([1, 2]).filter(function() { | |
65 'use strict'; | |
66 a.push(this); | |
67 }, ''); | |
68 assertEquals('', a[0]); | |
69 assertEquals(a[0], a[1]); | |
70 | |
71 // Calling this method on other types is a TypeError | |
72 assertThrows(function() { | |
73 constructor.prototype.filter.call([], function() {}); | |
74 }, TypeError); | |
75 | |
76 // Shadowing the length property doesn't change anything | |
77 a = new constructor([1, 2]); | |
78 Object.defineProperty(a, 'length', { value: 1 }); | |
79 assertArrayLikeEquals([2], a.filter(function(elt) { | |
80 return elt == 2; | |
81 }), constructor); | |
82 })(); | |
83 | |
84 (function TypedArrayMapTest() { | |
85 var a = new constructor([0, 1, 2, 3, 4]); | |
86 | |
87 // Simple use. | |
88 var result = [1, 2, 3, 4, 5]; | |
89 assertArrayLikeEquals(result, a.map(function(n) { return n + 1; }), | |
90 constructor); | |
91 assertEquals(a, a); | |
92 | |
93 // Use specified object as this object when calling the function. | |
94 var o = { delta: 42 } | |
95 result = [42, 43, 44, 45, 46]; | |
96 assertArrayLikeEquals(result, a.map(function(n) { | |
97 return this.delta + n; | |
98 }, o), constructor); | |
99 | |
100 // Modify original array. | |
101 a = new constructor([0, 1, 2, 3, 4]); | |
102 result = [1, 2, 3, 4, 5]; | |
103 assertArrayLikeEquals(result, a.map(function(n, index, array) { | |
104 array[index] = n + 1; | |
105 return n + 1; | |
106 }), constructor); | |
107 assertArrayLikeEquals(result, a, constructor); | |
108 | |
109 // Create a new object in each function call when receiver is a | |
110 // primitive value. See ECMA-262, Annex C. | |
111 a = []; | |
112 new constructor([1, 2]).map(function() { a.push(this) }, ''); | |
113 assertTrue(a[0] !== a[1]); | |
114 | |
115 // Do not create a new object otherwise. | |
116 a = []; | |
117 new constructor([1, 2]).map(function() { a.push(this) }, {}); | |
118 assertEquals(a[0], a[1]); | |
119 | |
120 // In strict mode primitive values should not be coerced to an object. | |
121 a = []; | |
122 new constructor([1, 2]).map(function() { 'use strict'; a.push(this); }, ''); | |
123 assertEquals('', a[0]); | |
124 assertEquals(a[0], a[1]); | |
125 | |
126 // Test that the result is converted to the right type | |
127 assertArrayLikeEquals([3, 3], new constructor([1, 2]).map(function() { | |
128 return "3"; | |
129 }), constructor); | |
130 if (constructor !== Float32Array && constructor !== Float64Array) { | |
131 assertArrayLikeEquals([0, 0], new constructor([1, 2]).map(function() { | |
132 return NaN; | |
133 }), constructor); | |
134 } | |
135 })(); | |
136 | |
137 // | |
138 // %TypedArray%.prototype.some | |
139 // | |
140 (function TypedArraySomeTest() { | |
141 var a = new constructor([0, 1, 2, 3, 4]); | |
142 | |
143 // Simple use. | |
144 assertTrue(a.some(function(n) { return n == 3})); | |
145 assertFalse(a.some(function(n) { return n == 5})); | |
146 | |
147 // Use specified object as this object when calling the function. | |
148 var o = { element: 42 }; | |
149 a = new constructor([1, 42, 3]); | |
150 assertTrue(a.some(function(n) { return this.element == n; }, o)); | |
151 a = new constructor([1]); | |
152 assertFalse(a.some(function(n) { return this.element == n; }, o)); | |
153 | |
154 // Modify original array. | |
155 a = new constructor([0, 1, 2, 3]); | |
156 assertTrue(a.some(function(n, index, array) { | |
157 array[index] = n + 1; | |
158 return n == 2; | |
159 })); | |
160 assertArrayLikeEquals([1, 2, 3, 3], a, constructor); | |
161 | |
162 // Create a new object in each function call when receiver is a | |
163 // primitive value. See ECMA-262, Annex C. | |
164 a = []; | |
165 new constructor([1, 2]).some(function() { a.push(this) }, ''); | |
166 assertTrue(a[0] !== a[1]); | |
167 | |
168 // Do not create a new object otherwise. | |
169 a = []; | |
170 new constructor([1, 2]).some(function() { a.push(this) }, {}); | |
171 assertEquals(a[0], a[1]); | |
172 | |
173 // In strict mode primitive values should not be coerced to an object. | |
174 a = []; | |
175 new constructor([1, 2]).some(function() { | |
176 'use strict'; | |
177 a.push(this); | |
178 }, ''); | |
179 assertEquals('', a[0]); | |
180 assertEquals(a[0], a[1]); | |
181 | |
182 // Calling this method on other types is a TypeError | |
183 assertThrows(function() { | |
184 constructor.prototype.some.call([], function() {}); | |
185 }, TypeError); | |
186 | |
187 // Shadowing the length property doesn't change anything | |
188 a = new constructor([1, 2]); | |
189 Object.defineProperty(a, 'length', { value: 1 }); | |
190 assertEquals(true, a.some(function(elt) { return elt == 2; })); | |
191 assertEquals(false, Array.prototype.some.call(a, function(elt) { | |
192 return elt == 2; | |
193 })); | |
194 })(); | |
195 | |
196 } | |
OLD | NEW |