Skip to content

Commit

Permalink
Add a few more tests for FixedBitSet
Browse files Browse the repository at this point in the history
Also, fix a few minor things in FixedBitSet
  • Loading branch information
lupino3 committed May 26, 2016
1 parent c27faff commit 16e058f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/org/edumips64/core/FixedBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
/** Abstract class: it contains a fixed-size BitSet instance.
* @author Salvatore Scellato
* */
public abstract class FixedBitSet {
abstract class FixedBitSet {

private BitSet bitset;
protected int size;

/** Creates a default new instance of FixedBitSet with zero size. */
public FixedBitSet() {
FixedBitSet() {
bitset = new BitSet();
size = 0;
}
Expand All @@ -53,14 +54,13 @@ public void reset(boolean value) {
/** Using a string containg binary digits (bits) this method sets the bit
* of the FixedBitSet starting from the <code>start</code> position until reaching
* the end of the string or the end of the FixedBitSet.
* TODO(andrea): should this raise an exception if len(bits) > this.size?
* @param bits string made of "0" and "1" chars
* @param start index of the first bit to be set
* @throws IrregularStringOfBitsException if the String bits does not contain only "0" and "1" chars
*/
public void setBits(String bits, int start) throws IrregularStringOfBitsException {
//System.err.println("setBits() " + bits + ", " + start);
int index = 0;

int index;
for (int i = 0; i < bits.length(); i++) {
index = i + start;

Expand All @@ -84,17 +84,19 @@ public void setBits(String bits, int start) throws IrregularStringOfBitsExceptio
}

/** Returns the bit sequence of this FixedBitSet as a string containing "0"s and "1"s.
* The sequence will contain all the bits in the bitset.
* @return string form of the bit sequence stored in this FixedBitSet
*/
public String getBinString() {
StringBuffer buf = new StringBuffer(size);

for (int i = 0; i < size; i++)
for (int i = 0; i < size; i++) {
if (bitset.get(i)) {
buf.append("1");
} else {
buf.append("0");
}
}

return new String(buf);
}
Expand Down
42 changes: 37 additions & 5 deletions test/org/edumips64/core/FixedBitSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,42 @@
* the methods are tested through BitSet64.
*/
public class FixedBitSetTest {
private BitSet64 bitset = new BitSet64();
private BitSet64 bitset = new BitSet64();

@Test(expected = IrregularStringOfBitsException.class)
public void testIrregularStringOfBits() throws Exception {
bitset.setBits("blah", 0);
}
// Tests that strings containing characters other than 1 and 0 raise exceptions.
@Test(expected = IrregularStringOfBitsException.class)
public void testIrregularStringOfBits() throws Exception {
bitset.setBits("blah01", 0);
}

/**
* Tests that getBinString() outputs exactly the string given in input to setBits,
* assuming the full width of the bitset is covered in the setBits call.
*/
@Test()
public void testGetIsReciprocalToSet() throws Exception {
String binEleven64Bits = "0000000000000000000000000000000000000000000000000000000000001011";
bitset.setBits(binEleven64Bits, 0);
String actualBitString = bitset.getBinString();
assertEquals(binEleven64Bits, actualBitString);
}

// Test all the unsigned byte values.
@Test()
public void testByteValues() throws Exception {
// 32 zeros, used as a prefix for the expected string.
String zeroPrefix = "00000000000000000000000000000000";
for(int i = 1; i < 256; ++i) {
String binary = Integer.toBinaryString(i);
bitset.setBits(binary, 64 - binary.length());
// Add the required padding, since getBinString always returns a string long N bits
// (where N is the length of the bitset);
String expected = zeroPrefix;
for (int j = 0; j < Integer.numberOfLeadingZeros(i); ++j) {
expected += '0';
}
expected += binary;
assertEquals(expected, bitset.getBinString());
}
}
}

0 comments on commit 16e058f

Please sign in to comment.