Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 // Simple test for array accesses.
     18 
     19 public class Main extends TestCase {
     20   public static void main(String[] args) {
     21     $opt$testReads(new boolean[1], new byte[1], new char[1], new short[1],
     22                    new int[1], new Object[1], new long[1], new float[1], new double[1], 0);
     23     $opt$testWrites(new boolean[2], new byte[2], new char[2], new short[2],
     24                     new int[2], new Object[2], new long[2], new float[2], new double[2], 1);
     25     ensureThrows(new boolean[2], 2);
     26     ensureThrows(new boolean[2], 4);
     27     ensureThrows(new boolean[2], -1);
     28     ensureThrows(new boolean[2], Integer.MIN_VALUE);
     29     ensureThrows(new boolean[2], Integer.MAX_VALUE);
     30   }
     31 
     32   static void $opt$testReads(boolean[] bools, byte[] bytes, char[] chars, short[] shorts,
     33                              int[] ints, Object[] objects, long[] longs, float[] floats,
     34                              double[] doubles, int index) {
     35     assertEquals(false, bools[0]);
     36     assertEquals(false, bools[index]);
     37 
     38     assertEquals(0, bytes[0]);
     39     assertEquals(0, bytes[index]);
     40 
     41     assertEquals(0, chars[0]);
     42     assertEquals(0, chars[index]);
     43 
     44     assertEquals(0, shorts[0]);
     45     assertEquals(0, shorts[index]);
     46 
     47     assertEquals(0, ints[0]);
     48     assertEquals(0, ints[index]);
     49 
     50     assertNull(objects[0]);
     51     assertNull(objects[index]);
     52 
     53     assertEquals(0, longs[0]);
     54     assertEquals(0, longs[index]);
     55 
     56     assertEquals(0, floats[0]);
     57     assertEquals(0, floats[index]);
     58 
     59     assertEquals(0, doubles[0]);
     60     assertEquals(0, doubles[index]);
     61   }
     62 
     63   static void $opt$testWrites(boolean[] bools, byte[] bytes, char[] chars, short[] shorts,
     64                               int[] ints, Object[] objects, long[] longs, float[] floats,
     65                               double doubles[], int index) {
     66     bools[0] = true;
     67     assertEquals(true, bools[0]);
     68     bools[index] = true;
     69     assertEquals(true, bools[index]);
     70 
     71     bytes[0] = -4;
     72     assertEquals(-4, bytes[0]);
     73     bytes[index] = -8;
     74     assertEquals(-8, bytes[index]);
     75 
     76     chars[0] = 'c';
     77     assertEquals('c', chars[0]);
     78     chars[index] = 'd';
     79     assertEquals('d', chars[index]);
     80 
     81     chars[0] = 65535;
     82     assertEquals(65535, chars[0]);
     83     // Do an update between the two max value updates, to avoid
     84     // optimizing the second away.
     85     chars[index] = 0;
     86     assertEquals(0, chars[index]);
     87     chars[index] = 65535;
     88     assertEquals(65535, chars[index]);
     89 
     90     shorts[0] = -42;
     91     assertEquals(-42, shorts[0]);
     92     shorts[index] = -84;
     93     assertEquals(-84, shorts[index]);
     94 
     95     ints[0] = -32;
     96     assertEquals(-32, ints[0]);
     97     ints[index] = -64;
     98     assertEquals(-64, ints[index]);
     99 
    100     Object o1 = new Object();
    101     objects[0] = o1;
    102     assertEquals(o1, objects[0]);
    103     Object o2 = new Object();
    104     objects[index] = o2;
    105     assertEquals(o2, objects[index]);
    106     // Longs are initially not supported in the linear scan register allocator
    107     // on 32bits. So we call out a long helper to ensure this method gets
    108     // optimized.
    109     $opt$testLongWrites(longs, index);
    110 
    111     float f = 1.0F;
    112     floats[0] = f / (1 | 0);
    113     assertEquals(f, floats[0]);
    114     floats[index] = f / (1 | 0);
    115     assertEquals(f, floats[index]);
    116 
    117     double d = 1.0F;
    118     doubles[0] = d / (1 | 0);
    119     assertEquals(d, doubles[0]);
    120     doubles[index] = d / (1 | 0);
    121     assertEquals(d, doubles[index]);
    122   }
    123 
    124   public static void $opt$testLongWrites(long[] longs, int index) {
    125     long l = -21876876876876876L;
    126     longs[0] = l;
    127     assertEquals(l, longs[0]);
    128     l = -21876876876876877L;
    129     longs[index] = l;
    130     assertEquals(l, longs[index]);
    131   }
    132 
    133   public static void ensureThrows(boolean[] array, int index) {
    134     ArrayIndexOutOfBoundsException exception = null;
    135     try {
    136       $opt$doArrayLoad(array, index);
    137     } catch (ArrayIndexOutOfBoundsException e) {
    138       exception = e;
    139     }
    140 
    141     assertNotNull(exception);
    142     assertTrue(exception.toString().contains(Integer.toString(index)));
    143 
    144     exception = null;
    145     try {
    146       $opt$doArrayStore(array, index);
    147     } catch (ArrayIndexOutOfBoundsException e) {
    148       exception = e;
    149     }
    150 
    151     assertNotNull(exception);
    152     assertTrue(exception.toString().contains(Integer.toString(index)));
    153   }
    154 
    155   public static void $opt$doArrayLoad(boolean[] array, int index) {
    156     boolean res = array[index];
    157   }
    158 
    159   public static void $opt$doArrayStore(boolean[] array, int index) {
    160     array[index] = false;
    161   }
    162 }
    163