|
20 | 20 | import org.xerial.snappy.Snappy; |
21 | 21 | import org.xerial.snappy.BitShuffle; |
22 | 22 | import java.util.Arrays; |
| 23 | +import java.util.function.Function; |
23 | 24 |
|
24 | 25 | public class BitShuffleFuzzer { |
25 | 26 | private static final int SIZE = 4096; |
26 | 27 |
|
27 | 28 | public static void fuzzerTestOneInput(FuzzedDataProvider data) { |
28 | | - fuzzBitshuffleInts(data.consumeInts(SIZE)); |
29 | | - fuzzBitshuffleLongs(data.consumeLongs(SIZE)); |
30 | | - fuzzBitshuffleShorts(data.consumeShorts(SIZE)); |
31 | | - } |
32 | | - |
33 | | - private static void runFuzz(RunnableWithException block) { |
34 | | - try { |
35 | | - block.run(); |
36 | | - } catch (Exception e) { |
37 | | - throw new RuntimeException(e); |
38 | | - } |
| 29 | + fuzzBitshuffle(data.consumeInts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleIntArray, "int[]"); |
| 30 | + fuzzBitshuffle(data.consumeLongs(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleLongArray, "long[]"); |
| 31 | + fuzzBitshuffle(data.consumeShorts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleShortArray, "short[]"); |
39 | 32 | } |
40 | 33 |
|
41 | 34 | @FunctionalInterface |
42 | | - private interface RunnableWithException { |
| 35 | + private interface ExceptionRunnable { |
43 | 36 | void run() throws Exception; |
44 | 37 | } |
45 | 38 |
|
46 | | - private static void fuzzBitshuffleInts(int[] original) { |
47 | | - runFuzz(() -> { |
48 | | - byte[] shuffledByteArray = BitShuffle.shuffle(original); |
49 | | - byte[] compressed = Snappy.compress(shuffledByteArray); |
50 | | - byte[] uncompressed = Snappy.uncompress(compressed); |
51 | | - int[] result = BitShuffle.unshuffleIntArray(uncompressed); |
52 | | - if (!Arrays.equals(original, result)) { |
53 | | - throw new IllegalStateException("Original and uncompressed data are different"); |
54 | | - } |
55 | | - }); |
| 39 | + @FunctionalInterface |
| 40 | + private interface ShuffleFn<T> { |
| 41 | + byte[] apply(T input) throws Exception; |
56 | 42 | } |
57 | 43 |
|
58 | | - private static void fuzzBitshuffleLongs(long[] original) { |
59 | | - runFuzz(() -> { |
60 | | - byte[] shuffledByteArray = BitShuffle.shuffle(original); |
61 | | - byte[] compressed = Snappy.compress(shuffledByteArray); |
62 | | - byte[] uncompressed = Snappy.uncompress(compressed); |
63 | | - long[] result = BitShuffle.unshuffleLongArray(uncompressed); |
64 | | - if (!Arrays.equals(original, result)) { |
65 | | - throw new IllegalStateException("Original and uncompressed data are different"); |
66 | | - } |
67 | | - }); |
| 44 | + @FunctionalInterface |
| 45 | + private interface UnshuffleFn<T> { |
| 46 | + T apply(byte[] input) throws Exception; |
68 | 47 | } |
69 | 48 |
|
70 | | - private static void fuzzBitshuffleShorts(short[] original) { |
71 | | - runFuzz(() -> { |
72 | | - byte[] shuffledByteArray = BitShuffle.shuffle(original); |
| 49 | + private static <T> void fuzzBitshuffle(T original, ShuffleFn<T> shuffle, UnshuffleFn<T> unshuffle, String typeName) { |
| 50 | + try { |
| 51 | + byte[] shuffledByteArray = shuffle.apply(original); |
73 | 52 | byte[] compressed = Snappy.compress(shuffledByteArray); |
74 | 53 | byte[] uncompressed = Snappy.uncompress(compressed); |
75 | | - short[] result = BitShuffle.unshuffleShortArray(uncompressed); |
76 | | - if (!Arrays.equals(original, result)) { |
77 | | - throw new IllegalStateException("Original and uncompressed data are different"); |
| 54 | + T result = unshuffle.apply(uncompressed); |
| 55 | + if (!Arrays.equals((Object[]) original, (Object[]) result)) { |
| 56 | + throw new IllegalStateException("Original and uncompressed " + typeName + " data are different"); |
78 | 57 | } |
79 | | - }); |
| 58 | + } catch (Exception e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
80 | 61 | } |
81 | 62 | } |
0 commit comments