-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit Tests and fixes for a few classes in samtools.cram.io #1198
Changes from all commits
0cb5dbf
cb9686a
66fc5e5
5f5203e
c120a4b
7677f8e
baa4a24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
/** | ||
* Methods to read and write CRAM array of integers data type. | ||
*/ | ||
public class CramArray { | ||
public class CramIntArray { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed because this gave me the impression of a generic array type, though it's limited to integer. I can revert this if you prefer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me. |
||
/** | ||
* Read CRAM int array from a {@link InputStream}. | ||
* | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package htsjdk.samtools.cram.io; | ||
|
||
import htsjdk.HtsjdkTest; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class CramIntArrayTest extends HtsjdkTest { | ||
|
||
@Test(dataProvider = "testInt32Arrays", dataProviderClass = IOTestCases.class) | ||
public void runTest(List<Integer> ints) throws IOException { | ||
|
||
int[] inputArray = ints.stream().mapToInt(Integer::intValue).toArray(); | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||
CramIntArray.write(inputArray, baos); | ||
|
||
try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { | ||
int[] outputArray = CramIntArray.array(bais); | ||
Assert.assertEquals(inputArray, outputArray, "Arrays did not match"); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package htsjdk.samtools.cram.io; | ||
|
||
import htsjdk.HtsjdkTest; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CramIntTest extends HtsjdkTest { | ||
private byte[] streamWritten(List<Integer> ints) throws IOException { | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||
for (int value : ints) { | ||
CramInt.writeInt32(value, baos); | ||
} | ||
return baos.toByteArray(); | ||
} | ||
} | ||
|
||
private byte[] byteArrayWritten(List<Integer> ints) { | ||
final int bufSize = 4; | ||
final int arraySize = bufSize * ints.size(); | ||
byte[] array = new byte[arraySize]; | ||
|
||
int offset = 0; | ||
byte[] arrayBuffer; | ||
|
||
for (int value : ints) { | ||
arrayBuffer = CramInt.writeInt32(value); | ||
System.arraycopy(arrayBuffer, 0, array, offset, bufSize); | ||
offset += bufSize; | ||
} | ||
|
||
return array; | ||
} | ||
|
||
@Test(dataProvider = "littleEndianTests32", dataProviderClass = IOTestCases.class) | ||
public void checkStreamLittleEndian(Integer testInt, byte[] expected) throws IOException { | ||
List<Integer> ints = new ArrayList<>(); | ||
ints.add(testInt); | ||
|
||
byte[] actual = streamWritten(ints); | ||
Assert.assertEquals(actual, expected); | ||
} | ||
|
||
@Test(dataProvider = "littleEndianTests32", dataProviderClass = IOTestCases.class) | ||
public void checkByteArrayLittleEndian(Integer testInt, byte[] expected) { | ||
List<Integer> ints = new ArrayList<>(); | ||
ints.add(testInt); | ||
|
||
byte[] actual = byteArrayWritten(ints); | ||
Assert.assertEquals(actual, expected); | ||
} | ||
|
||
// Combinatorial tests of 2 CramInt write methods x 3 CramInt read methods | ||
|
||
@Test(dataProvider = "testInt32Arrays", dataProviderClass = IOTestCases.class) | ||
public void matchStreamRead(List<Integer> ints) throws IOException { | ||
byte[][] inputs = {streamWritten(ints), byteArrayWritten(ints)}; | ||
|
||
for (byte[] byteArray : inputs) { | ||
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) { | ||
for (int value : ints) { | ||
int fromStream = CramInt.readInt32(bais); | ||
Assert.assertEquals(fromStream, value, "Value did not match"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test(dataProvider = "testInt32Arrays", dataProviderClass = IOTestCases.class) | ||
public void matchBufferRead(List<Integer> ints) throws IOException { | ||
byte[][] inputs = {streamWritten(ints), byteArrayWritten(ints)}; | ||
|
||
for (byte[] byteArray : inputs) { | ||
ByteBuffer bb = ByteBuffer.wrap(byteArray); | ||
|
||
for (int value : ints) { | ||
int fromBuffer = CramInt.readInt32(bb); | ||
Assert.assertEquals(fromBuffer, value, "Value did not match"); | ||
} | ||
} | ||
} | ||
|
||
@Test(dataProvider = "testInt32Arrays", dataProviderClass = IOTestCases.class) | ||
public void matchByteArrayRead(List<Integer> ints) throws IOException { | ||
byte[][] inputs = {streamWritten(ints), byteArrayWritten(ints)}; | ||
|
||
for (byte[] inputArray : inputs) { | ||
final int bufSize = 4; | ||
byte[] outBuf = new byte[bufSize]; | ||
int offset = 0; | ||
|
||
for (int value : ints) { | ||
System.arraycopy(inputArray, offset, outBuf, 0, bufSize); | ||
int fromBuffer = CramInt.readInt32(outBuf); | ||
Assert.assertEquals(fromBuffer, value, "Value did not match"); | ||
offset += bufSize; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package htsjdk.samtools.cram.io; | ||
|
||
import htsjdk.HtsjdkTest; | ||
import htsjdk.samtools.cram.encoding.rans.RANS; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
public class ExternalCompressionTest extends HtsjdkTest { | ||
public static final File BZIP2_FILE = new File("src/test/resources/htsjdk/samtools/cram/io/bzip2-test.bz2"); | ||
public static final byte[] TEST_BYTES = "This is a simple string to test compression".getBytes(); | ||
|
||
@Test | ||
public void testBZip2Decompression() throws IOException { | ||
final byte [] input = Files.readAllBytes(BZIP2_FILE.toPath()); | ||
final byte [] output = ExternalCompression.unbzip2(input); | ||
Assert.assertEquals(output, "BZip2 worked".getBytes()); | ||
} | ||
|
||
@Test | ||
public void testGZipRoundtrip() throws IOException { | ||
final byte [] compressed = ExternalCompression.gzip(TEST_BYTES); | ||
final byte [] restored = ExternalCompression.gunzip(compressed); | ||
Assert.assertEquals(TEST_BYTES, restored); | ||
} | ||
|
||
@Test | ||
public void testBZip2Roundtrip() throws IOException { | ||
final byte [] compressed = ExternalCompression.bzip2(TEST_BYTES); | ||
final byte [] restored = ExternalCompression.unbzip2(compressed); | ||
Assert.assertEquals(TEST_BYTES, restored); | ||
} | ||
|
||
@Test | ||
public void testRANSRoundtrip() { | ||
for(RANS.ORDER order : RANS.ORDER.values()) { | ||
final byte[] compressed = ExternalCompression.rans(TEST_BYTES, order); | ||
final byte[] restored = ExternalCompression.unrans(compressed); | ||
Assert.assertEquals(TEST_BYTES, restored); | ||
} | ||
} | ||
|
||
@Test | ||
public void testXZRoundtrip() throws IOException { | ||
final byte [] compressed = ExternalCompression.xz(TEST_BYTES); | ||
final byte [] restored = ExternalCompression.unxz(compressed); | ||
Assert.assertEquals(TEST_BYTES, restored); | ||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These shifts were being attempted in a
byte
contextThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a test case that fails without this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does my
0xff
comment work for you on this point?