This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PATCH: extra tests to check on negative float to unsigned cast
[perl5.git] / t / op / bop.t
CommitLineData
ddb9d9dc
PP
1#!./perl
2
3#
55497cff 4# test the bit operators '&', '|', '^', '~', '<<', and '>>'
ddb9d9dc
PP
5#
6
d1f8c7a4
CS
7BEGIN {
8 chdir 't' if -d 't';
20822f61 9 @INC = '../lib';
d1f8c7a4
CS
10}
11
66a74c25 12print "1..44\n";
ddb9d9dc
PP
13
14# numerics
15print ((0xdead & 0xbeef) == 0x9ead ? "ok 1\n" : "not ok 1\n");
16print ((0xdead | 0xbeef) == 0xfeef ? "ok 2\n" : "not ok 2\n");
17print ((0xdead ^ 0xbeef) == 0x6042 ? "ok 3\n" : "not ok 3\n");
55497cff
PP
18print ((~0xdead & 0xbeef) == 0x2042 ? "ok 4\n" : "not ok 4\n");
19
20# shifts
21print ((257 << 7) == 32896 ? "ok 5\n" : "not ok 5\n");
22print ((33023 >> 7) == 257 ? "ok 6\n" : "not ok 6\n");
23
24# signed vs. unsigned
25print ((~0 > 0 && do { use integer; ~0 } == -1)
26 ? "ok 7\n" : "not ok 7\n");
d1f8c7a4
CS
27
28my $bits = 0;
29for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
30my $cusp = 1 << ($bits - 1);
31
32print ((($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0)
55497cff 33 ? "ok 8\n" : "not ok 8\n");
d1f8c7a4 34print ((($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0)
55497cff 35 ? "ok 9\n" : "not ok 9\n");
d1f8c7a4 36print ((($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0)
55497cff 37 ? "ok 10\n" : "not ok 10\n");
d1f8c7a4
CS
38print (((1 << ($bits - 1)) == $cusp &&
39 do { use integer; 1 << ($bits - 1) } == -$cusp)
55497cff 40 ? "ok 11\n" : "not ok 11\n");
d1f8c7a4 41print ((($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
JH
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
JH
50print (("AAAAA" & "zzzzz") eq ($Aaz x 5) ? "ok 13\n" : "not ok 13\n");
51print (("AAAAA" | "zzzzz") eq ($Aoz x 5) ? "ok 14\n" : "not ok 14\n");
52print (("AAAAA" ^ "zzzzz") eq ($Axz x 5) ? "ok 15\n" : "not ok 15\n");
ddb9d9dc
PP
53
54# long strings
55$foo = "A" x 150;
56$bar = "z" x 75;
9d116dd7
JH
57$zap = "A" x 75;
58# & truncates
59print (($foo & $bar) eq ($Aaz x 75 ) ? "ok 16\n" : "not ok 16\n");
60# | does not truncate
61print (($foo | $bar) eq ($Aoz x 75 . $zap) ? "ok 17\n" : "not ok 17\n");
62# ^ does not truncate
63print (($foo ^ $bar) eq ($Axz x 75 . $zap) ? "ok 18\n" : "not ok 18\n");
64
0c57e439
GS
65#
66print "ok \xFF\xFF\n" & "ok 19\n";
67print "ok 20\n" | "ok \0\0\n";
68print "o\000 \0001\000" ^ "\000k\0002\000\n";
69
70#
71print "ok \x{FF}\x{FF}\n" & "ok 22\n";
72print "ok 23\n" | "ok \x{0}\x{0}\n";
73print "o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n";
74
75#
76print "ok 25\n" if sprintf("%vd", v4095 & v801) eq 801;
77print "ok 26\n" if sprintf("%vd", v4095 | v801) eq 4095;
78print "ok 27\n" if sprintf("%vd", v4095 ^ v801) eq 3294;
79
80#
81print "ok 28\n" if sprintf("%vd", v4095.801.4095 & v801.4095) eq '801.801';
82print "ok 29\n" if sprintf("%vd", v4095.801.4095 | v801.4095) eq '4095.4095.4095';
83print "ok 30\n" if sprintf("%vd", v801.4095 ^ v4095.801.4095) eq '3294.3294.4095';
2a4ebaa6 84#
1d68d6cd
SC
85print "ok 31\n" if sprintf("%vd", v120.300 & v200.400) eq '72.256';
86print "ok 32\n" if sprintf("%vd", v120.300 | v200.400) eq '248.444';
87print "ok 33\n" if sprintf("%vd", v120.300 ^ v200.400) eq '176.188';
2a4ebaa6
JH
88#
89my $a = v120.300;
90my $b = v200.400;
91$a ^= $b;
92print "ok 34\n" if sprintf("%vd", $a) eq '176.188';
93my $a = v120.300;
94my $b = v200.400;
95$a |= $b;
96print "ok 35\n" if sprintf("%vd", $a) eq '248.444';
3da1940a 97
1d68d6cd
SC
98#
99# UTF8 ~ behaviour
3da1940a
JH
100#
101
102my @not36;
103
f0da931d 104for (0x100...0xFFF) {
1d68d6cd 105 $a = ~(chr $_);
3da1940a
JH
106 push @not36, sprintf("%#03X", $_)
107 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
108}
109if (@not36) {
a1ca4561 110 print "# test 36 failed\n";
3da1940a 111 print "not ";
1d68d6cd
SC
112}
113print "ok 36\n";
114
3da1940a
JH
115my @not37;
116
1d68d6cd
SC
117for my $i (0xEEE...0xF00) {
118 for my $j (0x0..0x120) {
119 $a = ~(chr ($i) . chr $j);
3da1940a
JH
120 push @not37, sprintf("%#03X %#03X", $i, $j)
121 if $a ne chr(~$i).chr(~$j) or
122 length($a) != 2 or
a1ca4561 123 ~$a ne chr($i).chr($j);
1d68d6cd
SC
124 }
125}
3da1940a 126if (@not37) {
a1ca4561 127 print "# test 37 failed\n";
3da1940a
JH
128 print "not ";
129}
1d68d6cd 130print "ok 37\n";
f0da931d
JH
131
132print "not " unless ~chr(~0) eq "\0";
133print "ok 38\n";
a1ca4561
YST
134
135my @not39;
136
137for my $i (0x100..0x120) {
138 for my $j (0x100...0x120) {
139 push @not39, sprintf("%#03X %#03X", $i, $j)
140 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
141 }
142}
143if (@not39) {
144 print "# test 39 failed\n";
145 print "not ";
146}
147print "ok 39\n";
148
149my @not40;
150
151for my $i (0x100..0x120) {
152 for my $j (0x100...0x120) {
153 push @not40, sprintf("%#03X %#03X", $i, $j)
154 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
155 }
156}
157if (@not40) {
158 print "# test 40 failed\n";
159 print "not ";
160}
161print "ok 40\n";
299b089d
JH
162
163# More variations on 19 and 22.
164print "ok \xFF\x{FF}\n" & "ok 41\n";
165print "ok \x{FF}\xFF\n" & "ok 42\n";
66a74c25
JO
166
167# Tests to see if you really can do casts negative floats to unsigned properly
168$neg1 = -1.0;
169print ((~ $neg1 == 0) ? "ok 43\n" : "not ok 43\n");
170$neg7 = -7.0;
171print ((~ $neg7 == 6) ? "ok 44\n" : "not ok 44\n");