Skip to content

Commit

Permalink
Fix java/util/zip/DeflateIn_InflateOut.skipBytes Test
Browse files Browse the repository at this point in the history
java/util/zip/DeflateIn_InflateOut.skipBytes verifies functionality of
DeflaterInputStream.skipBytes. While testing skipping of bytes, test
uses different input data with same data length, and compares the
compressed sizes of these data to verify skipping bytes. As per zlib
specification which is what is used by Deflater in java, length of
compressed data is highly dependable on input data contents, so it will
be incorrect to assume that compression ratio would be constant for same
length input data. For more details please see explaination in
eclipse-openj9/openj9#14948 (comment).

Signed-off-by: Rahil Shah <rahil@ca.ibm.com>
  • Loading branch information
r30shah committed Jul 25, 2022
1 parent f96abef commit 05cf0e0
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions test/jdk/java/util/zip/DeflateIn_InflateOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ public class DeflateIn_InflateOut {
private static ByteArrayOutputStream baos;
private static InflaterOutputStream ios;

private static void reset() {
new Random(new Date().getTime()).nextBytes(data);

private static void resetStreams() {
bais = new ByteArrayInputStream(data);
dis = new DeflaterInputStream(bais);

baos = new ByteArrayOutputStream();
ios = new InflaterOutputStream(baos);
}

private static void resetAll() {
new Random(new Date().getTime()).nextBytes(data);
resetStreams();
}

/** Check byte arrays read/write. */
private static void ArrayReadWrite() throws Throwable {
byte[] buf = new byte[512];

reset();
resetAll();
check(dis.available() == 1);
for (;;) {
int len = dis.read(buf, 0, buf.length);
Expand All @@ -74,7 +77,7 @@ private static void ArrayReadWrite() throws Throwable {
private static void ArrayReadByteWrite() throws Throwable {
byte[] buf = new byte[512];

reset();
resetAll();
for (;;) {
int len = dis.read(buf, 0, buf.length);
if (len <= 0) {
Expand All @@ -100,7 +103,7 @@ private static void ByteReadArrayWrite() throws Throwable {
byte[] buf = new byte[8192];
int off = 0;

reset();
resetAll();
int datum = dis.read();
while (datum != -1) {
if (off == 8192) {
Expand All @@ -126,7 +129,7 @@ private static void ByteReadByteWrite() throws Throwable {
byte[] buf = new byte[512];
boolean reachEOF = false;

reset();
resetAll();
while (dis.available() == 1) {
int datum = dis.read();
if (datum == -1) {
Expand All @@ -149,7 +152,7 @@ private static void SkipBytes() throws Throwable {
int numReadable = 0;

// Count number of bytes that are read
reset();
resetAll();
check(dis.available() == 1);
for (;;) {
int count = dis.read(buf, 0, buf.length);
Expand All @@ -162,7 +165,7 @@ private static void SkipBytes() throws Throwable {
check(dis.available() == 0);

// Verify that skipping the first several bytes works.
reset();
resetStreams();
int numNotSkipped = 0;
int numSkipBytes = 2053; // arbitrarily chosen prime
check(dis.skip(numSkipBytes) == numSkipBytes);
Expand All @@ -177,7 +180,7 @@ private static void SkipBytes() throws Throwable {
check(numNotSkipped + numSkipBytes == numReadable);

// Verify that skipping some bytes mid-stream works.
reset();
resetStreams();
numNotSkipped = 0;
numSkipBytes = 8887; // arbitrarily chosen prime
for (int i = 0; ; i++) {
Expand All @@ -195,7 +198,7 @@ private static void SkipBytes() throws Throwable {
check(numNotSkipped + numSkipBytes == numReadable);

// Verify that skipping the last N bytes works.
reset();
resetStreams();
numNotSkipped = 0;
numSkipBytes = 6449; // arbitrarily chosen prime
for (int i = 0; ; i++) {
Expand Down

0 comments on commit 05cf0e0

Please sign in to comment.