01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.unit;
07:
08: import java.util.BitSet;
09: import java.util.Random;
10:
11: import org.h2.test.TestBase;
12: import org.h2.util.BitField;
13:
14: /**
15: * A unit test for bit fields.
16: */
17: public class TestBitField extends TestBase {
18:
19: public void test() throws Exception {
20: testRandom();
21: testGetSet();
22: }
23:
24: void testRandom() throws Exception {
25: BitField bits = new BitField();
26: BitSet set = new BitSet();
27: int max = 300;
28: int count = 100000;
29: Random random = new Random(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 {
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("not set: " + i);
54: }
55: if (bits.get(i + 1)) {
56: throw new Exception("set: " + i);
57: }
58: }
59: for (int i = 0; i < 10000; i++) {
60: if (!bits.get(i)) {
61: throw new Exception("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("" + k);
68: }
69: }
70: }
71: }
|