"Fossies" - the Fresh Open Source Software Archive

Member "perl-5.32.1/cpan/IO-Compress/t/001bzip2.t" (18 Dec 2020, 5601 Bytes) of package /linux/misc/perl-5.32.1.tar.xz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 BEGIN {
    2     if ($ENV{PERL_CORE}) {
    3         chdir 't' if -d 't';
    4         @INC = ("../lib", "lib/compress");
    5     }
    6 }
    7 
    8 use lib qw(t t/compress);
    9 
   10 use strict;
   11 use warnings;
   12 use bytes;
   13 
   14 use Test::More ;
   15 use CompTestUtils;
   16 
   17 BEGIN 
   18 { 
   19     # use Test::NoWarnings, if available
   20     my $extra = 0 ;
   21     $extra = 1
   22         if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };
   23 
   24     plan tests => 841 + $extra ;
   25 };
   26 
   27 
   28 use IO::Compress::Bzip2     qw($Bzip2Error) ;
   29 use IO::Uncompress::Bunzip2 qw($Bunzip2Error) ;
   30 
   31 
   32 my $CompressClass   = 'IO::Compress::Bzip2';
   33 my $UncompressClass = getInverse($CompressClass);
   34 my $Error           = getErrorRef($CompressClass);
   35 my $UnError         = getErrorRef($UncompressClass);
   36 
   37 sub myBZreadFile
   38 {
   39     my $filename = shift ;
   40     my $init = shift ;
   41 
   42 
   43     my $fil = new $UncompressClass $filename,
   44                                     -Strict   => 1,
   45                                     -Append   => 1
   46                                     ;
   47 
   48     my $data = '';
   49     $data = $init if defined $init ;
   50     1 while $fil->read($data) > 0;
   51 
   52     $fil->close ;
   53     return $data ;
   54 }
   55 
   56 
   57 {
   58 
   59     title "Testing $CompressClass Errors";
   60 
   61     my $buffer ;
   62 
   63     for my $value (undef, -1, 'fred')
   64     {
   65         my $stringValue = defined $value ? $value : 'undef';
   66         title "BlockSize100K => $stringValue";
   67         my $err = "Parameter 'BlockSize100K' must be an unsigned int, got '$stringValue'";
   68         my $bz ;
   69         eval { $bz = new IO::Compress::Bzip2(\$buffer, BlockSize100K => $value) };
   70         like $@,  mkErr("IO::Compress::Bzip2: $err"),
   71             "  value $stringValue is bad";
   72         is $Bzip2Error, "IO::Compress::Bzip2: $err",
   73             "  value $stringValue is bad";
   74         ok ! $bz, "  no bz object";
   75     }
   76 
   77     for my $value (0, 10, 99999)
   78     {
   79         my $stringValue = defined $value ? $value : 'undef';
   80         title "BlockSize100K => $stringValue";
   81         my $err = "Parameter 'BlockSize100K' not between 1 and 9, got $stringValue";
   82         my $bz ;
   83         eval { $bz = new IO::Compress::Bzip2(\$buffer, BlockSize100K => $value) };
   84         like $@,  mkErr("IO::Compress::Bzip2: $err"),
   85             "  value $stringValue is bad";
   86         is $Bzip2Error,  "IO::Compress::Bzip2: $err",
   87             "  value $stringValue is bad";
   88         ok ! $bz, "  no bz object";
   89     }
   90 
   91     for my $value (undef, -1, 'fred')
   92     {
   93         my $stringValue = defined $value ? $value : 'undef';
   94         title "WorkFactor => $stringValue";
   95         my $err = "Parameter 'WorkFactor' must be an unsigned int, got '$stringValue'";
   96         my $bz ;
   97         eval { $bz = new IO::Compress::Bzip2(\$buffer, WorkFactor => $value) };
   98         like $@,  mkErr("IO::Compress::Bzip2: $err"),
   99             "  value $stringValue is bad";
  100         is $Bzip2Error, "IO::Compress::Bzip2: $err",
  101             "  value $stringValue is bad";
  102         ok ! $bz, "  no bz object";
  103     }
  104 
  105     for my $value (251, 99999)
  106     {
  107         my $stringValue = defined $value ? $value : 'undef';
  108         title "WorkFactor => $stringValue";
  109         my $err = "Parameter 'WorkFactor' not between 0 and 250, got $stringValue";
  110         my $bz ;
  111         eval { $bz = new IO::Compress::Bzip2(\$buffer, WorkFactor => $value) };
  112         like $@,  mkErr("IO::Compress::Bzip2: $err"),
  113             "  value $stringValue is bad";
  114         is $Bzip2Error,  "IO::Compress::Bzip2: $err",
  115             "  value $stringValue is bad";
  116         ok ! $bz, "  no bz object";
  117     }
  118 
  119 }
  120 
  121 
  122 {
  123     title "Testing $UncompressClass Errors";
  124 
  125     my $buffer ;
  126 
  127     for my $value (-1, 'fred')
  128     {
  129         my $stringValue = defined $value ? $value : 'undef';
  130         title "Small => $stringValue";
  131         my $err = "Parameter 'Small' must be an int, got '$stringValue'";
  132         my $bz ;
  133         eval { $bz = new IO::Uncompress::Bunzip2(\$buffer, Small => $value) };
  134         like $@,  mkErr("IO::Uncompress::Bunzip2: $err"),
  135             "  value $stringValue is bad";
  136         is $Bunzip2Error, "IO::Uncompress::Bunzip2: $err",
  137             "  value $stringValue is bad";
  138         ok ! $bz, "  no bz object";
  139     }
  140 
  141 }
  142 
  143 {
  144     title "Testing $CompressClass and $UncompressClass";
  145 
  146     my $hello = <<EOM ;
  147 hello world
  148 this is a test
  149 EOM
  150 
  151     for my $value ( 1 .. 9 )
  152     {
  153         title "$CompressClass - BlockSize100K => $value";
  154         my $lex = new LexFile my $name ;
  155         my $bz ;
  156         $bz = new IO::Compress::Bzip2($name, BlockSize100K => $value)
  157             or diag $IO::Compress::Bzip2::Bzip2Error ;
  158         ok $bz, "  bz object ok";
  159         $bz->write($hello);
  160         $bz->close($hello);
  161 
  162         is myBZreadFile($name), $hello, "  got expected content";
  163     }
  164 
  165     for my $value ( 0 .. 250 )
  166     {
  167         title "$CompressClass - WorkFactor => $value";
  168         my $lex = new LexFile my $name ;
  169         my $bz ;
  170         $bz = new IO::Compress::Bzip2($name, WorkFactor => $value);
  171         ok $bz, "  bz object ok";
  172         $bz->write($hello);
  173         $bz->close($hello);
  174 
  175         is myBZreadFile($name), $hello, "  got expected content";
  176     }
  177 
  178     for my $value ( 0 .. 1 )
  179     {
  180         title "$UncompressClass - Small => $value";
  181         my $lex = new LexFile my $name ;
  182         my $bz ;
  183         $bz = new IO::Compress::Bzip2($name);
  184         ok $bz, "  bz object ok";
  185         $bz->write($hello);
  186         $bz->close($hello);
  187 
  188         my $fil = new $UncompressClass $name,
  189                                        Append  => 1,
  190                                        Small   => $value ;
  191 
  192         my $data = '';
  193         1 while $fil->read($data) > 0;
  194 
  195         $fil->close ;
  196 
  197         is $data, $hello, " got expected";
  198     }
  199 }
  200 
  201 
  202 1;
  203 
  204 
  205 
  206