summaryrefslogtreecommitdiffstats
path: root/Source/JavaScriptCore/tests/mozilla/js1_6/Array/regress-290592.js
blob: a0c53ca526cdccdc34bf8b46d41ab79df76ae38d (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is JavaScript Engine testing utilities.
 *
 * The Initial Developer of the Original Code is
 * Mozilla Foundation.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Mike Shaver
 *                 Bob Clary
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
//-----------------------------------------------------------------------------
var bug = 290592;
var summary = 'Array extras: forEach, indexOf, filter, map';
var actual = '';
var expect = '';

printBugNumber (bug);
printStatus (summary);

// Utility functions

function identity(v, index, array)
{
  reportCompare(v, array[index], 'identity: check callback argument consistency');
  return v;
}  

function mutate(v, index, array)
{
  reportCompare(v, array[index], 'mutate: check callback argument consistency');
  if (index == 0)
  {
    array[1] = 'mutated';
    delete array[2];
    array.push('not visited');
  }
  return v;
}

function mutateForEach(v, index, array)
{
  reportCompare(v, array[index], 'mutateForEach: check callback argument consistency');
  if (index == 0)
  {
    array[1] = 'mutated';
    delete array[2];
    array.push('not visited');
  }
  actual += v + ',';
}

function makeUpperCase(v, index, array)
{
  reportCompare(v, array[index], 'makeUpperCase: check callback argument consistency');
  try
  {
    return v.toUpperCase();
  }
  catch(e)
  {
  }
  return v;
}


function concat(v, index, array)
{
  reportCompare(v, array[index], 'concat: check callback argument consistency');
  actual += v + ',';
}


function isUpperCase(v, index, array)
{
  reportCompare(v, array[index], 'isUpperCase: check callback argument consistency');
  try
  {
    return v == v.toUpperCase();
  }
  catch(e)
  {
  }
  return false;
}

function isString(v, index, array)
{
  reportCompare(v, array[index], 'isString: check callback argument consistency');
  return typeof v == 'string';
}


// callback object.
function ArrayCallback(state)
{
  this.state = state;
}

ArrayCallback.prototype.makeUpperCase = function (v, index, array)
{
  reportCompare(v, array[index], 'ArrayCallback.prototype.makeUpperCase: check callback argument consistency');
  try
  {
    return this.state ? v.toUpperCase() : v.toLowerCase();
  }
  catch(e)
  {
  }
  return v;
};

ArrayCallback.prototype.concat = function(v, index, array)
{
  reportCompare(v, array[index], 'ArrayCallback.prototype.concat: check callback argument consistency');
  actual += v + ',';
};

ArrayCallback.prototype.isUpperCase = function(v, index, array)
{
  reportCompare(v, array[index], 'ArrayCallback.prototype.isUpperCase: check callback argument consistency');
  try
  {
    return this.state ? true : (v == v.toUpperCase());
  }
  catch(e)
  {
  }
  return false;
};

ArrayCallback.prototype.isString = function(v, index, array)
{
  reportCompare(v, array[index], 'ArrayCallback.prototype.isString: check callback argument consistency');
  return this.state ? true : (typeof v == 'string');
};

function dumpError(e)
{
  var s = e.name + ': ' + e.message + 
    ' File: ' + e.fileName + 
    ', Line: ' + e.lineNumber + 
    ', Stack: ' + e.stack;
  return s;
}

var obj;
var strings = ['hello', 'Array', 'WORLD'];
var mixed   = [0, '0', 0];
var sparsestrings = new Array();
sparsestrings[2] = 'sparse';

if ('map' in Array.prototype)
{
// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map

  // test Array.map

  // map has 1 required argument
  expect = 1;
  actual = Array.prototype.map.length;
  reportCompare(expect, actual, 'Array.prototype.map.length == 1');

  // throw TypeError if no callback function specified
  expect = 'TypeError';
  try
  {
    strings.map();
    actual = 'no error';
  }
  catch(e)
  {
    actual = e.name;
  }
  reportCompare(expect, actual, 'Array.map(undefined) throws TypeError');  

  try
  {
    // identity map
    expect = 'hello,Array,WORLD';
    actual = strings.map(identity).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.map: identity');  


  try
  {
    expect = 'hello,mutated,';
    actual = strings.map(mutate).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.map: mutate');  

  strings = ['hello', 'Array', 'WORLD'];

  try
  {
    // general map
    expect = 'HELLO,ARRAY,WORLD';
    actual = strings.map(makeUpperCase).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.map: uppercase');  

  try
  {
    // pass object method as map callback
    expect = 'HELLO,ARRAY,WORLD';
    var obj = new ArrayCallback(true);
    actual  = strings.map(obj.makeUpperCase, obj).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.map: uppercase with object callback');  

  try
  {
    expect = 'hello,array,world';
    obj = new ArrayCallback(false);
    actual = strings.map(obj.makeUpperCase, obj).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.map: lowercase with object callback');  

  try
  {
    // map on sparse arrays
    expect = ',,SPARSE';
    actual = sparsestrings.map(makeUpperCase).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.map: uppercase on sparse array');  
}

if ('forEach' in Array.prototype)
{
// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach

  // test Array.forEach

  // forEach has 1 required argument
  expect = 1;
  actual = Array.prototype.forEach.length;
  reportCompare(expect, actual, 'Array.prototype.forEach.length == 1');

  // throw TypeError if no callback function specified
  expect = 'TypeError';
  try
  {
    strings.forEach();
    actual = 'no error';
  }
  catch(e)
  {
    actual = e.name;
  }
  reportCompare(expect, actual, 'Array.forEach(undefined) throws TypeError');  

  try
  {
    // general forEach
    expect = 'hello,Array,WORLD,';
    actual = '';
    strings.forEach(concat);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.forEach');  

  try
  {
    expect = 'hello,mutated,';
    actual = '';
    strings.forEach(mutateForEach);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.forEach: mutate');  

  strings = ['hello', 'Array', 'WORLD'];



  try
  {
    // pass object method as forEach callback
    expect = 'hello,Array,WORLD,';
    actual = '';
    obj = new ArrayCallback(true);
    strings.forEach(obj.concat, obj);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.forEach with object callback 1');  

  try
  {
    expect = 'hello,Array,WORLD,';
    actual = '';
    obj = new ArrayCallback(false);
    strings.forEach(obj.concat, obj);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.forEach with object callback 2');  

  try
  {
    // test forEach on sparse arrays
    // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
    expect = 'sparse,';
    actual = '';
    sparsestrings.forEach(concat);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.forEach on sparse array');  
}

if ('filter' in Array.prototype)
{
// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter

  // test Array.filter

  // filter has 1 required argument
  expect = 1;
  actual = Array.prototype.filter.length;
  reportCompare(expect, actual, 'Array.prototype.filter.length == 1');

  // throw TypeError if no callback function specified
  expect = 'TypeError';
  try
  {
    strings.filter();
    actual = 'no error';
  }
  catch(e)
  {
    actual = e.name;
  }
  reportCompare(expect, actual, 'Array.filter(undefined) throws TypeError');  

  try
  {
    // test general filter
    expect = 'WORLD';
    actual = strings.filter(isUpperCase).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.filter');

  try
  {
    expect = 'WORLD';
    obj = new ArrayCallback(false);
    actual = strings.filter(obj.isUpperCase, obj).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.filter object callback 1');

  try
  {
    expect = 'hello,Array,WORLD';
    obj = new ArrayCallback(true);
    actual = strings.filter(obj.isUpperCase, obj).toString();
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'Array.filter object callback 2');
}

if ('every' in Array.prototype)
{
// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every

  // test Array.every

  // every has 1 required argument

  expect = 1;
  actual = Array.prototype.every.length;
  reportCompare(expect, actual, 'Array.prototype.every.length == 1');

  // throw TypeError if no every callback function specified
  expect = 'TypeError';
  try
  {
    strings.every();
    actual = 'no error';
  }
  catch(e)
  {
    actual = e.name;
  }
  reportCompare(expect, actual, 'Array.every(undefined) throws TypeError');  

  // test general every

  try
  {
    expect = true;
    actual = strings.every(isString);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'strings: every element is a string');

  try
  {
    expect = false;
    actual = mixed.every(isString);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'mixed: every element is a string');

  try
  {
    // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
    expect = true;
    actual = sparsestrings.every(isString);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'sparsestrings: every element is a string');

  // pass object method as map callback

  obj = new ArrayCallback(false);

  try
  {
    expect = true;
    actual = strings.every(obj.isString, obj);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'strings: every element is a string, via object callback');

  try
  {
    expect = false;
    actual = mixed.every(obj.isString, obj);
  }
  catch(e)
  {
    actual = dumpError(e) ;
  }
  reportCompare(expect, actual, 'mixed: every element is a string, via object callback');

  try
  {
    // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
    expect = true;
    actual = sparsestrings.every(obj.isString, obj);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'sparsestrings: every element is a string, via object callback');

}

if ('some' in Array.prototype)
{
// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some

  // test Array.some

  // some has 1 required argument

  expect = 1;
  actual = Array.prototype.some.length;
  reportCompare(expect, actual, 'Array.prototype.some.length == 1');

  // throw TypeError if no some callback function specified
  expect = 'TypeError';
  try
  {
    strings.some();
    actual = 'no error';
  }
  catch(e)
  {
    actual = e.name;
  }
  reportCompare(expect, actual, 'Array.some(undefined) throws TypeError');  

  // test general some

  try
  {
    expect = true;
    actual = strings.some(isString);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'strings: some element is a string');

  try
  {
    expect = true;
    actual = mixed.some(isString);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'mixed: some element is a string');

  try
  {
    expect = true;
    actual = sparsestrings.some(isString);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'sparsestrings: some element is a string');

  // pass object method as map callback

  obj = new ArrayCallback(false);

  try
  {
    expect = true;
    actual = strings.some(obj.isString, obj);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'strings: some element is a string, via object callback');

  try
  {
    expect = true;
    actual = mixed.some(obj.isString, obj);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'mixed: some element is a string, via object callback');

  try
  {
    expect = true;
    actual = sparsestrings.some(obj.isString, obj);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'sparsestrings: some element is a string, via object callback');

}

if ('indexOf' in Array.prototype)
{
// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf

  // test Array.indexOf

  // indexOf has 1 required argument

  expect = 1;
  actual = Array.prototype.indexOf.length;
  reportCompare(expect, actual, 'Array.prototype.indexOf.length == 1');

  // test general indexOf

  try
  {
    expect = -1;
    actual = mixed.indexOf('not found');
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'indexOf returns -1 if value not found');

  try
  {
    expect = 0;
    actual = mixed.indexOf(0);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'indexOf matches using strict equality');

  try
  {
    expect = 1;
    actual = mixed.indexOf('0');
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'indexOf matches using strict equality');

  try
  {
    expect = 2;
    actual = mixed.indexOf(0, 1);
  }
  catch(e)
  {
    actual = dumpError(e);
  }
  reportCompare(expect, actual, 'indexOf begins searching at fromIndex');
}