Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / mc / basic / CGReorderTest.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.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;
33
34 import java.util.Comparator;
35
36 import org.junit.Test;
37
38 /**
39  * regression test for choice reordering APIs 
40  */
41 public class CGReorderTest extends TestJPF {
42
43   public static class ReverseListener extends ListenerAdapter {  
44     @Override
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();
49       }
50     }
51
52     int lastVal = Integer.MAX_VALUE;
53     @Override
54     public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG){
55       if (currentCG instanceof IntIntervalGenerator){
56         int v = ((IntChoiceGenerator)currentCG).getNextChoice();
57         if (v >= lastVal){
58           fail("values not decreasing");
59         }
60         lastVal = v;
61       }
62     }
63   }
64   
65   @Test
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);
70     }
71   }
72
73   
74   public static class ReorderListener extends ListenerAdapter {
75     ChoiceGenerator<?> reorderedCG;
76     
77     @Override
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>(){
83           @Override
84                 public int compare (Double d1, Double d2){
85             return (int) (d2 - d1);
86           }
87         });
88         
89         System.out.println("replacing: " + nextCG);
90         System.out.println("with: " + reorderedCG);
91         SystemState ss = vm.getSystemState();
92         ss.removeNextChoiceGenerator();
93         ss.setNextChoiceGenerator(reorderedCG);
94       }
95     }
96
97     double lastVal = Double.MAX_VALUE;
98     @Override
99     public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG){
100       if (currentCG instanceof DoubleChoiceGenerator){
101         double v = ((DoubleChoiceGenerator)currentCG).getNextChoice();
102         if (v >= lastVal){
103           fail("values not decreasing");
104         }
105         lastVal = v;
106       }
107     }
108   }
109   
110   @Test
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);
115     }
116   }
117   
118 }