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.basic;
21 import gov.nasa.jpf.ListenerAdapter;
22 import gov.nasa.jpf.util.test.TestJPF;
23 import gov.nasa.jpf.vm.ChoiceGenerator;
24 import gov.nasa.jpf.vm.DoubleChoiceGenerator;
25 import gov.nasa.jpf.vm.Instruction;
26 import gov.nasa.jpf.vm.IntChoiceGenerator;
27 import gov.nasa.jpf.vm.ThreadInfo;
28 import gov.nasa.jpf.vm.VM;
29 import gov.nasa.jpf.vm.SystemState;
30 import gov.nasa.jpf.vm.Verify;
31 import gov.nasa.jpf.vm.choice.DoubleChoiceFromList;
32 import gov.nasa.jpf.vm.choice.IntIntervalGenerator;
34 import java.util.Comparator;
36 import org.junit.Test;
39 * regression test for choice reordering APIs
41 public class CGReorderTest extends TestJPF {
43 public static class ReverseListener extends ListenerAdapter {
45 public void choiceGeneratorSet (VM vm, ChoiceGenerator<?> newCG){
46 if (newCG instanceof IntIntervalGenerator){
47 System.out.println("reverse choice enumeration order");
48 ((IntIntervalGenerator)newCG).reverse();
52 int lastVal = Integer.MAX_VALUE;
54 public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG){
55 if (currentCG instanceof IntIntervalGenerator){
56 int v = ((IntChoiceGenerator)currentCG).getNextChoice();
58 fail("values not decreasing");
66 public void testReverse(){
67 if (verifyNoPropertyViolation("+listener=.test.mc.basic.CGReorderTest$ReverseListener")){
68 int x = Verify.getInt(0,4);
69 System.out.println(x);
74 public static class ReorderListener extends ListenerAdapter {
75 ChoiceGenerator<?> reorderedCG;
78 public void choiceGeneratorRegistered (VM vm, ChoiceGenerator<?> nextCG, ThreadInfo ti, Instruction executedInsn){
79 // make sure we are not getting recursive (could also use setId())
80 if (nextCG instanceof DoubleChoiceFromList && nextCG != reorderedCG){
81 System.out.println("reorder choices");
82 reorderedCG = ((DoubleChoiceFromList)nextCG).reorder( new Comparator<Double>(){
84 public int compare (Double d1, Double d2){
85 return (int) (d2 - d1);
89 System.out.println("replacing: " + nextCG);
90 System.out.println("with: " + reorderedCG);
91 SystemState ss = vm.getSystemState();
92 ss.removeNextChoiceGenerator();
93 ss.setNextChoiceGenerator(reorderedCG);
97 double lastVal = Double.MAX_VALUE;
99 public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG){
100 if (currentCG instanceof DoubleChoiceGenerator){
101 double v = ((DoubleChoiceGenerator)currentCG).getNextChoice();
103 fail("values not decreasing");
111 public void testReorder(){
112 if (verifyNoPropertyViolation("+listener=.test.mc.basic.CGReorderTest$ReorderListener")){
113 double x = Verify.getDoubleFromList(1.0, 2.0, 3.0, 4.0);
114 System.out.println(x);