summaryrefslogtreecommitdiff
path: root/t/021-compfile.t
blob: f751dde30699931c4f2bd2bb93ef38829168a9f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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" );