Upgrade to Compress::Zlib 2.000_05
[p5sagit/p5-mst-13.2.git] / ext / Compress / Zlib / t / 17isize.t
1
2 use lib 't';
3 use strict ;
4 use warnings;
5 use bytes;
6
7 use Test::More ;
8 use ZlibTestUtils;
9
10 BEGIN 
11
12     plan skip_all => "Lengthy Tests Disabled\n" .
13                      "set COMPRESS_ZLIB_RUN_ALL to run this test suite" 
14         unless defined $ENV{COMPRESS_ZLIB_RUN_ALL} ;
15
16     # use Test::NoWarnings, if available
17     my $extra = 0 ;
18     $extra = 1
19         if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };
20
21     plan tests => 76 + $extra ;
22
23
24     use_ok('Compress::Zlib', 2) ;
25     use_ok('IO::Compress::Gzip', qw($GzipError)) ;
26     use_ok('IO::Uncompress::Gunzip', qw($GunzipError)) ;
27     use_ok('Compress::Gzip::Constants');
28 }
29
30 my $compressed ;    
31 my $expected_crc ;
32
33 for my $wrap (0 .. 2)
34 {
35     for my $offset ( -1 .. 1 )
36     {
37         next if $wrap == 0 && $offset < 0 ;
38
39         title "Wrap $wrap, Offset $offset" ;
40
41         my $size = (GZIP_ISIZE_MAX * $wrap) + $offset ;
42
43         my $expected_isize ;
44         if ($wrap == 0) {
45             $expected_isize = $offset ;
46         }
47         elsif ($wrap == 1 && $offset <= 0) {
48             $expected_isize = GZIP_ISIZE_MAX + $offset ;
49         }
50         elsif ($wrap > 1) {
51             $expected_isize = GZIP_ISIZE_MAX + $offset - 1;
52         }
53         else {
54             $expected_isize = $offset - 1;
55         }
56         
57         sub gzipClosure
58         {
59             my $gzip = shift ;
60             my $max = shift  ;
61
62             my $index = 0 ;
63             my $inc = 1024 * 5000 ;
64             my $buff = 'x' x $inc ;
65             my $left = $max ;
66
67             return 
68                 sub {
69
70                     if ($max == 0 && $index == 0) {
71                         $expected_crc = crc32('') ;
72                         ok $gzip->close(), '  IO::Compress::Gzip::close ok X' ;
73                         ++ $index ;
74                         $_[0] .= $compressed;
75                         return length $compressed ;
76                     }
77
78                     return 0 if $index >= $max ;
79
80                     while ( ! length $compressed )
81                     {
82                         $index += $inc ;
83
84                         if ($index <= $max) {
85                             $gzip->write($buff) ;
86                             #print "Write " . length($buff) . "\n" ;
87                             #print "# LEN Compressed " . length($compressed) . "\n" ;
88                             $expected_crc = crc32($buff, $expected_crc) ;
89                             $left -= $inc ;
90                         }
91                         else  {
92                             #print "Write $left\n" ;
93                             $gzip->write('x' x $left) ;
94                             #print "# LEN Compressed " . length($compressed) . "\n" ;
95                             $expected_crc = crc32('x' x $left, $expected_crc) ;
96                             ok $gzip->close(), '  IO::Compress::Gzip::close ok ' ;
97                             last ;
98                         }
99                     }
100
101                     my $len = length $compressed ;
102                     $_[0] .= $compressed ;
103                     $compressed = '';
104                     #print "# LEN $len\n" if $len <=0 ;
105
106                     return $len ;
107                 };
108         }
109
110         my $gzip = new IO::Compress::Gzip \$compressed,
111                                 -Append     => 0,
112                                 -HeaderCRC  => 1;
113
114         ok $gzip, "  Created IO::Compress::Gzip object";
115
116         my $gunzip = new IO::Uncompress::Gunzip gzipClosure($gzip, $size),
117                                     -BlockSize  => 1024 * 500 ,
118                                     -Append => 0,
119                                     -Strict => 1;
120
121         ok $gunzip, "  Created IO::Uncompress::Gunzip object";
122
123         my $inflate = *$gunzip->{Inflate} ;
124         my $deflate = *$gzip->{Deflate} ;
125
126         my $status ;
127         my $uncompressed;
128         my $actual = 0 ;
129         while (($status = $gunzip->read($uncompressed)) > 0) {
130             #print "# READ $status\n" ;
131             $actual += $status ;
132         }
133
134         is $status, 0, '  IO::Uncompress::Gunzip::read returned 0'
135             or diag "error status is $status, error is $GunzipError" ;
136
137         ok $gunzip->close(), "  IO::Uncompress::Gunzip Closed ok" ;
138
139         is $actual, $size, "  Length of Gunzipped data is $size"
140             or diag "Expected $size, got $actual";
141
142         my $gunzip_hdr = $gunzip->getHeaderInfo();
143
144         is $gunzip_hdr->{ISIZE}, $expected_isize, 
145             sprintf("  ISIZE is $expected_isize [0x%X]", $expected_isize);
146         is $gunzip_hdr->{CRC32}, $expected_crc, 
147             sprintf("  CRC32 is $expected_crc [0x%X]", $expected_crc);
148
149         $expected_crc = 0 ;
150     }
151 }
152