-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #321 from jamesmudd/issue-318
#318 Add handling in shuffle filter for mismatched length
- Loading branch information
Showing
7 changed files
with
161 additions
and
14 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
60 changes: 60 additions & 0 deletions
60
jhdf/src/test/java/io/jhdf/filter/ByteShuffleFilterTest.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,60 @@ | ||
/* | ||
* This file is part of jHDF. A pure Java library for accessing HDF5 files. | ||
* | ||
* http://jhdf.io | ||
* | ||
* Copyright (c) 2021 James Mudd | ||
* | ||
* MIT License see 'LICENSE' file | ||
*/ | ||
package io.jhdf.filter; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
class ByteShuffleFilterTest { | ||
|
||
@Test | ||
void testShuffleElementSize2() { | ||
byte[] input = new byte[]{1, 3, 5, 7, 9, 2, 4, 6, 8, 10}; | ||
ByteShuffleFilter byteShuffleFilter = new ByteShuffleFilter(); | ||
|
||
byte[] output = byteShuffleFilter.decode(input, new int[]{2}); | ||
assertThat(output, is(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})); | ||
} | ||
|
||
// See https://github.com/jamesmudd/jhdf/issues/318 | ||
// When input length is not a multiple of the element size | ||
@Test | ||
void testShuffleElementSize4() { | ||
byte[] input = new byte[]{1, 5, 2, 6, 3, 7, 4, 8, 9, 10}; | ||
ByteShuffleFilter byteShuffleFilter = new ByteShuffleFilter(); | ||
|
||
byte[] output = byteShuffleFilter.decode(input, new int[]{4}); | ||
assertThat(output, is(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})); | ||
} | ||
|
||
@Test | ||
void testShuffleWithSingleValue() { | ||
byte[] input = new byte[]{1}; | ||
ByteShuffleFilter byteShuffleFilter = new ByteShuffleFilter(); | ||
|
||
byte[] output = byteShuffleFilter.decode(input, new int[]{2}); | ||
assertThat(output, is(new byte[]{1})); | ||
} | ||
|
||
// See https://github.com/jamesmudd/jhdf/issues/318 | ||
// When input length is not a multiple of the element size | ||
@Test | ||
void testShuffleWithMismatchedLength() { | ||
byte[] input = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, -40, -50, -60, -70}; | ||
ByteShuffleFilter byteShuffleFilter = new ByteShuffleFilter(); | ||
|
||
byte[] output = byteShuffleFilter.decode(input, new int[]{8}); | ||
assertThat(output, is(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, -40, -50, -60, -70})); | ||
} | ||
|
||
|
||
} |
58 changes: 58 additions & 0 deletions
58
jhdf/src/test/java/io/jhdf/filter/DeflatePipelineFilterTest.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,58 @@ | ||
/* | ||
* This file is part of jHDF. A pure Java library for accessing HDF5 files. | ||
* | ||
* http://jhdf.io | ||
* | ||
* Copyright (c) 2021 James Mudd | ||
* | ||
* MIT License see 'LICENSE' file | ||
*/ | ||
package io.jhdf.filter; | ||
|
||
import io.jhdf.exceptions.HdfFilterException; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.zip.Deflater; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class DeflatePipelineFilterTest { | ||
|
||
@Test | ||
void decodeWorks() { | ||
byte[] input = StringUtils.repeat( "TestString", 50).getBytes(StandardCharsets.UTF_8); | ||
byte[] compressedBuffer = new byte[1024]; | ||
|
||
Deflater deflater = new Deflater(); | ||
deflater.setInput(input); | ||
deflater.finish(); // This is all the input | ||
|
||
// Do the compression | ||
int size = deflater.deflate(compressedBuffer, 0, input.length); | ||
byte[] compressed = Arrays.copyOf(compressedBuffer, size); | ||
|
||
DeflatePipelineFilter deflatePipelineFilter = new DeflatePipelineFilter(); | ||
byte[] decoded = deflatePipelineFilter.decode(compressed, new int[0]); | ||
|
||
assertThat(decoded, is(input)); | ||
} | ||
|
||
@Test | ||
void decodeZeroLengthThrows() { | ||
DeflatePipelineFilter deflatePipelineFilter = new DeflatePipelineFilter(); | ||
assertThrows(HdfFilterException.class, () -> deflatePipelineFilter.decode(new byte[0], new int[0])); | ||
} | ||
|
||
@Test | ||
void decodeMalformedThrows() { | ||
DeflatePipelineFilter deflatePipelineFilter = new DeflatePipelineFilter(); | ||
assertThrows(HdfFilterException.class, () -> deflatePipelineFilter.decode(new byte[]{1,2,3}, new int[0])); | ||
} | ||
} |
Binary file not shown.