xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/arith.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl -w
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate}
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gateuse Config;
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gateprint "1..134\n";
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gatesub try ($$) {
13*0Sstevel@tonic-gate   print +($_[1] ? "ok" : "not ok"), " $_[0]\n";
14*0Sstevel@tonic-gate}
15*0Sstevel@tonic-gatesub tryeq ($$$) {
16*0Sstevel@tonic-gate  if ($_[1] == $_[2]) {
17*0Sstevel@tonic-gate    print "ok $_[0]\n";
18*0Sstevel@tonic-gate  } else {
19*0Sstevel@tonic-gate    print "not ok $_[0] # $_[1] != $_[2]\n";
20*0Sstevel@tonic-gate  }
21*0Sstevel@tonic-gate}
22*0Sstevel@tonic-gatesub tryeq_sloppy ($$$) {
23*0Sstevel@tonic-gate  if ($_[1] == $_[2]) {
24*0Sstevel@tonic-gate    print "ok $_[0]\n";
25*0Sstevel@tonic-gate  } else {
26*0Sstevel@tonic-gate    my $error = abs ($_[1] - $_[2]) / $_[1];
27*0Sstevel@tonic-gate    if ($error < 1e-9) {
28*0Sstevel@tonic-gate      print "ok $_[0] # $_[1] is close to $_[2], \$^O eq $^O\n";
29*0Sstevel@tonic-gate    } else {
30*0Sstevel@tonic-gate      print "not ok $_[0] # $_[1] != $_[2]\n";
31*0Sstevel@tonic-gate    }
32*0Sstevel@tonic-gate  }
33*0Sstevel@tonic-gate}
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gatetryeq 1,  13 %  4, 1;
36*0Sstevel@tonic-gatetryeq 2, -13 %  4, 3;
37*0Sstevel@tonic-gatetryeq 3,  13 % -4, -3;
38*0Sstevel@tonic-gatetryeq 4, -13 % -4, -1;
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gatemy $limit = 1e6;
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate# Division (and modulo) of floating point numbers
43*0Sstevel@tonic-gate# seem to be rather sloppy in Cray.
44*0Sstevel@tonic-gate$limit = 1e8 if $^O eq 'unicos';
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gatetry 5, abs( 13e21 %  4e21 -  1e21) < $limit;
47*0Sstevel@tonic-gatetry 6, abs(-13e21 %  4e21 -  3e21) < $limit;
48*0Sstevel@tonic-gatetry 7, abs( 13e21 % -4e21 - -3e21) < $limit;
49*0Sstevel@tonic-gatetry 8, abs(-13e21 % -4e21 - -1e21) < $limit;
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate# UVs should behave properly
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gatetryeq 9, 4063328477 % 65535, 27407;
54*0Sstevel@tonic-gatetryeq 10, 4063328477 % 4063328476, 1;
55*0Sstevel@tonic-gatetryeq 11, 4063328477 % 2031664238, 1;
56*0Sstevel@tonic-gatetryeq 12, 2031664238 % 4063328477, 2031664238;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate# These should trigger wrapping on 32 bit IVs and UVs
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gatetryeq 13, 2147483647 + 0, 2147483647;
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate# IV + IV promote to UV
63*0Sstevel@tonic-gatetryeq 14, 2147483647 + 1, 2147483648;
64*0Sstevel@tonic-gatetryeq 15, 2147483640 + 10, 2147483650;
65*0Sstevel@tonic-gatetryeq 16, 2147483647 + 2147483647, 4294967294;
66*0Sstevel@tonic-gate# IV + UV promote to NV
67*0Sstevel@tonic-gatetryeq 17, 2147483647 + 2147483649, 4294967296;
68*0Sstevel@tonic-gate# UV + IV promote to NV
69*0Sstevel@tonic-gatetryeq 18, 4294967294 + 2, 4294967296;
70*0Sstevel@tonic-gate# UV + UV promote to NV
71*0Sstevel@tonic-gatetryeq 19, 4294967295 + 4294967295, 8589934590;
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate# UV + IV to IV
74*0Sstevel@tonic-gatetryeq 20, 2147483648 + -1, 2147483647;
75*0Sstevel@tonic-gatetryeq 21, 2147483650 + -10, 2147483640;
76*0Sstevel@tonic-gate# IV + UV to IV
77*0Sstevel@tonic-gatetryeq 22, -1 + 2147483648, 2147483647;
78*0Sstevel@tonic-gatetryeq 23, -10 + 4294967294, 4294967284;
79*0Sstevel@tonic-gate# IV + IV to NV
80*0Sstevel@tonic-gatetryeq 24, -2147483648 + -2147483648, -4294967296;
81*0Sstevel@tonic-gatetryeq 25, -2147483640 + -10, -2147483650;
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate# Hmm. Don't forget the simple stuff
84*0Sstevel@tonic-gatetryeq 26, 1 + 1, 2;
85*0Sstevel@tonic-gatetryeq 27, 4 + -2, 2;
86*0Sstevel@tonic-gatetryeq 28, -10 + 100, 90;
87*0Sstevel@tonic-gatetryeq 29, -7 + -9, -16;
88*0Sstevel@tonic-gatetryeq 30, -63 + +2, -61;
89*0Sstevel@tonic-gatetryeq 31, 4 + -1, 3;
90*0Sstevel@tonic-gatetryeq 32, -1 + 1, 0;
91*0Sstevel@tonic-gatetryeq 33, +29 + -29, 0;
92*0Sstevel@tonic-gatetryeq 34, -1 + 4, 3;
93*0Sstevel@tonic-gatetryeq 35, +4 + -17, -13;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate# subtraction
96*0Sstevel@tonic-gatetryeq 36, 3 - 1, 2;
97*0Sstevel@tonic-gatetryeq 37, 3 - 15, -12;
98*0Sstevel@tonic-gatetryeq 38, 3 - -7, 10;
99*0Sstevel@tonic-gatetryeq 39, -156 - 5, -161;
100*0Sstevel@tonic-gatetryeq 40, -156 - -5, -151;
101*0Sstevel@tonic-gatetryeq 41, -5 - -12, 7;
102*0Sstevel@tonic-gatetryeq 42, -3 - -3, 0;
103*0Sstevel@tonic-gatetryeq 43, 15 - 15, 0;
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gatetryeq 44, 2147483647 - 0, 2147483647;
106*0Sstevel@tonic-gatetryeq 45, 2147483648 - 0, 2147483648;
107*0Sstevel@tonic-gatetryeq 46, -2147483648 - 0, -2147483648;
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gatetryeq 47, 0 - -2147483647, 2147483647;
110*0Sstevel@tonic-gatetryeq 48, -1 - -2147483648, 2147483647;
111*0Sstevel@tonic-gatetryeq 49, 2 - -2147483648, 2147483650;
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gatetryeq 50, 4294967294 - 3, 4294967291;
114*0Sstevel@tonic-gatetryeq 51, -2147483648 - -1, -2147483647;
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate# IV - IV promote to UV
117*0Sstevel@tonic-gatetryeq 52, 2147483647 - -1, 2147483648;
118*0Sstevel@tonic-gatetryeq 53, 2147483647 - -2147483648, 4294967295;
119*0Sstevel@tonic-gate# UV - IV promote to NV
120*0Sstevel@tonic-gatetryeq 54, 4294967294 - -3, 4294967297;
121*0Sstevel@tonic-gate# IV - IV promote to NV
122*0Sstevel@tonic-gatetryeq 55, -2147483648 - +1, -2147483649;
123*0Sstevel@tonic-gate# UV - UV promote to IV
124*0Sstevel@tonic-gatetryeq 56, 2147483648 - 2147483650, -2;
125*0Sstevel@tonic-gate# IV - UV promote to IV
126*0Sstevel@tonic-gatetryeq 57, 2000000000 - 4000000000, -2000000000;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate# No warnings should appear;
129*0Sstevel@tonic-gatemy $a;
130*0Sstevel@tonic-gate$a += 1;
131*0Sstevel@tonic-gatetryeq 58, $a, 1;
132*0Sstevel@tonic-gateundef $a;
133*0Sstevel@tonic-gate$a += -1;
134*0Sstevel@tonic-gatetryeq 59, $a, -1;
135*0Sstevel@tonic-gateundef $a;
136*0Sstevel@tonic-gate$a += 4294967290;
137*0Sstevel@tonic-gatetryeq 60, $a, 4294967290;
138*0Sstevel@tonic-gateundef $a;
139*0Sstevel@tonic-gate$a += -4294967290;
140*0Sstevel@tonic-gatetryeq 61, $a, -4294967290;
141*0Sstevel@tonic-gateundef $a;
142*0Sstevel@tonic-gate$a += 4294967297;
143*0Sstevel@tonic-gatetryeq 62, $a, 4294967297;
144*0Sstevel@tonic-gateundef $a;
145*0Sstevel@tonic-gate$a += -4294967297;
146*0Sstevel@tonic-gatetryeq 63, $a, -4294967297;
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gatemy $s;
149*0Sstevel@tonic-gate$s -= 1;
150*0Sstevel@tonic-gatetryeq 64, $s, -1;
151*0Sstevel@tonic-gateundef $s;
152*0Sstevel@tonic-gate$s -= -1;
153*0Sstevel@tonic-gatetryeq 65, $s, +1;
154*0Sstevel@tonic-gateundef $s;
155*0Sstevel@tonic-gate$s -= -4294967290;
156*0Sstevel@tonic-gatetryeq 66, $s, +4294967290;
157*0Sstevel@tonic-gateundef $s;
158*0Sstevel@tonic-gate$s -= 4294967290;
159*0Sstevel@tonic-gatetryeq 67, $s, -4294967290;
160*0Sstevel@tonic-gateundef $s;
161*0Sstevel@tonic-gate$s -= 4294967297;
162*0Sstevel@tonic-gatetryeq 68, $s, -4294967297;
163*0Sstevel@tonic-gateundef $s;
164*0Sstevel@tonic-gate$s -= -4294967297;
165*0Sstevel@tonic-gatetryeq 69, $s, +4294967297;
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate# Multiplication
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gatetryeq 70, 1 * 3, 3;
170*0Sstevel@tonic-gatetryeq 71, -2 * 3, -6;
171*0Sstevel@tonic-gatetryeq 72, 3 * -3, -9;
172*0Sstevel@tonic-gatetryeq 73, -4 * -3, 12;
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate# check with 0xFFFF and 0xFFFF
175*0Sstevel@tonic-gatetryeq 74, 65535 * 65535, 4294836225;
176*0Sstevel@tonic-gatetryeq 75, 65535 * -65535, -4294836225;
177*0Sstevel@tonic-gatetryeq 76, -65535 * 65535, -4294836225;
178*0Sstevel@tonic-gatetryeq 77, -65535 * -65535, 4294836225;
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate# check with 0xFFFF and 0x10001
181*0Sstevel@tonic-gatetryeq 78, 65535 * 65537, 4294967295;
182*0Sstevel@tonic-gatetryeq 79, 65535 * -65537, -4294967295;
183*0Sstevel@tonic-gatetryeq 80, -65535 * 65537, -4294967295;
184*0Sstevel@tonic-gatetryeq 81, -65535 * -65537, 4294967295;
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate# check with 0x10001 and 0xFFFF
187*0Sstevel@tonic-gatetryeq 82, 65537 * 65535, 4294967295;
188*0Sstevel@tonic-gatetryeq 83, 65537 * -65535, -4294967295;
189*0Sstevel@tonic-gatetryeq 84, -65537 * 65535, -4294967295;
190*0Sstevel@tonic-gatetryeq 85, -65537 * -65535, 4294967295;
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate# These should all be dones as NVs
193*0Sstevel@tonic-gatetryeq 86, 65537 * 65537, 4295098369;
194*0Sstevel@tonic-gatetryeq 87, 65537 * -65537, -4295098369;
195*0Sstevel@tonic-gatetryeq 88, -65537 * 65537, -4295098369;
196*0Sstevel@tonic-gatetryeq 89, -65537 * -65537, 4295098369;
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate# will overflow an IV (in 32-bit)
199*0Sstevel@tonic-gatetryeq 90, 46340 * 46342, 0x80001218;
200*0Sstevel@tonic-gatetryeq 91, 46340 * -46342, -0x80001218;
201*0Sstevel@tonic-gatetryeq 92, -46340 * 46342, -0x80001218;
202*0Sstevel@tonic-gatetryeq 93, -46340 * -46342, 0x80001218;
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gatetryeq 94, 46342 * 46340, 0x80001218;
205*0Sstevel@tonic-gatetryeq 95, 46342 * -46340, -0x80001218;
206*0Sstevel@tonic-gatetryeq 96, -46342 * 46340, -0x80001218;
207*0Sstevel@tonic-gatetryeq 97, -46342 * -46340, 0x80001218;
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate# will overflow a positive IV (in 32-bit)
210*0Sstevel@tonic-gatetryeq 98, 65536 * 32768, 0x80000000;
211*0Sstevel@tonic-gatetryeq 99, 65536 * -32768, -0x80000000;
212*0Sstevel@tonic-gatetryeq 100, -65536 * 32768, -0x80000000;
213*0Sstevel@tonic-gatetryeq 101, -65536 * -32768, 0x80000000;
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gatetryeq 102, 32768 * 65536, 0x80000000;
216*0Sstevel@tonic-gatetryeq 103, 32768 * -65536, -0x80000000;
217*0Sstevel@tonic-gatetryeq 104, -32768 * 65536, -0x80000000;
218*0Sstevel@tonic-gatetryeq 105, -32768 * -65536, 0x80000000;
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate# 2147483647 is prime. bah.
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gatetryeq 106, 46339 * 46341, 0x7ffea80f;
223*0Sstevel@tonic-gatetryeq 107, 46339 * -46341, -0x7ffea80f;
224*0Sstevel@tonic-gatetryeq 108, -46339 * 46341, -0x7ffea80f;
225*0Sstevel@tonic-gatetryeq 109, -46339 * -46341, 0x7ffea80f;
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gate# leading space should be ignored
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gatetryeq 110, 1 + " 1", 2;
230*0Sstevel@tonic-gatetryeq 111, 3 + " -1", 2;
231*0Sstevel@tonic-gatetryeq 112, 1.2, " 1.2";
232*0Sstevel@tonic-gatetryeq 113, -1.2, " -1.2";
233*0Sstevel@tonic-gate
234*0Sstevel@tonic-gate# divide
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gatetryeq 114, 28/14, 2;
237*0Sstevel@tonic-gatetryeq 115, 28/-7, -4;
238*0Sstevel@tonic-gatetryeq 116, -28/4, -7;
239*0Sstevel@tonic-gatetryeq 117, -28/-2, 14;
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gatetryeq 118, 0x80000000/1, 0x80000000;
242*0Sstevel@tonic-gatetryeq 119, 0x80000000/-1, -0x80000000;
243*0Sstevel@tonic-gatetryeq 120, -0x80000000/1, -0x80000000;
244*0Sstevel@tonic-gatetryeq 121, -0x80000000/-1, 0x80000000;
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate# The example for sloppy divide, rigged to avoid the peephole optimiser.
247*0Sstevel@tonic-gatetryeq_sloppy 122, "20." / "5.", 4;
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gatetryeq 123, 2.5 / 2, 1.25;
250*0Sstevel@tonic-gatetryeq 124, 3.5 / -2, -1.75;
251*0Sstevel@tonic-gatetryeq 125, -4.5 / 2, -2.25;
252*0Sstevel@tonic-gatetryeq 126, -5.5 / -2, 2.75;
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate# Bluuurg if your floating point can't accurately cope with powers of 2
255*0Sstevel@tonic-gate# [I suspect this is parsing string->float problems, not actual arith]
256*0Sstevel@tonic-gatetryeq_sloppy 127, 18446744073709551616/1, 18446744073709551616; # Bluuurg
257*0Sstevel@tonic-gatetryeq_sloppy 128, 18446744073709551616/2, 9223372036854775808;
258*0Sstevel@tonic-gatetryeq_sloppy 129, 18446744073709551616/4294967296, 4294967296;
259*0Sstevel@tonic-gatetryeq_sloppy 130, 18446744073709551616/9223372036854775808, 2;
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate{
262*0Sstevel@tonic-gate  # The peephole optimiser is wrong to think that it can substitute intops
263*0Sstevel@tonic-gate  # in place of regular ops, because i_multiply can overflow.
264*0Sstevel@tonic-gate  # Bug reported by "Sisyphus" <kalinabears@hdc.com.au>
265*0Sstevel@tonic-gate  my $n = 1127;
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate  my $float = ($n % 1000) * 167772160.0;
268*0Sstevel@tonic-gate  tryeq_sloppy 131, $float, 21307064320;
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate  # On a 32 bit machine, if the i_multiply op is used, you will probably get
271*0Sstevel@tonic-gate  # -167772160. It's actually undefined behaviour, so anything may happen.
272*0Sstevel@tonic-gate  my $int = ($n % 1000) * 167772160;
273*0Sstevel@tonic-gate  tryeq 132, $int, 21307064320;
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate  my $t = time;
276*0Sstevel@tonic-gate  my $t1000 = time() * 1000;
277*0Sstevel@tonic-gate  try 133, abs($t1000 -1000 * $t) <= 2000;
278*0Sstevel@tonic-gate}
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gateif ($^O eq 'vos') {
281*0Sstevel@tonic-gate  print "not ok 134 # TODO VOS raises SIGFPE instead of producing infinity.\n";
282*0Sstevel@tonic-gate}
283*0Sstevel@tonic-gateelsif (($^O eq 'VMS') && !defined($Config{useieee})) {
284*0Sstevel@tonic-gate  print "ok 134 # SKIP -- the IEEE infinity model is unavailable in this configuration.\n";
285*0Sstevel@tonic-gate}
286*0Sstevel@tonic-gateelsif ($^O eq 'ultrix') {
287*0Sstevel@tonic-gate  print "not ok 134 # TODO Ultrix enters deep nirvana instead of producing infinity.\n";
288*0Sstevel@tonic-gate}
289*0Sstevel@tonic-gateelse {
290*0Sstevel@tonic-gate  # The computation of $v should overflow and produce "infinity"
291*0Sstevel@tonic-gate  # on any system whose max exponent is less than 10**1506.
292*0Sstevel@tonic-gate  # The exact string used to represent infinity varies by OS,
293*0Sstevel@tonic-gate  # so we don't test for it; all we care is that we don't die.
294*0Sstevel@tonic-gate  #
295*0Sstevel@tonic-gate  # Perl considers it to be an error if SIGFPE is raised.
296*0Sstevel@tonic-gate  # Chances are the interpreter will die, since it doesn't set
297*0Sstevel@tonic-gate  # up a handler for SIGFPE.  That's why this test is last; to
298*0Sstevel@tonic-gate  # minimize the number of test failures.  --PG
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate  my $n = 5000;
301*0Sstevel@tonic-gate  my $v = 2;
302*0Sstevel@tonic-gate  while (--$n)
303*0Sstevel@tonic-gate  {
304*0Sstevel@tonic-gate    $v *= 2;
305*0Sstevel@tonic-gate  }
306*0Sstevel@tonic-gate  print "ok 134\n";
307*0Sstevel@tonic-gate}
308