-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f7771d
commit 6c13594
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/java/com/google/devtools/build/lib/remote/zstd/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
load("@rules_java//java:defs.bzl", "java_test") | ||
|
||
package( | ||
default_testonly = 1, | ||
default_visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
licenses(["notice"]) | ||
|
||
filegroup( | ||
name = "srcs", | ||
testonly = 0, | ||
srcs = glob(["**"]), | ||
visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
java_test( | ||
name = "zstd", | ||
srcs = glob(["*.java"]), | ||
test_class = "com.google.devtools.build.lib.AllTests", | ||
deps = [ | ||
"//src/main/java/com/google/devtools/build/lib/remote/zstd:zstd", | ||
"//src/test/java/com/google/devtools/build/lib:test_runner", | ||
"//third_party:junit4", | ||
"//third_party:truth", | ||
"@zstd-jni//:zstd-jni", | ||
], | ||
) |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/google/devtools/build/lib/remote/zstd/ZstdCompressingInputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.google.devtools.build.lib.remote.zstd; | ||
|
||
import com.github.luben.zstd.Zstd; | ||
import com.google.common.io.ByteStreams; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Random; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ZstdCompressingInputStreamTest { | ||
@Test | ||
public void compressionWorks() throws IOException { | ||
Random rand = new Random(); | ||
byte[] data = new byte[50]; | ||
rand.nextBytes(data); | ||
|
||
ByteArrayInputStream bais = new ByteArrayInputStream(data); | ||
ZstdCompressingInputStream zdis = new ZstdCompressingInputStream(bais); | ||
|
||
ByteArrayOutputStream compressed = new ByteArrayOutputStream(); | ||
ByteStreams.copy(zdis, compressed); | ||
|
||
assertThat(Zstd.decompress(compressed.toByteArray(), data.length)).isEqualTo(data); | ||
} | ||
|
||
@Test | ||
public void streamCanBeCompressedWithMinimumBufferSize() throws IOException { | ||
Random rand = new Random(); | ||
byte[] data = new byte[50]; | ||
rand.nextBytes(data); | ||
|
||
ByteArrayInputStream bais = new ByteArrayInputStream(data); | ||
ZstdCompressingInputStream zdis = | ||
new ZstdCompressingInputStream(bais, ZstdCompressingInputStream.MIN_BUFFER_SIZE); | ||
|
||
ByteArrayOutputStream compressed = new ByteArrayOutputStream(); | ||
ByteStreams.copy(zdis, compressed); | ||
|
||
assertThat(Zstd.decompress(compressed.toByteArray(), data.length)).isEqualTo(data); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...est/java/com/google/devtools/build/lib/remote/zstd/ZstdDecompressingOutputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.google.devtools.build.lib.remote.zstd; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.github.luben.zstd.Zstd; | ||
import com.github.luben.zstd.ZstdOutputStream; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Random; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ZstdDecompressingOutputStreamTest { | ||
@Test | ||
public void decompressionWorks() throws IOException { | ||
Random rand = new Random(); | ||
byte[] data = new byte[50]; | ||
rand.nextBytes(data); | ||
byte[] compressed = Zstd.compress(data); | ||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
ZstdDecompressingOutputStream zdos = new ZstdDecompressingOutputStream(baos); | ||
zdos.write(compressed); | ||
zdos.flush(); | ||
|
||
assertThat(baos.toByteArray()).isEqualTo(data); | ||
} | ||
|
||
@Test | ||
public void streamCanBeDecompressedOneByteAtATime() throws IOException { | ||
Random rand = new Random(); | ||
byte[] data = new byte[50]; | ||
rand.nextBytes(data); | ||
byte[] compressed = Zstd.compress(data); | ||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
ZstdDecompressingOutputStream zdos = new ZstdDecompressingOutputStream(baos); | ||
for (byte b : compressed) { | ||
zdos.write(b); | ||
} | ||
zdos.flush(); | ||
|
||
assertThat(baos.toByteArray()).isEqualTo(data); | ||
} | ||
|
||
@Test | ||
public void bytesWrittenMatchesDecompressedBytes() throws IOException { | ||
byte[] data = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes(); | ||
|
||
ByteArrayOutputStream compressed = new ByteArrayOutputStream(); | ||
ZstdOutputStream zos = new ZstdOutputStream(compressed); | ||
zos.setCloseFrameOnFlush(true); | ||
for (int i = 0; i < data.length; i++) { | ||
zos.write(data[i]); | ||
if (i % 5 == 0) { | ||
// Create multiple frames of 5 bytes each. | ||
zos.flush(); | ||
} | ||
} | ||
zos.close(); | ||
|
||
ByteArrayOutputStream decompressed = new ByteArrayOutputStream(); | ||
ZstdDecompressingOutputStream zdos = new ZstdDecompressingOutputStream(decompressed); | ||
for (byte b : compressed.toByteArray()) { | ||
zdos.write(b); | ||
zdos.flush(); | ||
assertThat(zdos.getBytesWritten()).isEqualTo(decompressed.toByteArray().length); | ||
} | ||
assertThat(decompressed.toByteArray()).isEqualTo(data); | ||
} | ||
} |