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