xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/64bitint.t (revision 0:68f95e015346)
1#./perl
2
3BEGIN {
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
17use warnings;
18no warnings qw(overflow portable);
19
20print "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
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
37my $q = 12345678901;
38my $r = 23456789012;
39my $f = 0xffffffff;
40my $x;
41my $y;
42
43$x = unpack "q", pack "q", $q;
44print "not " unless $x == $q && $x > $f;
45print "ok 1\n";
46
47
48$x = sprintf("%lld", 12345678901);
49print "not " unless $x eq $q && $x > $f;
50print "ok 2\n";
51
52
53$x = sprintf("%lld", $q);
54print "not " unless $x == $q && $x eq $q && $x > $f;
55print "ok 3\n";
56
57$x = sprintf("%Ld", $q);
58print "not " unless $x == $q && $x eq $q && $x > $f;
59print "ok 4\n";
60
61$x = sprintf("%qd", $q);
62print "not " unless $x == $q && $x eq $q && $x > $f;
63print "ok 5\n";
64
65
66$x = sprintf("%llx", $q);
67print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
68print "ok 6\n";
69
70$x = sprintf("%Lx", $q);
71print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
72print "ok 7\n";
73
74$x = sprintf("%qx", $q);
75print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
76print "ok 8\n";
77
78
79$x = sprintf("%llo", $q);
80print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
81print "ok 9\n";
82
83$x = sprintf("%Lo", $q);
84print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
85print "ok 10\n";
86
87$x = sprintf("%qo", $q);
88print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
89print "ok 11\n";
90
91
92$x = sprintf("%llb", $q);
93print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
94                    oct("0b$x") > $f;
95print "ok 12\n";
96
97$x = sprintf("%Lb", $q);
98print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
99                                   oct("0b$x") > $f;
100print "ok 13\n";
101
102$x = sprintf("%qb", $q);
103print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
104                    oct("0b$x") > $f;
105print "ok 14\n";
106
107
108$x = sprintf("%llu", $q);
109print "not " unless $x eq $q && $x > $f;
110print "ok 15\n";
111
112$x = sprintf("%Lu", $q);
113print "not " unless $x == $q && $x eq $q && $x > $f;
114print "ok 16\n";
115
116$x = sprintf("%qu", $q);
117print "not " unless $x == $q && $x eq $q && $x > $f;
118print "ok 17\n";
119
120
121$x = sprintf("%D", $q);
122print "not " unless $x == $q && $x eq $q && $x > $f;
123print "ok 18\n";
124
125$x = sprintf("%U", $q);
126print "not " unless $x == $q && $x eq $q && $x > $f;
127print "ok 19\n";
128
129$x = sprintf("%O", $q);
130print "not " unless oct($x) == $q && oct($x) > $f;
131print "ok 20\n";
132
133
134$x = $q + $r;
135print "not " unless $x == 35802467913 && $x > $f;
136print "ok 21\n";
137
138$x = $q - $r;
139print "not " unless $x == -11111110111 && -$x > $f;
140print "ok 22\n";
141
142if ($^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 = '';
245print "not " unless (vec($x, 1, 64) = $q) == $q;
246print "ok 38\n";
247
248print "not " unless vec($x, 1, 64) == $q && vec($x, 1, 64) > $f;
249print "ok 39\n";
250
251print "not " unless vec($x, 0, 64) == 0 && vec($x, 2, 64) == 0;
252print "ok 40\n";
253
254
255print "not " unless ~0 == 0xffffffffffffffff;
256print "ok 41\n";
257
258print "not " unless (0xffffffff<<32) == 0xffffffff00000000;
259print "ok 42\n";
260
261print "not " unless ((0xffffffff)<<32)>>32 == 0xffffffff;
262print "ok 43\n";
263
264print "not " unless 1<<63 == 0x8000000000000000;
265print "ok 44\n";
266
267print "not " unless (sprintf "%#Vx", 1<<63) eq '0x8000000000000000';
268print "ok 45\n";
269
270print "not " unless (0x8000000000000000 | 1) == 0x8000000000000001;
271print "ok 46\n";
272
273print "not "
274    unless (0xf000000000000000 & 0x8000000000000000) == 0x8000000000000000;
275print "ok 47\n";
276
277print "not "
278    unless (0xf000000000000000 ^ 0xfffffffffffffff0) == 0x0ffffffffffffff0;
279print "ok 48\n";
280
281
282print "not "
283    unless (sprintf "%b", ~0)   eq
284           '1111111111111111111111111111111111111111111111111111111111111111';
285print "ok 49\n";
286
287print "not "
288    unless (sprintf "%64b", ~0) eq
289           '1111111111111111111111111111111111111111111111111111111111111111';
290print "ok 50\n";
291
292print "not " unless (sprintf "%d", ~0>>1) eq '9223372036854775807';
293print "ok 51\n";
294
295print "not " unless (sprintf "%u", ~0)    eq '18446744073709551615';
296print "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;
302print "# $q ne\n# -9223372036854775808\nnot " unless "$q" eq "-9223372036854775808";
303print "ok 53\n";
304
305$q =  9223372036854775807;
306print "# $q ne\n# 9223372036854775807\nnot " unless "$q" eq "9223372036854775807";
307print "ok 54\n";
308
309$q = 18446744073709551615;
310print "# $q ne\n# 18446744073709551615\nnot " unless "$q" eq "18446744073709551615";
311print "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.
315my $num = 4294967297;
316my $string = "4294967297";
317{
318  use integer;
319  $num += 0;
320  $string += 0;
321}
322if ($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;
333if ($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;
341print "# \"18446744073709551616e0\" += 0 gives $q\nnot " if "$q" eq "18446744073709551615";
342print "ok 58\n";
343
344# 0xFFFFFFFFFFFFFFFF ==  1 * 3 * 5 * 17 * 257 * 641 * 65537 * 6700417'
345$q = 0xFFFFFFFFFFFFFFFF / 3;
346if ($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;
355if ($q == 0) {
356  print "ok 60\n";
357} else {
358  print "not ok 60 # 0xFFFFFFFFFFFFFFFF % 0x5555555555555555 => $q\n";
359}
360
361$q = 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0;
362if ($q == 0xF) {
363  print "ok 61\n";
364} else {
365  print "not ok 61 # 0xFFFFFFFFFFFFFFFF % 0xFFFFFFFFFFFFFFF0 => $q\n";
366}
367
368$q = 0x8000000000000000 % 9223372036854775807;
369if ($q == 1) {
370  print "ok 62\n";
371} else {
372  print "not ok 62 # 0x8000000000000000 % 9223372036854775807 => $q\n";
373}
374
375$q = 0x8000000000000000 % -9223372036854775807;
376if ($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
418