Home | History | Annotate | Download | only in webkit
      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 
     24 description("Tests for Array.prototype.filter");
     25 
     26 function passUndefined(element, index, array) {
     27     return typeof element === "undefined";
     28 }
     29 function passEven(a) {
     30     return !(a & 1);
     31 }
     32 function passAfter5(element, index) {
     33     return index >= 5;
     34 }
     35 var sparseArrayLength = 10100;
     36 mixPartialAndFast = new Array(sparseArrayLength);
     37 mixPartialAndFast[sparseArrayLength - 1] = sparseArrayLength - 1;
     38 for(var i = 0; i < 10; i++)
     39     mixPartialAndFast[i] = i;
     40 function 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 }
     48 function 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 }
     59 function 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 
     70 shouldBe("[undefined].filter(passUndefined)", "[undefined]");
     71 shouldBe("(new Array(20)).filter(passUndefined)", "[]");
     72 shouldBe("[0,1,2,3,4,5,6,7,8,9].filter(passEven)", "[0,2,4,6,8]");
     73 shouldBe("[0,1,2,3,4,5,6,7,8,9].filter(passAfter5)", "[5,6,7,8,9]");
     74 shouldBe("mixPartialAndFast.filter(passAfter5)", "[5,6,7,8,9,sparseArrayLength-1]");
     75 
     76 // Generic Object
     77 shouldBe("toObject([undefined]).filter(passUndefined)", "[undefined]");
     78 shouldBe("toObject(new Array(20)).filter(passUndefined)", "[]");
     79 shouldBe("toObject([0,1,2,3,4,5,6,7,8,9]).filter(passEven)", "[0,2,4,6,8]");
     80 shouldBe("toObject([0,1,2,3,4,5,6,7,8,9]).filter(passAfter5)", "[5,6,7,8,9]");
     81 shouldBe("toObject(mixPartialAndFast).filter(passAfter5)", "[5,6,7,8,9,sparseArrayLength-1]");
     82 
     83 // Reversed generic Object
     84 shouldBe("reverseInsertionOrder([undefined]).filter(passUndefined)", "[undefined]");
     85 shouldBe("reverseInsertionOrder(new Array(20)).filter(passUndefined)", "[]");
     86 shouldBe("reverseInsertionOrder([0,1,2,3,4,5,6,7,8,9]).filter(passEven)", "[0,2,4,6,8]");
     87 shouldBe("reverseInsertionOrder([0,1,2,3,4,5,6,7,8,9]).filter(passAfter5)", "[5,6,7,8,9]");
     88 shouldBe("reverseInsertionOrder(mixPartialAndFast).filter(passAfter5)", "[5,6,7,8,9,sparseArrayLength-1]");
     89 
     90 // Log evaluation order
     91 shouldBe("reverseInsertionOrder([undefined]).filter(filterLog(passUndefined))", "[undefined]");
     92 shouldBe("reverseInsertionOrder(new Array(20)).filter(filterLog(passUndefined))", "[]");
     93 shouldBe("reverseInsertionOrder([0,1,2,3,4]).filter(filterLog(passEven))", "[0,2,4]");
     94 shouldBe("reverseInsertionOrder(mixPartialAndFast).filter(filterLog(passAfter5))", "[5,6,7,8,9,sparseArrayLength-1]");
     95 shouldBe("([undefined]).filter(filterLog(passUndefined))", "[undefined]");
     96 shouldBe("(new Array(20)).filter(filterLog(passUndefined))", "[]");
     97 shouldBe("([0,1,2,3,4]).filter(filterLog(passEven))", "[0,2,4]");
     98 shouldBe("(mixPartialAndFast).filter(filterLog(passAfter5))", "[5,6,7,8,9,sparseArrayLength-1]");
     99 
    100 shouldBe("[1,2,3].filter(function(i,j,k,l,m){ return m=!m; })", "[1,2,3]")
    101