| 34 | import org.junit.Test; |
| 35 | |
| 36 | public class ZstdTest { |
| 37 | |
| 38 | static { |
| 39 | Os.init(); |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void testCompressThenDecompressHighlyCompressibleRuns() throws Exception { |
| 44 | TestUtils.assertMemoryLeak(() -> { |
| 45 | // Runs of repeated bytes should compress dramatically. |
| 46 | int len = 256 * 1024; |
| 47 | long src = Unsafe.malloc(len, MemoryTag.NATIVE_DEFAULT); |
| 48 | try { |
| 49 | for (int i = 0; i < len; i++) { |
| 50 | Unsafe.putByte(src + i, (byte) (i / 1024)); |
| 51 | } |
| 52 | roundTrip(src, len); |
| 53 | } finally { |
| 54 | Unsafe.free(src, len, MemoryTag.NATIVE_DEFAULT); |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testCompressThenDecompressRandom() throws Exception { |
| 61 | TestUtils.assertMemoryLeak(() -> { |
| 62 | int len = 64 * 1024; |
| 63 | long src = Unsafe.malloc(len, MemoryTag.NATIVE_DEFAULT); |
| 64 | try { |
| 65 | Rnd rnd = new Rnd(); |
| 66 | for (int i = 0; i < len; i++) { |
| 67 | Unsafe.putByte(src + i, (byte) rnd.nextInt()); |
| 68 | } |
| 69 | roundTrip(src, len); |
| 70 | } finally { |
| 71 | Unsafe.free(src, len, MemoryTag.NATIVE_DEFAULT); |
| 72 | } |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testCompressTooSmallDestinationReturnsError() throws Exception { |
| 78 | TestUtils.assertMemoryLeak(() -> { |
| 79 | // A realistic payload compressed into a destination one byte wide cannot fit |
| 80 | // even the zstd frame header. The native side must return a negative error |
| 81 | // code, not silently truncate. |
| 82 | int len = 4096; |
| 83 | long src = Unsafe.malloc(len, MemoryTag.NATIVE_DEFAULT); |
| 84 | long dst = Unsafe.malloc(1, MemoryTag.NATIVE_DEFAULT); |
| 85 | long cctx = Zstd.createCCtx(3); |
| 86 | try { |
| 87 | for (int i = 0; i < len; i++) { |
| 88 | Unsafe.putByte(src + i, (byte) i); |
| 89 | } |
| 90 | long result = Zstd.compress(cctx, src, len, dst, 1); |
| 91 | Assert.assertTrue("expected negative error, got " + result, result < 0); |
| 92 | } finally { |
| 93 | Zstd.freeCCtx(cctx); |
nothing calls this directly
no test coverage detected
searching dependent graphs…