-
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.
This is more complicated than originally described because the file encoding isn't really described and java bytes include negative values. EOF is triggered correctly, but it's also triggered on various potential characters which become negative values when they are truncated to byte. It's unclear if this is meant to only read ASCII, ISO 8859-1, or UTF-8 but nothing outside of the ascii space works correctly.
- Loading branch information
1 parent
f684576
commit e5ba94c
Showing
5 changed files
with
124 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package htsjdk.testutil; | ||
|
||
import org.testng.Assert; | ||
|
||
public interface Expected<T> { | ||
void test(ThrowingSupplier<T> functionToTest); | ||
|
||
|
||
interface ThrowingConsumer<T> { | ||
void test(T a) throws Exception; | ||
} | ||
|
||
static <T> Expected<T> match(final T expected) { | ||
return new ComparisonExpected<>((T actual) -> Assert.assertEquals(actual, expected)); | ||
} | ||
|
||
static <T> Expected<T> mismatch(final T expected) { | ||
return new ComparisonExpected<>((T actual) -> Assert.assertNotEquals(actual, expected)); | ||
} | ||
|
||
static <T> Expected<T> exception(final Class<? extends Exception> exceptionClass) { | ||
return functionToTest -> Assert.assertThrows(exceptionClass, functionToTest::produce); | ||
} | ||
|
||
interface ThrowingSupplier<T> { | ||
T produce() throws Exception; | ||
} | ||
} | ||
|
||
final class ComparisonExpected<T> implements Expected<T> { | ||
private final ThrowingConsumer<T> test; | ||
|
||
@Override | ||
public void test(ThrowingSupplier<T> supplier) { | ||
try { | ||
test.test(supplier.produce()); | ||
} catch (AssertionError e) { | ||
throw e; | ||
} catch (Exception e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
|
||
ComparisonExpected(ThrowingConsumer<T> test) { | ||
this.test = test; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/htsjdk/tribble/util/LittleEndianInputStreamTest.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,45 @@ | ||
package htsjdk.tribble.util; | ||
|
||
import htsjdk.HtsjdkTest; | ||
import htsjdk.testutil.Expected; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.EOFException; | ||
import java.io.FileInputStream; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
public class LittleEndianInputStreamTest extends HtsjdkTest { | ||
|
||
|
||
@DataProvider | ||
public Object[][] testCases() { | ||
final String missingTerminator = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_no_terminator.bin"; | ||
final String extendedAsciiFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin"; | ||
final Object utf8File = "src/test/resources/htsjdk/tribble/util/string_with_utf8_emoji_and_null_terminator.txt"; | ||
return new Object[][]{ | ||
{missingTerminator, StandardCharsets.ISO_8859_1, Expected.exception(EOFException.class)}, | ||
{missingTerminator, StandardCharsets.US_ASCII, Expected.exception(EOFException.class)}, | ||
{missingTerminator, StandardCharsets.UTF_8, Expected.exception(EOFException.class)}, | ||
{extendedAsciiFile, StandardCharsets.ISO_8859_1, Expected.match("very dràààààmatic and null terminated")}, | ||
{extendedAsciiFile, StandardCharsets.US_ASCII, Expected.mismatch("very dràààààmatic and null terminated")}, | ||
{extendedAsciiFile, StandardCharsets.UTF_8, Expected.mismatch("very dràààààmatic and null terminated")}, | ||
{utf8File, StandardCharsets.UTF_8, Expected.match("🐋 UTF8 is Great 🐋")}, | ||
{utf8File, StandardCharsets.ISO_8859_1, Expected.mismatch("🐋 UTF8 is Great 🐋")} | ||
}; | ||
} | ||
|
||
@Test(dataProvider = "testCases") | ||
public void testAllCases(String filename, Charset charset, Expected<String> expected) { | ||
expected.test(() -> { | ||
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(Files.newInputStream(Paths.get(filename))))){ | ||
return in.readString(charset); | ||
} | ||
}); | ||
} | ||
|
||
} |