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