Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / mc / basic / InvokeListenerTest.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
19 package gov.nasa.jpf.test.mc.basic;
20
21 import gov.nasa.jpf.ListenerAdapter;
22 import gov.nasa.jpf.jvm.bytecode.INVOKESTATIC;
23 import gov.nasa.jpf.jvm.bytecode.JVMInvokeInstruction;
24 import gov.nasa.jpf.jvm.bytecode.VirtualInvocation;
25 import gov.nasa.jpf.util.test.TestJPF;
26 import gov.nasa.jpf.vm.ElementInfo;
27 import gov.nasa.jpf.vm.Instruction;
28 import gov.nasa.jpf.vm.VM;
29 import gov.nasa.jpf.vm.MethodInfo;
30 import gov.nasa.jpf.vm.ThreadInfo;
31
32 import org.junit.Test;
33
34
35 /**
36  * testing various aspects of listeners on INVOKE instructions
37  */
38 public class InvokeListenerTest extends TestJPF {
39
40   //--- this is only used outside JPF execution
41   public static class Listener extends ListenerAdapter {
42
43     void checkArgs (ThreadInfo ti, Instruction insn, boolean isPostExec){
44       if (insn instanceof JVMInvokeInstruction){
45         JVMInvokeInstruction call = (JVMInvokeInstruction)insn;
46         MethodInfo mi = call.getInvokedMethod(ti);
47         String miSignature = mi.getUniqueName();
48         String mname = mi.getName();
49
50         if (miSignature.equals("testInstanceMethod(DI)D")){
51           Object[] args = call.getArgumentValues(ti);
52           ElementInfo ei = getTarget(ti,call);
53           log(mname, ei, args, isPostExec);
54           assert ((Double)args[0]) == 42.0;
55           assert ((Integer)args[1]) == 1;
56
57         } else if (miSignature.equals("testStaticMethod(I)I")){
58           Object[] args = call.getArgumentValues(ti);
59           ElementInfo ei = getTarget(ti,call);
60           log(mname, ei, args, isPostExec);
61           assert ((Integer)args[0]) == 42;
62
63         } else if (miSignature.equals("testNativeInstanceMethod(DI)D")){
64           Object[] args = call.getArgumentValues(ti);
65           ElementInfo ei = getTarget(ti,call);
66           log(mname, ei, args, isPostExec);
67           assert ((Double)args[0]) == 42.0;
68           assert ((Integer)args[1]) == 1;
69
70         }
71       }
72     }
73
74     ElementInfo getTarget (ThreadInfo ti, JVMInvokeInstruction call){
75       if (call instanceof VirtualInvocation){
76         int objRef = ((VirtualInvocation)call).getCalleeThis(ti);
77         return ti.getElementInfo(objRef);
78       } else if (call instanceof INVOKESTATIC){
79         return ((INVOKESTATIC)call).getInvokedMethod().getClassInfo().getStaticElementInfo();
80       } else {
81         return null;
82       }
83     }
84
85     void log (String mname, ElementInfo ei, Object[] args, boolean isPostExec){
86       System.out.print(isPostExec ? "# instructionExecuted: " : "# executeInstruction: ");
87
88       System.out.print(ei);
89       System.out.print('.');
90       System.out.print(mname);
91
92       System.out.print(" (");
93       for (int i=0; i<args.length; i++) {
94         if (i >0) System.out.print(',');
95         System.out.print( args[i]);
96       }
97       System.out.println(")");
98     }
99
100     @Override
101     public void executeInstruction (VM vm, ThreadInfo ti, Instruction insnToExecute){
102       checkArgs(ti, insnToExecute, false);
103     }
104
105     @Override
106     public void instructionExecuted (VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn){
107       checkArgs(ti, executedInsn, true);
108     }
109
110   }
111
112   double testInstanceMethod (double d, int c){
113     return d + c;
114   }
115   @Test public void testInstanceMethod (){
116     if (verifyNoPropertyViolation("+listener=.test.mc.basic.InvokeListenerTest$Listener")){
117       testInstanceMethod(42.0, 1);
118     }
119   }
120   
121   int testStaticMethod (int a){
122     return a + 1;
123   }
124   @Test public void testStaticMethod (){
125     if (verifyNoPropertyViolation("+listener=.test.mc.basic.InvokeListenerTest$Listener")){
126       testStaticMethod(42);
127     }
128   }
129
130   native double testNativeInstanceMethod (double d, int c);
131   @Test public void testNativeInstanceMethod (){
132     if (verifyNoPropertyViolation("+listener=.test.mc.basic.InvokeListenerTest$Listener")){
133       testNativeInstanceMethod(42.0, 1);
134     }
135   }
136
137
138 }