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