|
| 1 | +/* |
| 2 | + * |
| 3 | + * MIT License |
| 4 | + * |
| 5 | + * Copyright (c) 2020. Brockmann Consult GmbH ([email protected]) |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +package com.bc.zarr; |
| 28 | + |
| 29 | +import static org.hamcrest.Matchers.*; |
| 30 | +import static org.hamcrest.MatcherAssert.*; |
| 31 | +import static org.junit.Assert.assertNull; |
| 32 | +import static org.junit.Assert.assertNotNull; |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | +import static org.junit.Assert.assertArrayEquals; |
| 35 | + |
| 36 | +import org.junit.*; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.junit.runners.Parameterized; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.PrintWriter; |
| 42 | +import java.io.StringReader; |
| 43 | +import java.io.StringWriter; |
| 44 | +import java.nio.ByteOrder; |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.Collection; |
| 47 | + |
| 48 | +@RunWith(Parameterized.class) |
| 49 | +public class ZarrUtilsTestCompression { |
| 50 | + |
| 51 | + private ZarrHeader _zarrHeader; |
| 52 | + private final String compression; |
| 53 | + |
| 54 | + @Parameterized.Parameters |
| 55 | + public static Collection<String[]> getCompressions() { |
| 56 | + return Arrays.asList(new String[][] { |
| 57 | + {"blosc"}, |
| 58 | + {"zlib"}, |
| 59 | + {"null"} |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + public ZarrUtilsTestCompression(String compression) { |
| 64 | + this.compression = compression; |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setUp() { |
| 69 | + final int[] chunks = {5, 6}; |
| 70 | + final Compressor compressor = CompressorFactory.create(compression); |
| 71 | + final String dtype = "i4"; |
| 72 | + final int[] shape = {10, 15}; |
| 73 | + _zarrHeader = new ZarrHeader(shape, chunks, dtype, ByteOrder.BIG_ENDIAN, 3.6d, compressor, DimensionSeparator.DOT.getSeparatorChar()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void toJson() throws IOException { |
| 78 | + final StringWriter writer = new StringWriter(); |
| 79 | + |
| 80 | + ZarrUtils.toJson(_zarrHeader, writer); |
| 81 | + assertThat(strip(writer.toString()), is(equalToIgnoringWhiteSpace(expectedJson(compression)))); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @Test |
| 86 | + public void fromJson() throws IOException { |
| 87 | + //execution |
| 88 | + final ZarrHeader zarrHeader = ZarrUtils.fromJson(new StringReader(expectedJson(compression)), ZarrHeader.class); |
| 89 | + |
| 90 | + //verification |
| 91 | + assertNotNull(zarrHeader); |
| 92 | + assertThat(zarrHeader.getChunks(), is(equalTo(_zarrHeader.getChunks()))); |
| 93 | + assertThat(zarrHeader.getDtype(), is(equalTo(_zarrHeader.getDtype()))); |
| 94 | + if (compression == "null") { |
| 95 | + assertNull(zarrHeader.getCompressor()); |
| 96 | + } else { |
| 97 | + assertNotNull(zarrHeader.getCompressor()); |
| 98 | + assertThat(zarrHeader.getCompressor().toString(), is(equalTo(_zarrHeader.getCompressor().toString()))); |
| 99 | + } |
| 100 | + assertThat(zarrHeader.getFill_value().doubleValue(), is(equalTo(_zarrHeader.getFill_value().doubleValue()))); |
| 101 | + assertThat(zarrHeader.getShape(), is(equalTo(_zarrHeader.getShape()))); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + private String expectedJson(String compression) { |
| 106 | + final StringWriter sw = new StringWriter(); |
| 107 | + final PrintWriter pw = new PrintWriter(sw); |
| 108 | + pw.println("{"); |
| 109 | + pw.println(" \"chunks\": ["); |
| 110 | + pw.println(" 5,"); |
| 111 | + pw.println(" 6"); |
| 112 | + pw.println(" ],"); |
| 113 | + if (compression == "null") { |
| 114 | + pw.println(" \"compressor\": null,"); |
| 115 | + } else if (compression == "zlib") { |
| 116 | + pw.println(" \"compressor\": {"); |
| 117 | + pw.println(" \"level\": 1,"); |
| 118 | + pw.println(" \"id\": \"zlib\""); |
| 119 | + pw.println(" },"); |
| 120 | + } else if (compression == "blosc") { |
| 121 | + pw.println(" \"compressor\": {"); |
| 122 | + pw.println(" \"clevel\": 5,"); |
| 123 | + pw.println(" \"blocksize\": 0,"); |
| 124 | + pw.println(" \"shuffle\": 1,"); |
| 125 | + pw.println(" \"cname\": \"lz4\","); |
| 126 | + pw.println(" \"id\": \"blosc\""); |
| 127 | + pw.println(" },"); |
| 128 | + } |
| 129 | + pw.println(" \"dtype\": \">i4\","); |
| 130 | + pw.println(" \"fill_value\": 3.6,"); |
| 131 | + pw.println(" \"filters\": null,"); |
| 132 | + pw.println(" \"order\": \"C\","); |
| 133 | + pw.println(" \"shape\": ["); |
| 134 | + pw.println(" 10,"); |
| 135 | + pw.println(" 15"); |
| 136 | + pw.println(" ],"); |
| 137 | + pw.println(" \"dimension_separator\": \".\","); |
| 138 | + pw.println(" \"zarr_format\": 2"); |
| 139 | + pw.println("}"); |
| 140 | + |
| 141 | + return strip(sw.toString()); |
| 142 | + } |
| 143 | + |
| 144 | + private String strip(String s) { |
| 145 | + s = s.replace("\r", "").replace("\n", ""); |
| 146 | + s = s.replace(" ", ""); |
| 147 | +// while (s.contains(" ")) s = s.replace(" ", " "); |
| 148 | + return s; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments