* .hgignore: Removed tests/regression/codepatching/*.class.
[cacao.git] / tests / regression / junit / TestPatcher.java
1 /* tests/regression/bugzilla/TestPatcher.java
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 import junit.framework.*;
27 import junit.textui.*;
28
29 import java.io.*;
30
31 public class TestPatcher extends TestCase {
32     public static void main(String[] args) {
33         TestRunner.run(suite());
34     }
35
36     public static Test suite() {
37         return new TestSuite(TestPatcher.class);
38     }
39
40     static boolean doit = true;
41
42     final static int    i = 123;
43     final static long   l = 1234567890123L;
44     final static float  f = 123.456F;
45     final static double d = 789.012;
46     final static Object o = new Object();
47
48     public void testNormal() {
49         invokestatic();
50         invokespecial();
51
52         getstatic();
53         putstatic();
54         putstaticconst();
55
56         getfield();
57         putfield();
58         putfieldconst();
59
60         newarray();
61         multianewarray();
62
63         checkcast();
64         _instanceof();
65
66         aastoreconst();
67     }
68
69     public void testWithoutClasses() {
70         // Delete all classes.
71         //new File("TestPatcher$invokestatic.class").delete();
72
73         invokestatic();
74         invokespecial();
75
76         getstatic();
77         putstatic();
78         putstaticconst();
79
80         getfield();
81         putfield();
82         putfieldconst();
83
84         newarray();
85         multianewarray();
86
87         checkcast();
88         _instanceof();
89
90         aastoreconst();
91     }
92
93     private void invokestatic() {
94         try {
95             if (doit)
96                 invokestatic.sub();
97         } catch (NoClassDefFoundError e) {
98             fail(e.toString());
99         }
100     }
101
102     private void getstatic() {
103         try {
104             if (doit)
105                 assertTrue(getstaticI.i + " != " + i, getstaticI.i == i);
106         } catch (NoClassDefFoundError e) {
107             fail(e.toString());
108         }
109
110         try {
111             if (doit)
112                 assertTrue(getstaticJ.l + " != " + l, getstaticJ.l == l);
113         } catch (NoClassDefFoundError e) {
114             fail(e.toString());
115         }
116
117         try {
118             if (doit)
119                 assertTrue(getstaticF.f + " != " + f, getstaticF.f == f);
120         } catch (NoClassDefFoundError e) {
121             fail(e.toString());
122         }
123
124         try {
125             if (doit)
126                 assertTrue(getstaticD.d + " != " + d, getstaticD.d == d);
127         } catch (NoClassDefFoundError e) {
128             fail(e.toString());
129         }
130
131         try {
132             if (doit)
133                 assertTrue(getstaticL.o + " != null", getstaticL.o == null);
134         } catch (NoClassDefFoundError e) {
135             fail(e.toString());
136         }
137     }
138
139     private void putstatic() {
140         try {
141             if (doit) {
142                 putstaticI.i = i;
143                 assertTrue(putstaticI.i + " != " + i, putstaticI.i == i);
144             }
145         } catch (NoClassDefFoundError t) {
146             fail(t.toString());
147         }
148
149         try {
150             if (doit) {
151                 putstaticJ.l = l;
152                 assertTrue(putstaticJ.l + " != " + l, putstaticJ.l == l);
153             }
154         } catch (NoClassDefFoundError t) {
155             fail(t.toString());
156         }
157
158         try {
159             if (doit) {
160                 putstaticF.f = f;
161                 assertTrue(putstaticF.f + " != " + f, putstaticF.f == f);
162             }
163         } catch (NoClassDefFoundError t) {
164             fail(t.toString());
165         }
166
167         try {
168             if (doit) {
169                 putstaticD.d = d;
170                 assertTrue(putstaticD.d + " != " + d, putstaticD.d == d);
171             }
172         } catch (NoClassDefFoundError t) {
173             fail(t.toString());
174         }
175
176
177         try {
178             if (doit) {
179                 putstaticL.o = o;
180                 assertTrue(putstaticL.o + " != " + o, putstaticL.o == o);
181             }
182         } catch (NoClassDefFoundError t) {
183             fail(t.toString());
184         }
185     }
186
187     private void putstaticconst() {
188         try {
189             if (doit) {
190                 putstaticconstI.i = i;
191                 assertTrue(putstaticconstI.i + " != " + i, putstaticconstI.i == i);
192             }
193         } catch (NoClassDefFoundError t) {
194             fail(t.toString());
195         }
196
197         try {
198             if (doit) {
199                 putstaticconstJ.l = l;
200                 assertTrue(putstaticconstJ.l + " != " + l, putstaticconstJ.l == l);
201             }
202         } catch (NoClassDefFoundError t) {
203             fail(t.toString());
204         }
205
206         try {
207             if (doit) {
208                 putstaticconstF.f = f;
209                 assertTrue(putstaticconstF.f + " != " + f, putstaticconstF.f == f);
210             }
211         } catch (NoClassDefFoundError t) {
212             fail(t.toString());
213         }
214
215         try {
216             if (doit) {
217                 putstaticconstD.d = d;
218                 assertTrue(putstaticconstD.d + " != " + d, putstaticconstD.d == d);
219             }
220         } catch (NoClassDefFoundError t) {
221             fail(t.toString());
222         }
223
224         try {
225             if (doit) {
226                 putstaticconstI.i = 0;
227                 assertTrue(putstaticconstI.i + " != " + 0, putstaticconstI.i == 0);
228             }
229         } catch (NoClassDefFoundError t) {
230             fail(t.toString());
231         }
232
233         try {
234             if (doit) {
235                 putstaticconstJ.l = 0L;
236                 assertTrue(putstaticconstJ.l + " != " + 0L, putstaticconstJ.l == 0L);
237             }
238         } catch (NoClassDefFoundError t) {
239             fail(t.toString());
240         }
241
242         try {
243             if (doit) {
244                 putstaticconstF.f = 0.0F;
245                 assertTrue(putstaticconstF.f + " != " + 0.0F, putstaticconstF.f == 0.0F);
246             }
247         } catch (NoClassDefFoundError t) {
248             fail(t.toString());
249         }
250
251         try {
252             if (doit) {
253                 putstaticconstD.d = 0.0;
254                 assertTrue(putstaticconstD.d + " != " + 0.0, putstaticconstD.d == 0.0);
255             }
256         } catch (NoClassDefFoundError t) {
257             fail(t.toString());
258         }
259
260         try {
261             if (doit) {
262                 putstaticconstL.o = null;
263                 assertTrue(putstaticconstL.o + " != " + null, putstaticconstL.o == null);
264             }
265         } catch (NoClassDefFoundError t) {
266             fail(t.toString());
267         }
268
269         try {
270             if (doit) {
271                 putstaticconstC.c = putstaticconstC.class;
272                 assertTrue(putstaticconstC.c + " != " + Class.forName("TestPatcher$putstaticconstC"), putstaticconstC.c == Class.forName("TestPatcher$putstaticconstC"));
273             }
274         } catch (NoClassDefFoundError t) {
275             fail(t.toString());
276         } catch (ClassNotFoundException t) {
277             fail(t.toString());
278         }
279     }
280
281     private void getfield() {
282         try {
283             if (doit)
284                 assertTrue(new getfieldI().i + " != " + i, new getfieldI().i == i);
285         } catch (NoClassDefFoundError t) {
286             fail(t.toString());
287         }
288
289         try {
290             if (doit)
291                 assertTrue(new getfieldJ().l + " != " + l, new getfieldJ().l == l);
292         } catch (NoClassDefFoundError t) {
293             fail(t.toString());
294         }
295
296         try {
297             if (doit)
298                 assertTrue(new getfieldF().f + " != " + f, new getfieldF().f == f);
299         } catch (NoClassDefFoundError t) {
300             fail(t.toString());
301         }
302
303         try {
304             if (doit)
305                 assertTrue(new getfieldD().d + " != " + d, new getfieldD().d == d);
306         } catch (NoClassDefFoundError t) {
307             fail(t.toString());
308         }
309
310         try {
311             if (doit)
312                 assertTrue(new getfieldL().o + " != " + null, new getfieldL().o == null);
313         } catch (NoClassDefFoundError t) {
314             fail(t.toString());
315         }
316     }
317
318     private void putfield() {
319         try {
320             if (doit) {
321                 TestPatcher.putfieldI pfi = new TestPatcher.putfieldI();
322                 pfi.i = i;
323                 assertTrue(pfi.i + " != " + i, pfi.i == i);
324             }
325         } catch (NoClassDefFoundError t) {
326             fail(t.toString());
327         }
328
329         try {
330             if (doit) {
331                 putfieldJ pfj = new putfieldJ();
332                 pfj.l = l;
333                 assertTrue(pfj.l + " != " + l, pfj.l == l);
334             }
335         } catch (NoClassDefFoundError t) {
336             fail(t.toString());
337         }
338
339         try {
340             if (doit) {
341                 putfieldF pff = new putfieldF();
342                 pff.f = f;
343                 assertTrue(pff.f + " != " + f, pff.f == f);
344             }
345         } catch (NoClassDefFoundError t) {
346             fail(t.toString());
347         }
348
349         try {
350             if (doit) {
351                 putfieldD pfd = new putfieldD();
352                 pfd.d = d;
353                 assertTrue(pfd.d + " != " + d, pfd.d == d);
354             }
355         } catch (NoClassDefFoundError t) {
356             fail(t.toString());
357         }
358
359         try {
360             if (doit) {
361                 putfieldL pfl = new putfieldL();
362                 pfl.o = o;
363                 assertTrue(pfl.o + " != " + o, pfl.o == o);
364             }
365         } catch (NoClassDefFoundError t) {
366             fail(t.toString());
367         }
368     }
369
370     private void putfieldconst() {
371         try {
372             if (doit) {
373                 putfieldconstI pfci = new putfieldconstI();
374                 pfci.i = i;
375                 assertTrue(pfci.i + " != " + i, pfci.i == i);
376             }
377         } catch (NoClassDefFoundError t) {
378             fail(t.toString());
379         }
380  
381         try {
382             if (doit) {
383                 putfieldconstJ pfcj = new putfieldconstJ();
384                 pfcj.l = l;
385                 assertTrue(pfcj.l + " != " + l, pfcj.l == l);
386             }
387         } catch (NoClassDefFoundError t) {
388             fail(t.toString());
389         }
390
391         try {
392             if (doit) {
393                 putfieldconstF pfcf = new putfieldconstF();
394                 pfcf.f = f;
395                 assertTrue(pfcf.f + " != " + f, pfcf.f == f);
396             }
397         } catch (NoClassDefFoundError t) {
398             fail(t.toString());
399         }
400  
401         try {
402             if (doit) {
403                 putfieldconstD pfcd = new putfieldconstD();
404                 pfcd.d = d;
405                 assertTrue(pfcd.d + " != " + d, pfcd.d == d);
406             }
407         } catch (NoClassDefFoundError t) {
408             fail(t.toString());
409         }
410
411         try {
412             if (doit) {
413                 putfieldconstI pfci = new putfieldconstI();
414                 pfci.i = 0;
415                 assertTrue(pfci.i + " != " + 0, pfci.i == 0);
416             }
417         } catch (NoClassDefFoundError t) {
418             fail(t.toString());
419         }
420  
421         try {
422             if (doit) {
423                 putfieldconstJ pfcj = new putfieldconstJ();
424                 pfcj.l = 0L;
425                 assertTrue(pfcj.l + " != " + 0L, pfcj.l == 0L);
426             }
427         } catch (NoClassDefFoundError t) {
428             fail(t.toString());
429         }
430
431         try {
432             if (doit) {
433                 putfieldconstF pfcf = new putfieldconstF();
434                 pfcf.f = 0.0F;
435                 assertTrue(pfcf.f + " != " + 0.0F, pfcf.f == 0.0F);
436             }
437         } catch (NoClassDefFoundError t) {
438             fail(t.toString());
439         }
440  
441         try {
442             if (doit) {
443                 putfieldconstD pfcd = new putfieldconstD();
444                 pfcd.d = 0.0;
445                 assertTrue(pfcd.d + " != " + 0.0, pfcd.d == 0.0);
446             }
447         } catch (NoClassDefFoundError t) {
448             fail(t.toString());
449         }
450
451         try {
452             if (doit) {
453                 putfieldconstL pfcl = new putfieldconstL();
454                 pfcl.o = null;
455                 assertTrue(pfcl.o + " != " + null, pfcl.o == null);
456             }
457         } catch (NoClassDefFoundError t) {
458             fail(t.toString());
459         }
460
461         try {
462             if (doit) {
463                 putfieldconstC pfcc = new putfieldconstC();
464                 pfcc.c = putfieldconstC.class;
465                 assertTrue(pfcc.c + " != " + Class.forName("TestPatcher$putfieldconstC"), pfcc.c == Class.forName("TestPatcher$putfieldconstC"));
466             }
467         } catch (NoClassDefFoundError t) {
468             fail(t.toString());
469         } catch (ClassNotFoundException t) {
470             fail(t.toString());
471         }
472     }
473
474     private void newarray() {
475         try {
476             if (doit) {
477                 newarray[] na = new newarray[1];
478                 na[0] = null;
479             }
480         } catch (NoClassDefFoundError t) {
481             fail(t.toString());
482         }
483     }
484
485     private void multianewarray() {
486         try {
487             if (doit) {
488                 multianewarray[][] ma = new multianewarray[1][1];
489                 ma[0][0] = null;
490             }
491         } catch (NoClassDefFoundError t) {
492             fail(t.toString());
493         }
494     }
495
496     private void invokespecial() {
497         try {
498             if (doit)
499                 new invokespecial();
500         } catch (NoClassDefFoundError t) {
501             fail(t.toString());
502         }
503     }
504
505     private void checkcast() {
506         Object o = new Object();
507
508         // class
509         try {
510             if (doit) {
511                 checkcastC cc = (checkcastC) o;
512                 fail();
513             }
514         } catch (ClassCastException success) {
515             // This is OK.
516         } catch (NoClassDefFoundError t) {
517             fail(t.toString());
518         }
519
520         // interface
521         try {
522             if (doit) {
523                 checkcastI ci = (checkcastI) o;
524                 fail();
525             }
526         } catch (ClassCastException success) {
527             // This is OK.
528         } catch (NoClassDefFoundError t) {
529             fail(t.toString());
530         }
531
532
533         // array
534         Object[] oa = new Object[1];
535
536         try {
537             if (doit) {
538                 checkcastC[] cca = (checkcastC[]) oa;
539                 fail();
540             }
541         } catch (ClassCastException e) {
542             // This is OK.
543         } catch (NoClassDefFoundError t) {
544             fail(t.toString());
545         }
546     }
547
548     private void _instanceof() {
549         Object o = new Object();
550
551         try {
552             if (doit)
553                 if (o instanceof instanceofC)
554                     fail();
555         } catch (NoClassDefFoundError t) {
556             fail(t.toString());
557         }
558
559         try {
560             if (doit)
561                 if (o instanceof instanceofI)
562                     fail();
563         } catch (NoClassDefFoundError t) {
564             fail(t.toString());
565         }
566
567
568         // array
569         Object[] oa = new Object[1];
570
571         try {
572             if (doit)
573                 if (oa instanceof instanceofC[])
574                     fail();
575         } catch (NoClassDefFoundError t) {
576             fail(t.toString());
577         }
578     }
579
580     private void aastoreconst() {
581         Class[] ca = new Class[1];
582
583         try {
584             if (doit) {
585                 ca[0] = aastoreconstClass.class;
586
587                 if (ca[0] == null)
588                     fail();
589
590                 assertTrue(ca[0] + " != " + Class.forName("TestPatcher$aastoreconstClass") , ca[0] == Class.forName("TestPatcher$aastoreconstClass"));
591             }
592         } catch (NoClassDefFoundError t) {
593             fail(t.toString());
594         } catch (ClassNotFoundException t) {
595             fail(t.toString());
596         }
597     }
598
599     static class invokestatic  { static void sub() {} }
600     static class invokespecial { void invokespecial() {} }
601
602     static class getstaticI { static int    i = TestPatcher.i; }
603     static class getstaticJ { static long   l = TestPatcher.l; }
604     static class getstaticF { static float  f = TestPatcher.f; }
605     static class getstaticD { static double d = TestPatcher.d; }
606     static class getstaticL { static Object o = null; }
607
608     static class putstaticI { static int    i; }
609     static class putstaticJ { static long   l; }
610     static class putstaticF { static float  f; }
611     static class putstaticD { static double d; }
612     static class putstaticL { static Object o; }
613
614     static class putstaticconstI { static int    i; }
615     static class putstaticconstJ { static long   l; }
616     static class putstaticconstF { static float  f; }
617     static class putstaticconstD { static double d; }
618     static class putstaticconstL { static Object o; }
619     static class putstaticconstC { static Class<putstaticconstC> c; }
620
621     static class getfieldI { int    i = TestPatcher.i; }
622     static class getfieldJ { long   l = TestPatcher.l; }
623     static class getfieldF { float  f = TestPatcher.f; }
624     static class getfieldD { double d = TestPatcher.d; }
625     static class getfieldL { Object o = null; }
626
627     static class putfieldI { int    i; }
628     static class putfieldJ { long   l; }
629     static class putfieldF { float  f; }
630     static class putfieldD { double d; }
631     static class putfieldL { Object o; }
632
633     static class putfieldconstI { int    i; }
634     static class putfieldconstJ { long   l; }
635     static class putfieldconstF { float  f; }
636     static class putfieldconstD { double d; }
637     static class putfieldconstL { Object o; }
638     static class putfieldconstC { Class<putfieldconstC> c; }
639
640     static class newarray {}
641     static class multianewarray {}
642
643     static class instanceofC {}
644     static interface instanceofI {}
645
646     static class checkcastC {}
647     static interface checkcastI {}
648
649     static class aastoreconstClass {}
650 }