10 our ($BadPerl, $UncompressClass);
14 plan(skip_all => "Tied Filehandle needs Perl 5.005 or better" )
17 # use Test::NoWarnings, if available
20 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
23 $BadPerl = ($] >= 5.006 and $] <= 5.008) ;
32 plan tests => $tests + $extra ;
37 use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
43 my $filename = shift ;
47 my $fil = new $UncompressClass $filename,
53 $data = $init if defined $init ;
54 1 while $fil->read($data) > 0;
63 my $CompressClass = identify();
64 $UncompressClass = getInverse($CompressClass);
65 my $Error = getErrorRef($CompressClass);
66 my $UnError = getErrorRef($UncompressClass);
72 title "Testing $CompressClass";
76 my $gz = new $CompressClass(\$x);
81 like $@, mkErr("^getc Not Available: File opened only for output");
83 eval { read($gz, $buff, 1) } ;
84 like $@, mkErr("^read Not Available: File opened only for output");
87 like $@, mkErr("^readline Not Available: File opened only for output");
93 $UncompressClass = getInverse($CompressClass);
95 title "Testing $UncompressClass";
98 my $guz = new $CompressClass(\$gc);
103 my $gz = new $UncompressClass(\$gc);
107 eval { print $gz "abc" } ;
108 like $@, mkErr("^print Not Available: File opened only for intput");
110 eval { printf $gz "fmt", "abc" } ;
111 like $@, mkErr("^printf Not Available: File opened only for intput");
113 #eval { write($gz, $buff, 1) } ;
114 #like $@, mkErr("^write Not Available: File opened only for intput");
119 $UncompressClass = getInverse($CompressClass);
121 title "Testing $CompressClass and $UncompressClass";
126 # these tests come almost 100% from IO::String
128 my $lex = new LexFile my $name ;
130 my $io = $CompressClass->new($name);
134 my $heisan = "Heisan\n";
139 is $io->tell(), length($heisan) ;
141 print($io "a", "b", "c");
147 print $io "f", "g", "h";
150 my $foo = "1234567890";
152 ok syswrite($io, $foo, length($foo)) == length($foo) ;
154 { is $io->syswrite($foo, length $foo), length $foo }
156 { is $io->syswrite($foo), length $foo }
157 ok $io->syswrite($foo, length($foo)) == length $foo;
158 ok $io->write($foo, length($foo), 5) == 5;
159 ok $io->write("xxx\n", 100, -1) == 1;
162 printf $io "i(%d)", $_;
163 $io->printf("[%d]\n", $_);
173 is myGZreadFile($name), "Heisan\nabcde\nf,g,h\n" .
174 ("1234567890" x 3) . "67890\n" .
175 "i(1)[1]\ni(2)[2]\ni(3)[3]\n\n";
191 my $lex = new LexFile my $name ;
193 my $iow = new $CompressClass $name ;
200 my $io = new $UncompressClass $name ;
206 or print "# Got " . scalar(@lines) . " lines, expected 6\n" ;
207 is $lines[1], "of a paragraph\n" ;
208 is join('', @lines), $str ;
210 is $io->tell(), length($str) ;
214 ok ! ( defined($io->getline) ||
215 (@tmp = $io->getlines) ||
217 defined($io->getc) ||
218 read($io, $buf, 100) != 0) ;
223 local $/; # slurp mode
224 my $io = $UncompressClass->new($name);
226 my @lines = $io->getlines;
228 ok @lines == 1 && $lines[0] eq $str;
230 $io = $UncompressClass->new($name);
238 local $/ = ""; # paragraph mode
239 my $io = $UncompressClass->new($name);
244 or print "# Got " . scalar(@lines) . " lines, expected 2\n" ;
245 ok $lines[0] eq "This is an example\nof a paragraph\n\n\n"
246 or print "# $lines[0]\n";
247 ok $lines[1] eq "and a single line.\n\n";
252 my $io = $UncompressClass->new($name);
259 $err++ if $. != ++$no;
266 or print "# Got " . scalar(@lines) . " lines, expected 3\n" ;
267 ok join("-", @lines) eq
268 "This- is- an example\n" .
269 "of a paragraph\n\n\n" .
270 "and a single line.\n\n";
277 my $io = $UncompressClass->new($name);
281 eval { read($io, $buf, -1) } ;
282 like $@, mkErr("length parameter is negative");
285 is read($io, $buf, 0), 0, "Requested 0 bytes" ;
287 ok read($io, $buf, 3) == 3 ;
290 ok sysread($io, $buf, 3, 2) == 3 ;
292 or print "# [$buf]\n" ;;
299 # ok read($io, $buf, 20) == 4 ;
300 # ok $buf eq "e.\n\n";
302 # ok read($io, $buf, 20) == 0 ;
311 # Read from non-compressed file
322 my $lex = new LexFile my $name ;
324 writeFile($name, $str);
328 my $io = new $UncompressClass $name, -Transparent => 1 ;
332 ok $io->tell() == 0 ;
335 ok $lines[1] eq "of a paragraph\n" ;
336 ok join('', @lines) eq $str ;
338 ok $io->tell() == length($str) ;
342 ok ! ( defined($io->getline) ||
343 (@tmp = $io->getlines) ||
345 defined($io->getc) ||
346 read($io, $buf, 100) != 0) ;
351 local $/; # slurp mode
352 my $io = $UncompressClass->new($name);
354 my @lines = $io->getlines;
356 ok @lines == 1 && $lines[0] eq $str;
358 $io = $UncompressClass->new($name);
366 local $/ = ""; # paragraph mode
367 my $io = $UncompressClass->new($name);
372 or print "# exected 2 lines, got " . scalar(@lines) . "\n";
373 ok $lines[0] eq "This is an example\nof a paragraph\n\n\n"
374 or print "# [$lines[0]]\n" ;
375 ok $lines[1] eq "and a single line.\n\n";
380 my $io = $UncompressClass->new($name);
387 $err++ if $. != ++$no;
394 ok join("-", @lines) eq
395 "This- is- an example\n" .
396 "of a paragraph\n\n\n" .
397 "and a single line.\n\n";
404 my $io = $UncompressClass->new($name);
406 ok read($io, $buf, 3) == 3 ;
409 ok sysread($io, $buf, 3, 2) == 3 ;
417 # ok read($io, $buf, 20) == 4 ;
418 # ok $buf eq "e.\n\n";
420 # ok read($io, $buf, 20) == 0 ;
430 # Vary the length parameter in a read
445 foreach my $bufsize (1, 3, 512, 4096, length($str)-1, length($str), length($str)+1)
447 foreach my $trans (0, 1)
449 foreach my $append (0, 1)
451 title "Read Tests - buf length $bufsize, Transparent $trans, Append $append" ;
453 my $lex = new LexFile my $name ;
456 writeFile($name, $str) ;
459 my $iow = new $CompressClass $name ;
465 my $io = $UncompressClass->new($name,
467 -Transparent => $trans);
474 1 while $io->read($buf, $bufsize) > 0;
478 $buf .= $tmp while $io->read($tmp, $bufsize) > 0 ;
480 is length $buf, length $str;