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.util;
21 import gov.nasa.jpf.util.test.TestJPF;
23 import java.util.BitSet;
24 import java.util.Random;
26 import org.junit.Test;
29 * unit test for BitSet64
31 public class BitSet64Test extends TestJPF {
32 public static void main (String[] args){
34 // our performance evals
35 if (args.length == 1){
36 String mthName = args[0];
37 if (mthName.equals("evalBitSet")){
40 } else if (mthName.equals("evalBitSet64")){
46 // the regression tests
47 runTestsOfThisClass(args);
50 //--- regression tests
53 public void testBasic() {
54 BitSet64 b = new BitSet64();
77 assert b.cardinality() == 4;
78 assert b.length() == 64 : " wrong length: " + b.length();
87 public void testOutOfBounds() {
88 BitSet64 b = new BitSet64();
92 throw new AssertionError("set(64) failed to throw");
93 } catch (IndexOutOfBoundsException x){
94 System.out.println(x);
99 throw new AssertionError("get(256) failed to throw");
100 } catch (IndexOutOfBoundsException x){
101 System.out.println(x);
106 throw new AssertionError("clear(-1) failed to throw");
107 } catch (IndexOutOfBoundsException x){
108 System.out.println(x);
113 public void testEnumeration() {
114 BitSet64 b = new BitSet64();
116 assert b.nextSetBit(0) == -1;
119 assert b.nextSetBit(0) == 0;
123 assert b.nextSetBit(0) == 24;
127 assert b.nextSetBit(0) == 63;
136 assert b.nextSetBit(0) == 0;
137 assert b.nextSetBit(1) == 10;
138 assert b.nextSetBit(11) == 42;
139 assert b.nextSetBit(43) == 55;
143 public void testIntSetInterface(){
144 IntSet s = new BitSet64();
150 assertTrue(s.size() == 64);
151 assertTrue( s.contains(42));
153 for (IntIterator it = s.intIterator(); it.hasNext();){
155 System.out.println(i);
159 //--- performance section
161 static final int NROUNDS = 2000000;
162 static final int NITER = 500000;
164 public static void evalBitSet() {
165 Random r = new Random(0);
166 BitSet b = new BitSet(64);
168 long t1 = System.currentTimeMillis();
169 for (int j=0; j<NROUNDS; j++){
170 int i = r.nextInt(64);
175 long t2 = System.currentTimeMillis();
176 System.out.println("BitSet random access: " + (t2-t1));
185 t1 = System.currentTimeMillis();
186 for (int j=0; j<NROUNDS; j++){
187 for (int k=b.nextSetBit(0); k>=0; k=b.nextSetBit(k+1));
188 int n = b.cardinality();
190 t2 = System.currentTimeMillis();
191 System.out.println("BitSet set bits iteration: " + (t2-t1));
194 public static void evalBitSet64() {
195 Random r = new Random(0);
196 BitSet64 b = new BitSet64();
198 long t1 = System.currentTimeMillis();
199 for (int j=0; j<NROUNDS; j++){
200 int i = r.nextInt(64);
205 long t2 = System.currentTimeMillis();
206 System.out.println("BitSet random access: " + (t2-t1));
215 t1 = System.currentTimeMillis();
216 for (int j=0; j<NROUNDS; j++){
217 for (int k=b.nextSetBit(0); k>=0; k=b.nextSetBit(k+1));
218 int n = b.cardinality();
220 t2 = System.currentTimeMillis();
221 System.out.println("BitSet set bits iteration: " + (t2-t1));