2 * Copyright (C) 2015, 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.util;
20 import gov.nasa.jpf.util.test.TestJPF;
21 import org.junit.Test;
24 * regression test for PermutationGenerator
26 public class PermutationGeneratorTest extends TestJPF {
29 public void testTotalPermutation(){
30 PermutationGenerator pg = new TotalPermutationGenerator(4);
31 long nPerm = pg.getNumberOfPermutations();
32 assertTrue( nPerm == 24);
35 int[] perms = pg.next();
36 assertTrue(perms != null);
37 pg.printOn(System.out);
42 public void testPairPermutation(){
43 PermutationGenerator pg = new PairPermutationGenerator(4);
44 long nPerm = pg.getNumberOfPermutations();
45 assertTrue( nPerm == 7);
48 int[] perms = pg.next();
49 assertTrue(perms != null);
50 pg.printOn(System.out);
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);
61 System.out.println("this CAN have duplicates");
63 int[] perms = pg.next();
64 assertTrue(perms != null);
65 pg.printOn(System.out);
69 boolean isEqual (int[] a, int[] b){
70 if (a.length == b.length){
71 for (int i=0; i<a.length; i++){
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);
88 int[][] seen = new int[nPermutations][];
91 System.out.println("this should NOT have duplicates");
94 int[] perms = pg.next();
95 assertTrue(perms != null);
96 pg.printOn(System.out);
98 for (int i=0; i<n; i++){
99 assertFalse(isEqual(seen[i], perms));
101 seen[n++] = perms.clone();
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);
112 while (pg.hasNext()){
113 int[] perms = pg.next();
114 assertTrue(perms != null);
115 pg.printOn(System.out);