Skip to content

Commit

Permalink
Add unit test in BitSetSuite for BitSet#anySet()
Browse files Browse the repository at this point in the history
  • Loading branch information
tedyu committed May 6, 2015
1 parent 83f9f87 commit b51dcaf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public boolean isSet(int index) {
public int nextSetBit(int fromIndex) {
return BitSetMethods.nextSetBit(baseObject, baseOffset, fromIndex, numWords);
}

/*
* @return whether any bit in the BitSet is set
*/
public boolean anySet() {
return BitSetMethods.anySet(baseObject, baseOffset, numWords*BitSetMethods.WORD_SIZE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
*/
public final class BitSetMethods {

private static final long WORD_SIZE = 8;
private static final int SIZE_OF_LONG = Long.SIZE;
static final long WORD_SIZE = 8;

private BitSetMethods() {
// Make the default constructor private, since this only holds static methods.
Expand Down Expand Up @@ -72,10 +71,10 @@ public static boolean isSet(Object baseObject, long baseOffset, int index) {
* Returns {@code true} if any bit is set.
*/
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInBytes) {
assert bitSetWidthInBytes % SIZE_OF_LONG == 0;
int widthInLong = (int)(bitSetWidthInBytes / SIZE_OF_LONG);
assert bitSetWidthInBytes % WORD_SIZE == 0;
int widthInLong = (int)(bitSetWidthInBytes / WORD_SIZE);
long addr = baseOffset;
for (int i = 0; i <= widthInLong; i++, addr += SIZE_OF_LONG) {
for (int i = 0; i <= widthInLong; i++, addr += WORD_SIZE) {
if (PlatformDependent.UNSAFE.getLong(baseObject, addr) != 0) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void basicOps() {
for (int i = 0; i < bs.capacity(); i++) {
Assert.assertFalse(bs.isSet(i));
}
Assert.assertFalse(bs.anySet());

// Set every bit and check it.
for (int i = 0; i < bs.capacity(); i++) {
Expand Down

0 comments on commit b51dcaf

Please sign in to comment.