-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit Tests and fixes for a few classes in samtools.cram.io (#1198)
* Additional roundtrips for ExternalCompressionTest + moved inside samtools * Rename CramArray to CramIntArray and add tests - add IOTestCases and CramIntTest - rename RamInt.read to readInt32 - byte[] and ByteBuffer tests and fixes
- Loading branch information
1 parent
d504256
commit 37069a3
Showing
13 changed files
with
344 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/java/htsjdk/samtools/cram/io/CramIntArrayTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/htsjdk/samtools/cram/io/ExternalCompressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.