Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / util / PermutationGeneratorTest.java
1 /*
2  * Copyright (C) 2015, 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 package gov.nasa.jpf.util;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21 import org.junit.Test;
22
23 /**
24  * regression test for PermutationGenerator
25  */
26 public class PermutationGeneratorTest extends TestJPF {
27   
28   @Test
29   public void testTotalPermutation(){
30     PermutationGenerator pg = new TotalPermutationGenerator(4);
31     long nPerm = pg.getNumberOfPermutations();
32     assertTrue( nPerm == 24);
33     
34     while (pg.hasNext()){
35       int[] perms = pg.next();
36       assertTrue(perms != null);
37       pg.printOn(System.out);
38     }
39   }
40   
41   @Test
42   public void testPairPermutation(){
43     PermutationGenerator pg = new PairPermutationGenerator(4);
44     long nPerm = pg.getNumberOfPermutations();
45     assertTrue( nPerm == 7);
46     
47     while (pg.hasNext()){
48       int[] perms = pg.next();
49       assertTrue(perms != null);
50       pg.printOn(System.out);
51     }
52   }
53
54   @Test
55   public void testRandomPermutation(){
56     int nPermutations = 14;
57     PermutationGenerator pg = new RandomPermutationGenerator(4, nPermutations, 42);
58     long nPerm = pg.getNumberOfPermutations();
59     assertTrue( nPerm == nPermutations);
60     
61     System.out.println("this CAN have duplicates");
62     while (pg.hasNext()){
63       int[] perms = pg.next();
64       assertTrue(perms != null);
65       pg.printOn(System.out);
66     }    
67   }
68   
69   boolean isEqual (int[] a, int[] b){
70     if (a.length == b.length){
71       for (int i=0; i<a.length; i++){
72         if (a[i] != b[i]){
73           return false;
74         }
75       }
76       return true;
77     }
78     return false;
79   }
80   
81   @Test
82   public void testUniqueRandomPermutation(){
83     int nPermutations = 14;
84     PermutationGenerator pg = new UniqueRandomPermGenerator(4, nPermutations, 42);
85     long nPerm = pg.getNumberOfPermutations();
86     assertTrue( nPerm == nPermutations);
87     
88     int[][] seen = new int[nPermutations][];
89     int n = 0;
90     
91     System.out.println("this should NOT have duplicates");
92     
93     while (pg.hasNext()){
94       int[] perms = pg.next();
95       assertTrue(perms != null);
96       pg.printOn(System.out);
97       
98       for (int i=0; i<n; i++){
99         assertFalse(isEqual(seen[i], perms));
100       }
101       seen[n++] = perms.clone();
102     }    
103   }
104
105   @Test
106   public void testMaxUniqueRandomPermutation(){
107     int nPermutations = 14; // too high, this only has 3! different permutations
108     PermutationGenerator pg = new UniqueRandomPermGenerator(3, nPermutations, 42);
109     long nPerm = pg.getNumberOfPermutations();
110     assertTrue( nPerm == 6);
111
112     while (pg.hasNext()){
113       int[] perms = pg.next();
114       assertTrue(perms != null);
115       pg.printOn(System.out);
116     }
117   }
118 }