1b39c5158Smillert
2b39c5158Smillertuse strict;
3b39c5158Smillertuse warnings;
4b39c5158Smillertuse bytes;
5b39c5158Smillert
6b39c5158Smillertuse Test::More ;
7b39c5158Smillertuse CompTestUtils;
8b39c5158Smillert
9b39c5158SmillertBEGIN
10b39c5158Smillert{
11b39c5158Smillert    # use Test::NoWarnings, if available
12b39c5158Smillert    my $extra = 0 ;
13b39c5158Smillert    $extra = 1
14b39c5158Smillert        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };
15b39c5158Smillert
16b39c5158Smillert    plan tests => 49 + $extra ;
17b39c5158Smillert}
18b39c5158Smillert
19b39c5158Smillert
20b39c5158Smillert
21b39c5158Smillertmy $CompressClass   = identify();
22b39c5158Smillertmy $UncompressClass = getInverse($CompressClass);
23b39c5158Smillertmy $Error           = getErrorRef($CompressClass);
24b39c5158Smillertmy $UnError         = getErrorRef($UncompressClass);
25b39c5158Smillert
26b39c5158Smillertuse Compress::Raw::Zlib;
27b39c5158Smillertuse IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
28b39c5158Smillert
29b39c5158Smillertsub myGZreadFile
30b39c5158Smillert{
31b39c5158Smillert    my $filename = shift ;
32b39c5158Smillert    my $init = shift ;
33b39c5158Smillert
34b39c5158Smillert
35*256a93a4Safresh1    my $fil = $UncompressClass->can('new')->( $UncompressClass, $filename,
36b39c5158Smillert                                    -Strict   => 1,
37b39c5158Smillert                                    -Append   => 1
38*256a93a4Safresh1                                    );
39b39c5158Smillert
40b39c5158Smillert    my $data = '';
41b39c5158Smillert    $data = $init if defined $init ;
42b39c5158Smillert    1 while $fil->read($data) > 0;
43b39c5158Smillert
44b39c5158Smillert    $fil->close ;
45b39c5158Smillert    return $data ;
46b39c5158Smillert}
47b39c5158Smillert
48b39c5158Smillert
49b39c5158Smillert{
50b39c5158Smillert
51b39c5158Smillert    title "Testing $CompressClass Errors";
52b39c5158Smillert
53b39c5158Smillert}
54b39c5158Smillert
55b39c5158Smillert
56b39c5158Smillert{
57b39c5158Smillert    title "Testing $UncompressClass Errors";
58b39c5158Smillert
59b39c5158Smillert}
60b39c5158Smillert
61b39c5158Smillert{
62b39c5158Smillert    title "Testing $CompressClass and $UncompressClass";
63b39c5158Smillert
64b39c5158Smillert    {
65b39c5158Smillert        title "flush" ;
66b39c5158Smillert
67b39c5158Smillert
68*256a93a4Safresh1        my $lex = LexFile->new( my $name );
69b39c5158Smillert
70b39c5158Smillert        my $hello = <<EOM ;
71b39c5158Smillerthello world
72b39c5158Smillertthis is a test
73b39c5158SmillertEOM
74b39c5158Smillert
75b39c5158Smillert        {
76b39c5158Smillert          my $x ;
77*256a93a4Safresh1          ok $x = $CompressClass->can('new')->( $CompressClass, $name );
78b39c5158Smillert
79b39c5158Smillert          ok $x->write($hello), "write" ;
80b39c5158Smillert          ok $x->flush(Z_FINISH), "flush";
81b39c5158Smillert          ok $x->close, "close" ;
82b39c5158Smillert        }
83b39c5158Smillert
84b39c5158Smillert        {
85b39c5158Smillert          my $uncomp;
86*256a93a4Safresh1          ok my $x = $UncompressClass->can('new')->( $UncompressClass, $name, -Append => 1 );
87b39c5158Smillert
88b39c5158Smillert          my $len ;
89b39c5158Smillert          1 while ($len = $x->read($uncomp)) > 0 ;
90b39c5158Smillert
91b39c5158Smillert          is $len, 0, "read returned 0";
92b39c5158Smillert
93b39c5158Smillert          ok $x->close ;
94b39c5158Smillert          is $uncomp, $hello ;
95b39c5158Smillert        }
96b39c5158Smillert    }
97b39c5158Smillert
98b39c5158Smillert
99b39c5158Smillert    if ($CompressClass ne 'RawDeflate')
100b39c5158Smillert    {
101b39c5158Smillert        # write empty file
102b39c5158Smillert        #========================================
103b39c5158Smillert
104b39c5158Smillert        my $buffer = '';
105b39c5158Smillert        {
106b39c5158Smillert          my $x ;
107*256a93a4Safresh1          ok $x = $CompressClass->can('new')->( $CompressClass, \$buffer);
108b39c5158Smillert          ok $x->close ;
109b39c5158Smillert
110b39c5158Smillert        }
111b39c5158Smillert
112b39c5158Smillert        my $keep = $buffer ;
113b39c5158Smillert        my $uncomp= '';
114b39c5158Smillert        {
115b39c5158Smillert          my $x ;
116*256a93a4Safresh1          ok $x = $UncompressClass->can('new')->( $UncompressClass, \$buffer, Append => 1)  ;
117b39c5158Smillert
118b39c5158Smillert          1 while $x->read($uncomp) > 0  ;
119b39c5158Smillert
120b39c5158Smillert          ok $x->close ;
121b39c5158Smillert        }
122b39c5158Smillert
123b39c5158Smillert        ok $uncomp eq '' ;
124b39c5158Smillert        ok $buffer eq $keep ;
125b39c5158Smillert
126b39c5158Smillert    }
127b39c5158Smillert
128b39c5158Smillert
129b39c5158Smillert    {
130b39c5158Smillert        title "inflateSync on plain file";
131b39c5158Smillert
132b39c5158Smillert        my $hello = "I am a HAL 9000 computer" x 2001 ;
133b39c5158Smillert
134*256a93a4Safresh1        my $k = $UncompressClass->can('new')->( $UncompressClass, \$hello, Transparent => 1);
135b39c5158Smillert        ok $k ;
136b39c5158Smillert
137b39c5158Smillert        # Skip to the flush point -- no-op for plain file
138b39c5158Smillert        my $status = $k->inflateSync();
139b39c5158Smillert        is $status, 1
140b39c5158Smillert            or diag $k->error() ;
141b39c5158Smillert
142b39c5158Smillert        my $rest;
143b39c5158Smillert        is $k->read($rest, length($hello)), length($hello)
144b39c5158Smillert            or diag $k->error() ;
145b39c5158Smillert        ok $rest eq $hello ;
146b39c5158Smillert
147b39c5158Smillert        ok $k->close();
148b39c5158Smillert    }
149b39c5158Smillert
150b39c5158Smillert    {
151b39c5158Smillert        title "$CompressClass: inflateSync for real";
152b39c5158Smillert
153b39c5158Smillert        # create a deflate stream with flush points
154b39c5158Smillert
155b39c5158Smillert        my $hello = "I am a HAL 9000 computer" x 2001 ;
156b39c5158Smillert        my $goodbye = "Will I dream?" x 2010;
157b39c5158Smillert        my ($x, $err, $answer, $X, $Z, $status);
158b39c5158Smillert        my $Answer ;
159b39c5158Smillert
160*256a93a4Safresh1        ok ($x = $CompressClass->can('new')->( $CompressClass, \$Answer));
161b39c5158Smillert        ok $x ;
162b39c5158Smillert
163b39c5158Smillert        is $x->write($hello), length($hello);
164b39c5158Smillert
165b39c5158Smillert        # create a flush point
166b39c5158Smillert        ok $x->flush(Z_FULL_FLUSH) ;
167b39c5158Smillert
168b39c5158Smillert        is $x->write($goodbye), length($goodbye);
169b39c5158Smillert
170b39c5158Smillert        ok $x->close() ;
171b39c5158Smillert
172b39c5158Smillert        my $k;
173*256a93a4Safresh1        $k = $UncompressClass->can('new')->( $UncompressClass, \$Answer, BlockSize => 1);
174b39c5158Smillert        ok $k ;
175b39c5158Smillert
176b39c5158Smillert        my $initial;
177b39c5158Smillert        is $k->read($initial, 1), 1 ;
178b39c5158Smillert        is $initial, substr($hello, 0, 1);
179b39c5158Smillert
180b39c5158Smillert        # Skip to the flush point
181b39c5158Smillert        $status = $k->inflateSync();
182b39c5158Smillert        is $status, 1, "   inflateSync returned 1"
183b39c5158Smillert            or diag $k->error() ;
184b39c5158Smillert
185b39c5158Smillert        my $rest;
186b39c5158Smillert        is $k->read($rest, length($hello) + length($goodbye)),
187b39c5158Smillert                length($goodbye)
188b39c5158Smillert            or diag $k->error() ;
189b39c5158Smillert        ok $rest eq $goodbye, " got expected output" ;
190b39c5158Smillert
191b39c5158Smillert        ok $k->close();
192b39c5158Smillert    }
193b39c5158Smillert
194b39c5158Smillert    {
195b39c5158Smillert        title "$CompressClass: inflateSync no FLUSH point";
196b39c5158Smillert
197b39c5158Smillert        # create a deflate stream with flush points
198b39c5158Smillert
199b39c5158Smillert        my $hello = "I am a HAL 9000 computer" x 2001 ;
200b39c5158Smillert        my ($x, $err, $answer, $X, $Z, $status);
201b39c5158Smillert        my $Answer ;
202b39c5158Smillert
203*256a93a4Safresh1        ok ($x = $CompressClass->can('new')->( $CompressClass, \$Answer));
204b39c5158Smillert        ok $x ;
205b39c5158Smillert
206b39c5158Smillert        is $x->write($hello), length($hello);
207b39c5158Smillert
208b39c5158Smillert        ok $x->close() ;
209b39c5158Smillert
210*256a93a4Safresh1        my $k = $UncompressClass->can('new')->( $UncompressClass, \$Answer, BlockSize => 1);
211b39c5158Smillert        ok $k ;
212b39c5158Smillert
213b39c5158Smillert        my $initial;
214b39c5158Smillert        is $k->read($initial, 1), 1 ;
215b39c5158Smillert        is $initial, substr($hello, 0, 1);
216b39c5158Smillert
217b39c5158Smillert        # Skip to the flush point
218b39c5158Smillert        $status = $k->inflateSync();
219b39c5158Smillert        is $status, 0
220b39c5158Smillert            or diag $k->error() ;
221b39c5158Smillert
222b39c5158Smillert        ok $k->close();
223b39c5158Smillert        is $k->inflateSync(), 0 ;
224b39c5158Smillert    }
225b39c5158Smillert
226b39c5158Smillert}
227b39c5158Smillert
228b39c5158Smillert
229b39c5158Smillert1;
230