1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description("Tests for Array.prototype.filter");
25
26function passUndefined(element, index, array) {
27    return typeof element === "undefined";
28}
29function passEven(a) {
30    return !(a & 1);
31}
32function passAfter5(element, index) {
33    return index >= 5;
34}
35var sparseArrayLength = 10100;
36mixPartialAndFast = new Array(sparseArrayLength);
37mixPartialAndFast[sparseArrayLength - 1] = sparseArrayLength - 1;
38for(var i = 0; i < 10; i++)
39    mixPartialAndFast[i] = i;
40function toObject(array) {
41    var result = {};
42    result.length = array.length;
43    for (var i in array)
44        result[i] = array[i];
45    result.filter=Array.prototype.filter;
46    return result;
47}
48function reverseInsertionOrder(array) {
49    var obj = toObject(array);
50    var props = [];
51    for (var i in obj)
52        props.push(i);
53    var result = {};
54    for (var i = props.length - 1; i >= 0; i--)
55        result[props[i]] = obj[props[i]];
56    result.filter=Array.prototype.filter;
57    return result;
58}
59function filterLog(f) {
60    return function(i,j) {
61        try {
62        debug([i,j,arguments[2].toString().substring(0,20)].toString());
63        return f.apply(this, arguments);
64        } catch(e) {
65            console.error(e);
66        }
67    }
68}
69
70shouldBe("[undefined].filter(passUndefined)", "[undefined]");
71shouldBe("(new Array(20)).filter(passUndefined)", "[]");
72shouldBe("[0,1,2,3,4,5,6,7,8,9].filter(passEven)", "[0,2,4,6,8]");
73shouldBe("[0,1,2,3,4,5,6,7,8,9].filter(passAfter5)", "[5,6,7,8,9]");
74shouldBe("mixPartialAndFast.filter(passAfter5)", "[5,6,7,8,9,sparseArrayLength-1]");
75
76// Generic Object
77shouldBe("toObject([undefined]).filter(passUndefined)", "[undefined]");
78shouldBe("toObject(new Array(20)).filter(passUndefined)", "[]");
79shouldBe("toObject([0,1,2,3,4,5,6,7,8,9]).filter(passEven)", "[0,2,4,6,8]");
80shouldBe("toObject([0,1,2,3,4,5,6,7,8,9]).filter(passAfter5)", "[5,6,7,8,9]");
81shouldBe("toObject(mixPartialAndFast).filter(passAfter5)", "[5,6,7,8,9,sparseArrayLength-1]");
82
83// Reversed generic Object
84shouldBe("reverseInsertionOrder([undefined]).filter(passUndefined)", "[undefined]");
85shouldBe("reverseInsertionOrder(new Array(20)).filter(passUndefined)", "[]");
86shouldBe("reverseInsertionOrder([0,1,2,3,4,5,6,7,8,9]).filter(passEven)", "[0,2,4,6,8]");
87shouldBe("reverseInsertionOrder([0,1,2,3,4,5,6,7,8,9]).filter(passAfter5)", "[5,6,7,8,9]");
88shouldBe("reverseInsertionOrder(mixPartialAndFast).filter(passAfter5)", "[5,6,7,8,9,sparseArrayLength-1]");
89
90// Log evaluation order
91shouldBe("reverseInsertionOrder([undefined]).filter(filterLog(passUndefined))", "[undefined]");
92shouldBe("reverseInsertionOrder(new Array(20)).filter(filterLog(passUndefined))", "[]");
93shouldBe("reverseInsertionOrder([0,1,2,3,4]).filter(filterLog(passEven))", "[0,2,4]");
94shouldBe("reverseInsertionOrder(mixPartialAndFast).filter(filterLog(passAfter5))", "[5,6,7,8,9,sparseArrayLength-1]");
95shouldBe("([undefined]).filter(filterLog(passUndefined))", "[undefined]");
96shouldBe("(new Array(20)).filter(filterLog(passUndefined))", "[]");
97shouldBe("([0,1,2,3,4]).filter(filterLog(passEven))", "[0,2,4]");
98shouldBe("(mixPartialAndFast).filter(filterLog(passAfter5))", "[5,6,7,8,9,sparseArrayLength-1]");
99
100shouldBe("[1,2,3].filter(function(i,j,k,l,m){ return m=!m; })", "[1,2,3]")
101