12 # use Test::NoWarnings, if available
15 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
17 plan tests => 208 + $extra ;
19 use_ok('Compress::Zlib', 2) ;
20 use_ok('Compress::Gzip::Constants') ;
22 use_ok('IO::Compress::Gzip', qw($GzipError)) ;
31 my $len = length $hello ;
33 # Check zlib_version and ZLIB_VERSION are the same.
34 is Compress::Zlib::zlib_version, ZLIB_VERSION,
35 "ZLIB_VERSION matches Compress::Zlib::zlib_version" ;
40 my $name = "test.gz" ;
43 ok my $fil = gzopen($name, "wb") ;
45 is $gzerrno, 0, 'gzerrno is 0';
46 is $fil->gzerror(), 0, "gzerror() returned 0";
48 is $fil->gztell(), 0, "gztell returned 0";
49 is $gzerrno, 0, 'gzerrno is 0';
51 is $fil->gzwrite($hello), $len ;
52 is $gzerrno, 0, 'gzerrno is 0';
54 is $fil->gztell(), $len, "gztell returned $len";
55 is $gzerrno, 0, 'gzerrno is 0';
59 ok $fil = gzopen($name, "rb") ;
62 is $gzerrno, 0, 'gzerrno is 0';
65 is $fil->gzread($uncomp), $len;
67 is $fil->gztell(), $len;
74 ok $hello eq $uncomp ;
76 # check that a number can be gzipped
80 ok $fil = gzopen($name, "wb") ;
84 is $fil->gzwrite($number), $num_len, "gzwrite returned $num_len" ;
85 is $gzerrno, 0, 'gzerrno is 0';
86 ok $fil->gzflush(Z_FINISH) ;
88 is $gzerrno, 0, 'gzerrno is 0';
92 cmp_ok $gzerrno, '==', 0;
94 ok $fil = gzopen($name, "rb") ;
96 ok (($x = $fil->gzread($uncomp)) == $num_len) ;
98 ok $fil->gzerror() == 0 || $fil->gzerror() == Z_STREAM_END;
99 ok $gzerrno == 0 || $gzerrno == Z_STREAM_END;
106 or print "# gzerrno is $gzerrno\n" ;
110 ok $number == $uncomp ;
111 ok $number eq $uncomp ;
114 # now a bigger gzip test
117 my $file = "$text.gz" ;
119 ok my $f = gzopen($file, "wb") ;
121 # generate a long random string
124 { $contents .= chr int rand 256 }
126 $len = length $contents ;
128 ok $f->gzwrite($contents) == $len ;
132 ok $f = gzopen($file, "rb") ;
137 is $f->gzread($uncompressed, $len), $len ;
139 ok $contents eq $uncompressed
141 or print "# Length orig $len" .
142 ", Length uncompressed " . length($uncompressed) . "\n" ;
149 # gzip - readline tests
150 # ======================
152 # first create a small gzipped text file
154 my @text = (<<EOM, <<EOM, <<EOM, <<EOM) ;
159 the line after the previous line
164 $text = join("", @text) ;
166 ok $fil = gzopen($name, "wb") ;
167 ok $fil->gzwrite($text) == length $text ;
170 # now try to read it back in
171 ok $fil = gzopen($name, "rb") ;
174 for my $i (0 .. @text -2)
176 ok $fil->gzreadline($line) > 0;
177 ok $line eq $text[$i] ;
181 # now read the last line
182 ok $fil->gzreadline($line) > 0;
183 ok $line eq $text[-1] ;
187 is $fil->gzreadline($line), 0;
194 # a text file with a very long line (bigger than the internal buffer)
195 my $line1 = ("abcdefghijklmnopq" x 2000) . "\n" ;
196 my $line2 = "second line\n" ;
197 $text = $line1 . $line2 ;
198 ok $fil = gzopen($name, "wb") ;
199 ok $fil->gzwrite($text) == length $text ;
202 # now try to read it back in
203 ok $fil = gzopen($name, "rb") ;
207 while ($fil->gzreadline($line) > 0) {
212 ok $got[0] eq $line1 ;
213 ok $got[1] eq $line2 ;
221 # a text file which is not termined by an EOL
223 $line1 = "hello hello, I'm back again\n" ;
224 $line2 = "there is no end in sight" ;
226 $text = $line1 . $line2 ;
227 ok $fil = gzopen($name, "wb") ;
228 ok $fil->gzwrite($text) == length $text ;
231 # now try to read it back in
232 ok $fil = gzopen($name, "rb") ;
234 while ($fil->gzreadline($line) > 0) {
239 ok $got[0] eq $line1 ;
240 ok $got[1] eq $line2 ;
249 title 'mix gzread and gzreadline';
251 # case 1: read a line, then a block. The block is
252 # smaller than the internal block used by
254 my $name = "test.gz" ;
255 my $lex = new LexFile $name ;
256 $line1 = "hello hello, I'm back again\n" ;
257 $line2 = "abc" x 200 ;
258 my $line3 = "def" x 200 ;
260 $text = $line1 . $line2 . $line3 ;
261 ok $fil = gzopen($name, "wb"), ' gzopen for write ok' ;
262 is $fil->gzwrite($text), length $text, ' gzwrite ok' ;
263 is $fil->gztell(), length $text, ' gztell ok' ;
264 ok ! $fil->gzclose, ' gzclose ok' ;
266 # now try to read it back in
267 ok $fil = gzopen($name, "rb"), ' gzopen for read ok' ;
268 ok ! $fil->gzeof(), ' !gzeof' ;
269 cmp_ok $fil->gzreadline($line), '>', 0, ' gzreadline' ;
270 is $fil->gztell(), length $line1, ' gztell ok' ;
271 ok ! $fil->gzeof(), ' !gzeof' ;
272 is $line, $line1, ' got expected line' ;
273 cmp_ok $fil->gzread($line, length $line2), '>', 0, ' gzread ok' ;
274 is $fil->gztell(), length($line1)+length($line2), ' gztell ok' ;
275 ok ! $fil->gzeof(), ' !gzeof' ;
276 is $line, $line2, ' read expected block' ;
277 cmp_ok $fil->gzread($line, length $line3), '>', 0, ' gzread ok' ;
278 is $fil->gztell(), length($text), ' gztell ok' ;
279 ok $fil->gzeof(), ' !gzeof' ;
280 is $line, $line3, ' read expected block' ;
281 ok ! $fil->gzclose, ' gzclose' ;
285 title "Pass gzopen a filehandle - use IO::File" ;
287 my $name = "test.gz" ;
288 my $lex = new LexFile $name ;
290 my $hello = "hello" ;
291 my $len = length $hello ;
295 my $f = new IO::File ">$name" ;
298 ok my $fil = gzopen($f, "wb") ;
300 ok $fil->gzwrite($hello) == $len ;
304 $f = new IO::File "<$name" ;
305 ok $fil = gzopen($name, "rb") ;
308 ok (($x = $fil->gzread($uncomp)) == $len)
309 or print "# length $x, expected $len\n" ;
317 ok $hello eq $uncomp ;
324 title "Pass gzopen a filehandle - use open" ;
326 my $name = "test.gz" ;
327 my $lex = new LexFile $name ;
329 my $hello = "hello" ;
330 my $len = length $hello ;
336 ok my $fil = gzopen(*F, "wb") ;
338 is $fil->gzwrite($hello), $len ;
343 ok $fil = gzopen(*F, "rb") ;
346 $x = $fil->gzread($uncomp);
355 ok $hello eq $uncomp ;
360 foreach my $stdio ( ['-', '-'], [*STDIN, *STDOUT])
362 my $stdin = $stdio->[0];
363 my $stdout = $stdio->[1];
365 title "Pass gzopen a filehandle - use $stdin" ;
367 my $name = "test.gz" ;
368 my $lex = new LexFile $name ;
370 my $hello = "hello" ;
371 my $len = length $hello ;
375 ok open(SAVEOUT, ">&STDOUT"), " save STDOUT";
376 my $dummy = fileno SAVEOUT;
377 ok open(STDOUT, ">$name"), " redirect STDOUT" ;
381 my $fil = gzopen($stdout, "wb") ;
384 ($fil->gzwrite($hello) == $len) &&
385 ($fil->gzclose == 0) ;
387 open(STDOUT, ">&SAVEOUT");
389 ok $status, " wrote to stdout";
391 open(SAVEIN, "<&STDIN");
392 ok open(STDIN, "<$name"), " redirect STDIN";
393 $dummy = fileno SAVEIN;
395 ok $fil = gzopen($stdin, "rb") ;
398 ok (($x = $fil->gzread($uncomp)) == $len)
399 or print "# length $x, expected $len\n" ;
405 open(STDIN, "<&SAVEIN");
409 ok $hello eq $uncomp ;
415 title 'test parameters for gzopen';
416 my $name = "test.gz" ;
417 my $lex = new LexFile $name ;
424 eval ' $fil = gzopen() ' ;
425 like $@, mkEvalErr('Not enough arguments for Compress::Zlib::gzopen'),
426 ' gzopen with missing mode fails' ;
429 $fil = gzopen($name, "xy") ;
430 ok ! defined $fil, ' gzopen with unknown mode fails' ;
432 $fil = gzopen($name, "ab") ;
433 ok $fil, ' gzopen with mode "ab" is ok' ;
435 $fil = gzopen($name, "wb6") ;
436 ok $fil, ' gzopen with mode "wb6" is ok' ;
438 $fil = gzopen($name, "wbf") ;
439 ok $fil, ' gzopen with mode "wbf" is ok' ;
441 $fil = gzopen($name, "wbh") ;
442 ok $fil, ' gzopen with mode "wbh" is ok' ;
446 title 'Read operations when opened for writing';
448 my $name = "test.gz" ;
449 my $lex = new LexFile $name ;
450 ok $fil = gzopen($name, "wb"), ' gzopen for writing' ;
451 ok !$fil->gzeof(), ' !eof'; ;
452 is $fil->gzread(), Z_STREAM_ERROR, " gzread returns Z_STREAM_ERROR" ;
456 title 'write operations when opened for reading';
458 my $name = "test.gz" ;
459 my $lex = new LexFile $name ;
461 ok $fil = gzopen($name, "wb"), " gzopen for writing" ;
462 is $fil->gzwrite($text), length $text, " gzwrite ok" ;
463 ok ! $fil->gzclose, " gzclose ok" ;
465 ok $fil = gzopen($name, "rb"), " gzopen for reading" ;
466 is $fil->gzwrite(), Z_STREAM_ERROR, " gzwrite returns Z_STREAM_ERROR" ;
470 title 'read/write a non-readable/writable file';
475 my $lex = new LexFile $name ;
476 writeFile($name, "abc");
479 skip "Cannot create non-writable file", 3
482 ok ! -w $name, " input file not writable";
484 my $fil = gzopen($name, "wb") ;
485 ok !$fil, " gzopen returns undef" ;
486 ok $gzerrno, " gzerrno ok" or
487 diag " gzerrno $gzerrno\n";
495 my $lex = new LexFile $name ;
496 writeFile($name, "abc");
499 skip "Cannot create non-readable file", 3
502 ok ! -r $name, " input file not readable";
504 $fil = gzopen($name, "rb") ;
505 ok !$fil, " gzopen returns undef" ;
506 ok $gzerrno, " gzerrno ok";
516 my $name ;#= "test.gz" ;
517 my $lex = new LexFile $name ;
519 my $first = "beginning" ;
520 my $last = "the end" ;
521 my $iow = gzopen($name, "w");
522 $iow->gzwrite($first) ;
523 ok $iow->gzseek(5, SEEK_CUR) ;
524 is $iow->gztell(), length($first)+5;
525 ok $iow->gzseek(0, SEEK_CUR) ;
526 is $iow->gztell(), length($first)+5;
527 ok $iow->gzseek(length($first)+10, SEEK_SET) ;
528 is $iow->gztell(), length($first)+10;
530 $iow->gzwrite($last) ;
533 ok GZreadFile($name) eq $first . "\x00" x 10 . $last ;
535 my $io = gzopen($name, "r");
536 ok $io->gzseek(length($first), SEEK_CUR) ;
538 is $io->gztell(), length($first);
540 ok $io->gzread($buff, 5) ;
541 is $buff, "\x00" x 5 ;
542 is $io->gztell(), length($first) + 5;
544 is $io->gzread($buff, 0), 0 ;
545 #is $buff, "\x00" x 5 ;
546 is $io->gztell(), length($first) + 5;
548 ok $io->gzseek(0, SEEK_CUR) ;
549 my $here = $io->gztell() ;
550 is $here, length($first)+5;
552 ok $io->gzseek($here+5, SEEK_SET) ;
553 is $io->gztell(), $here+5 ;
554 ok $io->gzread($buff, 100) ;
561 my $name = "test.gz" ;
562 my $lex = new LexFile $name ;
564 my $a = gzopen($name, "w");
567 or print "# gzerrno is $Compress::Zlib::gzerrno \n" ;
568 eval { $a->gzseek(-1, 10) ; };
569 like $@, mkErr("gzseek: unknown value, 10, for whence parameter");
571 eval { $a->gzseek(-1, SEEK_END) ; };
572 like $@, mkErr("gzseek: cannot seek backwards");
578 my $u = gzopen($name, "r");
580 eval { $u->gzseek(-1, 10) ; };
581 like $@, mkErr("gzseek: unknown value, 10, for whence parameter");
583 eval { $u->gzseek(-1, SEEK_END) ; };
584 like $@, mkErr("gzseek: SEEK_END not allowed");
586 eval { $u->gzseek(-1, SEEK_CUR) ; };
587 like $@, mkErr("gzseek: cannot seek backwards");