Add a new warning, "State variable %s will be reinitialized"
[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";
784fea9c 11 require Config;
d1f8c7a4 12}
13
add36b05 14# Tests don't have names yet.
15# If you find tests are failing, please try adding names to tests to track
16# down where the failure is, and supply your new names as a patch.
17# (Just-in-time test naming)
74d49cd0 18plan tests => 161;
ddb9d9dc 19
20# numerics
add36b05 21ok ((0xdead & 0xbeef) == 0x9ead);
22ok ((0xdead | 0xbeef) == 0xfeef);
23ok ((0xdead ^ 0xbeef) == 0x6042);
24ok ((~0xdead & 0xbeef) == 0x2042);
55497cff 25
26# shifts
add36b05 27ok ((257 << 7) == 32896);
28ok ((33023 >> 7) == 257);
55497cff 29
30# signed vs. unsigned
add36b05 31ok ((~0 > 0 && do { use integer; ~0 } == -1));
d1f8c7a4 32
33my $bits = 0;
34for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
35my $cusp = 1 << ($bits - 1);
36
add36b05 37
38ok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
39ok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
40ok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
41ok ((1 << ($bits - 1)) == $cusp &&
42 do { use integer; 1 << ($bits - 1) } == -$cusp);
43ok (($cusp >> 1) == ($cusp / 2) &&
44 do { use integer; abs($cusp >> 1) } == ($cusp / 2));
ddb9d9dc 45
9d116dd7 46$Aaz = chr(ord("A") & ord("z"));
47$Aoz = chr(ord("A") | ord("z"));
48$Axz = chr(ord("A") ^ ord("z"));
49
ddb9d9dc 50# short strings
add36b05 51is (("AAAAA" & "zzzzz"), ($Aaz x 5));
52is (("AAAAA" | "zzzzz"), ($Aoz x 5));
53is (("AAAAA" ^ "zzzzz"), ($Axz x 5));
ddb9d9dc 54
55# long strings
56$foo = "A" x 150;
57$bar = "z" x 75;
9d116dd7 58$zap = "A" x 75;
59# & truncates
add36b05 60is (($foo & $bar), ($Aaz x 75 ));
9d116dd7 61# | does not truncate
add36b05 62is (($foo | $bar), ($Aoz x 75 . $zap));
9d116dd7 63# ^ does not truncate
add36b05 64is (($foo ^ $bar), ($Axz x 75 . $zap));
9d116dd7 65
0c57e439 66#
add36b05 67is ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
68is ("ok 20\n" | "ok \0\0\n", "ok 20\n");
69is ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
0c57e439 70
71#
add36b05 72is ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
73is ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
74is ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
0c57e439 75
76#
add36b05 77is (sprintf("%vd", v4095 & v801), 801);
78is (sprintf("%vd", v4095 | v801), 4095);
79is (sprintf("%vd", v4095 ^ v801), 3294);
0c57e439 80
81#
add36b05 82is (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
83is (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
84is (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
2a4ebaa6 85#
add36b05 86is (sprintf("%vd", v120.300 & v200.400), '72.256');
87is (sprintf("%vd", v120.300 | v200.400), '248.444');
88is (sprintf("%vd", v120.300 ^ v200.400), '176.188');
2a4ebaa6 89#
90my $a = v120.300;
91my $b = v200.400;
92$a ^= $b;
add36b05 93is (sprintf("%vd", $a), '176.188');
2a4ebaa6 94my $a = v120.300;
95my $b = v200.400;
96$a |= $b;
add36b05 97is (sprintf("%vd", $a), '248.444');
3da1940a 98
1d68d6cd 99#
100# UTF8 ~ behaviour
3da1940a 101#
102
210db7fc 103my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
104
3da1940a 105my @not36;
106
f0da931d 107for (0x100...0xFFF) {
1d68d6cd 108 $a = ~(chr $_);
210db7fc 109 if ($Is_EBCDIC) {
110 push @not36, sprintf("%#03X", $_)
111 if $a ne chr(~$_) or length($a) != 1;
112 }
113 else {
114 push @not36, sprintf("%#03X", $_)
115 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
116 }
3da1940a 117}
add36b05 118is (join (', ', @not36), '');
1d68d6cd 119
3da1940a 120my @not37;
121
1d68d6cd 122for my $i (0xEEE...0xF00) {
123 for my $j (0x0..0x120) {
124 $a = ~(chr ($i) . chr $j);
210db7fc 125 if ($Is_EBCDIC) {
126 push @not37, sprintf("%#03X %#03X", $i, $j)
127 if $a ne chr(~$i).chr(~$j) or
128 length($a) != 2;
129 }
130 else {
131 push @not37, sprintf("%#03X %#03X", $i, $j)
132 if $a ne chr(~$i).chr(~$j) or
133 length($a) != 2 or
134 ~$a ne chr($i).chr($j);
135 }
1d68d6cd 136 }
137}
add36b05 138is (join (', ', @not37), '');
139
140SKIP: {
141 skip "EBCDIC" if $Is_EBCDIC;
142 is (~chr(~0), "\0");
3da1940a 143}
f0da931d 144
a1ca4561 145
146my @not39;
147
148for my $i (0x100..0x120) {
149 for my $j (0x100...0x120) {
150 push @not39, sprintf("%#03X %#03X", $i, $j)
151 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
152 }
153}
add36b05 154is (join (', ', @not39), '');
a1ca4561 155
156my @not40;
157
158for my $i (0x100..0x120) {
159 for my $j (0x100...0x120) {
160 push @not40, sprintf("%#03X %#03X", $i, $j)
161 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
162 }
163}
add36b05 164is (join (', ', @not40), '');
165
299b089d 166
167# More variations on 19 and 22.
add36b05 168is ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
169is ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
66a74c25 170
171# Tests to see if you really can do casts negative floats to unsigned properly
172$neg1 = -1.0;
add36b05 173ok (~ $neg1 == 0);
66a74c25 174$neg7 = -7.0;
add36b05 175ok (~ $neg7 == 6);
891f9566 176
891f9566 177
178# double magic tests
179
180sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
181sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
182sub FETCH { $_[0]{fetch}++; $_[0]{value} }
183sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
184 delete(tied($_[0])->{store}) || 0 }
185sub fetches { delete(tied($_[0])->{fetch}) || 0 }
186
187# numeric double magic tests
188
189tie $x, "main", 1;
190tie $y, "main", 3;
191
192is(($x | $y), 3);
193is(fetches($x), 1);
194is(fetches($y), 1);
195is(stores($x), 0);
196is(stores($y), 0);
197
198is(($x & $y), 1);
199is(fetches($x), 1);
200is(fetches($y), 1);
201is(stores($x), 0);
202is(stores($y), 0);
203
204is(($x ^ $y), 2);
205is(fetches($x), 1);
206is(fetches($y), 1);
207is(stores($x), 0);
208is(stores($y), 0);
209
210is(($x |= $y), 3);
211is(fetches($x), 2);
212is(fetches($y), 1);
213is(stores($x), 1);
214is(stores($y), 0);
215
216is(($x &= $y), 1);
217is(fetches($x), 2);
218is(fetches($y), 1);
219is(stores($x), 1);
220is(stores($y), 0);
221
222is(($x ^= $y), 2);
223is(fetches($x), 2);
224is(fetches($y), 1);
225is(stores($x), 1);
226is(stores($y), 0);
227
228is(~~$y, 3);
229is(fetches($y), 1);
230is(stores($y), 0);
231
232{ use integer;
233
234is(($x | $y), 3);
235is(fetches($x), 1);
236is(fetches($y), 1);
237is(stores($x), 0);
238is(stores($y), 0);
239
240is(($x & $y), 1);
241is(fetches($x), 1);
242is(fetches($y), 1);
243is(stores($x), 0);
244is(stores($y), 0);
245
246is(($x ^ $y), 2);
247is(fetches($x), 1);
248is(fetches($y), 1);
249is(stores($x), 0);
250is(stores($y), 0);
251
252is(($x |= $y), 3);
253is(fetches($x), 2);
254is(fetches($y), 1);
255is(stores($x), 1);
256is(stores($y), 0);
257
258is(($x &= $y), 1);
259is(fetches($x), 2);
260is(fetches($y), 1);
261is(stores($x), 1);
262is(stores($y), 0);
263
264is(($x ^= $y), 2);
265is(fetches($x), 2);
266is(fetches($y), 1);
267is(stores($x), 1);
268is(stores($y), 0);
269
270is(~$y, -4);
271is(fetches($y), 1);
272is(stores($y), 0);
273
274} # end of use integer;
275
276# stringwise double magic tests
277
278tie $x, "main", "a";
279tie $y, "main", "c";
280
281is(($x | $y), ("a" | "c"));
282is(fetches($x), 1);
283is(fetches($y), 1);
284is(stores($x), 0);
285is(stores($y), 0);
286
287is(($x & $y), ("a" & "c"));
288is(fetches($x), 1);
289is(fetches($y), 1);
290is(stores($x), 0);
291is(stores($y), 0);
292
293is(($x ^ $y), ("a" ^ "c"));
294is(fetches($x), 1);
295is(fetches($y), 1);
296is(stores($x), 0);
297is(stores($y), 0);
298
299is(($x |= $y), ("a" | "c"));
300is(fetches($x), 2);
301is(fetches($y), 1);
302is(stores($x), 1);
303is(stores($y), 0);
304
305is(($x &= $y), ("a" & "c"));
306is(fetches($x), 2);
307is(fetches($y), 1);
308is(stores($x), 1);
309is(stores($y), 0);
310
311is(($x ^= $y), ("a" ^ "c"));
312is(fetches($x), 2);
313is(fetches($y), 1);
314is(stores($x), 1);
315is(stores($y), 0);
316
317is(~~$y, "c");
318is(fetches($y), 1);
319is(stores($y), 0);
d0a21e00 320
321$a = "\0\x{100}"; chop($a);
322ok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
323$a = ~$a;
324is($a, "\xFF", "~ works with utf-8");
80ff368f 325
326# [rt.perl.org 33003]
784fea9c 327# This would cause a segfault without malloc wrap
328SKIP: {
329 skip "No malloc wrap checks" unless $Config::Config{usemallocwrap};
330 like( runperl(prog => 'eval q($#a>>=1); print 1'), "^1\n?" );
331}
1a787b95 332
333# [perl #37616] Bug in &= (string) and/or m//
334{
335 $a = "aa";
336 $a &= "a";
337 ok($a =~ /a+$/, 'ASCII "a" is NUL-terminated');
338
339 $b = "bb\x{100}";
340 $b &= "b";
341 ok($b =~ /b+$/, 'Unicode "b" is NUL-terminated');
342}
794a0d33 343
344{
345 $a = chr(0x101) x 0x101;
346 $b = chr(0x0FF) x 0x0FF;
347
348 $c = $a | $b;
349 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
350
351 $c = $b | $a;
352 is($c, chr(0x1FF) x 0xFF . chr(0x101) x 2);
353
354 $c = $a & $b;
355 is($c, chr(0x001) x 0x0FF);
356
357 $c = $b & $a;
358 is($c, chr(0x001) x 0x0FF);
359
360 $c = $a ^ $b;
361 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
362
363 $c = $b ^ $a;
364 is($c, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
365}
366
367{
368 $a = chr(0x101) x 0x101;
369 $b = chr(0x0FF) x 0x0FF;
370
371 $a |= $b;
372 is($a, chr(0x1FF) x 0xFF . chr(0x101) x 2);
373}
374
375{
376 $a = chr(0x101) x 0x101;
377 $b = chr(0x0FF) x 0x0FF;
378
379 $b |= $a;
380 is($b, chr(0x1FF) x 0xFF . chr(0x101) x 2);
381}
382
383{
384 $a = chr(0x101) x 0x101;
385 $b = chr(0x0FF) x 0x0FF;
386
387 $a &= $b;
388 is($a, chr(0x001) x 0x0FF);
389}
390
391{
392 $a = chr(0x101) x 0x101;
393 $b = chr(0x0FF) x 0x0FF;
394
395 $b &= $a;
396 is($b, chr(0x001) x 0x0FF);
397}
398
399{
400 $a = chr(0x101) x 0x101;
401 $b = chr(0x0FF) x 0x0FF;
402
403 $a ^= $b;
404 is($a, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
405}
406
407{
408 $a = chr(0x101) x 0x101;
409 $b = chr(0x0FF) x 0x0FF;
410
411 $b ^= $a;
412 is($b, chr(0x1FE) x 0x0FF . chr(0x101) x 2);
413}
414
74d49cd0 415# update to pp_complement() via Coverity
416SKIP: {
417 # UTF-EBCDIC is limited to 0x7fffffff and can't encode ~0.
418 skip "EBCDIC" if $Is_EBCDIC;
419
420 my $str = "\x{10000}\x{800}";
421 # U+10000 is four bytes in UTF-8/UTF-EBCDIC.
422 # U+0800 is three bytes in UTF-8/UTF-EBCDIC.
423
424 no warnings "utf8";
425 { use bytes; $str =~ s/\C\C\z//; }
426
427 # it's really bogus that (~~malformed) is \0.
428 my $ref = "\x{10000}\0";
429 is(~~$str, $ref);
430}