Commit | Line | Data |
ddb9d9dc |
1 | #!./perl |
2 | |
3 | # |
55497cff |
4 | # test the bit operators '&', '|', '^', '~', '<<', and '>>' |
ddb9d9dc |
5 | # |
6 | |
d1f8c7a4 |
7 | BEGIN { |
8 | chdir 't' if -d 't'; |
20822f61 |
9 | @INC = '../lib'; |
d1f8c7a4 |
10 | } |
11 | |
d0a21e00 |
12 | print "1..145\n"; |
ddb9d9dc |
13 | |
14 | # numerics |
15 | print ((0xdead & 0xbeef) == 0x9ead ? "ok 1\n" : "not ok 1\n"); |
16 | print ((0xdead | 0xbeef) == 0xfeef ? "ok 2\n" : "not ok 2\n"); |
17 | print ((0xdead ^ 0xbeef) == 0x6042 ? "ok 3\n" : "not ok 3\n"); |
55497cff |
18 | print ((~0xdead & 0xbeef) == 0x2042 ? "ok 4\n" : "not ok 4\n"); |
19 | |
20 | # shifts |
21 | print ((257 << 7) == 32896 ? "ok 5\n" : "not ok 5\n"); |
22 | print ((33023 >> 7) == 257 ? "ok 6\n" : "not ok 6\n"); |
23 | |
24 | # signed vs. unsigned |
25 | print ((~0 > 0 && do { use integer; ~0 } == -1) |
26 | ? "ok 7\n" : "not ok 7\n"); |
d1f8c7a4 |
27 | |
28 | my $bits = 0; |
29 | for (my $i = ~0; $i; $i >>= 1) { ++$bits; } |
30 | my $cusp = 1 << ($bits - 1); |
31 | |
32 | print ((($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0) |
55497cff |
33 | ? "ok 8\n" : "not ok 8\n"); |
d1f8c7a4 |
34 | print ((($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0) |
55497cff |
35 | ? "ok 9\n" : "not ok 9\n"); |
d1f8c7a4 |
36 | print ((($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0) |
55497cff |
37 | ? "ok 10\n" : "not ok 10\n"); |
d1f8c7a4 |
38 | print (((1 << ($bits - 1)) == $cusp && |
39 | do { use integer; 1 << ($bits - 1) } == -$cusp) |
55497cff |
40 | ? "ok 11\n" : "not ok 11\n"); |
d1f8c7a4 |
41 | print ((($cusp >> 1) == ($cusp / 2) && |
85e0ebd8 |
42 | do { use integer; abs($cusp >> 1) } == ($cusp / 2)) |
55497cff |
43 | ? "ok 12\n" : "not ok 12\n"); |
ddb9d9dc |
44 | |
9d116dd7 |
45 | $Aaz = chr(ord("A") & ord("z")); |
46 | $Aoz = chr(ord("A") | ord("z")); |
47 | $Axz = chr(ord("A") ^ ord("z")); |
48 | |
ddb9d9dc |
49 | # short strings |
9d116dd7 |
50 | print (("AAAAA" & "zzzzz") eq ($Aaz x 5) ? "ok 13\n" : "not ok 13\n"); |
51 | print (("AAAAA" | "zzzzz") eq ($Aoz x 5) ? "ok 14\n" : "not ok 14\n"); |
52 | print (("AAAAA" ^ "zzzzz") eq ($Axz x 5) ? "ok 15\n" : "not ok 15\n"); |
ddb9d9dc |
53 | |
54 | # long strings |
55 | $foo = "A" x 150; |
56 | $bar = "z" x 75; |
9d116dd7 |
57 | $zap = "A" x 75; |
58 | # & truncates |
59 | print (($foo & $bar) eq ($Aaz x 75 ) ? "ok 16\n" : "not ok 16\n"); |
60 | # | does not truncate |
61 | print (($foo | $bar) eq ($Aoz x 75 . $zap) ? "ok 17\n" : "not ok 17\n"); |
62 | # ^ does not truncate |
63 | print (($foo ^ $bar) eq ($Axz x 75 . $zap) ? "ok 18\n" : "not ok 18\n"); |
64 | |
0c57e439 |
65 | # |
66 | print "ok \xFF\xFF\n" & "ok 19\n"; |
67 | print "ok 20\n" | "ok \0\0\n"; |
68 | print "o\000 \0001\000" ^ "\000k\0002\000\n"; |
69 | |
70 | # |
71 | print "ok \x{FF}\x{FF}\n" & "ok 22\n"; |
72 | print "ok 23\n" | "ok \x{0}\x{0}\n"; |
73 | print "o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n"; |
74 | |
75 | # |
76 | print "ok 25\n" if sprintf("%vd", v4095 & v801) eq 801; |
77 | print "ok 26\n" if sprintf("%vd", v4095 | v801) eq 4095; |
78 | print "ok 27\n" if sprintf("%vd", v4095 ^ v801) eq 3294; |
79 | |
80 | # |
81 | print "ok 28\n" if sprintf("%vd", v4095.801.4095 & v801.4095) eq '801.801'; |
82 | print "ok 29\n" if sprintf("%vd", v4095.801.4095 | v801.4095) eq '4095.4095.4095'; |
83 | print "ok 30\n" if sprintf("%vd", v801.4095 ^ v4095.801.4095) eq '3294.3294.4095'; |
2a4ebaa6 |
84 | # |
1d68d6cd |
85 | print "ok 31\n" if sprintf("%vd", v120.300 & v200.400) eq '72.256'; |
86 | print "ok 32\n" if sprintf("%vd", v120.300 | v200.400) eq '248.444'; |
87 | print "ok 33\n" if sprintf("%vd", v120.300 ^ v200.400) eq '176.188'; |
2a4ebaa6 |
88 | # |
89 | my $a = v120.300; |
90 | my $b = v200.400; |
91 | $a ^= $b; |
92 | print "ok 34\n" if sprintf("%vd", $a) eq '176.188'; |
93 | my $a = v120.300; |
94 | my $b = v200.400; |
95 | $a |= $b; |
96 | print "ok 35\n" if sprintf("%vd", $a) eq '248.444'; |
3da1940a |
97 | |
1d68d6cd |
98 | # |
99 | # UTF8 ~ behaviour |
3da1940a |
100 | # |
101 | |
210db7fc |
102 | my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0; |
103 | |
3da1940a |
104 | my @not36; |
105 | |
f0da931d |
106 | for (0x100...0xFFF) { |
1d68d6cd |
107 | $a = ~(chr $_); |
210db7fc |
108 | if ($Is_EBCDIC) { |
109 | push @not36, sprintf("%#03X", $_) |
110 | if $a ne chr(~$_) or length($a) != 1; |
111 | } |
112 | else { |
113 | push @not36, sprintf("%#03X", $_) |
114 | if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_); |
115 | } |
3da1940a |
116 | } |
117 | if (@not36) { |
a1ca4561 |
118 | print "# test 36 failed\n"; |
3da1940a |
119 | print "not "; |
1d68d6cd |
120 | } |
121 | print "ok 36\n"; |
122 | |
3da1940a |
123 | my @not37; |
124 | |
1d68d6cd |
125 | for my $i (0xEEE...0xF00) { |
126 | for my $j (0x0..0x120) { |
127 | $a = ~(chr ($i) . chr $j); |
210db7fc |
128 | if ($Is_EBCDIC) { |
129 | push @not37, sprintf("%#03X %#03X", $i, $j) |
130 | if $a ne chr(~$i).chr(~$j) or |
131 | length($a) != 2; |
132 | } |
133 | else { |
134 | push @not37, sprintf("%#03X %#03X", $i, $j) |
135 | if $a ne chr(~$i).chr(~$j) or |
136 | length($a) != 2 or |
137 | ~$a ne chr($i).chr($j); |
138 | } |
1d68d6cd |
139 | } |
140 | } |
3da1940a |
141 | if (@not37) { |
a1ca4561 |
142 | print "# test 37 failed\n"; |
3da1940a |
143 | print "not "; |
144 | } |
1d68d6cd |
145 | print "ok 37\n"; |
f0da931d |
146 | |
210db7fc |
147 | print "not " unless ~chr(~0) eq "\0" or $Is_EBCDIC; |
f0da931d |
148 | print "ok 38\n"; |
a1ca4561 |
149 | |
150 | my @not39; |
151 | |
152 | for my $i (0x100..0x120) { |
153 | for my $j (0x100...0x120) { |
154 | push @not39, sprintf("%#03X %#03X", $i, $j) |
155 | if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j)); |
156 | } |
157 | } |
158 | if (@not39) { |
159 | print "# test 39 failed\n"; |
160 | print "not "; |
161 | } |
162 | print "ok 39\n"; |
163 | |
164 | my @not40; |
165 | |
166 | for my $i (0x100..0x120) { |
167 | for my $j (0x100...0x120) { |
168 | push @not40, sprintf("%#03X %#03X", $i, $j) |
169 | if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j)); |
170 | } |
171 | } |
172 | if (@not40) { |
173 | print "# test 40 failed\n"; |
174 | print "not "; |
175 | } |
176 | print "ok 40\n"; |
299b089d |
177 | |
178 | # More variations on 19 and 22. |
179 | print "ok \xFF\x{FF}\n" & "ok 41\n"; |
180 | print "ok \x{FF}\xFF\n" & "ok 42\n"; |
66a74c25 |
181 | |
182 | # Tests to see if you really can do casts negative floats to unsigned properly |
183 | $neg1 = -1.0; |
184 | print ((~ $neg1 == 0) ? "ok 43\n" : "not ok 43\n"); |
185 | $neg7 = -7.0; |
186 | print ((~ $neg7 == 6) ? "ok 44\n" : "not ok 44\n"); |
891f9566 |
187 | |
188 | require "./test.pl"; |
189 | curr_test(45); |
190 | |
191 | # double magic tests |
192 | |
193 | sub TIESCALAR { bless { value => $_[1], orig => $_[1] } } |
194 | sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] } |
195 | sub FETCH { $_[0]{fetch}++; $_[0]{value} } |
196 | sub stores { tied($_[0])->{value} = tied($_[0])->{orig}; |
197 | delete(tied($_[0])->{store}) || 0 } |
198 | sub fetches { delete(tied($_[0])->{fetch}) || 0 } |
199 | |
200 | # numeric double magic tests |
201 | |
202 | tie $x, "main", 1; |
203 | tie $y, "main", 3; |
204 | |
205 | is(($x | $y), 3); |
206 | is(fetches($x), 1); |
207 | is(fetches($y), 1); |
208 | is(stores($x), 0); |
209 | is(stores($y), 0); |
210 | |
211 | is(($x & $y), 1); |
212 | is(fetches($x), 1); |
213 | is(fetches($y), 1); |
214 | is(stores($x), 0); |
215 | is(stores($y), 0); |
216 | |
217 | is(($x ^ $y), 2); |
218 | is(fetches($x), 1); |
219 | is(fetches($y), 1); |
220 | is(stores($x), 0); |
221 | is(stores($y), 0); |
222 | |
223 | is(($x |= $y), 3); |
224 | is(fetches($x), 2); |
225 | is(fetches($y), 1); |
226 | is(stores($x), 1); |
227 | is(stores($y), 0); |
228 | |
229 | is(($x &= $y), 1); |
230 | is(fetches($x), 2); |
231 | is(fetches($y), 1); |
232 | is(stores($x), 1); |
233 | is(stores($y), 0); |
234 | |
235 | is(($x ^= $y), 2); |
236 | is(fetches($x), 2); |
237 | is(fetches($y), 1); |
238 | is(stores($x), 1); |
239 | is(stores($y), 0); |
240 | |
241 | is(~~$y, 3); |
242 | is(fetches($y), 1); |
243 | is(stores($y), 0); |
244 | |
245 | { use integer; |
246 | |
247 | is(($x | $y), 3); |
248 | is(fetches($x), 1); |
249 | is(fetches($y), 1); |
250 | is(stores($x), 0); |
251 | is(stores($y), 0); |
252 | |
253 | is(($x & $y), 1); |
254 | is(fetches($x), 1); |
255 | is(fetches($y), 1); |
256 | is(stores($x), 0); |
257 | is(stores($y), 0); |
258 | |
259 | is(($x ^ $y), 2); |
260 | is(fetches($x), 1); |
261 | is(fetches($y), 1); |
262 | is(stores($x), 0); |
263 | is(stores($y), 0); |
264 | |
265 | is(($x |= $y), 3); |
266 | is(fetches($x), 2); |
267 | is(fetches($y), 1); |
268 | is(stores($x), 1); |
269 | is(stores($y), 0); |
270 | |
271 | is(($x &= $y), 1); |
272 | is(fetches($x), 2); |
273 | is(fetches($y), 1); |
274 | is(stores($x), 1); |
275 | is(stores($y), 0); |
276 | |
277 | is(($x ^= $y), 2); |
278 | is(fetches($x), 2); |
279 | is(fetches($y), 1); |
280 | is(stores($x), 1); |
281 | is(stores($y), 0); |
282 | |
283 | is(~$y, -4); |
284 | is(fetches($y), 1); |
285 | is(stores($y), 0); |
286 | |
287 | } # end of use integer; |
288 | |
289 | # stringwise double magic tests |
290 | |
291 | tie $x, "main", "a"; |
292 | tie $y, "main", "c"; |
293 | |
294 | is(($x | $y), ("a" | "c")); |
295 | is(fetches($x), 1); |
296 | is(fetches($y), 1); |
297 | is(stores($x), 0); |
298 | is(stores($y), 0); |
299 | |
300 | is(($x & $y), ("a" & "c")); |
301 | is(fetches($x), 1); |
302 | is(fetches($y), 1); |
303 | is(stores($x), 0); |
304 | is(stores($y), 0); |
305 | |
306 | is(($x ^ $y), ("a" ^ "c")); |
307 | is(fetches($x), 1); |
308 | is(fetches($y), 1); |
309 | is(stores($x), 0); |
310 | is(stores($y), 0); |
311 | |
312 | is(($x |= $y), ("a" | "c")); |
313 | is(fetches($x), 2); |
314 | is(fetches($y), 1); |
315 | is(stores($x), 1); |
316 | is(stores($y), 0); |
317 | |
318 | is(($x &= $y), ("a" & "c")); |
319 | is(fetches($x), 2); |
320 | is(fetches($y), 1); |
321 | is(stores($x), 1); |
322 | is(stores($y), 0); |
323 | |
324 | is(($x ^= $y), ("a" ^ "c")); |
325 | is(fetches($x), 2); |
326 | is(fetches($y), 1); |
327 | is(stores($x), 1); |
328 | is(stores($y), 0); |
329 | |
330 | is(~~$y, "c"); |
331 | is(fetches($y), 1); |
332 | is(stores($y), 0); |
d0a21e00 |
333 | |
334 | $a = "\0\x{100}"; chop($a); |
335 | ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there |
336 | $a = ~$a; |
337 | is($a, "\xFF", "~ works with utf-8"); |