1b39c5158Smillert 2b39c5158Smillertuse strict; 3b39c5158Smillertuse warnings; 4b39c5158Smillertuse bytes; 5b39c5158Smillert 6b39c5158Smillertuse Test::More ; 7b39c5158Smillert 8b39c5158Smillertuse IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END); 9898184e3Ssthenuse CompTestUtils; 10b39c5158Smillert 11b39c5158Smillertour ($UncompressClass); 12b39c5158SmillertBEGIN 13b39c5158Smillert{ 14b39c5158Smillert # use Test::NoWarnings, if available 15b39c5158Smillert my $extra = 0 ; 16b39c5158Smillert 17b39c5158Smillert my $st = eval { require Test::NoWarnings ; import Test::NoWarnings; 1; }; 18b39c5158Smillert $extra = 1 19b39c5158Smillert if $st ; 20b39c5158Smillert 21f3efcd01Safresh1 plan(tests => 799 + $extra) ; 22b39c5158Smillert} 23b39c5158Smillert 24b39c5158Smillertsub myGZreadFile 25b39c5158Smillert{ 26b39c5158Smillert my $filename = shift ; 27b39c5158Smillert my $init = shift ; 28b39c5158Smillert 29b39c5158Smillert 30*256a93a4Safresh1 my $fil = $UncompressClass->can('new')->( $UncompressClass, $filename, 31b39c5158Smillert -Strict => 0, 32b39c5158Smillert -Append => 1 33*256a93a4Safresh1 ); 34b39c5158Smillert 35b39c5158Smillert my $data = ''; 36b39c5158Smillert $data = $init if defined $init ; 37b39c5158Smillert 1 while $fil->read($data) > 0; 38b39c5158Smillert 39b39c5158Smillert $fil->close ; 40b39c5158Smillert return $data ; 41b39c5158Smillert} 42b39c5158Smillert 43b39c5158Smillertsub run 44b39c5158Smillert{ 45b39c5158Smillert my $CompressClass = identify(); 46b39c5158Smillert $UncompressClass = getInverse($CompressClass); 47b39c5158Smillert my $Error = getErrorRef($CompressClass); 48b39c5158Smillert my $UnError = getErrorRef($UncompressClass); 49b39c5158Smillert 50b39c5158Smillert if(1) 51b39c5158Smillert { 52b39c5158Smillert 53b39c5158Smillert title "Testing $CompressClass Errors"; 54b39c5158Smillert 55b39c5158Smillert # Buffer not writable 56*256a93a4Safresh1 eval qq[\$a = $CompressClass->new(\\1) ;] ; 57b39c5158Smillert like $@, mkEvalErr("^$CompressClass: output buffer is read-only") ; 58b39c5158Smillert 59b39c5158Smillert my($out, $gz); 60b39c5158Smillert 61b39c5158Smillert my $x ; 62*256a93a4Safresh1 $gz = $CompressClass->can('new')->($CompressClass, \$x); 63b39c5158Smillert 64b39c5158Smillert foreach my $name (qw(read readline getc)) 65b39c5158Smillert { 66b39c5158Smillert eval " \$gz->$name() " ; 67b39c5158Smillert like $@, mkEvalErr("^$name Not Available: File opened only for output"); 68b39c5158Smillert } 69b39c5158Smillert 70b39c5158Smillert eval ' $gz->write({})' ; 71b39c5158Smillert like $@, mkEvalErr("^${CompressClass}::write: not a scalar reference"); 72b39c5158Smillert 73b39c5158Smillert eval ' $gz->syswrite("abc", 1, 5)' ; 74b39c5158Smillert like $@, mkEvalErr("^${CompressClass}::write: offset outside string"); 75b39c5158Smillert 76b39c5158Smillert eval ' $gz->syswrite("abc", 1, -4)' ; 77b39c5158Smillert like $@, mkEvalErr("^${CompressClass}::write: offset outside string"), "write outside string"; 78b39c5158Smillert } 79b39c5158Smillert 80b39c5158Smillert 81b39c5158Smillert { 82b39c5158Smillert title "Testing $UncompressClass Errors"; 83b39c5158Smillert 84b39c5158Smillert my $out = "" ; 85b39c5158Smillert 86*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 87b39c5158Smillert 88b39c5158Smillert ok ! -e $name, " $name does not exist"; 89b39c5158Smillert 90*256a93a4Safresh1 $a = $UncompressClass->can('new')->( $UncompressClass, "$name" ); 91b39c5158Smillert is $a, undef; 92b39c5158Smillert 93b39c5158Smillert my $gc ; 94*256a93a4Safresh1 my $guz = $CompressClass->can('new')->( $CompressClass, \$gc); 95b39c5158Smillert $guz->write("abc") ; 96b39c5158Smillert $guz->close(); 97b39c5158Smillert 98b39c5158Smillert my $x ; 99*256a93a4Safresh1 my $gz = $UncompressClass->can('new')->( $UncompressClass, \$gc); 100b39c5158Smillert 101b39c5158Smillert foreach my $name (qw(print printf write)) 102b39c5158Smillert { 103b39c5158Smillert eval " \$gz->$name() " ; 104b39c5158Smillert like $@, mkEvalErr("^$name Not Available: File opened only for intput"); 105b39c5158Smillert } 106b39c5158Smillert 107b39c5158Smillert } 108b39c5158Smillert 109b39c5158Smillert 110b39c5158Smillert { 111b39c5158Smillert title "Testing $CompressClass and $UncompressClass"; 112b39c5158Smillert 113b39c5158Smillert { 114b39c5158Smillert my ($a, $x, @x) = ("","","") ; 115b39c5158Smillert 116b39c5158Smillert # Buffer not a scalar reference 117*256a93a4Safresh1 eval qq[\$a = $CompressClass->new( \\\@x );] ; 118b39c5158Smillert like $@, mkEvalErr("^$CompressClass: output parameter not a filename, filehandle or scalar ref"); 119b39c5158Smillert 120b39c5158Smillert # Buffer not a scalar reference 121*256a93a4Safresh1 eval qq[\$a = $UncompressClass->new( \\\@x );] ; 122b39c5158Smillert like $@, mkEvalErr("^$UncompressClass: input parameter not a filename, filehandle, array ref or scalar ref"); 123b39c5158Smillert } 124b39c5158Smillert 125b39c5158Smillert foreach my $Type ( $CompressClass, $UncompressClass) 126b39c5158Smillert { 127b39c5158Smillert # Check error handling with IO::Compress::Deflate and IO::Uncompress::Inflate 128b39c5158Smillert 129b39c5158Smillert my ($a, $x, @x) = ("","","") ; 130b39c5158Smillert 131b39c5158Smillert # Odd number of parameters 132*256a93a4Safresh1 eval qq[\$a = $Type->new( "abc", -Output ) ] ; 133b39c5158Smillert like $@, mkEvalErr("^$Type: Expected even number of parameters, got 1"); 134b39c5158Smillert 135b39c5158Smillert # Unknown parameter 136*256a93a4Safresh1 eval qq[\$a = $Type->new( "anc", -Fred => 123 );] ; 137b39c5158Smillert like $@, mkEvalErr("^$Type: unknown key value\\(s\\) Fred"); 138b39c5158Smillert 139b39c5158Smillert # no in or out param 140*256a93a4Safresh1 eval qq[\$a = $Type->new();] ; 141b39c5158Smillert like $@, mkEvalErr("^$Type: Missing (Input|Output) parameter"); 142b39c5158Smillert 143b39c5158Smillert } 144b39c5158Smillert 145b39c5158Smillert 146b39c5158Smillert { 147b39c5158Smillert # write a very simple compressed file 148b39c5158Smillert # and read back 149b39c5158Smillert #======================================== 150b39c5158Smillert 151b39c5158Smillert 152*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 153b39c5158Smillert 154b39c5158Smillert my $hello = <<EOM ; 155b39c5158Smillerthello world 156b39c5158Smillertthis is a test 157b39c5158SmillertEOM 158b39c5158Smillert 159b39c5158Smillert { 160b39c5158Smillert my $x ; 161*256a93a4Safresh1 ok $x = $CompressClass->can('new')->( $CompressClass, $name ); 162b39c5158Smillert is $x->autoflush(1), 0, "autoflush"; 163b39c5158Smillert is $x->autoflush(1), 1, "autoflush"; 164b39c5158Smillert ok $x->opened(), "opened"; 165b39c5158Smillert 166b39c5158Smillert ok $x->write($hello), "write" ; 167b39c5158Smillert ok $x->flush(), "flush"; 168b39c5158Smillert ok $x->close, "close" ; 169b39c5158Smillert ok ! $x->opened(), "! opened"; 170b39c5158Smillert } 171b39c5158Smillert 172b39c5158Smillert { 173b39c5158Smillert my $uncomp; 174*256a93a4Safresh1 ok my $x = $UncompressClass->can('new')->( $UncompressClass, $name, -Append => 1 ); 175b39c5158Smillert ok $x->opened(), "opened"; 176b39c5158Smillert 177b39c5158Smillert my $len ; 178b39c5158Smillert 1 while ($len = $x->read($uncomp)) > 0 ; 179b39c5158Smillert 180b39c5158Smillert is $len, 0, "read returned 0" 181b39c5158Smillert or diag $$UnError ; 182b39c5158Smillert 183b39c5158Smillert ok $x->close ; 184b39c5158Smillert is $uncomp, $hello ; 185b39c5158Smillert ok !$x->opened(), "! opened"; 186b39c5158Smillert } 187b39c5158Smillert } 188b39c5158Smillert 189b39c5158Smillert { 190b39c5158Smillert # write a very simple compressed file 191b39c5158Smillert # and read back 192b39c5158Smillert #======================================== 193b39c5158Smillert 194b39c5158Smillert 195*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 196b39c5158Smillert 197b39c5158Smillert my $hello = <<EOM ; 198b39c5158Smillerthello world 199b39c5158Smillertthis is a test 200b39c5158SmillertEOM 201b39c5158Smillert 202b39c5158Smillert { 203b39c5158Smillert my $x ; 204*256a93a4Safresh1 ok $x = $CompressClass->can('new')->( $CompressClass, $name ); 205b39c5158Smillert 206b39c5158Smillert is $x->write(''), 0, "Write empty string is ok"; 207b39c5158Smillert is $x->write(undef), 0, "Write undef is ok"; 208b39c5158Smillert ok $x->write($hello), "Write ok" ; 209b39c5158Smillert ok $x->close, "Close ok" ; 210b39c5158Smillert } 211b39c5158Smillert 212b39c5158Smillert { 213b39c5158Smillert my $uncomp; 214*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, $name ); 215b39c5158Smillert ok $x, "creates $UncompressClass $name" ; 216b39c5158Smillert 217b39c5158Smillert my $data = ''; 218b39c5158Smillert $data .= $uncomp while $x->read($uncomp) > 0 ; 219b39c5158Smillert 220b39c5158Smillert ok $x->close, "close ok" ; 221b39c5158Smillert is $data, $hello, "expected output" ; 222b39c5158Smillert } 223b39c5158Smillert } 224b39c5158Smillert 225b39c5158Smillert 226b39c5158Smillert { 227b39c5158Smillert # write a very simple file with using an IO filehandle 228b39c5158Smillert # and read back 229b39c5158Smillert #======================================== 230b39c5158Smillert 231b39c5158Smillert 232*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 233b39c5158Smillert 234b39c5158Smillert my $hello = <<EOM ; 235b39c5158Smillerthello world 236b39c5158Smillertthis is a test 237b39c5158SmillertEOM 238b39c5158Smillert 239b39c5158Smillert { 240*256a93a4Safresh1 my $fh = IO::File->new( ">$name" ); 241b39c5158Smillert ok $fh, "opened file $name ok"; 242*256a93a4Safresh1 my $x = $CompressClass->can('new')->( $CompressClass, $fh ); 243b39c5158Smillert ok $x, " created $CompressClass $fh" ; 244b39c5158Smillert 245b39c5158Smillert is $x->fileno(), fileno($fh), "fileno match" ; 246b39c5158Smillert is $x->write(''), 0, "Write empty string is ok"; 247b39c5158Smillert is $x->write(undef), 0, "Write undef is ok"; 248b39c5158Smillert ok $x->write($hello), "write ok" ; 249b39c5158Smillert ok $x->flush(), "flush"; 250b39c5158Smillert ok $x->close,"close" ; 251b39c5158Smillert $fh->close() ; 252b39c5158Smillert } 253b39c5158Smillert 254b39c5158Smillert my $uncomp; 255b39c5158Smillert { 256b39c5158Smillert my $x ; 257*256a93a4Safresh1 ok my $fh1 = IO::File->new( "<$name" ); 258*256a93a4Safresh1 ok $x = $UncompressClass->can('new')->( $UncompressClass, $fh1, -Append => 1 ); 259b39c5158Smillert ok $x->fileno() == fileno $fh1 ; 260b39c5158Smillert 261b39c5158Smillert 1 while $x->read($uncomp) > 0 ; 262b39c5158Smillert 263b39c5158Smillert ok $x->close ; 264b39c5158Smillert } 265b39c5158Smillert 266b39c5158Smillert ok $hello eq $uncomp ; 267b39c5158Smillert } 268b39c5158Smillert 269b39c5158Smillert { 270b39c5158Smillert # write a very simple file with using a glob filehandle 271b39c5158Smillert # and read back 272b39c5158Smillert #======================================== 273b39c5158Smillert 274b39c5158Smillert 275*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 276b39c5158Smillert #my $name = "/tmp/fred"; 277b39c5158Smillert 278b39c5158Smillert my $hello = <<EOM ; 279b39c5158Smillerthello world 280b39c5158Smillertthis is a test 281b39c5158SmillertEOM 282b39c5158Smillert 283b39c5158Smillert { 284b39c5158Smillert title "$CompressClass: Input from typeglob filehandle"; 285b39c5158Smillert ok open FH, ">$name" ; 286b39c5158Smillert 287*256a93a4Safresh1 my $x = $CompressClass->can('new')->( $CompressClass, *FH ); 288b39c5158Smillert ok $x, " create $CompressClass" ; 289b39c5158Smillert 290b39c5158Smillert is $x->fileno(), fileno(*FH), " fileno" ; 291b39c5158Smillert is $x->write(''), 0, " Write empty string is ok"; 292b39c5158Smillert is $x->write(undef), 0, " Write undef is ok"; 293b39c5158Smillert ok $x->write($hello), " Write ok" ; 294b39c5158Smillert ok $x->flush(), " Flush"; 295b39c5158Smillert ok $x->close, " Close" ; 296b39c5158Smillert close FH; 297b39c5158Smillert } 298b39c5158Smillert 299b39c5158Smillert 300b39c5158Smillert my $uncomp; 301b39c5158Smillert { 302b39c5158Smillert title "$UncompressClass: Input from typeglob filehandle, append output"; 303b39c5158Smillert my $x ; 304b39c5158Smillert ok open FH, "<$name" ; 305*256a93a4Safresh1 ok $x = $UncompressClass->can('new')->( $UncompressClass, *FH, -Append => 1, Transparent => 0 ) 306b39c5158Smillert or diag $$UnError ; 307b39c5158Smillert is $x->fileno(), fileno FH, " fileno ok" ; 308b39c5158Smillert 309b39c5158Smillert 1 while $x->read($uncomp) > 0 ; 310b39c5158Smillert 311b39c5158Smillert ok $x->close, " close" ; 3126fb12b70Safresh1 close FH; 313b39c5158Smillert } 314b39c5158Smillert 315b39c5158Smillert is $uncomp, $hello, " expected output" ; 316b39c5158Smillert } 317b39c5158Smillert 318b39c5158Smillert { 319*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 320b39c5158Smillert #my $name = "/tmp/fred"; 321b39c5158Smillert 322b39c5158Smillert my $hello = <<EOM ; 323b39c5158Smillerthello world 324b39c5158Smillertthis is a test 325b39c5158SmillertEOM 326b39c5158Smillert 327b39c5158Smillert { 328b39c5158Smillert title "Outout to stdout via '-'" ; 329b39c5158Smillert 330b39c5158Smillert open(SAVEOUT, ">&STDOUT"); 331b39c5158Smillert my $dummy = fileno SAVEOUT; 332b39c5158Smillert open STDOUT, ">$name" ; 333b39c5158Smillert 334*256a93a4Safresh1 my $x = $CompressClass->can('new')->( $CompressClass, '-' ); 335b39c5158Smillert $x->write($hello); 336b39c5158Smillert $x->close; 337b39c5158Smillert 338b39c5158Smillert open(STDOUT, ">&SAVEOUT"); 339b39c5158Smillert 340b39c5158Smillert ok 1, " wrote to stdout" ; 341b39c5158Smillert } 342b39c5158Smillert is myGZreadFile($name), $hello, " wrote OK"; 343b39c5158Smillert #hexDump($name); 344b39c5158Smillert 345*256a93a4Safresh1 SKIP: 346b39c5158Smillert { 347b39c5158Smillert title "Input from stdin via filename '-'"; 348b39c5158Smillert 349*256a93a4Safresh1 # Older versions of Windows can hang on these tests 350*256a93a4Safresh1 skip 'Skipping STDIN tests', 5 351*256a93a4Safresh1 if $ENV{IO_COMPRESS_SKIP_STDIN_TESTS}; 352*256a93a4Safresh1 353b39c5158Smillert my $x ; 354b39c5158Smillert my $uncomp ; 355b39c5158Smillert my $stdinFileno = fileno(STDIN); 356898184e3Ssthen # open below doesn't return 1 sometimes on XP 357b39c5158Smillert open(SAVEIN, "<&STDIN"); 358b39c5158Smillert ok open(STDIN, "<$name"), " redirect STDIN"; 359b39c5158Smillert my $dummy = fileno SAVEIN; 360*256a93a4Safresh1 $x = $UncompressClass->can('new')->( $UncompressClass, '-', Append => 1, Transparent => 0 ) 361b39c5158Smillert or diag $$UnError ; 362b39c5158Smillert ok $x, " created object" ; 363b39c5158Smillert is $x->fileno(), $stdinFileno, " fileno ok" ; 364b39c5158Smillert 365b39c5158Smillert 1 while $x->read($uncomp) > 0 ; 366b39c5158Smillert 367b39c5158Smillert ok $x->close, " close" ; 368b39c5158Smillert open(STDIN, "<&SAVEIN"); 369b39c5158Smillert is $uncomp, $hello, " expected output" ; 370b39c5158Smillert } 371b39c5158Smillert } 372b39c5158Smillert 373b39c5158Smillert { 374b39c5158Smillert # write a compressed file to memory 375b39c5158Smillert # and read back 376b39c5158Smillert #======================================== 377b39c5158Smillert 378b39c5158Smillert #my $name = "test.gz" ; 379*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 380b39c5158Smillert 381b39c5158Smillert my $hello = <<EOM ; 382b39c5158Smillerthello world 383b39c5158Smillertthis is a test 384b39c5158SmillertEOM 385b39c5158Smillert 386b39c5158Smillert my $buffer ; 387b39c5158Smillert { 388b39c5158Smillert my $x ; 389*256a93a4Safresh1 ok $x = $CompressClass->can('new')->( $CompressClass, \$buffer) ; 390b39c5158Smillert 391b39c5158Smillert ok ! defined $x->autoflush(1) ; 392b39c5158Smillert ok ! defined $x->autoflush(1) ; 393b39c5158Smillert ok ! defined $x->fileno() ; 394b39c5158Smillert is $x->write(''), 0, "Write empty string is ok"; 395b39c5158Smillert is $x->write(undef), 0, "Write undef is ok"; 396b39c5158Smillert ok $x->write($hello) ; 397b39c5158Smillert ok $x->flush(); 398b39c5158Smillert ok $x->close ; 399b39c5158Smillert 400b39c5158Smillert writeFile($name, $buffer) ; 401b39c5158Smillert #is anyUncompress(\$buffer), $hello, " any ok"; 402b39c5158Smillert } 403b39c5158Smillert 404b39c5158Smillert my $keep = $buffer ; 405b39c5158Smillert my $uncomp; 406b39c5158Smillert { 407b39c5158Smillert my $x ; 408*256a93a4Safresh1 ok $x = $UncompressClass->can('new')->( $UncompressClass, \$buffer, Append => 1) ; 409b39c5158Smillert 410b39c5158Smillert ok ! defined $x->autoflush(1) ; 411b39c5158Smillert ok ! defined $x->autoflush(1) ; 412b39c5158Smillert ok ! defined $x->fileno() ; 413b39c5158Smillert 1 while $x->read($uncomp) > 0 ; 414b39c5158Smillert 415b39c5158Smillert ok $x->close, "closed" ; 416b39c5158Smillert } 417b39c5158Smillert 418b39c5158Smillert is $uncomp, $hello, "got expected uncompressed data" ; 419b39c5158Smillert ok $buffer eq $keep, "compressed input not changed" ; 420b39c5158Smillert } 421b39c5158Smillert 422b39c5158Smillert if ($CompressClass ne 'RawDeflate') 423b39c5158Smillert { 424b39c5158Smillert # write empty file 425b39c5158Smillert #======================================== 426b39c5158Smillert 427b39c5158Smillert my $buffer = ''; 428b39c5158Smillert { 429b39c5158Smillert my $x ; 430*256a93a4Safresh1 $x = $CompressClass->can('new')->( $CompressClass, \$buffer); 431b39c5158Smillert ok $x, "new $CompressClass" ; 432b39c5158Smillert ok $x->close, "close ok" ; 433b39c5158Smillert 434b39c5158Smillert } 435b39c5158Smillert 436b39c5158Smillert my $keep = $buffer ; 437b39c5158Smillert my $uncomp= ''; 438b39c5158Smillert { 439b39c5158Smillert my $x ; 440*256a93a4Safresh1 ok $x = $UncompressClass->can('new')->( $UncompressClass, \$buffer, Append => 1) ; 441b39c5158Smillert 442b39c5158Smillert 1 while $x->read($uncomp) > 0 ; 443b39c5158Smillert 444b39c5158Smillert ok $x->close ; 445b39c5158Smillert } 446b39c5158Smillert 447b39c5158Smillert ok $uncomp eq '' ; 448b39c5158Smillert ok $buffer eq $keep ; 449b39c5158Smillert 450b39c5158Smillert } 451b39c5158Smillert 452b39c5158Smillert { 453b39c5158Smillert # write a larger file 454b39c5158Smillert #======================================== 455b39c5158Smillert 456b39c5158Smillert 457*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 458b39c5158Smillert 459b39c5158Smillert my $hello = <<EOM ; 460b39c5158Smillerthello world 461b39c5158Smillertthis is a test 462b39c5158SmillertEOM 463b39c5158Smillert 464b39c5158Smillert my $input = '' ; 465b39c5158Smillert my $contents = '' ; 466b39c5158Smillert 467b39c5158Smillert { 468*256a93a4Safresh1 my $x = $CompressClass->can('new')->( $CompressClass, $name ); 469b39c5158Smillert ok $x, " created $CompressClass object"; 470b39c5158Smillert 471b39c5158Smillert ok $x->write($hello), " write ok" ; 472b39c5158Smillert $input .= $hello ; 473b39c5158Smillert ok $x->write("another line"), " write ok" ; 474b39c5158Smillert $input .= "another line" ; 475b39c5158Smillert # all characters 476b39c5158Smillert foreach (0 .. 255) 477b39c5158Smillert { $contents .= chr int $_ } 478b39c5158Smillert # generate a long random string 479b39c5158Smillert foreach (1 .. 5000) 480b39c5158Smillert { $contents .= chr int rand 256 } 481b39c5158Smillert 482b39c5158Smillert ok $x->write($contents), " write ok" ; 483b39c5158Smillert $input .= $contents ; 484b39c5158Smillert ok $x->close, " close ok" ; 485b39c5158Smillert } 486b39c5158Smillert 487b39c5158Smillert ok myGZreadFile($name) eq $input ; 488b39c5158Smillert my $x = readFile($name) ; 489b39c5158Smillert #print "length " . length($x) . " \n"; 490b39c5158Smillert } 491b39c5158Smillert 492f3efcd01Safresh1 SKIP: 493b39c5158Smillert { 494b39c5158Smillert # embed a compressed file in another file 495b39c5158Smillert #================================ 496b39c5158Smillert 497f3efcd01Safresh1 skip "zstd doesn't support trailing data", 11 498f3efcd01Safresh1 if $CompressClass =~ /zstd/i ; 499b39c5158Smillert 500*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 501b39c5158Smillert 502b39c5158Smillert my $hello = <<EOM ; 503b39c5158Smillerthello world 504b39c5158Smillertthis is a test 505b39c5158SmillertEOM 506b39c5158Smillert 507b39c5158Smillert my $header = "header info\n" ; 508b39c5158Smillert my $trailer = "trailer data\n" ; 509b39c5158Smillert 510b39c5158Smillert { 511b39c5158Smillert my $fh ; 512*256a93a4Safresh1 ok $fh = IO::File->new( ">$name" ); 513b39c5158Smillert print $fh $header ; 514b39c5158Smillert my $x ; 515*256a93a4Safresh1 ok $x = $CompressClass->can('new')->( $CompressClass, $fh, 516*256a93a4Safresh1 -AutoClose => 0 ); 517b39c5158Smillert 518b39c5158Smillert ok $x->binmode(); 519b39c5158Smillert ok $x->write($hello) ; 520b39c5158Smillert ok $x->close ; 521b39c5158Smillert print $fh $trailer ; 522b39c5158Smillert $fh->close() ; 523b39c5158Smillert } 524b39c5158Smillert 525b39c5158Smillert my ($fil, $uncomp) ; 526b39c5158Smillert my $fh1 ; 527*256a93a4Safresh1 ok $fh1 = IO::File->new( "<$name" ); 528b39c5158Smillert # skip leading junk 529b39c5158Smillert my $line = <$fh1> ; 530b39c5158Smillert ok $line eq $header ; 531b39c5158Smillert 532*256a93a4Safresh1 ok my $x = $UncompressClass->can('new')->( $UncompressClass, $fh1, Append => 1 ); 533b39c5158Smillert ok $x->binmode(); 534b39c5158Smillert 1 while $x->read($uncomp) > 0 ; 535b39c5158Smillert 536f3efcd01Safresh1 is $uncomp, $hello ; 537b39c5158Smillert my $rest ; 538b39c5158Smillert read($fh1, $rest, 5000); 539b39c5158Smillert is $x->trailingData() . $rest, $trailer ; 540b39c5158Smillert #print "# [".$x->trailingData() . "][$rest]\n" ; 541b39c5158Smillert 542b39c5158Smillert } 543b39c5158Smillert 544f3efcd01Safresh1 SKIP: 545b39c5158Smillert { 546b39c5158Smillert # embed a compressed file in another buffer 547b39c5158Smillert #================================ 548b39c5158Smillert 549f3efcd01Safresh1 skip "zstd doesn't support trailing data", 6 550f3efcd01Safresh1 if $CompressClass =~ /zstd/i ; 551b39c5158Smillert 552b39c5158Smillert my $hello = <<EOM ; 553b39c5158Smillerthello world 554b39c5158Smillertthis is a test 555b39c5158SmillertEOM 556b39c5158Smillert 557b39c5158Smillert my $trailer = "trailer data" ; 558b39c5158Smillert 559b39c5158Smillert my $compressed ; 560b39c5158Smillert 561b39c5158Smillert { 562*256a93a4Safresh1 ok my $x = $CompressClass->can('new')->( $CompressClass, \$compressed); 563b39c5158Smillert 564b39c5158Smillert ok $x->write($hello) ; 565b39c5158Smillert ok $x->close ; 566b39c5158Smillert $compressed .= $trailer ; 567b39c5158Smillert } 568b39c5158Smillert 569b39c5158Smillert my $uncomp; 570*256a93a4Safresh1 ok my $x = $UncompressClass->can('new')->( $UncompressClass, \$compressed, Append => 1) ; 571b39c5158Smillert 1 while $x->read($uncomp) > 0 ; 572b39c5158Smillert 573b39c5158Smillert ok $uncomp eq $hello ; 574b39c5158Smillert is $x->trailingData(), $trailer ; 575b39c5158Smillert 576b39c5158Smillert } 577b39c5158Smillert 578b39c5158Smillert { 579b39c5158Smillert # Write 580b39c5158Smillert # these tests come almost 100% from IO::String 581b39c5158Smillert 582*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 583b39c5158Smillert 584b39c5158Smillert my $io = $CompressClass->new($name); 585b39c5158Smillert 586b39c5158Smillert is $io->tell(), 0, " tell returns 0"; ; 587b39c5158Smillert 588b39c5158Smillert my $heisan = "Heisan\n"; 589b39c5158Smillert $io->print($heisan) ; 590b39c5158Smillert 591b39c5158Smillert ok ! $io->eof(), " ! eof"; 592b39c5158Smillert 593b39c5158Smillert is $io->tell(), length($heisan), " tell is " . length($heisan) ; 594b39c5158Smillert 595b39c5158Smillert $io->print("a", "b", "c"); 596b39c5158Smillert 597b39c5158Smillert { 598b39c5158Smillert local($\) = "\n"; 599b39c5158Smillert $io->print("d", "e"); 600b39c5158Smillert local($,) = ","; 601b39c5158Smillert $io->print("f", "g", "h"); 602b39c5158Smillert } 603b39c5158Smillert 604b39c5158Smillert { 605b39c5158Smillert local($\) ; 606b39c5158Smillert $io->print("D", "E"); 607b39c5158Smillert local($,) = "."; 608b39c5158Smillert $io->print("F", "G", "H"); 609b39c5158Smillert } 610b39c5158Smillert 611b39c5158Smillert my $foo = "1234567890"; 612b39c5158Smillert 613b39c5158Smillert is $io->syswrite($foo, length($foo)), length($foo), " syswrite ok" ; 614b39c5158Smillert if ( $] < 5.6 ) 615b39c5158Smillert { is $io->syswrite($foo, length $foo), length $foo, " syswrite ok" } 616b39c5158Smillert else 617b39c5158Smillert { is $io->syswrite($foo), length $foo, " syswrite ok" } 618b39c5158Smillert is $io->syswrite($foo, length($foo)), length $foo, " syswrite ok"; 619b39c5158Smillert is $io->write($foo, length($foo), 5), 5, " write 5"; 620b39c5158Smillert is $io->write("xxx\n", 100, -1), 1, " write 1"; 621b39c5158Smillert 622b39c5158Smillert for (1..3) { 623b39c5158Smillert $io->printf("i(%d)", $_); 624b39c5158Smillert $io->printf("[%d]\n", $_); 625b39c5158Smillert } 626b39c5158Smillert $io->print("\n"); 627b39c5158Smillert 628b39c5158Smillert $io->close ; 629b39c5158Smillert 630b39c5158Smillert ok $io->eof(), " eof"; 631b39c5158Smillert 632b39c5158Smillert is myGZreadFile($name), "Heisan\nabcde\nf,g,h\nDEF.G.H" . 633b39c5158Smillert ("1234567890" x 3) . "67890\n" . 634b39c5158Smillert "i(1)[1]\ni(2)[2]\ni(3)[3]\n\n", 635b39c5158Smillert "myGZreadFile ok"; 636b39c5158Smillert 637b39c5158Smillert 638b39c5158Smillert } 639b39c5158Smillert 640b39c5158Smillert { 641b39c5158Smillert # Read 642b39c5158Smillert my $str = <<EOT; 643b39c5158SmillertThis is an example 644b39c5158Smillertof a paragraph 645b39c5158Smillert 646b39c5158Smillert 647b39c5158Smillertand a single line. 648b39c5158Smillert 649b39c5158SmillertEOT 650b39c5158Smillert 651*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 652b39c5158Smillert 653b39c5158Smillert my %opts = () ; 654*256a93a4Safresh1 my $iow = $CompressClass->can('new')->( $CompressClass, $name, %opts ); 655b39c5158Smillert is $iow->input_line_number, undef; 656b39c5158Smillert $iow->print($str) ; 657b39c5158Smillert is $iow->input_line_number, undef; 658b39c5158Smillert $iow->close ; 659b39c5158Smillert 660b39c5158Smillert my @tmp; 661b39c5158Smillert my $buf; 662b39c5158Smillert { 663*256a93a4Safresh1 my $io = $UncompressClass->can('new')->( $UncompressClass, $name ); 664b39c5158Smillert 665b39c5158Smillert is $., 0; 666b39c5158Smillert is $io->input_line_number, 0; 667b39c5158Smillert ok ! $io->eof, "eof"; 668b39c5158Smillert is $io->tell(), 0, "tell 0" ; 669b39c5158Smillert #my @lines = <$io>; 670b39c5158Smillert my @lines = $io->getlines(); 671b39c5158Smillert is @lines, 6 672b39c5158Smillert or print "# Got " . scalar(@lines) . " lines, expected 6\n" ; 673b39c5158Smillert is $lines[1], "of a paragraph\n" ; 674b39c5158Smillert is join('', @lines), $str ; 675b39c5158Smillert is $., 6; 676b39c5158Smillert is $io->input_line_number, 6; 677b39c5158Smillert is $io->tell(), length($str) ; 678b39c5158Smillert 679b39c5158Smillert ok $io->eof; 680b39c5158Smillert 681b39c5158Smillert ok ! ( defined($io->getline) || 682b39c5158Smillert (@tmp = $io->getlines) || 683b39c5158Smillert defined($io->getline) || 684b39c5158Smillert defined($io->getc) || 685b39c5158Smillert $io->read($buf, 100) != 0) ; 686b39c5158Smillert } 687b39c5158Smillert 688b39c5158Smillert 689b39c5158Smillert { 690b39c5158Smillert local $/; # slurp mode 691b39c5158Smillert my $io = $UncompressClass->new($name); 692898184e3Ssthen is $., 0, "line 0"; 693b39c5158Smillert is $io->input_line_number, 0; 694898184e3Ssthen ok ! $io->eof, "eof"; 695b39c5158Smillert my @lines = $io->getlines; 696898184e3Ssthen is $., 1, "line 1"; 697898184e3Ssthen is $io->input_line_number, 1, "line number 1"; 698898184e3Ssthen ok $io->eof, "eof" ; 699b39c5158Smillert ok @lines == 1 && $lines[0] eq $str; 700b39c5158Smillert 701b39c5158Smillert $io = $UncompressClass->new($name); 702b39c5158Smillert ok ! $io->eof; 703b39c5158Smillert my $line = $io->getline(); 704b39c5158Smillert ok $line eq $str; 705b39c5158Smillert ok $io->eof; 706b39c5158Smillert } 707b39c5158Smillert 708b39c5158Smillert { 709b39c5158Smillert local $/ = ""; # paragraph mode 710b39c5158Smillert my $io = $UncompressClass->new($name); 711b39c5158Smillert is $., 0; 712b39c5158Smillert is $io->input_line_number, 0; 713b39c5158Smillert ok ! $io->eof; 714b39c5158Smillert my @lines = $io->getlines(); 715b39c5158Smillert is $., 2; 716b39c5158Smillert is $io->input_line_number, 2; 717b39c5158Smillert ok $io->eof; 718b39c5158Smillert ok @lines == 2 719b39c5158Smillert or print "# Got " . scalar(@lines) . " lines, expected 2\n" ; 720b39c5158Smillert ok $lines[0] eq "This is an example\nof a paragraph\n\n\n" 721b39c5158Smillert or print "# $lines[0]\n"; 722b39c5158Smillert ok $lines[1] eq "and a single line.\n\n"; 723b39c5158Smillert } 724b39c5158Smillert 725b39c5158Smillert { 726b39c5158Smillert # Record mode 727b39c5158Smillert my $reclen = 7 ; 728b39c5158Smillert my $expected_records = int(length($str) / $reclen) 729b39c5158Smillert + (length($str) % $reclen ? 1 : 0); 730b39c5158Smillert local $/ = \$reclen; 731b39c5158Smillert 732b39c5158Smillert my $io = $UncompressClass->new($name); 733b39c5158Smillert is $., 0; 734b39c5158Smillert is $io->input_line_number, 0; 735b39c5158Smillert 736b39c5158Smillert ok ! $io->eof; 737b39c5158Smillert my @lines = $io->getlines(); 738b39c5158Smillert is $., $expected_records; 739b39c5158Smillert is $io->input_line_number, $expected_records; 740b39c5158Smillert ok $io->eof; 741b39c5158Smillert is @lines, $expected_records, 742b39c5158Smillert "Got $expected_records records\n" ; 743b39c5158Smillert ok $lines[0] eq substr($str, 0, $reclen) 744b39c5158Smillert or print "# $lines[0]\n"; 745b39c5158Smillert ok $lines[1] eq substr($str, $reclen, $reclen); 746b39c5158Smillert } 747b39c5158Smillert 748b39c5158Smillert { 749b39c5158Smillert local $/ = "is"; 750b39c5158Smillert my $io = $UncompressClass->new($name); 751b39c5158Smillert my @lines = (); 752b39c5158Smillert my $no = 0; 753b39c5158Smillert my $err = 0; 754b39c5158Smillert ok ! $io->eof; 755b39c5158Smillert while (my $a = $io->getline()) { 756b39c5158Smillert push(@lines, $a); 757b39c5158Smillert $err++ if $. != ++$no; 758b39c5158Smillert } 759b39c5158Smillert 760b39c5158Smillert ok $err == 0 ; 761b39c5158Smillert ok $io->eof; 762b39c5158Smillert 763b39c5158Smillert is $., 3; 764b39c5158Smillert is $io->input_line_number, 3; 765b39c5158Smillert ok @lines == 3 766b39c5158Smillert or print "# Got " . scalar(@lines) . " lines, expected 3\n" ; 767b39c5158Smillert ok join("-", @lines) eq 768b39c5158Smillert "This- is- an example\n" . 769b39c5158Smillert "of a paragraph\n\n\n" . 770b39c5158Smillert "and a single line.\n\n"; 771b39c5158Smillert } 772b39c5158Smillert 773b39c5158Smillert 774b39c5158Smillert # Test read 775b39c5158Smillert 776b39c5158Smillert { 777b39c5158Smillert my $io = $UncompressClass->new($name); 778b39c5158Smillert 779b39c5158Smillert 780b39c5158Smillert eval { $io->read(1) } ; 781b39c5158Smillert like $@, mkErr("buffer parameter is read-only"); 782b39c5158Smillert 783b39c5158Smillert $buf = "abcd"; 784b39c5158Smillert is $io->read($buf, 0), 0, "Requested 0 bytes" ; 785b39c5158Smillert is $buf, "", "Buffer empty"; 786b39c5158Smillert 787b39c5158Smillert is $io->read($buf, 3), 3 ; 788b39c5158Smillert is $buf, "Thi"; 789b39c5158Smillert 790b39c5158Smillert is $io->sysread($buf, 3, 2), 3 ; 791b39c5158Smillert is $buf, "Ths i" 792b39c5158Smillert or print "# [$buf]\n" ;; 793b39c5158Smillert ok ! $io->eof; 794b39c5158Smillert 795b39c5158Smillert $buf = "ab" ; 796b39c5158Smillert is $io->read($buf, 3, 4), 3 ; 797b39c5158Smillert is $buf, "ab" . "\x00" x 2 . "s a" 798b39c5158Smillert or print "# [$buf]\n" ;; 799b39c5158Smillert ok ! $io->eof; 800b39c5158Smillert 801b39c5158Smillert # read the rest of the file 802b39c5158Smillert $buf = ''; 803b39c5158Smillert my $remain = length($str) - 9; 804b39c5158Smillert is $io->read($buf, $remain+1), $remain ; 805b39c5158Smillert is $buf, substr($str, 9); 806b39c5158Smillert ok $io->eof; 807b39c5158Smillert 808b39c5158Smillert $buf = "hello"; 809b39c5158Smillert is $io->read($buf, 10), 0 ; 810b39c5158Smillert is $buf, "", "Buffer empty"; 811b39c5158Smillert ok $io->eof; 812b39c5158Smillert 813b39c5158Smillert ok $io->close(); 814b39c5158Smillert $buf = "hello"; 815b39c5158Smillert is $io->read($buf, 10), 0 ; 816b39c5158Smillert is $buf, "hello", "Buffer not empty"; 817b39c5158Smillert ok $io->eof; 818b39c5158Smillert 819b39c5158Smillert # $io->seek(-4, 2); 820b39c5158Smillert # 821b39c5158Smillert # ok ! $io->eof; 822b39c5158Smillert # 823b39c5158Smillert # ok read($io, $buf, 20) == 4 ; 824b39c5158Smillert # ok $buf eq "e.\n\n"; 825b39c5158Smillert # 826b39c5158Smillert # ok read($io, $buf, 20) == 0 ; 827b39c5158Smillert # ok $buf eq ""; 828b39c5158Smillert # 829b39c5158Smillert # ok ! $io->eof; 830b39c5158Smillert } 831b39c5158Smillert 832b39c5158Smillert } 833b39c5158Smillert 834b39c5158Smillert { 835b39c5158Smillert # Read from non-compressed file 836b39c5158Smillert 837b39c5158Smillert my $str = <<EOT; 838b39c5158SmillertThis is an example 839b39c5158Smillertof a paragraph 840b39c5158Smillert 841b39c5158Smillert 842b39c5158Smillertand a single line. 843b39c5158Smillert 844b39c5158SmillertEOT 845*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 846b39c5158Smillert 847b39c5158Smillert writeFile($name, $str); 848b39c5158Smillert my @tmp; 849b39c5158Smillert my $buf; 850b39c5158Smillert { 851*256a93a4Safresh1 my $io = $UncompressClass->can('new')->( $UncompressClass, $name, -Transparent => 1 ); 852b39c5158Smillert 853898184e3Ssthen isa_ok $io, $UncompressClass ; 854898184e3Ssthen ok ! $io->eof, "eof"; 855898184e3Ssthen is $io->tell(), 0, "tell == 0" ; 856b39c5158Smillert my @lines = $io->getlines(); 857898184e3Ssthen is @lines, 6, "got 6 lines"; 858b39c5158Smillert ok $lines[1] eq "of a paragraph\n" ; 859b39c5158Smillert ok join('', @lines) eq $str ; 860b39c5158Smillert is $., 6; 861b39c5158Smillert is $io->input_line_number, 6; 862b39c5158Smillert ok $io->tell() == length($str) ; 863b39c5158Smillert 864b39c5158Smillert ok $io->eof; 865b39c5158Smillert 866b39c5158Smillert ok ! ( defined($io->getline) || 867b39c5158Smillert (@tmp = $io->getlines) || 868b39c5158Smillert defined($io->getline) || 869b39c5158Smillert defined($io->getc) || 870b39c5158Smillert $io->read($buf, 100) != 0) ; 871b39c5158Smillert } 872b39c5158Smillert 873b39c5158Smillert 874b39c5158Smillert { 875b39c5158Smillert local $/; # slurp mode 876b39c5158Smillert my $io = $UncompressClass->new($name); 877b39c5158Smillert ok ! $io->eof; 878b39c5158Smillert my @lines = $io->getlines; 879b39c5158Smillert is $., 1; 880b39c5158Smillert is $io->input_line_number, 1; 881b39c5158Smillert ok $io->eof; 882b39c5158Smillert ok @lines == 1 && $lines[0] eq $str; 883b39c5158Smillert 884b39c5158Smillert $io = $UncompressClass->new($name); 885b39c5158Smillert ok ! $io->eof; 886b39c5158Smillert my $line = $io->getline; 887b39c5158Smillert is $., 1; 888b39c5158Smillert is $io->input_line_number, 1; 889898184e3Ssthen is $line, $str; 890b39c5158Smillert ok $io->eof; 891b39c5158Smillert } 892b39c5158Smillert 893b39c5158Smillert { 894b39c5158Smillert local $/ = ""; # paragraph mode 895b39c5158Smillert my $io = $UncompressClass->new($name); 896b39c5158Smillert ok ! $io->eof; 897b39c5158Smillert my @lines = $io->getlines; 898b39c5158Smillert is $., 2; 899b39c5158Smillert is $io->input_line_number, 2; 900b39c5158Smillert ok $io->eof; 901b39c5158Smillert ok @lines == 2 902898184e3Ssthen or print "# expected 2 lines, got " . scalar(@lines) . "\n"; 903b39c5158Smillert ok $lines[0] eq "This is an example\nof a paragraph\n\n\n" 904b39c5158Smillert or print "# [$lines[0]]\n" ; 905b39c5158Smillert ok $lines[1] eq "and a single line.\n\n"; 906b39c5158Smillert } 907b39c5158Smillert 908b39c5158Smillert { 909b39c5158Smillert # Record mode 910b39c5158Smillert my $reclen = 7 ; 911b39c5158Smillert my $expected_records = int(length($str) / $reclen) 912b39c5158Smillert + (length($str) % $reclen ? 1 : 0); 913b39c5158Smillert local $/ = \$reclen; 914b39c5158Smillert 915b39c5158Smillert my $io = $UncompressClass->new($name); 916b39c5158Smillert is $., 0; 917b39c5158Smillert is $io->input_line_number, 0; 918b39c5158Smillert 919b39c5158Smillert ok ! $io->eof; 920b39c5158Smillert my @lines = $io->getlines(); 921b39c5158Smillert is $., $expected_records; 922b39c5158Smillert is $io->input_line_number, $expected_records; 923b39c5158Smillert ok $io->eof; 924b39c5158Smillert is @lines, $expected_records, 925b39c5158Smillert "Got $expected_records records\n" ; 926b39c5158Smillert ok $lines[0] eq substr($str, 0, $reclen) 927b39c5158Smillert or print "# $lines[0]\n"; 928b39c5158Smillert ok $lines[1] eq substr($str, $reclen, $reclen); 929b39c5158Smillert } 930b39c5158Smillert 931b39c5158Smillert { 932b39c5158Smillert local $/ = "is"; 933b39c5158Smillert my $io = $UncompressClass->new($name); 934b39c5158Smillert my @lines = (); 935b39c5158Smillert my $no = 0; 936b39c5158Smillert my $err = 0; 937b39c5158Smillert ok ! $io->eof; 938b39c5158Smillert while (my $a = $io->getline) { 939b39c5158Smillert push(@lines, $a); 940b39c5158Smillert $err++ if $. != ++$no; 941b39c5158Smillert } 942b39c5158Smillert 943b39c5158Smillert is $., 3; 944b39c5158Smillert is $io->input_line_number, 3; 945b39c5158Smillert ok $err == 0 ; 946b39c5158Smillert ok $io->eof; 947b39c5158Smillert 948b39c5158Smillert 949b39c5158Smillert ok @lines == 3 ; 950b39c5158Smillert ok join("-", @lines) eq 951b39c5158Smillert "This- is- an example\n" . 952b39c5158Smillert "of a paragraph\n\n\n" . 953b39c5158Smillert "and a single line.\n\n"; 954b39c5158Smillert } 955b39c5158Smillert 956b39c5158Smillert 957b39c5158Smillert # Test Read 958b39c5158Smillert 959b39c5158Smillert { 960b39c5158Smillert my $io = $UncompressClass->new($name); 961b39c5158Smillert 962b39c5158Smillert $buf = "abcd"; 963b39c5158Smillert is $io->read($buf, 0), 0, "Requested 0 bytes" ; 964b39c5158Smillert is $buf, "", "Buffer empty"; 965b39c5158Smillert 966b39c5158Smillert ok $io->read($buf, 3) == 3 ; 967b39c5158Smillert ok $buf eq "Thi"; 968b39c5158Smillert 969b39c5158Smillert ok $io->sysread($buf, 3, 2) == 3 ; 970b39c5158Smillert ok $buf eq "Ths i"; 971b39c5158Smillert ok ! $io->eof; 972b39c5158Smillert 973b39c5158Smillert $buf = "ab" ; 974b39c5158Smillert is $io->read($buf, 3, 4), 3 ; 975b39c5158Smillert is $buf, "ab" . "\x00" x 2 . "s a" 976b39c5158Smillert or print "# [$buf]\n" ;; 977b39c5158Smillert ok ! $io->eof; 978b39c5158Smillert 979b39c5158Smillert # read the rest of the file 980b39c5158Smillert $buf = ''; 981b39c5158Smillert my $remain = length($str) - 9; 982b39c5158Smillert is $io->read($buf, $remain), $remain ; 983b39c5158Smillert is $buf, substr($str, 9); 984b39c5158Smillert ok $io->eof; 985b39c5158Smillert 986b39c5158Smillert $buf = "hello"; 987b39c5158Smillert is $io->read($buf, 10), 0 ; 988b39c5158Smillert is $buf, "", "Buffer empty"; 989b39c5158Smillert ok $io->eof; 990b39c5158Smillert 991b39c5158Smillert ok $io->close(); 992b39c5158Smillert $buf = "hello"; 993b39c5158Smillert is $io->read($buf, 10), 0 ; 994b39c5158Smillert is $buf, "hello", "Buffer not empty"; 995b39c5158Smillert ok $io->eof; 996b39c5158Smillert 997b39c5158Smillert # $io->seek(-4, 2); 998b39c5158Smillert # 999b39c5158Smillert # ok ! $io->eof; 1000b39c5158Smillert # 1001b39c5158Smillert # ok read($io, $buf, 20) == 4 ; 1002b39c5158Smillert # ok $buf eq "e.\n\n"; 1003b39c5158Smillert # 1004b39c5158Smillert # ok read($io, $buf, 20) == 0 ; 1005b39c5158Smillert # ok $buf eq ""; 1006b39c5158Smillert # 1007b39c5158Smillert # ok ! $io->eof; 1008b39c5158Smillert } 1009b39c5158Smillert 1010b39c5158Smillert 1011b39c5158Smillert } 1012b39c5158Smillert 1013b39c5158Smillert { 1014b39c5158Smillert # Vary the length parameter in a read 1015b39c5158Smillert 1016b39c5158Smillert my $str = <<EOT; 1017b39c5158Smillertx 1018b39c5158Smillertx 1019b39c5158SmillertThis is an example 1020b39c5158Smillertof a paragraph 1021b39c5158Smillert 1022b39c5158Smillert 1023b39c5158Smillertand a single line. 1024b39c5158Smillert 1025b39c5158SmillertEOT 1026b39c5158Smillert $str = $str x 100 ; 1027b39c5158Smillert 1028b39c5158Smillert 1029b39c5158Smillert foreach my $bufsize (1, 3, 512, 4096, length($str)-1, length($str), length($str)+1) 1030b39c5158Smillert { 1031b39c5158Smillert foreach my $trans (0, 1) 1032b39c5158Smillert { 1033b39c5158Smillert foreach my $append (0, 1) 1034b39c5158Smillert { 1035b39c5158Smillert title "Read Tests - buf length $bufsize, Transparent $trans, Append $append" ; 1036b39c5158Smillert 1037*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 1038b39c5158Smillert 1039b39c5158Smillert if ($trans) { 1040b39c5158Smillert writeFile($name, $str) ; 1041b39c5158Smillert } 1042b39c5158Smillert else { 1043*256a93a4Safresh1 my $iow = $CompressClass->can('new')->( $CompressClass, $name ); 1044b39c5158Smillert $iow->print($str) ; 1045b39c5158Smillert $iow->close ; 1046b39c5158Smillert } 1047b39c5158Smillert 1048b39c5158Smillert 1049b39c5158Smillert my $io = $UncompressClass->new($name, 1050b39c5158Smillert -Append => $append, 1051b39c5158Smillert -Transparent => $trans); 1052b39c5158Smillert 1053b39c5158Smillert my $buf; 1054b39c5158Smillert 1055b39c5158Smillert is $io->tell(), 0; 1056b39c5158Smillert 1057b39c5158Smillert if ($append) { 1058b39c5158Smillert 1 while $io->read($buf, $bufsize) > 0; 1059b39c5158Smillert } 1060b39c5158Smillert else { 1061b39c5158Smillert my $tmp ; 1062b39c5158Smillert $buf .= $tmp while $io->read($tmp, $bufsize) > 0 ; 1063b39c5158Smillert } 1064b39c5158Smillert is length $buf, length $str; 1065b39c5158Smillert ok $buf eq $str ; 1066b39c5158Smillert ok ! $io->error() ; 1067b39c5158Smillert ok $io->eof; 1068b39c5158Smillert } 1069b39c5158Smillert } 1070b39c5158Smillert } 1071b39c5158Smillert } 1072b39c5158Smillert 1073b39c5158Smillert foreach my $file (0, 1) 1074b39c5158Smillert { 1075b39c5158Smillert foreach my $trans (0, 1) 1076b39c5158Smillert { 1077b39c5158Smillert title "seek tests - file $file trans $trans" ; 1078b39c5158Smillert 1079b39c5158Smillert my $buffer ; 1080b39c5158Smillert my $buff ; 1081*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 1082b39c5158Smillert 1083b39c5158Smillert my $first = "beginning" ; 1084b39c5158Smillert my $last = "the end" ; 1085b39c5158Smillert 1086b39c5158Smillert if ($trans) 1087b39c5158Smillert { 1088b39c5158Smillert $buffer = $first . "\x00" x 10 . $last; 1089b39c5158Smillert writeFile($name, $buffer); 1090b39c5158Smillert } 1091b39c5158Smillert else 1092b39c5158Smillert { 1093b39c5158Smillert my $output ; 1094b39c5158Smillert if ($file) 1095b39c5158Smillert { 1096b39c5158Smillert $output = $name ; 1097b39c5158Smillert } 1098b39c5158Smillert else 1099b39c5158Smillert { 1100b39c5158Smillert $output = \$buffer; 1101b39c5158Smillert } 1102b39c5158Smillert 1103*256a93a4Safresh1 my $iow = $CompressClass->can('new')->( $CompressClass, $output ); 1104b39c5158Smillert $iow->print($first) ; 1105b39c5158Smillert ok $iow->seek(5, SEEK_CUR) ; 1106b39c5158Smillert ok $iow->tell() == length($first)+5; 1107b39c5158Smillert ok $iow->seek(0, SEEK_CUR) ; 1108b39c5158Smillert ok $iow->tell() == length($first)+5; 1109b39c5158Smillert ok $iow->seek(length($first)+10, SEEK_SET) ; 1110b39c5158Smillert ok $iow->tell() == length($first)+10; 1111b39c5158Smillert 1112b39c5158Smillert $iow->print($last) ; 1113b39c5158Smillert $iow->close ; 1114b39c5158Smillert } 1115b39c5158Smillert 1116b39c5158Smillert my $input ; 1117b39c5158Smillert if ($file) 1118b39c5158Smillert { 1119b39c5158Smillert $input = $name ; 1120b39c5158Smillert } 1121b39c5158Smillert else 1122b39c5158Smillert { 1123b39c5158Smillert $input = \$buffer ; 1124b39c5158Smillert } 1125b39c5158Smillert 1126b39c5158Smillert ok myGZreadFile($input) eq $first . "\x00" x 10 . $last ; 1127b39c5158Smillert 1128b39c5158Smillert my $io = $UncompressClass->new($input, Strict => 1); 1129b39c5158Smillert ok $io->seek(length($first), SEEK_CUR) 1130b39c5158Smillert or diag $$UnError ; 1131b39c5158Smillert ok ! $io->eof; 1132b39c5158Smillert is $io->tell(), length($first); 1133b39c5158Smillert 1134b39c5158Smillert ok $io->read($buff, 5) ; 1135b39c5158Smillert is $buff, "\x00" x 5 ; 1136b39c5158Smillert is $io->tell(), length($first) + 5; 1137b39c5158Smillert 1138b39c5158Smillert ok $io->seek(0, SEEK_CUR) ; 1139b39c5158Smillert my $here = $io->tell() ; 1140b39c5158Smillert is $here, length($first)+5; 1141b39c5158Smillert 1142b39c5158Smillert ok $io->seek($here+5, SEEK_SET) ; 1143b39c5158Smillert is $io->tell(), $here+5 ; 1144b39c5158Smillert ok $io->read($buff, 100) ; 1145b39c5158Smillert ok $buff eq $last ; 1146b39c5158Smillert ok $io->eof; 1147b39c5158Smillert } 1148b39c5158Smillert } 1149b39c5158Smillert 1150b39c5158Smillert { 1151b39c5158Smillert title "seek error cases" ; 1152b39c5158Smillert 1153b39c5158Smillert my $b ; 1154*256a93a4Safresh1 my $a = $CompressClass->can('new')->( $CompressClass, \$b) ; 1155b39c5158Smillert 1156f3efcd01Safresh1 ok ! $a->error() 1157f3efcd01Safresh1 or die $a->error() ; 1158b39c5158Smillert eval { $a->seek(-1, 10) ; }; 1159b39c5158Smillert like $@, mkErr("^${CompressClass}::seek: unknown value, 10, for whence parameter"); 1160b39c5158Smillert 1161b39c5158Smillert eval { $a->seek(-1, SEEK_END) ; }; 1162b39c5158Smillert like $@, mkErr("^${CompressClass}::seek: cannot seek backwards"); 1163b39c5158Smillert 1164b39c5158Smillert $a->write("fred"); 1165b39c5158Smillert $a->close ; 1166b39c5158Smillert 1167b39c5158Smillert 1168*256a93a4Safresh1 my $u = $UncompressClass->can('new')->( $UncompressClass, \$b) ; 1169b39c5158Smillert 1170b39c5158Smillert eval { $u->seek(-1, 10) ; }; 1171b39c5158Smillert like $@, mkErr("^${UncompressClass}::seek: unknown value, 10, for whence parameter"); 1172b39c5158Smillert 1173b39c5158Smillert eval { $u->seek(-1, SEEK_END) ; }; 1174b39c5158Smillert like $@, mkErr("^${UncompressClass}::seek: SEEK_END not allowed"); 1175b39c5158Smillert 1176b39c5158Smillert eval { $u->seek(-1, SEEK_CUR) ; }; 1177b39c5158Smillert like $@, mkErr("^${UncompressClass}::seek: cannot seek backwards"); 1178b39c5158Smillert } 1179b39c5158Smillert 1180b39c5158Smillert foreach my $fb (qw(filename buffer filehandle)) 1181b39c5158Smillert { 1182b39c5158Smillert foreach my $append (0, 1) 1183b39c5158Smillert { 1184b39c5158Smillert { 1185b39c5158Smillert title "$CompressClass -- Append $append, Output to $fb" ; 1186b39c5158Smillert 1187*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 1188b39c5158Smillert 1189b39c5158Smillert my $already = 'already'; 1190b39c5158Smillert my $buffer = $already; 1191b39c5158Smillert my $output; 1192b39c5158Smillert 1193b39c5158Smillert if ($fb eq 'buffer') 1194b39c5158Smillert { $output = \$buffer } 1195b39c5158Smillert elsif ($fb eq 'filename') 1196b39c5158Smillert { 1197b39c5158Smillert $output = $name ; 1198b39c5158Smillert writeFile($name, $buffer); 1199b39c5158Smillert } 1200b39c5158Smillert elsif ($fb eq 'filehandle') 1201b39c5158Smillert { 1202*256a93a4Safresh1 $output = IO::File->new( ">$name" ); 1203b39c5158Smillert print $output $buffer; 1204b39c5158Smillert } 1205b39c5158Smillert 1206*256a93a4Safresh1 my $a = $CompressClass->can('new')->( $CompressClass, $output, Append => $append) ; 1207b39c5158Smillert ok $a, " Created $CompressClass"; 1208b39c5158Smillert my $string = "appended"; 1209b39c5158Smillert $a->write($string); 1210b39c5158Smillert $a->close ; 1211b39c5158Smillert 1212b39c5158Smillert my $data ; 1213b39c5158Smillert if ($fb eq 'buffer') 1214b39c5158Smillert { 1215b39c5158Smillert $data = $buffer; 1216b39c5158Smillert } 1217b39c5158Smillert else 1218b39c5158Smillert { 1219b39c5158Smillert $output->close 1220b39c5158Smillert if $fb eq 'filehandle'; 1221b39c5158Smillert $data = readFile($name); 1222b39c5158Smillert } 1223b39c5158Smillert 1224b39c5158Smillert if ($append || $fb eq 'filehandle') 1225b39c5158Smillert { 1226b39c5158Smillert is substr($data, 0, length($already)), $already, " got prefix"; 1227b39c5158Smillert substr($data, 0, length($already)) = ''; 1228b39c5158Smillert } 1229b39c5158Smillert 1230b39c5158Smillert 1231b39c5158Smillert my $uncomp; 1232*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, \$data, Append => 1) ; 1233b39c5158Smillert ok $x, " created $UncompressClass"; 1234b39c5158Smillert 1235b39c5158Smillert my $len ; 1236b39c5158Smillert 1 while ($len = $x->read($uncomp)) > 0 ; 1237b39c5158Smillert 1238b39c5158Smillert $x->close ; 1239b39c5158Smillert is $uncomp, $string, ' Got uncompressed data' ; 1240b39c5158Smillert 1241b39c5158Smillert } 1242b39c5158Smillert } 1243b39c5158Smillert } 1244b39c5158Smillert 1245b39c5158Smillert foreach my $type (qw(buffer filename filehandle)) 1246b39c5158Smillert { 1247b39c5158Smillert foreach my $good (0, 1) 1248b39c5158Smillert { 1249b39c5158Smillert title "$UncompressClass -- InputLength, read from $type, good data => $good"; 1250b39c5158Smillert 1251b39c5158Smillert my $compressed ; 1252b39c5158Smillert my $string = "some data"; 1253b39c5158Smillert my $appended = "append"; 1254b39c5158Smillert 1255b39c5158Smillert if ($good) 1256b39c5158Smillert { 1257*256a93a4Safresh1 my $c = $CompressClass->can('new')->( $CompressClass, \$compressed); 1258b39c5158Smillert $c->write($string); 1259b39c5158Smillert $c->close(); 1260b39c5158Smillert } 1261b39c5158Smillert else 1262b39c5158Smillert { 1263b39c5158Smillert $compressed = $string ; 1264b39c5158Smillert } 1265b39c5158Smillert 1266b39c5158Smillert my $comp_len = length $compressed; 1267b39c5158Smillert $compressed .= $appended; 1268b39c5158Smillert 1269*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 1270b39c5158Smillert my $input ; 1271b39c5158Smillert writeFile ($name, $compressed); 1272b39c5158Smillert 1273b39c5158Smillert if ($type eq 'buffer') 1274b39c5158Smillert { 1275b39c5158Smillert $input = \$compressed; 1276b39c5158Smillert } 1277b39c5158Smillert if ($type eq 'filename') 1278b39c5158Smillert { 1279b39c5158Smillert $input = $name; 1280b39c5158Smillert } 1281b39c5158Smillert elsif ($type eq 'filehandle') 1282b39c5158Smillert { 1283*256a93a4Safresh1 my $fh = IO::File->new( "<$name" ); 1284b39c5158Smillert ok $fh, "opened file $name ok"; 1285b39c5158Smillert $input = $fh ; 1286b39c5158Smillert } 1287b39c5158Smillert 1288*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, $input, 1289b39c5158Smillert InputLength => $comp_len, 1290b39c5158Smillert Transparent => 1) ; 1291b39c5158Smillert ok $x, " created $UncompressClass"; 1292b39c5158Smillert 1293b39c5158Smillert my $len ; 1294b39c5158Smillert my $output; 1295b39c5158Smillert $len = $x->read($output, 100); 1296b39c5158Smillert 1297b39c5158Smillert is $len, length($string); 1298b39c5158Smillert is $output, $string; 1299b39c5158Smillert 1300b39c5158Smillert if ($type eq 'filehandle') 1301b39c5158Smillert { 1302b39c5158Smillert my $rest ; 1303b39c5158Smillert $input->read($rest, 1000); 1304b39c5158Smillert is $rest, $appended; 1305b39c5158Smillert } 1306b39c5158Smillert } 1307b39c5158Smillert 1308b39c5158Smillert 1309b39c5158Smillert } 1310b39c5158Smillert 1311b39c5158Smillert foreach my $append (0, 1) 1312b39c5158Smillert { 1313b39c5158Smillert title "$UncompressClass -- Append $append" ; 1314b39c5158Smillert 1315*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 1316b39c5158Smillert 1317b39c5158Smillert my $string = "appended"; 1318b39c5158Smillert my $compressed ; 1319*256a93a4Safresh1 my $c = $CompressClass->can('new')->( $CompressClass, \$compressed); 1320b39c5158Smillert $c->write($string); 1321b39c5158Smillert $c->close(); 1322b39c5158Smillert 1323*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, \$compressed, Append => $append) ; 1324b39c5158Smillert ok $x, " created $UncompressClass"; 1325b39c5158Smillert 1326b39c5158Smillert my $already = 'already'; 1327b39c5158Smillert my $output = $already; 1328b39c5158Smillert 1329b39c5158Smillert my $len ; 1330b39c5158Smillert $len = $x->read($output, 100); 1331b39c5158Smillert is $len, length($string); 1332b39c5158Smillert 1333b39c5158Smillert $x->close ; 1334b39c5158Smillert 1335b39c5158Smillert if ($append) 1336b39c5158Smillert { 1337b39c5158Smillert is substr($output, 0, length($already)), $already, " got prefix"; 1338b39c5158Smillert substr($output, 0, length($already)) = ''; 1339b39c5158Smillert } 1340b39c5158Smillert is $output, $string, ' Got uncompressed data' ; 1341b39c5158Smillert } 1342b39c5158Smillert 1343b39c5158Smillert 1344b39c5158Smillert foreach my $file (0, 1) 1345b39c5158Smillert { 1346b39c5158Smillert foreach my $trans (0, 1) 1347b39c5158Smillert { 1348b39c5158Smillert title "ungetc, File $file, Transparent $trans" ; 1349b39c5158Smillert 1350*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 1351b39c5158Smillert 1352b39c5158Smillert my $string = 'abcdeABCDE'; 1353b39c5158Smillert my $b ; 1354b39c5158Smillert if ($trans) 1355b39c5158Smillert { 1356b39c5158Smillert $b = $string ; 1357b39c5158Smillert } 1358b39c5158Smillert else 1359b39c5158Smillert { 1360*256a93a4Safresh1 my $a = $CompressClass->can('new')->( $CompressClass, \$b) ; 1361b39c5158Smillert $a->write($string); 1362b39c5158Smillert $a->close ; 1363b39c5158Smillert } 1364b39c5158Smillert 1365b39c5158Smillert my $from ; 1366b39c5158Smillert if ($file) 1367b39c5158Smillert { 1368b39c5158Smillert writeFile($name, $b); 1369b39c5158Smillert $from = $name ; 1370b39c5158Smillert } 1371b39c5158Smillert else 1372b39c5158Smillert { 1373b39c5158Smillert $from = \$b ; 1374b39c5158Smillert } 1375b39c5158Smillert 1376b39c5158Smillert my $u = $UncompressClass->new($from, Transparent => 1) ; 1377b39c5158Smillert my $first; 1378b39c5158Smillert my $buff ; 1379b39c5158Smillert 1380b39c5158Smillert # do an ungetc before reading 1381b39c5158Smillert $u->ungetc("X"); 1382b39c5158Smillert $first = $u->getc(); 1383b39c5158Smillert is $first, 'X'; 1384b39c5158Smillert 1385b39c5158Smillert $first = $u->getc(); 1386b39c5158Smillert is $first, substr($string, 0,1); 1387b39c5158Smillert $u->ungetc($first); 1388b39c5158Smillert $first = $u->getc(); 1389b39c5158Smillert is $first, substr($string, 0,1); 1390b39c5158Smillert $u->ungetc($first); 1391b39c5158Smillert 1392b39c5158Smillert is $u->read($buff, 5), 5 ; 1393b39c5158Smillert is $buff, substr($string, 0, 5); 1394b39c5158Smillert 1395b39c5158Smillert $u->ungetc($buff) ; 1396b39c5158Smillert is $u->read($buff, length($string)), length($string) ; 1397b39c5158Smillert is $buff, $string; 1398b39c5158Smillert 1399b39c5158Smillert is $u->read($buff, 1), 0; 1400b39c5158Smillert ok $u->eof() ; 1401b39c5158Smillert 1402b39c5158Smillert my $extra = 'extra'; 1403b39c5158Smillert $u->ungetc($extra); 1404b39c5158Smillert ok ! $u->eof(); 1405b39c5158Smillert is $u->read($buff), length($extra) ; 1406b39c5158Smillert is $buff, $extra; 1407b39c5158Smillert 1408b39c5158Smillert is $u->read($buff, 1), 0; 1409b39c5158Smillert ok $u->eof() ; 1410b39c5158Smillert 1411b39c5158Smillert # getc returns undef on eof 1412b39c5158Smillert is $u->getc(), undef; 1413b39c5158Smillert $u->close(); 1414b39c5158Smillert 1415b39c5158Smillert } 1416b39c5158Smillert } 1417b39c5158Smillert 1418b39c5158Smillert { 1419b39c5158Smillert title "write tests - invalid data" ; 1420b39c5158Smillert 1421*256a93a4Safresh1 #my $lex = LexFile->new( my $name1 ); 1422b39c5158Smillert my($Answer); 1423b39c5158Smillert 1424b39c5158Smillert #ok ! -e $name1, " File $name1 does not exist"; 1425b39c5158Smillert 1426b39c5158Smillert my @data = ( 1427b39c5158Smillert [ '{ }', "${CompressClass}::write: input parameter not a filename, filehandle, array ref or scalar ref" ], 1428b39c5158Smillert [ '[ { } ]', "${CompressClass}::write: input parameter not a filename, filehandle, array ref or scalar ref" ], 1429b39c5158Smillert [ '[ [ { } ] ]', "${CompressClass}::write: input parameter not a filename, filehandle, array ref or scalar ref" ], 1430b39c5158Smillert [ '[ "" ]', "${CompressClass}::write: input filename is undef or null string" ], 1431b39c5158Smillert [ '[ undef ]', "${CompressClass}::write: input filename is undef or null string" ], 1432b39c5158Smillert [ '[ \$Answer ]',"${CompressClass}::write: input and output buffer are identical" ], 1433b39c5158Smillert #[ "not readable", 'xx' ], 1434b39c5158Smillert # same filehandle twice, 'xx' 1435b39c5158Smillert ) ; 1436b39c5158Smillert 1437b39c5158Smillert foreach my $data (@data) 1438b39c5158Smillert { 1439b39c5158Smillert my ($send, $get) = @$data ; 1440b39c5158Smillert title "${CompressClass}::write( $send )"; 1441b39c5158Smillert my($copy); 1442b39c5158Smillert eval "\$copy = $send"; 1443*256a93a4Safresh1 my $x = $CompressClass->can('new')->( $CompressClass, \$Answer); 1444b39c5158Smillert ok $x, " Created $CompressClass object"; 1445b39c5158Smillert eval { $x->write($copy) } ; 1446b39c5158Smillert #like $@, "/^$get/", " error - $get"; 1447b39c5158Smillert like $@, "/not a scalar reference /", " error - not a scalar reference"; 1448b39c5158Smillert } 1449b39c5158Smillert 1450b39c5158Smillert # @data = ( 1451b39c5158Smillert # [ '[ $name1 ]', "input file '$name1' does not exist" ], 1452b39c5158Smillert # #[ "not readable", 'xx' ], 1453b39c5158Smillert # # same filehandle twice, 'xx' 1454b39c5158Smillert # ) ; 1455b39c5158Smillert # 1456b39c5158Smillert # foreach my $data (@data) 1457b39c5158Smillert # { 1458b39c5158Smillert # my ($send, $get) = @$data ; 1459b39c5158Smillert # title "${CompressClass}::write( $send )"; 1460b39c5158Smillert # my $copy; 1461b39c5158Smillert # eval "\$copy = $send"; 1462*256a93a4Safresh1 # my $x = $CompressClass->can('new')->( $CompressClass, \$Answer); 1463b39c5158Smillert # ok $x, " Created $CompressClass object"; 1464b39c5158Smillert # ok ! $x->write($copy), " write fails" ; 1465b39c5158Smillert # like $$Error, "/^$get/", " error - $get"; 1466b39c5158Smillert # } 1467b39c5158Smillert 1468b39c5158Smillert #exit; 1469b39c5158Smillert 1470b39c5158Smillert } 1471b39c5158Smillert 1472b39c5158Smillert 1473b39c5158Smillert # sub deepCopy 1474b39c5158Smillert # { 1475b39c5158Smillert # if (! ref $_[0] || ref $_[0] eq 'SCALAR') 1476b39c5158Smillert # { 1477b39c5158Smillert # return $_[0] ; 1478b39c5158Smillert # } 1479b39c5158Smillert # 1480b39c5158Smillert # if (ref $_[0] eq 'ARRAY') 1481b39c5158Smillert # { 1482b39c5158Smillert # my @a ; 1483b39c5158Smillert # for my $x ( @{ $_[0] }) 1484b39c5158Smillert # { 1485b39c5158Smillert # push @a, deepCopy($x); 1486b39c5158Smillert # } 1487b39c5158Smillert # 1488b39c5158Smillert # return \@a ; 1489b39c5158Smillert # } 1490b39c5158Smillert # 1491b39c5158Smillert # croak "bad! $_[0]"; 1492b39c5158Smillert # 1493b39c5158Smillert # } 1494b39c5158Smillert # 1495b39c5158Smillert # sub deepSubst 1496b39c5158Smillert # { 1497b39c5158Smillert # #my $data = shift ; 1498b39c5158Smillert # my $from = $_[1] ; 1499b39c5158Smillert # my $to = $_[2] ; 1500b39c5158Smillert # 1501b39c5158Smillert # if (! ref $_[0]) 1502b39c5158Smillert # { 1503b39c5158Smillert # $_[0] = $to 1504b39c5158Smillert # if $_[0] eq $from ; 1505b39c5158Smillert # return ; 1506b39c5158Smillert # 1507b39c5158Smillert # } 1508b39c5158Smillert # 1509b39c5158Smillert # if (ref $_[0] eq 'SCALAR') 1510b39c5158Smillert # { 1511b39c5158Smillert # $_[0] = \$to 1512b39c5158Smillert # if defined ${ $_[0] } && ${ $_[0] } eq $from ; 1513b39c5158Smillert # return ; 1514b39c5158Smillert # 1515b39c5158Smillert # } 1516b39c5158Smillert # 1517b39c5158Smillert # if (ref $_[0] eq 'ARRAY') 1518b39c5158Smillert # { 1519b39c5158Smillert # for my $x ( @{ $_[0] }) 1520b39c5158Smillert # { 1521b39c5158Smillert # deepSubst($x, $from, $to); 1522b39c5158Smillert # } 1523b39c5158Smillert # return ; 1524b39c5158Smillert # } 1525b39c5158Smillert # #croak "bad! $_[0]"; 1526b39c5158Smillert # } 1527b39c5158Smillert 1528b39c5158Smillert # { 1529b39c5158Smillert # title "More write tests" ; 1530b39c5158Smillert # 1531b39c5158Smillert # my $file1 = "file1" ; 1532b39c5158Smillert # my $file2 = "file2" ; 1533b39c5158Smillert # my $file3 = "file3" ; 1534*256a93a4Safresh1 # my $lex = LexFile->new( $file1, $file2, $file3 ); 1535b39c5158Smillert # 1536b39c5158Smillert # writeFile($file1, "F1"); 1537b39c5158Smillert # writeFile($file2, "F2"); 1538b39c5158Smillert # writeFile($file3, "F3"); 1539b39c5158Smillert # 1540b39c5158Smillert # my @data = ( 1541b39c5158Smillert # [ '""', "" ], 1542b39c5158Smillert # [ 'undef', "" ], 1543b39c5158Smillert # [ '"abcd"', "abcd" ], 1544b39c5158Smillert # 1545b39c5158Smillert # [ '\""', "" ], 1546b39c5158Smillert # [ '\undef', "" ], 1547b39c5158Smillert # [ '\"abcd"', "abcd" ], 1548b39c5158Smillert # 1549b39c5158Smillert # [ '[]', "" ], 1550b39c5158Smillert # [ '[[]]', "" ], 1551b39c5158Smillert # [ '[[[]]]', "" ], 1552b39c5158Smillert # [ '[\""]', "" ], 1553b39c5158Smillert # [ '[\undef]', "" ], 1554b39c5158Smillert # [ '[\"abcd"]', "abcd" ], 1555b39c5158Smillert # [ '[\"ab", \"cd"]', "abcd" ], 1556b39c5158Smillert # [ '[[\"ab"], [\"cd"]]', "abcd" ], 1557b39c5158Smillert # 1558b39c5158Smillert # [ '$file1', $file1 ], 1559b39c5158Smillert # [ '$fh2', "F2" ], 1560b39c5158Smillert # [ '[$file1, \"abc"]', "F1abc"], 1561b39c5158Smillert # [ '[\"a", $file1, \"bc"]', "aF1bc"], 1562b39c5158Smillert # [ '[\"a", $fh1, \"bc"]', "aF1bc"], 1563b39c5158Smillert # [ '[\"a", $fh1, \"bc", $file2]', "aF1bcF2"], 1564b39c5158Smillert # [ '[\"a", $fh1, \"bc", $file2, $fh3]', "aF1bcF2F3"], 1565b39c5158Smillert # ) ; 1566b39c5158Smillert # 1567b39c5158Smillert # 1568b39c5158Smillert # foreach my $data (@data) 1569b39c5158Smillert # { 1570b39c5158Smillert # my ($send, $get) = @$data ; 1571b39c5158Smillert # 1572*256a93a4Safresh1 # my $fh1 = IO::File->new( "< $file1" ); 1573*256a93a4Safresh1 # my $fh2 = IO::File->new( "< $file2" ); 1574*256a93a4Safresh1 # my $fh3 = IO::File->new( "< $file3" ); 1575b39c5158Smillert # 1576b39c5158Smillert # title "${CompressClass}::write( $send )"; 1577b39c5158Smillert # my $copy; 1578b39c5158Smillert # eval "\$copy = $send"; 1579b39c5158Smillert # my $Answer ; 1580*256a93a4Safresh1 # my $x = $CompressClass->can('new')->( $CompressClass, \$Answer); 1581b39c5158Smillert # ok $x, " Created $CompressClass object"; 1582b39c5158Smillert # my $len = length $get; 1583b39c5158Smillert # is $x->write($copy), length($get), " write $len bytes"; 1584b39c5158Smillert # ok $x->close(), " close ok" ; 1585b39c5158Smillert # 1586b39c5158Smillert # is myGZreadFile(\$Answer), $get, " got expected output" ; 1587b39c5158Smillert # cmp_ok $$Error, '==', 0, " no error"; 1588b39c5158Smillert # 1589b39c5158Smillert # 1590b39c5158Smillert # } 1591b39c5158Smillert # 1592b39c5158Smillert # } 1593b39c5158Smillert } 1594b39c5158Smillert 1595898184e3Ssthen { 1596898184e3Ssthen # Check can handle empty compressed files 1597898184e3Ssthen # Test is for rt.cpan #67554 1598898184e3Ssthen 1599898184e3Ssthen foreach my $type (qw(filename filehandle buffer )) 1600898184e3Ssthen { 1601898184e3Ssthen foreach my $append (0, 1) 1602898184e3Ssthen { 1603898184e3Ssthen title "$UncompressClass -- empty file read from $type, Append => $append"; 1604898184e3Ssthen 1605898184e3Ssthen my $appended = "append"; 1606898184e3Ssthen my $string = "some data"; 1607898184e3Ssthen my $compressed ; 1608898184e3Ssthen 1609*256a93a4Safresh1 my $c = $CompressClass->can('new')->( $CompressClass, \$compressed); 1610898184e3Ssthen $c->close(); 1611898184e3Ssthen 1612898184e3Ssthen my $comp_len = length $compressed; 1613f3efcd01Safresh1 $compressed .= $appended if $append && $CompressClass !~ /zstd/i; 1614898184e3Ssthen 1615*256a93a4Safresh1 my $lex = LexFile->new( my $name ); 1616898184e3Ssthen my $input ; 1617898184e3Ssthen writeFile ($name, $compressed); 1618898184e3Ssthen 1619898184e3Ssthen if ($type eq 'buffer') 1620898184e3Ssthen { 1621898184e3Ssthen $input = \$compressed; 1622898184e3Ssthen } 1623898184e3Ssthen elsif ($type eq 'filename') 1624898184e3Ssthen { 1625898184e3Ssthen $input = $name; 1626898184e3Ssthen } 1627898184e3Ssthen elsif ($type eq 'filehandle') 1628898184e3Ssthen { 1629*256a93a4Safresh1 my $fh = IO::File->new( "<$name" ); 1630898184e3Ssthen ok $fh, "opened file $name ok"; 1631898184e3Ssthen $input = $fh ; 1632898184e3Ssthen } 1633898184e3Ssthen 1634898184e3Ssthen { 1635898184e3Ssthen # Check that eof is true immediately after creating the 1636898184e3Ssthen # uncompression object. 1637898184e3Ssthen 1638898184e3Ssthen # Check that readline returns undef 1639898184e3Ssthen 1640*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, $input, Transparent => 0 ) 1641898184e3Ssthen or diag "$$UnError" ; 1642898184e3Ssthen isa_ok $x, $UncompressClass; 1643898184e3Ssthen 1644898184e3Ssthen # should be EOF immediately 1645898184e3Ssthen is $x->eof(), 1, "eof true"; 1646898184e3Ssthen 1647898184e3Ssthen is <$x>, undef, "getline is undef"; 1648898184e3Ssthen 1649898184e3Ssthen is $x->eof(), 1, "eof true"; 1650898184e3Ssthen } 1651898184e3Ssthen 1652898184e3Ssthen { 1653f3efcd01Safresh1 # Check that read returns an empty string 1654898184e3Ssthen if ($type eq 'filehandle') 1655898184e3Ssthen { 1656*256a93a4Safresh1 my $fh = IO::File->new( "<$name" ); 1657898184e3Ssthen ok $fh, "opened file $name ok"; 1658898184e3Ssthen $input = $fh ; 1659898184e3Ssthen } 1660898184e3Ssthen 1661*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, $input, Transparent => 0 ) 1662898184e3Ssthen or diag "$$UnError" ; 1663898184e3Ssthen isa_ok $x, $UncompressClass; 1664898184e3Ssthen 1665898184e3Ssthen my $buffer; 1666f3efcd01Safresh1 is $x->read($buffer), 0, "read 0 bytes" 1667f3efcd01Safresh1 or diag "read returned $$UnError"; 1668898184e3Ssthen ok defined $buffer, "buffer is defined"; 1669898184e3Ssthen is $buffer, "", "buffer is empty string"; 1670898184e3Ssthen 1671898184e3Ssthen is $x->eof(), 1, "eof true"; 1672898184e3Ssthen } 1673898184e3Ssthen 1674898184e3Ssthen { 1675898184e3Ssthen # Check that read return an empty string in Append Mode 1676898184e3Ssthen # to empty string 1677898184e3Ssthen 1678898184e3Ssthen if ($type eq 'filehandle') 1679898184e3Ssthen { 1680*256a93a4Safresh1 my $fh = IO::File->new( "<$name" ); 1681898184e3Ssthen ok $fh, "opened file $name ok"; 1682898184e3Ssthen $input = $fh ; 1683898184e3Ssthen } 1684*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, $input, Transparent => 0, 1685*256a93a4Safresh1 Append => 1 ) 1686898184e3Ssthen or diag "$$UnError" ; 1687898184e3Ssthen isa_ok $x, $UncompressClass; 1688898184e3Ssthen 1689898184e3Ssthen my $buffer; 1690898184e3Ssthen is $x->read($buffer), 0, "read 0 bytes"; 1691898184e3Ssthen ok defined $buffer, "buffer is defined"; 1692898184e3Ssthen is $buffer, "", "buffer is empty string"; 1693898184e3Ssthen 1694898184e3Ssthen is $x->eof(), 1, "eof true"; 1695898184e3Ssthen } 1696898184e3Ssthen { 1697898184e3Ssthen # Check that read return an empty string in Append Mode 1698898184e3Ssthen # to non-empty string 1699898184e3Ssthen 1700898184e3Ssthen if ($type eq 'filehandle') 1701898184e3Ssthen { 1702*256a93a4Safresh1 my $fh = IO::File->new( "<$name" ); 1703898184e3Ssthen ok $fh, "opened file $name ok"; 1704898184e3Ssthen $input = $fh ; 1705898184e3Ssthen } 1706*256a93a4Safresh1 my $x = $UncompressClass->can('new')->( $UncompressClass, $input, Append => 1 ); 1707898184e3Ssthen isa_ok $x, $UncompressClass; 1708898184e3Ssthen 1709898184e3Ssthen my $buffer = "123"; 1710898184e3Ssthen is $x->read($buffer), 0, "read 0 bytes"; 1711898184e3Ssthen ok defined $buffer, "buffer is defined"; 1712898184e3Ssthen is $buffer, "123", "buffer orig string"; 1713898184e3Ssthen 1714898184e3Ssthen is $x->eof(), 1, "eof true"; 1715898184e3Ssthen } 1716898184e3Ssthen } 1717898184e3Ssthen } 1718898184e3Ssthen } 1719f3efcd01Safresh1 1720f3efcd01Safresh1 { 1721f3efcd01Safresh1 # Round trip binary data that happens to contain \r\n 1722f3efcd01Safresh1 # via the filesystem 1723f3efcd01Safresh1 1724f3efcd01Safresh1 my $original = join '', map { chr } 0x00 .. 0xff ; 1725f3efcd01Safresh1 $original .= "data1\r\ndata2\r\ndata3\r\n" ; 1726f3efcd01Safresh1 1727f3efcd01Safresh1 1728f3efcd01Safresh1 title "$UncompressClass -- round trip test"; 1729f3efcd01Safresh1 1730f3efcd01Safresh1 my $string = $original; 1731f3efcd01Safresh1 1732*256a93a4Safresh1 my $lex = LexFile->new( my $name, my $compressed) ; 1733f3efcd01Safresh1 my $input ; 1734f3efcd01Safresh1 writeFile ($name, $original); 1735f3efcd01Safresh1 1736*256a93a4Safresh1 my $c = $CompressClass->can('new')->( $CompressClass, $compressed); 1737f3efcd01Safresh1 isa_ok $c, $CompressClass; 1738f3efcd01Safresh1 $c->print($string); 1739f3efcd01Safresh1 $c->close(); 1740f3efcd01Safresh1 1741*256a93a4Safresh1 my $u = $UncompressClass->can('new')->( $UncompressClass, $compressed, Transparent => 0 ) 1742f3efcd01Safresh1 or diag "$$UnError" ; 1743f3efcd01Safresh1 isa_ok $u, $UncompressClass; 1744f3efcd01Safresh1 my $buffer; 1745f3efcd01Safresh1 is $u->read($buffer), length($original), "read bytes"; 1746f3efcd01Safresh1 is $buffer, $original, " round tripped ok"; 1747f3efcd01Safresh1 1748f3efcd01Safresh1 1749f3efcd01Safresh1 } 1750b39c5158Smillert} 1751b39c5158Smillert 1752b39c5158Smillert1; 1753