4 @INC = ("../lib", "lib");
17 plan(skip_all => "oneshot needs Perl 5.005 or better - you have Perl $]" )
21 # use Test::NoWarnings, if available
24 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
26 plan tests => 2462 + $extra ;
28 use_ok('Compress::Zlib', 2) ;
30 use_ok('IO::Compress::Gzip', qw($GzipError)) ;
31 use_ok('IO::Uncompress::Gunzip', qw($GunzipError)) ;
33 use_ok('IO::Compress::Deflate', qw($DeflateError)) ;
34 use_ok('IO::Uncompress::Inflate', qw($InflateError)) ;
36 use_ok('IO::Compress::RawDeflate', qw($RawDeflateError)) ;
37 use_ok('IO::Uncompress::RawInflate', qw($RawInflateError)) ;
39 use_ok('IO::Uncompress::AnyInflate', qw(anyinflate $AnyInflateError)) ;
44 # Check zlib_version and ZLIB_VERSION are the same.
45 is Compress::Zlib::zlib_version, ZLIB_VERSION,
46 "ZLIB_VERSION matches Compress::Zlib::zlib_version" ;
50 foreach my $bit ('IO::Compress::Gzip',
51 'IO::Uncompress::Gunzip',
52 'IO::Compress::Deflate',
53 'IO::Uncompress::Inflate',
54 'IO::Compress::RawDeflate',
55 'IO::Uncompress::RawInflate',
56 'IO::Uncompress::AnyInflate',
59 my $Error = getErrorRef($bit);
60 my $Func = getTopFuncRef($bit);
61 my $TopType = getTopFuncName($bit);
63 title "Testing $TopType Error Cases";
68 eval { $a = $Func->(\$a => \$x, Fred => 1) ;} ;
69 like $@, mkErr("^$TopType: unknown key value\\(s\\) Fred"), ' Illegal Parameters';
71 eval { $a = $Func->() ;} ;
72 like $@, mkErr("^$TopType: expected at least 1 parameters"), ' No Parameters';
74 eval { $a = $Func->(\$x, \1) ;} ;
75 like $@, mkErr("^$TopType: output buffer is read-only"), ' Output is read-only' ;
78 eval { $a = $Func->($in, \$x) ;} ;
79 like $@, mkErr("^$TopType: input filename is undef or null string"),
80 ' Input filename undef' ;
83 eval { $a = $Func->($in, \$x) ;} ;
84 like $@, mkErr("^$TopType: input filename is undef or null string"),
85 ' Input filename empty' ;
87 my $lex1 = new LexFile my $in1 ;
88 writeFile($in1, "abc");
90 eval { $a = $Func->($in1, $out) ;} ;
91 like $@, mkErr("^$TopType: input and output filename are identical"),
92 ' Input and Output filename are the same';
94 eval { $a = $Func->(\$in, \$in) ;} ;
95 like $@, mkErr("^$TopType: input and output buffer are identical"),
96 ' Input and Output buffer are the same';
98 my $lex = new LexFile my $out_file ;
99 open OUT, ">$out_file" ;
100 eval { $a = $Func->(\*OUT, \*OUT) ;} ;
101 like $@, mkErr("^$TopType: input and output handle are identical"),
102 ' Input and Output handle are the same';
105 is -s $out_file, 0, " File zero length" ;
108 my $object = bless \%x, "someClass" ;
110 # Buffer not a scalar reference
111 #eval { $a = $Func->(\$x, \%x) ;} ;
112 eval { $a = $Func->(\$x, $object) ;} ;
113 like $@, mkErr("^$TopType: illegal output parameter"),
117 #eval { $a = $Func->(\%x, \$x) ;} ;
118 eval { $a = $Func->($object, \$x) ;} ;
119 like $@, mkErr("^$TopType: illegal input parameter"),
123 my $filename = 'abc.def';
124 ok ! -e $filename, " input file '$filename' does not exist";
125 $a = $Func->($filename, \$x) ;
126 is $a, undef, " $TopType returned undef";
127 like $$Error, "/^input file '$filename' does not exist\$/", " input File '$filename' does not exist";
129 $filename = '/tmp/abd/abc.def';
130 ok ! -e $filename, " output File '$filename' does not exist";
131 $a = $Func->(\$x, $filename) ;
132 is $a, undef, " $TopType returned undef";
133 like $$Error, ("/^(cannot open file '$filename'|input file '$filename' does not exist):/"), " output File '$filename' does not exist";
135 $a = $Func->(\$x, '<abc>') ;
136 is $a, undef, " $TopType returned undef";
137 like $$Error, "/Need input fileglob for outout fileglob/",
138 ' Output fileglob with no input fileglob';
140 $a = $Func->('<abc)>', '<abc>') ;
141 is $a, undef, " $TopType returned undef";
142 like $$Error, "/Unmatched \\) in input fileglob/",
143 " Unmatched ) in input fileglob";
146 foreach my $bit ('IO::Uncompress::Gunzip',
147 'IO::Uncompress::Inflate',
148 'IO::Uncompress::RawInflate',
149 'IO::Uncompress::AnyInflate',
152 my $Error = getErrorRef($bit);
153 my $Func = getTopFuncRef($bit);
154 my $TopType = getTopFuncName($bit);
156 my $data = "mary had a little lamb" ;
159 for my $trans ( 0, 1)
161 title "Non-compressed data with $TopType, Transparent => $trans ";
166 $a = $Func->(\$data, \$out, Transparent => $trans) ;
168 is $data, $keep, " Input buffer not changed" ;
172 ok $a, " $TopType returned true" ;
173 is $out, $data, " got expected output" ;
174 ok ! $$Error, " no error [$$Error]" ;
178 ok ! $a, " $TopType returned false" ;
179 #like $$Error, '/xxx/', " error" ;
180 ok $$Error, " error is '$$Error'" ;
185 foreach my $bit ('IO::Compress::Gzip',
186 'IO::Compress::Deflate',
187 'IO::Compress::RawDeflate',
190 my $Error = getErrorRef($bit);
191 my $Func = getTopFuncRef($bit);
192 my $TopType = getTopFuncName($bit);
193 my $TopTypeInverse = getInverse($bit);
194 my $FuncInverse = getTopFuncRef($TopTypeInverse);
195 my $ErrorInverse = getErrorRef($TopTypeInverse);
197 title "$TopTypeInverse - corrupt data";
199 my $data = "abcd" x 100 ;
202 ok $Func->(\$data, \$out), " $TopType ok";
204 # corrupt the compressed data
205 #substr($out, -10, 10) = "x" x 10 ;
206 substr($out, int(length($out)/3), 10) = 'abcdeabcde';
209 ok ! $FuncInverse->(\$out => \$result, Transparent => 0), " $TopTypeInverse ok";
210 ok $$ErrorInverse, " Got error '$$ErrorInverse'" ;
212 #is $result, $data, " data ok";
214 ok ! anyinflate(\$out => \$result, Transparent => 0), " anyinflate ok";
215 ok $AnyInflateError, " Got error '$AnyInflateError'" ;
219 foreach my $bit ('IO::Compress::Gzip',
220 'IO::Compress::Deflate',
221 'IO::Compress::RawDeflate',
224 my $Error = getErrorRef($bit);
225 my $Func = getTopFuncRef($bit);
226 my $TopType = getTopFuncName($bit);
227 my $TopTypeInverse = getInverse($bit);
228 my $FuncInverse = getTopFuncRef($TopTypeInverse);
230 for my $append ( 1, 0 )
233 $already = 'abcde' if $append ;
235 for my $buffer ( undef, '', "abcde" )
238 my $disp_content = defined $buffer ? $buffer : '<undef>' ;
241 my $out_file = "abcde.out";
242 my $in_file = "abcde.in";
245 title "$TopType - From Buff to Buff content '$disp_content' Append $append" ;
247 my $output = $already;
248 ok &$Func(\$buffer, \$output, Append => $append), ' Compressed ok' ;
250 is $keep, $buffer, " Input buffer not changed" ;
251 my $got = anyUncompress(\$output, $already);
252 $got = undef if ! defined $buffer && $got eq '' ;
253 is $got, $buffer, " Uncompressed matches original";
258 title "$TopType - From Buff to Array Ref content '$disp_content' Append $append" ;
260 my @output = ('first') ;
261 ok &$Func(\$buffer, \@output, Append => $append), ' Compressed ok' ;
263 is $output[0], 'first', " Array[0] unchanged";
264 is $keep, $buffer, " Input buffer not changed" ;
265 my $got = anyUncompress($output[1]);
266 $got = undef if ! defined $buffer && $got eq '' ;
267 is $got, $buffer, " Uncompressed matches original";
271 title "$TopType - From Array Ref to Array Ref content '$disp_content' Append $append" ;
273 my @output = ('first') ;
274 my @input = ( \$buffer);
275 ok &$Func(\@input, \@output, Append => $append), ' Compressed ok' ;
277 is $output[0], 'first', " Array[0] unchanged";
278 is $keep, $buffer, " Input buffer not changed" ;
279 my $got = anyUncompress($output[1]);
280 $got = undef if ! defined $buffer && $got eq '' ;
281 is $got, $buffer, " Uncompressed matches original";
286 title "$TopType - From Buff to Filename content '$disp_content' Append $append" ;
288 my $lex = new LexFile($out_file) ;
289 ok ! -e $out_file, " Output file does not exist";
290 writeFile($out_file, $already);
292 ok &$Func(\$buffer, $out_file, Append => $append), ' Compressed ok' ;
294 ok -e $out_file, " Created output file";
295 my $got = anyUncompress($out_file, $already);
296 $got = undef if ! defined $buffer && $got eq '' ;
297 is $got, $buffer, " Uncompressed matches original";
301 title "$TopType - From Buff to Handle content '$disp_content' Append $append" ;
303 my $lex = new LexFile($out_file) ;
305 ok ! -e $out_file, " Output file does not exist";
306 writeFile($out_file, $already);
307 my $of = new IO::File ">>$out_file" ;
308 ok $of, " Created output filehandle" ;
310 ok &$Func(\$buffer, $of, AutoClose => 1, Append => $append), ' Compressed ok' ;
312 ok -e $out_file, " Created output file";
313 my $got = anyUncompress($out_file, $already);
314 $got = undef if ! defined $buffer && $got eq '' ;
315 is $got, $buffer, " Uncompressed matches original";
320 title "$TopType - From Filename to Filename content '$disp_content' Append $append" ;
322 my $lex = new LexFile($in_file, $out_file) ;
323 writeFile($in_file, $buffer);
325 ok ! -e $out_file, " Output file does not exist";
326 writeFile($out_file, $already);
328 ok &$Func($in_file => $out_file, Append => $append), ' Compressed ok' ;
330 ok -e $out_file, " Created output file";
331 my $got = anyUncompress($out_file, $already);
332 $got = undef if ! defined $buffer && $got eq '' ;
333 is $got, $buffer, " Uncompressed matches original";
338 title "$TopType - From Filename to Handle content '$disp_content' Append $append" ;
340 my $lex = new LexFile($in_file, $out_file) ;
341 writeFile($in_file, $buffer);
343 ok ! -e $out_file, " Output file does not exist";
344 writeFile($out_file, $already);
345 my $out = new IO::File ">>$out_file" ;
347 ok &$Func($in_file, $out, AutoClose => 1, Append => $append), ' Compressed ok' ;
349 ok -e $out_file, " Created output file";
350 my $got = anyUncompress($out_file, $already);
351 $got = undef if ! defined $buffer && $got eq '' ;
352 is $got, $buffer, " Uncompressed matches original";
357 title "$TopType - From Filename to Buffer content '$disp_content' Append $append" ;
359 my $lex = new LexFile($in_file, $out_file) ;
360 writeFile($in_file, $buffer);
364 ok &$Func($in_file => \$out, Append => $append), ' Compressed ok' ;
366 my $got = anyUncompress(\$out, $already);
367 $got = undef if ! defined $buffer && $got eq '' ;
368 is $got, $buffer, " Uncompressed matches original";
373 title "$TopType - From Handle to Filename content '$disp_content' Append $append" ;
375 my $lex = new LexFile($in_file, $out_file) ;
376 writeFile($in_file, $buffer);
377 my $in = new IO::File "<$in_file" ;
379 ok ! -e $out_file, " Output file does not exist";
380 writeFile($out_file, $already);
382 ok &$Func($in, $out_file, Append => $append), ' Compressed ok'
383 or diag "error is $GzipError" ;
385 ok -e $out_file, " Created output file";
386 my $got = anyUncompress($out_file, $already);
387 $got = undef if ! defined $buffer && $got eq '' ;
388 is $buffer, $got, " Uncompressed matches original";
393 title "$TopType - From Handle to Handle content '$disp_content' Append $append" ;
395 my $lex = new LexFile($in_file, $out_file) ;
396 writeFile($in_file, $buffer);
397 my $in = new IO::File "<$in_file" ;
399 ok ! -e $out_file, " Output file does not exist";
400 writeFile($out_file, $already);
401 my $out = new IO::File ">>$out_file" ;
403 ok &$Func($in, $out, AutoClose => 1, Append => $append), ' Compressed ok' ;
405 ok -e $out_file, " Created output file";
406 my $got = anyUncompress($out_file, $already);
407 $got = undef if ! defined $buffer && $got eq '' ;
408 is $buffer, $got, " Uncompressed matches original";
413 title "$TopType - From Handle to Buffer content '$disp_content' Append $append" ;
415 my $lex = new LexFile($in_file, $out_file) ;
416 writeFile($in_file, $buffer);
417 my $in = new IO::File "<$in_file" ;
421 ok &$Func($in, \$out, Append => $append), ' Compressed ok' ;
423 my $got = anyUncompress(\$out, $already);
424 $got = undef if ! defined $buffer && $got eq '' ;
425 is $buffer, $got, " Uncompressed matches original";
430 title "$TopType - From stdin (via '-') to Buffer content '$disp_content' Append $append" ;
432 my $lex = new LexFile($in_file, $out_file) ;
433 writeFile($in_file, $buffer);
435 open(SAVEIN, "<&STDIN");
436 my $dummy = fileno SAVEIN ;
437 ok open(STDIN, "<$in_file"), " redirect STDIN";
441 ok &$Func('-', \$out, Append => $append), ' Compressed ok'
444 open(STDIN, "<&SAVEIN");
446 my $got = anyUncompress(\$out, $already);
447 $got = undef if ! defined $buffer && $got eq '' ;
448 is $buffer, $got, " Uncompressed matches original";
456 foreach my $bit ('IO::Compress::Gzip',
457 'IO::Compress::Deflate',
458 'IO::Compress::RawDeflate',
461 my $Error = getErrorRef($bit);
462 my $Func = getTopFuncRef($bit);
463 my $TopType = getTopFuncName($bit);
465 my $TopTypeInverse = getInverse($bit);
466 my $FuncInverse = getTopFuncRef($TopTypeInverse);
468 my ($file1, $file2) = ("file1", "file2");
469 my $lex = new LexFile($file1, $file2) ;
471 writeFile($file1, "data1");
472 writeFile($file2, "data2");
473 my $of = new IO::File "<$file1" ;
474 ok $of, " Created output filehandle" ;
476 my @input = ( undef, "", $file2, \undef, \'', \"abcde", $of) ;
477 my @expected = ("", "", $file2, "", "", "abcde", "data1");
478 my @uexpected = ("", "", "data2", "", "", "abcde", "data1");
483 title "$TopType - From Array Ref to Array Ref" ;
485 my @output = ('first') ;
486 ok &$Func(\@input, \@output, AutoClose => 0), ' Compressed ok' ;
488 is $output[0], 'first', " Array[0] unchanged";
490 is_deeply \@input, \@keep, " Input array not changed" ;
491 my @got = shift @output;
492 foreach (@output) { push @got, anyUncompress($_) }
494 is_deeply \@got, ['first', @expected], " Got Expected uncompressed data";
499 title "$TopType - From Array Ref to Buffer" ;
501 # rewind the filehandle
502 $of->open("<$file1") ;
505 ok &$Func(\@input, \$output, AutoClose => 0), ' Compressed ok' ;
507 my $got = anyUncompress(\$output);
509 is $got, join('', @expected), " Got Expected uncompressed data";
513 title "$TopType - From Array Ref to Filename" ;
515 my ($file3) = ("file3");
516 my $lex = new LexFile($file3) ;
518 # rewind the filehandle
519 $of->open("<$file1") ;
522 ok &$Func(\@input, $file3, AutoClose => 0), ' Compressed ok' ;
524 my $got = anyUncompress($file3);
526 is $got, join('', @expected), " Got Expected uncompressed data";
530 title "$TopType - From Array Ref to Filehandle" ;
532 my ($file3) = ("file3");
533 my $lex = new LexFile($file3) ;
535 my $fh3 = new IO::File ">$file3";
537 # rewind the filehandle
538 $of->open("<$file1") ;
541 ok &$Func(\@input, $fh3, AutoClose => 0), ' Compressed ok' ;
545 my $got = anyUncompress($file3);
547 is $got, join('', @expected), " Got Expected uncompressed data";
551 foreach my $bit ('IO::Compress::Gzip',
552 'IO::Compress::Deflate',
553 'IO::Compress::RawDeflate',
556 my $Error = getErrorRef($bit);
557 my $Func = getTopFuncRef($bit);
558 my $TopType = getTopFuncName($bit);
560 my $TopTypeInverse = getInverse($bit);
561 my $FuncInverse = getTopFuncRef($TopTypeInverse);
563 my @inFiles = map { "in$_.tmp" } 1..4;
564 my @outFiles = map { "out$_.tmp" } 1..4;
565 my $lex = new LexFile(@inFiles, @outFiles);
567 writeFile($_, "data $_") foreach @inFiles ;
570 title "$TopType - Hash Ref: to filename" ;
573 ok &$Func( { $inFiles[0] => $outFiles[0],
574 $inFiles[1] => $outFiles[1],
575 $inFiles[2] => $outFiles[2] } ), ' Compressed ok' ;
579 my $got = anyUncompress($outFiles[$_]);
580 is $got, "data $inFiles[$_]", " Uncompressed $_ matches original";
585 title "$TopType - Hash Ref: to buffer" ;
588 ok &$Func( { $inFiles[0] => \$buffer[0],
589 $inFiles[1] => \$buffer[1],
590 $inFiles[2] => \$buffer[2] } ), ' Compressed ok' ;
594 my $got = anyUncompress(\$buffer[$_]);
595 is $got, "data $inFiles[$_]", " Uncompressed $_ matches original";
600 title "$TopType - Hash Ref: to undef" ;
603 my %hash = ( $inFiles[0] => undef,
604 $inFiles[1] => undef,
605 $inFiles[2] => undef,
608 ok &$Func( \%hash ), ' Compressed ok' ;
612 my $got = anyUncompress(\$hash{$_});
613 is $got, "data $_", " Uncompressed $_ matches original";
618 title "$TopType - Filename to Hash Ref" ;
621 ok &$Func( $inFiles[0] => \%output), ' Compressed ok' ;
623 is keys %output, 1, " one pair in hash" ;
624 my ($k, $v) = each %output;
625 is $k, $inFiles[0], " key is '$inFiles[0]'";
626 my $got = anyUncompress($v);
627 is $got, "data $inFiles[0]", " Uncompressed matches original";
631 title "$TopType - File Glob to Hash Ref" ;
634 ok &$Func( '<in*.tmp>' => \%output), ' Compressed ok' ;
636 is keys %output, 4, " four pairs in hash" ;
637 foreach my $fil (@inFiles)
639 ok exists $output{$fil}, " key '$fil' exists" ;
640 my $got = anyUncompress($output{$fil});
641 is $got, "data $fil", " Uncompressed matches original";
648 # title "$TopType - Hash Ref to Array Ref" ;
650 # my @output = ('first') ;
651 # ok &$Func( { \@input, \@output } , AutoClose => 0), ' Compressed ok' ;
653 # is $output[0], 'first', " Array[0] unchanged";
655 # is_deeply \@input, \@keep, " Input array not changed" ;
656 # my @got = shift @output;
657 # foreach (@output) { push @got, anyUncompress($_) }
659 # is_deeply \@got, ['first', @expected], " Got Expected uncompressed data";
665 # title "$TopType - From Array Ref to Buffer" ;
667 # # rewind the filehandle
668 # $of->open("<$file1") ;
671 # ok &$Func(\@input, \$output, AutoClose => 0), ' Compressed ok' ;
673 # my $got = anyUncompress(\$output);
675 # is $got, join('', @expected), " Got Expected uncompressed data";
680 # title "$TopType - From Array Ref to Filename" ;
682 # my ($file3) = ("file3");
683 # my $lex = new LexFile($file3) ;
685 # # rewind the filehandle
686 # $of->open("<$file1") ;
689 # ok &$Func(\@input, $file3, AutoClose => 0), ' Compressed ok' ;
691 # my $got = anyUncompress($file3);
693 # is $got, join('', @expected), " Got Expected uncompressed data";
698 # title "$TopType - From Array Ref to Filehandle" ;
700 # my ($file3) = ("file3");
701 # my $lex = new LexFile($file3) ;
703 # my $fh3 = new IO::File ">$file3";
705 # # rewind the filehandle
706 # $of->open("<$file1") ;
709 # ok &$Func(\@input, $fh3, AutoClose => 0), ' Compressed ok' ;
713 # my $got = anyUncompress($file3);
715 # is $got, join('', @expected), " Got Expected uncompressed data";
719 foreach my $bit ('IO::Compress::Gzip',
720 'IO::Compress::Deflate',
721 'IO::Compress::RawDeflate',
724 my $Error = getErrorRef($bit);
725 my $Func = getTopFuncRef($bit);
726 my $TopType = getTopFuncName($bit);
728 for my $files ( [qw(a1)], [qw(a1 a2 a3)] )
731 my $tmpDir1 = 'tmpdir1';
732 my $tmpDir2 = 'tmpdir2';
733 my $lex = new LexDir($tmpDir1, $tmpDir2) ;
735 mkdir $tmpDir1, 0777;
736 mkdir $tmpDir2, 0777;
738 ok -d $tmpDir1, " Temp Directory $tmpDir1 exists";
739 #ok ! -d $tmpDir2, " Temp Directory $tmpDir2 does not exist";
741 my @files = map { "$tmpDir1/$_.tmp" } @$files ;
742 foreach (@files) { writeFile($_, "abc $_") }
744 my @expected = map { "abc $_" } @files ;
745 my @outFiles = map { s/$tmpDir1/$tmpDir2/; $_ } @files ;
748 title "$TopType - From FileGlob to FileGlob files [@$files]" ;
750 ok &$Func("<$tmpDir1/a*.tmp>" => "<$tmpDir2/a#1.tmp>"), ' Compressed ok'
753 my @copy = @expected;
754 for my $file (@outFiles)
756 is anyUncompress($file), shift @copy, " got expected from $file" ;
759 is @copy, 0, " got all files";
763 title "$TopType - From FileGlob to Array files [@$files]" ;
765 my @buffer = ('first') ;
766 ok &$Func("<$tmpDir1/a*.tmp>" => \@buffer), ' Compressed ok'
769 is shift @buffer, 'first';
771 my @copy = @expected;
772 for my $buffer (@buffer)
774 is anyUncompress($buffer), shift @copy, " got expected " ;
777 is @copy, 0, " got all files";
781 title "$TopType - From FileGlob to Buffer files [@$files]" ;
784 ok &$Func("<$tmpDir1/a*.tmp>" => \$buffer), ' Compressed ok'
789 my $got = anyUncompress([ \$buffer, MultiStream => 1 ]);
791 is $got, join("", @expected), " got expected" ;
795 title "$TopType - From FileGlob to Filename files [@$files]" ;
797 my $filename = "abcde";
798 my $lex = new LexFile($filename) ;
800 ok &$Func("<$tmpDir1/a*.tmp>" => $filename), ' Compressed ok'
805 my $got = anyUncompress([$filename, MultiStream => 1]);
807 is $got, join("", @expected), " got expected" ;
811 title "$TopType - From FileGlob to Filehandle files [@$files]" ;
813 my $filename = "abcde";
814 my $lex = new LexFile($filename) ;
815 my $fh = new IO::File ">$filename";
817 ok &$Func("<$tmpDir1/a*.tmp>" => $fh, AutoClose => 1), ' Compressed ok'
822 my $got = anyUncompress([$filename, MultiStream => 1]);
824 is $got, join("", @expected), " got expected" ;
830 foreach my $bit ('IO::Uncompress::Gunzip',
831 'IO::Uncompress::Inflate',
832 'IO::Uncompress::RawInflate',
833 'IO::Uncompress::AnyInflate',
836 my $Error = getErrorRef($bit);
837 my $Func = getTopFuncRef($bit);
838 my $TopType = getTopFuncName($bit);
840 my $buffer = "abcde" ;
841 my $buffer2 = "ABCDE" ;
842 my $keep_orig = $buffer;
844 my $comp = compressBuffer($TopType, $buffer) ;
845 my $comp2 = compressBuffer($TopType, $buffer2) ;
846 my $keep_comp = $comp;
848 my $incumbent = "incumbent data" ;
850 for my $append (0, 1)
852 my $expected = $buffer ;
853 $expected = $incumbent . $buffer if $append ;
856 title "$TopType - From Buff to Buff, Append($append)" ;
859 $output = $incumbent if $append ;
860 ok &$Func(\$comp, \$output, Append => $append), ' Uncompressed ok' ;
862 is $keep_comp, $comp, " Input buffer not changed" ;
863 is $output, $expected, " Uncompressed matches original";
867 title "$TopType - From Buff to Array, Append($append)" ;
869 my @output = ('first');
870 #$output = $incumbent if $append ;
871 ok &$Func(\$comp, \@output, Append => $append), ' Uncompressed ok' ;
873 is $keep_comp, $comp, " Input buffer not changed" ;
874 is $output[0], 'first', " Uncompressed matches original";
875 is ${ $output[1] }, $buffer, " Uncompressed matches original"
877 is @output, 2, " only 2 elements in the array" ;
881 title "$TopType - From Buff to Filename, Append($append)" ;
883 my $out_file = "abcde";
884 my $lex = new LexFile($out_file) ;
886 { writeFile($out_file, $incumbent) }
888 { ok ! -e $out_file, " Output file does not exist" }
890 ok &$Func(\$comp, $out_file, Append => $append), ' Uncompressed ok' ;
892 ok -e $out_file, " Created output file";
893 my $content = readFile($out_file) ;
895 is $keep_comp, $comp, " Input buffer not changed" ;
896 is $content, $expected, " Uncompressed matches original";
900 title "$TopType - From Buff to Handle, Append($append)" ;
902 my $out_file = "abcde";
903 my $lex = new LexFile($out_file) ;
906 writeFile($out_file, $incumbent) ;
907 $of = new IO::File "+< $out_file" ;
910 ok ! -e $out_file, " Output file does not exist" ;
911 $of = new IO::File "> $out_file" ;
913 isa_ok $of, 'IO::File', ' $of' ;
915 ok &$Func(\$comp, $of, Append => $append, AutoClose => 1), ' Uncompressed ok' ;
917 ok -e $out_file, " Created output file";
918 my $content = readFile($out_file) ;
920 is $keep_comp, $comp, " Input buffer not changed" ;
921 is $content, $expected, " Uncompressed matches original";
925 title "$TopType - From Filename to Filename, Append($append)" ;
927 my $out_file = "abcde.out";
928 my $in_file = "abcde.in";
929 my $lex = new LexFile($in_file, $out_file) ;
931 { writeFile($out_file, $incumbent) }
933 { ok ! -e $out_file, " Output file does not exist" }
935 writeFile($in_file, $comp);
937 ok &$Func($in_file, $out_file, Append => $append), ' Uncompressed ok' ;
939 ok -e $out_file, " Created output file";
940 my $content = readFile($out_file) ;
942 is $keep_comp, $comp, " Input buffer not changed" ;
943 is $content, $expected, " Uncompressed matches original";
947 title "$TopType - From Filename to Handle, Append($append)" ;
949 my $out_file = "abcde.out";
950 my $in_file = "abcde.in";
951 my $lex = new LexFile($in_file, $out_file) ;
954 writeFile($out_file, $incumbent) ;
955 $out = new IO::File "+< $out_file" ;
958 ok ! -e $out_file, " Output file does not exist" ;
959 $out = new IO::File "> $out_file" ;
961 isa_ok $out, 'IO::File', ' $out' ;
963 writeFile($in_file, $comp);
965 ok &$Func($in_file, $out, Append => $append, AutoClose => 1), ' Uncompressed ok' ;
967 ok -e $out_file, " Created output file";
968 my $content = readFile($out_file) ;
970 is $keep_comp, $comp, " Input buffer not changed" ;
971 is $content, $expected, " Uncompressed matches original";
975 title "$TopType - From Filename to Buffer, Append($append)" ;
977 my $in_file = "abcde.in";
978 my $lex = new LexFile($in_file) ;
979 writeFile($in_file, $comp);
982 $output = $incumbent if $append ;
984 ok &$Func($in_file, \$output, Append => $append), ' Uncompressed ok' ;
986 is $keep_comp, $comp, " Input buffer not changed" ;
987 is $output, $expected, " Uncompressed matches original";
991 title "$TopType - From Handle to Filename, Append($append)" ;
993 my $out_file = "abcde.out";
994 my $in_file = "abcde.in";
995 my $lex = new LexFile($in_file, $out_file) ;
997 { writeFile($out_file, $incumbent) }
999 { ok ! -e $out_file, " Output file does not exist" }
1001 writeFile($in_file, $comp);
1002 my $in = new IO::File "<$in_file" ;
1004 ok &$Func($in, $out_file, Append => $append), ' Uncompressed ok' ;
1006 ok -e $out_file, " Created output file";
1007 my $content = readFile($out_file) ;
1009 is $keep_comp, $comp, " Input buffer not changed" ;
1010 is $content, $expected, " Uncompressed matches original";
1014 title "$TopType - From Handle to Handle, Append($append)" ;
1016 my $out_file = "abcde.out";
1017 my $in_file = "abcde.in";
1018 my $lex = new LexFile($in_file, $out_file) ;
1021 writeFile($out_file, $incumbent) ;
1022 $out = new IO::File "+< $out_file" ;
1025 ok ! -e $out_file, " Output file does not exist" ;
1026 $out = new IO::File "> $out_file" ;
1028 isa_ok $out, 'IO::File', ' $out' ;
1030 writeFile($in_file, $comp);
1031 my $in = new IO::File "<$in_file" ;
1033 ok &$Func($in, $out, Append => $append, AutoClose => 1), ' Uncompressed ok' ;
1035 ok -e $out_file, " Created output file";
1036 my $content = readFile($out_file) ;
1038 is $keep_comp, $comp, " Input buffer not changed" ;
1039 is $content, $expected, " Uncompressed matches original";
1043 title "$TopType - From Filename to Buffer, Append($append)" ;
1045 my $in_file = "abcde.in";
1046 my $lex = new LexFile($in_file) ;
1047 writeFile($in_file, $comp);
1048 my $in = new IO::File "<$in_file" ;
1051 $output = $incumbent if $append ;
1053 ok &$Func($in, \$output, Append => $append), ' Uncompressed ok' ;
1055 is $keep_comp, $comp, " Input buffer not changed" ;
1056 is $output, $expected, " Uncompressed matches original";
1060 title "$TopType - From stdin (via '-') to Buffer content, Append($append) " ;
1062 my $in_file = "abcde.in";
1063 my $lex = new LexFile($in_file) ;
1064 writeFile($in_file, $comp);
1066 open(SAVEIN, "<&STDIN");
1067 my $dummy = fileno SAVEIN ;
1068 ok open(STDIN, "<$in_file"), " redirect STDIN";
1071 $output = $incumbent if $append ;
1073 ok &$Func('-', \$output, Append => $append), ' Uncompressed ok'
1076 open(STDIN, "<&SAVEIN");
1078 is $keep_comp, $comp, " Input buffer not changed" ;
1079 is $output, $expected, " Uncompressed matches original";
1084 title "$TopType - From Handle to Buffer, InputLength" ;
1086 my $out_file = "abcde.out";
1087 my $in_file = "abcde.in";
1088 my $lex = new LexFile($in_file, $out_file) ;
1091 my $expected = $buffer ;
1092 my $appended = 'appended';
1093 my $len_appended = length $appended;
1094 writeFile($in_file, $comp . $appended . $comp . $appended) ;
1095 my $in = new IO::File "<$in_file" ;
1097 ok &$Func($in, \$out, Transparent => 0, InputLength => length $comp), ' Uncompressed ok' ;
1099 is $out, $expected, " Uncompressed matches original";
1102 is $in->read($buff, $len_appended), $len_appended, " Length of Appended data ok";
1103 is $buff, $appended, " Appended data ok";
1106 ok &$Func($in, \$out, Transparent => 0, InputLength => length $comp), ' Uncompressed ok' ;
1108 is $out, $expected, " Uncompressed matches original";
1111 is $in->read($buff, $len_appended), $len_appended, " Length of Appended data ok";
1112 is $buff, $appended, " Appended data ok";
1115 for my $stdin ('-', *STDIN) # , \*STDIN)
1117 title "$TopType - From stdin (via $stdin) to Buffer content, InputLength" ;
1119 my $lex = new LexFile my $in_file ;
1120 my $expected = $buffer ;
1121 my $appended = 'appended';
1122 my $len_appended = length $appended;
1123 writeFile($in_file, $comp . $appended ) ;
1125 open(SAVEIN, "<&STDIN");
1126 my $dummy = fileno SAVEIN ;
1127 ok open(STDIN, "<$in_file"), " redirect STDIN";
1131 ok &$Func($stdin, \$output, Transparent => 0, InputLength => length $comp), ' Uncompressed ok'
1135 is read(STDIN, $buff, $len_appended), $len_appended, " Length of Appended data ok";
1137 is $output, $expected, " Uncompressed matches original";
1138 is $buff, $appended, " Appended data ok";
1140 open(STDIN, "<&SAVEIN");
1144 foreach my $bit ('IO::Uncompress::Gunzip',
1145 'IO::Uncompress::Inflate',
1146 'IO::Uncompress::RawInflate',
1147 'IO::Uncompress::AnyInflate',
1150 # TODO -- Add Append mode tests
1152 my $Error = getErrorRef($bit);
1153 my $Func = getTopFuncRef($bit);
1154 my $TopType = getTopFuncName($bit);
1156 my $buffer = "abcde" ;
1157 my $keep_orig = $buffer;
1160 my $null = compressBuffer($TopType, "") ;
1161 my $undef = compressBuffer($TopType, undef) ;
1162 my $comp = compressBuffer($TopType, $buffer) ;
1163 my $keep_comp = $comp;
1165 my $incumbent = "incumbent data" ;
1167 #my ($file1, $file2) = ("file1", "file2");
1168 my $lex = new LexFile(my $file1, my $file2) ;
1170 writeFile($file1, compressBuffer($TopType,"data1"));
1171 writeFile($file2, compressBuffer($TopType,"data2"));
1173 my $of = new IO::File "<$file1" ;
1174 ok $of, " Created output filehandle" ;
1176 my @input = ($file2, \$undef, \$null, \$comp, $of) ;
1177 my @expected = ('data2', '', '', 'abcde', 'data1');
1182 title "$TopType - From ArrayRef to Buffer" ;
1185 ok &$Func(\@input, \$output, AutoClose => 0), ' UnCompressed ok' ;
1187 is $output, join('', @expected)
1191 title "$TopType - From ArrayRef to Filename" ;
1194 my $lex = new LexFile $output;
1195 $of->open("<$file1") ;
1197 ok &$Func(\@input, $output, AutoClose => 0), ' UnCompressed ok' ;
1199 is readFile($output), join('', @expected)
1203 title "$TopType - From ArrayRef to Filehandle" ;
1206 my $lex = new LexFile $output;
1207 my $fh = new IO::File ">$output" ;
1208 $of->open("<$file1") ;
1210 ok &$Func(\@input, $fh, AutoClose => 0), ' UnCompressed ok' ;
1213 is readFile($output), join('', @expected)
1217 title "$TopType - From Array Ref to Array Ref" ;
1219 my @output = (\'first') ;
1220 $of->open("<$file1") ;
1221 ok &$Func(\@input, \@output, AutoClose => 0), ' UnCompressed ok' ;
1223 is_deeply \@input, \@keep, " Input array not changed" ;
1224 is_deeply [map { defined $$_ ? $$_ : "" } @output],
1225 ['first', @expected],
1226 " Got Expected uncompressed data";
1231 foreach my $bit ('IO::Uncompress::Gunzip',
1232 'IO::Uncompress::Inflate',
1233 'IO::Uncompress::RawInflate',
1234 'IO::Uncompress::AnyInflate',
1237 # TODO -- Add Append mode tests
1239 my $Error = getErrorRef($bit);
1240 my $Func = getTopFuncRef($bit);
1241 my $TopType = getTopFuncName($bit);
1243 my $tmpDir1 = 'tmpdir1';
1244 my $tmpDir2 = 'tmpdir2';
1245 my $lex = new LexDir($tmpDir1, $tmpDir2) ;
1247 mkdir $tmpDir1, 0777;
1248 mkdir $tmpDir2, 0777;
1250 ok -d $tmpDir1, " Temp Directory $tmpDir1 exists";
1251 #ok ! -d $tmpDir2, " Temp Directory $tmpDir2 does not exist";
1253 my @files = map { "$tmpDir1/$_.tmp" } qw( a1 a2 a3) ;
1254 foreach (@files) { writeFile($_, compressBuffer($TopType, "abc $_")) }
1256 my @expected = map { "abc $_" } @files ;
1257 my @outFiles = map { s/$tmpDir1/$tmpDir2/; $_ } @files ;
1260 title "$TopType - From FileGlob to FileGlob" ;
1262 ok &$Func("<$tmpDir1/a*.tmp>" => "<$tmpDir2/a#1.tmp>"), ' UnCompressed ok'
1265 my @copy = @expected;
1266 for my $file (@outFiles)
1268 is readFile($file), shift @copy, " got expected from $file" ;
1271 is @copy, 0, " got all files";
1275 title "$TopType - From FileGlob to Arrayref" ;
1277 my @output = (\'first');
1278 ok &$Func("<$tmpDir1/a*.tmp>" => \@output), ' UnCompressed ok'
1281 my @copy = ('first', @expected);
1282 for my $data (@output)
1284 is $$data, shift @copy, " got expected data" ;
1287 is @copy, 0, " got all files";
1291 title "$TopType - From FileGlob to Buffer" ;
1294 ok &$Func("<$tmpDir1/a*.tmp>" => \$output), ' UnCompressed ok'
1297 is $output, join('', @expected), " got expected uncompressed data";
1301 title "$TopType - From FileGlob to Filename" ;
1303 my $output = 'abc' ;
1304 my $lex = new LexFile $output ;
1305 ok ! -e $output, " $output does not exist" ;
1306 ok &$Func("<$tmpDir1/a*.tmp>" => $output), ' UnCompressed ok'
1309 ok -e $output, " $output does exist" ;
1310 is readFile($output), join('', @expected), " got expected uncompressed data";
1314 title "$TopType - From FileGlob to Filehandle" ;
1316 my $output = 'abc' ;
1317 my $lex = new LexFile $output ;
1318 my $fh = new IO::File ">$output" ;
1319 ok &$Func("<$tmpDir1/a*.tmp>" => $fh, AutoClose => 1), ' UnCompressed ok'
1322 ok -e $output, " $output does exist" ;
1323 is readFile($output), join('', @expected), " got expected uncompressed data";
1328 foreach my $TopType ('IO::Compress::Gzip::gzip',
1329 'IO::Compress::Deflate',
1330 'IO::Compress::RawDeflate',
1331 # TODO -- add the inflate classes
1334 my $Error = getErrorRef($TopType);
1335 my $Func = getTopFuncRef($TopType);
1336 my $Name = getTopFuncName($TopType);
1338 title "More write tests" ;
1340 my $lex = new LexFile(my $file1, my $file2, my $file3) ;
1342 writeFile($file1, "F1");
1343 writeFile($file2, "F2");
1344 writeFile($file3, "F3");
1350 [ '[\"abcd"]', "abcd" ],
1351 [ '[\"ab", \"cd"]', "abcd" ],
1354 [ '[\"a", $fh1, \"bc"]', "aF1bc"],
1358 foreach my $data (@data)
1360 my ($send, $get) = @$data ;
1362 my $fh1 = new IO::File "< $file1" ;
1363 my $fh2 = new IO::File "< $file2" ;
1364 my $fh3 = new IO::File "< $file3" ;
1368 eval "\$copy = $send";
1370 ok &$Func($copy, \$Answer), " $Name ok";
1372 my $got = anyUncompress(\$Answer);
1373 is $got, $get, " got expected output" ;
1374 ok ! $$Error, " no error"
1375 or diag "Error is $$Error";
1379 title "Array Input Error tests" ;
1384 '[[\"ab"], [\"cd"]]',
1388 foreach my $send (@data)
1390 my $fh1 = new IO::File "< $file1" ;
1391 my $fh2 = new IO::File "< $file2" ;
1392 my $fh3 = new IO::File "< $file3" ;
1396 eval "\$copy = $send";
1398 ok ! &$Func($copy, \$Answer), " $Name fails";
1400 is $$Error, "unknown input parameter", " got error message";
1408 my $content = shift ;
1414 ok IO::Compress::Gzip::gzip($in, \$out, %opts), " gzip ok" ;
1415 ok IO::Uncompress::Gunzip::gunzip(\$out, \$got), " gunzip ok"
1416 or diag $GunzipError ;
1417 is $got, $content, " got expected content" ;
1419 my $gunz = new IO::Uncompress::Gunzip \$out, Strict => 0
1420 or diag "GunzipError is $IO::Uncompress::Gunzip::GunzipError" ;
1421 ok $gunz, " Created IO::Uncompress::Gunzip object";
1422 my $hdr = $gunz->getHeaderInfo();
1423 ok $hdr, " got Header info";
1425 ok $gunz->read($uncomp), " read ok" ;
1426 is $uncomp, $content, " got expected content";
1427 ok $gunz->close, " closed ok" ;
1434 title "Check gzip header default NAME & MTIME settings" ;
1436 my $lex = new LexFile my $file1;
1438 my $content = "hello ";
1442 writeFile($file1, $content);
1443 $mtime = (stat($file1))[8];
1444 # make sure that the gzip file isn't created in the same
1445 # second as the input file
1447 $hdr = gzipGetHeader($file1, $content);
1449 is $hdr->{Name}, $file1, " Name is '$file1'";
1450 is $hdr->{Time}, $mtime, " Time is ok";
1452 title "Override Name" ;
1454 writeFile($file1, $content);
1455 $mtime = (stat($file1))[8];
1457 $hdr = gzipGetHeader($file1, $content, Name => "abcde");
1459 is $hdr->{Name}, "abcde", " Name is 'abcde'" ;
1460 is $hdr->{Time}, $mtime, " Time is ok";
1462 title "Override Time" ;
1464 writeFile($file1, $content);
1465 $hdr = gzipGetHeader($file1, $content, Time => 1234);
1467 is $hdr->{Name}, $file1, " Name is '$file1'" ;
1468 is $hdr->{Time}, 1234, " Time is 1234";
1470 title "Override Name and Time" ;
1472 writeFile($file1, $content);
1473 $hdr = gzipGetHeader($file1, $content, Time => 4321, Name => "abcde");
1475 is $hdr->{Name}, "abcde", " Name is 'abcde'" ;
1476 is $hdr->{Time}, 4321, " Time is 4321";
1478 title "Filehandle doesn't have default Name or Time" ;
1479 my $fh = new IO::File "< $file1"
1480 or diag "Cannot open '$file1': $!\n" ;
1483 $hdr = gzipGetHeader($fh, $content);
1486 ok ! defined $hdr->{Name}, " Name is undef";
1487 cmp_ok $hdr->{Time}, '>=', $before, " Time is ok";
1488 cmp_ok $hdr->{Time}, '<=', $after, " Time is ok";
1492 title "Buffer doesn't have default Name or Time" ;
1493 my $buffer = $content;
1495 $hdr = gzipGetHeader(\$buffer, $content);
1498 ok ! defined $hdr->{Name}, " Name is undef";
1499 cmp_ok $hdr->{Time}, '>=', $before, " Time is ok";
1500 cmp_ok $hdr->{Time}, '<=', $after, " Time is ok";
1503 # TODO add more error cases