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