KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > unit > TestBitField


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.unit;
6
7 import java.util.BitSet JavaDoc;
8 import java.util.Random JavaDoc;
9
10 import org.h2.test.TestBase;
11 import org.h2.util.BitField;
12
13 /**
14  * @author Thomas
15  */

16
17 public class TestBitField extends TestBase {
18     
19     public void test() throws Exception JavaDoc {
20         testRandom();
21         testGetSet();
22     }
23     
24     void testRandom() throws Exception JavaDoc {
25         BitField bits = new BitField();
26         BitSet JavaDoc set = new BitSet JavaDoc();
27         int max = 300;
28         int count = 100000;
29         Random JavaDoc random = new Random JavaDoc(1);
30         for(int i=0; i<count; i++) {
31             int idx = random.nextInt(max);
32             if(random.nextBoolean()) {
33                 if(random.nextBoolean()) {
34                     bits.set(idx);
35                     set.set(idx);
36                 } else {
37                     bits.clear(idx);
38                     set.clear(idx);
39                 }
40             } else {
41                 check(bits.get(idx), set.get(idx));
42                 check(bits.nextClearBit(idx), set.nextClearBit(idx));
43                 check(bits.nextSetBit(idx), set.nextSetBit(idx));
44             }
45         }
46     }
47     
48     void testGetSet() throws Exception JavaDoc {
49         BitField bits = new BitField();
50         for(int i=0; i<10000; i++) {
51             bits.set(i);
52             if(!bits.get(i)) {
53                 throw new Exception JavaDoc("not set: "+i);
54             }
55             if(bits.get(i+1)) {
56                 throw new Exception JavaDoc("set: "+i);
57             }
58         }
59         for(int i=0; i<10000; i++) {
60             if(!bits.get(i)) {
61                 throw new Exception JavaDoc("not set: "+i);
62             }
63         }
64         for(int i=0; i<1000; i++) {
65             int k = bits.nextClearBit(0);
66             if(k != 10000) {
67                 throw new Exception JavaDoc("" + k);
68             }
69         }
70     }
71 }
72
Popular Tags