Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.zarr</groupId>
<artifactId>jzarr</artifactId>
<version>0.4.0</version>
<version>0.4.1-SNAPSHOT</version>

<name>JZarr</name>

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/bc/zarr/CompressorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

package com.bc.zarr;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.jna.ptr.NativeLongByReference;
import org.blosc.BufferSizes;
import org.blosc.IBloscDll;
Expand Down Expand Up @@ -321,6 +322,7 @@ public String getCname() {
return cname;
}

@JsonIgnore
public int getNumThreads() {
return nthreads;
}
Expand Down
94 changes: 93 additions & 1 deletion src/test/java/com/bc/zarr/CompressorFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,96 @@ public void create_compressor_not_supported() {
assertEquals("Compressor id:'kkkkkkk' not supported.", expected.getMessage());
}
}
}

@Test
public void createBloscValidCnames() {
String[] cnames = { "zstd", "blosclz", "lz4", "lz4hc", "zlib" };
for (int i = 0; i < cnames.length; i += 1) {
final Compressor compressor = CompressorFactory.create("blosc", "cname", cnames[i]);
assertNotNull(compressor);
assertEquals("blosc", compressor.getId());
assertEquals(
"compressor=blosc/cname=" + cnames[i] +
"/clevel=5/blocksize=0/shuffle=1", compressor.toString());
}
}

@Test
public void createBloscInvalidCname() {
try {
CompressorFactory.create("blosc", "cname", "unsupported");
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertEquals("blosc: compressor not supported: 'unsupported'; expected one of [zstd, blosclz, lz4, lz4hc, zlib]", expected.getMessage());
}
}

@Test
public void createBloscValidClevel() {
final Compressor compressor = CompressorFactory.create("blosc", "clevel", 1);
assertNotNull(compressor);
assertEquals("blosc", compressor.getId());
assertEquals(
"compressor=blosc/cname=lz4" +
"/clevel=1/blocksize=0/shuffle=1", compressor.toString());
}

@Test
public void createBloscInvalidClevel() {
try {
CompressorFactory.create("blosc", "clevel", -1);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertEquals("blosc: clevel parameter must be between 0 and 9 but was: -1", expected.getMessage());
}

try {
CompressorFactory.create("blosc", "clevel", 10);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertEquals(
"blosc: clevel parameter must be between 0 and 9 but was: 10",
expected.getMessage());
}
}

@Test
public void createBloscValidShuffles() {
int[] shuffles = { 0, 1, 2 };
for (int i = 0; i < shuffles.length; i += 1) {
final Compressor compressor = CompressorFactory.create("blosc", "shuffle", shuffles[i]);
assertNotNull(compressor);
assertEquals("blosc", compressor.getId());
assertEquals(
"compressor=blosc/cname=lz4" +
"/clevel=5/blocksize=0/shuffle=" +
shuffles[i], compressor.toString());
}
}

@Test
public void createBloscInvalidShuffle() {
try {
CompressorFactory.create("blosc", "shuffle", -1);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertEquals(
"blosc: shuffle type not supported: '-1'; expected one of [0 (NOSHUFFLE), 1 (BYTESHUFFLE), 2 (BITSHUFFLE)]",
expected.getMessage());
}
}

@Test
public void createBloscValidBlockSizes() {
int[] blockSizes = { 0, 1, 20 };
for (int i = 0; i < blockSizes.length; i += 1) {
final Compressor compressor = CompressorFactory.create("blosc", "blocksize", blockSizes[i]);
assertNotNull(compressor);
assertEquals("blosc", compressor.getId());
assertEquals(
"compressor=blosc/cname=lz4" +
"/clevel=5/blocksize=" + blockSizes[i] +
"/shuffle=1", compressor.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public void setUp() throws Exception {
}

@Test
public void getCompressor() throws IOException {
public void getNullCompressor() throws IOException {
final int[] shape = {1, 1};
final int[] chunkShape = {1, 1};
final DataType dataType = DataType.i1; // Byte
final Compressor compressor = CompressorFactory.nullCompressor;
final Compressor compressor = CompressorFactory.create("null");
final ArrayParams parameters = new ArrayParams()
.shape(shape).chunks(chunkShape)
.dataType(dataType)
Expand All @@ -72,6 +72,36 @@ public void getCompressor() throws IOException {
assertEquals(compressor, array.getCompressor());
}

@Test
public void getBloscCompressor() throws IOException {
final int[] shape = {1, 1};
final int[] chunkShape = {1, 1};
final DataType dataType = DataType.i1; // Byte
final Compressor compressor = CompressorFactory.create("blosc");
final ArrayParams parameters = new ArrayParams()
.shape(shape).chunks(chunkShape)
.dataType(dataType)
.compressor(compressor);
final ZarrArray array = ZarrArray.create(
new ZarrPath(arrayName), store, parameters, null);
assertEquals(compressor, array.getCompressor());
}

@Test
public void getZlibCompressor() throws IOException {
final int[] shape = {1, 1};
final int[] chunkShape = {1, 1};
final DataType dataType = DataType.i1; // Byte
final Compressor compressor = CompressorFactory.create("zlib");
final ArrayParams parameters = new ArrayParams()
.shape(shape).chunks(chunkShape)
.dataType(dataType)
.compressor(compressor);
final ZarrArray array = ZarrArray.create(
new ZarrPath(arrayName), store, parameters, null);
assertEquals(compressor, array.getCompressor());
}

@Test
public void writeAndRead_Byte_Full() throws IOException, InvalidRangeException {
//preparation
Expand Down
151 changes: 151 additions & 0 deletions src/test/java/com/bc/zarr/ZarrUtilsTestCompression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
*
* MIT License
*
* Copyright (c) 2020. Brockmann Consult GmbH (info@brockmann-consult.de)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package com.bc.zarr;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;

import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class ZarrUtilsTestCompression {

private ZarrHeader _zarrHeader;
private final String compression;

@Parameterized.Parameters
public static Collection<String[]> getCompressions() {
return Arrays.asList(new String[][] {
{"blosc"},
{"zlib"},
{"null"}
});
}

public ZarrUtilsTestCompression(String compression) {
this.compression = compression;
}

@Before
public void setUp() {
final int[] chunks = {5, 6};
final Compressor compressor = CompressorFactory.create(compression);
final String dtype = "i4";
final int[] shape = {10, 15};
_zarrHeader = new ZarrHeader(shape, chunks, dtype, ByteOrder.BIG_ENDIAN, 3.6d, compressor, DimensionSeparator.DOT.getSeparatorChar());
}

@Test
public void toJson() throws IOException {
final StringWriter writer = new StringWriter();

ZarrUtils.toJson(_zarrHeader, writer);
assertThat(strip(writer.toString()), is(equalToIgnoringWhiteSpace(expectedJson(compression))));
}


@Test
public void fromJson() throws IOException {
//execution
final ZarrHeader zarrHeader = ZarrUtils.fromJson(new StringReader(expectedJson(compression)), ZarrHeader.class);

//verification
assertNotNull(zarrHeader);
assertThat(zarrHeader.getChunks(), is(equalTo(_zarrHeader.getChunks())));
assertThat(zarrHeader.getDtype(), is(equalTo(_zarrHeader.getDtype())));
if (compression == "null") {
assertNull(zarrHeader.getCompressor());
} else {
assertNotNull(zarrHeader.getCompressor());
assertThat(zarrHeader.getCompressor().toString(), is(equalTo(_zarrHeader.getCompressor().toString())));
}
assertThat(zarrHeader.getFill_value().doubleValue(), is(equalTo(_zarrHeader.getFill_value().doubleValue())));
assertThat(zarrHeader.getShape(), is(equalTo(_zarrHeader.getShape())));
}


private String expectedJson(String compression) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
pw.println("{");
pw.println(" \"chunks\": [");
pw.println(" 5,");
pw.println(" 6");
pw.println(" ],");
if (compression == "null") {
pw.println(" \"compressor\": null,");
} else if (compression == "zlib") {
pw.println(" \"compressor\": {");
pw.println(" \"level\": 1,");
pw.println(" \"id\": \"zlib\"");
pw.println(" },");
} else if (compression == "blosc") {
pw.println(" \"compressor\": {");
pw.println(" \"clevel\": 5,");
pw.println(" \"blocksize\": 0,");
pw.println(" \"shuffle\": 1,");
pw.println(" \"cname\": \"lz4\",");
pw.println(" \"id\": \"blosc\"");
pw.println(" },");
}
pw.println(" \"dtype\": \">i4\",");
pw.println(" \"fill_value\": 3.6,");
pw.println(" \"filters\": null,");
pw.println(" \"order\": \"C\",");
pw.println(" \"shape\": [");
pw.println(" 10,");
pw.println(" 15");
pw.println(" ],");
pw.println(" \"dimension_separator\": \".\",");
pw.println(" \"zarr_format\": 2");
pw.println("}");

return strip(sw.toString());
}

private String strip(String s) {
s = s.replace("\r", "").replace("\n", "");
s = s.replace(" ", "");
// while (s.contains(" ")) s = s.replace(" ", " ");
return s;
}

}