blob: 3edae6d72c42277a6029514b8ca74c02229c6bd5 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
buzbee43a36422011-09-14 14:00:13 -070016
17class ExceptionTest {
18
19 public int ifoo;
20
21 /* Test requires visual inspection of object code to verify */
22 int noThrow(ExceptionTest nonNullA,
23 ExceptionTest nonNullB,
24 ExceptionTest 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(ExceptionTest nonNullA,
57 ExceptionTest nonNullB,
58 ExceptionTest nonNullC,
59 ExceptionTest nullA,
60 ExceptionTest nullB,
61 ExceptionTest 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 ExceptionTest base = new ExceptionTest();
135 ExceptionTest a = new ExceptionTest();
136 ExceptionTest b = new ExceptionTest();
137 ExceptionTest c = new ExceptionTest();
138 base.ifoo = x;
139 return base.noThrow(a,b,c);
140 }
141
142 static int nullCheckTestThrow(int x) {
143 ExceptionTest base = new ExceptionTest();
144 ExceptionTest a = new ExceptionTest();
145 ExceptionTest b = new ExceptionTest();
146 ExceptionTest c = new ExceptionTest();
147 ExceptionTest d = null;
148 ExceptionTest e = null;
149 ExceptionTest f = null;
150 base.ifoo = x;
151 return base.checkThrow(a,b,c,d,e,f);
152 }
153
154
Ian Rogers9651f422011-09-19 20:26:07 -0700155 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
buzbee43a36422011-09-14 14:00:13 -0700198 public static void main(String[] args) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700199 boolean failure = false;
buzbee43a36422011-09-14 14:00:13 -0700200 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);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700207 failure = true;
buzbee43a36422011-09-14 14:00:13 -0700208 }
209
210 res = nullCheckTestThrow(1976);
211 if (res == 2057) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700212 System.out.println("nullCheckTestThrow PASSED");
buzbee43a36422011-09-14 14:00:13 -0700213 } else {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700214 System.out.println("nullCheckTestThrow FAILED: " + res);
215 failure = true;
buzbee43a36422011-09-14 14:00:13 -0700216 }
Ian Rogers9651f422011-09-19 20:26:07 -0700217
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 }
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700233 System.exit(failure ? 1 : 0);
buzbee43a36422011-09-14 14:00:13 -0700234 }
235}