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.
19 package gov.nasa.jpf.test.mc.data;
21 import gov.nasa.jpf.util.test.TestJPF;
22 import gov.nasa.jpf.vm.Verify;
24 import org.junit.Test;
27 * regression test for Perturbator listener
29 public class PerturbatorTest extends TestJPF {
33 public static void main(String[] args) {
34 runTestsOfThisClass(args);
38 public void testIntFieldPerturbation() {
41 Verify.resetCounter(0);
44 if (verifyNoPropertyViolation("+listener=.listener.Perturbator",
45 "+perturb.fields=data",
46 "+perturb.data.class=.perturb.IntOverUnder",
47 "+perturb.data.field=gov.nasa.jpf.test.mc.data.PerturbatorTest.data",
48 "+perturb.data.delta=1")){
49 System.out.println("instance field perturbation test");
51 System.out.print("d = ");
52 System.out.println(d);
54 Verify.incrementCounter(0);
55 switch (Verify.getCounter(0)){
56 case 1: assert d == 43; break;
57 case 2: assert d == 42; break;
58 case 3: assert d == 41; break;
60 assert false : "wrong counter value: " + Verify.getCounter(0);
64 assert Verify.getCounter(0) == 3;
69 public void testFieldPerturbationLocation() {
72 Verify.resetCounter(0);
75 if (verifyNoPropertyViolation("+listener=.listener.Perturbator",
76 "+perturb.fields=data",
77 "+perturb.data.class=.perturb.IntOverUnder",
78 "+perturb.data.field=gov.nasa.jpf.test.mc.data.PerturbatorTest.data",
79 "+perturb.data.location=PerturbatorTest.java:87",
80 "+perturb.data.delta=1")){
81 System.out.println("instance field location perturbation test");
83 int x = data; // this should not be perturbed
84 System.out.print("x = ");
85 System.out.println(x);
87 int d = data; // this should be
88 System.out.print("d = ");
89 System.out.println(d);
91 Verify.incrementCounter(0);
94 assert Verify.getCounter(0) == 3;
102 int bar (int i, boolean b) {
111 public void testIntReturnPerturbation() {
113 Verify.resetCounter(0);
114 Verify.resetCounter(1);
117 if (verifyNoPropertyViolation("+listener=.listener.Perturbator",
118 "+perturb.returns=foo,bar",
120 "+perturb.foo.class=.perturb.IntOverUnder",
121 "+perturb.foo.method=gov.nasa.jpf.test.mc.data.PerturbatorTest.foo(int)",
122 "+perturb.foo.location=PerturbatorTest.java:136",
123 "+perturb.foo.delta=1",
125 "+perturb.bar.class=.perturb.IntOverUnder",
126 "+perturb.bar.method=gov.nasa.jpf.test.mc.data.PerturbatorTest.bar(int,boolean)",
127 "+perturb.bar.delta=5")){
130 System.out.println("int return perturbation test");
132 x = foo(-1); // this should not be perturbed ('foo' has a location spec)
133 System.out.print("foo() = ");
134 System.out.println(x);
136 x = foo(42); // line 136 => this should be
137 System.out.print("foo() = ");
138 System.out.println(x);
140 Verify.incrementCounter(0);
141 switch (Verify.getCounter(0)){
142 // foo() preturbations
143 case 1: assert x == 43; break;
144 case 2: assert x == 42; break;
145 case 3: assert x == 41; break;
147 assert false : "wrong counter 0 (foo() perturbation) value: " + Verify.getCounter(0);
151 y = bar(40, false); // this too (no location spec for 'bar')
152 System.out.print("bar() = ");
153 System.out.println(y);
155 Verify.incrementCounter(1);
156 switch (Verify.getCounter(1)){
157 // bar() perturbations
158 case 1: assert y == 44; break;
159 case 2: assert y == 39; break;
160 case 3: assert y == 34; break;
162 assert false : "wrong counter 1 (bar() perturbation) value: " + Verify.getCounter(1);
167 assert Verify.getCounter(0) == 3;
168 assert Verify.getCounter(1) == 3;
172 static void printParams(int i, boolean b) {
173 System.out.println("(" + i + ", " + b + ")");
176 static int zoo(int i, boolean b) {
184 void printParam(long i, double d) {
185 System.out.println("(" + i + ", " + d + ")");
188 double foobar(long i, double d) {
195 public void testParamPerturbation() {
198 Verify.resetCounter(0);
199 Verify.resetCounter(1);
202 if (verifyNoPropertyViolation("+listener=.listener.Perturbator",
203 "+perturb.params=foo,zoo",
204 "+perturb.foo.class=.perturb.GenericDataAbstractor",
205 "+perturb.foo.method=gov.nasa.jpf.test.mc.data.PerturbatorTest.foobar(long,double)",
206 "+perturb.foo.location=PerturbatorTest.java:233", // <<<<<<<<< update if file is changed!
207 "+perturb.zoo.class=.perturb.GenericDataAbstractor",
208 "+perturb.zoo.method=gov.nasa.jpf.test.mc.data.PerturbatorTest.zoo(int,boolean)"
210 System.out.println("parameters perturbation test");
212 int e = zoo(42, false);
213 System.out.print("zoo = ");
214 System.out.println(e);
216 Verify.incrementCounter(0);
217 switch (Verify.getCounter(0)){
218 case 1: assert e == 21; break;
219 case 2: assert e == -21; break;
220 case 3: assert e == 0; break;
221 case 4: assert e == 0; break;
222 case 5: assert e == -84; break;
223 case 6: assert e == 84; break;
225 assert false : "wrong counter value: " + Verify.getCounter(0);
229 double d = foobar(42, 0.0); // no perturbation
230 System.out.print("foobar = ");
231 System.out.println(d);
233 d = foobar(42, 0.0); // <<<< yes perturbation, line 233
234 System.out.print("foobar = ");
235 System.out.println(d);
237 Verify.incrementCounter(1);
238 switch (Verify.getCounter(1)){
239 case 1: assert Math.abs(d - 0.586) < 0.0001; break;
240 case 2: assert d == 2; break;
241 case 3: assert d == 5.141; break;
242 case 4: assert d == -1.414; break;
243 case 5: assert d == 0; break;
244 case 6: assert d == 3.141; break;
245 case 7: assert d == -1.414; break;
246 case 8: assert d == 0; break;
247 case 9: assert d == 3.141; break;
249 assert false : "wrong counter value: " + Verify.getCounter(1);
253 assert Verify.getCounter(0) == 6;
254 assert Verify.getCounter(1) == 9;