|
| 1 | +package io.github.dfa1.zstd.it; |
| 2 | + |
| 3 | +import com.github.luben.zstd.ZstdDictCompress; |
| 4 | +import com.github.luben.zstd.ZstdDictDecompress; |
| 5 | +import io.github.dfa1.zstd.Zstd; |
| 6 | +import io.github.dfa1.zstd.ZstdCompressCtx; |
| 7 | +import io.github.dfa1.zstd.ZstdDecompressCtx; |
| 8 | +import io.github.dfa1.zstd.ZstdDictionary; |
| 9 | +import io.github.dfa1.zstd.ZstdInputStream; |
| 10 | +import io.github.dfa1.zstd.ZstdOutputStream; |
| 11 | +import org.junit.jupiter.api.Nested; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.params.ParameterizedTest; |
| 14 | +import org.junit.jupiter.params.provider.Arguments; |
| 15 | +import org.junit.jupiter.params.provider.MethodSource; |
| 16 | + |
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Random; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +/// Format-compatibility tests: frames produced by the reference zstd-jni binding |
| 28 | +/// must decode with zstd-java and vice versa. zstd-jni bundles a different zstd |
| 29 | +/// version (1.5.x) than this library (1.6.0); the frame format is compatible, so |
| 30 | +/// these prove real interop, not just internal round-trips. |
| 31 | +class ZstdJniInteropTest { |
| 32 | + |
| 33 | + static Stream<Arguments> payloadsAndLevels() { |
| 34 | + Random r = new Random(0xABCD); |
| 35 | + List<Arguments> cases = new ArrayList<>(); |
| 36 | + int[] sizes = {0, 1, 1024, 64 * 1024, 1024 * 1024}; |
| 37 | + int[] levels = {Zstd.minCompressionLevel(), 1, Zstd.defaultCompressionLevel(), Zstd.maxCompressionLevel()}; |
| 38 | + for (int size : sizes) { |
| 39 | + byte[] data = size % 2 == 0 ? compressible(r, size) : random(r, size); |
| 40 | + for (int level : levels) { |
| 41 | + cases.add(Arguments.of(level, data)); |
| 42 | + } |
| 43 | + } |
| 44 | + return cases.stream(); |
| 45 | + } |
| 46 | + |
| 47 | + @ParameterizedTest |
| 48 | + @MethodSource("payloadsAndLevels") |
| 49 | + void jniCompressJavaDecompress(int level, byte[] data) { |
| 50 | + byte[] frame = com.github.luben.zstd.Zstd.compress(data, level); |
| 51 | + assertThat(Zstd.decompress(frame, data.length)).isEqualTo(data); |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @MethodSource("payloadsAndLevels") |
| 56 | + void javaCompressJniDecompress(int level, byte[] data) { |
| 57 | + byte[] frame = Zstd.compress(data, level); |
| 58 | + assertThat(com.github.luben.zstd.Zstd.decompress(frame, data.length)).isEqualTo(data); |
| 59 | + } |
| 60 | + |
| 61 | + @Nested |
| 62 | + class Streaming { |
| 63 | + |
| 64 | + @Test |
| 65 | + void javaStreamToJniStream() throws Exception { |
| 66 | + byte[] data = "interop streaming ".repeat(50_000).getBytes(StandardCharsets.UTF_8); |
| 67 | + |
| 68 | + ByteArrayOutputStream sink = new ByteArrayOutputStream(); |
| 69 | + try (ZstdOutputStream zout = new ZstdOutputStream(sink, 7)) { |
| 70 | + zout.write(data); |
| 71 | + } |
| 72 | + byte[] restored; |
| 73 | + try (var zin = new com.github.luben.zstd.ZstdInputStream( |
| 74 | + new ByteArrayInputStream(sink.toByteArray()))) { |
| 75 | + restored = zin.readAllBytes(); |
| 76 | + } |
| 77 | + assertThat(restored).isEqualTo(data); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void jniStreamToJavaStream() throws Exception { |
| 82 | + byte[] data = "interop streaming ".repeat(50_000).getBytes(StandardCharsets.UTF_8); |
| 83 | + |
| 84 | + ByteArrayOutputStream sink = new ByteArrayOutputStream(); |
| 85 | + try (var zout = new com.github.luben.zstd.ZstdOutputStream(sink)) { |
| 86 | + zout.write(data); |
| 87 | + } |
| 88 | + byte[] restored; |
| 89 | + try (ZstdInputStream zin = new ZstdInputStream(new ByteArrayInputStream(sink.toByteArray()))) { |
| 90 | + restored = zin.readAllBytes(); |
| 91 | + } |
| 92 | + assertThat(restored).isEqualTo(data); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Nested |
| 97 | + class Dictionary { |
| 98 | + |
| 99 | + @Test |
| 100 | + void javaDictCompressJniDictDecompress() { |
| 101 | + ZstdDictionary dict = trainDict(); |
| 102 | + byte[] record = record(11); |
| 103 | + |
| 104 | + byte[] frame; |
| 105 | + try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { |
| 106 | + frame = ctx.compress(record, dict); |
| 107 | + } |
| 108 | + ZstdDictDecompress jniDict = new ZstdDictDecompress(dict.toByteArray()); |
| 109 | + assertThat(com.github.luben.zstd.Zstd.decompress(frame, jniDict, record.length)).isEqualTo(record); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void jniDictCompressJavaDictDecompress() { |
| 114 | + ZstdDictionary dict = trainDict(); |
| 115 | + byte[] record = record(22); |
| 116 | + |
| 117 | + ZstdDictCompress jniDict = new ZstdDictCompress(dict.toByteArray(), Zstd.defaultCompressionLevel()); |
| 118 | + byte[] frame = com.github.luben.zstd.Zstd.compress(record, jniDict); |
| 119 | + |
| 120 | + byte[] restored; |
| 121 | + try (ZstdDecompressCtx ctx = new ZstdDecompressCtx()) { |
| 122 | + restored = ctx.decompress(frame, record.length, dict); |
| 123 | + } |
| 124 | + assertThat(restored).isEqualTo(record); |
| 125 | + } |
| 126 | + |
| 127 | + private ZstdDictionary trainDict() { |
| 128 | + List<byte[]> samples = new ArrayList<>(); |
| 129 | + for (int i = 0; i < 3000; i++) { |
| 130 | + samples.add(record(i)); |
| 131 | + } |
| 132 | + return ZstdDictionary.train(samples, 8 * 1024); |
| 133 | + } |
| 134 | + |
| 135 | + private byte[] record(int i) { |
| 136 | + return ("{\"id\":" + i + ",\"user\":\"u" + (i % 30) + "\",\"event\":\"click\"}") |
| 137 | + .getBytes(StandardCharsets.UTF_8); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static byte[] random(Random r, int size) { |
| 142 | + byte[] b = new byte[size]; |
| 143 | + r.nextBytes(b); |
| 144 | + return b; |
| 145 | + } |
| 146 | + |
| 147 | + private static byte[] compressible(Random r, int size) { |
| 148 | + byte[] b = new byte[size]; |
| 149 | + for (int i = 0; i < size; i++) { |
| 150 | + b[i] = (byte) ((i % 13 == 0) ? r.nextInt(256) : 'a' + (i % 8)); |
| 151 | + } |
| 152 | + return b; |
| 153 | + } |
| 154 | +} |
0 commit comments