Commit | Line | Data |
25f0751f |
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 | use strict; |
10 | use warnings; |
11 | use bytes; |
12 | |
13 | use Test::More ; |
14 | use CompTestUtils; |
15 | |
16 | BEGIN { |
17 | # use Test::NoWarnings, if available |
18 | my $extra = 0 ; |
19 | $extra = 1 |
20 | if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 }; |
21 | |
258133d1 |
22 | plan tests => 76 + $extra ; |
25f0751f |
23 | |
24 | use_ok('IO::Compress::Base::Common'); |
25f0751f |
25 | } |
26 | |
25f0751f |
27 | # Compress::Zlib::Common; |
28 | |
29 | sub My::testParseParameters() |
30 | { |
31 | eval { ParseParameters(1, {}, 1) ; }; |
32 | like $@, mkErr(': Expected even number of parameters, got 1'), |
33 | "Trap odd number of params"; |
34 | |
35 | eval { ParseParameters(1, {}, undef) ; }; |
36 | like $@, mkErr(': Expected even number of parameters, got 1'), |
37 | "Trap odd number of params"; |
38 | |
39 | eval { ParseParameters(1, {}, []) ; }; |
40 | like $@, mkErr(': Expected even number of parameters, got 1'), |
41 | "Trap odd number of params"; |
42 | |
43 | eval { ParseParameters(1, {'Fred' => [1, 1, Parse_boolean, 0]}, Fred => 'joe') ; }; |
44 | like $@, mkErr("Parameter 'Fred' must be an int, got 'joe'"), |
45 | "wanted unsigned, got undef"; |
46 | |
47 | eval { ParseParameters(1, {'Fred' => [1, 1, Parse_unsigned, 0]}, Fred => undef) ; }; |
48 | like $@, mkErr("Parameter 'Fred' must be an unsigned int, got 'undef'"), |
49 | "wanted unsigned, got undef"; |
50 | |
51 | eval { ParseParameters(1, {'Fred' => [1, 1, Parse_signed, 0]}, Fred => undef) ; }; |
52 | like $@, mkErr("Parameter 'Fred' must be a signed int, got 'undef'"), |
53 | "wanted signed, got undef"; |
54 | |
55 | eval { ParseParameters(1, {'Fred' => [1, 1, Parse_signed, 0]}, Fred => 'abc') ; }; |
56 | like $@, mkErr("Parameter 'Fred' must be a signed int, got 'abc'"), |
57 | "wanted signed, got 'abc'"; |
58 | |
258133d1 |
59 | eval { ParseParameters(1, {'Fred' => [1, 1, Parse_writable_scalar, 0]}, Fred => 'abc') ; }; |
60 | like $@, mkErr("Parameter 'Fred' not writable"), |
61 | "wanted writable, got readonly"; |
62 | |
63 | my @xx; |
64 | eval { ParseParameters(1, {'Fred' => [1, 1, Parse_writable_scalar, 0]}, Fred => \@xx) ; }; |
65 | like $@, mkErr("Parameter 'Fred' not a scalar reference"), |
66 | "wanted scalar reference"; |
67 | |
68 | local *ABC; |
69 | eval { ParseParameters(1, {'Fred' => [1, 1, Parse_writable_scalar, 0]}, Fred => *ABC) ; }; |
70 | like $@, mkErr("Parameter 'Fred' not a scalar"), |
71 | "wanted scalar"; |
25f0751f |
72 | |
258133d1 |
73 | #eval { ParseParameters(1, {'Fred' => [1, 1, Parse_any|Parse_multiple, 0]}, Fred => 1, Fred => 2) ; }; |
74 | #like $@, mkErr("Muliple instances of 'Fred' found"), |
75 | #"wanted scalar"; |
76 | |
77 | ok 1; |
78 | |
79 | my $got = ParseParameters(1, {'Fred' => [1, 1, 0x1000000, 0]}, Fred => 'abc') ; |
25f0751f |
80 | is $got->value('Fred'), "abc", "other" ; |
81 | |
c70c1701 |
82 | $got = ParseParameters(1, {'Fred' => [0, 1, Parse_any, undef]}, Fred => |
83 | undef) ; |
84 | ok $got->parsed('Fred'), "undef" ; |
85 | ok ! defined $got->value('Fred'), "undef" ; |
86 | |
87 | $got = ParseParameters(1, {'Fred' => [0, 1, Parse_string, undef]}, Fred => |
88 | undef) ; |
89 | ok $got->parsed('Fred'), "undef" ; |
90 | is $got->value('Fred'), "", "empty string" ; |
91 | |
258133d1 |
92 | my $xx; |
93 | $got = ParseParameters(1, {'Fred' => [1, 1, Parse_writable_scalar, undef]}, Fred => $xx) ; |
94 | |
95 | ok $got->parsed('Fred'), "parsed" ; |
96 | my $xx_ref = $got->value('Fred'); |
97 | $$xx_ref = 77 ; |
98 | is $xx, 77; |
99 | |
100 | $got = ParseParameters(1, {'Fred' => [1, 1, Parse_writable_scalar, undef]}, Fred => \$xx) ; |
101 | |
102 | ok $got->parsed('Fred'), "parsed" ; |
103 | $xx_ref = $got->value('Fred'); |
104 | $$xx_ref = 666 ; |
105 | is $xx, 666; |
106 | |
25f0751f |
107 | } |
108 | |
109 | My::testParseParameters(); |
110 | |
111 | |
112 | { |
113 | title "isaFilename" ; |
114 | ok isaFilename("abc"), "'abc' isaFilename"; |
115 | |
116 | ok ! isaFilename(undef), "undef ! isaFilename"; |
117 | ok ! isaFilename([]), "[] ! isaFilename"; |
118 | $main::X = 1; $main::X = $main::X ; |
119 | ok ! isaFilename(*X), "glob ! isaFilename"; |
120 | } |
121 | |
122 | { |
123 | title "whatIsInput" ; |
124 | |
125 | my $lex = new LexFile my $out_file ; |
126 | open FH, ">$out_file" ; |
127 | is whatIsInput(*FH), 'handle', "Match filehandle" ; |
128 | close FH ; |
129 | |
130 | my $stdin = '-'; |
131 | is whatIsInput($stdin), 'handle', "Match '-' as stdin"; |
132 | #is $stdin, \*STDIN, "'-' changed to *STDIN"; |
133 | #isa_ok $stdin, 'IO::File', "'-' changed to IO::File"; |
134 | is whatIsInput("abc"), 'filename', "Match filename"; |
135 | is whatIsInput(\"abc"), 'buffer', "Match buffer"; |
136 | is whatIsInput(sub { 1 }, 1), 'code', "Match code"; |
137 | is whatIsInput(sub { 1 }), '' , "Don't match code"; |
138 | |
139 | } |
140 | |
141 | { |
142 | title "whatIsOutput" ; |
143 | |
144 | my $lex = new LexFile my $out_file ; |
145 | open FH, ">$out_file" ; |
146 | is whatIsOutput(*FH), 'handle', "Match filehandle" ; |
147 | close FH ; |
148 | |
149 | my $stdout = '-'; |
150 | is whatIsOutput($stdout), 'handle', "Match '-' as stdout"; |
151 | #is $stdout, \*STDOUT, "'-' changed to *STDOUT"; |
152 | #isa_ok $stdout, 'IO::File', "'-' changed to IO::File"; |
153 | is whatIsOutput("abc"), 'filename', "Match filename"; |
154 | is whatIsOutput(\"abc"), 'buffer', "Match buffer"; |
155 | is whatIsOutput(sub { 1 }, 1), 'code', "Match code"; |
156 | is whatIsOutput(sub { 1 }), '' , "Don't match code"; |
157 | |
158 | } |
e7d45986 |
159 | |
160 | # U64 |
161 | |
162 | { |
163 | title "U64" ; |
164 | |
165 | my $x = new U64(); |
166 | is $x->getHigh, 0, " getHigh is 0"; |
167 | is $x->getLow, 0, " getLow is 0"; |
168 | |
169 | $x = new U64(1,2); |
170 | $x = new U64(1,2); |
171 | is $x->getHigh, 1, " getHigh is 1"; |
172 | is $x->getLow, 2, " getLow is 2"; |
173 | |
174 | $x = new U64(0xFFFFFFFF,2); |
175 | is $x->getHigh, 0xFFFFFFFF, " getHigh is 0xFFFFFFFF"; |
176 | is $x->getLow, 2, " getLow is 2"; |
177 | |
178 | $x = new U64(7, 0xFFFFFFFF); |
179 | is $x->getHigh, 7, " getHigh is 7"; |
180 | is $x->getLow, 0xFFFFFFFF, " getLow is 0xFFFFFFFF"; |
181 | |
182 | $x = new U64(666); |
183 | is $x->getHigh, 0, " getHigh is 0"; |
184 | is $x->getLow, 666, " getLow is 666"; |
185 | |
186 | title "U64 - add" ; |
187 | |
188 | $x = new U64(0, 1); |
189 | is $x->getHigh, 0, " getHigh is 0"; |
190 | is $x->getLow, 1, " getLow is 1"; |
191 | |
192 | $x->add(1); |
193 | is $x->getHigh, 0, " getHigh is 0"; |
194 | is $x->getLow, 2, " getLow is 2"; |
195 | |
196 | $x = new U64(0, 0xFFFFFFFE); |
197 | is $x->getHigh, 0, " getHigh is 0"; |
198 | is $x->getLow, 0xFFFFFFFE, " getLow is 0xFFFFFFFE"; |
199 | |
200 | $x->add(1); |
201 | is $x->getHigh, 0, " getHigh is 0"; |
202 | is $x->getLow, 0xFFFFFFFF, " getLow is 0xFFFFFFFF"; |
203 | |
204 | $x->add(1); |
205 | is $x->getHigh, 1, " getHigh is 1"; |
206 | is $x->getLow, 0, " getLow is 0"; |
207 | |
208 | $x->add(1); |
209 | is $x->getHigh, 1, " getHigh is 1"; |
210 | is $x->getLow, 1, " getLow is 1"; |
211 | |
212 | $x = new U64(1, 0xFFFFFFFE); |
213 | my $y = new U64(2, 3); |
214 | |
215 | $x->add($y); |
216 | is $x->getHigh, 4, " getHigh is 4"; |
217 | is $x->getLow, 1, " getLow is 1"; |
218 | |
219 | title "U64 - equal" ; |
220 | |
221 | $x = new U64(0, 1); |
222 | is $x->getHigh, 0, " getHigh is 0"; |
223 | is $x->getLow, 1, " getLow is 1"; |
224 | |
225 | $y = new U64(0, 1); |
226 | is $x->getHigh, 0, " getHigh is 0"; |
227 | is $x->getLow, 1, " getLow is 1"; |
228 | |
229 | my $z = new U64(0, 2); |
230 | is $x->getHigh, 0, " getHigh is 0"; |
231 | is $x->getLow, 1, " getLow is 1"; |
232 | |
233 | ok $x->equal($y), " equal"; |
234 | ok !$x->equal($z), " ! equal"; |
235 | |
236 | title "U64 - pack_V" ; |
237 | } |