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