1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.maintenance.sync;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Random;
import static com.yahoo.vespa.hosted.node.admin.maintenance.sync.ZstdCompressingInputStream.compressor;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author freva
*/
public class ZstdCompressingInputStreamTest {
@Test
void compression_test() {
Random rnd = new Random();
byte[] data = new byte[(int) (100_000 * (10 + rnd.nextDouble()))];
rnd.nextBytes(data);
assertCompression(data, 1 << 14);
}
@Test
void compress_empty_file_test() {
byte[] compressedData = compress(new byte[0], 1 << 10);
assertEquals(13, compressedData.length, "zstd compressing an empty file results in a 13 bytes file");
}
private static void assertCompression(byte[] data, int bufferSize) {
byte[] compressedData = compress(data, bufferSize);
byte[] decompressedData = new byte[data.length];
compressor.decompress(compressedData, 0, compressedData.length, decompressedData, 0, decompressedData.length);
assertArrayEquals(data, decompressedData);
}
private static byte[] compress(byte[] data, int bufferSize) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZstdCompressingInputStream zcis = new ZstdCompressingInputStream(bais, bufferSize)) {
byte[] buffer = new byte[bufferSize];
for (int nRead; (nRead = zcis.read(buffer, 0, buffer.length)) != -1; )
baos.write(buffer, 0, nRead);
baos.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return baos.toByteArray();
}
}
|