TotalInOut.java revision 8729:0242fce0f717
134192Sjdp/*
255687Sjdp * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3110804Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4217722Skib *
534192Sjdp * This code is free software; you can redistribute it and/or modify it
634192Sjdp * under the terms of the GNU General Public License version 2 only, as
734192Sjdp * published by the Free Software Foundation.
834192Sjdp *
934192Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT
1034192Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1134192Sjdp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1234192Sjdp * version 2 for more details (a copy is included in the LICENSE file that
1334192Sjdp * accompanied this code).
1434192Sjdp *
1534192Sjdp * You should have received a copy of the GNU General Public License version
1634192Sjdp * 2 along with this work; if not, write to the Free Software Foundation,
1734192Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1834192Sjdp *
1934192Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2034192Sjdp * or visit www.oracle.com if you need additional information or have any
2134192Sjdp * questions.
2234192Sjdp */
2334192Sjdp
2434192Sjdp/* @test
2534192Sjdp * @bug 7188852
2634192Sjdp * @summary Test De/Inflater.getBytesRead/Written()
2750476Speter */
2834192Sjdp
2934192Sjdpimport java.io.*;
3034192Sjdpimport java.util.*;
3134192Sjdpimport java.util.zip.*;
3234192Sjdp
3334192Sjdp
3434192Sjdppublic class TotalInOut {
3534192Sjdp     static final int BUF_SIZE= 1 * 1024 * 1024;
3634192Sjdp
3734192Sjdp     static void realMain (String[] args) throws Throwable {
3834192Sjdp         long dataSize = 128L * 1024L * 1024L;      // 128MB
3934192Sjdp         if (args.length > 0 && "large".equals(args[0]))
4034192Sjdp             dataSize = 5L * 1024L * 1024L * 1024L; //  5GB
41144062Scperciva
4234192Sjdp         Deflater deflater = new Deflater();
4350609Sjdp         Inflater inflater = new Inflater();
44211413Skib
45165916Sjhb         byte[] dataIn = new byte[BUF_SIZE];
46189959Skib         byte[] dataOut = new byte[BUF_SIZE];
47165916Sjhb         byte[] tmp = new byte[BUF_SIZE];
4834192Sjdp
4934192Sjdp         Random r = new Random();
5034192Sjdp         r.nextBytes(dataIn);
5134192Sjdp         long bytesReadDef    = 0;
5234192Sjdp         long bytesWrittenDef = 0;
5334192Sjdp         long bytesReadInf    = 0;
5434192Sjdp         long bytesWrittenInf = 0;
5534192Sjdp
5634192Sjdp         deflater.setInput(dataIn, 0, dataIn.length);
5734192Sjdp         while (bytesReadDef < dataSize || bytesWrittenInf < dataSize) {
5834192Sjdp             int len = r.nextInt(BUF_SIZE/2) + BUF_SIZE / 2;
5934192Sjdp             if (deflater.needsInput()) {
6034192Sjdp                 bytesReadDef += dataIn.length;
61113229Smdodd                 check(bytesReadDef == deflater.getBytesRead());
62133063Sdfr                 deflater.setInput(dataIn, 0, dataIn.length);
63225152Skib             }
64233694Skib             int n = deflater.deflate(tmp, 0, len);
6534192Sjdp             bytesWrittenDef += n;
66127250Speter             check(bytesWrittenDef == deflater.getBytesWritten());
67119014Sgordon
68127250Speter             inflater.setInput(tmp, 0, n);
69127250Speter             bytesReadInf += n;
70127250Speter             while (!inflater.needsInput()) {
7134192Sjdp                 bytesWrittenInf += inflater.inflate(dataOut, 0, dataOut.length);
7234192Sjdp                 check(bytesWrittenInf == inflater.getBytesWritten());
7334192Sjdp             }
74110804Skan             check(bytesReadInf == inflater.getBytesRead());
7534192Sjdp         }
7662801Sjdp     }
7734192Sjdp
7834192Sjdp     //--------------------- Infrastructure ---------------------------
7945890Sjdp     static volatile int passed = 0, failed = 0;
80153515Skan     static void pass() {passed++;}
81211413Skib     static void pass(String msg) {System.out.println(msg); passed++;}
82211413Skib     static void fail() {failed++; Thread.dumpStack();}
83211413Skib     static void fail(String msg) {System.out.println(msg); fail();}
8493610Sjake     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
8548871Sjdp     static void unexpected(Throwable t, String msg) {
8634192Sjdp         System.out.println(msg); failed++; t.printStackTrace();}
87230410Skib     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
88216695Skib     static void equal(Object x, Object y) {
89199829Skib          if (x == null ? y == null : x.equals(y)) pass();
90110804Skan          else fail(x + " not equal to " + y);}
9166056Sjdp     public static void main(String[] args) throws Throwable {
9270677Sjdp          try {realMain(args);} catch (Throwable t) {unexpected(t);}
9370677Sjdp          System.out.println("\nPassed = " + passed + " failed = " + failed);
94110804Skan          if (failed > 0) throw new AssertionError("Some tests failed");}
9534192Sjdp}
9638836Sjdp