This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update documentation and warning in I18N::Collate.
[perl5.git] / t / op / bop.t
CommitLineData
ddb9d9dc
PP
1#!./perl
2
3#
55497cff 4# test the bit operators '&', '|', '^', '~', '<<', and '>>'
ddb9d9dc
PP
5#
6
55497cff 7print "1..18\n";
ddb9d9dc
PP
8
9# numerics
10print ((0xdead & 0xbeef) == 0x9ead ? "ok 1\n" : "not ok 1\n");
11print ((0xdead | 0xbeef) == 0xfeef ? "ok 2\n" : "not ok 2\n");
12print ((0xdead ^ 0xbeef) == 0x6042 ? "ok 3\n" : "not ok 3\n");
55497cff
PP
13print ((~0xdead & 0xbeef) == 0x2042 ? "ok 4\n" : "not ok 4\n");
14
15# shifts
16print ((257 << 7) == 32896 ? "ok 5\n" : "not ok 5\n");
17print ((33023 >> 7) == 257 ? "ok 6\n" : "not ok 6\n");
18
19# signed vs. unsigned
20print ((~0 > 0 && do { use integer; ~0 } == -1)
21 ? "ok 7\n" : "not ok 7\n");
22print (((2147483648 & -1) > 0 && do { use integer; 2147483648 & -1 } < 0)
23 ? "ok 8\n" : "not ok 8\n");
24print (((2147483648 | 1) > 0 && do { use integer; 2147483648 | 1 } < 0)
25 ? "ok 9\n" : "not ok 9\n");
26print (((2147483648 ^ 1) > 0 && do { use integer; 2147483648 ^ 1 } < 0)
27 ? "ok 10\n" : "not ok 10\n");
28print (((1 << 31) == 2147483648 && do { use integer; 1 << 31 } == -2147483648)
29 ? "ok 11\n" : "not ok 11\n");
30print (((2147483648 >> 1) == 1073741824 &&
31 do { use integer; 2147483648 >> 1 } == -1073741824)
32 ? "ok 12\n" : "not ok 12\n");
ddb9d9dc
PP
33
34# short strings
55497cff
PP
35print (("AAAAA" & "zzzzz") eq '@@@@@' ? "ok 13\n" : "not ok 13\n");
36print (("AAAAA" | "zzzzz") eq '{{{{{' ? "ok 14\n" : "not ok 14\n");
37print (("AAAAA" ^ "zzzzz") eq ';;;;;' ? "ok 15\n" : "not ok 15\n");
ddb9d9dc
PP
38
39# long strings
40$foo = "A" x 150;
41$bar = "z" x 75;
55497cff
PP
42print (($foo & $bar) eq ('@'x75 ) ? "ok 16\n" : "not ok 16\n");
43print (($foo | $bar) eq ('{'x75 . 'A'x75) ? "ok 17\n" : "not ok 17\n");
44print (($foo ^ $bar) eq (';'x75 . 'A'x75) ? "ok 18\n" : "not ok 18\n");