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], 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], 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, int index) {
     34     assertEquals(false, bools[0]);
     35     assertEquals(false, bools[index]);
     36 
     37     assertEquals(0, bytes[0]);
     38     assertEquals(0, bytes[index]);
     39 
     40     assertEquals(0, chars[0]);
     41     assertEquals(0, chars[index]);
     42 
     43     assertEquals(0, shorts[0]);
     44     assertEquals(0, shorts[index]);
     45 
     46     assertEquals(0, ints[0]);
     47     assertEquals(0, ints[index]);
     48 
     49     assertNull(objects[0]);
     50     assertNull(objects[index]);
     51 
     52     assertEquals(0, longs[0]);
     53     assertEquals(0, longs[index]);
     54   }
     55 
     56   static void $opt$testWrites(boolean[] bools, byte[] bytes, char[] chars, short[] shorts,
     57                               int[] ints, Object[] objects, long[] longs, int index) {
     58     bools[0] = true;
     59     assertEquals(true, bools[0]);
     60     bools[1] = true;
     61     assertEquals(true, bools[index]);
     62 
     63     bytes[0] = -4;
     64     assertEquals(-4, bytes[0]);
     65     bytes[index] = -8;
     66     assertEquals(-8, bytes[index]);
     67 
     68     chars[0] = 'c';
     69     assertEquals('c', chars[0]);
     70     chars[index] = 'd';
     71     assertEquals('d', chars[index]);
     72 
     73     shorts[0] = -42;
     74     assertEquals(-42, shorts[0]);
     75     shorts[index] = -84;
     76     assertEquals(-84, shorts[index]);
     77 
     78     ints[0] = -32;
     79     assertEquals(-32, ints[0]);
     80     ints[index] = -64;
     81     assertEquals(-64, ints[index]);
     82 
     83     Object o1 = new Object();
     84     objects[0] = o1;
     85     assertEquals(o1, objects[0]);
     86     Object o2 = new Object();
     87     objects[index] = o2;
     88     assertEquals(o2, objects[index]);
     89 
     90     long l = -21876876876876876L;
     91     longs[0] = l;
     92     assertEquals(l, longs[0]);
     93     l = -21876876876876877L;
     94     longs[index] = l;
     95     assertEquals(l, longs[index]);
     96   }
     97 
     98   public static void ensureThrows(boolean[] array, int index) {
     99     ArrayIndexOutOfBoundsException exception = null;
    100     try {
    101       $opt$doArrayLoad(array, index);
    102     } catch (ArrayIndexOutOfBoundsException e) {
    103       exception = e;
    104     }
    105 
    106     assertNotNull(exception);
    107     assertTrue(exception.toString().contains(Integer.toString(index)));
    108 
    109     exception = null;
    110     try {
    111       $opt$doArrayStore(array, index);
    112     } catch (ArrayIndexOutOfBoundsException e) {
    113       exception = e;
    114     }
    115 
    116     assertNotNull(exception);
    117     assertTrue(exception.toString().contains(Integer.toString(index)));
    118   }
    119 
    120   public static void $opt$doArrayLoad(boolean[] array, int index) {
    121     boolean res = array[index];
    122   }
    123 
    124   public static void $opt$doArrayStore(boolean[] array, int index) {
    125     array[index] = false;
    126   }
    127 }
    128