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