diff options
Diffstat (limited to 't')
34 files changed, 2014 insertions, 0 deletions
diff --git a/t/010-useability.t b/t/010-useability.t new file mode 100644 index 0000000..81abfe7 --- /dev/null +++ b/t/010-useability.t @@ -0,0 +1,32 @@ +# -*- mode: perl -*- + +use Test::More tests => 3; + +## this tests the useability of the package +## also, tests the version number of bzlib, although the +## boot code should bail if the version isn't right + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +my $fail; +foreach my $constname (qw( + BZ_CONFIG_ERROR BZ_DATA_ERROR BZ_DATA_ERROR_MAGIC BZ_FINISH + BZ_FINISH_OK BZ_FLUSH BZ_FLUSH_OK BZ_IO_ERROR BZ_MAX_UNUSED + BZ_MEM_ERROR BZ_OK BZ_OUTBUFF_FULL BZ_PARAM_ERROR BZ_RUN BZ_RUN_OK + BZ_SEQUENCE_ERROR BZ_STREAM_END BZ_UNEXPECTED_EOF)) { + next if (eval "my \$a = $constname; 1"); + if ($@ =~ /^Your vendor has not defined Compress::Bzip2 macro $constname/) { + print "# pass: $@"; + } else { + print "# fail: $@"; + $fail = 1; + } +} + +ok( !$fail, "constants imported ok" ); + +my $version = bzlibversion(); +ok( $version && $version =~ /^1\./, "bzlib version is $version" ); + diff --git a/t/011-prototype.t b/t/011-prototype.t new file mode 100644 index 0000000..06e309f --- /dev/null +++ b/t/011-prototype.t @@ -0,0 +1,54 @@ +# -*- mode: perl -*- + +use Test::More tests => 9; + +## compress sample1 from the bzip2 1.0.2 distribution + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample1.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 011-tmp) ); + +my $in; +open( $in, $INFILE ); + +my $d; +ok( eval '$d = bzopen( "$PREFIX-sample.bz2", "w" ); 1;', "bzopen prototype ok" ); +ok( $d, "bzopen $PREFIX-sample.bz2 successful" ); +ok( eval '$d->bzsetparams( -blockSize100k => 1 ); 1;', "bzsetparams prototype ok" ); + +my $counter = 0; +my $bytes = 0; +my $buf; +my ( $evalres1, $evalres2 ); +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out1 = -1; + my $out2 = -1; + $evalres1 = eval '$out1 = $d->bzwrite( $buf, $ln ); 1;'; + last if !$evalres1; + + $evalres2 = eval '$out2 = $d->bzwrite( $buf ); 1;'; + last if !$evalres2; + + if ( $out1 < 0 || $out2 < 0 || $out1 != $out2 ) { + print STDERR "error: $out1 $out2 $Compress::Bzip2::bzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( defined($evalres1), 'bzwrite prototype $$$ ok'.($@ ? " - $@" : '') ); +ok( defined($evalres2), 'bzwrite prototype $$ ok'.($@ ? " - $@" : '') ); +ok( $counter, "$counter blocks were read, $bytes bytes" ); + +my $res; +ok( eval '$res = $d->bzclose; 1;', 'bzclose prototype ok' ); +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($in); + diff --git a/t/020-compfile.t b/t/020-compfile.t new file mode 100644 index 0000000..7a47e11 --- /dev/null +++ b/t/020-compfile.t @@ -0,0 +1,48 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## compress a simple text file - the lyrics to end of the world REM +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample0.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 020-tmp) ); + +my $in; +open( $in, $INFILE ); + +my $d = bzopen( "$PREFIX-sample.bz2", "w" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out = $d->bzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $Compress::Bzip2::bzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( $counter, "$counter blocks were read, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($in); + +ok ( compare_binary_files( "$PREFIX-sample.bz2", $MODELFILE ), 'no differences with reference' ); +#system( "bzip2 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); + +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); diff --git a/t/021-compfile.t b/t/021-compfile.t new file mode 100644 index 0000000..f751dde --- /dev/null +++ b/t/021-compfile.t @@ -0,0 +1,59 @@ +# -*- mode: perl -*- + +use Test::More tests => 7; + +## compress sample1 from the bzip2 1.0.2 distribution +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample1.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 021-tmp) ); + +my $in; +open( $in, $INFILE ); + +my $d = bzopen( "$PREFIX-sample.bz2", "w" ); +$d->bzsetparams( -blockSize100k => 1 ); + + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out = $d->bzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $Compress::Bzip2::bzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( $counter, "$counter blocks were read, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($in); + +ok ( compare_binary_files( "$PREFIX-sample.bz2", $MODELFILE ), 'no differences with reference' ); + +$d = bzopen( "$PREFIX-sample.bz2", "w" ); +my $out = $d->bzwrite( $buf, '' ); +is($out, 0, "cpan #36246 do not shortcut compressing a 0-byte file"); +if ( $out < 0 ) { + diag "error: $out $Compress::Bzip2::bzerrno\n"; +} +$d->bzclose; +ok(-s "$PREFIX-sample.bz2", "filesize > 0"); + +#system( "bzip2 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); + +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); diff --git a/t/022-compfile.t b/t/022-compfile.t new file mode 100644 index 0000000..5257702 --- /dev/null +++ b/t/022-compfile.t @@ -0,0 +1,49 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; + +## compress sample2 from the bzip2 1.0.2 distribution +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample2.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 022-tmp) ); + +my $in; +open( $in, $INFILE ); + +my $d = bzopen( "$PREFIX-sample.bz2", "w" ); +$d->bzsetparams( -blockSize100k => 2 ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out = $d->bzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $Compress::Bzip2::bzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( $counter, "$counter blocks were read, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($in); + +ok ( compare_binary_files( "$PREFIX-sample.bz2", $MODELFILE ), 'no differences with reference' ); + +#system( "bzip2 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); + +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); diff --git a/t/023-compfile.t b/t/023-compfile.t new file mode 100644 index 0000000..414c4a5 --- /dev/null +++ b/t/023-compfile.t @@ -0,0 +1,49 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; + +## compress sample3 from the bzip2 1.0.2 distribution +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample3.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 023-tmp) ); + +my $in; +open( $in, $INFILE ); + +my $d = bzopen( "$PREFIX-sample.bz2", "w" ); +$d->bzsetparams( -blockSize100k => 3 ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out = $d->bzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $Compress::Bzip2::bzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( $counter, "$counter blocks were read, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($in); + +ok ( compare_binary_files( "$PREFIX-sample.bz2", $MODELFILE ), 'no differences with reference' ); + +#system( "bzip2 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); + +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); diff --git a/t/024-compfile.t b/t/024-compfile.t new file mode 100644 index 0000000..0e489d7 --- /dev/null +++ b/t/024-compfile.t @@ -0,0 +1,77 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; + +## test the rollover past blockSize100k +## - make up a large file by essentially repeating one of the other sample files + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample2.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 024-tmp) ); + +my $in; +open( $in, $INFILE ); + +my $d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0, -blockSize100k => 2 ); +$d->bzopen( "$PREFIX-sample.bz2", "w" ); + +ok( $d, "open was successful" ); + +my $filecount = 0; +my $counter = 0; +my $bytes = 0; +while ( $bytes < 1_000_000 ) { + while ( my $ln = sysread( $in, $buf, 512 ) ) { + my $out = $d->bzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $Compress::Bzip2::bzerrno\n"; + last; + } + print STDERR "read $ln bytes, bzwrite $out bytes\n" if $debugf; + $bytes += $ln; + $counter++; + } + + $filecount++; + sysseek($in,0,0); +} +ok( $counter, "$counter blocks were read, $bytes bytes, in $filecount loops" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($in); + +my $out; +open( $in, $INFILE ) or die; +#open( $out, "> $PREFIX-reference.bz2" ) or die; + +print STDERR "Running $BZIP -2 > $PREFIX-reference.bz2\n" if $debugf; + +open( $out, "| $BZIP -2 > $PREFIX-reference.bz2" ) or die; +for (my $i=0; $i<$filecount; $i++) { + while ( my $ln = sysread( $in, $buf, 512 ) ) { + syswrite($out, $buf, $ln); + } + sysseek($in,0,0); +} +close($in); +close($out); + +#system( 'od -x < t/024-tmp-sample.bz2 > t/024-tmp-sample-bz2.odx' ); +#system( 'diff t/024-tmp-sample-bz2.odx t/024-tmp-reference-bz2.odx > t/024-tmp-diff.txt' ); + +#ok( ! -s 't/024-tmp-diff.txt', "no differences with bzip2" ); + +ok ( compare_binary_files( "$PREFIX-sample.bz2", "$PREFIX-reference.bz2" ), 'no differences with reference' ); + +#system( "bzip2 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); + +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); diff --git a/t/025-compfile.t b/t/025-compfile.t new file mode 100644 index 0000000..710584d --- /dev/null +++ b/t/025-compfile.t @@ -0,0 +1,71 @@ +# -*- mode: perl -*- + +use Test::More tests => 12; +#use Test::More qw(no_plan); + +## using the simple text file from test 020 +## run the test using total_in, total_out and bzflush +## compress a simple text file - the lyrics to end of the world REM +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +our ( $debugf, $BZIP ); + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample0.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 025-tmp) ); + +my $in; +open( $in, "< $INFILE" ); + +my $d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0, -blockSize100k => 1 ); +$d->bzopen( "$PREFIX-sample.bz2", "w" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out = $d->bzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $Compress::Bzip2::bzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( $counter, "$counter data was written, $bytes bytes" ); + +my $res = $d->bzflush; +ok( !$res, "flush test of write file, res=$res" ); + +my $total_in = $d->total_in; +my $total_out_b4 = $d->total_out; + +$res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +my $total_out = $d->total_out; + +close($in); + +ok( $total_in, "something was read in, $total_in bytes" ); +ok( $total_out_b4, "flush wrote out something, $total_out_b4 bytes" ); +ok( $total_out, "something was written out, $total_out bytes" ); + +ok( $total_out_b4 < $total_out, "file trailer written by bzclose" ); + +ok( $total_in == -s $INFILE, "total_in should be ".(-s $INFILE).", is $total_in" ); +ok( $total_out == -s "$PREFIX-sample.bz2", "total_out should be ".(-s "$PREFIX-sample.bz2").", is $total_out" ); + +#system( "$BZIP -1 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); + +system( "$BZIP -1 < $INFILE > $PREFIX-reference.bz2" ); +ok ( compare_binary_files( "$PREFIX-sample.bz2", "$PREFIX-reference.bz2" ), 'no differences with reference' ); diff --git a/t/026-compfile.t b/t/026-compfile.t new file mode 100644 index 0000000..09eb182 --- /dev/null +++ b/t/026-compfile.t @@ -0,0 +1,70 @@ +# -*- mode: perl -*- + +use Test::More tests => 12; +#use Test::More qw(no_plan); + +## using the large bzip2 1.0.2 test file from test 022 +## run the test using total_in, total_out and bzflush +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +our ( $debugf, $BZIP ); + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample2.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 026-tmp) ); + +my $in; +open( $in, "< $INFILE" ); + +my $d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0, -blockSize100k => 1 ); +$d->bzopen( "$PREFIX-sample.bz2", "w" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out = $d->bzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $Compress::Bzip2::bzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( $counter, "$counter data was written, $bytes bytes" ); + +my $res = $d->bzflush; +ok( !$res, "flush test of write file, res=$res" ); + +my $total_in = $d->total_in; +my $total_out_b4 = $d->total_out; + +$res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +my $total_out = $d->total_out; + +close($in); + +ok( $total_in, "something was read in, $total_in bytes" ); +ok( $total_out_b4, "flush wrote out something, $total_out_b4 bytes" ); +ok( $total_out, "something was written out, $total_out bytes" ); + +ok( $total_out_b4 < $total_out, "file trailer written by bzclose" ); + +ok( $total_in == -s $INFILE, "total_in should be ".(-s $INFILE).", is $total_in" ); +ok( $total_out == -s "$PREFIX-sample.bz2", "total_out should be ".(-s "$PREFIX-sample.bz2").", is $total_out" ); + +#system( "bzip2 -1 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); + +system( "$BZIP -1 < $INFILE > $PREFIX-reference.bz2" ); +ok ( compare_binary_files( "$PREFIX-sample.bz2", "$PREFIX-reference.bz2" ), 'no differences with reference' ); diff --git a/t/030-uncompfile.t b/t/030-uncompfile.t new file mode 100644 index 0000000..a7539f0 --- /dev/null +++ b/t/030-uncompfile.t @@ -0,0 +1,52 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## uncompress a compressed simple text file - the lyrics to end of the world REM +## compare against bunzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample0.bz2) ); +( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 030-tmp) ); + +my $out; +open( $out, "> $PREFIX-sample.txt" ); + +my $d = Compress::Bzip2->new( -verbosity => 0 ); +$d->bzopen( $INFILE, "r" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; + +my $read; +while ( $read = $d->bzread( $buf, 512 ) ) { + if ( $read < 0 ) { + print STDERR "error: $bytes $Compress::Bzip2::bzerrno\n"; + last; + } + + $bytes += syswrite( $out, $buf, $read ); + $counter++; +} + +ok( $counter, "$counter data was written, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($out); + +#system( "bunzip2 < $INFILE > $PREFIX-reference.txt" ); +#system( "diff $PREFIX-sample.txt $PREFIX-reference.txt > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bunzip2" ); + +ok ( compare_binary_files( "$PREFIX-sample.txt", $MODELFILE ), 'no differences with decompressing $INFILE' ); diff --git a/t/031-uncompfile.t b/t/031-uncompfile.t new file mode 100644 index 0000000..47361b3 --- /dev/null +++ b/t/031-uncompfile.t @@ -0,0 +1,54 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## uncompress sample1 from the bzip2 1.0.2 distribution +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +our ( $debugf, $BZIP ); + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample1.bz2) ); +( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 031-tmp) ); + +my $out; +open( $out, "> $PREFIX-sample.txt" ); + +my $d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0 ); +$d->bzopen( $INFILE, "r" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; + +my $read; +while ( $read = $d->bzread( $buf, 512 ) ) { + if ( $read < 0 ) { + print STDERR "error: $bytes $Compress::Bzip2::bzerrno\n"; + last; + } + + $bytes += syswrite( $out, $buf, $read ); + $counter++; +} + +ok( $counter, "$counter data was written, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($out); + +#system( "bunzip2 < $INFILE > $PREFIX-reference.txt" ); +#system( "diff $PREFIX-sample.txt $PREFIX-reference.txt > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bunzip2" ); + +ok ( compare_binary_files( "$PREFIX-sample.txt", $MODELFILE ), 'no differences with decompressing $INFILE' ); diff --git a/t/032-uncompfile.t b/t/032-uncompfile.t new file mode 100644 index 0000000..01937cd --- /dev/null +++ b/t/032-uncompfile.t @@ -0,0 +1,57 @@ +# -*- mode: perl -*- + +use Test::More tests => 7; +#use Test::More qw(no_plan); + +## uncompress sample2 compressed file from the bzip2 1.0.2 distribution +## compare against bunzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample2.bz2) ); +( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 032-tmp) ); + + +my $out; +open( $out, "> $PREFIX-sample.txt" ); + +my $d = Compress::Bzip2->new( -verbosity => 0 ); +$d->bzopen( $INFILE, "r" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; + +ok( !$d->bzeof, "not at EOF" ); + +my $read; +while ( $read = $d->bzread( $buf, 512 ) ) { + if ( $read < 0 ) { + print STDERR "error: $bytes $Compress::Bzip2::bzerrno\n"; + last; + } + + $bytes += syswrite( $out, $buf, $read ); + $counter++; +} + +ok( $d->bzeof, "at EOF" ); + +ok( $counter, "$counter data was written, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($out); + +#system( "bunzip2 < $INFILE > $PREFIX-reference.txt" ); +#system( "diff $PREFIX-sample.txt $PREFIX-reference.txt > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bunzip2" ); + +ok ( compare_binary_files( "$PREFIX-sample.txt", $MODELFILE ), 'no differences with decompressing $INFILE' ); diff --git a/t/033-uncompfile.t b/t/033-uncompfile.t new file mode 100644 index 0000000..4c3a520 --- /dev/null +++ b/t/033-uncompfile.t @@ -0,0 +1,53 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## uncompress sample3 compressed file from the bzip2 1.0.2 distribution +## compare against bunzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample3.bz2) ); +( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 033-tmp) ); + + +my $out; +open( $out, "> $PREFIX-sample.txt" ); + +my $d = Compress::Bzip2->new( -verbosity => 0 ); +$d->bzopen( $INFILE, "r" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; + +my $read; +while ( $read = $d->bzread( $buf, 512 ) ) { + if ( $read < 0 ) { + print STDERR "error: $bytes $Compress::Bzip2::bzerrno\n"; + last; + } + + $bytes += syswrite( $out, $buf, $read ); + $counter++; +} + +ok( $counter, "$counter data was written, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($out); + +#system( "bunzip2 < $INFILE > $PREFIX-reference.txt" ); +#system( "diff $PREFIX-sample.txt $PREFIX-reference.txt > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bunzip2" ); + +ok ( compare_binary_files( "$PREFIX-sample.txt", $MODELFILE ), "no differences with decompressing $INFILE" ); diff --git a/t/034-uncompfile.t b/t/034-uncompfile.t new file mode 100644 index 0000000..d2d8d0e --- /dev/null +++ b/t/034-uncompfile.t @@ -0,0 +1,76 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## uncompress sample3 compressed file from the bzip2 1.0.2 distribution +## compare against bunzip2 command with od -x and diff + +## test uncompressing a large file +## - make up a large file by essentially repeating one of the other sample files + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample1.ref) ); +#( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 034-tmp) ); + +my $in; +my $out; +my $dup; +open( $in, $INFILE ) or die; + +print STDERR "Running $BZIP -1 > $PREFIX-reference.bz2\n" if $debugf; + +my $filecount = 11; + +open( $out, "| $BZIP -1 > $PREFIX-reference.bz2" ) or die; +open( $dup, "> $PREFIX-reference.txt" ) or die; + +my $MODELFILE = "$PREFIX-reference.txt"; + +for (my $i=0; $i<$filecount; $i++) { + while ( my $ln = sysread( $in, $buf, 512 ) ) { + syswrite($out, $buf, $ln); + syswrite($dup, $buf, $ln); + } + sysseek($in,0,0); +} +close($in); +close($out); +close($dup); + +undef $out; +open( $out, "> $PREFIX-sample.txt" ); + +my $d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0 ); +$d->bzopen( "$PREFIX-reference.bz2", "r" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; +my $written = 0; +while ( my $read = $d->bzread( $buf, 512 ) ) { + if ( $read < 0 ) { + print STDERR "error: $bytes $Compress::Bzip2::bzerrno\n"; + last; + } + + $written = syswrite( $out, $buf, $read ); + + $bytes += $read; + $counter++; +} +ok( $counter, "$counter blocks were read, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($out); + +ok ( compare_binary_files( "$PREFIX-sample.txt", $MODELFILE ), "no differences with decompressing $INFILE times 11" ); diff --git a/t/035-uncompreadline.t b/t/035-uncompreadline.t new file mode 100644 index 0000000..69d1161 --- /dev/null +++ b/t/035-uncompreadline.t @@ -0,0 +1,89 @@ +# -*- mode: perl -*- + +#use Test::More tests => 5; +use Test::More qw(no_plan); + +## uncompress a compressed simple text file - the lyrics to end of the world REM +## compare against bunzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample0.bz2) ); +( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 035-tmp) ); + +my $out; +open( $out, "> $PREFIX-sample.txt" ); + +my $d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0 ); +$d->bzopen( $INFILE, "r" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; + +my $buf; +my $read; +while ( $read = $d->bzread( $buf, 512 ) ) { + if ( $read < 0 ) { + print STDERR "error: $bytes $Compress::Bzip2::bzerrno\n"; + last; + } + + $bytes += syswrite( $out, $buf, $read ); + $counter++; +} + +ok( $counter, "$counter blocks were written, $bytes bytes" ); + +my $res = $d->bzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($out); + +ok ( compare_binary_files( "$PREFIX-sample.txt", $MODELFILE ), 'no differences with decompressing $INFILE' ); + +## test readline + +my ($rlin, %reflines, %lines); +open( $rlin, "< $MODELFILE" ) or die; +while (<$rlin>) { + chomp; + $reflines{count}++; + $reflines{maxlen} = length($_) if !defined($reflines{maxlen}) || length($_) > $reflines{maxlen}; + $reflines{minlen} = length($_) if !defined($reflines{minlen}) || length($_) < $reflines{minlen}; + $reflines{first} = $_ if $reflines{count}==1; + $reflines{last} = $_; +} +close($rlin); + +$d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0 ); +$d->bzopen( $INFILE, "r" ); + +my $ln; +ok( $d, "readline open was successful" ); +while ( $ln = $d->bzreadline( $buf, 512 ) ) { + if ( $ln < 0 ) { + print STDERR "error: $Compress::Bzip2::bzerrno\n"; + last; + } + + chomp $buf; + + $lines{count}++; + $lines{maxlen} = length($buf) if !defined($lines{maxlen}) || length($buf) > $lines{maxlen}; + $lines{minlen} = length($buf) if !defined($lines{minlen}) || length($buf) < $lines{minlen}; + $lines{first} = $buf if $lines{count}==1; + $lines{last} = $buf; +} + +ok( $reflines{count} == $lines{count}, "readline linecount is $lines{count}, should be $reflines{count}" ); +ok( $reflines{maxlen} == $lines{maxlen}, "readline maximum line length is $lines{maxlen}, should be $reflines{maxlen}" ); +ok( $reflines{minlen} == $lines{minlen}, "readline minimum line length is $lines{minlen}, should be $reflines{minlen}" ); +ok( $reflines{first} eq $lines{first}, "readline first line is '$lines{first}', should be '$reflines{first}'" ); +ok( $reflines{last} eq $lines{last}, "readline last line is '$lines{last}', should be '$reflines{last}'" ); diff --git a/t/040-memory.t b/t/040-memory.t new file mode 100644 index 0000000..a89fc87 --- /dev/null +++ b/t/040-memory.t @@ -0,0 +1,59 @@ +# -*- mode: perl -*- + +use Test::More tests => 15; + +BEGIN { + use_ok('Compress::Bzip2', qw(:utilities :bzip1)); +}; + + +my $string = q/ +Twas brillig and the slithy toves +did gire and gimble in the wabe +All mimsey were the borogroves +and the Momewrathes outgrabe + /; + +my $compress = memBzip( $string ); +my $uncompress = memBunzip( $compress ); + +ok( substr($compress,5,16) =~ /^BZh/, "compressed starts with bzip magic header" ); + +ok( $compress ne $string, "string was not inouted" ); +ok( length($compress)-10 < length($string), "string compression - ".length($compress).' vs '.length($string) ); +ok( $uncompress eq $string, "uncompressed is same as the original" ); + +my $string10 = $string x 10; +my $compress10 = memBzip( $string10 ); +my $uncompress10 = memBunzip( $compress10 ); + +ok( $compress10 ne $string10, "x10 string was not inouted" ); +ok( length($compress10) < length($string10), "x10 string compression - ".length($compress10).' vs '.length($string10) ); +ok( $uncompress10 eq $string10, "x10 uncompressed is same as the original" ); + +$compress = compress( $string ); +$uncompress = decompress( $compress ); + +ok( $compress ne $string, "bzip1 string was not inouted" ); +ok( length($compress)-10 < length($string), "bzip1 string compression - ".length($compress).' vs '.length($string) ); +ok( $uncompress eq $string, "bzip1 decompress is same as the original" ); + +do 't/lib.pl'; + +# allow plain BZh files with memBunzip also +my $INFILE = catfile( qw(bzlib-src sample0.bz2) ); +local $/ = undef; +open( IN, "< $INFILE" ) or die "$INFILE: $!"; +binmode IN; +my $sample0 = <IN>; +close( IN ); + +$uncompress = memBunzip( $sample0 ); +ok( $uncompress, "sample0 uncompressed w/o header" ); +like( $uncompress, qr/^That\'s great, it starts with an earthquake/ ); + +my $header = pack("C", 0xf0); +$header .= pack "N", $uncompress ? length($uncompress) : 2027; +$uncompress = memBunzip( $header . $sample0 ); +ok( $uncompress, "sample0 uncompressed w/ header" ); +like( $uncompress, qr/^That\'s great, it starts with an earthquake/ ); diff --git a/t/041-double-free.t b/t/041-double-free.t new file mode 100644 index 0000000..44b4c39 --- /dev/null +++ b/t/041-double-free.t @@ -0,0 +1,17 @@ +# -*- mode: perl -*- + +use Test::More tests => 1; + +use strict; +BEGIN { + use_ok('Compress::Bzip2'); +}; + +do 't/lib.pl'; + +# [cpan #28366] double free or corruption on 32 bit, threaded +my $INFILE = catfile( qw(bzlib-src sample1.ref) ); +my $bz = bzopen($INFILE, 'rb') ; +print "a"; +while ($bz->bzreadline($_) > 0) {} +$bz->bzclose (); diff --git a/t/041-error.t b/t/041-error.t new file mode 100644 index 0000000..02886d8 --- /dev/null +++ b/t/041-error.t @@ -0,0 +1,72 @@ +# -*- mode: perl -*- + +use Test::More tests => 9; +use Fcntl; + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +our ( $debugf, $BZIP ); + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample0.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 041-tmp) ); + +## verbosity 0-4, small 0,1, blockSize100k 1-9, workFactor 0-250, readUncompressed 0,1 +my ( $d, $err, $in, $out, $buf, $res ); + +$d = Compress::Bzip2->new( -workFactor => 1000 ); +ok( $d, "object created in spite of error" ); +$err = $d->bzerror; +ok( $err, "error is set '$err' vs '$bzerrno'" ); + +$d->bzclearerr; + +$err = $d->bzerror; +ok( !$err, "after bzclearerr, error is not set '$err' vs '$bzerrno'" ); +my $TMPFILE = "$PREFIX-protected.bz2"; +unlink( $TMPFILE ) if -f $TMPFILE; + +SKIP: { + skip "bzclose handle not with 5.6", 2 if $] < 5.008; + sysopen( $out, $TMPFILE, O_WRONLY|O_CREAT ) or die "failed $TMPFILE $!"; + ok( $d->bzopen( $out, "w" ), "bzopen with file handle instead of file" ); + + open( $in, "<", $INFILE ); + while ( my $ln = sysread( $in, $buf, 512 ) ) { + $res = $d->bzwrite( $buf, $ln ); + if ( $res < 0 ) { + print STDERR "error: $res $bzerrno\n"; + last; + } + } + + $res = $d->bzclose; + ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); +} + +close($in); + +if ($] < 5.008) { + require File::Copy; + File::Copy::syscopy($MODELFILE, $TMPFILE); + chmod( 0644, $TMPFILE ) or die; +} +ok ( compare_binary_files( $MODELFILE, $TMPFILE ), "no differences with $MODELFILE reference" ); + +chmod( 0000, $TMPFILE ) or die; + +SKIP: { + skip "0000 but writable ($>/$^O)", 2 if -w $TMPFILE; + + $d = Compress::Bzip2->new( -verbosity => $debugf ? 4 : 0, -blockSize100k => 1 ); + $res = $d->bzopen( $TMPFILE, "w" ); + + ok( !$res, "open failed" ); + + $res = $d->bzerror; + ok( $res, "error set, is '$res' '$bzerrno'" ); +} diff --git a/t/050-deflate.t b/t/050-deflate.t new file mode 100644 index 0000000..46c1696 --- /dev/null +++ b/t/050-deflate.t @@ -0,0 +1,68 @@ +# -*- mode: perl -*- + +use Test::More tests => 4; +#use Test::More qw(no_plan); + +## compress sample2 from the bzip2 1.0.2 distribution +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +our ( $debugf, $BZIP ); + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample2.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 050-tmp) ); + +my ( $in, $out, $d, $outbuf, $counter, $bytes, $bytesout ); + +open( $in, "< $INFILE" ) or die "$INFILE: $!"; +open( $out, "> $PREFIX-out.bz2" ) or die "$PREFIX-out.bz2: $!"; + +## verbosity 0-4, small 0,1, blockSize100k 1-9, workFactor 0-250, readUncompressed 0,1 +$d = bzdeflateInit( -verbosity => $debugf ? 4 : 0, -blockSize100k => 2 ); + +ok( $d, "bzdeflateInit was successful" ); + +$counter = 0; +$bytes = 0; +$bytesout = 0; +while ( my $ln = sysread( $in, $buf, 512 ) ) { + $outbuf = $d->bzdeflate( $buf ); + if ( !defined($outbuf) ) { + print STDERR "error: $outbuf $bzerrno\n"; + last; + } + + if ( $outbuf ne '' ) { + syswrite( $out, $outbuf ); + $bytesout += length($outbuf); + } + + $bytes += $ln; + $counter++; +} + +$outbuf = $d->bzclose; +if ( defined($outbuf) && $outbuf ne '' ) { + syswrite( $out, $outbuf ); + $bytesout += length($outbuf); + + $counter++; +} + +ok( $bytes && $bytesout, "$counter blocks read, $bytes bytes in, $bytesout bytes out" ); + +close($in); +close($out); + +#system( "$BZIP -1 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-out.bz2 > $PREFIX-out-bz2.odx" ); +#system( "diff $PREFIX-out-bz2.odx $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); + +ok ( compare_binary_files( "$PREFIX-out.bz2", $MODELFILE ), "no differences with stream compressing $INFILE" ); diff --git a/t/051-deflate-flush.t b/t/051-deflate-flush.t new file mode 100644 index 0000000..36d40cf --- /dev/null +++ b/t/051-deflate-flush.t @@ -0,0 +1,78 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## test bzflush +## stream compress sample2 from the bzip2 1.0.2 distribution +## verify bunzip2 can reconstruct the output + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +our ( $debugf, $BZIP ); + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample2.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 051-tmp) ); + +my ( $in, $out, $d, $outbuf, $counter, $bytes, $bytesout, $flushcount, $bytesflushedmark ); + +open( $in, "< $INFILE" ) or die "$INFILE: $!"; +open( $out, "> $PREFIX-out.bz2" ) or die "$PREFIX-out.bz2: $!"; + +## verbosity 0-4, small 0,1, blockSize100k 1-9, workFactor 0-250, readUncompressed 0,1 +$d = bzdeflateInit( -verbosity => $debugf ? 4 : 0 ); + +ok( $d, "bzdeflateInit was successful" ); + +$counter = 0; +$bytes = 0; +$bytesout = 0; +$bytesflushedmark = 0; +$flushcount = 0; +while ( my $ln = sysread( $in, $buf, 512 ) ) { + $outbuf = $d->bzdeflate( $buf ); + if ( !defined($outbuf) ) { + print STDERR "error: $outbuf $bzerrno\n"; + last; + } + + if ( $bytes - $bytesflushedmark > 50_000 && $outbuf eq '' ) { + $outbuf = $d->bzflush; + $flushcount++ if $outbuf; + $bytesflushedmark = $bytes; + } + + if ( $outbuf ne '' ) { + syswrite( $out, $outbuf ); + $bytesout += length($outbuf); + } + + $bytes += $ln; + $counter++; +} + +$outbuf = $d->bzclose; +if ( defined($outbuf) && $outbuf ne '' ) { + syswrite( $out, $outbuf ); + $bytesout += length($outbuf); + + $counter++; +} + +ok( $bytes && $bytesout, "$counter blocks read, $bytes bytes in, $bytesout bytes out" ); +ok( $flushcount, "successful flushes at 50,000 - $flushcount" ); + +close($in); +close($out); + +system( "$BZIP -d < $PREFIX-out.bz2 > $PREFIX-reference-out-bunzip2.txt" ); +#system( "od -x < $INFILE > $PREFIX-infile.odx" ); +#system( "diff $PREFIX-infile.odx $PREFIX-reference-out-bunzip.odx > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); + +ok ( compare_binary_files( $INFILE, "$PREFIX-reference-out-bunzip2.txt" ), "no differences with 50k stream compressing $INFILE" ); diff --git a/t/060-inflate.t b/t/060-inflate.t new file mode 100644 index 0000000..5f038b2 --- /dev/null +++ b/t/060-inflate.t @@ -0,0 +1,71 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; + +## stream uncompress sample2 from the bzip2 1.0.2 distribution +## compare against bunzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2'); +}; + +our ( $debugf, $BZIP ); + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample2.bz2) ); +( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 060-tmp) ); + +my ( $in, $out, $d, $outbuf, $status, $counter, $bytes, $bytesout ); +$debugf = $ENV{DEBUG} ? 1 : 0; +open( $in, "< $INFILE" ) or die "$INFILE: $!"; +open( $out, "> $PREFIX-out.txt" ) or die "$PREFIX-out.txt: $!"; + +## verbosity 0-4, small 0,1, blockSize100k 1-9, workFactor 0-250, readUncompressed 0,1 +( $d, $status ) = bzinflateInit( -verbosity => $debugf ? 4 : 0 ); + +ok( $d && $status == BZ_OK, "bzinflateInit was successful" ); + +$counter = 0; +$bytes = 0; +$bytesout = 0; +while ( my $ln = sysread( $in, $buf, 512 ) ) { + # test buf as scalar or scalarref + ( $outbuf, $status ) = $d->bzinflate( $counter % 2 ? \$buf : $buf ); + # the 2nd attempt to read from the 512 buf in bzfile_streambuf_read will cause a BZ_IO_ERROR + if ( $status != BZ_IO_ERROR && $status != BZ_STREAM_END && $status != BZ_OK || !defined($outbuf) ) { + diag "error: $outbuf $bzerrno\n"; + last; + } + + if ( $outbuf ne '' ) { + syswrite( $out, $outbuf ); + $bytesout += length($outbuf); + } + + $bytes += $ln; + $counter++; +} + +( $outbuf, $status ) = $d->bzclose; +ok( $status == BZ_OK, "bzclose was successful" ); + +if ( defined($outbuf) && $outbuf ne '' ) { + syswrite( $out, $outbuf ); + $bytesout += length($outbuf); + + $counter++; +} + +ok( $bytes && $bytesout, "$counter blocks read, $bytes bytes in, $bytesout bytes out" ); + +close($in); +close($out); + +#system( "$BZIP -d < $INFILE | od -x > $PREFIX-reference-txt.odx" ); +#system( "od -x < $PREFIX-out.txt > $PREFIX-out-txt.odx" ); +#system( "diff $PREFIX-out-txt.odx $PREFIX-reference-txt.odx > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bunzip2" ); + +ok ( compare_binary_files( "$PREFIX-out.txt", $MODELFILE ), 'no differences with decompressing $INFILE' ); diff --git a/t/070-gzcomp.t b/t/070-gzcomp.t new file mode 100644 index 0000000..771986c --- /dev/null +++ b/t/070-gzcomp.t @@ -0,0 +1,49 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## test the Compress::Zlib compatibility +## compress a simple text file - the lyrics to end of the world REM +## compare against bzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2', qw(:gzip)); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample0.ref) ); +( my $MODELFILE = $INFILE ) =~ s/\.ref$/.bz2/; +my $PREFIX = catfile( qw(t 020-tmp) ); + +my $in; +open( $in, $INFILE ); + +my $d = gzopen( "$PREFIX-sample.bz2", "w" ); + +ok( $d, "gzopen was successful" ); + +my $counter = 0; +my $bytes = 0; +while ( my $ln = read( $in, $buf, 512 ) ) { + my $out = $d->gzwrite( $buf, $ln ); + if ( $out < 0 ) { + print STDERR "error: $out $gzerrno\n"; + last; + } + $bytes += $ln; + $counter++; +} +ok( $counter, "$counter blocks were read, $bytes bytes" ); + +my $res = $d->gzclose; +ok( !$res, "file was closed $res $gzerrno" ); + +close($in); + +ok ( compare_binary_files( "$PREFIX-sample.bz2", $MODELFILE ), 'no differences with reference' ); +#system( "bzip2 < $INFILE | od -x > $PREFIX-reference-bz2.odx" ); +#system( "od -x < $PREFIX-sample.bz2 | diff - $PREFIX-reference-bz2.odx > $PREFIX-diff.txt" ); + +#ok( ! -s "$PREFIX-diff.txt", "no differences with bzip2" ); diff --git a/t/071-gzuncomp.t b/t/071-gzuncomp.t new file mode 100644 index 0000000..5735360 --- /dev/null +++ b/t/071-gzuncomp.t @@ -0,0 +1,52 @@ +# -*- mode: perl -*- + +use Test::More tests => 5; +#use Test::More qw(no_plan); + +## uncompress a compressed simple text file - the lyrics to end of the world REM +## compare against bunzip2 command with od -x and diff + +BEGIN { + use_ok('Compress::Bzip2', qw(:gzip)); +}; + +do 't/lib.pl'; + +my $INFILE = catfile( qw(bzlib-src sample0.bz2) ); +( my $MODELFILE = $INFILE ) =~ s/\.bz2$/.ref/; +my $PREFIX = catfile( qw(t 030-tmp) ); + +my $out; +open( $out, "> $PREFIX-sample.txt" ); + +my $d = Compress::Bzip2->new( -verbosity => 0 ); +$d->bzopen( $INFILE, "r" ); + +ok( $d, "open was successful" ); + +my $counter = 0; +my $bytes = 0; + +my $read; +while ( $read = $d->gzread( $buf, 512 ) ) { + if ( $read < 0 ) { + print STDERR "error: $bytes $gzerrno\n"; + last; + } + + $bytes += syswrite( $out, $buf, $read ); + $counter++; +} + +ok( $counter, "$counter data was written, $bytes bytes" ); + +my $res = $d->gzclose; +ok( !$res, "file was closed $res $Compress::Bzip2::bzerrno" ); + +close($out); + +#system( "bunzip2 < $INFILE > $PREFIX-reference.txt" ); +#system( "diff $PREFIX-sample.txt $PREFIX-reference.txt > $PREFIX-diff.txt" ); +#ok( ! -s "$PREFIX-diff.txt", "no differences with bunzip2" ); + +ok ( compare_binary_files( "$PREFIX-sample.txt", $MODELFILE ), 'no differences with decompressing $INFILE' ); diff --git a/t/080-version.t b/t/080-version.t new file mode 100644 index 0000000..37840a6 --- /dev/null +++ b/t/080-version.t @@ -0,0 +1,24 @@ +# -*- mode: perl -*- + +## this is the 01version.t test from Compress::Bzip2 1.03 with no changes +## (except for comments) + +# basic test, see if we can load the module and get a version + +use strict; + +use Test::More tests => 3; + +BEGIN { + use_ok('Compress::Bzip2', + qw(compress decompress compress_init decompress_init)); +} + +my $mod_ver = $Compress::Bzip2::VERSION; +ok($mod_ver,"module version $mod_ver"); + +my $lib_ver = Compress::Bzip2::version(); +ok($lib_ver,"library version $lib_ver"); + + +# vi:ts=4:noet diff --git a/t/081-basic.t b/t/081-basic.t new file mode 100644 index 0000000..ddff953 --- /dev/null +++ b/t/081-basic.t @@ -0,0 +1,98 @@ +# -*- mode: perl -*- + +## this is the 02basic.t test from Compress::Bzip2 1.03 with no changes +## (except for comments) + +# simple compression/decompression + +use strict; + +use Test::More tests => 208; +use Compress::Bzip2 qw(compress decompress); + + +# globals + +my $Level = 6; +my ($In,$Out) = (0,0); +my @AlNum = ('A'..'Z','a'..'z','0'..'9',' '); + + +# subs + +sub try { + my $str = shift; + my $level = shift || $Level; + my $comp = compress($str,$level); + return 0 if not defined $comp; + my $orig = decompress($comp); + return 0 if not defined $orig; + $In += length $orig; + $Out += length $comp; + return $orig eq $str ? 1 : 0; +} + + +# tests + +# some short strings + +my $sum; +ok(try('', 1),'empty string'); +ok((eval { try(undef) }, $@),'undef fails'); +$sum = 0; +$sum += try($_) for @AlNum; +is($sum,scalar(@AlNum),'alphanumerics'); +ok(try('FOO'),'FOO'); +ok(try('bar'),'bar'); +ok(try(' '),'spaces'); + +# references are supposed to work too + +my $str = 'reference test'; +$sum = 0; +for(1..5) { + $str = \$str; + $sum += try($str); +} +is($sum,5,'reference tests'); + +# random strings + +$sum = 0; +for my $random(1..100) { + my $level = $Level; + $level = 1 if 20 == $random; + $level = 9 if 80 == $random; + my $str = ''; + $str .= $AlNum[rand @AlNum] for 1..rand 100; + $sum += try($str, $level); +} +is($sum,100,'100 random strings'); + +# long strings with repetition + +($In,$Out) = (0,0); +for my $random(1..100) { + my $level = $Level; + $level = 1 if 20 == $random; + $level = 9 if 80 == $random; + my $str = ''; + $str .= ($AlNum[rand @AlNum] x rand 1000) for 1..100+rand 900; + ok(try($str, $level),"long string $random"); +} +#diag(sprintf "compression ratio %.2f%%",100*$Out/$In); + +# binary strings + +($In,$Out) = (0,0); +for my $random(1..100) { + my $level = $Level; + $level = 1 if 20 == $random; + $level = 9 if 80 == $random; + my $str = ''; + $str .= chr(rand 256) for 1..1000+rand 9000; + ok(try($str, $level),"binary string $random"); +} +#diag(sprintf "compression ratio %.2f%%",100*$Out/$In); + diff --git a/t/082-stream.t b/t/082-stream.t new file mode 100644 index 0000000..28581f2 --- /dev/null +++ b/t/082-stream.t @@ -0,0 +1,137 @@ +# -*- mode: perl -*- + +## +## this is the 03stream.t test from Compress::Bzip2 1.03 with no changes +## (except for comments) +## +## - seems to run a lot slower than the 1.03 test, probably because the add +## routine is now perl, not perlxs. +## + +# streaming test + +use strict; +use Test::More tests => 208; + +use Compress::Bzip2 qw(compress_init decompress_init decompress); + +# globals + +my $Level = 1; +my @AlNum = ('A'..'Z','a'..'z','0'..'9',' '); +my ($In,$Out) = (0,0); +my $Prefix = 0; + + +# subs + +sub try { + my ($str,$chunk,$inc) = @_; + $chunk ||= 100; + $inc ||= 0; + + my $stream = compress_init(); + + # piece it into chunks and feed it to the monster + my ($size,$pos,$out,$done,$status,$orig) = ($chunk,0,''); + for(;;) { + if($pos > length $str) { + $status = $stream->finish(); + $done = 1; + } else { + my $piece = substr $str,$pos,$size; + $pos += $size; + $size += $inc; + $status = $stream->add($piece); + } + return 0 if not defined $status; + $out .= $status; + last if $done; + } + + # see if we can get it back + if($Prefix) { + $orig = decompress($stream->prefix().$out); + } else { + $stream = decompress_init(); + + ($size,$pos,$orig,$done) = ($chunk,0,''); + for(;;) { + if($pos > length $out) { + $status = $stream->finish(); + $done = 1; + } else { + my $piece = substr $out,$pos,$size; + $pos += $size; + $size += $inc; + $status = $stream->add($piece); + } + return 0 if not defined $status; + $orig .= $status; + last if $done; + } + } + + $In += length $orig; + $Out += length $out; + return $orig eq $str ? 1 : 0; +} + + +# tests + +# some short strings + +my $sum; +ok(try(''),'empty string'); +pass('undef'); +$sum = 0; +$sum += try($_) for @AlNum; +is($sum,scalar(@AlNum),'alphanumerics'); +ok(try('FOO'),'FOO'); +ok(try('bar'),'bar'); +ok(try(' '),'spaces'); + +# references are supposed to work too + +my $str = 'reference test'; +$sum = 0; +for(1..5) { + $str = \$str; + $sum += try($str); +} +is($sum,5,'reference tests'); + +# random strings + +$sum = 0; +for my $random(1..100) { + $Level = 9 if 80 == $random; + my $str = ''; + $str .= $AlNum[rand @AlNum] for 1..rand 100; + $sum += try($str); +} +is($sum,100,'100 random strings'); + +# long strings with repetition + +($In,$Out) = (0,0); +for my $random(1..100) { + $Level = 1 if 20 == $random; + my $str = ''; + $str .= ($AlNum[rand @AlNum] x rand 1000) for 1..100+rand 900; + ok(try($str),"long string $random"); +} +#diag(sprintf "compression ratio %.2f%%",100*$Out/$In); + +# binary strings + +($In,$Out) = (0,0); +for my $random(1..100) { + $Level = 9 if 80 == $random; + my $str = ''; + $str .= chr(rand 256) for 1..1000+rand 9000; + ok(try($str),"binary string $random"); +} +#diag(sprintf "compression ratio %.2f%%",100*$Out/$In); + diff --git a/t/090-memory-usage.pl b/t/090-memory-usage.pl new file mode 100644 index 0000000..f3eaef8 --- /dev/null +++ b/t/090-memory-usage.pl @@ -0,0 +1,59 @@ +#! perl +use strict; +use 5.006002; +use Compress::Bzip2; +use File::Spec; +use Memory::Usage; +# Note that Test::LeakTrace does not find the XS leaks here. Always use Memory::Usage also. + +use constant MAX => 5; # MAX times all files in t => 125MB with 75, 15MB with 5, 43MB with 25 +use constant MAX_5 => MAX >= 5 ? MAX / 5 : 1; +my $bigfile = File::Spec->catfile('t', '090-tmp-bigfile.bz2'); + +sub t_compress { + warn "Creating $bigfile ...\n"; + my $buf; + my $mu = Memory::Usage->new(); + $mu->record("Before bzopen"); + my $bz = Compress::Bzip2::bzopen($bigfile, "w"); + for (0 .. MAX) { + $mu->record("Before bzwrite: $_") unless $_ % MAX_5; + for my $infile (glob "t/*") { + next if $infile eq $bigfile; + open(my $fh, "<", $infile); + while ( read( $fh, $buf, 65335 ) ) { + $bz->bzwrite( $buf ); + } + close $fh; + } + $mu->record("After bzwrite: $_") unless $_ % MAX_5; + } + $bz->bzclose; + $mu->record("After bzclose"); + $mu->dump(); + system("bunzip2", "-tv", $bigfile); + warn(" size: ", -s $bigfile, "\n"); +} + +sub t_uncompress { + warn "Uncompressing $bigfile 5x ...\n"; + my $buf; + my $mu = Memory::Usage->new(); + for ( 1 .. 5 ) { + my $bz = Compress::Bzip2::bzinflateInit( -verbosity => 0 ); + $mu->record("Before bunzip: $_"); + open( my $fh, '<', $bigfile ); + while ( read( $fh, $buf, 65335 ) ) { + my ( $output, $status ) = $bz->bzinflate( $buf ); + } + close($fh); + $bz->bzclose(); + $mu->record("After bunzip: $_"); + } + $mu->dump(); +} + +t_compress() unless -f $bigfile; +t_uncompress(); + +unlink $bigfile; diff --git a/t/900_kwalitee.t b/t/900_kwalitee.t new file mode 100644 index 0000000..40b1696 --- /dev/null +++ b/t/900_kwalitee.t @@ -0,0 +1,21 @@ +# -*- perl -*- +use strict; +use warnings; + +use Test::More; +use Config; + +plan skip_all => 'This test is only run for the module author' + unless -d '.git' || $ENV{IS_MAINTAINER}; +plan skip_all => 'Test::Kwalitee fails with clang -faddress-sanitizer' + if $Config{ccflags} =~ /-faddress-sanitizer/; + +use File::Copy 'cp'; +cp('MYMETA.yml','META.yml') if -e 'MYMETA.yml' and !-e 'META.yml'; +eval { + require Test::Kwalitee; + Test::Kwalitee->import( + tests => [ qw( -use_strict -has_test_pod -has_test_pod_coverage)]); +}; +plan skip_all => "Test::Kwalitee needed for testing kwalitee" + if $@; diff --git a/t/900_leaktrace.t b/t/900_leaktrace.t new file mode 100644 index 0000000..233d085 --- /dev/null +++ b/t/900_leaktrace.t @@ -0,0 +1,31 @@ +#!perl -w + +use strict; +use constant HAS_LEAKTRACE => eval{ require Test::LeakTrace }; +use Test::More HAS_LEAKTRACE ? (tests => 2) : (skip_all => 'require Test::LeakTrace'); +use Test::LeakTrace; + +use Compress::Bzip2; +my $string = q/ +Twas brillig and the slithy toves +did gire and gimble in the wabe +All mimsey were the borogroves +and the Momewrathes outgrabe + / x 20; + +leaks_cmp_ok{ + my $compress = memBzip( $string ); + my $uncompress = memBunzip( $compress ); +} '<', 1; + +do 't/lib.pl'; + +leaks_cmp_ok{ + my $INFILE = catfile( qw(bzlib-src sample0.bz2) ); + local $/ = undef; + open( IN, "< $INFILE" ) or die "$INFILE: $!"; + binmode IN; + my $sample0 = <IN>; + close( IN ); + my $uncompress = memBunzip( $sample0 ); +} '<', 1; diff --git a/t/900_meta.t b/t/900_meta.t new file mode 100644 index 0000000..953c12c --- /dev/null +++ b/t/900_meta.t @@ -0,0 +1,28 @@ +# -*- perl -*- + +# Test that our META.yml file matches the current specification. + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +my $MODULE = 'Test::CPAN::Meta 0.12'; + +# Don't run tests for installs +use Test::More; +use Config; +plan skip_all => 'This test is only run for the module author' + unless -d '.git' || $ENV{IS_MAINTAINER}; +plan skip_all => 'Test::CPAN::Meta fails with clang -faddress-sanitizer' + if $Config{ccflags} =~ /-faddress-sanitizer/; + +# Load the testing module +eval "use $MODULE;"; +if ( $@ ) { + plan( skip_all => "$MODULE not available for testing" ); + die "Failed to load required release-testing module $MODULE 0.12" + if -d '.git' || $ENV{IS_MAINTAINER}; +} +meta_yaml_ok(); diff --git a/t/900_perl_minimum_version.t b/t/900_perl_minimum_version.t new file mode 100644 index 0000000..8b51c57 --- /dev/null +++ b/t/900_perl_minimum_version.t @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +# Test that our declared minimum Perl version matches our syntax +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +my @MODULES = ( + 'Perl::MinimumVersion 1.20', + 'Test::MinimumVersion 0.008', +); + +# Don't run tests during end-user installs +use Test::More; +unless (-d '.git' || $ENV{IS_MAINTAINER}) { + plan( skip_all => "Author tests not required for installation" ); +} + +# Load the testing modules +foreach my $MODULE ( @MODULES ) { + eval "use $MODULE"; + if ( $@ ) { + plan( skip_all => "$MODULE not available for testing" ); + die "Failed to load required release-testing module $MODULE" + if -d '.git' || $ENV{IS_MAINTAINER}; + } +} + +# Note: constant_hash requires 5.8 but works okay with 5.6.2 here +all_minimum_version_ok("5.008"); + +1; diff --git a/t/900_pod-coverage.t b/t/900_pod-coverage.t new file mode 100644 index 0000000..75e3153 --- /dev/null +++ b/t/900_pod-coverage.t @@ -0,0 +1,16 @@ +# -*- perl -*- +use strict; +use warnings; +use Test::More; + +plan skip_all => 'done_testing requires 5.8.6' if $] <= 5.008005; +plan skip_all => 'This test is only run for the module author' + unless -d '.git' || $ENV{IS_MAINTAINER}; + +eval "use Test::Pod::Coverage 1.04"; +plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" + if $@; + +pod_coverage_ok( "Compress::Bzip2", { trustme => [ qw( constant )]}); + +done_testing(); diff --git a/t/900_pod.t b/t/900_pod.t new file mode 100644 index 0000000..e51c651 --- /dev/null +++ b/t/900_pod.t @@ -0,0 +1,5 @@ +# -*- perl -*-
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+all_pod_files_ok();
diff --git a/t/lib.pl b/t/lib.pl new file mode 100644 index 0000000..4a96ce2 --- /dev/null +++ b/t/lib.pl @@ -0,0 +1,205 @@ +use File::Copy; +use Cwd; +use Config; + +BEGIN { + eval { require File::Spec::Functions ; File::Spec::Functions->import( qw(catfile rel2abs) ) } ; + *catfile = sub { return join( '/', @_ ) } if $@; +} + +require VMS::Filespec if $^O eq 'VMS'; + +sub dump_block { + my %block; + ( $block{1}, $block{2} ) = @_; + + for ( my $i=0; $i<length($block{1}); $i+=16 ) { + my %xbuf; + + foreach my $j ( 1, 2 ) { + for ( my $k=0; $k<16 && $i+$k<length($block{$j}); $k++ ) { + $xbuf{$j} .= ' ' if $xbuf{$j} && $k % 2 == 0; + $xbuf{$j} .= unpack( "H2", substr($block{$j},$i+$k,1) ); + } + + printf STDERR "%03d %s\n", $i, $xbuf{$j}; + } + + print STDERR "\n"; + } +} + +sub compare_binary_files { + my ( $file1, $file2 ) = @_; + my ( %fh, %buf, %ln, %counter ); + + if ( -s $file1 != -s $file2 ) { + print STDERR "files not the same size", " $file1 is ".(-s $file1), ", $file2 is ".(-s $file2),"\n"; + return 0; + } + + open( $fh{1}, $file1 ); + open( $fh{2}, $file2 ); + + my $same = 1; + my $notdone; + $counter{blocks}++; + while ( !$notdone ) { + $ln{1} = sysread( $fh{1}, $buf{1}, 512 ); + $ln{2} = sysread( $fh{2}, $buf{2}, 512 ); + if ( $ln{1} != $ln{2} ) { + print STDERR "blocks not the same size\n"; + return 0; + } + if ($buf{1} ne $buf{2}) { + print STDERR "block $counter{blocks} not the same\n"; + dump_block($buf{1}, $buf{2}); + return 0; + } + return 1 if $ln{1} == 0; + $counter{blocks}++; + } + + close( $fh{1} ); + close( $fh{2} ); + + return 0; +} + +sub display_file { + my ( $file ) = @_; + my $in; + if ( !open( $in, $file ) ) { + warn "Error: unable to open $file: $!\n"; + } + else { + while (<$in>) { + print STDERR $_; + } + close($in); + } +} + +our $BZLIB_BIN ; +our $BZLIB_LIB ; +our $BZLIB_INCLUDE ; +our $BUILD_BZLIB ; + +sub ParseCONFIG { + my $CONFIG = shift || 'config.in' ; + + my ($k, $v) ; + my @badkey = () ; + my %Info = () ; + my @Options = qw( BZLIB_INCLUDE BZLIB_LIB BUILD_BZLIB BZLIB_BIN ) ; + my %ValidOption = map {$_, 1} @Options ; + my %Parsed = %ValidOption ; + my $debugf = 0; + + print STDERR "Parsing $CONFIG...\n" if $debugf; + + if (!open(F, "<$CONFIG")) { + warn "warning: failed to open $CONFIG: $!\n"; + } + else { + while (<F>) { + chomp; + s/#.*$//; + next if !/\S/; + + ($k, $v) = split(/\s*=\s*/, $_, 2) ; + $k = uc $k ; + + if ($ValidOption{$k}) { + delete $Parsed{$k} ; + $Info{$k} = $v ; + } + else { + push(@badkey, $k) ; + } + } + close F ; + } + + print STDERR "Unknown keys in $CONFIG ignored [@badkey]\n" if $debugf && scalar(@badkey) ; + + $BZLIB_INCLUDE = $ENV{'BZLIB_INCLUDE'} || $Info{'BZLIB_INCLUDE'} ; + $BZLIB_LIB = $ENV{'BZLIB_LIB'} || $Info{'BZLIB_LIB'} ; + $BZLIB_BIN = $ENV{'BZLIB_BIN'} || $Info{'BZLIB_BIN'} ; + + if ($^O eq 'VMS') { + $BZLIB_INCLUDE = VMS::Filespec::vmspath($BZLIB_INCLUDE); + $BZLIB_LIB = VMS::Filespec::vmspath($BZLIB_LIB); + $BZLIB_BIN = VMS::Filespec::vmspath($BZLIB_BIN); + } + + my $x = defined($ENV{BUILD_BZLIB}) ? $ENV{BUILD_BZLIB} : $Info{BUILD_BZLIB}; + $x = 'Test' if !defined($x); + + if ( $x =~ /^yes|on|true|1$/i ) { + $BUILD_BZLIB = 1; + + print STDERR "Building internal libbz2 enabled\n" if $debugf ; + } + elsif ( $x =~ /^test$/i ) { + undef $BUILD_BZLIB; + + ## prefix libpth locincpth + my $command = $Config{cc} . + ' '. $Config{ccflags} . + ( $BZLIB_INCLUDE ? " -I$BZLIB_INCLUDE" : '' ) . + ' '. $Config{ldflags} . + ' -o show_bzversion show_bzversion.c' . + ( $BZLIB_LIB ? " -L$BZLIB_LIB" : '' ) . + ' -lbz2' + . ($^O eq 'MSWin32' ? ' 2>nul' : ' 2>/dev/null'); + + #print STDERR "command $command\n"; + if ( !system( $command ) ) { + if ( -x 'show_bzversion' && -s 'show_bzversion' ) { + my $version = `./show_bzversion`; + if ( $version ) { + chomp $version; + $BUILD_BZLIB = 0; + print STDERR "found bzip2 $version ".($BZLIB_LIB ? "in $BZLIB_LIB" : 'installed')."\n" if $debugf; + } + else { + $BUILD_BZLIB = 1; + print STDERR "compile command '$command' failed\n" if $debugf; + print STDERR "system bzip2 not useable, building internal libbz2\n" if $debugf; + } + } + else { + $BUILD_BZLIB = 1; + print STDERR "compile command '$command' failed\n" if $debugf; + print STDERR "system bzip2 not useable, building internal libbz2\n" if $debugf; + } + } + else { + $BUILD_BZLIB = 1; + print STDERR "compile command '$command' failed\n" if $debugf; + print STDERR "system bzip2 not found, building internal libbz2\n" if $debugf; + } + } + + print STDERR <<EOM if $debugf ; +INCLUDE [$BZLIB_INCLUDE] +LIB [$BZLIB_LIB] +BIN [$BZLIB_BIN] + +EOM +; + + print STDERR "Looks Good.\n" if $debugf; +} + +ParseCONFIG() ; + +$::BZIP = 'bzip2'.$Config{exe_ext}; +$::BZIP = $BZLIB_BIN ? catfile( $BZLIB_BIN, $::BZIP) : + -x catfile( 'bzlib-src', $::BZIP ) ? rel2abs( catfile( 'bzlib-src', $::BZIP ) ) : $::BZIP; + +$ENV{PATH} .= ';' . getcwd() . '\\bzlib-src' if $^O =~ /win32/i; # just in case + +$::debugf = $ENV{DEBUG}; + |