Skip to content

Commit

Permalink
Relax Beta Encoding requirement to allow 0 bits (#1233)
Browse files Browse the repository at this point in the history
*  allow BetaIntegerEncoding to be specified with 0 bits.  
    This was disabled in #1199, as a probable error state, but it turns out that it's used in some crams created with htslib.
  • Loading branch information
jmthibault79 authored and lbergelson committed Dec 3, 2018
1 parent c596e6b commit 4ff5190
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class BetaIntegerEncoding extends CRAMEncoding<Integer> {
public BetaIntegerEncoding(final int offset, final int bitsPerValue) {
super(EncodingID.BETA);

if (bitsPerValue <= 0) {
throw new IllegalArgumentException("Number of bits per value must be positive");
if (bitsPerValue < 0) {
throw new IllegalArgumentException("Number of bits per value must not be negative");
} else if (bitsPerValue > 32) {
throw new IllegalArgumentException("Number of bits per value must be 32 or lower");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public Object[][] basicTestData() {
return new Object[][] {
{8, -100, new int[]{100, 101, 102, (1<<8) + 98, (1<<8) + 99}},
{4, 10015, new int[]{-10015, -10014, -10001, -10000}},
{0, 0, new int[]{0, 0, 0}},
{0, 100, new int[]{-100, -100}},
};
}

Expand All @@ -62,6 +64,7 @@ public Object[][] basicTestNoOffsetData() {
return new Object[][] {
{8, new int[]{0, 1, 2, 100, (1 << 8) - 2, (1 << 8) - 1}},
{16, new int[]{0, 1, 255, (1 << 16) - 2, (1 << 16) - 1}},
{0, new int[]{0, 0, 0}},
};
}

Expand All @@ -77,13 +80,15 @@ public Object[][] overflowData() {
// tuples of bitsPerValue and offsets + values which are too big to store
return new Object[][] {
// first with zero offset
{0, 0, 1},
{1, 0, (1 << 1)},
{2, 0, (1 << 2)},
{4, 0, (1 << 4)},
{8, 0, (1 << 8)},
{16, 0, (1 << 16)},

// adding offset of 1 will put it over
{0, 1, 0},
{1, 1, (1 << 1) - 1},
{2, 1, (1 << 2) - 1},
{4, 1, (1 << 4) - 1},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class BetaIntegerEncodingTest extends HtsjdkTest {
public Object[][] testData() {
return new Object[][] {
// positive values below the ITF8 single-byte limit (128) are encoded as-is
{0, 0, new byte[] { 0, 0 }},
{0, 8, new byte[] { 0, 8 }},
{127, 32, new byte[] { 127, 32 }},

Expand All @@ -32,12 +33,11 @@ public void paramsTest(final int offset, final int bitLength, final byte[] expec
}


// sanity checks for bitsPerValue. Must be > 0 and <= 32
// sanity checks for bitsPerValue. Must be between 0 and 32, inclusive

@DataProvider(name = "bitsPerValue")
public Object[][] bitsPerValueData() {
return new Object[][] {
{0},
{-1},
{33}
};
Expand Down

0 comments on commit 4ff5190

Please sign in to comment.