Home | History | Annotate | Download | only in compiler
      1 // Copyright 2010 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 function Get0(a) {
     29   return a[0];
     30 }
     31 
     32 function GetN(a,n) {
     33   return a[n];
     34 }
     35 
     36 function GetA0(a) {
     37   return a[a[0]];
     38 }
     39 
     40 function GetAN(a,n) {
     41   return a[a[n]];
     42 }
     43 
     44 function GetAAN(a,n) {
     45   return a[a[a[n]]];
     46 }
     47 
     48 function RunGetTests() {
     49   var a = [2,0,1];
     50   assertEquals(2, Get0(a));
     51 
     52   assertEquals(2, GetN(a, 0));
     53   assertEquals(0, GetN(a, 1));
     54   assertEquals(1, GetN(a, 2));
     55 
     56   assertEquals(1, GetA0(a));
     57 
     58   assertEquals(1, GetAN(a,0));
     59   assertEquals(2, GetAN(a,1));
     60   assertEquals(0, GetAN(a,2));
     61 
     62   assertEquals(0, GetAAN(a,0));
     63   assertEquals(1, GetAAN(a,1));
     64   assertEquals(2, GetAAN(a,2));
     65 }
     66 
     67 
     68 function Set07(a) {
     69   a[0] = 7;
     70 }
     71 
     72 function Set0V(a, v) {
     73   a[0] = v;
     74 }
     75 
     76 function SetN7(a, n) {
     77   a[n] = 7;
     78 }
     79 
     80 function SetNX(a, n, x) {
     81   a[n] = x;
     82 }
     83 
     84 function RunSetTests(a) {
     85   Set07(a);
     86   assertEquals(7, a[0]);
     87   assertEquals(0, a[1]);
     88   assertEquals(0, a[2]);
     89 
     90   Set0V(a, 1);
     91   assertEquals(1, a[0]);
     92   assertEquals(0, a[1]);
     93   assertEquals(0, a[2]);
     94 
     95   SetN7(a, 2);
     96   assertEquals(1, a[0]);
     97   assertEquals(0, a[1]);
     98   assertEquals(7, a[2]);
     99 
    100   SetNX(a, 1, 5);
    101   assertEquals(1, a[0]);
    102   assertEquals(5, a[1]);
    103   assertEquals(7, a[2]);
    104 
    105   for (var i = 0; i < 3; i++) SetNX(a, i, 0);
    106   assertEquals(0, a[0]);
    107   assertEquals(0, a[1]);
    108   assertEquals(0, a[2]);
    109 }
    110 
    111 function RunArrayBoundsCheckTest() {
    112   var g = [1,2,3];
    113 
    114   function f(a, i) { a[i] = 42; }
    115 
    116   for (var i = 0; i < 100000; i++) { f(g, 0); }
    117 
    118   f(g, 4);
    119 
    120   assertEquals(42, g[0]);
    121   assertEquals(42, g[4]);
    122 }
    123 
    124 var a = [0,0,0];
    125 var o = {0: 0, 1: 0, 2: 0};
    126 for (var i = 0; i < 1000; i++) {
    127   RunGetTests();
    128   RunSetTests(a);
    129   RunSetTests(o);
    130 }
    131 
    132 RunArrayBoundsCheckTest();
    133