Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // Tests for non-standard array iteration functions.
     29 //
     30 // See
     31 //
     32 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array>
     33 //
     34 // for an explanation of each of the functions.
     35 
     36 //
     37 // Array.prototype.filter
     38 //
     39 (function() {
     40   // Simple use.
     41   var a = [0,1];
     42   assertArrayEquals([0], a.filter(function(n) { return n == 0; }));
     43   assertArrayEquals([0,1], a);
     44 
     45   // Use specified object as this object when calling the function.
     46   var o = { value: 42 }
     47   a = [1,42,3,42,4];
     48   assertArrayEquals([42,42], a.filter(function(n) { return this.value == n }, o))
     49 
     50   // Modify original array.
     51   a = [1,42,3,42,4];
     52   assertArrayEquals([42,42], a.filter(function(n, index, array) { array[index] = 43; return 42 == n; }));
     53   assertArrayEquals([43,43,43,43,43], a);
     54 
     55   // Only loop through initial part of array eventhough elements are
     56   // added.
     57   a = [1,1];
     58   assertArrayEquals([], a.filter(function(n, index, array) { array.push(n+1); return n == 2; }));
     59   assertArrayEquals([1,1,2,2], a);
     60 
     61   // Respect holes.
     62   a = new Array(20);
     63   var count = 0;
     64   a[2] = 2;
     65   a[15] = 2;
     66   a[17] = 4;
     67   var a = a.filter(function(n) { count++; return n == 2; });
     68   assertEquals(3, count);
     69   for (var i in a) assertEquals(2, a[i]);
     70 
     71 })();
     72 
     73 
     74 //
     75 // Array.prototype.forEach
     76 //
     77 (function() {
     78   // Simple use.
     79   var a = [0,1];
     80   var count = 0;
     81   a.forEach(function(n) { count++; });
     82   assertEquals(2, count);
     83 
     84   // Use specified object as this object when calling the function.
     85   var o = { value: 42 }
     86   var result = [];
     87   a.forEach(function(n) { result.push(this.value); }, o);
     88   assertArrayEquals([42,42], result);
     89 
     90   // Modify original array.
     91   a = [0,1];
     92   count = 0;
     93   a.forEach(function(n, index, array) { array[index] = n + 1; count++; });
     94   assertEquals(2, count);
     95   assertArrayEquals([1,2], a);
     96 
     97   // Only loop through initial part of array eventhough elements are
     98   // added.
     99   a = [1,1];
    100   count = 0;
    101   a.forEach(function(n, index, array) { array.push(n+1); count++; });
    102   assertEquals(2, count);
    103   assertArrayEquals([1,1,2,2], a);
    104 
    105   // Respect holes.
    106   a = new Array(20);
    107   count = 0;
    108   a[15] = 2;
    109   a.forEach(function(n) { count++; });
    110   assertEquals(1, count);
    111 
    112 })();
    113 
    114 
    115 //
    116 // Array.prototype.every
    117 //
    118 (function() {
    119   // Simple use.
    120   var a = [0,1];
    121   assertFalse(a.every(function(n) { return n == 0 }));
    122   a = [0,0];
    123   assertTrue(a.every(function(n) { return n == 0 }));
    124   assertTrue([].every(function(n) { return n == 0}));
    125 
    126   // Use specified object as this object when calling the function.
    127   var o = { value: 42 }
    128   a = [0];
    129   assertFalse(a.every(function(n) { return this.value == n; }, o));
    130   a = [42];
    131   assertTrue(a.every(function(n) { return this.value == n; }, o));
    132 
    133   // Modify original array.
    134   a = [0,1];
    135   assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n == 1;}));
    136   assertArrayEquals([1,1], a);
    137 
    138   // Only loop through initial part of array eventhough elements are
    139   // added.
    140   a = [1,1];
    141   assertTrue(a.every(function(n, index, array) { array.push(n + 1); return n == 1;}));
    142   assertArrayEquals([1,1,2,2], a);
    143 
    144   // Respect holes.
    145   a = new Array(20);
    146   var count = 0;
    147   a[2] = 2;
    148   a[15] = 2;
    149   assertTrue(a.every(function(n) { count++; return n == 2; }));
    150   assertEquals(2, count);
    151 
    152 })();
    153 
    154 //
    155 // Array.prototype.map
    156 //
    157 (function() {
    158   var a = [0,1,2,3,4];
    159 
    160   // Simple use.
    161   var result = [1,2,3,4,5];
    162   assertArrayEquals(result, a.map(function(n) { return n + 1; }));
    163   assertEquals(a, a);
    164 
    165   // Use specified object as this object when calling the function.
    166   var o = { delta: 42 }
    167   result = [42,43,44,45,46];
    168   assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o));
    169 
    170   // Modify original array.
    171   a = [0,1,2,3,4];
    172   result = [1,2,3,4,5];
    173   assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n + 1; return n + 1;}));
    174   assertArrayEquals(result, a);
    175 
    176   // Only loop through initial part of array eventhough elements are
    177   // added.
    178   a = [0,1,2,3,4];
    179   result = [1,2,3,4,5];
    180   assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); return n + 1;}));
    181   assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a);
    182 
    183   // Respect holes.
    184   a = new Array(20);
    185   a[15] = 2;
    186   a = a.map(function(n) { return 2*n; });
    187   for (var i in a) assertEquals(4, a[i]);
    188 
    189 })();
    190 
    191 //
    192 // Array.prototype.some
    193 //
    194 (function() {
    195   var a = [0,1,2,3,4];
    196 
    197   // Simple use.
    198   assertTrue(a.some(function(n) { return n == 3}));
    199   assertFalse(a.some(function(n) { return n == 5}));
    200 
    201   // Use specified object as this object when calling the function.
    202   var o = { element: 42 };
    203   a = [1,42,3];
    204   assertTrue(a.some(function(n) { return this.element == n; }, o));
    205   a = [1];
    206   assertFalse(a.some(function(n) { return this.element == n; }, o));
    207 
    208   // Modify original array.
    209   a = [0,1,2,3];
    210   assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n == 2; }));
    211   assertArrayEquals([1,2,3,3], a);
    212 
    213   // Only loop through initial part when elements are added.
    214   a = [0,1,2];
    215   assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42; }));
    216   assertArrayEquals([0,1,2,42,42,42], a);
    217 
    218   // Respect holes.
    219   a = new Array(20);
    220   var count = 0;
    221   a[2] = 42;
    222   a[10] = 2;
    223   a[15] = 42;
    224   assertTrue(a.some(function(n) { count++; return n == 2; }));
    225   assertEquals(2, count);
    226 
    227 })();
    228