This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
TODO tests for reads from a scalar changed to upgraded after open
[perl5.git] / t / op / pack.t
CommitLineData
d50dd4e4 1#!./perl -w
a687059c 2
a1a0e61e
TD
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
0b568b5f 6 require './test.pl';
b23b8711
MS
7}
8
d9db00a6
NC
9# This is truth in an if statement, and could be a skip message
10my $no_endianness = $] > 5.009 ? '' :
11 "Endianness pack modifiers not available on this perl";
12my $no_signedness = $] > 5.009 ? '' :
13 "Signed/unsigned pack modifiers not available on this perl";
14
21361d07 15plan tests => 14704;
0b568b5f 16
fa8ec7c1 17use strict;
bd7d4f4d 18use warnings qw(FATAL all);
b23b8711
MS
19use Config;
20
fa8ec7c1 21my $Is_EBCDIC = (defined $Config{ebcdic} && $Config{ebcdic} eq 'define');
0b568b5f 22my $Perl = which_perl();
1109a392
MHM
23my @valid_errors = (qr/^Invalid type '\w'/);
24
25my $ByteOrder = 'unknown';
26my $maybe_not_avail = '(?:hto[bl]e|[bl]etoh)';
d9db00a6
NC
27if ($no_endianness) {
28 push @valid_errors, qr/^Invalid type '[<>]'/;
29} elsif ($Config{byteorder} =~ /^1234(?:5678)?$/) {
1109a392
MHM
30 $ByteOrder = 'little';
31 $maybe_not_avail = '(?:htobe|betoh)';
32}
33elsif ($Config{byteorder} =~ /^(?:8765)?4321$/) {
34 $ByteOrder = 'big';
35 $maybe_not_avail = '(?:htole|letoh)';
36}
37else {
38 push @valid_errors, qr/^Can't (?:un)?pack (?:big|little)-endian .*? on this platform/;
39}
40
d9db00a6
NC
41if ($no_signedness) {
42 push @valid_errors, qr/^'!' allowed only after types sSiIlLxX in (?:un)?pack/;
43}
44
1109a392 45for my $size ( 16, 32, 64 ) {
49fe901c 46 if (defined $Config{"u${size}size"} and ($Config{"u${size}size"}||0) != ($size >> 3)) {
1109a392
MHM
47 push @valid_errors, qr/^Perl_my_$maybe_not_avail$size\(\) not available/;
48 }
49}
50
51my $IsTwosComplement = pack('i', -1) eq "\xFF" x $Config{intsize};
52print "# \$IsTwosComplement = $IsTwosComplement\n";
53
54sub is_valid_error
55{
56 my $err = shift;
57
58 for my $e (@valid_errors) {
59 $err =~ $e and return 1;
60 }
61
62 return 0;
63}
c274e827 64
b85d93de 65sub encode_list {
9e17a6b8 66 my @result = map {_qq($_)} @_;
b85d93de
NC
67 if (@result == 1) {
68 return @result;
69 }
70 return '(' . join (', ', @result) . ')';
71}
72
a1a0e61e 73
b85d93de
NC
74sub list_eq ($$) {
75 my ($l, $r) = @_;
c8f824eb 76 return 0 unless @$l == @$r;
b85d93de
NC
77 for my $i (0..$#$l) {
78 if (defined $l->[$i]) {
c8f824eb 79 return 0 unless defined ($r->[$i]) && $l->[$i] eq $r->[$i];
b85d93de 80 } else {
c8f824eb 81 return 0 if defined $r->[$i]
b85d93de
NC
82 }
83 }
84 return 1;
85}
86
87##############################################################################
88#
89# Here starteth the tests
90#
91
fa8ec7c1 92{
0b568b5f
MS
93 my $format = "c2 x5 C C x s d i l a6";
94 # Need the expression in here to force ary[5] to be numeric. This avoids
95 # test2 failing because ary2 goes str->numeric->str and ary doesn't.
96 my @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,
97 "abcdef");
98 my $foo = pack($format,@ary);
99 my @ary2 = unpack($format,$foo);
100
101 is($#ary, $#ary2);
102
103 my $out1=join(':',@ary);
104 my $out2=join(':',@ary2);
105 # Using long double NVs may introduce greater accuracy than wanted.
106 $out1 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
107 $out2 =~ s/:9\.87654321097999\d*:/:9.87654321098:/;
108 is($out1, $out2);
109
110 like($foo, qr/def/);
fa8ec7c1 111}
79072805
LW
112# How about counting bits?
113
fa8ec7c1 114{
0b568b5f
MS
115 my $x;
116 is( ($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")), 16 );
79072805 117
0b568b5f 118 is( ($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")), 12 );
79072805 119
0b568b5f 120 is( ($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")), 9 );
fa8ec7c1 121}
79072805 122
fa8ec7c1 123{
0b568b5f
MS
124 my $sum = 129; # ASCII
125 $sum = 103 if $Is_EBCDIC;
9d116dd7 126
0b568b5f
MS
127 my $x;
128 is( ($x = unpack("%32B*", "Now is the time for all good blurfl")), $sum );
79072805 129
0b568b5f
MS
130 my $foo;
131 open(BIN, $Perl) || die "Can't open $Perl: $!\n";
bd7d4f4d 132 binmode BIN;
0b568b5f
MS
133 sysread BIN, $foo, 8192;
134 close BIN;
79072805 135
0b568b5f
MS
136 $sum = unpack("%32b*", $foo);
137 my $longway = unpack("b*", $foo);
138 is( $sum, $longway =~ tr/1/1/ );
fa8ec7c1 139}
73a1c01a 140
fa8ec7c1
NC
141{
142 my $x;
0b568b5f 143 is( ($x = unpack("I",pack("I", 0xFFFFFFFF))), 0xFFFFFFFF );
fa8ec7c1 144}
def98dd4 145
fa8ec7c1 146{
0b568b5f
MS
147 # check 'w'
148 my @x = (5,130,256,560,32000,3097152,268435455,1073741844, 2**33,
149 '4503599627365785','23728385234614992549757750638446');
150 my $x = pack('w*', @x);
151 my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f8480808014A0808'.
152 '0800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e';
153
154 is($x, $y);
155
156 my @y = unpack('w*', $y);
157 my $a;
158 while ($a = pop @x) {
159 my $b = pop @y;
160 is($a, $b);
161 }
def98dd4 162
0b568b5f 163 @y = unpack('w2', $x);
def98dd4 164
0b568b5f
MS
165 is(scalar(@y), 2);
166 is($y[1], 130);
a6c48a57
UP
167 $x = pack('w*', 5000000000); $y = '';
168 eval {
169 use Math::BigInt;
170 $y = pack('w*', Math::BigInt::->new(5000000000));
171 };
172 is($x, $y);
196b62db
NC
173
174 $x = pack 'w', ~0;
175 $y = pack 'w', (~0).'';
176 is($x, $y);
177 is(unpack ('w',$x), ~0);
178 is(unpack ('w',$y), ~0);
179
180 $x = pack 'w', ~0 - 1;
181 $y = pack 'w', (~0) - 2;
182
183 if (~0 - 1 == (~0) - 2) {
184 is($x, $y, "NV arithmetic");
185 } else {
186 isnt($x, $y, "IV/NV arithmetic");
187 }
188 cmp_ok(unpack ('w',$x), '==', ~0 - 1);
189 cmp_ok(unpack ('w',$y), '==', ~0 - 2);
203d3f29
NC
190
191 # These should spot that pack 'w' is using NV, not double, on platforms
192 # where IVs are smaller than doubles, and harmlessly pass elsewhere.
193 # (tests for change 16861)
194 my $x0 = 2**54+3;
195 my $y0 = 2**54-2;
196
197 $x = pack 'w', $x0;
198 $y = pack 'w', $y0;
199
200 if ($x0 == $y0) {
201 is($x, $y, "NV arithmetic");
202 } else {
203 isnt($x, $y, "IV/NV arithmetic");
204 }
205 cmp_ok(unpack ('w',$x), '==', $x0);
206 cmp_ok(unpack ('w',$y), '==', $y0);
fa8ec7c1 207}
def98dd4 208
0b568b5f 209
fa8ec7c1 210{
40837afc 211 print "# test exceptions\n";
fa8ec7c1
NC
212 my $x;
213 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff};
0b568b5f 214 like($@, qr/^Unterminated compressed integer/);
def98dd4 215
fa8ec7c1 216 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff};
0b568b5f 217 like($@, qr/^Unterminated compressed integer/);
def98dd4 218
fa8ec7c1 219 eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
0b568b5f 220 like($@, qr/^Unterminated compressed integer/);
0258719b
NC
221
222 eval { $x = pack 'w', -1 };
223 like ($@, qr/^Cannot compress negative numbers/);
224
225 eval { $x = pack 'w', '1'x(1 + length ~0) . 'e0' };
226 like ($@, qr/^Can only compress unsigned integers/);
227
9391b049
NC
228 # Check that the warning behaviour on the modifiers !, < and > is as we
229 # expect it for this perl.
230 my $can_endian = $no_endianness ? '' : 'sSiIlLqQjJfFdDpP';
231 my $can_shriek = 'sSiIlL';
efb78d13 232 $can_shriek .= 'nNvV' unless $no_signedness;
9391b049 233 # h and H can't do either, so act as sanity checks in blead
efb78d13 234 foreach my $base (split '', 'hHsSiIlLqQjJfFdDpPnNvV') {
9391b049
NC
235 foreach my $mod ('', '<', '>', '!', '<!', '>!', '!<', '!>') {
236 SKIP: {
237 # Avoid void context warnings.
238 my $a = eval {pack "$base$mod"};
239 skip "pack can't $base", 1 if $@ =~ /^Invalid type '\w'/;
240 # Which error you get when 2 would be possible seems to be emergent
241 # behaviour of pack's format parser.
242
243 my $fails_shriek = $mod =~ /!/ && index ($can_shriek, $base) == -1;
244 my $fails_endian = $mod =~ /[<>]/ && index ($can_endian, $base) == -1;
245 my $shriek_first = $mod =~ /^!/;
246
247 if ($no_endianness and ($mod eq '<!' or $mod eq '>!')) {
248 # The ! isn't seem as part of $base. Instead it's seen as a modifier
249 # on > or <
250 $fails_shriek = 1;
251 undef $fails_endian;
252 } elsif ($fails_shriek and $fails_endian) {
253 if ($shriek_first) {
254 undef $fails_endian;
255 }
256 }
257
258 if ($fails_endian) {
259 if ($no_endianness) {
260 # < and > are seen as pattern letters, not modifiers
261 like ($@, qr/^Invalid type '[<>]'/, "pack can't $base$mod");
262 } else {
263 like ($@, qr/^'[<>]' allowed only after types/,
264 "pack can't $base$mod");
265 }
266 } elsif ($fails_shriek) {
267 like ($@, qr/^'!' allowed only after types/,
268 "pack can't $base$mod");
269 } else {
270 is ($@, '', "pack can $base$mod");
271 }
272 }
273 }
274 }
275
d9db00a6
NC
276 SKIP: {
277 skip $no_endianness, 2*3 + 2*8 if $no_endianness;
278 for my $mod (qw( ! < > )) {
279 eval { $x = pack "a$mod", 42 };
280 like ($@, qr/^'$mod' allowed only after types \S+ in pack/);
1109a392 281
d9db00a6
NC
282 eval { $x = unpack "a$mod", 'x'x8 };
283 like ($@, qr/^'$mod' allowed only after types \S+ in unpack/);
284 }
1109a392 285
d9db00a6
NC
286 for my $mod (qw( <> >< !<> !>< <!> >!< <>! ><! )) {
287 eval { $x = pack "sI${mod}s", 42, 47, 11 };
288 like ($@, qr/^Can't use both '<' and '>' after type 'I' in pack/);
1109a392 289
d9db00a6
NC
290 eval { $x = unpack "sI${mod}s", 'x'x16 };
291 like ($@, qr/^Can't use both '<' and '>' after type 'I' in unpack/);
292 }
1109a392
MHM
293 }
294
0258719b
NC
295 SKIP: {
296 # Is this a stupid thing to do on VMS, VOS and other unusual platforms?
71f93cb9 297
b987f19e 298 skip("-- the IEEE infinity model is unavailable in this configuration.", 1)
71f93cb9
CB
299 if (($^O eq 'VMS') && !defined($Config{useieee}));
300
b987f19e 301 skip("-- $^O has serious fp indigestion on w-packed infinities", 1)
5688dfbf 302 if (
5688dfbf
JH
303 ($^O eq 'ultrix')
304 ||
cd512f8e 305 ($^O =~ /^svr4/ && -f "/etc/issue" && -f "/etc/.relid") # NCR MP-RAS
5688dfbf 306 );
8b571b32 307
1109a392 308 my $inf = eval '2**1000000';
0258719b 309
b987f19e 310 skip("Couldn't generate infinity - got error '$@'", 1)
71b080b8 311 unless defined $inf and $inf == $inf / 2 and $inf + 1 == $inf;
0258719b 312
46ff39aa
PG
313 local our $TODO;
314 $TODO = "VOS needs a fix for posix-1022 to pass this test."
315 if ($^O eq 'vos');
316
0258719b 317 eval { $x = pack 'w', $inf };
46ff39aa 318 like ($@, qr/^Cannot compress integer/, "Cannot compress integer");
0258719b
NC
319 }
320
321 SKIP: {
71f93cb9 322
b987f19e 323 skip("-- the full range of an IEEE double may not be available in this configuration.", 3)
71f93cb9
CB
324 if (($^O eq 'VMS') && !defined($Config{useieee}));
325
b987f19e 326 skip("-- $^O does not like 2**1023", 3)
ad415dae
JH
327 if (($^O eq 'ultrix'));
328
0258719b
NC
329 # This should be about the biggest thing possible on an IEEE double
330 my $big = eval '2**1023';
331
b987f19e 332 skip("Couldn't generate 2**1023 - got error '$@'", 3)
0258719b
NC
333 unless defined $big and $big != $big / 2;
334
335 eval { $x = pack 'w', $big };
336 is ($@, '', "Should be able to pack 'w', $big # 2**1023");
337
338 my $y = eval {unpack 'w', $x};
339 is ($@, '',
340 "Should be able to unpack 'w' the result of pack 'w', $big # 2**1023");
341
342 # I'm getting about 1e-16 on FreeBSD
343 my $quotient = int (100 * ($y - $big) / $big);
344 ok($quotient < 2 && $quotient > -2,
1109a392 345 "Round trip pack, unpack 'w' of $big is within 1% ($quotient%)");
0258719b
NC
346 }
347
fa8ec7c1 348}
def98dd4 349
40837afc 350print "# test the 'p' template\n";
84902520
TB
351
352# literals
0b568b5f 353is(unpack("p",pack("p","foo")), "foo");
d9db00a6
NC
354SKIP: {
355 skip $no_endianness, 2 if $no_endianness;
356 is(unpack("p<",pack("p<","foo")), "foo");
357 is(unpack("p>",pack("p>","foo")), "foo");
358}
84902520 359# scalars
0b568b5f 360is(unpack("p",pack("p",239)), 239);
d9db00a6
NC
361SKIP: {
362 skip $no_endianness, 2 if $no_endianness;
363 is(unpack("p<",pack("p<",239)), 239);
364 is(unpack("p>",pack("p>",239)), 239);
365}
84902520
TB
366
367# temps
368sub foo { my $a = "a"; return $a . $a++ . $a++ }
369{
bd7d4f4d 370 use warnings qw(NONFATAL all);;
0b568b5f 371 my $warning;
84902520 372 local $SIG{__WARN__} = sub {
0b568b5f 373 $warning = $_[0];
84902520
TB
374 };
375 my $junk = pack("p", &foo);
0b568b5f
MS
376
377 like($warning, qr/temporary val/);
84902520
TB
378}
379
380# undef should give null pointer
1109a392 381like(pack("p", undef), qr/^\0+$/);
d9db00a6
NC
382SKIP: {
383 skip $no_endianness, 2 if $no_endianness;
384 like(pack("p<", undef), qr/^\0+$/);
385 like(pack("p>", undef), qr/^\0+$/);
386}
84902520 387
20408e3c
GS
388# Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives
389# 4294967295 instead of -1)
390# see #ifdef __osf__ in pp.c pp_unpack
0b568b5f 391is((unpack("i",pack("i",-1))), -1);
20408e3c 392
1109a392
MHM
393print "# test the pack lengths of s S i I l L n N v V + modifiers\n";
394
395my @lengths = (
396 qw(s 2 S 2 i -4 I -4 l 4 L 4 n 2 N 4 v 2 V 4 n! 2 N! 4 v! 2 V! 4),
397 's!' => $Config{shortsize}, 'S!' => $Config{shortsize},
398 'i!' => $Config{intsize}, 'I!' => $Config{intsize},
399 'l!' => $Config{longsize}, 'L!' => $Config{longsize},
400);
401
402while (my ($base, $expect) = splice @lengths, 0, 2) {
403 my @formats = ($base);
404 $base =~ /^[nv]/i or push @formats, "$base>", "$base<";
405 for my $format (@formats) {
d9db00a6
NC
406 SKIP: {
407 skip $no_endianness, 1 if $no_endianness && $format =~ m/[<>]/;
408 skip $no_signedness, 1 if $no_signedness && $format =~ /[nNvV]!/;
409 my $len = length(pack($format, 0));
410 if ($expect > 0) {
411 is($expect, $len, "format '$format'");
412 } else {
413 $expect = -$expect;
414 ok ($len >= $expect, "format '$format'") ||
415 print "# format '$format' has length $len, expected >= $expect\n";
416 }
1109a392 417 }
fa8ec7c1
NC
418 }
419}
d4217c7e 420
d4217c7e 421
40837afc
NC
422print "# test unpack-pack lengths\n";
423
f337b084 424my @templates = qw(c C W i I s S l L n N v V f d q Q);
d4217c7e 425
1109a392
MHM
426foreach my $base (@templates) {
427 my @tmpl = ($base);
f337b084 428 $base =~ /^[cwnv]/i or push @tmpl, "$base>", "$base<";
1109a392
MHM
429 foreach my $t (@tmpl) {
430 SKIP: {
431 my @t = eval { unpack("$t*", pack("$t*", 12, 34)) };
0b568b5f 432
1109a392
MHM
433 skip "cannot pack '$t' on this perl", 4
434 if is_valid_error($@);
3020ec7a 435
86704b5f 436 is( $@, '', "Template $t works");
1109a392 437 is(scalar @t, 2);
3020ec7a 438
0b568b5f
MS
439 is($t[0], 12);
440 is($t[1], 34);
441 }
0b568b5f 442 }
d4217c7e 443}
9d116dd7 444
fa8ec7c1 445{
0b568b5f 446 # uuencode/decode
9d116dd7 447
0b568b5f
MS
448 # Note that first uuencoding known 'text' data and then checking the
449 # binary values of the uuencoded version would not be portable between
450 # character sets. Uuencoding is meant for encoding binary data, not
451 # text data.
c4d5f83a 452
0b568b5f 453 my $in = pack 'C*', 0 .. 255;
ba1ac976 454
0b568b5f
MS
455 # just to be anal, we do some random tr/`/ /
456 my $uu = <<'EOUU';
ba1ac976 457M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL
9d116dd7
JH
458M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9
459M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6&
460MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S
461MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@
ba1ac976 462?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P `
9d116dd7
JH
463EOUU
464
0b568b5f
MS
465 $_ = $uu;
466 tr/ /`/;
9d116dd7 467
0b568b5f 468 is(pack('u', $in), $_);
fa8ec7c1 469
0b568b5f 470 is(unpack('u', $uu), $in);
9d116dd7 471
0b568b5f
MS
472 $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";
473 $uu = <<'EOUU';
eddc390b
JH
474M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F
475&8%P:````
476EOUU
477
0b568b5f 478 is(unpack('u', $uu), $in);
eddc390b 479
0b568b5f
MS
480# This is identical to the above except that backquotes have been
481# changed to spaces
eddc390b 482
0b568b5f 483 $uu = <<'EOUU';
eddc390b 484M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F
c4d5f83a 485&8%P:
eddc390b
JH
486EOUU
487
0b568b5f
MS
488 # ' # Grr
489 is(unpack('u', $uu), $in);
ef54e1a4 490
d99ad34e 491}
726ea183 492
0b568b5f 493# test the ascii template types (A, a, Z)
726ea183 494
fa8ec7c1 495foreach (
0b568b5f 496['p', 'A*', "foo\0bar\0 ", "foo\0bar\0 "],
fa8ec7c1 497['p', 'A11', "foo\0bar\0 ", "foo\0bar\0 "],
0b568b5f
MS
498['u', 'A*', "foo\0bar \0", "foo\0bar"],
499['u', 'A8', "foo\0bar \0", "foo\0bar"],
500['p', 'a*', "foo\0bar\0 ", "foo\0bar\0 "],
fa8ec7c1 501['p', 'a11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
0b568b5f
MS
502['u', 'a*', "foo\0bar \0", "foo\0bar \0"],
503['u', 'a8', "foo\0bar \0", "foo\0bar "],
504['p', 'Z*', "foo\0bar\0 ", "foo\0bar\0 \0"],
fa8ec7c1 505['p', 'Z11', "foo\0bar\0 ", "foo\0bar\0 \0\0"],
0b568b5f
MS
506['p', 'Z3', "foo", "fo\0"],
507['u', 'Z*', "foo\0bar \0", "foo"],
508['u', 'Z8', "foo\0bar \0", "foo"],
28be1210 509)
0b568b5f
MS
510{
511 my ($what, $template, $in, $out) = @$_;
512 my $got = $what eq 'u' ? (unpack $template, $in) : (pack $template, $in);
513 unless (is($got, $out)) {
0b568b5f 514 my $un = $what eq 'u' ? 'un' : '';
9e17a6b8
NC
515 print "# ${un}pack ('$template', "._qq($in).') gave '._qq($out).
516 ' not '._qq($got)."\n";
0b568b5f 517 }
d99ad34e 518}
726ea183 519
40837afc 520print "# packing native shorts/ints/longs\n";
726ea183 521
0b568b5f
MS
522is(length(pack("s!", 0)), $Config{shortsize});
523is(length(pack("i!", 0)), $Config{intsize});
524is(length(pack("l!", 0)), $Config{longsize});
525ok(length(pack("s!", 0)) <= length(pack("i!", 0)));
526ok(length(pack("i!", 0)) <= length(pack("l!", 0)));
527is(length(pack("i!", 0)), length(pack("i", 0)));
726ea183 528
fa8ec7c1 529sub numbers {
1109a392
MHM
530 my $base = shift;
531 my @formats = ($base);
532 $base =~ /^[silqjfdp]/i and push @formats, "$base>", "$base<";
533 for my $format (@formats) {
534 numbers_with_total ($format, undef, @_);
535 }
d99ad34e 536}
726ea183 537
fa8ec7c1
NC
538sub numbers_with_total {
539 my $format = shift;
540 my $total = shift;
541 if (!defined $total) {
542 foreach (@_) {
543 $total += $_;
544 }
545 }
40837afc 546 print "# numbers test for $format\n";
fa8ec7c1 547 foreach (@_) {
0b568b5f
MS
548 SKIP: {
549 my $out = eval {unpack($format, pack($format, $_))};
1109a392
MHM
550 skip "cannot pack '$format' on this perl", 2
551 if is_valid_error($@);
0b568b5f 552
96b96e00
NC
553 is($@, '', "no error");
554 is($out, $_, "unpack pack $format $_");
fa8ec7c1 555 }
fa8ec7c1 556 }
726ea183 557
fa8ec7c1
NC
558 my $skip_if_longer_than = ~0; # "Infinity"
559 if (~0 - 1 == ~0) {
560 # If we're running with -DNO_PERLPRESERVE_IVUV and NVs don't preserve all
561 # UVs (in which case ~0 is NV, ~0-1 will be the same NV) then we can't
562 # correctly in perl calculate UV totals for long checksums, as pp_unpack
563 # is using UV maths, and we've only got NVs.
53133ed1 564 $skip_if_longer_than = $Config{nv_preserves_uv_bits};
fa8ec7c1 565 }
726ea183 566
fa8ec7c1 567 foreach ('', 1, 2, 3, 15, 16, 17, 31, 32, 33, 53, 54, 63, 64, 65) {
0b568b5f
MS
568 SKIP: {
569 my $sum = eval {unpack "%$_$format*", pack "$format*", @_};
570 skip "cannot pack '$format' on this perl", 3
1109a392 571 if is_valid_error($@);
0b568b5f 572
96b96e00
NC
573 is($@, '', "no error");
574 ok(defined $sum, "sum bits $_, format $format defined");
0b568b5f
MS
575
576 my $len = $_; # Copy, so that we can reassign ''
577 $len = 16 unless length $len;
578
579 SKIP: {
580 skip "cannot test checksums over $skip_if_longer_than bits", 1
581 if $len > $skip_if_longer_than;
582
583 # Our problem with testing this portably is that the checksum code in
584 # pp_unpack is able to cast signed to unsigned, and do modulo 2**n
585 # arithmetic in unsigned ints, which perl has no operators to do.
586 # (use integer; does signed ints, which won't wrap on UTS, which is just
587 # fine with ANSI, but not with most people's assumptions.
588 # This is why we need to supply the totals for 'Q' as there's no way in
589 # perl to calculate them, short of unpack '%0Q' (is that documented?)
590 # ** returns NVs; make sure it's IV.
591 my $max = 1 + 2 * (int (2 ** ($len-1))-1); # The max possible checksum
592 my $max_p1 = $max + 1;
593 my ($max_is_integer, $max_p1_is_integer);
594 $max_p1_is_integer = 1 unless $max_p1 + 1 == $max_p1;
595 $max_is_integer = 1 if $max - 1 < ~0;
596
597 my $calc_sum;
598 if (ref $total) {
599 $calc_sum = &$total($len);
600 } else {
601 $calc_sum = $total;
602 # Shift into range by some multiple of the total
53f3fd80 603 my $mult = $max_p1 ? int ($total / $max_p1) : undef;
0b568b5f
MS
604 # Need this to make sure that -1 + (~0+1) is ~0 (ie still integer)
605 $calc_sum = $total - $mult;
606 $calc_sum -= $mult * $max;
607 if ($calc_sum < 0) {
608 $calc_sum += 1;
609 $calc_sum += $max;
610 }
611 }
612 if ($calc_sum == $calc_sum - 1 && $calc_sum == $max_p1) {
613 # we're into floating point (either by getting out of the range of
28be1210 614 # UV arithmetic, or because we're doing a floating point checksum)
0b568b5f
MS
615 # and our calculation of the checksum has become rounded up to
616 # max_checksum + 1
617 $calc_sum = 0;
618 }
619
0dccb3d1 620 if ($calc_sum == $sum) { # HAS to be ==, not eq (so no is()).
96b96e00 621 pass ("unpack '%$_$format' gave $sum");
0b568b5f
MS
622 } else {
623 my $delta = 1.000001;
624 if ($format =~ tr /dDfF//
625 && ($calc_sum <= $sum * $delta && $calc_sum >= $sum / $delta)) {
0dccb3d1 626 pass ("unpack '%$_$format' gave $sum, expected $calc_sum");
0b568b5f
MS
627 } else {
628 my $text = ref $total ? &$total($len) : $total;
629 fail;
630 print "# For list (" . join (", ", @_) . ") (total $text)"
631 . " packed with $format unpack '%$_$format' gave $sum,"
632 . " expected $calc_sum\n";
633 }
634 }
fa8ec7c1 635 }
96b96e00 636 }
2e821511
NC
637 }
638}
639
fa8ec7c1
NC
640numbers ('c', -128, -1, 0, 1, 127);
641numbers ('C', 0, 1, 127, 128, 255);
f337b084 642numbers ('W', 0, 1, 127, 128, 255, 256, 0x7ff, 0x800, 0xfffd);
fa8ec7c1
NC
643numbers ('s', -32768, -1, 0, 1, 32767);
644numbers ('S', 0, 1, 32767, 32768, 65535);
645numbers ('i', -2147483648, -1, 0, 1, 2147483647);
646numbers ('I', 0, 1, 2147483647, 2147483648, 4294967295);
647numbers ('l', -2147483648, -1, 0, 1, 2147483647);
648numbers ('L', 0, 1, 2147483647, 2147483648, 4294967295);
649numbers ('s!', -32768, -1, 0, 1, 32767);
650numbers ('S!', 0, 1, 32767, 32768, 65535);
651numbers ('i!', -2147483648, -1, 0, 1, 2147483647);
652numbers ('I!', 0, 1, 2147483647, 2147483648, 4294967295);
653numbers ('l!', -2147483648, -1, 0, 1, 2147483647);
654numbers ('L!', 0, 1, 2147483647, 2147483648, 4294967295);
655numbers ('n', 0, 1, 32767, 32768, 65535);
656numbers ('v', 0, 1, 32767, 32768, 65535);
657numbers ('N', 0, 1, 2147483647, 2147483648, 4294967295);
658numbers ('V', 0, 1, 2147483647, 2147483648, 4294967295);
068bd2e7
MHM
659numbers ('n!', -32768, -1, 0, 1, 32767);
660numbers ('v!', -32768, -1, 0, 1, 32767);
661numbers ('N!', -2147483648, -1, 0, 1, 2147483647);
662numbers ('V!', -2147483648, -1, 0, 1, 2147483647);
fa8ec7c1
NC
663# All these should have exact binary representations:
664numbers ('f', -1, 0, 0.5, 42, 2**34);
b85d93de
NC
665numbers ('d', -(2**34), -1, 0, 1, 2**34);
666## These don't, but 'd' is NV. XXX wrong, it's double
667#numbers ('d', -1, 0, 1, 1-exp(-1), -exp(1));
fa8ec7c1
NC
668
669numbers_with_total ('q', -1,
670 -9223372036854775808, -1, 0, 1,9223372036854775807);
800e6488 671# This total is icky, but the true total is 2**65-1, and need a way to generate
93f09d7b 672# the expected checksum on any system including those where NVs can preserve
800e6488
JH
673# 65 bits. (long double is 128 bits on sparc, so they certainly can)
674# or where rounding is down not up on binary conversion (crays)
675numbers_with_total ('Q', sub {
676 my $len = shift;
677 $len = 65 if $len > 65; # unmasked total is 2**65-1 here
678 my $total = 1 + 2 * (int (2**($len - 1)) - 1);
679 return 0 if $total == $total - 1; # Overflowed integers
680 return $total; # NVs still accurate to nearest integer
681 },
fa8ec7c1
NC
682 0, 1,9223372036854775807, 9223372036854775808,
683 18446744073709551615);
684
40837afc 685print "# pack nvNV byteorders\n";
fa8ec7c1 686
0b568b5f
MS
687is(pack("n", 0xdead), "\xde\xad");
688is(pack("v", 0xdead), "\xad\xde");
689is(pack("N", 0xdeadbeef), "\xde\xad\xbe\xef");
690is(pack("V", 0xdeadbeef), "\xef\xbe\xad\xde");
43192e07 691
d9db00a6
NC
692SKIP: {
693 skip $no_signedness, 4 if $no_signedness;
694 is(pack("n!", 0xdead), "\xde\xad");
695 is(pack("v!", 0xdead), "\xad\xde");
696 is(pack("N!", 0xdeadbeef), "\xde\xad\xbe\xef");
697 is(pack("V!", 0xdeadbeef), "\xef\xbe\xad\xde");
698}
068bd2e7 699
1109a392
MHM
700print "# test big-/little-endian conversion\n";
701
702sub byteorder
703{
704 my $format = shift;
705 print "# byteorder test for $format\n";
706 for my $value (@_) {
707 SKIP: {
708 my($nat,$be,$le) = eval { map { pack $format.$_, $value } '', '>', '<' };
709 skip "cannot pack '$format' on this perl", 5
710 if is_valid_error($@);
711
90d0f5d9
RGS
712 {
713 use warnings qw(NONFATAL utf8);
714 print "# [$value][$nat][$be][$le][$@]\n";
715 }
1109a392
MHM
716
717 SKIP: {
718 skip "cannot compare native byteorder with big-/little-endian", 1
719 if $ByteOrder eq 'unknown';
720
721 is($nat, $ByteOrder eq 'big' ? $be : $le);
722 }
723 is($be, reverse($le));
724 my @x = eval { unpack "$format$format>$format<", $nat.$be.$le };
725
726 print "# [$value][", join('][', @x), "][$@]\n";
727
728 is($@, '');
729 is($x[0], $x[1]);
730 is($x[0], $x[2]);
731 }
732 }
733}
734
735byteorder('s', -32768, -1, 0, 1, 32767);
736byteorder('S', 0, 1, 32767, 32768, 65535);
737byteorder('i', -2147483648, -1, 0, 1, 2147483647);
738byteorder('I', 0, 1, 2147483647, 2147483648, 4294967295);
739byteorder('l', -2147483648, -1, 0, 1, 2147483647);
740byteorder('L', 0, 1, 2147483647, 2147483648, 4294967295);
741byteorder('j', -2147483648, -1, 0, 1, 2147483647);
742byteorder('J', 0, 1, 2147483647, 2147483648, 4294967295);
743byteorder('s!', -32768, -1, 0, 1, 32767);
744byteorder('S!', 0, 1, 32767, 32768, 65535);
745byteorder('i!', -2147483648, -1, 0, 1, 2147483647);
746byteorder('I!', 0, 1, 2147483647, 2147483648, 4294967295);
747byteorder('l!', -2147483648, -1, 0, 1, 2147483647);
748byteorder('L!', 0, 1, 2147483647, 2147483648, 4294967295);
749byteorder('q', -9223372036854775808, -1, 0, 1, 9223372036854775807);
750byteorder('Q', 0, 1, 9223372036854775807, 9223372036854775808, 18446744073709551615);
751byteorder('f', -1, 0, 0.5, 42, 2**34);
752byteorder('F', -1, 0, 0.5, 42, 2**34);
753byteorder('d', -(2**34), -1, 0, 1, 2**34);
754byteorder('D', -(2**34), -1, 0, 1, 2**34);
755
756print "# test negative numbers\n";
757
758SKIP: {
759 skip "platform is not using two's complement for negative integers", 120
760 unless $IsTwosComplement;
761
762 for my $format (qw(s i l j s! i! l! q)) {
763 SKIP: {
764 my($nat,$be,$le) = eval { map { pack $format.$_, -1 } '', '>', '<' };
765 skip "cannot pack '$format' on this perl", 15
766 if is_valid_error($@);
767
768 my $len = length $nat;
769 is($_, "\xFF"x$len) for $nat, $be, $le;
770
771 my(@val,@ref);
772 if ($len >= 8) {
773 @val = (-2, -81985529216486896, -9223372036854775808);
774 @ref = ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE",
775 "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
776 "\x80\x00\x00\x00\x00\x00\x00\x00");
777 }
778 elsif ($len >= 4) {
779 @val = (-2, -19088744, -2147483648);
780 @ref = ("\xFF\xFF\xFF\xFE",
781 "\xFE\xDC\xBA\x98",
782 "\x80\x00\x00\x00");
783 }
784 else {
785 @val = (-2, -292, -32768);
786 @ref = ("\xFF\xFE",
787 "\xFE\xDC",
788 "\x80\x00");
789 }
790 for my $x (@ref) {
791 if ($len > length $x) {
792 $x = $x . "\xFF" x ($len - length $x);
793 }
794 }
795
796 for my $i (0 .. $#val) {
797 my($nat,$be,$le) = eval { map { pack $format.$_, $val[$i] } '', '>', '<' };
798 is($@, '');
799
800 SKIP: {
801 skip "cannot compare native byteorder with big-/little-endian", 1
802 if $ByteOrder eq 'unknown';
803
804 is($nat, $ByteOrder eq 'big' ? $be : $le);
805 }
806
807 is($be, $ref[$i]);
808 is($be, reverse($le));
809 }
810 }
811 }
812}
813
fa8ec7c1
NC
814{
815 # /
816
21361d07 817 my ($x, $y, $z, @a);
fa8ec7c1 818 eval { ($x) = unpack '/a*','hello' };
49704364 819 like($@, qr!'/' must follow a numeric type!);
72b034c3
NC
820 undef $x;
821 eval { $x = unpack '/a*','hello' };
49704364 822 like($@, qr!'/' must follow a numeric type!);
0b568b5f 823
21361d07
MHM
824 # [perl #60204] Unhelpful error message from unpack
825 eval { @a = unpack 'v/a*','h' };
826 is($@, '');
827 is(scalar @a, 0);
828 eval { $x = unpack 'v/a*','h' };
829 is($@, '');
830 is($x, undef);
831
72b034c3 832 undef $x;
fa8ec7c1 833 eval { ($z,$x,$y) = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
0b568b5f
MS
834 is($@, '');
835 is($z, 'ok');
836 is($x, 'yes');
837 is($y, 'z');
72b034c3
NC
838 undef $z;
839 eval { $z = unpack 'a3/A C/a* C/Z', "003ok \003yes\004z\000abc" };
840 is($@, '');
841 is($z, 'ok');
842
fa8ec7c1 843
72b034c3 844 undef $x;
fa8ec7c1 845 eval { ($x) = pack '/a*','hello' };
49704364 846 like($@, qr!Invalid type '/'!);
72b034c3
NC
847 undef $x;
848 eval { $x = pack '/a*','hello' };
49704364 849 like($@, qr!Invalid type '/'!);
fa8ec7c1
NC
850
851 $z = pack 'n/a* N/Z* w/A*','string','hi there ','etc';
852 my $expect = "\000\006string\0\0\0\012hi there \000\003etc";
0b568b5f 853 is($z, $expect);
4b5b2118 854
72b034c3 855 undef $x;
b85d93de
NC
856 $expect = 'hello world';
857 eval { ($x) = unpack ("w/a", chr (11) . "hello world!")};
0b568b5f
MS
858 is($x, $expect);
859 is($@, '');
860
72b034c3 861 undef $x;
b85d93de
NC
862 # Doing this in scalar context used to fail.
863 eval { $x = unpack ("w/a", chr (11) . "hello world!")};
0b568b5f
MS
864 is($@, '');
865 is($x, $expect);
b85d93de 866
fa8ec7c1 867 foreach (
0b568b5f
MS
868 ['a/a*/a*', '212ab345678901234567','ab3456789012'],
869 ['a/a*/a*', '3012ab345678901234567', 'ab3456789012'],
870 ['a/a*/b*', '212ab', $Is_EBCDIC ? '100000010100' : '100001100100'],
28be1210 871 )
0b568b5f 872 {
fa8ec7c1 873 my ($pat, $in, $expect) = @$_;
72b034c3 874 undef $x;
fa8ec7c1 875 eval { ($x) = unpack $pat, $in };
0b568b5f 876 is($@, '');
28be1210 877 is($x, $expect) ||
0b568b5f
MS
878 printf "# list unpack ('$pat', '$in') gave %s, expected '$expect'\n",
879 encode_list ($x);
880
72b034c3 881 undef $x;
b85d93de 882 eval { $x = unpack $pat, $in };
0b568b5f
MS
883 is($@, '');
884 is($x, $expect) ||
885 printf "# scalar unpack ('$pat', '$in') gave %s, expected '$expect'\n",
886 encode_list ($x);
fa8ec7c1 887 }
4b5b2118 888
0b568b5f 889 # / with #
17f4a12d 890
72b034c3 891 my $pattern = <<'EOU';
17f4a12d
IZ
892 a3/A # Count in ASCII
893 C/a* # Count in a C char
894 C/Z # Count in a C char but skip after \0
895EOU
17f4a12d 896
72b034c3
NC
897 $x = $y = $z =undef;
898 eval { ($z,$x,$y) = unpack $pattern, "003ok \003yes\004z\000abc" };
0b568b5f
MS
899 is($@, '');
900 is($z, 'ok');
901 is($x, 'yes');
902 is($y, 'z');
72b034c3
NC
903 undef $x;
904 eval { $z = unpack $pattern, "003ok \003yes\004z\000abc" };
905 is($@, '');
906 is($z, 'ok');
0b568b5f 907
72b034c3 908 $pattern = <<'EOP';
17f4a12d
IZ
909 n/a* # Count as network short
910 w/A* # Count a BER integer
911EOP
fa8ec7c1 912 $expect = "\000\006string\003etc";
72b034c3 913 $z = pack $pattern,'string','etc';
9e17a6b8 914 is($z, $expect);
fa8ec7c1
NC
915}
916
60a99fa7
JH
917
918SKIP: {
919 skip("(EBCDIC and) version strings are bad idea", 2) if $Is_EBCDIC;
920
921 is("1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000));
922 is("1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000));
923}
0b568b5f 924isnt(v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000));
fa8ec7c1 925
0b568b5f 926my $rslt = $Is_EBCDIC ? "156 67" : "199 162";
1651fc44 927is(join(" ", unpack("U0 C*", chr(0x1e2))), $rslt);
fa8ec7c1
NC
928
929# does pack U create Unicode?
0b568b5f 930is(ord(pack('U', 300)), 300);
fa8ec7c1
NC
931
932# does unpack U deref Unicode?
0b568b5f 933is((unpack('U', chr(300)))[0], 300);
fa8ec7c1
NC
934
935# is unpack U the reverse of pack U for Unicode string?
0b568b5f 936is("@{[unpack('U*', pack('U*', 100, 200, 300))]}", "100 200 300");
fa8ec7c1
NC
937
938# is unpack U the reverse of pack U for byte string?
0b568b5f
MS
939is("@{[unpack('U*', pack('U*', 100, 200))]}", "100 200");
940
941
942SKIP: {
943 skip "Not for EBCDIC", 4 if $Is_EBCDIC;
fa8ec7c1 944
7eed2ccc 945 # does pack U0C create Unicode?
0b568b5f 946 is("@{[pack('U0C*', 100, 195, 136)]}", v100.v200);
fa8ec7c1 947
7eed2ccc 948 # does pack C0U create characters?
0b568b5f 949 is("@{[pack('C0U*', 100, 200)]}", pack("C*", 100, 195, 136));
fa8ec7c1 950
2dcec3fe
JH
951 # does unpack U0U on byte data warn?
952 {
bd7d4f4d
NC
953 use warnings qw(NONFATAL all);;
954
08ca2aa3 955 my $bad = pack("U0C", 255);
2dcec3fe 956 local $SIG{__WARN__} = sub { $@ = "@_" };
08ca2aa3 957 my @null = unpack('U0U', $bad);
1109a392 958 like($@, qr/^Malformed UTF-8 character /);
2dcec3fe 959 }
35bcd338
JH
960}
961
fa8ec7c1
NC
962{
963 my $p = pack 'i*', -2147483648, ~0, 0, 1, 2147483647;
964 my (@a);
965 # bug - % had to be at the start of the pattern, no leading whitespace or
966 # comments. %i! didn't work at all.
967 foreach my $pat ('%32i*', ' %32i*', "# Muhahahaha\n%32i*", '%32i* ',
968 '%32i!*', ' %32i!*', "\n#\n#\n\r \t\f%32i!*", '%32i!*#') {
969 @a = unpack $pat, $p;
0b568b5f 970 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
fa8ec7c1 971 @a = scalar unpack $pat, $p;
0b568b5f 972 is($a[0], 0xFFFFFFFF) || print "# $pat\n";
fa8ec7c1
NC
973 }
974
975
976 $p = pack 'I*', 42, 12;
977 # Multiline patterns in scalar context failed.
978 foreach my $pat ('I', <<EOPOEMSNIPPET, 'I#I', 'I # I', 'I # !!!') {
979# On the Ning Nang Nong
980# Where the Cows go Bong!
981# And the Monkeys all say Boo!
982I
983EOPOEMSNIPPET
0b568b5f
MS
984 @a = unpack $pat, $p;
985 is(scalar @a, 1);
986 is($a[0], 42);
987 @a = scalar unpack $pat, $p;
988 is(scalar @a, 1);
989 is($a[0], 42);
990 }
fa8ec7c1
NC
991
992 # shorts (of all flavours) didn't calculate checksums > 32 bits with floating
993 # point, so a pathologically long pattern would wrap at 32 bits.
994 my $pat = "\xff\xff"x65538; # Start with it long, to save any copying.
995 foreach (4,3,2,1,0) {
996 my $len = 65534 + $_;
0b568b5f 997 is(unpack ("%33n$len", $pat), 65535 * $len);
fa8ec7c1
NC
998 }
999}
b85d93de
NC
1000
1001
1002# pack x X @
1003foreach (
0b568b5f
MS
1004 ['x', "N", "\0"],
1005 ['x4', "N", "\0"x4],
1006 ['xX', "N", ""],
1007 ['xXa*', "Nick", "Nick"],
1008 ['a5Xa5', "cameL", "llama", "camellama"],
1009 ['@4', 'N', "\0"x4],
1010 ['a*@8a*', 'Camel', 'Dromedary', "Camel\0\0\0Dromedary"],
1011 ['a*@4a', 'Perl rules', '!', 'Perl!'],
28be1210 1012)
0b568b5f 1013{
b85d93de
NC
1014 my ($template, @in) = @$_;
1015 my $out = pop @in;
1016 my $got = eval {pack $template, @in};
0b568b5f
MS
1017 is($@, '');
1018 is($out, $got) ||
1019 printf "# pack ('$template', %s) gave %s expected %s\n",
1020 encode_list (@in), encode_list ($got), encode_list ($out);
b85d93de
NC
1021}
1022
1023# unpack x X @
1024foreach (
0b568b5f
MS
1025 ['x', "N"],
1026 ['xX', "N"],
1027 ['xXa*', "Nick", "Nick"],
1028 ['a5Xa5', "camellama", "camel", "llama"],
1029 ['@3', "ice"],
1030 ['@2a2', "water", "te"],
1031 ['a*@1a3', "steam", "steam", "tea"],
28be1210 1032)
0b568b5f 1033{
b85d93de
NC
1034 my ($template, $in, @out) = @$_;
1035 my @got = eval {unpack $template, $in};
0b568b5f 1036 is($@, '');
c8f824eb 1037 ok (list_eq (\@got, \@out)) ||
9e17a6b8
NC
1038 printf "# list unpack ('$template', %s) gave %s expected %s\n",
1039 _qq($in), encode_list (@got), encode_list (@out);
b85d93de
NC
1040
1041 my $got = eval {unpack $template, $in};
0b568b5f
MS
1042 is($@, '');
1043 @out ? is( $got, $out[0] ) # 1 or more items; should get first
1044 : ok( !defined $got ) # 0 items; should get undef
9e17a6b8
NC
1045 or printf "# scalar unpack ('$template', %s) gave %s expected %s\n",
1046 _qq($in), encode_list ($got), encode_list ($out[0]);
b85d93de 1047}
d50dd4e4
JH
1048
1049{
d50dd4e4
JH
1050 my $t = 'Z*Z*';
1051 my ($u, $v) = qw(foo xyzzy);
1052 my $p = pack($t, $u, $v);
1053 my @u = unpack($t, $p);
0b568b5f
MS
1054 is(scalar @u, 2);
1055 is($u[0], $u);
1056 is($u[1], $v);
d50dd4e4 1057}
bf80f5a2
JH
1058
1059{
0b568b5f 1060 is((unpack("w/a*", "\x02abc"))[0], "ab");
bf80f5a2 1061
0b568b5f 1062 # "w/a*" should be seen as one unit
bf80f5a2 1063
0b568b5f 1064 is(scalar unpack("w/a*", "\x02abc"), "ab");
bf80f5a2 1065}
b81060d6 1066
d9db00a6 1067SKIP: {
66c611c5
MHM
1068 print "# group modifiers\n";
1069
d9db00a6
NC
1070 skip $no_endianness, 3 * 2 + 3 * 2 + 1 if $no_endianness;
1071
66c611c5
MHM
1072 for my $t (qw{ (s<)< (sl>s)> (s(l(sl)<l)s)< }) {
1073 print "# testing pattern '$t'\n";
1074 eval { ($_) = unpack($t, 'x'x18); };
1075 is($@, '');
1076 eval { $_ = pack($t, (0)x6); };
1077 is($@, '');
1078 }
1079
1080 for my $t (qw{ (s<)> (sl>s)< (s(l(sl)<l)s)> }) {
1081 print "# testing pattern '$t'\n";
1082 eval { ($_) = unpack($t, 'x'x18); };
1083 like($@, qr/Can't use '[<>]' in a group with different byte-order in unpack/);
1084 eval { $_ = pack($t, (0)x6); };
1085 like($@, qr/Can't use '[<>]' in a group with different byte-order in pack/);
1086 }
1087
d9db00a6
NC
1088 is(pack('L<L>', (0x12345678)x2),
1089 pack('(((L1)1)<)(((L)1)1)>1', (0x12345678)x2));
1090}
1091
1092{
66c611c5
MHM
1093 sub compress_template {
1094 my $t = shift;
1095 for my $mod (qw( < > )) {
1096 $t =~ s/((?:(?:[SILQJFDP]!?$mod|[^SILQJFDP\W]!?)(?:\d+|\*|\[(?:[^]]+)\])?\/?){2,})/
1097 my $x = $1; $x =~ s!$mod!!g ? "($x)$mod" : $x /ieg;
1098 }
1099 return $t;
1100 }
1101
66c611c5
MHM
1102 my %templates = (
1103 's<' => [-42],
1104 's<c2x![S]S<' => [-42, -11, 12, 4711],
1105 '(i<j<[s]l<)3' => [-11, -22, -33, 1000000, 1100, 2201, 3302,
1106 -1000000, 32767, -32768, 1, -123456789 ],
1107 '(I!<4(J<2L<)3)5' => [1 .. 65],
1108 'q<Q<' => [-50000000005, 60000000006],
1109 'f<F<d<' => [3.14159, 111.11, 2222.22],
1110 'D<cCD<' => [1e42, -128, 255, 1e-42],
1111 'n/a*' => ['/usr/bin/perl'],
1112 'C/a*S</A*L</Z*I</a*' => [qw(Just another Perl hacker)],
1113 );
1114
1115 for my $tle (sort keys %templates) {
1116 my @d = @{$templates{$tle}};
1117 my $tbe = $tle;
1118 $tbe =~ y/</>/;
1119 for my $t ($tbe, $tle) {
1120 my $c = compress_template($t);
1121 print "# '$t' -> '$c'\n";
1122 SKIP: {
1123 my $p1 = eval { pack $t, @d };
1124 skip "cannot pack '$t' on this perl", 5 if is_valid_error($@);
1125 my $p2 = eval { pack $c, @d };
1126 is($@, '');
1127 is($p1, $p2);
1128 s!(/[aAZ])\*!$1!g for $t, $c;
1129 my @u1 = eval { unpack $t, $p1 };
1130 is($@, '');
1131 my @u2 = eval { unpack $c, $p2 };
1132 is($@, '');
1133 is(join('!', @u1), join('!', @u2));
1134 }
1135 }
1136 }
1137}
1138
1139{
b81060d6
WL
1140 # from Wolfgang Laun: fix in change #13163
1141
1142 my $s = 'ABC' x 10;
644d52eb
JH
1143 my $t = '*';
1144 my $x = ord($t);
b81060d6
WL
1145 my $buf = pack( 'Z*/A* C', $s, $x );
1146 my $y;
1147
1148 my $h = $buf;
1149 $h =~ s/[^[:print:]]/./g;
1150 ( $s, $y ) = unpack( "Z*/A* C", $buf );
644d52eb 1151 is($h, "30.ABCABCABCABCABCABCABCABCABCABC$t");
0b568b5f
MS
1152 is(length $buf, 34);
1153 is($s, "ABCABCABCABCABCABCABCABCABCABC");
644d52eb 1154 is($y, $x);
b81060d6 1155}
3bf38418
WL
1156
1157{
b223308b 1158 # from Wolfgang Laun: fix in change #13288
3bf38418 1159
b223308b 1160 eval { my $t=unpack("P*", "abc") };
49704364 1161 like($@, qr/'P' must have an explicit size/);
3bf38418 1162}
18529408
IZ
1163
1164{ # Grouping constructs
1165 my (@a, @b);
1166 @a = unpack '(SL)', pack 'SLSLSL', 67..90;
1167 is("@a", "67 68");
1168 @a = unpack '(SL)3', pack 'SLSLSL', 67..90;
1169 @b = (67..72);
1170 is("@a", "@b");
1171 @a = unpack '(SL)3', pack 'SLSLSLSL', 67..90;
1172 is("@a", "@b");
1173 @a = unpack '(SL)[3]', pack 'SLSLSLSL', 67..90;
1174 is("@a", "@b");
1175 @a = unpack '(SL)[2] SL', pack 'SLSLSLSL', 67..90;
1176 is("@a", "@b");
1177 @a = unpack 'A/(SL)', pack 'ASLSLSLSL', 3, 67..90;
1178 is("@a", "@b");
1179 @a = unpack 'A/(SL)SL', pack 'ASLSLSLSL', 2, 67..90;
1180 is("@a", "@b");
1181 @a = unpack '(SL)*', pack 'SLSLSLSL', 67..90;
1182 @b = (67..74);
1183 is("@a", "@b");
1184 @a = unpack '(SL)*SL', pack 'SLSLSLSL', 67..90;
1185 is("@a", "@b");
1186 eval { @a = unpack '(*SL)', '' };
1187 like($@, qr/\(\)-group starts with a count/);
1188 eval { @a = unpack '(3SL)', '' };
1189 like($@, qr/\(\)-group starts with a count/);
1190 eval { @a = unpack '([3]SL)', '' };
1191 like($@, qr/\(\)-group starts with a count/);
1192 eval { @a = pack '(*SL)' };
1193 like($@, qr/\(\)-group starts with a count/);
1194 @a = unpack '(SL)3 SL', pack '(SL)4', 67..74;
1195 is("@a", "@b");
1196 @a = unpack '(SL)3 SL', pack '(SL)[4]', 67..74;
1197 is("@a", "@b");
1198 @a = unpack '(SL)3 SL', pack '(SL)*', 67..74;
1199 is("@a", "@b");
1200}
206947d2 1201
49704364 1202{ # more on grouping (W.Laun)
49704364
WL
1203 # @ absolute within ()-group
1204 my $badc = pack( '(a)*', unpack( '(@1a @0a @2)*', 'abcd' ) );
1205 is( $badc, 'badc' );
1206 my @b = ( 1, 2, 3 );
1207 my $buf = pack( '(@1c)((@2C)@3c)', @b );
1208 is( $buf, "\0\1\0\0\2\3" );
1209 my @a = unpack( '(@1c)((@2c)@3c)', $buf );
1210 is( "@a", "@b" );
1211
28be1210 1212 # various unpack count/code scenarios
49704364
WL
1213 my @Env = ( a => 'AAA', b => 'BBB' );
1214 my $env = pack( 'S(S/A*S/A*)*', @Env/2, @Env );
1215
1216 # unpack full length - ok
1217 my @pup = unpack( 'S/(S/A* S/A*)', $env );
1218 is( "@pup", "@Env" );
1219
1220 # warn when count/code goes beyond end of string
1221 # \0002 \0001 a \0003 AAA \0001 b \0003 BBB
1222 # 2 4 5 7 10 1213
1223 eval { @pup = unpack( 'S/(S/A* S/A*)', substr( $env, 0, 13 ) ) };
1224 like( $@, qr{length/code after end of string} );
28be1210 1225
49704364
WL
1226 # postfix repeat count
1227 $env = pack( '(S/A* S/A*)' . @Env/2, @Env );
1228
1229 # warn when count/code goes beyond end of string
1230 # \0001 a \0003 AAA \0001 b \0003 BBB
1231 # 2 3c 5 8 10 11 13 16
1232 eval { @pup = unpack( '(S/A* S/A*)' . @Env/2, substr( $env, 0, 11 ) ) };
1233 like( $@, qr{length/code after end of string} );
1234
1235 # catch stack overflow/segfault
1236 eval { $_ = pack( ('(' x 105) . 'A' . (')' x 105) ); };
1237 like( $@, qr{Too deeply nested \(\)-groups} );
1238}
1239
1240{ # syntax checks (W.Laun)
bd7d4f4d 1241 use warnings qw(NONFATAL all);;
49704364
WL
1242 my @warning;
1243 local $SIG{__WARN__} = sub {
1244 push( @warning, $_[0] );
1245 };
1246 eval { my $s = pack( 'Ax![4c]A', 1..5 ); };
1247 like( $@, qr{Malformed integer in \[\]} );
1248
1249 eval { my $buf = pack( '(c/*a*)', 'AAA', 'BB' ); };
1250 like( $@, qr{'/' does not take a repeat count} );
1251
1252 eval { my @inf = unpack( 'c/1a', "\x03AAA\x02BB" ); };
1253 like( $@, qr{'/' does not take a repeat count} );
1254
1255 eval { my @inf = unpack( 'c/*a', "\x03AAA\x02BB" ); };
1256 like( $@, qr{'/' does not take a repeat count} );
1257
28be1210 1258 # white space where possible
49704364
WL
1259 my @Env = ( a => 'AAA', b => 'BBB' );
1260 my $env = pack( ' S ( S / A* S / A* )* ', @Env/2, @Env );
1261 my @pup = unpack( ' S / ( S / A* S / A* ) ', $env );
1262 is( "@pup", "@Env" );
1263
1264 # white space in 4 wrong places
1265 for my $temp ( 'A ![4]', 'A [4]', 'A *', 'A 4' ){
1266 eval { my $s = pack( $temp, 'B' ); };
1267 like( $@, qr{Invalid type } );
1268 }
1269
1270 # warning for commas
1271 @warning = ();
1272 my $x = pack( 'I,A', 4, 'X' );
1273 like( $warning[0], qr{Invalid type ','} );
1274
1275 # comma warning only once
1276 @warning = ();
1277 $x = pack( 'C(C,C)C,C', 65..71 );
1278 like( scalar @warning, 1 );
1279
1280 # forbidden code in []
1281 eval { my $x = pack( 'A[@4]', 'XXXX' ); };
1282 like( $@, qr{Within \[\]-length '\@' not allowed} );
1283
1284 # @ repeat default 1
1285 my $s = pack( 'AA@A', 'A', 'B', 'C' );
1286 my @c = unpack( 'AA@A', $s );
28be1210
TH
1287 is( $s, 'AC' );
1288 is( "@c", "A C C" );
49704364
WL
1289
1290 # no unpack code after /
1291 eval { my @a = unpack( "C/", "\3" ); };
1292 like( $@, qr{Code missing after '/'} );
1293
d9db00a6
NC
1294 SKIP: {
1295 skip $no_endianness, 6 if $no_endianness;
1296
1297 # modifier warnings
1298 @warning = ();
1299 $x = pack "I>>s!!", 47, 11;
1300 ($x) = unpack "I<<l!>!>", 'x'x20;
1301 is(scalar @warning, 5);
1302 like($warning[0], qr/Duplicate modifier '>' after 'I' in pack/);
1303 like($warning[1], qr/Duplicate modifier '!' after 's' in pack/);
1304 like($warning[2], qr/Duplicate modifier '<' after 'I' in unpack/);
1305 like($warning[3], qr/Duplicate modifier '!' after 'l' in unpack/);
1306 like($warning[4], qr/Duplicate modifier '>' after 'l' in unpack/);
1307 }
49704364
WL
1308}
1309
206947d2 1310{ # Repeat count [SUBEXPR]
f337b084 1311 my @codes = qw( x A Z a c C W B b H h s v n S i I l V N L p P f F d
92d41999
JH
1312 s! S! i! I! l! L! j J);
1313 my $G;
206947d2
IZ
1314 if (eval { pack 'q', 1 } ) {
1315 push @codes, qw(q Q);
1316 } else {
1109a392 1317 push @codes, qw(s S); # Keep the count the same
206947d2 1318 }
92d41999
JH
1319 if (eval { pack 'D', 1 } ) {
1320 push @codes, 'D';
1321 } else {
1322 push @codes, 'd'; # Keep the count the same
1323 }
206947d2 1324
1109a392
MHM
1325 push @codes, map { /^[silqjfdp]/i ? ("$_<", "$_>") : () } @codes;
1326
206947d2
IZ
1327 my %val;
1328 @val{@codes} = map { / [Xx] (?{ undef })
1329 | [AZa] (?{ 'something' })
1330 | C (?{ 214 })
f337b084 1331 | W (?{ 8188 })
206947d2
IZ
1332 | c (?{ 114 })
1333 | [Bb] (?{ '101' })
1334 | [Hh] (?{ 'b8' })
92d41999 1335 | [svnSiIlVNLqQjJ] (?{ 10111 })
206947d2
IZ
1336 | [FfDd] (?{ 1.36514538e67 })
1337 | [pP] (?{ "try this buffer" })
1338 /x; $^R } @codes;
1339 my @end = (0x12345678, 0x23456781, 0x35465768, 0x15263748);
1340 my $end = "N4";
1341
1342 for my $type (@codes) {
1343 my @list = $val{$type};
1344 @list = () unless defined $list[0];
1345 for my $count ('', '3', '[11]') {
1346 my $c = 1;
1347 $c = $1 if $count =~ /(\d+)/;
1348 my @list1 = @list;
1349 @list1 = (@list1) x $c unless $type =~ /[XxAaZBbHhP]/;
1350 for my $groupend ('', ')2', ')[8]') {
1351 my $groupbegin = ($groupend ? '(' : '');
1352 $c = 1;
1353 $c = $1 if $groupend =~ /(\d+)/;
1354 my @list2 = (@list1) x $c;
1355
1109a392
MHM
1356 SKIP: {
1357 my $junk1 = "$groupbegin $type$count $groupend";
1358 # print "# junk1=$junk1\n";
1359 my $p = eval { pack $junk1, @list2 };
1360 skip "cannot pack '$type' on this perl", 12
1361 if is_valid_error($@);
9b72c5e4 1362 die "pack $junk1 failed: $@" if $@;
1109a392
MHM
1363
1364 my $half = int( (length $p)/2 );
1365 for my $move ('', "X$half", "X!$half", 'x1', 'x!8', "x$half") {
1366 my $junk = "$junk1 $move";
1367 # print "# junk='$junk', list=(@list2)\n";
1368 $p = pack "$junk $end", @list2, @end;
1369 my @l = unpack "x[$junk] $end", $p;
1370 is(scalar @l, scalar @end);
1371 is("@l", "@end", "skipping x[$junk]");
1372 }
1373 }
206947d2
IZ
1374 }
1375 }
1376 }
1377}
1378
1379# / is recognized after spaces in scalar context
1380# XXXX no spaces are allowed in pack... In pack only before the slash...
1381is(scalar unpack('A /A Z20', pack 'A/A* Z20', 'bcde', 'xxxxx'), 'bcde');
1382is(scalar unpack('A /A /A Z20', '3004bcde'), 'bcde');
62f95557
IZ
1383
1384{ # X! and x!
1385 my $t = 'C[3] x!8 C[2]';
1386 my @a = (0x73..0x77);
1387 my $p = pack($t, @a);
1388 is($p, "\x73\x74\x75\0\0\0\0\0\x76\x77");
1389 my @b = unpack $t, $p;
1390 is(scalar @b, scalar @a);
1391 is("@b", "@a", 'x!8');
1392 $t = 'x[5] C[6] X!8 C[2]';
1393 @a = (0x73..0x7a);
1394 $p = pack($t, @a);
1395 is($p, "\0\0\0\0\0\x73\x74\x75\x79\x7a");
1396 @b = unpack $t, $p;
1397 @a = (0x73..0x75, 0x79, 0x7a, 0x79, 0x7a);
1398 is(scalar @b, scalar @a);
1399 is("@b", "@a");
1400}
1401
1402{ # struct {char c1; double d; char cc[2];}
1403 my $t = 'C x![d] d C[2]';
1404 my @a = (173, 1.283476517e-45, 42, 215);
1405 my $p = pack $t, @a;
1406 ok( length $p);
1407 my @b = unpack "$t X[$t] $t", $p; # Extract, step back, extract again
1408 is(scalar @b, 2 * scalar @a);
3f7e3417
JH
1409 $b = "@b";
1410 $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble
1411 is($b, "@a @a");
62f95557 1412
bd7d4f4d 1413 use warnings qw(NONFATAL all);;
62f95557
IZ
1414 my $warning;
1415 local $SIG{__WARN__} = sub {
1416 $warning = $_[0];
1417 };
1418 @b = unpack "x[C] x[$t] X[$t] X[C] $t", "$p\0";
1419
1420 is($warning, undef);
1421 is(scalar @b, scalar @a);
3f7e3417
JH
1422 $b = "@b";
1423 $b =~ s/(?:17000+|16999+)\d+(e-45) /17$1 /gi; # stringification is gamble
1424 is($b, "@a");
62f95557 1425}
92d41999
JH
1426
1427is(length(pack("j", 0)), $Config{ivsize});
1428is(length(pack("J", 0)), $Config{uvsize});
1429is(length(pack("F", 0)), $Config{nvsize});
1430
1431numbers ('j', -2147483648, -1, 0, 1, 2147483647);
1432numbers ('J', 0, 1, 2147483647, 2147483648, 4294967295);
1433numbers ('F', -(2**34), -1, 0, 1, 2**34);
1434SKIP: {
1435 my $t = eval { unpack("D*", pack("D", 12.34)) };
1436
1109a392 1437 skip "Long doubles not in use", 166 if $@ =~ /Invalid type/;
92d41999
JH
1438
1439 is(length(pack("D", 0)), $Config{longdblsize});
1440 numbers ('D', -(2**34), -1, 0, 1, 2**34);
1441}
1442
c8f824eb
NC
1443# Maybe this knowledge needs to be "global" for all of pack.t
1444# Or a "can checksum" which would effectively be all the number types"
1445my %cant_checksum = map {$_=> 1} qw(A Z u w);
1446# not a b B h H
1447foreach 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)) {
1448 SKIP: {
1449 my $packed = eval {pack "${template}4", 1, 4, 9, 16};
1450 if ($@) {
49704364 1451 die unless $@ =~ /Invalid type '$template'/;
c8f824eb
NC
1452 skip ("$template not supported on this perl",
1453 $cant_checksum{$template} ? 4 : 8);
1454 }
1455 my @unpack4 = unpack "${template}4", $packed;
1456 my @unpack = unpack "${template}*", $packed;
1457 my @unpack1 = unpack "${template}", $packed;
1458 my @unpack1s = scalar unpack "${template}", $packed;
1459 my @unpack4s = scalar unpack "${template}4", $packed;
1460 my @unpacks = scalar unpack "${template}*", $packed;
1461
1462 my @tests = ( ["${template}4 vs ${template}*", \@unpack4, \@unpack],
1463 ["scalar ${template} ${template}", \@unpack1s, \@unpack1],
1464 ["scalar ${template}4 vs ${template}", \@unpack4s, \@unpack1],
1465 ["scalar ${template}* vs ${template}", \@unpacks, \@unpack1],
1466 );
1467
1468 unless ($cant_checksum{$template}) {
1469 my @unpack4_c = unpack "\%${template}4", $packed;
1470 my @unpack_c = unpack "\%${template}*", $packed;
1471 my @unpack1_c = unpack "\%${template}", $packed;
1472 my @unpack1s_c = scalar unpack "\%${template}", $packed;
1473 my @unpack4s_c = scalar unpack "\%${template}4", $packed;
1474 my @unpacks_c = scalar unpack "\%${template}*", $packed;
1475
1476 push @tests,
1477 ( ["% ${template}4 vs ${template}*", \@unpack4_c, \@unpack_c],
1478 ["% scalar ${template} ${template}", \@unpack1s_c, \@unpack1_c],
1479 ["% scalar ${template}4 vs ${template}*", \@unpack4s_c, \@unpack_c],
1480 ["% scalar ${template}* vs ${template}*", \@unpacks_c, \@unpack_c],
1481 );
1482 }
1483 foreach my $test (@tests) {
1484 ok (list_eq ($test->[1], $test->[2]), $test->[0]) ||
1485 printf "# unpack gave %s expected %s\n",
1486 encode_list (@{$test->[1]}), encode_list (@{$test->[2]});
1487 }
1488 }
1489}
9c3dc587
HS
1490
1491ok(pack('u2', 'AA'), "[perl #8026]"); # used to hang and eat RAM in perl 5.7.2
1492
fe581ec7
JH
1493$_ = pack('c', 65); # 'A' would not be EBCDIC-friendly
1494is(unpack('c'), 65, "one-arg unpack (change #18751)"); # defaulting to $_
db37a92e
JH
1495
1496{
250d67eb 1497 my $a = "X\x0901234567\n" x 100; # \t would not be EBCDIC TAB
db37a92e
JH
1498 my @a = unpack("(a1 c/a)*", $a);
1499 is(scalar @a, 200, "[perl #15288]");
1500 is($a[-1], "01234567\n", "[perl #15288]");
1501 is($a[-2], "X", "[perl #15288]");
1502}
0ed7c1bb 1503
f7f98fdf 1504{
bd7d4f4d 1505 use warnings qw(NONFATAL all);;
f7f98fdf
TH
1506 my $warning;
1507 local $SIG{__WARN__} = sub {
1508 $warning = $_[0];
1509 };
1510 my $out = pack("u99", "foo" x 99);
1511 like($warning, qr/Field too wide in 'u' format in pack at /,
1512 "Warn about too wide uuencode");
1513 is($out, ("_" . "9F]O" x 21 . "\n") x 4 . "M" . "9F]O" x 15 . "\n",
1514 "Use max width in case of too wide uuencode");
1515}
1516
0ed7c1bb
TH
1517# checksums
1518{
1519 # verify that unpack advances correctly wrt a checksum
1520 my (@x) = unpack("b10a", "abcd");
1521 my (@y) = unpack("%b10a", "abcd");
1522 is($x[1], $y[1], "checksum advance ok");
d6d3e8bd
TH
1523
1524 # verify that the checksum is not overflowed with C0
250d67eb
JH
1525 if (ord('A') == 193) {
1526 is(unpack("C0%128U", "/bcd"), unpack("U0%128U", "abcd"), "checksum not overflowed");
1527 } else {
1528 is(unpack("C0%128U", "abcd"), unpack("U0%128U", "abcd"), "checksum not overflowed");
1529 }
0ed7c1bb 1530}
0c81e54b
TH
1531
1532{
1533 # U0 and C0 must be scoped
1534 my (@x) = unpack("a(U0)U", "b\341\277\274");
1535 is($x[0], 'b', 'before scope');
08ca2aa3 1536 is($x[1], 8188, 'after scope');
f337b084
TH
1537
1538 is(pack("a(U0)U", "b", 8188), "b\341\277\274");
0c81e54b 1539}
21c16052
TH
1540
1541{
1542 # counted length prefixes shouldn't change C0/U0 mode
1543 # (note the length is actually 0 in this test)
250d67eb
JH
1544 if (ord('A') == 193) {
1545 is(join(',', unpack("aU0C/UU", "b\0\341\277\274")), 'b,0');
1546 is(join(',', unpack("aU0C/CU", "b\0\341\277\274")), 'b,0');
1547 } else {
1548 is(join(',', unpack("aC/UU", "b\0\341\277\274")), 'b,8188');
1549 is(join(',', unpack("aC/CU", "b\0\341\277\274")), 'b,8188');
1550 is(join(',', unpack("aU0C/UU", "b\0\341\277\274")), 'b,225');
1551 is(join(',', unpack("aU0C/CU", "b\0\341\277\274")), 'b,225');
1552 }
21c16052 1553}
f0df5f8b
TH
1554
1555{
1556 # "Z0" (bug #34062)
1557 my (@x) = unpack("C*", pack("CZ0", 1, "b"));
1558 is(join(',', @x), '1', 'pack Z0 doesn\'t destroy the character before');
1559}
f337b084
TH
1560
1561{
1562 # Encoding neutrality
1563 # String we will pull apart and rebuild in several ways:
1564 my $down = "\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\x05\x06";
1565 my $up = $down;
1566 utf8::upgrade($up);
1567
1568 my %expect =
1569 # [expected result,
1570 # how many chars it should progress,
1571 # (optional) expected result of pack]
1572 (a5 => ["\xf8\xf9\xfa\xfb\xfc", 5],
1573 A5 => ["\xf8\xf9\xfa\xfb\xfc", 5],
1574 Z5 => ["\xf8\xf9\xfa\xfb\xfc", 5, "\xf8\xf9\xfa\xfb\x00\xfd"],
1575 b21 => ["000111111001111101011", 3, "\xf8\xf9\x1a\xfb"],
1576 B21 => ["111110001111100111111", 3, "\xf8\xf9\xf8\xfb"],
1577 H5 => ["f8f9f", 3, "\xf8\xf9\xf0\xfb"],
1578 h5 => ["8f9fa", 3, "\xf8\xf9\x0a\xfb"],
1579 "s<" => [-1544, 2],
1580 "s>" => [-1799, 2],
1581 "S<" => [0xf9f8, 2],
1582 "S>" => [0xf8f9, 2],
1583 "l<" => [-67438088, 4],
1584 "l>" => [-117835013, 4],
1585 "L>" => [0xf8f9fafb, 4],
1586 "L<" => [0xfbfaf9f8, 4],
1587 n => [0xf8f9, 2],
1588 N => [0xf8f9fafb, 4],
1589 v => [63992, 2],
1590 V => [0xfbfaf9f8, 4],
1591 c => [-8, 1],
1592 U0U => [0xf8, 1],
1593 w => ["8715569050387726213", 9],
1594 q => ["-283686952306184", 8],
1595 Q => ["18446460386757245432", 8],
1596 );
1597
1598 for my $string ($down, $up) {
1599 for my $format (sort {lc($a) cmp lc($b) || $a cmp $b } keys %expect) {
1600 SKIP: {
1601 my $expect = $expect{$format};
1602 # unpack upgraded and downgraded string
1603 my @result = eval { unpack("$format C0 W", $string) };
1604 skip "cannot pack/unpack '$format C0 W' on this perl", 5 if
1605 $@ && is_valid_error($@);
1606 is(@result, 2, "Two results from unpack $format C0 W");
1607
1608 # pack to downgraded
1609 my $new = pack("$format C0 W", @result);
1610 is(length($new), $expect->[1]+1,
1611 "pack $format C0 W should give $expect->[1]+1 chars");
1612 is($new, $expect->[2] || substr($string, 0, length $new),
1613 "pack $format C0 W returns expected value");
1614
1615 # pack to upgraded
1616 $new = pack("a0 $format C0 W", chr(256), @result);
1617 is(length($new), $expect->[1]+1,
1618 "pack a0 $format C0 W should give $expect->[1]+1 chars");
1619 is($new, $expect->[2] || substr($string, 0, length $new),
1620 "pack a0 $format C0 W returns expected value");
1621 }
1622 }
1623 }
1624}
1625
1626{
1627 # Encoding neutrality, numbers
1628 my $val = -2.68;
1629 for my $format (qw(s S i I l L j J f d F D q Q
1630 s! S! i! I! l! L! n! N! v! V!)) {
1631 SKIP: {
1632 my $down = eval { pack($format, $val) };
1633 skip "cannot pack/unpack $format on this perl", 9 if
1634 $@ && is_valid_error($@);
1635 ok(!utf8::is_utf8($down), "Simple $format pack doesn't get upgraded");
1636 my $up = pack("a0 $format", chr(256), $val);
1637 ok(utf8::is_utf8($up), "a0 $format with high char leads to upgrade");
1638 is($down, $up, "$format generated strings are equal though");
1639 my @down_expanded = unpack("$format W", $down . chr(0xce));
1640 is(@down_expanded, 2, "Expand to two values");
1641 is($down_expanded[1], 0xce,
1642 "unpack $format left us at the expected position");
1643 my @up_expanded = unpack("$format W", $up . chr(0xce));
1644 is(@up_expanded, 2, "Expand to two values");
1645 is($up_expanded[1], 0xce,
1646 "unpack $format left us at the expected position");
1647 is($down_expanded[0], $up_expanded[0], "$format unpack was neutral");
1648 is(pack($format, $down_expanded[0]), $down, "Pack $format undoes unpack $format");
1649 }
1650 }
1651}
1652
1653{
1651fc44 1654 # C *is* neutral
f337b084
TH
1655 my $down = "\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\x05\x06";
1656 my $up = $down;
1657 utf8::upgrade($up);
1658 my @down = unpack("C*", $down);
1659 my @expect_down = (0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0x05, 0x06);
1660 is("@down", "@expect_down", "byte expand");
1661 is(pack("C*", @down), $down, "byte join");
1662
1663 my @up = unpack("C*", $up);
1651fc44 1664 my @expect_up = (0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0x05, 0x06);
f337b084
TH
1665 is("@up", "@expect_up", "UTF-8 expand");
1666 is(pack("U0C0C*", @up), $up, "UTF-8 join");
1667}
1668
1669{
1670 # Harder cases for the neutrality test
1671
1672 # u format
1673 my $down = "\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\x05\x06";
1674 my $up = $down;
1675 utf8::upgrade($up);
1676 is(pack("u", $down), pack("u", $up), "u pack is neutral");
1677 is(unpack("u", pack("u", $down)), $down, "u unpack to downgraded works");
1678 is(unpack("U0C0u", pack("u", $down)), $up, "u unpack to upgraded works");
1679
1680 # p/P format
1681 # This actually only tests something if the address contains a byte >= 0x80
1682 my $str = "abc\xa5\x00\xfede";
1683 $down = pack("p", $str);
1684 is(pack("P", $str), $down);
1685 is(pack("U0C0p", $str), $down);
1686 is(pack("U0C0P", $str), $down);
1687 is(unpack("p", $down), "abc\xa5", "unpack p downgraded");
1688 $up = $down;
1689 utf8::upgrade($up);
1690 is(unpack("p", $up), "abc\xa5", "unpack p upgraded");
1691
1692 is(unpack("P7", $down), "abc\xa5\x00\xfed", "unpack P downgraded");
1693 is(unpack("P7", $up), "abc\xa5\x00\xfed", "unpack P upgraded");
1694
1695 # x, X and @
1696 $down = "\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\x05\x06";
1697 $up = $down;
1698 utf8::upgrade($up);
1699
1700 is(unpack('@4W', $down), 0xfc, "\@positioning on downgraded string");
1701 is(unpack('@4W', $up), 0xfc, "\@positioning on upgraded string");
1702
1703 is(unpack('@4x2W', $down), 0xfe, "x moving on downgraded string");
1704 is(unpack('@4x2W', $up), 0xfe, "x moving on upgraded string");
1705 is(unpack('@4x!4W', $down), 0xfc, "x! moving on downgraded string");
1706 is(unpack('@4x!4W', $up), 0xfc, "x! moving on upgraded string");
1707 is(unpack('@5x!4W', $down), 0x05, "x! moving on downgraded string");
1708 is(unpack('@5x!4W', $up), 0x05, "x! moving on upgraded string");
1709
1710 is(unpack('@4X2W', $down), 0xfa, "X moving on downgraded string");
1711 is(unpack('@4X2W', $up), 0xfa, "X moving on upgraded string");
1712 is(unpack('@4X!4W', $down), 0xfc, "X! moving on downgraded string");
1713 is(unpack('@4X!4W', $up), 0xfc, "X! moving on upgraded string");
1714 is(unpack('@5X!4W', $down), 0xfc, "X! moving on downgraded string");
1715 is(unpack('@5X!4W', $up), 0xfc, "X! moving on upgraded string");
1716 is(unpack('@5X!8W', $down), 0xf8, "X! moving on downgraded string");
1717 is(unpack('@5X!8W', $up), 0xf8, "X! moving on upgraded string");
1718
1719 is(pack("W2x", 0xfa, 0xe3), "\xfa\xe3\x00", "x on downgraded string");
28be1210 1720 is(pack("W2x!4", 0xfa, 0xe3), "\xfa\xe3\x00\x00",
f337b084
TH
1721 "x! on downgraded string");
1722 is(pack("W2x!2", 0xfa, 0xe3), "\xfa\xe3", "x! on downgraded string");
1723 is(pack("U0C0W2x", 0xfa, 0xe3), "\xfa\xe3\x00", "x on upgraded string");
28be1210 1724 is(pack("U0C0W2x!4", 0xfa, 0xe3), "\xfa\xe3\x00\x00",
f337b084
TH
1725 "x! on upgraded string");
1726 is(pack("U0C0W2x!2", 0xfa, 0xe3), "\xfa\xe3", "x! on upgraded string");
1727 is(pack("W2X", 0xfa, 0xe3), "\xfa", "X on downgraded string");
1728 is(pack("U0C0W2X", 0xfa, 0xe3), "\xfa", "X on upgraded string");
1729 is(pack("W2X!2", 0xfa, 0xe3), "\xfa\xe3", "X! on downgraded string");
1730 is(pack("U0C0W2X!2", 0xfa, 0xe3), "\xfa\xe3", "X! on upgraded string");
1731 is(pack("W3X!2", 0xfa, 0xe3, 0xa6), "\xfa\xe3", "X! on downgraded string");
28be1210 1732 is(pack("U0C0W3X!2", 0xfa, 0xe3, 0xa6), "\xfa\xe3",
f337b084
TH
1733 "X! on upgraded string");
1734
1735 # backward eating through a ( moves the group starting point backwards
28be1210 1736 is(pack("a*(Xa)", "abc", "q"), "abq",
f337b084 1737 "eating before strbeg moves it back");
28be1210 1738 is(pack("a*(Xa)", "ab" . chr(512), "q"), "abq",
f337b084
TH
1739 "eating before strbeg moves it back");
1740
1741 # Check marked_upgrade
1742 is(pack('W(W(Wa@3W)@6W)@9W', 0xa1, 0xa2, 0xa3, "a", 0xa4, 0xa5, 0xa6),
1743 "\xa1\xa2\xa3a\x00\xa4\x00\xa5\x00\xa6");
1744 $up = "a";
1745 utf8::upgrade($up);
1746 is(pack('W(W(Wa@3W)@6W)@9W', 0xa1, 0xa2, 0xa3, $up, 0xa4, 0xa5, 0xa6),
1747 "\xa1\xa2\xa3a\x00\xa4\x00\xa5\x00\xa6", "marked upgrade caused by a");
1748 is(pack('W(W(WW@3W)@6W)@9W', 0xa1, 0xa2, 0xa3, 256, 0xa4, 0xa5, 0xa6),
28be1210 1749 "\xa1\xa2\xa3\x{100}\x00\xa4\x00\xa5\x00\xa6",
f337b084
TH
1750 "marked upgrade caused by W");
1751 is(pack('W(W(WU0aC0@3W)@6W)@9W', 0xa1, 0xa2, 0xa3, "a", 0xa4, 0xa5, 0xa6),
1752 "\xa1\xa2\xa3a\x00\xa4\x00\xa5\x00\xa6", "marked upgrade caused by U0");
1753
1754 # a, A and Z
1755 $down = "\xa4\xa6\xa7";
1756 $up = $down;
1757 utf8::upgrade($up);
1758 utf8::upgrade(my $high = "\xfeb");
1759
1760 for my $format ("a0", "A0", "Z0", "U0a0C0", "U0A0C0", "U0Z0C0") {
28be1210 1761 is(pack("a* $format a*", "ab", $down, "cd"), "abcd",
f337b084
TH
1762 "$format format on plain string");
1763 is(pack("a* $format a*", "ab", $up, "cd"), "abcd",
1764 "$format format on upgraded string");
28be1210 1765 is(pack("a* $format a*", $high, $down, "cd"), "\xfebcd",
f337b084
TH
1766 "$format format on plain string");
1767 is(pack("a* $format a*", $high, $up, "cd"), "\xfebcd",
1768 "$format format on upgraded string");
1769 my @down = unpack("a1 $format a*", "\xfeb");
1770 is("@down", "\xfe b", "unpack $format");
1771 my @up = unpack("a1 $format a*", $high);
1772 is("@up", "\xfe b", "unpack $format");
1773 }
1774 is(pack("a1", $high), "\xfe");
1775 is(pack("A1", $high), "\xfe");
1776 is(pack("Z1", $high), "\x00");
1777 is(pack("a2", $high), "\xfeb");
1778 is(pack("A2", $high), "\xfeb");
1779 is(pack("Z2", $high), "\xfe\x00");
1780 is(pack("a5", $high), "\xfeb\x00\x00\x00");
1781 is(pack("A5", $high), "\xfeb ");
1782 is(pack("Z5", $high), "\xfeb\x00\x00\x00");
1783 is(pack("a*", $high), "\xfeb");
1784 is(pack("A*", $high), "\xfeb");
1785 is(pack("Z*", $high), "\xfeb\x00");
1786
1787 utf8::upgrade($high = "\xc3\xbeb");
1788 is(pack("U0a2", $high), "\xfe");
1789 is(pack("U0A2", $high), "\xfe");
1790 is(pack("U0Z1", $high), "\x00");
1791 is(pack("U0a3", $high), "\xfeb");
1792 is(pack("U0A3", $high), "\xfeb");
1793 is(pack("U0Z3", $high), "\xfe\x00");
1794 is(pack("U0a6", $high), "\xfeb\x00\x00\x00");
1795 is(pack("U0A6", $high), "\xfeb ");
1796 is(pack("U0Z6", $high), "\xfeb\x00\x00\x00");
1797 is(pack("U0a*", $high), "\xfeb");
1798 is(pack("U0A*", $high), "\xfeb");
1799 is(pack("U0Z*", $high), "\xfeb\x00");
1800}
246f24af
TH
1801{
1802 # pack /
1803 my @array = 1..14;
1804 my @out = unpack("N/S", pack("N/S", @array) . "abcd");
1805 is("@out", "@array", "pack N/S works");
1806 @out = unpack("N/S*", pack("N/S*", @array) . "abcd");
1807 is("@out", "@array", "pack N/S* works");
1808 @out = unpack("N/S*", pack("N/S14", @array) . "abcd");
1809 is("@out", "@array", "pack N/S14 works");
1810 @out = unpack("N/S*", pack("N/S15", @array) . "abcd");
1811 is("@out", "@array", "pack N/S15 works");
1812 @out = unpack("N/S*", pack("N/S13", @array) . "abcd");
1813 is("@out", "@array[0..12]", "pack N/S13 works");
1814 @out = unpack("N/S*", pack("N/S0", @array) . "abcd");
1815 is("@out", "", "pack N/S0 works");
1816 is(pack("Z*/a0", "abc"), "0\0", "pack Z*/a0 makes a short string");
1817 is(pack("Z*/Z0", "abc"), "0\0", "pack Z*/Z0 makes a short string");
1818 is(pack("Z*/a3", "abc"), "3\0abc", "pack Z*/a3 makes a full string");
1819 is(pack("Z*/Z3", "abc"), "3\0ab\0", "pack Z*/Z3 makes a short string");
1820 is(pack("Z*/a5", "abc"), "5\0abc\0\0", "pack Z*/a5 makes a long string");
1821 is(pack("Z*/Z5", "abc"), "5\0abc\0\0", "pack Z*/Z5 makes a long string");
1822 is(pack("Z*/Z"), "1\0\0", "pack Z*/Z makes an extended string");
1823 is(pack("Z*/Z", ""), "1\0\0", "pack Z*/Z makes an extended string");
1824 is(pack("Z*/a", ""), "0\0", "pack Z*/a makes an extended string");
1825}
18bdf90a
TH
1826{
1827 # unpack("A*", $unicode) strips general unicode spaces
28be1210 1828 is(unpack("A*", "ab \n\xa0 \0"), "ab \n\xa0",
18bdf90a 1829 'normal A* strip leaves \xa0');
28be1210 1830 is(unpack("U0C0A*", "ab \n\xa0 \0"), "ab \n\xa0",
18bdf90a
TH
1831 'normal A* strip leaves \xa0 even if it got upgraded for technical reasons');
1832 is(unpack("A*", pack("a*(U0U)a*", "ab \n", 0xa0, " \0")), "ab",
1833 'upgraded strings A* removes \xa0');
1834 is(unpack("A*", pack("a*(U0UU)a*", "ab \n", 0xa0, 0x1680, " \0")), "ab",
1835 'upgraded strings A* removes all unicode whitespace');
1836 is(unpack("A5", pack("a*(U0U)a*", "ab \n", 0x1680, "def", "ab")), "ab",
1837 'upgraded strings A5 removes all unicode whitespace');
1838 is(unpack("A*", pack("U", 0x1680)), "",
1839 'upgraded strings A* with nothing left');
1840}
28be1210
TH
1841{
1842 # Testing unpack . and .!
1843 is(unpack(".", "ABCD"), 0, "offset at start of string is 0");
1844 is(unpack(".", ""), 0, "offset at start of empty string is 0");
1845 is(unpack("x3.", "ABCDEF"), 3, "simple offset works");
1846 is(unpack("x3.", "ABC"), 3, "simple offset at end of string works");
1847 is(unpack("x3.0", "ABC"), 0, "self offset is 0");
1848 is(unpack("x3(x2.)", "ABCDEF"), 2, "offset is relative to inner group");
1849 is(unpack("x3(X2.)", "ABCDEF"), -2,
1850 "negative offset relative to inner group");
1851 is(unpack("x3(X2.2)", "ABCDEF"), 1, "offset is relative to inner group");
1852 is(unpack("x3(x2.0)", "ABCDEF"), 0, "self offset in group is still 0");
1853 is(unpack("x3(x2.2)", "ABCDEF"), 5, "offset counts groups");
1854 is(unpack("x3(x2.*)", "ABCDEF"), 5, "star offset is relative to start");
1855
1856 my $high = chr(8188) x 6;
1857 is(unpack("x3(x2.)", $high), 2, "utf8 offset is relative to inner group");
1858 is(unpack("x3(X2.)", $high), -2,
1859 "utf8 negative offset relative to inner group");
1860 is(unpack("x3(X2.2)", $high), 1, "utf8 offset counts groups");
1861 is(unpack("x3(x2.0)", $high), 0, "utf8 self offset in group is still 0");
1862 is(unpack("x3(x2.2)", $high), 5, "utf8 offset counts groups");
1863 is(unpack("x3(x2.*)", $high), 5, "utf8 star offset is relative to start");
1864
1865 is(unpack("U0x3(x2.)", $high), 2,
1866 "U0 mode utf8 offset is relative to inner group");
1867 is(unpack("U0x3(X2.)", $high), -2,
1868 "U0 mode utf8 negative offset relative to inner group");
1869 is(unpack("U0x3(X2.2)", $high), 1,
1870 "U0 mode utf8 offset counts groups");
1871 is(unpack("U0x3(x2.0)", $high), 0,
1872 "U0 mode utf8 self offset in group is still 0");
1873 is(unpack("U0x3(x2.2)", $high), 5,
1874 "U0 mode utf8 offset counts groups");
1875 is(unpack("U0x3(x2.*)", $high), 5,
1876 "U0 mode utf8 star offset is relative to start");
1877
1878 is(unpack("x3(x2.!)", $high), 2*3,
1879 "utf8 offset is relative to inner group");
1880 is(unpack("x3(X2.!)", $high), -2*3,
1881 "utf8 negative offset relative to inner group");
1882 is(unpack("x3(X2.!2)", $high), 1*3,
1883 "utf8 offset counts groups");
1884 is(unpack("x3(x2.!0)", $high), 0,
1885 "utf8 self offset in group is still 0");
1886 is(unpack("x3(x2.!2)", $high), 5*3,
1887 "utf8 offset counts groups");
1888 is(unpack("x3(x2.!*)", $high), 5*3,
1889 "utf8 star offset is relative to start");
1890
1891 is(unpack("U0x3(x2.!)", $high), 2,
1892 "U0 mode utf8 offset is relative to inner group");
1893 is(unpack("U0x3(X2.!)", $high), -2,
1894 "U0 mode utf8 negative offset relative to inner group");
1895 is(unpack("U0x3(X2.!2)", $high), 1,
1896 "U0 mode utf8 offset counts groups");
1897 is(unpack("U0x3(x2.!0)", $high), 0,
1898 "U0 mode utf8 self offset in group is still 0");
1899 is(unpack("U0x3(x2.!2)", $high), 5,
1900 "U0 mode utf8 offset counts groups");
1901 is(unpack("U0x3(x2.!*)", $high), 5,
1902 "U0 mode utf8 star offset is relative to start");
1903}
1904{
1905 # Testing pack . and .!
1906 is(pack("(a)5 .", 1..5, 3), "123", ". relative to string start, shorten");
1907 eval { () = pack("(a)5 .", 1..5, -3) };
1908 like($@, qr{'\.' outside of string in pack}, "Proper error message");
1909 is(pack("(a)5 .", 1..5, 8), "12345\x00\x00\x00",
1910 ". relative to string start, extend");
1911 is(pack("(a)5 .", 1..5, 5), "12345", ". relative to string start, keep");
1912
1913 is(pack("(a)5 .0", 1..5, -3), "12",
1914 ". relative to string current, shorten");
1915 is(pack("(a)5 .0", 1..5, 2), "12345\x00\x00",
1916 ". relative to string current, extend");
1917 is(pack("(a)5 .0", 1..5, 0), "12345",
1918 ". relative to string current, keep");
1919
1920 is(pack("(a)5 (.)", 1..5, -3), "12",
1921 ". relative to group, shorten");
1922 is(pack("(a)5 (.)", 1..5, 2), "12345\x00\x00",
1923 ". relative to group, extend");
1924 is(pack("(a)5 (.)", 1..5, 0), "12345",
1925 ". relative to group, keep");
1926
1927 is(pack("(a)3 ((a)2 .)", 1..5, -2), "1",
1928 ". relative to group, shorten");
1929 is(pack("(a)3 ((a)2 .)", 1..5, 2), "12345",
1930 ". relative to group, keep");
1931 is(pack("(a)3 ((a)2 .)", 1..5, 4), "12345\x00\x00",
1932 ". relative to group, extend");
1933
1934 is(pack("(a)3 ((a)2 .2)", 1..5, 2), "12",
1935 ". relative to counted group, shorten");
1936 is(pack("(a)3 ((a)2 .2)", 1..5, 7), "12345\x00\x00",
1937 ". relative to counted group, extend");
1938 is(pack("(a)3 ((a)2 .2)", 1..5, 5), "12345",
1939 ". relative to counted group, keep");
1940
1941 is(pack("(a)3 ((a)2 .*)", 1..5, 2), "12",
1942 ". relative to start, shorten");
1943 is(pack("(a)3 ((a)2 .*)", 1..5, 7), "12345\x00\x00",
1944 ". relative to start, extend");
1945 is(pack("(a)3 ((a)2 .*)", 1..5, 5), "12345",
1946 ". relative to start, keep");
1947
1948 is(pack('(a)5 (. @2 a)', 1..5, -3, "a"), "12\x00\x00a",
1949 ". based shrink properly updates group starts");
1950
1951 is(pack("(W)3 ((W)2 .)", 0x301..0x305, -2), "\x{301}",
1952 "utf8 . relative to group, shorten");
1953 is(pack("(W)3 ((W)2 .)", 0x301..0x305, 2),
1954 "\x{301}\x{302}\x{303}\x{304}\x{305}",
1955 "utf8 . relative to group, keep");
1956 is(pack("(W)3 ((W)2 .)", 0x301..0x305, 4),
1957 "\x{301}\x{302}\x{303}\x{304}\x{305}\x00\x00",
1958 "utf8 . relative to group, extend");
1959
1960 is(pack("(W)3 ((W)2 .!)", 0x301..0x305, -2), "\x{301}\x{302}",
1961 "utf8 . relative to group, shorten");
1962 is(pack("(W)3 ((W)2 .!)", 0x301..0x305, 4),
1963 "\x{301}\x{302}\x{303}\x{304}\x{305}",
1964 "utf8 . relative to group, keep");
1965 is(pack("(W)3 ((W)2 .!)", 0x301..0x305, 6),
1966 "\x{301}\x{302}\x{303}\x{304}\x{305}\x00\x00",
1967 "utf8 . relative to group, extend");
1968
1969 is(pack('(W)5 (. @2 a)', 0x301..0x305, -3, "a"),
1970 "\x{301}\x{302}\x00\x00a",
1971 "utf8 . based shrink properly updates group starts");
1972}
1973{
1974 # Testing @!
1975 is(pack('a* @3', "abcde"), "abc", 'Test basic @');
1976 is(pack('a* @!3', "abcde"), "abc", 'Test basic @!');
1977 is(pack('a* @2', "\x{301}\x{302}\x{303}\x{304}\x{305}"), "\x{301}\x{302}",
1978 'Test basic utf8 @');
1979 is(pack('a* @!2', "\x{301}\x{302}\x{303}\x{304}\x{305}"), "\x{301}",
1980 'Test basic utf8 @!');
1981
1982 is(unpack('@4 a*', "abcde"), "e", 'Test basic @');
1983 is(unpack('@!4 a*', "abcde"), "e", 'Test basic @!');
1984 is(unpack('@4 a*', "\x{301}\x{302}\x{303}\x{304}\x{305}"), "\x{305}",
1985 'Test basic utf8 @');
1986 is(unpack('@!4 a*', "\x{301}\x{302}\x{303}\x{304}\x{305}"),
1987 "\x{303}\x{304}\x{305}", 'Test basic utf8 @!');
1988}
c6f750d1
NC
1989{
1990 #50256
1991 my ($v) = split //, unpack ('(B)*', 'ab');
1992 is($v, 0); # Doesn't SEGV :-)
1993}
858fe5e1
TC
1994{
1995 #73814
1996 my $x = runperl( prog => 'print split( /,/, unpack(q(%2H*), q(hello world))), qq(\n)' );
1997 is($x, "0\n", "split /a/, unpack('%2H*'...) didn't crash");
1998
1999 my $y = runperl( prog => 'print split( /,/, unpack(q(%32u*), q(#,3,Q)), qq(\n)), qq(\n)' );
2000 is($y, "0\n", "split /a/, unpack('%32u*'...) didn't crash");
2001}
c5333953
FC
2002
2003#90160
2004is(eval { () = unpack "C0 U*", ""; "ok" }, "ok",
2005 'medial U* on empty string');