diff --git a/src/main/java/com/sangupta/jerry/ds/bitarray/JavaBitSetArray.java b/src/main/java/com/sangupta/jerry/ds/bitarray/JavaBitSetArray.java index 4151b45..a1cef0e 100644 --- a/src/main/java/com/sangupta/jerry/ds/bitarray/JavaBitSetArray.java +++ b/src/main/java/com/sangupta/jerry/ds/bitarray/JavaBitSetArray.java @@ -42,9 +42,12 @@ public class JavaBitSetArray implements BitArray { final BitSet bitSet; final int size; + + final int maxIndex; public JavaBitSetArray(int numBits) { this.bitSet = new BitSet(numBits); + this.maxIndex = numBits; this.size = this.bitSet.size(); } @@ -55,22 +58,54 @@ public void clear() { @Override public boolean getBit(int index) { + if(index < 0) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + + if(index > this.maxIndex) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + return this.bitSet.get(index); } @Override public boolean setBit(int index) { + if(index < 0) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + + if(index > this.maxIndex) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + this.bitSet.set(index); return true; } @Override public void clearBit(int index) { + if(index < 0) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + + if(index > this.maxIndex) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + this.bitSet.clear(index); } @Override public boolean setBitIfUnset(int index) { + if(index < 0) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + + if(index > this.maxIndex) { + throw new IndexOutOfBoundsException("Index is out of range: " + index); + } + if(!this.bitSet.get(index)) { return this.setBit(index); } diff --git a/src/test/java/com/sangupta/jerry/ds/bitarray/TestJavaBitSetArray.java b/src/test/java/com/sangupta/jerry/ds/bitarray/TestJavaBitSetArray.java index 3aaf973..1f76324 100644 --- a/src/test/java/com/sangupta/jerry/ds/bitarray/TestJavaBitSetArray.java +++ b/src/test/java/com/sangupta/jerry/ds/bitarray/TestJavaBitSetArray.java @@ -22,73 +22,16 @@ package com.sangupta.jerry.ds.bitarray; -import java.util.Random; +public class TestJavaBitSetArray extends TestAbstractBitArray { -import org.junit.Assert; -import org.junit.Test; - -public class TestJavaBitSetArray { - - private static final int MAX_ELEMENTS = 1000 * 10; - - @Test - public void testGetAndSet() throws Exception { - BitArray ba = new JavaBitSetArray(MAX_ELEMENTS); - - Random random = new Random(); - for(int index = 0; index < MAX_ELEMENTS; index++) { - int nextBit = random.nextInt(MAX_ELEMENTS); - boolean didSet = ba.setBit(nextBit); - if(didSet) { - boolean isSet = ba.getBit(nextBit); - Assert.assertTrue(isSet); - ba.clearBit(nextBit); - isSet = ba.getBit(nextBit); - Assert.assertFalse(isSet); - } - } - - ba.close(); - } - - @Test - public void testGetHighestBitSet() throws Exception { - BitArray ba = new JavaBitSetArray(MAX_ELEMENTS); - - Random random = new Random(); - int currentMaxBit = -1; - for(int index = 0; index < MAX_ELEMENTS; index++) { - int nextBit = random.nextInt(MAX_ELEMENTS); - boolean didSet = ba.setBit(nextBit); - if(!didSet) { - continue; - } - - currentMaxBit = Math.max(currentMaxBit, nextBit); - Assert.assertEquals(currentMaxBit, ba.getHighestBitSet()); - } - - ba.close(); + @Override + protected BitArray getNewBitArray() throws Exception { + return new JavaBitSetArray(MAX_ELEMENTS); } - - @Test - public void testGetLowestBitSet() throws Exception { - BitArray ba = new JavaBitSetArray(MAX_ELEMENTS); - - Random random = new Random(); - int currentMinBit = Integer.MAX_VALUE; - for(int index = 0; index < MAX_ELEMENTS; index++) { - int nextBit = random.nextInt(MAX_ELEMENTS); - boolean didSet = ba.setBit(nextBit); - if(!didSet) { - continue; - } - - currentMinBit = Math.min(currentMinBit, nextBit); - Assert.assertEquals(currentMinBit, ba.getLowestBitSet()); - } - - ba.close(); + + @Override + protected int getMaxElements() { + return 1024 * 10; // JavaBitSet is too slow } }