blob: 51c439203d907bad0bef433858d49449c6764905 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
5var typedArrayConstructors = [
6 Uint8Array,
7 Int8Array,
8 Uint16Array,
9 Int16Array,
10 Uint32Array,
11 Int32Array,
12 Uint8ClampedArray,
13 Float32Array,
14 Float64Array];
15
16for (var constructor of typedArrayConstructors) {
17
18assertEquals(1, constructor.prototype.findIndex.length);
19
20var a = new constructor([21, 22, 23, 24]);
21assertEquals(-1, a.findIndex(function() { return false; }));
22assertEquals(-1, a.findIndex(function(val) { return 121 === val; }));
23assertEquals(0, a.findIndex(function() { return true; }));
24assertEquals(1, a.findIndex(function(val) { return 22 === val; }), undefined);
25assertEquals(2, a.findIndex(function(val) { return 23 === val; }), null);
26assertEquals(3, a.findIndex(function(val) { return 24 === val; }));
27
28
29//
30// Test predicate is not called when array is empty
31//
32(function() {
33 var a = new constructor([]);
34 var l = -1;
35 var o = -1;
36 var v = -1;
37 var k = -1;
38
39 a.findIndex(function(val, key, obj) {
40 o = obj;
41 l = obj.length;
42 v = val;
43 k = key;
44
45 return false;
46 });
47
48 assertEquals(-1, l);
49 assertEquals(-1, o);
50 assertEquals(-1, v);
51 assertEquals(-1, k);
52})();
53
54
55//
56// Test predicate is called with correct argumetns
57//
58(function() {
59 var a = new constructor([5]);
60 var l = -1;
61 var o = -1;
62 var v = -1;
63 var k = -1;
64
65 var index = a.findIndex(function(val, key, obj) {
66 o = obj;
67 l = obj.length;
68 v = val;
69 k = key;
70
71 return false;
72 });
73
74 assertArrayEquals(a, o);
75 assertEquals(a.length, l);
76 assertEquals(5, v);
77 assertEquals(0, k);
78 assertEquals(-1, index);
79})();
80
81
82//
83// Test predicate is called array.length times
84//
85(function() {
86 var a = new constructor([1, 2, 3, 4, 5]);
87 var l = 0;
88
89 a.findIndex(function() {
90 l++;
91 return false;
92 });
93
94 assertEquals(a.length, l);
95})();
96
97
98//
99// Test array modifications
100//
101(function() {
102 a = new constructor([1, 2, 3]);
103 a.findIndex(function(val, key) { a[key] = ++val; return false; });
104 assertArrayEquals([2, 3, 4], a);
105 assertEquals(3, a.length);
106})();
107
108
109//
110// Test thisArg
111//
112(function() {
113 // Test String as a thisArg
114 var index = new constructor([1, 2, 3]).findIndex(function(val, key) {
115 return this.charAt(Number(key)) === String(val);
116 }, "321");
117 assertEquals(1, index);
118
119 // Test object as a thisArg
120 var thisArg = {
121 elementAt: function(key) {
122 return this[key];
123 }
124 };
125 Array.prototype.push.apply(thisArg, [3, 2, 1]);
126
127 index = new constructor([1, 2, 3]).findIndex(function(val, key) {
128 return this.elementAt(key) === val;
129 }, thisArg);
130 assertEquals(1, index);
131
132 // Create a new object in each function call when receiver is a
133 // primitive value. See ECMA-262, Annex C.
134 a = [];
135 new constructor([1, 2]).findIndex(function() { a.push(this) }, "");
136 assertTrue(a[0] !== a[1]);
137
138 // Do not create a new object otherwise.
139 a = [];
140 new constructor([1, 2]).findIndex(function() { a.push(this) }, {});
141 assertEquals(a[0], a[1]);
142
143 // In strict mode primitive values should not be coerced to an object.
144 a = [];
145 new constructor([1, 2]).findIndex(function() { 'use strict'; a.push(this); }, "");
146 assertEquals("", a[0]);
147 assertEquals(a[0], a[1]);
148
149})();
150
151// Test exceptions
152assertThrows('constructor.prototype.findIndex.call(null, function() { })',
153 TypeError);
154assertThrows('constructor.prototype.findIndex.call(undefined, function() { })',
155 TypeError);
156assertThrows('constructor.prototype.findIndex.apply(null, function() { }, [])',
157 TypeError);
158assertThrows('constructor.prototype.findIndex.apply(undefined, function() { }, [])',
159 TypeError);
160assertThrows('constructor.prototype.findIndex.apply([], function() { }, [])',
161 TypeError);
162assertThrows('constructor.prototype.findIndex.apply({}, function() { }, [])',
163 TypeError);
164assertThrows('constructor.prototype.findIndex.apply("", function() { }, [])',
165 TypeError);
166
167assertThrows('new constructor([]).findIndex(null)', TypeError);
168assertThrows('new constructor([]).findIndex(undefined)', TypeError);
169assertThrows('new constructor([]).findIndex(0)', TypeError);
170assertThrows('new constructor([]).findIndex(true)', TypeError);
171assertThrows('new constructor([]).findIndex(false)', TypeError);
172assertThrows('new constructor([]).findIndex("")', TypeError);
173assertThrows('new constructor([]).findIndex({})', TypeError);
174assertThrows('new constructor([]).findIndex([])', TypeError);
175assertThrows('new constructor([]).findIndex(/\d+/)', TypeError);
176
177// Shadowing length doesn't affect findIndex, unlike Array.prototype.findIndex
178a = new constructor([1, 2]);
179Object.defineProperty(a, 'length', {value: 1});
180var x = 0;
181assertEquals(a.findIndex(function(elt) { x += elt; return false; }), -1);
182assertEquals(x, 3);
183assertEquals(Array.prototype.findIndex.call(a,
184 function(elt) { x += elt; return false; }), -1);
185assertEquals(x, 4);
186
187}