1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//-----------------------------------------------------------------------------
var BUGNUMBER = 885553;
var summary = 'Array.prototype.find and Array.prototype.findIndex';
print(BUGNUMBER + ": " + summary);
/**************
* BEGIN TEST *
**************/
function isString(v, index, array)
{
assertEq(array[index], v);
return typeof v == 'string';
}
function dumpError(e)
{
var s = e.name + ': ' + e.message +
' File: ' + e.fileName +
', Line: ' + e.lineNumber +
', Stack: ' + e.stack;
return s;
}
var expect;
var actual;
var obj;
var strings = ['hello', 'Array', 'WORLD'];
var mixed = [0, '1', 2];
var sparsestrings = new Array();
sparsestrings[2] = 'sparse';
var arraylike = {0:0, 1:'string', 2:2, length:3};
// array for which JSObject::isIndexed() holds.
var indexedArray = [];
Object.defineProperty(indexedArray, 42, { get: function() { return 42; } });
Object.defineProperty(indexedArray, 142, { get: function() { return 'string'; } });
// find and findIndex have 1 required argument
expect = 1;
actual = Array.prototype.find.length;
reportCompare(expect, actual, 'Array.prototype.find.length == 1');
actual = Array.prototype.findIndex.length;
reportCompare(expect, actual, 'Array.prototype.findIndex.length == 1');
// throw TypeError if no predicate specified
expect = 'TypeError';
try
{
strings.find();
actual = 'no error';
}
catch(e)
{
actual = e.name;
}
reportCompare(expect, actual, 'Array.find(undefined) throws TypeError');
try
{
strings.findIndex();
actual = 'no error';
}
catch(e)
{
actual = e.name;
}
reportCompare(expect, actual, 'Array.findIndex(undefined) throws TypeError');
// Length gets treated as integer, not uint32
obj = { length: -4294967295, 0: 42 };
expected = undefined;
actual = Array.prototype.find.call(obj, () => true);
reportCompare(expected, actual, 'find correctly treats "length" as an integer');
expected = -1
actual = Array.prototype.findIndex.call(obj, () => true);
reportCompare(expected, actual, 'findIndex correctly treats "length" as an integer');
// test find and findIndex results
try
{
expect = 'hello';
actual = strings.find(isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'strings: find finds first string element');
try
{
expect = 0;
actual = strings.findIndex(isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'strings: findIndex finds first string element');
try
{
expect = '1';
actual = mixed.find(isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'mixed: find finds first string element');
try
{
expect = 1;
actual = mixed.findIndex(isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'mixed: findIndex finds first string element');
try
{
expect = 'sparse';
actual = sparsestrings.find(isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'sparsestrings: find finds first string element');
try
{
expect = 2;
actual = sparsestrings.findIndex(isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'sparsestrings: findIndex finds first string element');
try
{
expect = 'string';
actual = [].find.call(arraylike, isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'arraylike: find finds first string element');
try
{
expect = 1;
actual = [].findIndex.call(arraylike, isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'arraylike: findIndex finds first string element');
try
{
expect = 1;
actual = 0;
Array.prototype.find.call({get 0(){ actual++ }, length: 1}, ()=>true);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'arraylike with getter: getter only called once');
try
{
expect = 'string';
actual = [].find.call(indexedArray, isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'indexedArray: find finds first string element');
try
{
expect = 142;
actual = [].findIndex.call(indexedArray, isString);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, 'indexedArray: findIndex finds first string element');
// Bug 1058394 - Array#find and Array#findIndex no longer skip holes
var sparseArray = [,,1];
var sparseArrayWithInheritedDataProperty = Object.setPrototypeOf([,,1], {
__proto__: [].__proto__,
0 : 0
});
var sparseArrayWithInheritedAccessorProperty = Object.setPrototypeOf([,,1], {
__proto__: [].__proto__,
get 0(){
throw "get 0";
}
});
try
{
expect = undefined;
actual = sparseArray.find(() => true);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Don't skip holes in Array#find.");
try
{
expect = 0;
actual = sparseArray.findIndex(() => true);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Don't skip holes in Array#findIndex.");
try
{
expect = 0;
actual = sparseArrayWithInheritedDataProperty.find(v => v === 0);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Array#find can find inherited data property.");
try
{
expect = 0;
actual = sparseArrayWithInheritedDataProperty.findIndex(v => v === 0);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Array#findIndex can find inherited data property.");
try
{
expect = "get 0";
actual = sparseArrayWithInheritedAccessorProperty.find(() => true);
}
catch(e)
{
actual = e;
}
reportCompare(expect, actual, "Array#find can find inherited accessor property.");
try
{
expect = "get 0";
actual = sparseArrayWithInheritedAccessorProperty.findIndex(() => true);
}
catch(e)
{
actual = e;
}
reportCompare(expect, actual, "Array#findIndex can find inherited accessor property.");
|