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