Skip to content

Commit

Permalink
Updated for using single-set of abstract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed Dec 8, 2016
1 parent 0b20f60 commit 2b05d01
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 65 deletions.
35 changes: 35 additions & 0 deletions src/main/java/com/sangupta/jerry/ds/bitarray/JavaBitSetArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

0 comments on commit 2b05d01

Please sign in to comment.