Blame t/060-inflate.t

Packit Service 4d3269
# -*- mode: perl -*-
Packit Service 4d3269
Packit Service 4d3269
use Test::More tests => 5;
Packit Service 4d3269
Packit Service 4d3269
## stream uncompress sample2 from the bzip2 1.0.2 distribution
Packit Service 4d3269
## compare against bunzip2 command with od -x and diff
Packit Service 4d3269
Packit Service 4d3269
BEGIN {
Packit Service 4d3269
  use_ok('Compress::Bzip2');
Packit Service 4d3269
};
Packit Service 4d3269
Packit Service 4d3269
our ( $debugf, $BZIP );
Packit Service 4d3269
Packit Service 4d3269
do './t/lib.pl';
Packit Service 4d3269
Packit Service 4d3269
my $INFILE = catfile( qw(bzlib-src sample2.bz2) );
Packit Service 4d3269
( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/;
Packit Service 4d3269
my $PREFIX = catfile( qw(t 060-tmp) );
Packit Service 4d3269
Packit Service 4d3269
my ( $in, $out, $d, $outbuf, $status, $counter, $bytes, $bytesout );
Packit Service 4d3269
$debugf = $ENV{DEBUG} ? 1 : 0;
Packit Service 4d3269
open( $in, "< $INFILE" ) or die "$INFILE: $!";
Packit Service 4d3269
open( $out, "> $PREFIX-out.txt" ) or die "$PREFIX-out.txt: $!";
Packit Service 4d3269
Packit Service 4d3269
## verbosity 0-4, small 0,1, blockSize100k 1-9, workFactor 0-250, readUncompressed 0,1
Packit Service 4d3269
( $d, $status ) = bzinflateInit( -verbosity => $debugf ? 4 : 0 );
Packit Service 4d3269
Packit Service 4d3269
ok( $d && $status == BZ_OK, "bzinflateInit was successful" );
Packit Service 4d3269
Packit Service 4d3269
$counter = 0;
Packit Service 4d3269
$bytes = 0;
Packit Service 4d3269
$bytesout = 0;
Packit Service 4d3269
while ( my $ln = sysread( $in, $buf, 512 ) ) {
Packit Service 4d3269
  # test buf as scalar or scalarref
Packit Service 4d3269
  ( $outbuf, $status ) = $d->bzinflate( $counter % 2 ? \$buf : $buf );
Packit Service 4d3269
  # the 2nd attempt to read from the 512 buf in bzfile_streambuf_read will cause a BZ_IO_ERROR
Packit Service 4d3269
  if ( $status != BZ_IO_ERROR && $status != BZ_STREAM_END && $status != BZ_OK || !defined($outbuf) ) {
Packit Service 4d3269
    diag "error: $outbuf $bzerrno\n";
Packit Service 4d3269
    last;
Packit Service 4d3269
  }
Packit Service 4d3269
Packit Service 4d3269
  if ( $outbuf ne '' ) {
Packit Service 4d3269
    syswrite( $out, $outbuf );
Packit Service 4d3269
    $bytesout += length($outbuf);
Packit Service 4d3269
  }
Packit Service 4d3269
Packit Service 4d3269
  $bytes += $ln;
Packit Service 4d3269
  $counter++;
Packit Service 4d3269
}
Packit Service 4d3269
Packit Service 4d3269
( $outbuf, $status ) = $d->bzclose;
Packit Service 4d3269
ok( $status == BZ_OK, "bzclose was successful" );
Packit Service 4d3269
Packit Service 4d3269
if ( defined($outbuf) && $outbuf ne '' ) {
Packit Service 4d3269
  syswrite( $out, $outbuf );
Packit Service 4d3269
  $bytesout += length($outbuf);
Packit Service 4d3269
  
Packit Service 4d3269
  $counter++;
Packit Service 4d3269
}
Packit Service 4d3269
Packit Service 4d3269
ok( $bytes && $bytesout, "$counter blocks read, $bytes bytes in, $bytesout bytes out" );
Packit Service 4d3269
Packit Service 4d3269
close($in);
Packit Service 4d3269
close($out);
Packit Service 4d3269
Packit Service 4d3269
#system( "$BZIP -d < $INFILE | od -x > $PREFIX-reference-txt.odx" );
Packit Service 4d3269
#system( "od -x < $PREFIX-out.txt > $PREFIX-out-txt.odx" );
Packit Service 4d3269
#system( "diff $PREFIX-out-txt.odx $PREFIX-reference-txt.odx > $PREFIX-diff.txt" );
Packit Service 4d3269
#ok( ! -s "$PREFIX-diff.txt", "no differences with bunzip2" );
Packit Service 4d3269
Packit Service 4d3269
ok ( compare_binary_files( "$PREFIX-out.txt", $MODELFILE ), 'no differences with decompressing $INFILE' );