This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Rewrite tests for objects and ~~
[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     {
176         no warnings 'imprecision';
177         $c = $a--;
178     }
179     print "not "
180         unless $a == -9223372036854775809 && $c == -9223372036854775808;
181     print "ok 29\n";
182
183     $a = -9223372036854775808;
184     {
185         no warnings 'imprecision';
186         $c = --$a;
187     }
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;
200     {
201         no warnings 'imprecision';
202         $c = $a--;
203     }
204     print "not "
205         unless $a == -9223372036854775809 && $c == -9223372036854775808;
206     print "ok 32\n";
207     
208     $a = 9223372036854775808;
209     $a = -$a;
210     {
211         no warnings 'imprecision';
212         $c = --$a;
213     }
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;
227     {
228         no warnings 'imprecision';
229         $c = $b--;
230     }
231     print "not "
232         unless $b == -$a-1 && $c == -$a;
233     print "ok 35\n";
234
235     $a = 9223372036854775808;
236     $b = -$a;
237     {
238         no warnings 'imprecision';
239         $c = --$b;
240     }
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),
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) {
257         print "ok $_ # skipped: too imprecise numbers\n";
258     }
259 }
260
261
262 $x = '';
263 print "not " unless (vec($x, 1, 64) = $q) == $q;
264 print "ok 38\n";
265
266 print "not " unless vec($x, 1, 64) == $q && vec($x, 1, 64) > $f;
267 print "ok 39\n";
268
269 print "not " unless vec($x, 0, 64) == 0 && vec($x, 2, 64) == 0;
270 print "ok 40\n";
271
272
273 print "not " unless ~0 == 0xffffffffffffffff;
274 print "ok 41\n";
275
276 print "not " unless (0xffffffff<<32) == 0xffffffff00000000;
277 print "ok 42\n";
278
279 print "not " unless ((0xffffffff)<<32)>>32 == 0xffffffff;
280 print "ok 43\n";
281
282 print "not " unless 1<<63 == 0x8000000000000000;
283 print "ok 44\n";
284
285 print "not " unless (sprintf "%#Vx", 1<<63) eq '0x8000000000000000';
286 print "ok 45\n";
287
288 print "not " unless (0x8000000000000000 | 1) == 0x8000000000000001;
289 print "ok 46\n";
290
291 print "not "
292     unless (0xf000000000000000 & 0x8000000000000000) == 0x8000000000000000;
293 print "ok 47\n";
294
295 print "not "
296     unless (0xf000000000000000 ^ 0xfffffffffffffff0) == 0x0ffffffffffffff0;
297 print "ok 48\n";
298
299
300 print "not "
301     unless (sprintf "%b", ~0)   eq
302            '1111111111111111111111111111111111111111111111111111111111111111';
303 print "ok 49\n";
304
305 print "not "
306     unless (sprintf "%64b", ~0) eq
307            '1111111111111111111111111111111111111111111111111111111111111111';
308 print "ok 50\n";
309
310 print "not " unless (sprintf "%d", ~0>>1) eq '9223372036854775807';
311 print "ok 51\n";
312
313 print "not " unless (sprintf "%u", ~0)    eq '18446744073709551615';
314 print "ok 52\n";
315
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;
320 print "# $q ne\n# -9223372036854775808\nnot " unless "$q" eq "-9223372036854775808";
321 print "ok 53\n";
322
323 $q =  9223372036854775807;
324 print "# $q ne\n# 9223372036854775807\nnot " unless "$q" eq "9223372036854775807";
325 print "ok 54\n";
326
327 $q = 18446744073709551615;
328 print "# $q ne\n# 18446744073709551615\nnot " unless "$q" eq "18446744073709551615";
329 print "ok 55\n";
330
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.
333 my $num = 4294967297;
334 my $string = "4294967297";
335 {
336   use integer;
337   $num += 0;
338   $string += 0;
339 }
340 if ($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;
351 if ($num eq $string) {
352   print "ok 57\n";
353 } else {
354   print "not ok 57 # \"$num\" ne \"$string\"\n";
355 }
356
357 $q = "18446744073709551616e0";
358 $q += 0;
359 print "# \"18446744073709551616e0\" += 0 gives $q\nnot " if "$q" eq "18446744073709551615";
360 print "ok 58\n";
361
362 # 0xFFFFFFFFFFFFFFFF ==  1 * 3 * 5 * 17 * 257 * 641 * 65537 * 6700417'
363 $q = 0xFFFFFFFFFFFFFFFF / 3;
364 if ($q == 0x5555555555555555 and ($q != 0x5555555555555556
365                                   or !$maths_preserves_UVs)) {
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 }
371
372 $q = 0xFFFFFFFFFFFFFFFF % 0x5555555555555555;
373 if ($q == 0) {
374   print "ok 60\n";
375 } else {
376   print "not ok 60 # 0xFFFFFFFFFFFFFFFF % 0x5555555555555555 => $q\n";
377 }
378
379 $q = 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0;
380 if ($q == 0xF) {
381   print "ok 61\n";
382 } else {
383   print "not ok 61 # 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0 => $q\n";
384 }
385
386 $q = 0x8000000000000000 % 9223372036854775807;
387 if ($q == 1) {
388   print "ok 62\n";
389 } else {
390   print "not ok 62 # 0x8000000000000000 % 9223372036854775807 => $q\n";
391 }
392
393 $q = 0x8000000000000000 % -9223372036854775807;
394 if ($q == -9223372036854775806) {
395   print "ok 63\n";
396 } else {
397   print "not ok 63 # 0x8000000000000000 % -9223372036854775807 => $q\n";
398 }
399
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
435 # eof