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