Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2011 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 class Main {
     18 
     19     public int ifoo;
     20 
     21     /* Test requires visual inspection of object code to verify */
     22     int noThrow(Main nonNullA,
     23                 Main nonNullB,
     24                 Main nonNullC) {
     25 
     26         // "this" check should be eliminated on both IGET/IPUT
     27         ifoo++;
     28 
     29        // "this" check should be eliminated on both IGET/IPUT
     30        if (ifoo != 321) {
     31            // Check not eliminated
     32            nonNullA.ifoo = 12;
     33            // Check not eliminated
     34            nonNullB.ifoo = 21;
     35        } else {
     36            // Check not eliminated
     37            nonNullA.ifoo = 12;
     38        }
     39 
     40        // Check eliminated
     41        nonNullA.ifoo = 13;
     42 
     43        // Check not eliminated
     44        nonNullB.ifoo = 21;
     45 
     46        nonNullC = nonNullB;
     47 
     48        // Check eliminated
     49        nonNullC.ifoo = 32;
     50 
     51       // All null checks eliminated
     52       return ifoo + nonNullA.ifoo + nonNullB.ifoo + nonNullC.ifoo;
     53     }
     54 
     55     /* Test to ensure we don't remove necessary null checks */
     56     int checkThrow(Main nonNullA,
     57                    Main nonNullB,
     58                    Main nonNullC,
     59                    Main nullA,
     60                    Main nullB,
     61                    Main nullC) {
     62 
     63         // "this" check should be eliminated on both IGET/IPUT
     64         ifoo++;
     65 
     66        try {
     67            nullA.ifoo = 12;
     68            // Should not be reached
     69            return -1;
     70        } catch (NullPointerException npe) {
     71            ifoo++;
     72        }
     73        try {
     74            nullB.ifoo = 13;
     75            // Should not be reached
     76            return -2;
     77        } catch (NullPointerException npe) {
     78            ifoo++;
     79        }
     80        try {
     81            nullC.ifoo = 14;
     82            // Should not be reached
     83            return -3;
     84        } catch (NullPointerException npe) {
     85            ifoo++;
     86        }
     87 
     88        // "this" check should be eliminated
     89        if (ifoo != 321) {
     90            // Check not eliminated
     91            nonNullA.ifoo = 12;
     92            // Check not eliminated
     93            nonNullB.ifoo = 21;
     94            // Should throw here
     95            try {
     96                nullA.ifoo = 11;
     97                return -4;
     98            } catch (NullPointerException npe) {
     99            }
    100        } else {
    101            // Check not eliminated
    102            nonNullA.ifoo = 12;
    103            // Should throw here
    104            try {
    105                nullA.ifoo = 11;
    106                return -5;
    107            } catch (NullPointerException npe) {
    108            }
    109        }
    110 
    111        // Check not eliminated
    112        nonNullA.ifoo = 13;
    113 
    114        // Check not eliminated
    115        nonNullB.ifoo = 21;
    116 
    117        nonNullC = nonNullB;
    118 
    119        // Check eliminated
    120        nonNullC.ifoo = 32;
    121 
    122        // Should throw here
    123        try {
    124            nullA.ifoo = 13;
    125            return -6;
    126        } catch (NullPointerException npe) {
    127        }
    128 
    129       return ifoo + nonNullA.ifoo + nonNullB.ifoo + nonNullC.ifoo;
    130     }
    131 
    132 
    133     static int nullCheckTestNoThrow(int x) {
    134         Main base = new Main();
    135         Main a = new Main();
    136         Main b = new Main();
    137         Main c = new Main();
    138         base.ifoo = x;
    139         return base.noThrow(a,b,c);
    140     }
    141 
    142     static int nullCheckTestThrow(int x) {
    143         Main base = new Main();
    144         Main a = new Main();
    145         Main b = new Main();
    146         Main c = new Main();
    147         Main d = null;
    148         Main e = null;
    149         Main f = null;
    150         base.ifoo = x;
    151         return base.checkThrow(a,b,c,d,e,f);
    152     }
    153 
    154 
    155     static void throwImplicitAIOBE(int[] array, int index) {
    156       array[index] = 0;
    157     }
    158 
    159     static int checkAIOBE() {
    160       int[] array = new int[10];
    161       int res;
    162       try {
    163         throwImplicitAIOBE(array, 11);
    164         res = 123;
    165       } catch (NullPointerException npe) {
    166         res = 768;
    167       } catch (ArrayIndexOutOfBoundsException e) {
    168         res = 456;
    169       }
    170       try {
    171         throwImplicitAIOBE(array, -1);
    172         res += 123;
    173       } catch (NullPointerException npe) {
    174         res += 768;
    175       } catch (ArrayIndexOutOfBoundsException e) {
    176         res += 456;
    177       }
    178       return res;
    179     }
    180 
    181     static int throwImplicitDivZero(int x, int y) {
    182       return x / y;
    183     }
    184 
    185     static int checkDivZero() {
    186       try {
    187         throwImplicitDivZero(100, 0);
    188         return 123;
    189       } catch (NullPointerException npe) {
    190         return 768;
    191       } catch (ArrayIndexOutOfBoundsException e) {
    192         return 987;
    193       } catch (ArithmeticException e) {
    194         return 456;
    195       }
    196     }
    197 
    198     public static void main(String[] args) {
    199         boolean failure = false;
    200         int res;
    201 
    202         res = nullCheckTestNoThrow(1976);
    203         if (res == 2054) {
    204             System.out.println("nullCheckTestNoThrow PASSED");
    205         } else {
    206             System.out.println("nullCheckTestNoThrow FAILED: " + res);
    207             failure = true;
    208         }
    209 
    210         res = nullCheckTestThrow(1976);
    211         if (res == 2057) {
    212             System.out.println("nullCheckTestThrow PASSED");
    213         } else {
    214             System.out.println("nullCheckTestThrow FAILED: " + res);
    215             failure = true;
    216         }
    217 
    218         res = checkAIOBE();
    219         if (res == 912) {
    220           System.out.println("checkAIOBE PASSED");
    221         } else {
    222           System.out.println("checkAIOBE FAILED: " + res);
    223           failure = true;
    224         }
    225 
    226         res = checkDivZero();
    227         if (res == 456) {
    228           System.out.println("checkDivZero PASSED");
    229         } else {
    230           System.out.println("checkDivZero FAILED: " + res);
    231           failure = true;
    232         }
    233         System.exit(failure ? 1 : 0);
    234     }
    235 }
    236