Debian Codegraph @

rhino/testsrc/jstests/harmony/array-findIndex.js

raw symbols SCIP
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 load("testsrc/assert.js");
6
7 //
8 // Test The length of 'Array.prototype.findIndex' is 1
9 // (22.1.3.9)
10 (function () {
11 assertEquals(1, Array.prototype.findIndex.length);
12 })();
13
14 //
15 // Quick check for base cases
16 //
17 (function () {
18 var a = [21, 22, 23, 24];
19
20 // well, it works
21 assertEquals(0, a.findIndex(function () { return true; }));
22
23 // predicate is called with current value, index and object on which `findIndex()` was called
24 assertEquals(3, a.findIndex(function (val, i, array) { return array === a && i === 3; }));
25
26 // 'this' can be augmented by second optional parameter
27 var thisArg = {};
28 assertEquals(0, a.findIndex(function () { return this === thisArg; }, thisArg));
29
30 // when nothing found, `-1` is returned
31 assertEquals(-1, a.findIndex(function () { return false; }));
32
33 // it is not required to return Boolean, it will be automatically casted
34 assertEquals(2, a.findIndex(function (val) { return (val === a[2] ? "true" : null); }));
35 })();
36
37 //
38 // Test predicate is anything that has [[Call]] internal method
39 //
40 (function () {
41 var a = [21, 22, 23, 24];
42
43 // `InterpretedFunction` or `? extends NativeFunction`
44 assertEquals(0, a.findIndex(function () { return true; }));
45 // `IdScriptableObject`
46 assertEquals(0, a.findIndex(Object.prototype.toString));
47 assertEquals(0, a.findIndex(String));
48 // `BoundFunction`
49 assertEquals(0, a.findIndex((function () { return true; }).bind({})))
50 })();
51
52 //
53 // Test predicate is not called when array is empty
54 //
55 (function () {
56 var l = -1;
57 var o = -1;
58 var v = -1;
59 var k = -1;
60
61 [].findIndex(function (val, key, obj) {
62 o = obj;
63 l = obj.length;
64 v = val;
65 k = key;
66
67 return false;
68 });
69
70 assertEquals(-1, l);
71 assertEquals(-1, o);
72 assertEquals(-1, v);
73 assertEquals(-1, k);
74 })();
75
76 //
77 // Test predicate is called with correct arguments
78 //
79 (function () {
80 var a = ["b"];
81 var l = -1;
82 var o = -1;
83 var v = -1;
84 var k = -1;
85
86 var found = a.findIndex(function (val, key, obj) {
87 o = obj;
88 l = obj.length;
89 v = val;
90 k = key;
91
92 return false;
93 });
94
95 assertArrayEquals(a, o);
96 assertEquals(a.length, l);
97 assertEquals("b", v);
98 assertEquals(0, k);
99 assertEquals(-1, found);
100 })();
101
102 //
103 // Test predicate is called array.length times
104 //
105 (function () {
106 var a = [1, 2, 3, 4, 5];
107 var l = 0;
108 var sawUndefined = false;
109 var predicate = function p(v) {
110 l++;
111 sawUndefined = sawUndefined || (v === undefined);
112 };
113
114 a.findIndex(predicate);
115 assertEquals(a.length, l);
116 assertFalse(sawUndefined);
117
118 // even for sparse arrays
119 a = new Array(10);
120 l = 0;
121 a.findIndex(predicate);
122 assertEquals(a.length, l);
123 assertTrue(sawUndefined);
124
125 a = [];
126 a[10] = 1;
127 l = 0;
128 sawUndefined = false;
129 a.findIndex(predicate);
130 assertEquals(a.length, l);
131 assertTrue(sawUndefined);
132 })();
133
134
135 //
136 // Test Array.prototype.findIndex is generic and works with String
137 //
138 (function () {
139 var a = "abcd";
140 var l = -1;
141 var o = -1;
142 var v = -1;
143 var k = -1;
144 var found = Array.prototype.findIndex.call(a, function (val, key, obj) {
145 o = obj.toString();
146 l = obj.length;
147 v = val;
148 k = key;
149
150 return false;
151 });
152
153 assertEquals(a, o);
154 assertEquals(a.length, l);
155 assertEquals("d", v);
156 assertEquals(3, k);
157 assertEquals(-1, found);
158
159 found = Array.prototype.findIndex.apply(a, [function (val, key, obj) {
160 o = obj.toString();
161 l = obj.length;
162 v = val;
163 k = key;
164
165 return true;
166 }]);
167
168 assertEquals(a, o);
169 assertEquals(a.length, l);
170 assertEquals("a", v);
171 assertEquals(0, k);
172 assertEquals(0, found);
173 })();
174
175 //
176 // Test Array.prototype.findIndex works with simple arraylike objects
177 //
178 (function () {
179 var o = {0: 0, 1: 1, 2: 2, length: 3};
180 assertEquals(2, Array.prototype.findIndex.call(o, function (v) { return v == o[2]; }));
181 assertEquals(1, Array.prototype.findIndex.apply(o, [function (v) { return v == o[1]; }]));
182
183 // object without `length` property defined,
184 // behaves as if it has `length` set 0
185 var empty = {0: 0, 1: 1, 2: 2};
186 var called = false;
187 Array.prototype.findIndex.call(empty, function () { called = true; });
188 assertEquals(false, called);
189 })();
190
191 //
192 // Test Array.prototype.findIndex works with mixed arraylike objects
193 //
194 (function () {
195 var l = -1;
196 var o = -1;
197 var v = -1;
198 var k = -1;
199 var a = {
200 prop1: "val1",
201 prop2: "val2",
202 isValid: function () {
203 return this.prop1 === "val1" && this.prop2 === "val2";
204 },
205 length: 0
206 };
207
208 Array.prototype.push.apply(a, [30, 31, 32]);
209 var found = Array.prototype.findIndex.call(a, function (val, key, obj) {
210 o = obj;
211 l = obj.length;
212 v = val;
213 k = key;
214
215 return !obj.isValid();
216 });
217
218 assertArrayEquals(a, o);
219 assertEquals(3, l);
220 assertEquals(32, v);
221 assertEquals(2, k);
222 assertEquals(-1, found);
223 })();
224
225 //
226 // Test Array.prototype.findIndex works with arraylike object with getters
227 //
228 (function () {
229 var count = 0;
230 var a = {get 0() { return count++; }, length: 1};
231 // FIXME: right now, Rhino will get raw getter function
232 // during iteration and use it as a value passed to predicate
233 //Array.prototype.findIndex.call(a, (function () { return true; }));
234 //assertEquals(1, count);
235 })();
236
237 //
238 // Test Array.prototype.findIndex iteration includes inherited properties
239 //
240 (function () {
241 var o1 = {0: 0, 1: 1};
242 var o2 = {2: 2, length: 3};
243 // FIXME: use Object.setPrototypeOf instead
244 o2.__proto__ = o1;
245 var a = [];
246 Array.prototype.findIndex.call(o2, function (v) { a.push(v); });
247 assertEquals([0, 1, 2], a);
248 assertEquals(0, Array.prototype.findIndex.call(o2, function () { return true; }));
249 })();
250
251 //
252 // Test array modifications
253 //
254 (function () {
255 var a = [1, 2, 3];
256 var found = a.findIndex(function (val) {
257 a.push(val);
258 return false;
259 });
260 assertArrayEquals([1, 2, 3, 1, 2, 3], a);
261 assertEquals(6, a.length);
262 assertEquals(-1, found);
263
264 a = [1, 2, 3];
265 found = a.findIndex(function (val, key) {
266 a[key] = ++val;
267 return false;
268 });
269 assertArrayEquals([2, 3, 4], a);
270 assertEquals(3, a.length);
271 assertEquals(-1, found);
272 })();
273
274 //
275 // Test thisArg
276 //
277 (function () {
278 // If thisArg is not provided, predicate is invoked with this set to `undefined`
279 // FIXME:
280 // var o = -1;
281 //[1,2].findIndex(function () { o = this; });
282 //assertEquals(undefined, o);
283
284 // Test String as a thisArg
285 var found = [1, 2, 3].findIndex(function (val, key) {
286 return this.charAt(Number(key)) === String(val);
287 }, "321");
288 assertEquals(1, found);
289
290 // Test object as a thisArg
291 var thisArg = {
292 elementAt: function (key) {
293 return this[key];
294 }
295 };
296 Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
297
298 found = ["a", "b", "c"].findIndex(function (val, key) {
299 return this.elementAt(key) === val;
300 }, thisArg);
301 assertEquals(1, found);
302
303 // Test array itself as thisArg
304 var o;
305 var a = [1, 2];
306 a.findIndex(function () { o = this; }, a);
307 assertEquals(a, o);
308 })();
309
310 // Test exceptions
311 assertThrows('Array.prototype.findIndex.call(null, function() { })', TypeError);
312 assertThrows('Array.prototype.findIndex.call(undefined, function() { })', TypeError);
313 assertThrows('Array.prototype.findIndex.apply(null, function() { }, [])', TypeError);
314 assertThrows('Array.prototype.findIndex.apply(undefined, function() { }, [])', TypeError);
315
316 assertThrows('[].findIndex(null)', TypeError);
317 assertThrows('[].findIndex(undefined)', TypeError);
318 assertThrows('[].findIndex(0)', TypeError);
319 assertThrows('[].findIndex(true)', TypeError);
320 assertThrows('[].findIndex(false)', TypeError);
321 assertThrows('[].findIndex("")', TypeError);
322 assertThrows('[].findIndex({})', TypeError);
323 assertThrows('[].findIndex([])', TypeError);
324 assertThrows('[].findIndex(/\d+/)', TypeError);
325
326 assertThrows('Array.prototype.findIndex.call({}, null)', TypeError);
327 assertThrows('Array.prototype.findIndex.call({}, undefined)', TypeError);
328 assertThrows('Array.prototype.findIndex.call({}, 0)', TypeError);
329 assertThrows('Array.prototype.findIndex.call({}, true)', TypeError);
330 assertThrows('Array.prototype.findIndex.call({}, false)', TypeError);
331 assertThrows('Array.prototype.findIndex.call({}, "")', TypeError);
332 assertThrows('Array.prototype.findIndex.call({}, {})', TypeError);
333 assertThrows('Array.prototype.findIndex.call({}, [])', TypeError);
334 assertThrows('Array.prototype.findIndex.call({}, /\d+/)', TypeError);
335
336 assertThrows('Array.prototype.findIndex.apply({}, null, [])', TypeError);
337 assertThrows('Array.prototype.findIndex.apply({}, undefined, [])', TypeError);
338 assertThrows('Array.prototype.findIndex.apply({}, 0, [])', TypeError);
339 assertThrows('Array.prototype.findIndex.apply({}, true, [])', TypeError);
340 assertThrows('Array.prototype.findIndex.apply({}, false, [])', TypeError);
341 assertThrows('Array.prototype.findIndex.apply({}, "", [])', TypeError);
342 assertThrows('Array.prototype.findIndex.apply({}, {}, [])', TypeError);
343 assertThrows('Array.prototype.findIndex.apply({}, [], [])', TypeError);
344 assertThrows('Array.prototype.findIndex.apply({}, /\d+/, [])', TypeError);
345
346 "success";