|
| 1 | +package org.xerial.snappy; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.junit.runners.Parameterized; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.ThreadLocalRandom; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertArrayEquals; |
| 14 | + |
| 15 | +@RunWith(Parameterized.class) |
| 16 | +public class SnappyGenerativeTest { |
| 17 | + |
| 18 | + @Parameterized.Parameters |
| 19 | + public static Iterable<Object[]> data() { |
| 20 | + List<Object[]> testCases = new ArrayList<>(100); |
| 21 | + for (int i = 0 ; i < testCases.size(); ++i) { |
| 22 | + testCases.add(randomData()); |
| 23 | + } |
| 24 | + return testCases; |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + private final byte[] input; |
| 29 | + |
| 30 | + public SnappyGenerativeTest(byte[] input) { |
| 31 | + this.input = input; |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void roundTripDirectToDirect() throws IOException { |
| 36 | + ByteBuffer in = ByteBuffer.allocateDirect(input.length); |
| 37 | + in.put(input); |
| 38 | + ByteBuffer compressed = ByteBuffer.allocateDirect(input.length * 2); |
| 39 | + Snappy.compress(in, compressed); |
| 40 | + Snappy.uncompress(compressed, in); |
| 41 | + byte[] result = new byte[input.length]; |
| 42 | + in.flip(); |
| 43 | + in.get(result); |
| 44 | + assertArrayEquals(input, result); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void roundTripDirectToHeap() throws IOException { |
| 49 | + ByteBuffer in = ByteBuffer.allocateDirect(input.length); |
| 50 | + in.put(input); |
| 51 | + in.flip(); |
| 52 | + ByteBuffer compressed = ByteBuffer.allocateDirect(input.length * 2); |
| 53 | + Snappy.compress(in, compressed); |
| 54 | + ByteBuffer out = ByteBuffer.allocate(input.length); |
| 55 | + Snappy.uncompress(compressed, out); |
| 56 | + out.flip(); |
| 57 | + assertArrayEquals(input, out.array()); |
| 58 | + } |
| 59 | + |
| 60 | + private static Object[] randomData() { |
| 61 | + int length = Math.abs(ThreadLocalRandom.current().nextInt(10,10_000)); |
| 62 | + byte[] data = new byte[length]; |
| 63 | + ThreadLocalRandom.current().nextBytes(data); |
| 64 | + return new Object[] {data}; |
| 65 | + } |
| 66 | +} |
0 commit comments