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