Commit | Line | Data |
d50dd4e4 |
1 | #!./perl -w |
a687059c |
2 | |
a1a0e61e |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
20822f61 |
5 | @INC = '../lib'; |
0b568b5f |
6 | require './test.pl'; |
b23b8711 |
7 | } |
8 | |
72b034c3 |
9 | plan tests => 1476; |
0b568b5f |
10 | |
fa8ec7c1 |
11 | use strict; |
12 | use warnings; |
b23b8711 |
13 | use Config; |
14 | |
fa8ec7c1 |
15 | my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define'); |
0b568b5f |
16 | my $Perl = which_perl(); |
c274e827 |
17 | |
b85d93de |
18 | sub encode_list { |
9e17a6b8 |
19 | my @result = map {_qq($_)} @_; |
b85d93de |
20 | if (@result == 1) { |
21 | return @result; |
22 | } |
23 | return '(' . join (', ', @result) . ')'; |
24 | } |
25 | |
a1a0e61e |
26 | |
b85d93de |
27 | sub list_eq ($$) { |
28 | my ($l, $r) = @_; |
29 | return unless @$l == @$r; |
30 | for my $i (0..$#$l) { |
31 | if (defined $l->[$i]) { |
32 | return unless defined ($r->[$i]) && $l->[$i] eq $r->[$i]; |
33 | } else { |
34 | return if defined $r->[$i] |
35 | } |
36 | } |
37 | return 1; |
38 | } |
39 | |
40 | ############################################################################## |
41 | # |
42 | # Here starteth the tests |
43 | # |
44 | |
fa8ec7c1 |
45 | { |
0b568b5f |
46 | my $format = "c2 x5 C C x s d i l a6"; |
47 | # Need the expression in here to force ary[5] to be numeric. This avoids |
48 | # test2 failing because ary2 goes str->numeric->str and ary doesn't. |
49 | my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456, |
50 | "abcdef"); |
51 | my $foo = pack($format,@ary); |
52 | my @ary2 = unpack($format,$foo); |
53 | |
54 | is($#ary, $#ary2); |
55 | |
56 | my $out1=join(':',@ary); |
57 | my $out2=join(':',@ary2); |
58 | # Using long double NVs may introduce greater accuracy than wanted. |
59 | $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/; |
60 | $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/; |
61 | is($out1, $out2); |
62 | |
63 | like($foo, qr/def/); |
fa8ec7c1 |
64 | } |
79072805 |
65 | # How about counting bits? |
66 | |
fa8ec7c1 |
67 | { |
0b568b5f |
68 | my $x; |
69 | is( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")), 16 ); |
79072805 |
70 | |
0b568b5f |
71 | is( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")), 12 ); |
79072805 |
72 | |
0b568b5f |
73 | is( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")), 9 ); |
fa8ec7c1 |
74 | } |
79072805 |
75 | |
fa8ec7c1 |
76 | { |
0b568b5f |
77 | my $sum = 129; # ASCII |
78 | $sum = 103 if $Is_EBCDIC; |
9d116dd7 |
79 | |
0b568b5f |
80 | my $x; |
81 | is( ($x = unpack("%32B*", "Now is the time for all good blurfl")), $sum ); |
79072805 |
82 | |
0b568b5f |
83 | my $foo; |
84 | open(BIN, $Perl) || die "Can't open $Perl: $!\n"; |
85 | sysread BIN, $foo, 8192; |
86 | close BIN; |
79072805 |
87 | |
0b568b5f |
88 | $sum = unpack("%32b*", $foo); |
89 | my $longway = unpack("b*", $foo); |
90 | is( $sum, $longway =~ tr/1/1/ ); |
fa8ec7c1 |
91 | } |
73a1c01a |
92 | |
fa8ec7c1 |
93 | { |
94 | my $x; |
0b568b5f |
95 | is( ($x = unpack("I",pack("I", 0xFFFFFFFF))), 0xFFFFFFFF ); |
fa8ec7c1 |
96 | } |
def98dd4 |
97 | |
fa8ec7c1 |
98 | { |
0b568b5f |
99 | # check 'w' |
100 | my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33, |
101 | '4503599627365785','23728385234614992549757750638446'); |
102 | my $x = pack('w*', @x); |
103 | my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A0808'. |
104 | '0800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e'; |
105 | |
106 | is($x, $y); |
107 | |
108 | my @y = unpack('w*', $y); |
109 | my $a; |
110 | while ($a = pop @x) { |
111 | my $b = pop @y; |
112 | is($a, $b); |
113 | } |
def98dd4 |
114 | |
0b568b5f |
115 | @y = unpack('w2', $x); |
def98dd4 |
116 | |
0b568b5f |
117 | is(scalar(@y), 2); |
118 | is($y[1], 130); |
fa8ec7c1 |
119 | } |
def98dd4 |
120 | |
0b568b5f |
121 | |
fa8ec7c1 |
122 | { |
123 | # test exeptions |
124 | my $x; |
125 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff}; |
0b568b5f |
126 | like($@, qr/^Unterminated compressed integer/); |
def98dd4 |
127 | |
fa8ec7c1 |
128 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff}; |
0b568b5f |
129 | like($@, qr/^Unterminated compressed integer/); |
def98dd4 |
130 | |
fa8ec7c1 |
131 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
0b568b5f |
132 | like($@, qr/^Unterminated compressed integer/); |
fa8ec7c1 |
133 | } |
def98dd4 |
134 | |
84902520 |
135 | # |
136 | # test the "p" template |
137 | |
138 | # literals |
0b568b5f |
139 | is(unpack("p",pack("p","foo")), "foo"); |
84902520 |
140 | |
141 | # scalars |
0b568b5f |
142 | is(unpack("p",pack("p",239)), 239); |
84902520 |
143 | |
144 | # temps |
145 | sub foo { my $a = "a"; return $a . $a++ . $a++ } |
146 | { |
9f1b1f2d |
147 | use warnings; |
0b568b5f |
148 | my $warning; |
84902520 |
149 | local $SIG{__WARN__} = sub { |
0b568b5f |
150 | $warning = $_[0]; |
84902520 |
151 | }; |
152 | my $junk = pack("p", &foo); |
0b568b5f |
153 | |
154 | like($warning, qr/temporary val/); |
84902520 |
155 | } |
156 | |
157 | # undef should give null pointer |
0b568b5f |
158 | like(pack("p", undef), qr/^\0+/); |
84902520 |
159 | |
20408e3c |
160 | # Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives |
161 | # 4294967295 instead of -1) |
162 | # see #ifdef __osf__ in pp.c pp_unpack |
0b568b5f |
163 | is((unpack("i",pack("i",-1))), -1); |
20408e3c |
164 | |
0b568b5f |
165 | # test the pack lengths of s S i I l L |
166 | # test the pack lengths of n N v V |
fa8ec7c1 |
167 | my @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4); |
168 | while (my ($format, $expect) = splice @lengths, 0, 2) { |
169 | my $len = length(pack($format, 0)); |
170 | if ($expect > 0) { |
0b568b5f |
171 | is($expect, $len, "format '$format'"); |
fa8ec7c1 |
172 | } else { |
173 | $expect = -$expect; |
0b568b5f |
174 | ok ($len >= $expect, "format '$format'") || |
175 | print "# format '$format' has length $len, expected >= $expect\n"; |
fa8ec7c1 |
176 | } |
177 | } |
d4217c7e |
178 | |
d4217c7e |
179 | |
0b568b5f |
180 | # test unpack-pack lengths |
181 | my @templates = qw(c C i I s S l L n N v V f d q Q); |
d4217c7e |
182 | |
183 | foreach my $t (@templates) { |
0b568b5f |
184 | SKIP: { |
185 | my @t = eval { unpack("$t*", pack("$t*", 12, 34)) }; |
186 | |
187 | # quads not supported everywhere |
188 | skip "Quads not supported", 4 if $@ =~ /Invalid type in pack/; |
189 | is( $@, '' ); |
190 | |
191 | is(scalar @t, 2); |
3020ec7a |
192 | |
193 | SKIP: { |
194 | skip "$t not expected to work for some reason", 2 if $t =~ /[nv]/i; |
195 | |
0b568b5f |
196 | is($t[0], 12); |
197 | is($t[1], 34); |
198 | } |
0b568b5f |
199 | } |
d4217c7e |
200 | } |
9d116dd7 |
201 | |
fa8ec7c1 |
202 | { |
0b568b5f |
203 | # uuencode/decode |
9d116dd7 |
204 | |
0b568b5f |
205 | # Note that first uuencoding known 'text' data and then checking the |
206 | # binary values of the uuencoded version would not be portable between |
207 | # character sets. Uuencoding is meant for encoding binary data, not |
208 | # text data. |
c4d5f83a |
209 | |
0b568b5f |
210 | my $in = pack 'C*', 0 .. 255; |
ba1ac976 |
211 | |
0b568b5f |
212 | # just to be anal, we do some random tr/`/ / |
213 | my $uu = <<'EOUU'; |
ba1ac976 |
214 | M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL |
9d116dd7 |
215 | M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9 |
216 | M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6& |
217 | MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S |
218 | MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@ |
ba1ac976 |
219 | ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P ` |
9d116dd7 |
220 | EOUU |
221 | |
0b568b5f |
222 | $_ = $uu; |
223 | tr/ /`/; |
9d116dd7 |
224 | |
0b568b5f |
225 | is(pack('u', $in), $_); |
fa8ec7c1 |
226 | |
0b568b5f |
227 | is(unpack('u', $uu), $in); |
9d116dd7 |
228 | |
0b568b5f |
229 | $in = "\x1f\x8b\x08\x08\x58\xdc\xc4\x35\x02\x03\x4a\x41\x50\x55\x00\xf3\x2a\x2d\x2e\x51\x48\xcc\xcb\x2f\xc9\x48\x2d\x52\x08\x48\x2d\xca\x51\x28\x2d\x4d\xce\x4f\x49\x2d\xe2\x02\x00\x64\x66\x60\x5c\x1a\x00\x00\x00"; |
230 | $uu = <<'EOUU'; |
eddc390b |
231 | M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F |
232 | &8%P:```` |
233 | EOUU |
234 | |
0b568b5f |
235 | is(unpack('u', $uu), $in); |
eddc390b |
236 | |
0b568b5f |
237 | # This is identical to the above except that backquotes have been |
238 | # changed to spaces |
eddc390b |
239 | |
0b568b5f |
240 | $uu = <<'EOUU'; |
eddc390b |
241 | M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F |
c4d5f83a |
242 | &8%P: |
eddc390b |
243 | EOUU |
244 | |
0b568b5f |
245 | # ' # Grr |
246 | is(unpack('u', $uu), $in); |
ef54e1a4 |
247 | |
d99ad34e |
248 | } |
726ea183 |
249 | |
0b568b5f |
250 | # test the ascii template types (A, a, Z) |
726ea183 |
251 | |
fa8ec7c1 |
252 | foreach ( |
0b568b5f |
253 | ['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "], |
fa8ec7c1 |
254 | ['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "], |
0b568b5f |
255 | ['u', 'A*', "foo\0bar \0", "foo\0bar"], |
256 | ['u', 'A8', "foo\0bar \0", "foo\0bar"], |
257 | ['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "], |
fa8ec7c1 |
258 | ['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"], |
0b568b5f |
259 | ['u', 'a*', "foo\0bar \0", "foo\0bar \0"], |
260 | ['u', 'a8', "foo\0bar \0", "foo\0bar "], |
261 | ['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"], |
fa8ec7c1 |
262 | ['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"], |
0b568b5f |
263 | ['p', 'Z3', "foo", "fo\0"], |
264 | ['u', 'Z*', "foo\0bar \0", "foo"], |
265 | ['u', 'Z8', "foo\0bar \0", "foo"], |
266 | ) |
267 | { |
268 | my ($what, $template, $in, $out) = @$_; |
269 | my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in); |
270 | unless (is($got, $out)) { |
0b568b5f |
271 | my $un = $what eq 'u' ? 'un' : ''; |
9e17a6b8 |
272 | print "# ${un}pack ('$template', "._qq($in).') gave '._qq($out). |
273 | ' not '._qq($got)."\n"; |
0b568b5f |
274 | } |
d99ad34e |
275 | } |
726ea183 |
276 | |
0b568b5f |
277 | # packing native shorts/ints/longs |
726ea183 |
278 | |
0b568b5f |
279 | is(length(pack("s!", 0)), $Config{shortsize}); |
280 | is(length(pack("i!", 0)), $Config{intsize}); |
281 | is(length(pack("l!", 0)), $Config{longsize}); |
282 | ok(length(pack("s!", 0)) <= length(pack("i!", 0))); |
283 | ok(length(pack("i!", 0)) <= length(pack("l!", 0))); |
284 | is(length(pack("i!", 0)), length(pack("i", 0))); |
726ea183 |
285 | |
fa8ec7c1 |
286 | sub numbers { |
287 | my $format = shift; |
288 | return numbers_with_total ($format, undef, @_); |
d99ad34e |
289 | } |
726ea183 |
290 | |
fa8ec7c1 |
291 | sub numbers_with_total { |
292 | my $format = shift; |
293 | my $total = shift; |
294 | if (!defined $total) { |
295 | foreach (@_) { |
296 | $total += $_; |
297 | } |
298 | } |
299 | foreach (@_) { |
0b568b5f |
300 | SKIP: { |
301 | my $out = eval {unpack($format, pack($format, $_))}; |
302 | skip "cannot pack '$format' on this perl", 2 if |
303 | $@ =~ /Invalid type in pack: '$format'/; |
304 | |
305 | is($@, ''); |
306 | is($out, $_); |
fa8ec7c1 |
307 | } |
fa8ec7c1 |
308 | } |
726ea183 |
309 | |
fa8ec7c1 |
310 | my $skip_if_longer_than = ~0; # "Infinity" |
311 | if (~0 - 1 == ~0) { |
312 | # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all |
313 | # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't |
314 | # correctly in perl calculate UV totals for long checksums, as pp_unpack |
315 | # is using UV maths, and we've only got NVs. |
316 | $skip_if_longer_than = $Config{d_nv_preserves_uv_bits}; |
317 | } |
726ea183 |
318 | |
fa8ec7c1 |
319 | foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) { |
0b568b5f |
320 | SKIP: { |
321 | my $sum = eval {unpack "%$_$format*", pack "$format*", @_}; |
322 | skip "cannot pack '$format' on this perl", 3 |
323 | if $@ =~ /Invalid type in pack: '$format'/; |
324 | |
325 | is($@, ''); |
326 | ok(defined $sum); |
327 | |
328 | my $len = $_; # Copy, so that we can reassign '' |
329 | $len = 16 unless length $len; |
330 | |
331 | SKIP: { |
332 | skip "cannot test checksums over $skip_if_longer_than bits", 1 |
333 | if $len > $skip_if_longer_than; |
334 | |
335 | # Our problem with testing this portably is that the checksum code in |
336 | # pp_unpack is able to cast signed to unsigned, and do modulo 2**n |
337 | # arithmetic in unsigned ints, which perl has no operators to do. |
338 | # (use integer; does signed ints, which won't wrap on UTS, which is just |
339 | # fine with ANSI, but not with most people's assumptions. |
340 | # This is why we need to supply the totals for 'Q' as there's no way in |
341 | # perl to calculate them, short of unpack '%0Q' (is that documented?) |
342 | # ** returns NVs; make sure it's IV. |
343 | my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum |
344 | my $max_p1 = $max + 1; |
345 | my ($max_is_integer, $max_p1_is_integer); |
346 | $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1; |
347 | $max_is_integer = 1 if $max - 1 < ~0; |
348 | |
349 | my $calc_sum; |
350 | if (ref $total) { |
351 | $calc_sum = &$total($len); |
352 | } else { |
353 | $calc_sum = $total; |
354 | # Shift into range by some multiple of the total |
355 | my $mult = int ($total / $max_p1); |
356 | # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer) |
357 | $calc_sum = $total - $mult; |
358 | $calc_sum -= $mult * $max; |
359 | if ($calc_sum < 0) { |
360 | $calc_sum += 1; |
361 | $calc_sum += $max; |
362 | } |
363 | } |
364 | if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) { |
365 | # we're into floating point (either by getting out of the range of |
366 | # UV arithmetic, or because we're doing a floating point checksum) |
367 | # and our calculation of the checksum has become rounded up to |
368 | # max_checksum + 1 |
369 | $calc_sum = 0; |
370 | } |
371 | |
0dccb3d1 |
372 | if ($calc_sum == $sum) { # HAS to be ==, not eq (so no is()). |
373 | ok ("unpack '%$_$format' gave $sum"); |
0b568b5f |
374 | } else { |
375 | my $delta = 1.000001; |
376 | if ($format =~ tr /dDfF// |
377 | && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) { |
0dccb3d1 |
378 | pass ("unpack '%$_$format' gave $sum, expected $calc_sum"); |
0b568b5f |
379 | } else { |
380 | my $text = ref $total ? &$total($len) : $total; |
381 | fail; |
382 | print "# For list (" . join (", ", @_) . ") (total $text)" |
383 | . " packed with $format unpack '%$_$format' gave $sum," |
384 | . " expected $calc_sum\n"; |
385 | } |
386 | } |
fa8ec7c1 |
387 | } |
0b568b5f |
388 | } |
2e821511 |
389 | } |
390 | } |
391 | |
fa8ec7c1 |
392 | numbers ('c', -128, -1, 0, 1, 127); |
393 | numbers ('C', 0, 1, 127, 128, 255); |
394 | numbers ('s', -32768, -1, 0, 1, 32767); |
395 | numbers ('S', 0, 1, 32767, 32768, 65535); |
396 | numbers ('i', -2147483648, -1, 0, 1, 2147483647); |
397 | numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295); |
398 | numbers ('l', -2147483648, -1, 0, 1, 2147483647); |
399 | numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295); |
400 | numbers ('s!', -32768, -1, 0, 1, 32767); |
401 | numbers ('S!', 0, 1, 32767, 32768, 65535); |
402 | numbers ('i!', -2147483648, -1, 0, 1, 2147483647); |
403 | numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295); |
404 | numbers ('l!', -2147483648, -1, 0, 1, 2147483647); |
405 | numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295); |
406 | numbers ('n', 0, 1, 32767, 32768, 65535); |
407 | numbers ('v', 0, 1, 32767, 32768, 65535); |
408 | numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295); |
409 | numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295); |
410 | # All these should have exact binary representations: |
411 | numbers ('f', -1, 0, 0.5, 42, 2**34); |
b85d93de |
412 | numbers ('d', -(2**34), -1, 0, 1, 2**34); |
413 | ## These don't, but 'd' is NV. XXX wrong, it's double |
414 | #numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1)); |
fa8ec7c1 |
415 | |
416 | numbers_with_total ('q', -1, |
417 | -9223372036854775808, -1, 0, 1,9223372036854775807); |
800e6488 |
418 | # This total is icky, but the true total is 2**65-1, and need a way to generate |
419 | # the epxected checksum on any system including those where NVs can preserve |
420 | # 65 bits. (long double is 128 bits on sparc, so they certainly can) |
421 | # or where rounding is down not up on binary conversion (crays) |
422 | numbers_with_total ('Q', sub { |
423 | my $len = shift; |
424 | $len = 65 if $len > 65; # unmasked total is 2**65-1 here |
425 | my $total = 1 + 2 * (int (2**($len - 1)) - 1); |
426 | return 0 if $total == $total - 1; # Overflowed integers |
427 | return $total; # NVs still accurate to nearest integer |
428 | }, |
fa8ec7c1 |
429 | 0, 1,9223372036854775807, 9223372036854775808, |
430 | 18446744073709551615); |
431 | |
432 | # pack nvNV byteorders |
433 | |
0b568b5f |
434 | is(pack("n", 0xdead), "\xde\xad"); |
435 | is(pack("v", 0xdead), "\xad\xde"); |
436 | is(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef"); |
437 | is(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde"); |
43192e07 |
438 | |
fa8ec7c1 |
439 | { |
440 | # / |
441 | |
442 | my ($x, $y, $z); |
443 | eval { ($x) = unpack '/a*','hello' }; |
0b568b5f |
444 | like($@, qr!/ must follow a numeric type!); |
72b034c3 |
445 | undef $x; |
446 | eval { $x = unpack '/a*','hello' }; |
447 | like($@, qr!/ must follow a numeric type!); |
0b568b5f |
448 | |
72b034c3 |
449 | undef $x; |
fa8ec7c1 |
450 | eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" }; |
0b568b5f |
451 | is($@, ''); |
452 | is($z, 'ok'); |
453 | is($x, 'yes'); |
454 | is($y, 'z'); |
72b034c3 |
455 | undef $z; |
456 | eval { $z = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" }; |
457 | is($@, ''); |
458 | is($z, 'ok'); |
459 | |
fa8ec7c1 |
460 | |
72b034c3 |
461 | undef $x; |
fa8ec7c1 |
462 | eval { ($x) = pack '/a*','hello' }; |
0b568b5f |
463 | like($@, qr!Invalid type in pack: '/'!); |
72b034c3 |
464 | undef $x; |
465 | eval { $x = pack '/a*','hello' }; |
466 | like($@, qr!Invalid type in pack: '/'!); |
fa8ec7c1 |
467 | |
468 | $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc'; |
469 | my $expect = "\000\006string\0\0\0\012hi there \000\003etc"; |
0b568b5f |
470 | is($z, $expect); |
4b5b2118 |
471 | |
72b034c3 |
472 | undef $x; |
b85d93de |
473 | $expect = 'hello world'; |
474 | eval { ($x) = unpack ("w/a", chr (11) . "hello world!")}; |
0b568b5f |
475 | is($x, $expect); |
476 | is($@, ''); |
477 | |
72b034c3 |
478 | undef $x; |
b85d93de |
479 | # Doing this in scalar context used to fail. |
480 | eval { $x = unpack ("w/a", chr (11) . "hello world!")}; |
0b568b5f |
481 | is($@, ''); |
482 | is($x, $expect); |
b85d93de |
483 | |
fa8ec7c1 |
484 | foreach ( |
0b568b5f |
485 | ['a/a*/a*', '212ab345678901234567','ab3456789012'], |
486 | ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'], |
487 | ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'], |
488 | ) |
489 | { |
fa8ec7c1 |
490 | my ($pat, $in, $expect) = @$_; |
72b034c3 |
491 | undef $x; |
fa8ec7c1 |
492 | eval { ($x) = unpack $pat, $in }; |
0b568b5f |
493 | is($@, ''); |
494 | is($x, $expect) || |
495 | printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n", |
496 | encode_list ($x); |
497 | |
72b034c3 |
498 | undef $x; |
b85d93de |
499 | eval { $x = unpack $pat, $in }; |
0b568b5f |
500 | is($@, ''); |
501 | is($x, $expect) || |
502 | printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n", |
503 | encode_list ($x); |
fa8ec7c1 |
504 | } |
4b5b2118 |
505 | |
0b568b5f |
506 | # / with # |
17f4a12d |
507 | |
72b034c3 |
508 | my $pattern = <<'EOU'; |
17f4a12d |
509 | a3/A # Count in ASCII |
510 | C/a* # Count in a C char |
511 | C/Z # Count in a C char but skip after \0 |
512 | EOU |
17f4a12d |
513 | |
72b034c3 |
514 | $x = $y = $z =undef; |
515 | eval { ($z,$x,$y) = unpack $pattern, "003ok \003yes\004z\000abc" }; |
0b568b5f |
516 | is($@, ''); |
517 | is($z, 'ok'); |
518 | is($x, 'yes'); |
519 | is($y, 'z'); |
72b034c3 |
520 | undef $x; |
521 | eval { $z = unpack $pattern, "003ok \003yes\004z\000abc" }; |
522 | is($@, ''); |
523 | is($z, 'ok'); |
0b568b5f |
524 | |
72b034c3 |
525 | $pattern = <<'EOP'; |
17f4a12d |
526 | n/a* # Count as network short |
527 | w/A* # Count a BER integer |
528 | EOP |
fa8ec7c1 |
529 | $expect = "\000\006string\003etc"; |
72b034c3 |
530 | $z = pack $pattern,'string','etc'; |
9e17a6b8 |
531 | is($z, $expect); |
fa8ec7c1 |
532 | } |
533 | |
0b568b5f |
534 | is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000)); |
535 | is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000)); |
536 | isnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000)); |
fa8ec7c1 |
537 | |
0b568b5f |
538 | my $rslt = $Is_EBCDIC ? "156 67" : "199 162"; |
539 | is(join(" ", unpack("C*", chr(0x1e2))), $rslt); |
fa8ec7c1 |
540 | |
541 | # does pack U create Unicode? |
0b568b5f |
542 | is(ord(pack('U', 300)), 300); |
fa8ec7c1 |
543 | |
544 | # does unpack U deref Unicode? |
0b568b5f |
545 | is((unpack('U', chr(300)))[0], 300); |
fa8ec7c1 |
546 | |
547 | # is unpack U the reverse of pack U for Unicode string? |
0b568b5f |
548 | is("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300"); |
fa8ec7c1 |
549 | |
550 | # is unpack U the reverse of pack U for byte string? |
0b568b5f |
551 | is("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200"); |
552 | |
553 | |
554 | SKIP: { |
555 | skip "Not for EBCDIC", 4 if $Is_EBCDIC; |
fa8ec7c1 |
556 | |
7eed2ccc |
557 | # does unpack C unravel pack U? |
0b568b5f |
558 | is("@{[unpack('C*', pack('U*', 100, 200))]}", "100 195 136"); |
fa8ec7c1 |
559 | |
7eed2ccc |
560 | # does pack U0C create Unicode? |
0b568b5f |
561 | is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200); |
fa8ec7c1 |
562 | |
7eed2ccc |
563 | # does pack C0U create characters? |
0b568b5f |
564 | is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136)); |
fa8ec7c1 |
565 | |
2dcec3fe |
566 | # does unpack U0U on byte data warn? |
567 | { |
568 | local $SIG{__WARN__} = sub { $@ = "@_" }; |
569 | my @null = unpack('U0U', chr(255)); |
0b568b5f |
570 | like($@, /^Malformed UTF-8 character /); |
2dcec3fe |
571 | } |
35bcd338 |
572 | } |
573 | |
fa8ec7c1 |
574 | { |
575 | my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647; |
576 | my (@a); |
577 | # bug - % had to be at the start of the pattern, no leading whitespace or |
578 | # comments. %i! didn't work at all. |
579 | foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ', |
580 | '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') { |
581 | @a = unpack $pat, $p; |
0b568b5f |
582 | is($a[0], 0xFFFFFFFF) || print "# $pat\n"; |
fa8ec7c1 |
583 | @a = scalar unpack $pat, $p; |
0b568b5f |
584 | is($a[0], 0xFFFFFFFF) || print "# $pat\n"; |
fa8ec7c1 |
585 | } |
586 | |
587 | |
588 | $p = pack 'I*', 42, 12; |
589 | # Multiline patterns in scalar context failed. |
590 | foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') { |
591 | # On the Ning Nang Nong |
592 | # Where the Cows go Bong! |
593 | # And the Monkeys all say Boo! |
594 | I |
595 | EOPOEMSNIPPET |
0b568b5f |
596 | @a = unpack $pat, $p; |
597 | is(scalar @a, 1); |
598 | is($a[0], 42); |
599 | @a = scalar unpack $pat, $p; |
600 | is(scalar @a, 1); |
601 | is($a[0], 42); |
602 | } |
fa8ec7c1 |
603 | |
604 | # shorts (of all flavours) didn't calculate checksums > 32 bits with floating |
605 | # point, so a pathologically long pattern would wrap at 32 bits. |
606 | my $pat = "\xff\xff"x65538; # Start with it long, to save any copying. |
607 | foreach (4,3,2,1,0) { |
608 | my $len = 65534 + $_; |
0b568b5f |
609 | is(unpack ("%33n$len", $pat), 65535 * $len); |
fa8ec7c1 |
610 | } |
611 | } |
b85d93de |
612 | |
613 | |
614 | # pack x X @ |
615 | foreach ( |
0b568b5f |
616 | ['x', "N", "\0"], |
617 | ['x4', "N", "\0"x4], |
618 | ['xX', "N", ""], |
619 | ['xXa*', "Nick", "Nick"], |
620 | ['a5Xa5', "cameL", "llama", "camellama"], |
621 | ['@4', 'N', "\0"x4], |
622 | ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"], |
623 | ['a*@4a', 'Perl rules', '!', 'Perl!'], |
624 | ) |
625 | { |
b85d93de |
626 | my ($template, @in) = @$_; |
627 | my $out = pop @in; |
628 | my $got = eval {pack $template, @in}; |
0b568b5f |
629 | is($@, ''); |
630 | is($out, $got) || |
631 | printf "# pack ('$template', %s) gave %s expected %s\n", |
632 | encode_list (@in), encode_list ($got), encode_list ($out); |
b85d93de |
633 | } |
634 | |
635 | # unpack x X @ |
636 | foreach ( |
0b568b5f |
637 | ['x', "N"], |
638 | ['xX', "N"], |
639 | ['xXa*', "Nick", "Nick"], |
640 | ['a5Xa5', "camellama", "camel", "llama"], |
641 | ['@3', "ice"], |
642 | ['@2a2', "water", "te"], |
643 | ['a*@1a3', "steam", "steam", "tea"], |
644 | ) |
645 | { |
b85d93de |
646 | my ($template, $in, @out) = @$_; |
647 | my @got = eval {unpack $template, $in}; |
0b568b5f |
648 | is($@, ''); |
649 | list_eq (\@got, \@out) || |
9e17a6b8 |
650 | printf "# list unpack ('$template', %s) gave %s expected %s\n", |
651 | _qq($in), encode_list (@got), encode_list (@out); |
b85d93de |
652 | |
653 | my $got = eval {unpack $template, $in}; |
0b568b5f |
654 | is($@, ''); |
655 | @out ? is( $got, $out[0] ) # 1 or more items; should get first |
656 | : ok( !defined $got ) # 0 items; should get undef |
9e17a6b8 |
657 | or printf "# scalar unpack ('$template', %s) gave %s expected %s\n", |
658 | _qq($in), encode_list ($got), encode_list ($out[0]); |
b85d93de |
659 | } |
d50dd4e4 |
660 | |
661 | { |
d50dd4e4 |
662 | my $t = 'Z*Z*'; |
663 | my ($u, $v) = qw(foo xyzzy); |
664 | my $p = pack($t, $u, $v); |
665 | my @u = unpack($t, $p); |
0b568b5f |
666 | is(scalar @u, 2); |
667 | is($u[0], $u); |
668 | is($u[1], $v); |
d50dd4e4 |
669 | } |
bf80f5a2 |
670 | |
671 | { |
0b568b5f |
672 | is((unpack("w/a*", "\x02abc"))[0], "ab"); |
bf80f5a2 |
673 | |
0b568b5f |
674 | # "w/a*" should be seen as one unit |
bf80f5a2 |
675 | |
0b568b5f |
676 | is(scalar unpack("w/a*", "\x02abc"), "ab"); |
bf80f5a2 |
677 | } |
b81060d6 |
678 | |
679 | { |
b81060d6 |
680 | # from Wolfgang Laun: fix in change #13163 |
681 | |
682 | my $s = 'ABC' x 10; |
683 | my $x = 42; |
684 | my $buf = pack( 'Z*/A* C', $s, $x ); |
685 | my $y; |
686 | |
687 | my $h = $buf; |
688 | $h =~ s/[^[:print:]]/./g; |
689 | ( $s, $y ) = unpack( "Z*/A* C", $buf ); |
0b568b5f |
690 | is($h, "30.ABCABCABCABCABCABCABCABCABCABC*"); |
691 | is(length $buf, 34); |
692 | is($s, "ABCABCABCABCABCABCABCABCABCABC"); |
693 | is($y, 42); |
b81060d6 |
694 | } |
3bf38418 |
695 | |
696 | { |
b223308b |
697 | # from Wolfgang Laun: fix in change #13288 |
3bf38418 |
698 | |
b223308b |
699 | eval { my $t=unpack("P*", "abc") }; |
0b568b5f |
700 | like($@, qr/P must have an explicit size/); |
3bf38418 |
701 | } |