* Added other tests
[cacao.git] / tests / regression / extest.java
1 public class extest {
2     final static int INDEX1 = 0xcafebabe;
3     final static int INDEX2 = 0xbabecafe;
4     final static int INDEX3 = 0xdeadbeef;
5
6     static boolean printStackTrace;
7     
8     public static void main(String[] argv) {
9         printStackTrace = true;
10
11 //      if (argv.length > 0) 
12 //              if (argv[0].equals("stacktrace"))
13 //                  printStackTrace = true;
14
15         boolean catched = false;
16
17         pln("---------- normal exceptions --------------------");
18
19         try {
20             p("throw new Exception():");
21             throw new Exception();
22         } catch (Exception e) {
23             catched = true;
24             ok();
25             pstacktrace(e);
26
27         } finally {
28             /* check if catch block was executed */
29             if (!catched) {
30                 failed();
31             }
32         }
33
34         try {
35             p("throw new Exception() (from subroutines):");
36             sub();
37             failed();
38         } catch (Exception e) {
39             ok();
40             pstacktrace(e);
41         }
42
43         try {
44             p("NullPointerException:");
45             int[] ia = null;
46             int i = ia.length;
47             failed();
48         } catch (NullPointerException e) {
49             ok();
50             pstacktrace(e);
51         }
52
53         pln();
54
55
56         pln("---------- test soft inline exceptions ----------");
57         pln("/* thrown twice to check the inline jump code */");
58
59         try {
60             p("ArithmeticException (only w/ -softnull):");
61             int i = 1, j = 0, k = i / j;
62             failed();
63         } catch (ArithmeticException e) {
64             String msg = e.getMessage();
65
66             if (msg == null || !msg.equals("/ by zero")) {
67                 pln("FAILED: wrong message: " + msg + ", should be: / by zero");
68                 pstacktrace(e);
69             } else {
70                 ok();
71                 pstacktrace(e);
72             }
73         }
74
75         try {
76             p("ArithmeticException (only w/ -softnull):");
77             long i = 1, j = 0, k = i / j;
78             failed();
79         } catch (ArithmeticException e) {
80             String msg = e.getMessage();
81
82             if (msg == null || !msg.equals("/ by zero")) {
83                 pln("FAILED: wrong message: " + msg + ", should be: / by zero");
84
85             } else {
86                 ok();
87                 pstacktrace(e);
88             }
89         }
90
91
92         try {
93             p("ArrayIndexOutOfBoundsException:");
94             int[] ia = new int[1];
95             ia[INDEX1] = 1;
96             failed();
97         } catch (ArrayIndexOutOfBoundsException e) {
98             String msg = e.getMessage();
99
100             if (msg == null || !msg.equals(String.valueOf(INDEX1))) {
101                 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX1);
102                 pstacktrace(e);
103             } else {
104                 ok();
105                 pstacktrace(e);
106             }
107         }
108
109         try {
110             p("ArrayIndexOutOfBoundsException:");
111             int[] ia = new int[1];
112             ia[INDEX2] = 1;
113             failed();
114         } catch (ArrayIndexOutOfBoundsException e) {
115             String msg = e.getMessage();
116
117             if (msg == null || !msg.equals(String.valueOf(INDEX2))) {
118                 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX2);
119                 pstacktrace(e);
120             } else {
121                 ok();
122                 pstacktrace(e);
123
124             }
125         }
126
127
128         try {
129             p("ArrayStoreException:");
130             Integer[] ia = new Integer[1];
131             Object[] oa = (Object[]) ia;
132             oa[0] = new Object();
133             failed();
134         } catch (ArrayStoreException e) {
135             ok();
136             pstacktrace(e);
137         }
138
139         try {
140             p("ArrayStoreException:");
141             Integer[] ia = new Integer[1];
142             Object[] oa = (Object[]) ia;
143             oa[0] = new Object();
144             failed();
145         } catch (ArrayStoreException e) {
146             ok();
147             pstacktrace(e);
148         }
149
150
151         try {
152             p("ClassCastException:");
153             Object o = new Object();
154             Integer i = (Integer) o;
155             failed();
156         } catch (ClassCastException e) {
157             ok();
158             pstacktrace(e);
159         }
160
161         try {
162             p("ClassCastException:");
163             Object o = new Object();
164             Integer i = null;
165             i = (Integer) o;
166             failed();
167         } catch (ClassCastException e) {
168             ok();
169             pstacktrace(e);
170         }
171
172
173         try {
174             p("NegativeArraySizeException:");
175             int[] ia = new int[-1];
176             failed();
177         } catch (NegativeArraySizeException e) {
178             ok();
179             pstacktrace(e);
180         }
181
182         try {
183             p("NegativeArraySizeException:");
184             int[] ia = new int[-1];
185             failed();
186         } catch (NegativeArraySizeException e) {
187             ok();
188             pstacktrace(e);
189         }
190
191         
192         try {
193             p("NullPointerException (only w/ -softnull):");
194             int[] ia = null;
195             int i = ia.length;
196             failed();
197         } catch (NullPointerException e) {
198             ok();
199             pstacktrace(e);
200         }
201
202         try {
203             p("NullPointerException (only w/ -softnull):");
204             int[] ia = null;
205             int i = ia.length;
206             failed();
207         } catch (NullPointerException e) {
208             ok();
209             pstacktrace(e);
210         }
211
212         try {
213             p("OutOfMemoryError:");
214             /* 100 MB should be enough */
215             byte[] ba = new byte[100 * 1024 * 1024];
216             failed();
217         } catch (OutOfMemoryError e) {
218             ok();
219             pstacktrace(e);
220         }
221
222         try {
223             p("OutOfMemoryError:");
224             /* 100 MB should be enough */
225             byte[] ba = new byte[100 * 1024 * 1024];
226             failed();
227         } catch (OutOfMemoryError e) {
228             ok();
229             pstacktrace(e);
230         }
231         
232         pln();
233
234
235         pln("---------- exceptions in leaf functions ---------");
236
237         try {
238             p("ArithmeticException:");
239             aesub(1, 0);
240             failed();
241         } catch (ArithmeticException e) {
242             ok();
243             pstacktrace(e);
244         }
245
246         try {
247             p("ArrayIndexOutOfBoundsException:");
248             aioobesub(new int[1]);
249             failed();
250         } catch (ArrayIndexOutOfBoundsException e) {
251             String msg = e.getMessage();
252
253             if (msg == null || !msg.equals(String.valueOf(INDEX3))) {
254                 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX3);
255                 pstacktrace(e);
256             } else {
257                 ok();
258                 pstacktrace(e);
259
260             }
261         }
262
263         try {
264             p("ClassCastException:");
265             ccesub(new Object(), new Integer(0));
266             failed();
267         } catch (ClassCastException e) {
268             ok();
269             pstacktrace(e);
270         }
271
272         try {
273             p("NullPointerException:");
274             npesub(null);
275             failed();
276         } catch (NullPointerException e) {
277             ok();
278             pstacktrace(e);
279         }
280
281         pln();
282
283
284         pln("---------- some asmpart exceptions --------------");
285
286         try {
287             p("NullPointerException in <clinit> (PUTSTATIC):");
288             extest_clinit_1.i = 1;
289             failed();
290         } catch (ExceptionInInitializerError e) {
291             if (e.getCause().getClass() != NullPointerException.class) {
292                 failed();
293             } else {
294                 ok();
295                 pstacktrace(e);
296             }
297         }
298
299         try {
300             p("NullPointerException in <clinit> (GETSTATIC):");
301             int i = extest_clinit_2.i;
302             failed();
303         } catch (ExceptionInInitializerError e) {
304             if (e.getCause().getClass() != NullPointerException.class) {
305                 failed();
306             } else {
307                 ok();
308                 pstacktrace(e);
309             }
310         }
311
312         pln();
313
314
315         pln("---------- exception related things -------------");
316
317         try {
318             p("load/link an exception class in asmpart:");
319             throw new Exception();
320         } catch (UnknownError e) {
321             /* this exception class MUST NOT be loaded before!!!
322                otherwise this test in useless */
323         } catch (Exception e) {
324             ok();
325             pstacktrace(e);
326         }
327
328         pln();
329
330
331         pln("---------- native stub exceptions ---------------");
332
333         try {
334             p("NullPointerException (native):");
335             System.arraycopy(null, 1, null, 1, 1);
336             failed();
337         } catch (Exception e) {
338             ok();
339             pstacktrace(e);
340         }
341
342         try {
343             p("NullPointerException in <clinit>:");
344             extest_clinit_3.sub();
345             failed();
346         } catch (ExceptionInInitializerError e) {
347             ok();
348             pstacktrace(e);
349         } catch (UnsatisfiedLinkError e) {
350             /* catch this one for staticvm and say it's ok */
351             ok();
352             pstacktrace(e);
353         }
354
355         /*
356         try {
357             p("UnsatisfiedLinkError:");
358             nsub();
359             failed();
360         } catch (UnsatisfiedLinkError e) {
361             ok();
362             pstacktrace(e);
363         }
364         */
365
366         pln();
367
368
369         pln("---------- no OK beyond this point --------------");
370
371         pln("NullPointerException (without catch):");
372         String s = null;
373         int i = s.length();
374         failed();
375     }
376
377     synchronized static void sub() throws Exception {
378         sub2();
379     }
380
381     static void sub2() throws Exception {
382         sub3();
383     }
384
385     synchronized static void sub3() throws Exception {
386         sub4();
387     }
388
389     static void sub4() throws Exception {
390         throw new Exception();
391     }
392
393     static void aesub(int a, int b) {
394         int c = a / b;
395     }
396
397     static void aioobesub(int[] ia) {
398         ia[INDEX3] = 0;
399     }
400
401     static void ccesub(Object o, Integer i) {
402         i = (Integer) o;
403     }
404
405     static void npesub(int[] ia) {
406         int a = ia.length;
407     }
408
409     static native void nsub();
410
411     static void p(String s) {
412         System.out.print(s);
413         for (int i = s.length(); i < 46; i++) {
414             System.out.print(" ");
415         }
416     }
417
418     static void pln() {
419         System.out.println();
420     }
421
422     static void pln(String s) {
423         System.out.println(s);
424     }
425
426     static void ok() {
427         pln("OK");
428     }
429
430     static void failed() {
431         pln("FAILED");
432     }
433
434     static void pstacktrace(Throwable e) {
435         if (!printStackTrace)
436             return;
437         e.printStackTrace();
438         System.out.println();
439     }
440 }
441
442 public class extest_clinit_1 {
443     static {
444         String s = null;
445         s.length();
446     }
447
448     public static int i;
449 }
450
451 public class extest_clinit_2 {
452     static {
453         String s = null;
454         s.length();
455     }
456
457     public static int i;
458 }
459
460 public class extest_clinit_3 {
461     static {
462         String s = null;
463         s.length();
464     }
465
466     public static native void sub();
467 }