This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add more tests for 'my @a =' OPpASSIGN_COMMON
[perl5.git] / t / op / 64bitint.t
CommitLineData
e312add1
GS
1#./perl
2
0f4b6630 3BEGIN {
ea2b5ef6 4 eval { my $q = pack "q", 0 };
0f4b6630 5 if ($@) {
195d559b 6 print "1..0 # Skip: no 64-bit types\n";
0f4b6630
JH
7 exit(0);
8 }
ea2b5ef6 9 chdir 't' if -d 't';
20822f61 10 @INC = '../lib';
0f4b6630
JH
11}
12
686fa4bb 13# This could use many more tests.
0f4b6630 14
d0ba1bd2 15# so that using > 0xfffffff constants and
972b05a9 16# 32+ bit integers don't cause noise
59d8ce62 17use warnings;
4438c4b7 18no warnings qw(overflow portable);
ea2b5ef6 19
53305cf1 20print "1..67\n";
0f4b6630 21
59d8ce62
NC
22# as 6 * 6 = 36, the last digit of 6**n will always be six. Hence the last
23# digit of 16**n will always be six. Hence 16**n - 1 will always end in 5.
24# Assumption is that UVs will always be a multiple of 4 bits long.
25
26my $UV_max = ~0;
27die "UV_max eq '$UV_max', doesn't end in 5; your UV isn't 4n bits long :-(."
28 unless $UV_max =~ /5$/;
29my $UV_max_less3 = $UV_max - 3;
30my $maths_preserves_UVs = $UV_max_less3 =~ /^\d+2$/; # 5 - 3 is 2.
31if ($maths_preserves_UVs) {
32 print "# This perl's maths preserves all bits of a UV.\n";
33} else {
34 print "# This perl's maths does not preserve all bits of a UV.\n";
35}
36
0f4b6630
JH
37my $q = 12345678901;
38my $r = 23456789012;
20fe1ea2 39my $f = 0xffffffff;
0f4b6630 40my $x;
2d4389e4 41my $y;
0f4b6630
JH
42
43$x = unpack "q", pack "q", $q;
20fe1ea2 44print "not " unless $x == $q && $x > $f;
0f4b6630
JH
45print "ok 1\n";
46
47
22f3ae8c 48$x = sprintf("%lld", 12345678901);
20fe1ea2 49print "not " unless $x eq $q && $x > $f;
0f4b6630
JH
50print "ok 2\n";
51
52
0f4b6630 53$x = sprintf("%lld", $q);
20fe1ea2 54print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 55print "ok 3\n";
0f4b6630
JH
56
57$x = sprintf("%Ld", $q);
20fe1ea2 58print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 59print "ok 4\n";
0f4b6630
JH
60
61$x = sprintf("%qd", $q);
20fe1ea2 62print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 63print "ok 5\n";
0f4b6630 64
0f4b6630
JH
65
66$x = sprintf("%llx", $q);
20fe1ea2 67print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
22f3ae8c 68print "ok 6\n";
0f4b6630
JH
69
70$x = sprintf("%Lx", $q);
20fe1ea2 71print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
22f3ae8c 72print "ok 7\n";
0f4b6630
JH
73
74$x = sprintf("%qx", $q);
20fe1ea2 75print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
22f3ae8c 76print "ok 8\n";
0f4b6630 77
0f4b6630
JH
78
79$x = sprintf("%llo", $q);
20fe1ea2 80print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
22f3ae8c 81print "ok 9\n";
0f4b6630
JH
82
83$x = sprintf("%Lo", $q);
20fe1ea2 84print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
22f3ae8c 85print "ok 10\n";
0f4b6630
JH
86
87$x = sprintf("%qo", $q);
20fe1ea2 88print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
22f3ae8c 89print "ok 11\n";
0f4b6630 90
0f4b6630
JH
91
92$x = sprintf("%llb", $q);
20fe1ea2
JH
93print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
94 oct("0b$x") > $f;
22f3ae8c 95print "ok 12\n";
0f4b6630
JH
96
97$x = sprintf("%Lb", $q);
20fe1ea2
JH
98print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
99 oct("0b$x") > $f;
22f3ae8c 100print "ok 13\n";
0f4b6630
JH
101
102$x = sprintf("%qb", $q);
20fe1ea2
JH
103print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
104 oct("0b$x") > $f;
22f3ae8c 105print "ok 14\n";
0f4b6630
JH
106
107
22f3ae8c 108$x = sprintf("%llu", $q);
20fe1ea2 109print "not " unless $x eq $q && $x > $f;
22f3ae8c 110print "ok 15\n";
0f4b6630 111
22f3ae8c 112$x = sprintf("%Lu", $q);
20fe1ea2 113print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 114print "ok 16\n";
0f4b6630 115
22f3ae8c 116$x = sprintf("%qu", $q);
20fe1ea2 117print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 118print "ok 17\n";
0f4b6630
JH
119
120
29fe7a80 121$x = sprintf("%D", $q);
20fe1ea2 122print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 123print "ok 18\n";
29fe7a80
JH
124
125$x = sprintf("%U", $q);
20fe1ea2 126print "not " unless $x == $q && $x eq $q && $x > $f;
22f3ae8c 127print "ok 19\n";
29fe7a80
JH
128
129$x = sprintf("%O", $q);
20fe1ea2 130print "not " unless oct($x) == $q && oct($x) > $f;
22f3ae8c 131print "ok 20\n";
29fe7a80
JH
132
133
0f4b6630 134$x = $q + $r;
20fe1ea2 135print "not " unless $x == 35802467913 && $x > $f;
22f3ae8c 136print "ok 21\n";
0f4b6630
JH
137
138$x = $q - $r;
20fe1ea2 139print "not " unless $x == -11111110111 && -$x > $f;
22f3ae8c 140print "ok 22\n";
0f4b6630 141
f3ff050f
JH
142if ($^O ne 'unicos') {
143 $x = $q * 1234567;
144 print "not " unless $x == 15241567763770867 && $x > $f;
145 print "ok 23\n";
0f4b6630 146
8d489514
JH
147 $x /= 1234567;
148 print "not " unless $x == $q && $x > $f;
149 print "ok 24\n";
2d4389e4 150
8d489514
JH
151 $x = 98765432109 % 12345678901;
152 print "not " unless $x == 901;
153 print "ok 25\n";
154
155 # The following 12 tests adapted from op/inc.
2d4389e4 156
f3ff050f
JH
157 $a = 9223372036854775807;
158 $c = $a++;
159 print "not " unless $a == 9223372036854775808;
160 print "ok 26\n";
161
162 $a = 9223372036854775807;
163 $c = ++$a;
164 print "not "
165 unless $a == 9223372036854775808 && $c == $a;
166 print "ok 27\n";
167
168 $a = 9223372036854775807;
169 $c = $a + 1;
170 print "not "
171 unless $a == 9223372036854775807 && $c == 9223372036854775808;
172 print "ok 28\n";
173
174 $a = -9223372036854775808;
a12a6a4d
NC
175 {
176 no warnings 'imprecision';
177 $c = $a--;
178 }
f3ff050f
JH
179 print "not "
180 unless $a == -9223372036854775809 && $c == -9223372036854775808;
181 print "ok 29\n";
182
183 $a = -9223372036854775808;
a12a6a4d
NC
184 {
185 no warnings 'imprecision';
186 $c = --$a;
187 }
f3ff050f
JH
188 print "not "
189 unless $a == -9223372036854775809 && $c == $a;
190 print "ok 30\n";
191
192 $a = -9223372036854775808;
193 $c = $a - 1;
194 print "not "
195 unless $a == -9223372036854775808 && $c == -9223372036854775809;
196 print "ok 31\n";
197
198 $a = 9223372036854775808;
199 $a = -$a;
a12a6a4d
NC
200 {
201 no warnings 'imprecision';
202 $c = $a--;
203 }
f3ff050f
JH
204 print "not "
205 unless $a == -9223372036854775809 && $c == -9223372036854775808;
206 print "ok 32\n";
207
208 $a = 9223372036854775808;
209 $a = -$a;
a12a6a4d
NC
210 {
211 no warnings 'imprecision';
212 $c = --$a;
213 }
f3ff050f
JH
214 print "not "
215 unless $a == -9223372036854775809 && $c == $a;
216 print "ok 33\n";
217
218 $a = 9223372036854775808;
219 $a = -$a;
220 $c = $a - 1;
221 print "not "
222 unless $a == -9223372036854775808 && $c == -9223372036854775809;
223 print "ok 34\n";
224
225 $a = 9223372036854775808;
226 $b = -$a;
a12a6a4d
NC
227 {
228 no warnings 'imprecision';
229 $c = $b--;
230 }
f3ff050f
JH
231 print "not "
232 unless $b == -$a-1 && $c == -$a;
233 print "ok 35\n";
234
235 $a = 9223372036854775808;
236 $b = -$a;
a12a6a4d
NC
237 {
238 no warnings 'imprecision';
239 $c = --$b;
240 }
f3ff050f
JH
241 print "not "
242 unless $b == -$a-1 && $c == $b;
243 print "ok 36\n";
244
245 $a = 9223372036854775808;
246 $b = -$a;
247 $b = $b - 1;
248 print "not "
249 unless $b == -(++$a);
250 print "ok 37\n";
251
252} else {
253 # Unicos has imprecise doubles (14 decimal digits or so),
8d489514
JH
254 # especially if operating near the UV/IV limits the low-order bits
255 # become mangled even by simple arithmetic operations.
256 for (23..37) {
2f7c487e 257 print "ok $_ # skipped: too imprecise numbers\n";
f3ff050f
JH
258 }
259}
e312add1 260
2d4389e4 261
c5a0f51a
JH
262$x = '';
263print "not " unless (vec($x, 1, 64) = $q) == $q;
e312add1 264print "ok 38\n";
c5a0f51a
JH
265
266print "not " unless vec($x, 1, 64) == $q && vec($x, 1, 64) > $f;
e312add1 267print "ok 39\n";
c5a0f51a
JH
268
269print "not " unless vec($x, 0, 64) == 0 && vec($x, 2, 64) == 0;
e312add1 270print "ok 40\n";
c5a0f51a 271
972b05a9
JH
272
273print "not " unless ~0 == 0xffffffffffffffff;
e312add1 274print "ok 41\n";
972b05a9
JH
275
276print "not " unless (0xffffffff<<32) == 0xffffffff00000000;
e312add1 277print "ok 42\n";
972b05a9
JH
278
279print "not " unless ((0xffffffff)<<32)>>32 == 0xffffffff;
e312add1 280print "ok 43\n";
972b05a9
JH
281
282print "not " unless 1<<63 == 0x8000000000000000;
e312add1 283print "ok 44\n";
972b05a9
JH
284
285print "not " unless (sprintf "%#Vx", 1<<63) eq '0x8000000000000000';
e312add1 286print "ok 45\n";
972b05a9
JH
287
288print "not " unless (0x8000000000000000 | 1) == 0x8000000000000001;
e312add1 289print "ok 46\n";
972b05a9 290
f3ff050f
JH
291print "not "
292 unless (0xf000000000000000 & 0x8000000000000000) == 0x8000000000000000;
e312add1 293print "ok 47\n";
972b05a9 294
f3ff050f
JH
295print "not "
296 unless (0xf000000000000000 ^ 0xfffffffffffffff0) == 0x0ffffffffffffff0;
e312add1 297print "ok 48\n";
972b05a9 298
686fa4bb 299
f3ff050f
JH
300print "not "
301 unless (sprintf "%b", ~0) eq
302 '1111111111111111111111111111111111111111111111111111111111111111';
686fa4bb
JH
303print "ok 49\n";
304
f3ff050f
JH
305print "not "
306 unless (sprintf "%64b", ~0) eq
307 '1111111111111111111111111111111111111111111111111111111111111111';
686fa4bb
JH
308print "ok 50\n";
309
310print "not " unless (sprintf "%d", ~0>>1) eq '9223372036854775807';
311print "ok 51\n";
312
313print "not " unless (sprintf "%u", ~0) eq '18446744073709551615';
314print "ok 52\n";
315
868d6b85
JH
316# If the 53..55 fail you have problems in the parser's string->int conversion,
317# see toke.c:scan_num().
318
319$q = -9223372036854775808;
00450673 320print "# $q ne\n# -9223372036854775808\nnot " unless "$q" eq "-9223372036854775808";
868d6b85
JH
321print "ok 53\n";
322
323$q = 9223372036854775807;
00450673 324print "# $q ne\n# 9223372036854775807\nnot " unless "$q" eq "9223372036854775807";
868d6b85
JH
325print "ok 54\n";
326
327$q = 18446744073709551615;
00450673 328print "# $q ne\n# 18446744073709551615\nnot " unless "$q" eq "18446744073709551615";
868d6b85
JH
329print "ok 55\n";
330
85b81d93
NC
331# Test that sv_2nv then sv_2iv is the same as sv_2iv direct
332# fails if whatever Atol is defined as can't actually cope with >32 bits.
333my $num = 4294967297;
334my $string = "4294967297";
335{
336 use integer;
337 $num += 0;
338 $string += 0;
339}
340if ($num eq $string) {
341 print "ok 56\n";
342} else {
343 print "not ok 56 # \"$num\" ne \"$string\"\n";
344}
345
346# Test that sv_2nv then sv_2uv is the same as sv_2uv direct
347$num = 4294967297;
348$string = "4294967297";
349$num &= 0;
350$string &= 0;
351if ($num eq $string) {
352 print "ok 57\n";
353} else {
354 print "not ok 57 # \"$num\" ne \"$string\"\n";
355}
356
09bb3e27
NC
357$q = "18446744073709551616e0";
358$q += 0;
359print "# \"18446744073709551616e0\" += 0 gives $q\nnot " if "$q" eq "18446744073709551615";
360print "ok 58\n";
361
5479d192
NC
362# 0xFFFFFFFFFFFFFFFF == 1 * 3 * 5 * 17 * 257 * 641 * 65537 * 6700417'
363$q = 0xFFFFFFFFFFFFFFFF / 3;
59d8ce62
NC
364if ($q == 0x5555555555555555 and ($q != 0x5555555555555556
365 or !$maths_preserves_UVs)) {
5479d192
NC
366 print "ok 59\n";
367} else {
368 print "not ok 59 # 0xFFFFFFFFFFFFFFFF / 3 = $q\n";
369 print "# Should not be floating point\n" if $q =~ tr/e.//;
370}
09bb3e27 371
e2c88acc
NC
372$q = 0xFFFFFFFFFFFFFFFF % 0x5555555555555555;
373if ($q == 0) {
374 print "ok 60\n";
375} else {
376 print "not ok 60 # 0xFFFFFFFFFFFFFFFF % 0x5555555555555555 => $q\n";
377}
378
379$q = 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0;
380if ($q == 0xF) {
381 print "ok 61\n";
382} else {
383 print "not ok 61 # 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0 => $q\n";
384}
385
386$q = 0x8000000000000000 % 9223372036854775807;
387if ($q == 1) {
388 print "ok 62\n";
389} else {
390 print "not ok 62 # 0x8000000000000000 % 9223372036854775807 => $q\n";
391}
392
393$q = 0x8000000000000000 % -9223372036854775807;
394if ($q == -9223372036854775806) {
395 print "ok 63\n";
396} else {
397 print "not ok 63 # 0x8000000000000000 % -9223372036854775807 => $q\n";
398}
399
53305cf1
NC
400{
401 use integer;
402 $q = hex "0x123456789abcdef0";
403 if ($q == 0x123456789abcdef0 and $q != 0x123456789abcdef1) {
404 print "ok 64\n";
405 } else {
406 printf "not ok 64 # hex \"0x123456789abcdef0\" = $q (%X)\n", $q;
407 print "# Should not be floating point\n" if $q =~ tr/e.//;
408 }
409
410 $q = oct "0x123456789abcdef0";
411 if ($q == 0x123456789abcdef0 and $q != 0x123456789abcdef1) {
412 print "ok 65\n";
413 } else {
414 printf "not ok 65 # oct \"0x123456789abcdef0\" = $q (%X)\n", $q;
415 print "# Should not be floating point\n" if $q =~ tr/e.//;
416 }
417
418 $q = oct "765432176543217654321";
419 if ($q == 0765432176543217654321 and $q != 0765432176543217654322) {
420 print "ok 66\n";
421 } else {
422 printf "not ok 66 # oct \"765432176543217654321\" = $q (%o)\n", $q;
423 print "# Should not be floating point\n" if $q =~ tr/e.//;
424 }
425
426 $q = oct "0b0101010101010101010101010101010101010101010101010101010101010101";
427 if ($q == 0x5555555555555555 and $q != 0x5555555555555556) {
428 print "ok 67\n";
429 } else {
430 printf "not ok 67 # oct \"0b0101010101010101010101010101010101010101010101010101010101010101\" = $q (%b)\n", $q;
431 print "# Should not be floating point\n" if $q =~ tr/e.//;
432 }
433}
434
c5a0f51a 435# eof