76e114ebeac0182924a70a26da70d5254eb902f3
[jpf-core.git] / src / tests / gov / nasa / jpf / test / vm / basic / MethodTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.test.vm.basic;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21
22 import org.junit.Test;
23
24
25 /**
26  * basic method invocation test
27  */
28
29
30 interface TMI {
31   void gna();
32 }
33
34 class TestMethodBase extends TestJPF implements TMI {
35   
36   int baseData;
37
38   static int sData;
39   
40   static {
41     sData = -1;
42   }
43   
44   static void taz () {
45     sData = 24;
46   }
47   
48   TestMethodBase (int a) {
49     assert a == 42;
50     baseData = 42;
51   }
52
53   boolean baz (boolean p, byte b, char c, short s, int i, long l, float f, double d, Object o) {
54     assert p;
55     assert b == 4;
56     assert c == '?';
57     assert s == 42;
58     assert i == 4242;
59     assert l == 424242;
60     assert f == 4.2f;
61     assert d == 4.242;
62     assert o.equals(new Integer(42));
63
64     baseData = 44;
65
66     return p;
67   }
68   
69   void faz () {
70     gna();
71   }
72   
73   @Override
74   public void gna () {
75     baseData = 0;
76   }
77   
78   int har () {
79     return priv();
80   }
81   
82   private int priv () {
83     return 7;
84   }
85 }
86
87 public class MethodTest extends TestMethodBase {
88   
89   int data;
90   
91   static void taz () {
92     sData = 9;
93   }
94   
95   public MethodTest () {
96     super(42);
97     
98     data = 42;
99   }
100
101   MethodTest (int a) {
102     super(a);
103     
104     data = a;
105   }
106   
107   double foo (boolean p, byte b, char c, short s, int i, long l, float f, double d, Object o) {
108     assert p;
109     assert b == 4;
110     assert c == '?';
111     assert s == 42;
112     assert i == 4242;
113     assert l == 424242;
114     assert f == 4.2f;
115     assert d == 4.242;
116     assert o.equals(new Integer(42));
117
118     data = 43;
119
120     return d;
121   }
122
123   @Override
124   public void gna () {
125     baseData = 45;
126   }
127   
128   int priv () {
129     return 8;
130   }
131   
132   @Test
133   public void testCtor() {
134     if (verifyNoPropertyViolation()) {
135       MethodTest o1 = new MethodTest();
136       assert o1.data == 42;
137       assert o1.baseData == 42;
138
139       MethodTest o2 = new MethodTest(42);
140       assert o2.data == 42;
141       assert o2.baseData == 42;
142     }
143   }
144
145   @Test
146   public void testCall() {
147     if (verifyNoPropertyViolation()) {
148       MethodTest o = new MethodTest();
149       assert o.foo(true, (byte) 4, '?', (short) 42, 4242, 424242, 4.2f, 4.242, new Integer(42)) == 4.242;
150       assert o.data == 43;
151     }
152   }
153
154   @Test
155   public void testInheritedCall() {
156     if (verifyNoPropertyViolation()) {
157       MethodTest o = new MethodTest();
158       assert o.baz(true, (byte) 4, '?', (short) 42, 4242, 424242, 4.2f, 4.242, new Integer(42));
159       assert o.baseData == 44;
160     }
161   }
162
163   @Test
164   public void testVirtualCall() {
165     if (verifyNoPropertyViolation()) {
166       MethodTest o = new MethodTest();
167       TestMethodBase b = o;
168
169       b.faz();
170       assert o.baseData == 45;
171     }
172   }
173
174   @Test
175   public void testSpecialCall() {
176     if (verifyNoPropertyViolation()) {
177       MethodTest o = new MethodTest();
178       assert o.har() == 7;
179     }
180   }
181
182   @Test
183   public void testStaticCall() {
184     if (verifyNoPropertyViolation()) {
185       assert TestMethodBase.sData == -1;
186
187       MethodTest.taz();
188       assert TestMethodBase.sData == 9;
189
190       TestMethodBase.taz();
191       assert TestMethodBase.sData == 24;
192
193       // used to be:
194       //TestMethod o = new TestMethod();
195       //o.taz();
196       // statically equiv. to this: (no warnings) - pcd
197       new MethodTest();
198       MethodTest.taz();
199
200       assert TestMethodBase.sData == 9;
201     }
202   }
203
204   @Test
205   public void testInterfaceCall() {
206     if (verifyNoPropertyViolation()) {
207       MethodTest o = new MethodTest();
208       TMI ifc = o;
209
210       ifc.gna();
211       assert o.baseData == 45;
212     }
213   }
214   
215   static class A {
216     public int foo () {
217       assert false : "should bever be invoked";
218       return -1;
219     }
220   }
221   
222   static class A0 extends A {
223     @Override
224         public int foo () {
225       return 0;
226     }
227   }
228   
229   static class A1 extends A {
230     @Override
231         public int foo () {
232       return 1;
233     }
234   }
235     
236   /**
237    * this is tricky - both the allocations and the foo() calls have to be
238    * the same instructions, and we have to remove all 'a' references before
239    * forcing a GC. Otherwise we don't allocate 'a's with the same reference values 
240    *
241    */
242   @Test public void testCallAcrossGC () {
243
244     if (verifyNoPropertyViolation()){
245       System.out.println("testing virtual calls on GC boundaries");
246       A a;
247
248       a = new A0();
249       assert a.foo() == 0 : "wrong A.foo() called for A0";
250
251       // do a GC
252       Runtime.getRuntime().gc(); // this should recycle 'a'
253
254       a = new A1();
255       assert a.foo() == 1 : "wrong A.foo() called for A1";
256     }
257   }
258 }