From fbd65b755e41980e6c88a675a0b07d5be66d6011 Mon Sep 17 00:00:00 2001 From: Potato Date: Fri, 5 Jul 2024 18:53:12 +0800 Subject: [PATCH] enhance test for direct buffer (#146) Signed-off-by: OneSizeFitQuorum --- .../org/apache/tsfile/compress/LZ4Test.java | 27 +++++++++++++++++++ .../org/apache/tsfile/compress/LZMA2Test.java | 2 +- .../org/apache/tsfile/compress/ZstdTest.java | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/java/tsfile/src/test/java/org/apache/tsfile/compress/LZ4Test.java b/java/tsfile/src/test/java/org/apache/tsfile/compress/LZ4Test.java index 299c70f73..761699bc0 100644 --- a/java/tsfile/src/test/java/org/apache/tsfile/compress/LZ4Test.java +++ b/java/tsfile/src/test/java/org/apache/tsfile/compress/LZ4Test.java @@ -20,6 +20,7 @@ import org.apache.tsfile.compress.ICompressor.LZ4Compressor; import org.apache.tsfile.compress.IUnCompressor.LZ4UnCompressor; +import org.apache.tsfile.utils.ReadWriteIOUtils; import org.junit.After; import org.junit.Assert; @@ -27,6 +28,7 @@ import org.junit.Test; import java.io.IOException; +import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.concurrent.ThreadLocalRandom; @@ -101,4 +103,29 @@ public void testBytes3() throws IOException { unCompressor.uncompress(compressed, offset, compressedLength, uncompressed, 0); Assert.assertArrayEquals(origin, uncompressed); } + + @Test + public void testByteBuffer() throws IOException { + for (int i = 1; i < 500000; i += 100000) { + String input = randomString(i); + ByteBuffer source = ByteBuffer.allocateDirect(input.getBytes().length); + source.put(input.getBytes()); + source.flip(); + + ICompressor.LZ4Compressor compressor = new ICompressor.LZ4Compressor(); + ByteBuffer compressed = + ByteBuffer.allocateDirect(Math.max(source.remaining() * 3 + 1, 28 + source.remaining())); + compressor.compress(source, compressed); + + IUnCompressor.LZ4UnCompressor unCompressor = new IUnCompressor.LZ4UnCompressor(); + ByteBuffer uncompressedByteBuffer = + ByteBuffer.allocateDirect(compressed.remaining() + 28 * 2); + compressed.flip(); + unCompressor.uncompress(compressed, uncompressedByteBuffer); + + uncompressedByteBuffer.flip(); + String afterDecode = ReadWriteIOUtils.readStringFromDirectByteBuffer(uncompressedByteBuffer); + assert input.equals(afterDecode); + } + } } diff --git a/java/tsfile/src/test/java/org/apache/tsfile/compress/LZMA2Test.java b/java/tsfile/src/test/java/org/apache/tsfile/compress/LZMA2Test.java index 36aaf367d..ff7188ef1 100644 --- a/java/tsfile/src/test/java/org/apache/tsfile/compress/LZMA2Test.java +++ b/java/tsfile/src/test/java/org/apache/tsfile/compress/LZMA2Test.java @@ -80,7 +80,7 @@ public void testBytes2() throws IOException { } @Test - public void testBytes3() throws IOException { + public void testByteBuffer() throws IOException { for (int i = 0; i < 500; i += 1) { String input = randomString(i); ByteBuffer source = ByteBuffer.allocateDirect(input.getBytes().length); diff --git a/java/tsfile/src/test/java/org/apache/tsfile/compress/ZstdTest.java b/java/tsfile/src/test/java/org/apache/tsfile/compress/ZstdTest.java index 803de7125..dd0e6c171 100644 --- a/java/tsfile/src/test/java/org/apache/tsfile/compress/ZstdTest.java +++ b/java/tsfile/src/test/java/org/apache/tsfile/compress/ZstdTest.java @@ -48,7 +48,7 @@ public void setUp() {} public void tearDown() {} @Test - public void testBytes1() throws IOException { + public void testBytebuffer() throws IOException { for (int i = 1; i < 500000; i += 100000) { String input = randomString(i); ByteBuffer source = ByteBuffer.allocateDirect(input.getBytes().length);