1BEGIN {
2    if ($ENV{PERL_CORE}) {
3        chdir 't' if -d 't';
4        @INC = ("../lib", "lib/compress");
5    }
6}
7
8use lib qw(t t/compress);
9
10use strict;
11use warnings;
12use bytes;
13
14use Test::More ;
15use CompTestUtils;
16
17BEGIN
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
28use IO::Compress::Bzip2     qw($Bzip2Error) ;
29use IO::Uncompress::Bunzip2 qw($Bunzip2Error) ;
30
31
32my $CompressClass   = 'IO::Compress::Bzip2';
33my $UncompressClass = getInverse($CompressClass);
34my $Error           = getErrorRef($CompressClass);
35my $UnError         = getErrorRef($UncompressClass);
36
37sub myBZreadFile
38{
39    my $filename = shift ;
40    my $init = shift ;
41
42
43    my $fil = $UncompressClass->can('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 = IO::Compress::Bzip2->new(\$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 = IO::Compress::Bzip2->new(\$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 = IO::Compress::Bzip2->new(\$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 = IO::Compress::Bzip2->new(\$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 = IO::Uncompress::Bunzip2->new(\$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 ;
147hello world
148this is a test
149EOM
150
151    for my $value ( 1 .. 9 )
152    {
153        title "$CompressClass - BlockSize100K => $value";
154        my $lex = LexFile->new( my $name );
155        my $bz ;
156        $bz = IO::Compress::Bzip2->new($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 = LexFile->new( my $name );
169        my $bz ;
170        $bz = IO::Compress::Bzip2->new($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 = LexFile->new( my $name );
182        my $bz ;
183        $bz = IO::Compress::Bzip2->new($name);
184        ok $bz, "  bz object ok";
185        $bz->write($hello);
186        $bz->close($hello);
187
188        my $fil = $UncompressClass->can('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
2021;
203