Commit | Line | Data |
---|---|---|
d50dd4e4 | 1 | #!./perl -w |
a687059c | 2 | |
a1a0e61e TD |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
20822f61 | 5 | @INC = '../lib'; |
0b568b5f | 6 | require './test.pl'; |
b23b8711 MS |
7 | } |
8 | ||
49704364 | 9 | plan tests => 5849; |
0b568b5f | 10 | |
fa8ec7c1 NC |
11 | use strict; |
12 | use warnings; | |
b23b8711 MS |
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 NC |
20 | if (@result == 1) { |
21 | return @result; | |
22 | } | |
23 | return '(' . join (', ', @result) . ')'; | |
24 | } | |
25 | ||
a1a0e61e | 26 | |
b85d93de NC |
27 | sub list_eq ($$) { |
28 | my ($l, $r) = @_; | |
c8f824eb | 29 | return 0 unless @$l == @$r; |
b85d93de NC |
30 | for my $i (0..$#$l) { |
31 | if (defined $l->[$i]) { | |
c8f824eb | 32 | return 0 unless defined ($r->[$i]) && $l->[$i] eq $r->[$i]; |
b85d93de | 33 | } else { |
c8f824eb | 34 | return 0 if defined $r->[$i] |
b85d93de NC |
35 | } |
36 | } | |
37 | return 1; | |
38 | } | |
39 | ||
40 | ############################################################################## | |
41 | # | |
42 | # Here starteth the tests | |
43 | # | |
44 | ||
fa8ec7c1 | 45 | { |
0b568b5f MS |
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 LW |
65 | # How about counting bits? |
66 | ||
fa8ec7c1 | 67 | { |
0b568b5f MS |
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 MS |
77 | my $sum = 129; # ASCII |
78 | $sum = 103 if $Is_EBCDIC; | |
9d116dd7 | 79 | |
0b568b5f MS |
80 | my $x; |
81 | is( ($x = unpack("%32B*", "Now is the time for all good blurfl")), $sum ); | |
79072805 | 82 | |
0b568b5f MS |
83 | my $foo; |
84 | open(BIN, $Perl) || die "Can't open $Perl: $!\n"; | |
85 | sysread BIN, $foo, 8192; | |
86 | close BIN; | |
79072805 | 87 | |
0b568b5f MS |
88 | $sum = unpack("%32b*", $foo); |
89 | my $longway = unpack("b*", $foo); | |
90 | is( $sum, $longway =~ tr/1/1/ ); | |
fa8ec7c1 | 91 | } |
73a1c01a | 92 | |
fa8ec7c1 NC |
93 | { |
94 | my $x; | |
0b568b5f | 95 | is( ($x = unpack("I",pack("I", 0xFFFFFFFF))), 0xFFFFFFFF ); |
fa8ec7c1 | 96 | } |
def98dd4 | 97 | |
fa8ec7c1 | 98 | { |
0b568b5f MS |
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 MS |
117 | is(scalar(@y), 2); |
118 | is($y[1], 130); | |
a6c48a57 UP |
119 | $x = pack('w*', 5000000000); $y = ''; |
120 | eval { | |
121 | use Math::BigInt; | |
122 | $y = pack('w*', Math::BigInt::->new(5000000000)); | |
123 | }; | |
124 | is($x, $y); | |
196b62db NC |
125 | |
126 | $x = pack 'w', ~0; | |
127 | $y = pack 'w', (~0).''; | |
128 | is($x, $y); | |
129 | is(unpack ('w',$x), ~0); | |
130 | is(unpack ('w',$y), ~0); | |
131 | ||
132 | $x = pack 'w', ~0 - 1; | |
133 | $y = pack 'w', (~0) - 2; | |
134 | ||
135 | if (~0 - 1 == (~0) - 2) { | |
136 | is($x, $y, "NV arithmetic"); | |
137 | } else { | |
138 | isnt($x, $y, "IV/NV arithmetic"); | |
139 | } | |
140 | cmp_ok(unpack ('w',$x), '==', ~0 - 1); | |
141 | cmp_ok(unpack ('w',$y), '==', ~0 - 2); | |
203d3f29 NC |
142 | |
143 | # These should spot that pack 'w' is using NV, not double, on platforms | |
144 | # where IVs are smaller than doubles, and harmlessly pass elsewhere. | |
145 | # (tests for change 16861) | |
146 | my $x0 = 2**54+3; | |
147 | my $y0 = 2**54-2; | |
148 | ||
149 | $x = pack 'w', $x0; | |
150 | $y = pack 'w', $y0; | |
151 | ||
152 | if ($x0 == $y0) { | |
153 | is($x, $y, "NV arithmetic"); | |
154 | } else { | |
155 | isnt($x, $y, "IV/NV arithmetic"); | |
156 | } | |
157 | cmp_ok(unpack ('w',$x), '==', $x0); | |
158 | cmp_ok(unpack ('w',$y), '==', $y0); | |
fa8ec7c1 | 159 | } |
def98dd4 | 160 | |
0b568b5f | 161 | |
fa8ec7c1 | 162 | { |
196b62db | 163 | # test exceptions |
fa8ec7c1 NC |
164 | my $x; |
165 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff}; | |
0b568b5f | 166 | like($@, qr/^Unterminated compressed integer/); |
def98dd4 | 167 | |
fa8ec7c1 | 168 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff}; |
0b568b5f | 169 | like($@, qr/^Unterminated compressed integer/); |
def98dd4 | 170 | |
fa8ec7c1 | 171 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
0b568b5f | 172 | like($@, qr/^Unterminated compressed integer/); |
0258719b NC |
173 | |
174 | eval { $x = pack 'w', -1 }; | |
175 | like ($@, qr/^Cannot compress negative numbers/); | |
176 | ||
177 | eval { $x = pack 'w', '1'x(1 + length ~0) . 'e0' }; | |
178 | like ($@, qr/^Can only compress unsigned integers/); | |
179 | ||
180 | SKIP: { | |
181 | # Is this a stupid thing to do on VMS, VOS and other unusual platforms? | |
182 | my $inf = eval '2**10000'; | |
183 | ||
184 | skip "Couldn't generate infinity - got error '$@'" | |
71b080b8 | 185 | unless defined $inf and $inf == $inf / 2 and $inf + 1 == $inf; |
0258719b | 186 | |
46ff39aa PG |
187 | local our $TODO; |
188 | $TODO = "VOS needs a fix for posix-1022 to pass this test." | |
189 | if ($^O eq 'vos'); | |
190 | ||
0258719b | 191 | eval { $x = pack 'w', $inf }; |
46ff39aa | 192 | like ($@, qr/^Cannot compress integer/, "Cannot compress integer"); |
0258719b NC |
193 | } |
194 | ||
195 | SKIP: { | |
196 | # This should be about the biggest thing possible on an IEEE double | |
197 | my $big = eval '2**1023'; | |
198 | ||
92b5f8d1 | 199 | skip "Couldn't generate 2**1023 - got error '$@'", 3 |
0258719b NC |
200 | unless defined $big and $big != $big / 2; |
201 | ||
202 | eval { $x = pack 'w', $big }; | |
203 | is ($@, '', "Should be able to pack 'w', $big # 2**1023"); | |
204 | ||
205 | my $y = eval {unpack 'w', $x}; | |
206 | is ($@, '', | |
207 | "Should be able to unpack 'w' the result of pack 'w', $big # 2**1023"); | |
208 | ||
209 | # I'm getting about 1e-16 on FreeBSD | |
210 | my $quotient = int (100 * ($y - $big) / $big); | |
211 | ok($quotient < 2 && $quotient > -2, | |
212 | "Round trip pack, unpack 'w' of $big is withing 1% ($quotient%)"); | |
213 | } | |
214 | ||
fa8ec7c1 | 215 | } |
def98dd4 | 216 | |
84902520 TB |
217 | # |
218 | # test the "p" template | |
219 | ||
220 | # literals | |
0b568b5f | 221 | is(unpack("p",pack("p","foo")), "foo"); |
84902520 TB |
222 | |
223 | # scalars | |
0b568b5f | 224 | is(unpack("p",pack("p",239)), 239); |
84902520 TB |
225 | |
226 | # temps | |
227 | sub foo { my $a = "a"; return $a . $a++ . $a++ } | |
228 | { | |
9f1b1f2d | 229 | use warnings; |
0b568b5f | 230 | my $warning; |
84902520 | 231 | local $SIG{__WARN__} = sub { |
0b568b5f | 232 | $warning = $_[0]; |
84902520 TB |
233 | }; |
234 | my $junk = pack("p", &foo); | |
0b568b5f MS |
235 | |
236 | like($warning, qr/temporary val/); | |
84902520 TB |
237 | } |
238 | ||
239 | # undef should give null pointer | |
0b568b5f | 240 | like(pack("p", undef), qr/^\0+/); |
84902520 | 241 | |
20408e3c GS |
242 | # Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives |
243 | # 4294967295 instead of -1) | |
244 | # see #ifdef __osf__ in pp.c pp_unpack | |
0b568b5f | 245 | is((unpack("i",pack("i",-1))), -1); |
20408e3c | 246 | |
0b568b5f MS |
247 | # test the pack lengths of s S i I l L |
248 | # test the pack lengths of n N v V | |
fa8ec7c1 NC |
249 | my @lengths = qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4); |
250 | while (my ($format, $expect) = splice @lengths, 0, 2) { | |
251 | my $len = length(pack($format, 0)); | |
252 | if ($expect > 0) { | |
0b568b5f | 253 | is($expect, $len, "format '$format'"); |
fa8ec7c1 NC |
254 | } else { |
255 | $expect = -$expect; | |
0b568b5f MS |
256 | ok ($len >= $expect, "format '$format'") || |
257 | print "# format '$format' has length $len, expected >= $expect\n"; | |
fa8ec7c1 NC |
258 | } |
259 | } | |
d4217c7e | 260 | |
d4217c7e | 261 | |
0b568b5f MS |
262 | # test unpack-pack lengths |
263 | my @templates = qw(c C i I s S l L n N v V f d q Q); | |
d4217c7e JH |
264 | |
265 | foreach my $t (@templates) { | |
0b568b5f MS |
266 | SKIP: { |
267 | my @t = eval { unpack("$t*", pack("$t*", 12, 34)) }; | |
268 | ||
269 | # quads not supported everywhere | |
49704364 | 270 | skip "Quads not supported", 4 if $@ =~ /Invalid type/; |
0b568b5f MS |
271 | is( $@, '' ); |
272 | ||
273 | is(scalar @t, 2); | |
3020ec7a MS |
274 | |
275 | SKIP: { | |
276 | skip "$t not expected to work for some reason", 2 if $t =~ /[nv]/i; | |
277 | ||
0b568b5f MS |
278 | is($t[0], 12); |
279 | is($t[1], 34); | |
280 | } | |
0b568b5f | 281 | } |
d4217c7e | 282 | } |
9d116dd7 | 283 | |
fa8ec7c1 | 284 | { |
0b568b5f | 285 | # uuencode/decode |
9d116dd7 | 286 | |
0b568b5f MS |
287 | # Note that first uuencoding known 'text' data and then checking the |
288 | # binary values of the uuencoded version would not be portable between | |
289 | # character sets. Uuencoding is meant for encoding binary data, not | |
290 | # text data. | |
c4d5f83a | 291 | |
0b568b5f | 292 | my $in = pack 'C*', 0 .. 255; |
ba1ac976 | 293 | |
0b568b5f MS |
294 | # just to be anal, we do some random tr/`/ / |
295 | my $uu = <<'EOUU'; | |
ba1ac976 | 296 | M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL |
9d116dd7 JH |
297 | M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9 |
298 | M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6& | |
299 | MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S | |
300 | MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@ | |
ba1ac976 | 301 | ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P ` |
9d116dd7 JH |
302 | EOUU |
303 | ||
0b568b5f MS |
304 | $_ = $uu; |
305 | tr/ /`/; | |
9d116dd7 | 306 | |
0b568b5f | 307 | is(pack('u', $in), $_); |
fa8ec7c1 | 308 | |
0b568b5f | 309 | is(unpack('u', $uu), $in); |
9d116dd7 | 310 | |
0b568b5f MS |
311 | $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"; |
312 | $uu = <<'EOUU'; | |
eddc390b JH |
313 | M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F |
314 | &8%P:```` | |
315 | EOUU | |
316 | ||
0b568b5f | 317 | is(unpack('u', $uu), $in); |
eddc390b | 318 | |
0b568b5f MS |
319 | # This is identical to the above except that backquotes have been |
320 | # changed to spaces | |
eddc390b | 321 | |
0b568b5f | 322 | $uu = <<'EOUU'; |
eddc390b | 323 | M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F |
c4d5f83a | 324 | &8%P: |
eddc390b JH |
325 | EOUU |
326 | ||
0b568b5f MS |
327 | # ' # Grr |
328 | is(unpack('u', $uu), $in); | |
ef54e1a4 | 329 | |
d99ad34e | 330 | } |
726ea183 | 331 | |
0b568b5f | 332 | # test the ascii template types (A, a, Z) |
726ea183 | 333 | |
fa8ec7c1 | 334 | foreach ( |
0b568b5f | 335 | ['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "], |
fa8ec7c1 | 336 | ['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "], |
0b568b5f MS |
337 | ['u', 'A*', "foo\0bar \0", "foo\0bar"], |
338 | ['u', 'A8', "foo\0bar \0", "foo\0bar"], | |
339 | ['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "], | |
fa8ec7c1 | 340 | ['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"], |
0b568b5f MS |
341 | ['u', 'a*', "foo\0bar \0", "foo\0bar \0"], |
342 | ['u', 'a8', "foo\0bar \0", "foo\0bar "], | |
343 | ['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"], | |
fa8ec7c1 | 344 | ['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"], |
0b568b5f MS |
345 | ['p', 'Z3', "foo", "fo\0"], |
346 | ['u', 'Z*', "foo\0bar \0", "foo"], | |
347 | ['u', 'Z8', "foo\0bar \0", "foo"], | |
348 | ) | |
349 | { | |
350 | my ($what, $template, $in, $out) = @$_; | |
351 | my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in); | |
352 | unless (is($got, $out)) { | |
0b568b5f | 353 | my $un = $what eq 'u' ? 'un' : ''; |
9e17a6b8 NC |
354 | print "# ${un}pack ('$template', "._qq($in).') gave '._qq($out). |
355 | ' not '._qq($got)."\n"; | |
0b568b5f | 356 | } |
d99ad34e | 357 | } |
726ea183 | 358 | |
0b568b5f | 359 | # packing native shorts/ints/longs |
726ea183 | 360 | |
0b568b5f MS |
361 | is(length(pack("s!", 0)), $Config{shortsize}); |
362 | is(length(pack("i!", 0)), $Config{intsize}); | |
363 | is(length(pack("l!", 0)), $Config{longsize}); | |
364 | ok(length(pack("s!", 0)) <= length(pack("i!", 0))); | |
365 | ok(length(pack("i!", 0)) <= length(pack("l!", 0))); | |
366 | is(length(pack("i!", 0)), length(pack("i", 0))); | |
726ea183 | 367 | |
fa8ec7c1 NC |
368 | sub numbers { |
369 | my $format = shift; | |
370 | return numbers_with_total ($format, undef, @_); | |
d99ad34e | 371 | } |
726ea183 | 372 | |
fa8ec7c1 NC |
373 | sub numbers_with_total { |
374 | my $format = shift; | |
375 | my $total = shift; | |
376 | if (!defined $total) { | |
377 | foreach (@_) { | |
378 | $total += $_; | |
379 | } | |
380 | } | |
381 | foreach (@_) { | |
0b568b5f MS |
382 | SKIP: { |
383 | my $out = eval {unpack($format, pack($format, $_))}; | |
384 | skip "cannot pack '$format' on this perl", 2 if | |
49704364 | 385 | $@ =~ /Invalid type '$format'/; |
0b568b5f MS |
386 | |
387 | is($@, ''); | |
388 | is($out, $_); | |
fa8ec7c1 | 389 | } |
fa8ec7c1 | 390 | } |
726ea183 | 391 | |
fa8ec7c1 NC |
392 | my $skip_if_longer_than = ~0; # "Infinity" |
393 | if (~0 - 1 == ~0) { | |
394 | # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all | |
395 | # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't | |
396 | # correctly in perl calculate UV totals for long checksums, as pp_unpack | |
397 | # is using UV maths, and we've only got NVs. | |
53133ed1 | 398 | $skip_if_longer_than = $Config{nv_preserves_uv_bits}; |
fa8ec7c1 | 399 | } |
726ea183 | 400 | |
fa8ec7c1 | 401 | foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) { |
0b568b5f MS |
402 | SKIP: { |
403 | my $sum = eval {unpack "%$_$format*", pack "$format*", @_}; | |
404 | skip "cannot pack '$format' on this perl", 3 | |
49704364 | 405 | if $@ =~ /Invalid type '$format'/; |
0b568b5f MS |
406 | |
407 | is($@, ''); | |
408 | ok(defined $sum); | |
409 | ||
410 | my $len = $_; # Copy, so that we can reassign '' | |
411 | $len = 16 unless length $len; | |
412 | ||
413 | SKIP: { | |
414 | skip "cannot test checksums over $skip_if_longer_than bits", 1 | |
415 | if $len > $skip_if_longer_than; | |
416 | ||
417 | # Our problem with testing this portably is that the checksum code in | |
418 | # pp_unpack is able to cast signed to unsigned, and do modulo 2**n | |
419 | # arithmetic in unsigned ints, which perl has no operators to do. | |
420 | # (use integer; does signed ints, which won't wrap on UTS, which is just | |
421 | # fine with ANSI, but not with most people's assumptions. | |
422 | # This is why we need to supply the totals for 'Q' as there's no way in | |
423 | # perl to calculate them, short of unpack '%0Q' (is that documented?) | |
424 | # ** returns NVs; make sure it's IV. | |
425 | my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum | |
426 | my $max_p1 = $max + 1; | |
427 | my ($max_is_integer, $max_p1_is_integer); | |
428 | $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1; | |
429 | $max_is_integer = 1 if $max - 1 < ~0; | |
430 | ||
431 | my $calc_sum; | |
432 | if (ref $total) { | |
433 | $calc_sum = &$total($len); | |
434 | } else { | |
435 | $calc_sum = $total; | |
436 | # Shift into range by some multiple of the total | |
437 | my $mult = int ($total / $max_p1); | |
438 | # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer) | |
439 | $calc_sum = $total - $mult; | |
440 | $calc_sum -= $mult * $max; | |
441 | if ($calc_sum < 0) { | |
442 | $calc_sum += 1; | |
443 | $calc_sum += $max; | |
444 | } | |
445 | } | |
446 | if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) { | |
447 | # we're into floating point (either by getting out of the range of | |
448 | # UV arithmetic, or because we're doing a floating point checksum) | |
449 | # and our calculation of the checksum has become rounded up to | |
450 | # max_checksum + 1 | |
451 | $calc_sum = 0; | |
452 | } | |
453 | ||
0dccb3d1 NC |
454 | if ($calc_sum == $sum) { # HAS to be ==, not eq (so no is()). |
455 | ok ("unpack '%$_$format' gave $sum"); | |
0b568b5f MS |
456 | } else { |
457 | my $delta = 1.000001; | |
458 | if ($format =~ tr /dDfF// | |
459 | && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) { | |
0dccb3d1 | 460 | pass ("unpack '%$_$format' gave $sum, expected $calc_sum"); |
0b568b5f MS |
461 | } else { |
462 | my $text = ref $total ? &$total($len) : $total; | |
463 | fail; | |
464 | print "# For list (" . join (", ", @_) . ") (total $text)" | |
465 | . " packed with $format unpack '%$_$format' gave $sum," | |
466 | . " expected $calc_sum\n"; | |
467 | } | |
468 | } | |
fa8ec7c1 | 469 | } |
0b568b5f | 470 | } |
2e821511 NC |
471 | } |
472 | } | |
473 | ||
fa8ec7c1 NC |
474 | numbers ('c', -128, -1, 0, 1, 127); |
475 | numbers ('C', 0, 1, 127, 128, 255); | |
476 | numbers ('s', -32768, -1, 0, 1, 32767); | |
477 | numbers ('S', 0, 1, 32767, 32768, 65535); | |
478 | numbers ('i', -2147483648, -1, 0, 1, 2147483647); | |
479 | numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295); | |
480 | numbers ('l', -2147483648, -1, 0, 1, 2147483647); | |
481 | numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295); | |
482 | numbers ('s!', -32768, -1, 0, 1, 32767); | |
483 | numbers ('S!', 0, 1, 32767, 32768, 65535); | |
484 | numbers ('i!', -2147483648, -1, 0, 1, 2147483647); | |
485 | numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295); | |
486 | numbers ('l!', -2147483648, -1, 0, 1, 2147483647); | |
487 | numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295); | |
488 | numbers ('n', 0, 1, 32767, 32768, 65535); | |
489 | numbers ('v', 0, 1, 32767, 32768, 65535); | |
490 | numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295); | |
491 | numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295); | |
492 | # All these should have exact binary representations: | |
493 | numbers ('f', -1, 0, 0.5, 42, 2**34); | |
b85d93de NC |
494 | numbers ('d', -(2**34), -1, 0, 1, 2**34); |
495 | ## These don't, but 'd' is NV. XXX wrong, it's double | |
496 | #numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1)); | |
fa8ec7c1 NC |
497 | |
498 | numbers_with_total ('q', -1, | |
499 | -9223372036854775808, -1, 0, 1,9223372036854775807); | |
800e6488 JH |
500 | # This total is icky, but the true total is 2**65-1, and need a way to generate |
501 | # the epxected checksum on any system including those where NVs can preserve | |
502 | # 65 bits. (long double is 128 bits on sparc, so they certainly can) | |
503 | # or where rounding is down not up on binary conversion (crays) | |
504 | numbers_with_total ('Q', sub { | |
505 | my $len = shift; | |
506 | $len = 65 if $len > 65; # unmasked total is 2**65-1 here | |
507 | my $total = 1 + 2 * (int (2**($len - 1)) - 1); | |
508 | return 0 if $total == $total - 1; # Overflowed integers | |
509 | return $total; # NVs still accurate to nearest integer | |
510 | }, | |
fa8ec7c1 NC |
511 | 0, 1,9223372036854775807, 9223372036854775808, |
512 | 18446744073709551615); | |
513 | ||
514 | # pack nvNV byteorders | |
515 | ||
0b568b5f MS |
516 | is(pack("n", 0xdead), "\xde\xad"); |
517 | is(pack("v", 0xdead), "\xad\xde"); | |
518 | is(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef"); | |
519 | is(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde"); | |
43192e07 | 520 | |
fa8ec7c1 NC |
521 | { |
522 | # / | |
523 | ||
524 | my ($x, $y, $z); | |
525 | eval { ($x) = unpack '/a*','hello' }; | |
49704364 | 526 | like($@, qr!'/' must follow a numeric type!); |
72b034c3 NC |
527 | undef $x; |
528 | eval { $x = unpack '/a*','hello' }; | |
49704364 | 529 | like($@, qr!'/' must follow a numeric type!); |
0b568b5f | 530 | |
72b034c3 | 531 | undef $x; |
fa8ec7c1 | 532 | eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" }; |
0b568b5f MS |
533 | is($@, ''); |
534 | is($z, 'ok'); | |
535 | is($x, 'yes'); | |
536 | is($y, 'z'); | |
72b034c3 NC |
537 | undef $z; |
538 | eval { $z = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" }; | |
539 | is($@, ''); | |
540 | is($z, 'ok'); | |
541 | ||
fa8ec7c1 | 542 | |
72b034c3 | 543 | undef $x; |
fa8ec7c1 | 544 | eval { ($x) = pack '/a*','hello' }; |
49704364 | 545 | like($@, qr!Invalid type '/'!); |
72b034c3 NC |
546 | undef $x; |
547 | eval { $x = pack '/a*','hello' }; | |
49704364 | 548 | like($@, qr!Invalid type '/'!); |
fa8ec7c1 NC |
549 | |
550 | $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc'; | |
551 | my $expect = "\000\006string\0\0\0\012hi there \000\003etc"; | |
0b568b5f | 552 | is($z, $expect); |
4b5b2118 | 553 | |
72b034c3 | 554 | undef $x; |
b85d93de NC |
555 | $expect = 'hello world'; |
556 | eval { ($x) = unpack ("w/a", chr (11) . "hello world!")}; | |
0b568b5f MS |
557 | is($x, $expect); |
558 | is($@, ''); | |
559 | ||
72b034c3 | 560 | undef $x; |
b85d93de NC |
561 | # Doing this in scalar context used to fail. |
562 | eval { $x = unpack ("w/a", chr (11) . "hello world!")}; | |
0b568b5f MS |
563 | is($@, ''); |
564 | is($x, $expect); | |
b85d93de | 565 | |
fa8ec7c1 | 566 | foreach ( |
0b568b5f MS |
567 | ['a/a*/a*', '212ab345678901234567','ab3456789012'], |
568 | ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'], | |
569 | ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'], | |
570 | ) | |
571 | { | |
fa8ec7c1 | 572 | my ($pat, $in, $expect) = @$_; |
72b034c3 | 573 | undef $x; |
fa8ec7c1 | 574 | eval { ($x) = unpack $pat, $in }; |
0b568b5f MS |
575 | is($@, ''); |
576 | is($x, $expect) || | |
577 | printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n", | |
578 | encode_list ($x); | |
579 | ||
72b034c3 | 580 | undef $x; |
b85d93de | 581 | eval { $x = unpack $pat, $in }; |
0b568b5f MS |
582 | is($@, ''); |
583 | is($x, $expect) || | |
584 | printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n", | |
585 | encode_list ($x); | |
fa8ec7c1 | 586 | } |
4b5b2118 | 587 | |
0b568b5f | 588 | # / with # |
17f4a12d | 589 | |
72b034c3 | 590 | my $pattern = <<'EOU'; |
17f4a12d IZ |
591 | a3/A # Count in ASCII |
592 | C/a* # Count in a C char | |
593 | C/Z # Count in a C char but skip after \0 | |
594 | EOU | |
17f4a12d | 595 | |
72b034c3 NC |
596 | $x = $y = $z =undef; |
597 | eval { ($z,$x,$y) = unpack $pattern, "003ok \003yes\004z\000abc" }; | |
0b568b5f MS |
598 | is($@, ''); |
599 | is($z, 'ok'); | |
600 | is($x, 'yes'); | |
601 | is($y, 'z'); | |
72b034c3 NC |
602 | undef $x; |
603 | eval { $z = unpack $pattern, "003ok \003yes\004z\000abc" }; | |
604 | is($@, ''); | |
605 | is($z, 'ok'); | |
0b568b5f | 606 | |
72b034c3 | 607 | $pattern = <<'EOP'; |
17f4a12d IZ |
608 | n/a* # Count as network short |
609 | w/A* # Count a BER integer | |
610 | EOP | |
fa8ec7c1 | 611 | $expect = "\000\006string\003etc"; |
72b034c3 | 612 | $z = pack $pattern,'string','etc'; |
9e17a6b8 | 613 | is($z, $expect); |
fa8ec7c1 NC |
614 | } |
615 | ||
60a99fa7 JH |
616 | |
617 | SKIP: { | |
618 | skip("(EBCDIC and) version strings are bad idea", 2) if $Is_EBCDIC; | |
619 | ||
620 | is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000)); | |
621 | is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000)); | |
622 | } | |
0b568b5f | 623 | isnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000)); |
fa8ec7c1 | 624 | |
0b568b5f MS |
625 | my $rslt = $Is_EBCDIC ? "156 67" : "199 162"; |
626 | is(join(" ", unpack("C*", chr(0x1e2))), $rslt); | |
fa8ec7c1 NC |
627 | |
628 | # does pack U create Unicode? | |
0b568b5f | 629 | is(ord(pack('U', 300)), 300); |
fa8ec7c1 NC |
630 | |
631 | # does unpack U deref Unicode? | |
0b568b5f | 632 | is((unpack('U', chr(300)))[0], 300); |
fa8ec7c1 NC |
633 | |
634 | # is unpack U the reverse of pack U for Unicode string? | |
0b568b5f | 635 | is("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300"); |
fa8ec7c1 NC |
636 | |
637 | # is unpack U the reverse of pack U for byte string? | |
0b568b5f MS |
638 | is("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200"); |
639 | ||
640 | ||
641 | SKIP: { | |
642 | skip "Not for EBCDIC", 4 if $Is_EBCDIC; | |
fa8ec7c1 | 643 | |
7eed2ccc | 644 | # does unpack C unravel pack U? |
0b568b5f | 645 | is("@{[unpack('C*', pack('U*', 100, 200))]}", "100 195 136"); |
fa8ec7c1 | 646 | |
7eed2ccc | 647 | # does pack U0C create Unicode? |
0b568b5f | 648 | is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200); |
fa8ec7c1 | 649 | |
7eed2ccc | 650 | # does pack C0U create characters? |
0b568b5f | 651 | is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136)); |
fa8ec7c1 | 652 | |
2dcec3fe JH |
653 | # does unpack U0U on byte data warn? |
654 | { | |
655 | local $SIG{__WARN__} = sub { $@ = "@_" }; | |
656 | my @null = unpack('U0U', chr(255)); | |
0b568b5f | 657 | like($@, /^Malformed UTF-8 character /); |
2dcec3fe | 658 | } |
35bcd338 JH |
659 | } |
660 | ||
fa8ec7c1 NC |
661 | { |
662 | my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647; | |
663 | my (@a); | |
664 | # bug - % had to be at the start of the pattern, no leading whitespace or | |
665 | # comments. %i! didn't work at all. | |
666 | foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ', | |
667 | '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') { | |
668 | @a = unpack $pat, $p; | |
0b568b5f | 669 | is($a[0], 0xFFFFFFFF) || print "# $pat\n"; |
fa8ec7c1 | 670 | @a = scalar unpack $pat, $p; |
0b568b5f | 671 | is($a[0], 0xFFFFFFFF) || print "# $pat\n"; |
fa8ec7c1 NC |
672 | } |
673 | ||
674 | ||
675 | $p = pack 'I*', 42, 12; | |
676 | # Multiline patterns in scalar context failed. | |
677 | foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') { | |
678 | # On the Ning Nang Nong | |
679 | # Where the Cows go Bong! | |
680 | # And the Monkeys all say Boo! | |
681 | I | |
682 | EOPOEMSNIPPET | |
0b568b5f MS |
683 | @a = unpack $pat, $p; |
684 | is(scalar @a, 1); | |
685 | is($a[0], 42); | |
686 | @a = scalar unpack $pat, $p; | |
687 | is(scalar @a, 1); | |
688 | is($a[0], 42); | |
689 | } | |
fa8ec7c1 NC |
690 | |
691 | # shorts (of all flavours) didn't calculate checksums > 32 bits with floating | |
692 | # point, so a pathologically long pattern would wrap at 32 bits. | |
693 | my $pat = "\xff\xff"x65538; # Start with it long, to save any copying. | |
694 | foreach (4,3,2,1,0) { | |
695 | my $len = 65534 + $_; | |
0b568b5f | 696 | is(unpack ("%33n$len", $pat), 65535 * $len); |
fa8ec7c1 NC |
697 | } |
698 | } | |
b85d93de NC |
699 | |
700 | ||
701 | # pack x X @ | |
702 | foreach ( | |
0b568b5f MS |
703 | ['x', "N", "\0"], |
704 | ['x4', "N", "\0"x4], | |
705 | ['xX', "N", ""], | |
706 | ['xXa*', "Nick", "Nick"], | |
707 | ['a5Xa5', "cameL", "llama", "camellama"], | |
708 | ['@4', 'N', "\0"x4], | |
709 | ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"], | |
710 | ['a*@4a', 'Perl rules', '!', 'Perl!'], | |
711 | ) | |
712 | { | |
b85d93de NC |
713 | my ($template, @in) = @$_; |
714 | my $out = pop @in; | |
715 | my $got = eval {pack $template, @in}; | |
0b568b5f MS |
716 | is($@, ''); |
717 | is($out, $got) || | |
718 | printf "# pack ('$template', %s) gave %s expected %s\n", | |
719 | encode_list (@in), encode_list ($got), encode_list ($out); | |
b85d93de NC |
720 | } |
721 | ||
722 | # unpack x X @ | |
723 | foreach ( | |
0b568b5f MS |
724 | ['x', "N"], |
725 | ['xX', "N"], | |
726 | ['xXa*', "Nick", "Nick"], | |
727 | ['a5Xa5', "camellama", "camel", "llama"], | |
728 | ['@3', "ice"], | |
729 | ['@2a2', "water", "te"], | |
730 | ['a*@1a3', "steam", "steam", "tea"], | |
731 | ) | |
732 | { | |
b85d93de NC |
733 | my ($template, $in, @out) = @$_; |
734 | my @got = eval {unpack $template, $in}; | |
0b568b5f | 735 | is($@, ''); |
c8f824eb | 736 | ok (list_eq (\@got, \@out)) || |
9e17a6b8 NC |
737 | printf "# list unpack ('$template', %s) gave %s expected %s\n", |
738 | _qq($in), encode_list (@got), encode_list (@out); | |
b85d93de NC |
739 | |
740 | my $got = eval {unpack $template, $in}; | |
0b568b5f MS |
741 | is($@, ''); |
742 | @out ? is( $got, $out[0] ) # 1 or more items; should get first | |
743 | : ok( !defined $got ) # 0 items; should get undef | |
9e17a6b8 NC |
744 | or printf "# scalar unpack ('$template', %s) gave %s expected %s\n", |
745 | _qq($in), encode_list ($got), encode_list ($out[0]); | |
b85d93de | 746 | } |
d50dd4e4 JH |
747 | |
748 | { | |
d50dd4e4 JH |
749 | my $t = 'Z*Z*'; |
750 | my ($u, $v) = qw(foo xyzzy); | |
751 | my $p = pack($t, $u, $v); | |
752 | my @u = unpack($t, $p); | |
0b568b5f MS |
753 | is(scalar @u, 2); |
754 | is($u[0], $u); | |
755 | is($u[1], $v); | |
d50dd4e4 | 756 | } |
bf80f5a2 JH |
757 | |
758 | { | |
0b568b5f | 759 | is((unpack("w/a*", "\x02abc"))[0], "ab"); |
bf80f5a2 | 760 | |
0b568b5f | 761 | # "w/a*" should be seen as one unit |
bf80f5a2 | 762 | |
0b568b5f | 763 | is(scalar unpack("w/a*", "\x02abc"), "ab"); |
bf80f5a2 | 764 | } |
b81060d6 WL |
765 | |
766 | { | |
b81060d6 WL |
767 | # from Wolfgang Laun: fix in change #13163 |
768 | ||
769 | my $s = 'ABC' x 10; | |
644d52eb JH |
770 | my $t = '*'; |
771 | my $x = ord($t); | |
b81060d6 WL |
772 | my $buf = pack( 'Z*/A* C', $s, $x ); |
773 | my $y; | |
774 | ||
775 | my $h = $buf; | |
776 | $h =~ s/[^[:print:]]/./g; | |
777 | ( $s, $y ) = unpack( "Z*/A* C", $buf ); | |
644d52eb | 778 | is($h, "30.ABCABCABCABCABCABCABCABCABCABC$t"); |
0b568b5f MS |
779 | is(length $buf, 34); |
780 | is($s, "ABCABCABCABCABCABCABCABCABCABC"); | |
644d52eb | 781 | is($y, $x); |
b81060d6 | 782 | } |
3bf38418 WL |
783 | |
784 | { | |
b223308b | 785 | # from Wolfgang Laun: fix in change #13288 |
3bf38418 | 786 | |
b223308b | 787 | eval { my $t=unpack("P*", "abc") }; |
49704364 | 788 | like($@, qr/'P' must have an explicit size/); |
3bf38418 | 789 | } |
18529408 IZ |
790 | |
791 | { # Grouping constructs | |
792 | my (@a, @b); | |
793 | @a = unpack '(SL)', pack 'SLSLSL', 67..90; | |
794 | is("@a", "67 68"); | |
795 | @a = unpack '(SL)3', pack 'SLSLSL', 67..90; | |
796 | @b = (67..72); | |
797 | is("@a", "@b"); | |
798 | @a = unpack '(SL)3', pack 'SLSLSLSL', 67..90; | |
799 | is("@a", "@b"); | |
800 | @a = unpack '(SL)[3]', pack 'SLSLSLSL', 67..90; | |
801 | is("@a", "@b"); | |
802 | @a = unpack '(SL)[2] SL', pack 'SLSLSLSL', 67..90; | |
803 | is("@a", "@b"); | |
804 | @a = unpack 'A/(SL)', pack 'ASLSLSLSL', 3, 67..90; | |
805 | is("@a", "@b"); | |
806 | @a = unpack 'A/(SL)SL', pack 'ASLSLSLSL', 2, 67..90; | |
807 | is("@a", "@b"); | |
808 | @a = unpack '(SL)*', pack 'SLSLSLSL', 67..90; | |
809 | @b = (67..74); | |
810 | is("@a", "@b"); | |
811 | @a = unpack '(SL)*SL', pack 'SLSLSLSL', 67..90; | |
812 | is("@a", "@b"); | |
813 | eval { @a = unpack '(*SL)', '' }; | |
814 | like($@, qr/\(\)-group starts with a count/); | |
815 | eval { @a = unpack '(3SL)', '' }; | |
816 | like($@, qr/\(\)-group starts with a count/); | |
817 | eval { @a = unpack '([3]SL)', '' }; | |
818 | like($@, qr/\(\)-group starts with a count/); | |
819 | eval { @a = pack '(*SL)' }; | |
820 | like($@, qr/\(\)-group starts with a count/); | |
821 | @a = unpack '(SL)3 SL', pack '(SL)4', 67..74; | |
822 | is("@a", "@b"); | |
823 | @a = unpack '(SL)3 SL', pack '(SL)[4]', 67..74; | |
824 | is("@a", "@b"); | |
825 | @a = unpack '(SL)3 SL', pack '(SL)*', 67..74; | |
826 | is("@a", "@b"); | |
827 | } | |
206947d2 | 828 | |
49704364 WL |
829 | { # more on grouping (W.Laun) |
830 | use warnings; | |
831 | my $warning; | |
832 | local $SIG{__WARN__} = sub { | |
833 | $warning = $_[0]; | |
834 | }; | |
835 | # @ absolute within ()-group | |
836 | my $badc = pack( '(a)*', unpack( '(@1a @0a @2)*', 'abcd' ) ); | |
837 | is( $badc, 'badc' ); | |
838 | my @b = ( 1, 2, 3 ); | |
839 | my $buf = pack( '(@1c)((@2C)@3c)', @b ); | |
840 | is( $buf, "\0\1\0\0\2\3" ); | |
841 | my @a = unpack( '(@1c)((@2c)@3c)', $buf ); | |
842 | is( "@a", "@b" ); | |
843 | ||
844 | # various unpack count/code scenarios | |
845 | my @Env = ( a => 'AAA', b => 'BBB' ); | |
846 | my $env = pack( 'S(S/A*S/A*)*', @Env/2, @Env ); | |
847 | ||
848 | # unpack full length - ok | |
849 | my @pup = unpack( 'S/(S/A* S/A*)', $env ); | |
850 | is( "@pup", "@Env" ); | |
851 | ||
852 | # warn when count/code goes beyond end of string | |
853 | # \0002 \0001 a \0003 AAA \0001 b \0003 BBB | |
854 | # 2 4 5 7 10 1213 | |
855 | eval { @pup = unpack( 'S/(S/A* S/A*)', substr( $env, 0, 13 ) ) }; | |
856 | like( $@, qr{length/code after end of string} ); | |
857 | ||
858 | # postfix repeat count | |
859 | $env = pack( '(S/A* S/A*)' . @Env/2, @Env ); | |
860 | ||
861 | # warn when count/code goes beyond end of string | |
862 | # \0001 a \0003 AAA \0001 b \0003 BBB | |
863 | # 2 3c 5 8 10 11 13 16 | |
864 | eval { @pup = unpack( '(S/A* S/A*)' . @Env/2, substr( $env, 0, 11 ) ) }; | |
865 | like( $@, qr{length/code after end of string} ); | |
866 | ||
867 | # catch stack overflow/segfault | |
868 | eval { $_ = pack( ('(' x 105) . 'A' . (')' x 105) ); }; | |
869 | like( $@, qr{Too deeply nested \(\)-groups} ); | |
870 | } | |
871 | ||
872 | { # syntax checks (W.Laun) | |
873 | use warnings; | |
874 | my @warning; | |
875 | local $SIG{__WARN__} = sub { | |
876 | push( @warning, $_[0] ); | |
877 | }; | |
878 | eval { my $s = pack( 'Ax![4c]A', 1..5 ); }; | |
879 | like( $@, qr{Malformed integer in \[\]} ); | |
880 | ||
881 | eval { my $buf = pack( '(c/*a*)', 'AAA', 'BB' ); }; | |
882 | like( $@, qr{'/' does not take a repeat count} ); | |
883 | ||
884 | eval { my @inf = unpack( 'c/1a', "\x03AAA\x02BB" ); }; | |
885 | like( $@, qr{'/' does not take a repeat count} ); | |
886 | ||
887 | eval { my @inf = unpack( 'c/*a', "\x03AAA\x02BB" ); }; | |
888 | like( $@, qr{'/' does not take a repeat count} ); | |
889 | ||
890 | # white space where possible | |
891 | my @Env = ( a => 'AAA', b => 'BBB' ); | |
892 | my $env = pack( ' S ( S / A* S / A* )* ', @Env/2, @Env ); | |
893 | my @pup = unpack( ' S / ( S / A* S / A* ) ', $env ); | |
894 | is( "@pup", "@Env" ); | |
895 | ||
896 | # white space in 4 wrong places | |
897 | for my $temp ( 'A ![4]', 'A [4]', 'A *', 'A 4' ){ | |
898 | eval { my $s = pack( $temp, 'B' ); }; | |
899 | like( $@, qr{Invalid type } ); | |
900 | } | |
901 | ||
902 | # warning for commas | |
903 | @warning = (); | |
904 | my $x = pack( 'I,A', 4, 'X' ); | |
905 | like( $warning[0], qr{Invalid type ','} ); | |
906 | ||
907 | # comma warning only once | |
908 | @warning = (); | |
909 | $x = pack( 'C(C,C)C,C', 65..71 ); | |
910 | like( scalar @warning, 1 ); | |
911 | ||
912 | # forbidden code in [] | |
913 | eval { my $x = pack( 'A[@4]', 'XXXX' ); }; | |
914 | like( $@, qr{Within \[\]-length '\@' not allowed} ); | |
915 | ||
916 | # @ repeat default 1 | |
917 | my $s = pack( 'AA@A', 'A', 'B', 'C' ); | |
918 | my @c = unpack( 'AA@A', $s ); | |
919 | is( $s, 'AC' ); | |
920 | is( "@c", "A C C" ); | |
921 | ||
922 | # no unpack code after / | |
923 | eval { my @a = unpack( "C/", "\3" ); }; | |
924 | like( $@, qr{Code missing after '/'} ); | |
925 | ||
926 | } | |
927 | ||
206947d2 | 928 | { # Repeat count [SUBEXPR] |
92d41999 JH |
929 | my @codes = qw( x A Z a c C B b H h s v n S i I l V N L p P f F d |
930 | s! S! i! I! l! L! j J); | |
931 | my $G; | |
206947d2 IZ |
932 | if (eval { pack 'q', 1 } ) { |
933 | push @codes, qw(q Q); | |
934 | } else { | |
935 | push @codes, qw(c C); # Keep the count the same | |
936 | } | |
92d41999 JH |
937 | if (eval { pack 'D', 1 } ) { |
938 | push @codes, 'D'; | |
939 | } else { | |
940 | push @codes, 'd'; # Keep the count the same | |
941 | } | |
206947d2 IZ |
942 | |
943 | my %val; | |
944 | @val{@codes} = map { / [Xx] (?{ undef }) | |
945 | | [AZa] (?{ 'something' }) | |
946 | | C (?{ 214 }) | |
947 | | c (?{ 114 }) | |
948 | | [Bb] (?{ '101' }) | |
949 | | [Hh] (?{ 'b8' }) | |
92d41999 | 950 | | [svnSiIlVNLqQjJ] (?{ 10111 }) |
206947d2 IZ |
951 | | [FfDd] (?{ 1.36514538e67 }) |
952 | | [pP] (?{ "try this buffer" }) | |
953 | /x; $^R } @codes; | |
954 | my @end = (0x12345678, 0x23456781, 0x35465768, 0x15263748); | |
955 | my $end = "N4"; | |
956 | ||
957 | for my $type (@codes) { | |
958 | my @list = $val{$type}; | |
959 | @list = () unless defined $list[0]; | |
960 | for my $count ('', '3', '[11]') { | |
961 | my $c = 1; | |
962 | $c = $1 if $count =~ /(\d+)/; | |
963 | my @list1 = @list; | |
964 | @list1 = (@list1) x $c unless $type =~ /[XxAaZBbHhP]/; | |
965 | for my $groupend ('', ')2', ')[8]') { | |
966 | my $groupbegin = ($groupend ? '(' : ''); | |
967 | $c = 1; | |
968 | $c = $1 if $groupend =~ /(\d+)/; | |
969 | my @list2 = (@list1) x $c; | |
970 | ||
971 | my $junk1 = "$groupbegin $type$count $groupend"; | |
972 | # print "# junk1=$junk1\n"; | |
973 | my $p = pack $junk1, @list2; | |
974 | my $half = int( (length $p)/2 ); | |
62f95557 | 975 | for my $move ('', "X$half", "X!$half", 'x1', 'x!8', "x$half") { |
206947d2 | 976 | my $junk = "$junk1 $move"; |
62f95557 | 977 | # print "# junk='$junk', list=(@list2)\n"; |
206947d2 IZ |
978 | $p = pack "$junk $end", @list2, @end; |
979 | my @l = unpack "x[$junk] $end", $p; | |
980 | is(scalar @l, scalar @end); | |
981 | is("@l", "@end", "skipping x[$junk]"); | |
982 | } | |
983 | } | |
984 | } | |
985 | } | |
986 | } | |
987 | ||
988 | # / is recognized after spaces in scalar context | |
989 | # XXXX no spaces are allowed in pack... In pack only before the slash... | |
990 | is(scalar unpack('A /A Z20', pack 'A/A* Z20', 'bcde', 'xxxxx'), 'bcde'); | |
991 | is(scalar unpack('A /A /A Z20', '3004bcde'), 'bcde'); | |
62f95557 IZ |
992 | |
993 | { # X! and x! | |
994 | my $t = 'C[3] x!8 C[2]'; | |
995 | my @a = (0x73..0x77); | |
996 | my $p = pack($t, @a); | |
997 | is($p, "\x73\x74\x75\0\0\0\0\0\x76\x77"); | |
998 | my @b = unpack $t, $p; | |
999 | is(scalar @b, scalar @a); | |
1000 | is("@b", "@a", 'x!8'); | |
1001 | $t = 'x[5] C[6] X!8 C[2]'; | |
1002 | @a = (0x73..0x7a); | |
1003 | $p = pack($t, @a); | |
1004 | is($p, "\0\0\0\0\0\x73\x74\x75\x79\x7a"); | |
1005 | @b = unpack $t, $p; | |
1006 | @a = (0x73..0x75, 0x79, 0x7a, 0x79, 0x7a); | |
1007 | is(scalar @b, scalar @a); | |
1008 | is("@b", "@a"); | |
1009 | } | |
1010 | ||
1011 | { # struct {char c1; double d; char cc[2];} | |
1012 | my $t = 'C x![d] d C[2]'; | |
1013 | my @a = (173, 1.283476517e-45, 42, 215); | |
1014 | my $p = pack $t, @a; | |
1015 | ok( length $p); | |
1016 | my @b = unpack "$t X[$t] $t", $p; # Extract, step back, extract again | |
1017 | is(scalar @b, 2 * scalar @a); | |
3f7e3417 JH |
1018 | $b = "@b"; |
1019 | $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble | |
1020 | is($b, "@a @a"); | |
62f95557 IZ |
1021 | |
1022 | my $warning; | |
1023 | local $SIG{__WARN__} = sub { | |
1024 | $warning = $_[0]; | |
1025 | }; | |
1026 | @b = unpack "x[C] x[$t] X[$t] X[C] $t", "$p\0"; | |
1027 | ||
1028 | is($warning, undef); | |
1029 | is(scalar @b, scalar @a); | |
3f7e3417 JH |
1030 | $b = "@b"; |
1031 | $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble | |
1032 | is($b, "@a"); | |
62f95557 | 1033 | } |
92d41999 JH |
1034 | |
1035 | is(length(pack("j", 0)), $Config{ivsize}); | |
1036 | is(length(pack("J", 0)), $Config{uvsize}); | |
1037 | is(length(pack("F", 0)), $Config{nvsize}); | |
1038 | ||
1039 | numbers ('j', -2147483648, -1, 0, 1, 2147483647); | |
1040 | numbers ('J', 0, 1, 2147483647, 2147483648, 4294967295); | |
1041 | numbers ('F', -(2**34), -1, 0, 1, 2**34); | |
1042 | SKIP: { | |
1043 | my $t = eval { unpack("D*", pack("D", 12.34)) }; | |
1044 | ||
49704364 | 1045 | skip "Long doubles not in use", 56 if $@ =~ /Invalid type/; |
92d41999 JH |
1046 | |
1047 | is(length(pack("D", 0)), $Config{longdblsize}); | |
1048 | numbers ('D', -(2**34), -1, 0, 1, 2**34); | |
1049 | } | |
1050 | ||
c8f824eb NC |
1051 | # Maybe this knowledge needs to be "global" for all of pack.t |
1052 | # Or a "can checksum" which would effectively be all the number types" | |
1053 | my %cant_checksum = map {$_=> 1} qw(A Z u w); | |
1054 | # not a b B h H | |
1055 | foreach my $template (qw(A Z c C s S i I l L n N v V q Q j J f d F D u U w)) { | |
1056 | SKIP: { | |
1057 | my $packed = eval {pack "${template}4", 1, 4, 9, 16}; | |
1058 | if ($@) { | |
49704364 | 1059 | die unless $@ =~ /Invalid type '$template'/; |
c8f824eb NC |
1060 | skip ("$template not supported on this perl", |
1061 | $cant_checksum{$template} ? 4 : 8); | |
1062 | } | |
1063 | my @unpack4 = unpack "${template}4", $packed; | |
1064 | my @unpack = unpack "${template}*", $packed; | |
1065 | my @unpack1 = unpack "${template}", $packed; | |
1066 | my @unpack1s = scalar unpack "${template}", $packed; | |
1067 | my @unpack4s = scalar unpack "${template}4", $packed; | |
1068 | my @unpacks = scalar unpack "${template}*", $packed; | |
1069 | ||
1070 | my @tests = ( ["${template}4 vs ${template}*", \@unpack4, \@unpack], | |
1071 | ["scalar ${template} ${template}", \@unpack1s, \@unpack1], | |
1072 | ["scalar ${template}4 vs ${template}", \@unpack4s, \@unpack1], | |
1073 | ["scalar ${template}* vs ${template}", \@unpacks, \@unpack1], | |
1074 | ); | |
1075 | ||
1076 | unless ($cant_checksum{$template}) { | |
1077 | my @unpack4_c = unpack "\%${template}4", $packed; | |
1078 | my @unpack_c = unpack "\%${template}*", $packed; | |
1079 | my @unpack1_c = unpack "\%${template}", $packed; | |
1080 | my @unpack1s_c = scalar unpack "\%${template}", $packed; | |
1081 | my @unpack4s_c = scalar unpack "\%${template}4", $packed; | |
1082 | my @unpacks_c = scalar unpack "\%${template}*", $packed; | |
1083 | ||
1084 | push @tests, | |
1085 | ( ["% ${template}4 vs ${template}*", \@unpack4_c, \@unpack_c], | |
1086 | ["% scalar ${template} ${template}", \@unpack1s_c, \@unpack1_c], | |
1087 | ["% scalar ${template}4 vs ${template}*", \@unpack4s_c, \@unpack_c], | |
1088 | ["% scalar ${template}* vs ${template}*", \@unpacks_c, \@unpack_c], | |
1089 | ); | |
1090 | } | |
1091 | foreach my $test (@tests) { | |
1092 | ok (list_eq ($test->[1], $test->[2]), $test->[0]) || | |
1093 | printf "# unpack gave %s expected %s\n", | |
1094 | encode_list (@{$test->[1]}), encode_list (@{$test->[2]}); | |
1095 | } | |
1096 | } | |
1097 | } | |
9c3dc587 HS |
1098 | |
1099 | ok(pack('u2', 'AA'), "[perl #8026]"); # used to hang and eat RAM in perl 5.7.2 | |
1100 | ||
fe581ec7 JH |
1101 | $_ = pack('c', 65); # 'A' would not be EBCDIC-friendly |
1102 | is(unpack('c'), 65, "one-arg unpack (change #18751)"); # defaulting to $_ |