Enable 64-bit clean bit ops.
[p5sagit/p5-mst-13.2.git] / t / op / 64bit.t
1 BEGIN {
2         eval { my $q = pack "q", 0 };
3         if ($@) {
4                 print "1..0\n# no 64-bit types\n";
5                 exit(0);
6         }
7         chdir 't' if -d 't';
8         unshift @INC, '../lib';
9 }
10
11 # This could use a lot of more tests.
12 #
13 # Nota bene: bit operations (&, |, ^, ~, <<, >>) are not 64-bit clean.
14 # See the beginning of pp.c and the explanation next to IBW/UBW.
15
16 # So that using > 0xfffffff constants and
17 # 32+ bit vector sizes and shift doesn't cause noise.
18 no warnings qw(overflow portable);
19
20 print "1..48\n";
21
22 my $q = 12345678901;
23 my $r = 23456789012;
24 my $f = 0xffffffff;
25 my $x;
26 my $y;
27 my $z;
28
29
30 $x = unpack "q", pack "q", $q;
31 print "not " unless $x == $q && $x > $f;
32 print "ok 1\n";
33
34
35 $x = sprintf("%d", 12345678901);
36 print "not " unless $x eq $q && $x > $f;
37 print "ok 2\n";
38
39
40 $x = sprintf("%d", $q);
41 print "not " unless $x == $q && $x eq $q && $x > $f;
42 print "ok 3\n";
43
44 $x = sprintf("%lld", $q);
45 print "not " unless $x == $q && $x eq $q && $x > $f;
46 print "ok 4\n";
47
48 $x = sprintf("%Ld", $q);
49 print "not " unless $x == $q && $x eq $q && $x > $f;
50 print "ok 5\n";
51
52 $x = sprintf("%qd", $q);
53 print "not " unless $x == $q && $x eq $q && $x > $f;
54 print "ok 6\n";
55
56
57 $x = sprintf("%x", $q);
58 print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
59 print "ok 7\n";
60
61 $x = sprintf("%llx", $q);
62 print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
63 print "ok 8\n";
64
65 $x = sprintf("%Lx", $q);
66 print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
67 print "ok 9\n";
68
69 $x = sprintf("%qx", $q);
70 print "not " unless hex($x) == 0x2dfdc1c35 && hex($x) > $f;
71 print "ok 10\n";
72
73
74 $x = sprintf("%o", $q);
75 print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
76 print "ok 11\n";
77
78 $x = sprintf("%llo", $q);
79 print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
80 print "ok 12\n";
81
82 $x = sprintf("%Lo", $q);
83 print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
84 print "ok 13\n";
85
86 $x = sprintf("%qo", $q);
87 print "not " unless oct("0$x") == 0133767016065 && oct($x) > $f;
88 print "ok 14\n";
89
90
91 $x = sprintf("%b", $q);
92 print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
93                     oct("0b$x") > $f;
94 print "ok 15\n";
95
96 $x = sprintf("%llb", $q);
97 print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
98                     oct("0b$x") > $f;
99 print "ok 16\n";
100
101 $x = sprintf("%Lb", $q);
102 print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
103                                    oct("0b$x") > $f;
104 print "ok 17\n";
105
106 $x = sprintf("%qb", $q);
107 print "not " unless oct("0b$x") == 0b1011011111110111000001110000110101 &&
108                     oct("0b$x") > $f;
109 print "ok 18\n";
110
111
112 $x = sprintf("%u", 12345678901);
113 print "not " unless $x eq $q && $x > $f;
114 print "ok 19\n";
115
116 $x = sprintf("%u", $q);
117 print "not " unless $x == $q && $x eq $q && $x > $f;
118 print "ok 20\n";
119
120 $x = sprintf("%llu", $q);
121 print "not " unless $x == $q && $x eq $q && $x > $f;
122 print "ok 21\n";
123
124 $x = sprintf("%Lu", $q);
125 print "not " unless $x == $q && $x eq $q && $x > $f;
126 print "ok 22\n";
127
128
129 $x = sprintf("%D", $q);
130 print "not " unless $x == $q && $x eq $q && $x > $f;
131 print "ok 23\n";
132
133 $x = sprintf("%U", $q);
134 print "not " unless $x == $q && $x eq $q && $x > $f;
135 print "ok 24\n";
136
137 $x = sprintf("%O", $q);
138 print "not " unless oct($x) == $q && oct($x) > $f;
139 print "ok 25\n";
140
141
142 $x = $q + $r;
143 print "not " unless $x == 35802467913 && $x > $f;
144 print "ok 26\n";
145
146 $x = $q - $r;
147 print "not " unless $x == -11111110111 && -$x > $f;
148 print "ok 27\n";
149
150 $x = $q * 1234567;
151 print "not " unless $x == 15241567763770867 && $x > $f;
152 print "ok 28\n";
153
154 $x /= 1234567;
155 print "not " unless $x == $q && $x > $f;
156 print "ok 29\n";
157
158 $x = 98765432109 % 12345678901;
159 print "not " unless $x == 901;
160 print "ok 30\n";
161
162 # The following six adapted from op/inc.
163
164 $a = 9223372036854775807;
165 $c = $a++;
166 print "not " unless $a == 9223372036854775808;
167 print "ok 31\n";
168
169 $a = 9223372036854775807;
170 $c = ++$a;
171 print "not " unless $a == 9223372036854775808;
172 print "ok 32\n";
173
174 $a = 9223372036854775807;
175 $c = $a + 1;
176 print "not " unless $a == 9223372036854775808;
177 print "ok 33\n";
178
179 $a = -9223372036854775808;
180 $c = $a--;
181 print "not " unless $a == -9223372036854775809;
182 print "ok 34\n";
183
184 $a = -9223372036854775808;
185 $c = --$a;
186 print "not " unless $a == -9223372036854775809;
187 print "ok 35\n";
188
189 $a = -9223372036854775808;
190 $c = $a - 1;
191 print "not " unless $a == -9223372036854775809;
192 print "ok 36\n";
193
194
195 $x = '';
196 print "not " unless (vec($x, 1, 64) = $q) == $q;
197 print "ok 37\n";
198
199 print "not " unless vec($x, 1, 64) == $q && vec($x, 1, 64) > $f;
200 print "ok 38\n";
201
202 print "not " unless vec($x, 0, 64) == 0 && vec($x, 2, 64) == 0;
203 print "ok 39\n";
204
205
206 print "not " unless ($q & $r) == 1442844692;
207 print "ok 40\n";
208
209 print "not " unless ($q | $r) == 34359623221;
210 print "ok 41\n";
211
212 print "not " unless ($q ^ $r) == 32916778529;
213 print "ok 42\n";
214
215 print "not " unless ~$q == 18446744061363872714;
216 print "ok 43\n";
217
218 print "not " unless ($q << 1) == 24691357802;
219 print "ok 44\n";
220
221 print "not " unless (($q << 1) >> 1) == $q;
222 print "ok 45\n";
223
224 print "not " unless (1 << 32) == 2**32; # Risky because of the **?
225 print "ok 46\n";
226
227 print "not " unless ((1 << 40) >> 32) == 256;
228 print "ok 47\n";
229
230 print "not " unless (1 << 63) == ~0 ^ (~0 >> 1);
231 print "ok 48\n";
232
233 # eof