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.data;
20 import gov.nasa.jpf.util.test.TestJPF;
21 import gov.nasa.jpf.vm.Verify;
22 import gov.nasa.jpf.vm.serialize.AbstractionAdapter;
24 import org.junit.Test;
27 * regression test for field value abstractions
29 public class DynamicAbstractionTest extends TestJPF {
31 static final String SERIALIZER_ARG = "+vm.serializer.class=.vm.serialize.DynamicAbstractionSerializer";
33 static class MyClass {
38 public static class MyClassDataAbstraction extends AbstractionAdapter {
41 public int getAbstractValue (int data){
43 if (data > 5) cat = 2;
44 if (data > 10) cat = 3;
46 System.out.println("abstracted value for " + data + " = " + cat);
51 //---------------------------------------------------------------------------
54 public void testMyClass() {
56 Verify.resetCounter(0);
59 if (verifyNoPropertyViolation(SERIALIZER_ARG,
60 "+das.classes.include=*$MyClass",
62 "+das.data.field=*$MyClass.data",
63 "+das.data.abstraction=gov.nasa.jpf.test.mc.data.DynamicAbstractionTest$MyClassDataAbstraction")){
64 MyClass matchedObject = new MyClass();
65 matchedObject.data = Verify.getInt(0, 20);
67 Verify.breakTransition("testDataAbstraction");
68 System.out.println("new state for myClass.data = " + matchedObject.data);
69 Verify.incrementCounter(0);
73 assertTrue( Verify.getCounter(0) == 3);
78 //---------------------------------------------------------------------------
81 public void testMixedFields(){
83 Verify.resetCounter(0);
86 if (verifyNoPropertyViolation(SERIALIZER_ARG,
87 "+das.classes.include=*$MyClass",
89 "+das.data.field=*$MyClass.data",
90 "+das.data.abstraction=gov.nasa.jpf.test.mc.data.DynamicAbstractionTest$MyClassDataAbstraction")){
91 MyClass matchedObject = new MyClass();
92 matchedObject.data = Verify.getInt(0, 20);
94 if (matchedObject.data % 4 == 0){
95 System.out.println(" notAbstracted=1");
96 matchedObject.notAbstracted = 1;
99 Verify.breakTransition("testDataAbstraction"); // matching point
100 System.out.println("new state for myClass.data = " + matchedObject.data + ", " + matchedObject.notAbstracted);
101 Verify.incrementCounter(0);
105 assertTrue( Verify.getCounter(0) == 6);
110 //---------------------------------------------------------------------------
112 static class SomeIgnoredClass {
113 int data; // note that it is not @FilterField annotated
117 public void testClassFilter() {
119 Verify.resetCounter(0);
122 if (verifyNoPropertyViolation(SERIALIZER_ARG,
123 "+das.classes.include=*$MyClass", // only consider MyClass instance and static data
124 "+das.methods.exclude=*", // make sure we don't match this stackframe ('i' changes)
125 "+vm.max_transition_length=MAX")){
126 MyClass matchedObject = new MyClass();
127 SomeIgnoredClass ignoredObject = new SomeIgnoredClass();
129 matchedObject.data = Verify.getInt(0, 2); // (1) 1st CG
130 System.out.print(" matchedObject.data=");
131 System.out.println( matchedObject.data);
133 for (int i=0; i<2; i++){
134 ignoredObject.data = i;
135 System.out.print(" ignoredObject.data=");
136 System.out.println( ignoredObject.data);
138 Verify.breakTransition("testDataAbstraction"); // (2) matching point for someObject
140 // if we get here we had a new state (i.e. wasn't matched)
141 // NOTE we don't get here for matchedObject.data=0 because that would match with the state before (1)
142 System.out.printf(" new state for matched=%d, ignored=%d\n", matchedObject.data, ignoredObject.data);
143 Verify.incrementCounter(0); // should be only reached once for matchedObject.data={1,2}
148 assertTrue( Verify.getCounter(0) == 2);
153 //---------------------------------------------------------------------------
156 for (int i=0; i<2; i++){
157 System.out.printf(" matchThis() i=%d\n", i);
159 Verify.breakTransition("testDataAbstraction"); // 'i' has changed
160 System.out.println(" new state");
161 Verify.incrementCounter(0);
166 public void testStackFrameFilter() {
168 Verify.resetCounter(0);
171 if (verifyNoPropertyViolation(SERIALIZER_ARG,
172 "+das.methods.include=*DynamicAbstractionTest.matchThis(*)V")){
173 for (int i=0; i<10; i++){ // 'i' changes this frame..
174 System.out.printf("loop cycle %d\n", i);
175 matchThis(); // ..but not this one
180 assertTrue( Verify.getCounter(0) == 2);