2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.test.mc.basic;
20 import gov.nasa.jpf.ListenerAdapter;
21 import gov.nasa.jpf.jvm.bytecode.JVMInvokeInstruction;
22 import gov.nasa.jpf.jvm.bytecode.PUTFIELD;
23 import gov.nasa.jpf.util.test.TestJPF;
24 import gov.nasa.jpf.vm.ChoiceGenerator;
25 import gov.nasa.jpf.vm.FieldInfo;
26 import gov.nasa.jpf.vm.Instruction;
27 import gov.nasa.jpf.vm.VM;
28 import gov.nasa.jpf.vm.SystemState;
29 import gov.nasa.jpf.vm.ThreadInfo;
30 import gov.nasa.jpf.vm.Verify;
32 import org.junit.Test;
36 * simple test application to break transitions from listeners
38 public class BreakTest extends TestJPF {
40 static final String LISTENER = "+listener=.test.mc.basic.BreakTestListener";
42 static class BreakListener extends ListenerAdapter {
43 public static int nCG; // braindead, just to check from outside
45 public BreakListener() {
50 public void choiceGeneratorSet (VM vm, ChoiceGenerator<?> newCG) {
51 System.out.println("CG set: " + newCG);
56 public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG) {
57 System.out.println("CG advanced: " + currentCG);
66 public static class FieldIgnorer extends BreakListener {
68 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
69 SystemState ss = vm.getSystemState();
71 if (executedInsn instanceof PUTFIELD) { // break on field access
72 FieldInfo fi = ((PUTFIELD) executedInsn).getFieldInfo();
73 if (fi.getClassInfo().getName().endsWith(".BreakTest")) {
74 System.out.println("# ignoring after: " + executedInsn);
82 public void testSimpleIgnore () {
83 if (verifyNoPropertyViolation("+listener=.test.mc.basic.BreakTest$FieldIgnorer",
84 "+vm.max_transition_length=1000000")) {
86 data = i; // we ignore here
87 fail("should never get here");
90 if (BreakListener.nCG != 1) { // that's really simplistic
91 fail("wrong number of CGs: " + BreakListener.nCG);
99 public static class FieldBreaker extends BreakListener {
101 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
102 SystemState ss = vm.getSystemState();
104 if (executedInsn instanceof PUTFIELD) { // break on field access
105 FieldInfo fi = ((PUTFIELD) executedInsn).getFieldInfo();
106 if (fi.getClassInfo().getName().endsWith(".BreakTest")) {
107 System.out.println("# breaking after: " + executedInsn);
108 ti.breakTransition("breakTest");
115 public void testSimpleBreak () {
116 if (verifyNoPropertyViolation("+listener=.test.mc.basic.BreakTest$FieldBreaker",
117 "+vm.max_transition_length=1000000")) {
119 data = i; // we break after that
123 if (BreakListener.nCG != 2) { // that's really simplistic
124 fail("wrong number of CGs: " + BreakListener.nCG);
130 //--- test CG chain break
132 public static class FooCallBreaker extends BreakListener {
134 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
135 SystemState ss = vm.getSystemState();
137 if (executedInsn instanceof JVMInvokeInstruction) { // break on method call
138 JVMInvokeInstruction call = (JVMInvokeInstruction) executedInsn;
140 if ("foo()V".equals(call.getInvokedMethodName())) {
141 System.out.println("# breaking & pruning after: " + executedInsn);
142 System.out.println("# registered (ignored) CG: " + ss.getNextChoiceGenerator());
143 ti.breakTransition("breakTest"); // not required since we ignore
151 System.out.println("foo");
155 System.out.println("bar");
159 public void testDeepCGBreak () {
161 Verify.resetCounter(0);
164 if (verifyNoPropertyViolation("+listener=.test.mc.basic.BreakTest$FooCallBreaker")) {
165 if (Verify.getBoolean(false)) {
166 System.out.println("foo,bar branch");
167 foo(); // listener sets it ignored -> break
169 fail("should not get here");
172 Verify.incrementCounter(0);
174 System.out.println("bar,foo branch");
176 foo(); // listener sets it ignored -> break
177 fail("should not get here");
182 assert Verify.getCounter(0) == 1;
187 //--- test ignore after setting nextCG
189 public static class VerifyNextIntBreaker extends BreakListener {
191 public void choiceGeneratorRegistered(VM vm, ChoiceGenerator<?> nextCG, ThreadInfo ti, Instruction executedInsn) {
192 SystemState ss = vm.getSystemState();
194 ChoiceGenerator<?> cg = ss.getNextChoiceGenerator();
195 if (cg.getId().equals("verifyGetInt(II)")) {
196 System.out.println("# breaking & pruning after: " + ti.getPC());
197 System.out.println("# registered (ignored) CG: " + cg);
199 ss.setIgnored(true); // should reset the IntIntervalCG registered by the native getInt()
200 ti.breakTransition("breakTest"); // should have no effect
206 public void testIgnoreAfterCG () {
208 Verify.resetCounter(0);
211 if (verifyNoPropertyViolation("+listener=.test.mc.basic.BreakTest$VerifyNextIntBreaker")) {
212 if (Verify.getBoolean(false)){
213 System.out.println("true branch (should be first)");
215 int i = Verify.getInt(1, 2); // listener breaks & ignores post exec
216 fail("should never get here");
219 Verify.incrementCounter(0);
221 System.out.println("false branch");
226 assert Verify.getCounter(0) == 1;