Regression test driver now can read different expected output files
[cacao.git] / tests / regression / extest.java
1 public class extest {
2     final static int COLUMN = 55;
3
4     final static int INDEX1 = 0xcafebabe;
5     final static int INDEX2 = 0xbabecafe;
6     final static int INDEX3 = 0xdeadbeef;
7
8     static boolean printStackTrace;
9     
10     public static void main(String[] argv) {
11         printStackTrace = true;
12
13         Runtime r = Runtime.getRuntime();
14         int maxmem = (int) r.maxMemory();
15
16 //      if (argv.length > 0) 
17 //              if (argv[0].equals("stacktrace"))
18 //                  printStackTrace = true;
19
20         boolean caught = false;
21
22         pheader("normal exceptions");
23
24         try {
25             p("throw new Exception():");
26             throw new Exception();
27         } catch (Exception e) {
28             caught = true;
29             ok();
30             pstacktrace(e);
31         } finally {
32             /* check if catch block was executed */
33             if (!caught) {
34                 failed();
35             }
36         }
37
38         try {
39             p("throw new Exception() (from subroutines):");
40             sub();
41             failed();
42         } catch (Exception e) {
43             ok();
44             pstacktrace(e);
45         }
46
47         try {
48             p("NullPointerException:");
49             int[] ia = null;
50             int i = ia.length;
51             failed();
52         } catch (NullPointerException e) {
53             ok();
54             pstacktrace(e);
55         }
56
57         pln();
58
59
60         pheader("exceptions thrown in JIT code");
61
62         try {
63             p("ArithmeticException (only w/ -softnull):");
64             int i = 1, j = 0, k = i / j;
65             failed();
66         } catch (ArithmeticException e) {
67             String msg = e.getMessage();
68
69             if (msg == null || !msg.equals("/ by zero")) {
70                 pln("FAILED: wrong message: " + msg + ", should be: / by zero");
71                 pstacktrace(e);
72             } else {
73                 ok();
74                 pstacktrace(e);
75             }
76         }
77
78         try {
79             p("ArrayIndexOutOfBoundsException:");
80             int[] ia = new int[1];
81             ia[INDEX1] = 1;
82             failed();
83         } catch (ArrayIndexOutOfBoundsException e) {
84             String msg = e.getMessage();
85
86             if (msg == null || !msg.equals(String.valueOf(INDEX1))) {
87                 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX1);
88                 pstacktrace(e);
89             } else {
90                 ok();
91                 pstacktrace(e);
92             }
93         }
94
95         try {
96             p("ArrayStoreException:");
97             Integer[] ia = new Integer[1];
98             Object[] oa = (Object[]) ia;
99             oa[0] = new Object();
100             failed();
101         } catch (ArrayStoreException e) {
102             ok();
103             pstacktrace(e);
104         }
105
106         try {
107             p("ClassCastException:");
108             Object o = new Object();
109             Integer i = (Integer) o;
110             failed();
111         } catch (ClassCastException e) {
112             ok();
113             pstacktrace(e);
114         }
115
116         try {
117             p("NegativeArraySizeException (newarray):");
118             int[] ia = new int[-1];
119             failed();
120         } catch (NegativeArraySizeException e) {
121             ok();
122             pstacktrace(e);
123         }
124
125         try {
126             p("NegativeArraySizeException (multianewarray):");
127             int[][] ia = new int[1][-1];
128             failed();
129         } catch (NegativeArraySizeException e) {
130             ok();
131             pstacktrace(e);
132         }
133
134         try {
135             p("OutOfMemoryError:");
136             /* maxmem + 1 should be enough and hopefully not overflow the int so it becomes negative */
137             byte[] ba = new byte[maxmem];
138             failed();
139         } catch (OutOfMemoryError e) {
140             ok();
141             pstacktrace(e);
142         }
143
144         try {
145             p("OutOfMemoryError (multianewarray):");
146             byte[][] ba = new byte[maxmem][maxmem];
147             failed();
148         } catch (OutOfMemoryError e) {
149             ok();
150             pstacktrace(e);
151         }
152         
153         pln();
154
155
156         pheader("exceptions in leaf functions");
157
158         try {
159             p("ArithmeticException:");
160             aesub(1, 0);
161             failed();
162         } catch (ArithmeticException e) {
163             ok();
164             pstacktrace(e);
165         }
166
167         try {
168             p("ArrayIndexOutOfBoundsException:");
169             aioobesub(new int[1]);
170             failed();
171         } catch (ArrayIndexOutOfBoundsException e) {
172             String msg = e.getMessage();
173
174             if (msg == null || !msg.equals(String.valueOf(INDEX3))) {
175                 pln("FAILED: wrong index: " + msg + ", should be: " + INDEX3);
176                 pstacktrace(e);
177             } else {
178                 ok();
179                 pstacktrace(e);
180
181             }
182         }
183
184         try {
185             p("ClassCastException:");
186             ccesub(new Object(), new Integer(0));
187             failed();
188         } catch (ClassCastException e) {
189             ok();
190             pstacktrace(e);
191         }
192
193         try {
194             p("NullPointerException:");
195             npesub(null);
196             failed();
197         } catch (NullPointerException e) {
198             ok();
199             pstacktrace(e);
200         }
201
202         try {
203             p("Exception in <clinit> triggered from a leaf method:");
204             extest_clinit_patcher.i = 1;
205             failed();
206         } catch (ExceptionInInitializerError e) {
207             ok();
208             pstacktrace(e);
209         }
210
211         pln();
212
213
214         pheader("exception related things");
215
216         try {
217             p("load/link an exception class in asmpart:");
218             throw new Exception();
219         } catch (UnknownError e) {
220             /* this exception class MUST NOT be loaded before!!!
221                otherwise this test is useless */
222         } catch (Exception e) {
223             ok();
224             pstacktrace(e);
225         }
226
227         pln();
228
229
230         pheader("native stub exceptions");
231
232         try {
233             p("NullPointerException in <clinit>:");
234             extest_clinit.sub();
235             failed();
236         } catch (ExceptionInInitializerError e) {
237             ok();
238             pstacktrace(e);
239         }
240
241         try {
242             p("UnsatisfiedLinkError:");
243             nsub();
244             failed();
245         } catch (UnsatisfiedLinkError e) {
246             ok();
247             pstacktrace(e);
248         }
249
250         try {
251             p("NullPointerException (native):");
252             System.arraycopy(null, 1, null, 1, 1);
253             failed();
254         } catch (NullPointerException e) {
255             ok();
256             pstacktrace(e);
257         }
258
259         pln();
260
261
262         pheader("special exceptions");
263
264         try {
265             p("OutOfMemoryError (array clone):");
266             /* use half of the heap size */
267             byte[] ba1 = new byte[maxmem / 2];
268             byte[] ba2 = (byte[]) ba1.clone();
269             failed();
270         } catch (OutOfMemoryError e) {
271             ok();
272             pstacktrace(e);
273         }
274
275         pln();
276
277
278         pheader("exception thrown to command-line");
279
280         pln("NullPointerException (without catch):");
281         String s = null;
282         int i = s.length();
283         failed();
284     }
285
286     synchronized static void sub() throws Exception {
287         sub2();
288     }
289
290     static void sub2() throws Exception {
291         sub3();
292     }
293
294     synchronized static void sub3() throws Exception {
295         sub4();
296     }
297
298     static void sub4() throws Exception {
299         throw new Exception();
300     }
301
302     static void aesub(int a, int b) {
303         int c = a / b;
304     }
305
306     static void aioobesub(int[] ia) {
307         ia[INDEX3] = 0;
308     }
309
310     static void ccesub(Object o, Integer i) {
311         i = (Integer) o;
312     }
313
314     static void npesub(int[] ia) {
315         int a = ia.length;
316     }
317
318     static native void nsub();
319
320     static void p(String s) {
321         System.out.print(s);
322         for (int i = s.length(); i < COLUMN; i++) {
323             System.out.print(" ");
324         }
325     }
326
327     static void pheader(String s) {
328         System.out.print(s);
329         for (int i = s.length(); i < COLUMN + 3; i++) {
330             System.out.print("-");
331         }
332         System.out.println();
333         System.out.println();
334     }
335
336     static void pln() {
337         System.out.println();
338     }
339
340     static void pln(String s) {
341         System.out.println(s);
342     }
343
344     static void ok() {
345         pln("OK");
346     }
347
348     static void failed() {
349         pln("FAILED");
350     }
351
352     static void pstacktrace(Throwable e) {
353         if (!printStackTrace)
354             return;
355         e.printStackTrace();
356         System.out.println();
357     }
358 }
359
360 class extest_clinit {
361     static {
362         String s = null;
363         s.length();
364     }
365
366     public static native void sub();
367 }
368
369 class extest_clinit_patcher {
370     static int i;
371
372     static {
373         int[] ia = null;
374         int a = ia.length;
375     }
376 }