Fix: else-if block was missing code, so the "user" option for console publisher was...
[jpf-core.git] / src / tests / gov / nasa / jpf / util / BitSet64Test.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.util;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22
23 import java.util.BitSet;
24 import java.util.Random;
25
26 import org.junit.Test;
27
28 /**
29  * unit test for BitSet64
30  */
31 public class BitSet64Test extends TestJPF {
32   public static void main (String[] args){
33
34     // our performance evals
35     if (args.length == 1){
36       String mthName = args[0];
37       if (mthName.equals("evalBitSet")){
38         evalBitSet();
39         return;
40       } else if (mthName.equals("evalBitSet64")){
41         evalBitSet64();
42         return;
43       }
44     }
45
46     // the regression tests
47     runTestsOfThisClass(args);
48   }
49
50   //--- regression tests
51
52   @Test
53   public void testBasic() {
54     BitSet64 b = new BitSet64();
55
56     assert b.isEmpty();
57     assert !b.get(0);
58
59     b.set(0);
60     assert b.get(0);
61
62     b.set(63);
63     assert b.get(63);
64
65     b.set(27);
66     assert b.get(27);
67
68     b.set(58);
69     assert b.get(58);
70
71     b.set(31);
72     assert b.get(31);
73
74
75     b.clear(31);
76
77     assert b.cardinality() == 4;
78     assert b.length() == 64 : " wrong length: " + b.length();
79
80     b = new BitSet64();
81     b.set(4);
82     b.set(4,false);
83     assert !b.get(4);
84   }
85
86   @Test
87   public void testOutOfBounds() {
88     BitSet64 b = new BitSet64();
89
90     try {
91       b.set(64);
92       throw new AssertionError("set(64) failed to throw");
93     } catch (IndexOutOfBoundsException x){
94       System.out.println(x);
95     }
96
97     try {
98       b.get(256);
99       throw new AssertionError("get(256) failed to throw");
100     } catch (IndexOutOfBoundsException x){
101       System.out.println(x);
102     }
103
104     try {
105       b.clear(-1);
106       throw new AssertionError("clear(-1) failed to throw");
107     } catch (IndexOutOfBoundsException x){
108       System.out.println(x);
109     }
110   }
111
112   @Test
113   public void testEnumeration() {
114     BitSet64 b = new BitSet64();
115
116     assert b.nextSetBit(0) == -1;
117
118     b.set(0);
119     assert b.nextSetBit(0) == 0;
120
121     b.clear();
122     b.set(24);
123     assert b.nextSetBit(0) == 24;
124
125     b.clear();
126     b.set(63);
127     assert b.nextSetBit(0) == 63;
128
129
130     b.clear();
131     b.set(0);
132     b.set(42);
133     b.set(10);
134     b.set(55);
135
136     assert b.nextSetBit(0) == 0;
137     assert b.nextSetBit(1) == 10;
138     assert b.nextSetBit(11) == 42;
139     assert b.nextSetBit(43) == 55;
140   }
141
142   @Test
143   public void testIntSetInterface(){
144     IntSet s = new BitSet64();
145     
146     s.add(42);
147     s.add(0);
148     s.add(63);
149     
150     assertTrue(s.size() == 64);
151     assertTrue( s.contains(42));
152     
153     for (IntIterator it = s.intIterator(); it.hasNext();){
154       int i = it.next();
155       System.out.println(i);
156     }
157   }
158   
159   //--- performance section
160
161   static final int NROUNDS = 2000000;
162   static final int NITER = 500000;
163
164   public static void evalBitSet() {
165     Random r = new Random(0);
166     BitSet b = new BitSet(64);
167
168     long t1 = System.currentTimeMillis();
169     for (int j=0; j<NROUNDS; j++){
170       int i = r.nextInt(64);
171       b.set(i);
172       b.get(i);
173       b.clear(i);
174     }
175     long t2 = System.currentTimeMillis();
176     System.out.println("BitSet random access: " + (t2-t1));
177
178     b.clear();
179     b.set(0);
180     b.set(42);
181     b.set(10);
182     b.set(32);
183     b.set(60);
184
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();
189     }
190     t2 = System.currentTimeMillis();
191     System.out.println("BitSet set bits iteration: " + (t2-t1));
192   }
193
194   public static void evalBitSet64() {
195     Random r = new Random(0);
196     BitSet64 b = new BitSet64();
197
198     long t1 = System.currentTimeMillis();
199     for (int j=0; j<NROUNDS; j++){
200       int i = r.nextInt(64);
201       b.set(i);
202       b.get(i);
203       b.clear(i);
204     }
205     long t2 = System.currentTimeMillis();
206     System.out.println("BitSet random access: " + (t2-t1));
207
208     b.clear();
209     b.set(0);
210     b.set(42);
211     b.set(10);
212     b.set(32);
213     b.set(60);
214
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();
219     }
220     t2 = System.currentTimeMillis();
221     System.out.println("BitSet set bits iteration: " + (t2-t1));
222   }
223
224 }