xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/bop.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate#
4*0Sstevel@tonic-gate# test the bit operators '&', '|', '^', '~', '<<', and '>>'
5*0Sstevel@tonic-gate#
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateBEGIN {
8*0Sstevel@tonic-gate    chdir 't' if -d 't';
9*0Sstevel@tonic-gate    @INC = '../lib';
10*0Sstevel@tonic-gate    require "./test.pl";
11*0Sstevel@tonic-gate}
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate# Tests don't have names yet.
14*0Sstevel@tonic-gate# If you find tests are failing, please try adding names to tests to track
15*0Sstevel@tonic-gate# down where the failure is, and supply your new names as a patch.
16*0Sstevel@tonic-gate# (Just-in-time test naming)
17*0Sstevel@tonic-gateplan tests => 46;
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gate# numerics
20*0Sstevel@tonic-gateok ((0xdead & 0xbeef) == 0x9ead);
21*0Sstevel@tonic-gateok ((0xdead | 0xbeef) == 0xfeef);
22*0Sstevel@tonic-gateok ((0xdead ^ 0xbeef) == 0x6042);
23*0Sstevel@tonic-gateok ((~0xdead & 0xbeef) == 0x2042);
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate# shifts
26*0Sstevel@tonic-gateok ((257 << 7) == 32896);
27*0Sstevel@tonic-gateok ((33023 >> 7) == 257);
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate# signed vs. unsigned
30*0Sstevel@tonic-gateok ((~0 > 0 && do { use integer; ~0 } == -1));
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gatemy $bits = 0;
33*0Sstevel@tonic-gatefor (my $i = ~0; $i; $i >>= 1) { ++$bits; }
34*0Sstevel@tonic-gatemy $cusp = 1 << ($bits - 1);
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gateok (($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0);
38*0Sstevel@tonic-gateok (($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0);
39*0Sstevel@tonic-gateok (($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0);
40*0Sstevel@tonic-gateok ((1 << ($bits - 1)) == $cusp &&
41*0Sstevel@tonic-gate    do { use integer; 1 << ($bits - 1) } == -$cusp);
42*0Sstevel@tonic-gateok (($cusp >> 1) == ($cusp / 2) &&
43*0Sstevel@tonic-gate    do { use integer; abs($cusp >> 1) } == ($cusp / 2));
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate$Aaz = chr(ord("A") & ord("z"));
46*0Sstevel@tonic-gate$Aoz = chr(ord("A") | ord("z"));
47*0Sstevel@tonic-gate$Axz = chr(ord("A") ^ ord("z"));
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate# short strings
50*0Sstevel@tonic-gateis (("AAAAA" & "zzzzz"), ($Aaz x 5));
51*0Sstevel@tonic-gateis (("AAAAA" | "zzzzz"), ($Aoz x 5));
52*0Sstevel@tonic-gateis (("AAAAA" ^ "zzzzz"), ($Axz x 5));
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate# long strings
55*0Sstevel@tonic-gate$foo = "A" x 150;
56*0Sstevel@tonic-gate$bar = "z" x 75;
57*0Sstevel@tonic-gate$zap = "A" x 75;
58*0Sstevel@tonic-gate# & truncates
59*0Sstevel@tonic-gateis (($foo & $bar), ($Aaz x 75 ));
60*0Sstevel@tonic-gate# | does not truncate
61*0Sstevel@tonic-gateis (($foo | $bar), ($Aoz x 75 . $zap));
62*0Sstevel@tonic-gate# ^ does not truncate
63*0Sstevel@tonic-gateis (($foo ^ $bar), ($Axz x 75 . $zap));
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate#
66*0Sstevel@tonic-gateis ("ok \xFF\xFF\n" & "ok 19\n", "ok 19\n");
67*0Sstevel@tonic-gateis ("ok 20\n" | "ok \0\0\n", "ok 20\n");
68*0Sstevel@tonic-gateis ("o\000 \0001\000" ^ "\000k\0002\000\n", "ok 21\n");
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate#
71*0Sstevel@tonic-gateis ("ok \x{FF}\x{FF}\n" & "ok 22\n", "ok 22\n");
72*0Sstevel@tonic-gateis ("ok 23\n" | "ok \x{0}\x{0}\n", "ok 23\n");
73*0Sstevel@tonic-gateis ("o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n", "ok 24\n");
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate#
76*0Sstevel@tonic-gateis (sprintf("%vd", v4095 & v801), 801);
77*0Sstevel@tonic-gateis (sprintf("%vd", v4095 | v801), 4095);
78*0Sstevel@tonic-gateis (sprintf("%vd", v4095 ^ v801), 3294);
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate#
81*0Sstevel@tonic-gateis (sprintf("%vd", v4095.801.4095 & v801.4095), '801.801');
82*0Sstevel@tonic-gateis (sprintf("%vd", v4095.801.4095 | v801.4095), '4095.4095.4095');
83*0Sstevel@tonic-gateis (sprintf("%vd", v801.4095 ^ v4095.801.4095), '3294.3294.4095');
84*0Sstevel@tonic-gate#
85*0Sstevel@tonic-gateis (sprintf("%vd", v120.300 & v200.400), '72.256');
86*0Sstevel@tonic-gateis (sprintf("%vd", v120.300 | v200.400), '248.444');
87*0Sstevel@tonic-gateis (sprintf("%vd", v120.300 ^ v200.400), '176.188');
88*0Sstevel@tonic-gate#
89*0Sstevel@tonic-gatemy $a = v120.300;
90*0Sstevel@tonic-gatemy $b = v200.400;
91*0Sstevel@tonic-gate$a ^= $b;
92*0Sstevel@tonic-gateis (sprintf("%vd", $a), '176.188');
93*0Sstevel@tonic-gatemy $a = v120.300;
94*0Sstevel@tonic-gatemy $b = v200.400;
95*0Sstevel@tonic-gate$a |= $b;
96*0Sstevel@tonic-gateis (sprintf("%vd", $a), '248.444');
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate#
99*0Sstevel@tonic-gate# UTF8 ~ behaviour
100*0Sstevel@tonic-gate#
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gatemy $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gatemy @not36;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gatefor (0x100...0xFFF) {
107*0Sstevel@tonic-gate  $a = ~(chr $_);
108*0Sstevel@tonic-gate  if ($Is_EBCDIC) {
109*0Sstevel@tonic-gate      push @not36, sprintf("%#03X", $_)
110*0Sstevel@tonic-gate          if $a ne chr(~$_) or length($a) != 1;
111*0Sstevel@tonic-gate  }
112*0Sstevel@tonic-gate  else {
113*0Sstevel@tonic-gate      push @not36, sprintf("%#03X", $_)
114*0Sstevel@tonic-gate          if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
115*0Sstevel@tonic-gate  }
116*0Sstevel@tonic-gate}
117*0Sstevel@tonic-gateis (join (', ', @not36), '');
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gatemy @not37;
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gatefor my $i (0xEEE...0xF00) {
122*0Sstevel@tonic-gate  for my $j (0x0..0x120) {
123*0Sstevel@tonic-gate    $a = ~(chr ($i) . chr $j);
124*0Sstevel@tonic-gate    if ($Is_EBCDIC) {
125*0Sstevel@tonic-gate        push @not37, sprintf("%#03X %#03X", $i, $j)
126*0Sstevel@tonic-gate	    if $a ne chr(~$i).chr(~$j) or
127*0Sstevel@tonic-gate	       length($a) != 2;
128*0Sstevel@tonic-gate    }
129*0Sstevel@tonic-gate    else {
130*0Sstevel@tonic-gate        push @not37, sprintf("%#03X %#03X", $i, $j)
131*0Sstevel@tonic-gate	    if $a ne chr(~$i).chr(~$j) or
132*0Sstevel@tonic-gate	       length($a) != 2 or
133*0Sstevel@tonic-gate               ~$a ne chr($i).chr($j);
134*0Sstevel@tonic-gate    }
135*0Sstevel@tonic-gate  }
136*0Sstevel@tonic-gate}
137*0Sstevel@tonic-gateis (join (', ', @not37), '');
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gateSKIP: {
140*0Sstevel@tonic-gate  skip "EBCDIC" if $Is_EBCDIC;
141*0Sstevel@tonic-gate  is (~chr(~0), "\0");
142*0Sstevel@tonic-gate}
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gatemy @not39;
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gatefor my $i (0x100..0x120) {
148*0Sstevel@tonic-gate    for my $j (0x100...0x120) {
149*0Sstevel@tonic-gate	push @not39, sprintf("%#03X %#03X", $i, $j)
150*0Sstevel@tonic-gate	    if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
151*0Sstevel@tonic-gate    }
152*0Sstevel@tonic-gate}
153*0Sstevel@tonic-gateis (join (', ', @not39), '');
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gatemy @not40;
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gatefor my $i (0x100..0x120) {
158*0Sstevel@tonic-gate    for my $j (0x100...0x120) {
159*0Sstevel@tonic-gate	push @not40, sprintf("%#03X %#03X", $i, $j)
160*0Sstevel@tonic-gate	    if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
161*0Sstevel@tonic-gate    }
162*0Sstevel@tonic-gate}
163*0Sstevel@tonic-gateis (join (', ', @not40), '');
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate# More variations on 19 and 22.
167*0Sstevel@tonic-gateis ("ok \xFF\x{FF}\n" & "ok 41\n", "ok 41\n");
168*0Sstevel@tonic-gateis ("ok \x{FF}\xFF\n" & "ok 42\n", "ok 42\n");
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate# Tests to see if you really can do casts negative floats to unsigned properly
171*0Sstevel@tonic-gate$neg1 = -1.0;
172*0Sstevel@tonic-gateok (~ $neg1 == 0);
173*0Sstevel@tonic-gate$neg7 = -7.0;
174*0Sstevel@tonic-gateok (~ $neg7 == 6);
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate$a = "\0\x{100}"; chop($a);
178*0Sstevel@tonic-gateok(utf8::is_utf8($a)); # make sure UTF8 flag is still there
179*0Sstevel@tonic-gate$a = ~$a;
180*0Sstevel@tonic-gateis($a, "\xFF", "~ works with utf-8");
181