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