KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > utilint > BitMapTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: BitMapTest.java,v 1.4 2006/10/30 21:14:54 bostic Exp $
7  */

8
9 package com.sleepycat.je.utilint;
10
11 import junit.framework.TestCase;
12
13
14 public class BitMapTest extends TestCase {
15
16     public void testSegments() {
17         
18         BitMap bmap = new BitMap();
19         int startBit = 15;
20         int endBit = 62;
21         assertEquals(0, bmap.cardinality());
22         assertEquals(0, bmap.getNumSegments());
23
24         assertFalse(bmap.get(1001L));
25         assertEquals(0, bmap.getNumSegments());
26
27         /* set a bit in different segments. */
28         for (int i = startBit; i <= endBit; i++) {
29             long index = 1L << i;
30             index += 17;
31             bmap.set(index);
32         }
33
34         assertEquals((endBit - startBit +1), bmap.cardinality());
35         assertEquals((endBit - startBit + 1), bmap.getNumSegments());
36         
37         /* should be set. */
38         for (int i = startBit; i <= endBit; i++) {
39             long index = 1L << i;
40             index += 17;
41             assertTrue(bmap.get(index));
42         }
43
44         /* should be clear. */
45         for (int i = startBit; i <= endBit; i++) {
46             long index = 7 + (1L << i);
47             assertFalse(bmap.get(index));
48         }
49
50         /* checking for non-set bits should not create more segments. */
51         assertEquals((endBit - startBit +1), bmap.cardinality());
52         assertEquals((endBit - startBit + 1), bmap.getNumSegments());
53     }
54
55     public void testNegative() {
56         BitMap bMap = new BitMap();
57         
58         try {
59             bMap.set(-300);
60             fail("should have thrown exception");
61         } catch (IndexOutOfBoundsException JavaDoc expected) {
62         }
63     }
64 }
65
Popular Tags