8 # This file has been placed in t/opbasic to indicate that it should not use
9 # functions imported from t/test.pl or Test::More, as those programs/libraries
10 # use operators which are what is being tested in this file.
15 print +($_[1] ? "ok" : "not ok"), " $_[0] - $_[2]\n";
21 print "not ok $_[0] # $_[1] != $_[2]";
25 sub tryeq_sloppy ($$$$) {
29 my $error = abs (($_[1] - $_[2]) / $_[1]);
31 print "ok $_[0] # $_[1] is close to $_[2], \$^O eq $^O";
33 print "not ok $_[0] # $_[1] != $_[2]";
40 tryeq $T++, 13 % 4, 1, 'modulo: positive positive';
41 tryeq $T++, -13 % 4, 3, 'modulo: negative positive';
42 tryeq $T++, 13 % -4, -3, 'modulo: positive negative';
43 tryeq $T++, -13 % -4, -1, 'modulo: negative negative';
45 # Give abs() a good work-out before using it in anger
46 tryeq $T++, abs(0), 0, 'abs(): 0 0';
47 tryeq $T++, abs(1), 1, 'abs(): 1 1';
48 tryeq $T++, abs(-1), 1, 'abs(): -1 1';
49 tryeq $T++, abs(2147483647), 2147483647, 'abs(): 2**31-1: pos pos';
50 tryeq $T++, abs(-2147483647), 2147483647, 'abs(): 2**31-1: neg pos';
51 tryeq $T++, abs(4294967295), 4294967295, 'abs(): 2**32-1: pos pos';
52 tryeq $T++, abs(-4294967295), 4294967295, 'abs(): 2**32-1: neg pos';
53 tryeq $T++, abs(9223372036854775807), 9223372036854775807,
54 'abs(): 2**63-1: pos pos';
55 tryeq $T++, abs(-9223372036854775807), 9223372036854775807,
56 'abs(): 2**63-1: neg pos';
57 # Assume no change whatever; no slop needed
58 tryeq $T++, abs(1e50), 1e50, 'abs(): 1e50: pos pos';
59 # Assume only sign bit flipped
60 tryeq $T++, abs(-1e50), 1e50, 'abs(): 1e50: neg pos';
64 # Division (and modulo) of floating point numbers
65 # seem to be rather sloppy in Cray.
66 $limit = 1e8 if $^O eq 'unicos';
68 try $T++, abs( 13e21 % 4e21 - 1e21) < $limit, 'abs() for floating point';
69 try $T++, abs(-13e21 % 4e21 - 3e21) < $limit, 'abs() for floating point';
70 try $T++, abs( 13e21 % -4e21 - -3e21) < $limit, 'abs() for floating point';
71 try $T++, abs(-13e21 % -4e21 - -1e21) < $limit, 'abs() for floating point';
73 tryeq $T++, 4063328477 % 65535, 27407, 'UV behaves properly: modulo';
74 tryeq $T++, 4063328477 % 4063328476, 1, 'UV behaves properly: modulo';
75 tryeq $T++, 4063328477 % 2031664238, 1, 'UV behaves properly: modulo';
76 tryeq $T++, 2031664238 % 4063328477, 2031664238,
77 'UV behaves properly: modulo';
79 tryeq $T++, 2147483647 + 0, 2147483647,
80 'trigger wrapping on 32 bit IVs and UVs';
82 tryeq $T++, 2147483647 + 1, 2147483648, 'IV + IV promotes to UV';
83 tryeq $T++, 2147483640 + 10, 2147483650, 'IV + IV promotes to UV';
84 tryeq $T++, 2147483647 + 2147483647, 4294967294, 'IV + IV promotes to UV';
85 tryeq $T++, 2147483647 + 2147483649, 4294967296, 'IV + UV promotes to NV';
86 tryeq $T++, 4294967294 + 2, 4294967296, 'UV + IV promotes to NV';
87 tryeq $T++, 4294967295 + 4294967295, 8589934590, 'UV + UV promotes to NV';
89 tryeq $T++, 2147483648 + -1, 2147483647, 'UV + IV promotes to IV';
90 tryeq $T++, 2147483650 + -10, 2147483640, 'UV + IV promotes to IV';
91 tryeq $T++, -1 + 2147483648, 2147483647, 'IV + UV promotes to IV';
92 tryeq $T++, -10 + 4294967294, 4294967284, 'IV + UV promotes to IV';
93 tryeq $T++, -2147483648 + -2147483648, -4294967296, 'IV + IV promotes to NV';
94 tryeq $T++, -2147483640 + -10, -2147483650, 'IV + IV promotes to NV';
96 # Hmm. Do not forget the simple stuff
98 tryeq $T++, 1 + 1, 2, 'addition of 2 positive integers';
99 tryeq $T++, 4 + -2, 2, 'addition of positive and negative integer';
100 tryeq $T++, -10 + 100, 90, 'addition of negative and positive integer';
101 tryeq $T++, -7 + -9, -16, 'addition of 2 negative integers';
102 tryeq $T++, -63 + +2, -61, 'addition of signed negative and positive integers';
103 tryeq $T++, 4 + -1, 3, 'addition of positive and negative integer';
104 tryeq $T++, -1 + 1, 0, 'addition which sums to 0';
105 tryeq $T++, +29 + -29, 0, 'addition which sums to 0';
106 tryeq $T++, -1 + 4, 3, 'addition of signed negative and positive integers';
107 tryeq $T++, +4 + -17, -13, 'addition of signed positive and negative integers';
110 tryeq $T++, 3 - 1, 2, 'subtraction of two positive integers';
111 tryeq $T++, 3 - 15, -12,
112 'subtraction of two positive integers: minuend smaller';
113 tryeq $T++, 3 - -7, 10, 'subtraction of positive and negative integer';
114 tryeq $T++, -156 - 5, -161, 'subtraction of negative and positive integer';
115 tryeq $T++, -156 - -5, -151, 'subtraction of two negative integers';
116 tryeq $T++, -5 - -12, 7,
117 'subtraction of two negative integers: minuend smaller';
118 tryeq $T++, -3 - -3, 0, 'subtraction of two negative integers with result of 0';
119 tryeq $T++, 15 - 15, 0, 'subtraction of two positive integers with result of 0';
120 tryeq $T++, 2147483647 - 0, 2147483647, 'subtraction from large integer';
121 tryeq $T++, 2147483648 - 0, 2147483648, 'subtraction from large integer';
122 tryeq $T++, -2147483648 - 0, -2147483648,
123 'subtraction from large negative integer';
124 tryeq $T++, 0 - -2147483647, 2147483647,
125 'subtraction of large negative integer from 0';
126 tryeq $T++, -1 - -2147483648, 2147483647,
127 'subtraction of large negative integer from negative integer';
128 tryeq $T++, 2 - -2147483648, 2147483650,
129 'subtraction of large negative integer from positive integer';
130 tryeq $T++, 4294967294 - 3, 4294967291, 'subtraction from large integer';
131 tryeq $T++, -2147483648 - -1, -2147483647,
132 'subtraction from large negative integer';
133 tryeq $T++, 2147483647 - -1, 2147483648, 'IV - IV promote to UV';
134 tryeq $T++, 2147483647 - -2147483648, 4294967295, 'IV - IV promote to UV';
135 tryeq $T++, 4294967294 - -3, 4294967297, 'UV - IV promote to NV';
136 tryeq $T++, -2147483648 - +1, -2147483649, 'IV - IV promote to NV';
137 tryeq $T++, 2147483648 - 2147483650, -2, 'UV - UV promote to IV';
138 tryeq $T++, 2000000000 - 4000000000, -2000000000, 'IV - UV promote to IV';
140 # No warnings should appear;
143 tryeq $T++, $a, 1, '+= with positive';
146 tryeq $T++, $a, -1, '+= with negative';
149 tryeq $T++, $a, 4294967290, '+= with positive';
152 tryeq $T++, $a, -4294967290, '+= with negative';
155 tryeq $T++, $a, 4294967297, '+= with positive';
158 tryeq $T++, $a, -4294967297, '+= with negative';
162 tryeq $T++, $s, -1, '-= with positive';
165 tryeq $T++, $s, +1, '-= with negative';
168 tryeq $T++, $s, +4294967290, '-= with negative';
171 tryeq $T++, $s, -4294967290, '-= with negative';
174 tryeq $T++, $s, -4294967297, '-= with positive';
177 tryeq $T++, $s, +4294967297, '-= with positive';
180 tryeq $T++, 1 * 3, 3, 'multiplication of two positive integers';
181 tryeq $T++, -2 * 3, -6, 'multiplication of negative and positive integer';
182 tryeq $T++, 3 * -3, -9, 'multiplication of positive and negative integer';
183 tryeq $T++, -4 * -3, 12, 'multiplication of two negative integers';
185 # check with 0xFFFF and 0xFFFF
186 tryeq $T++, 65535 * 65535, 4294836225,
187 'multiplication: 0xFFFF and 0xFFFF: pos pos';
188 tryeq $T++, 65535 * -65535, -4294836225,
189 'multiplication: 0xFFFF and 0xFFFF: pos neg';
190 tryeq $T++, -65535 * 65535, -4294836225,
191 'multiplication: 0xFFFF and 0xFFFF: pos neg';
192 tryeq $T++, -65535 * -65535, 4294836225,
193 'multiplication: 0xFFFF and 0xFFFF: neg neg';
195 # check with 0xFFFF and 0x10001
196 tryeq $T++, 65535 * 65537, 4294967295,
197 'multiplication: 0xFFFF and 0x10001: pos pos';
198 tryeq $T++, 65535 * -65537, -4294967295,
199 'multiplication: 0xFFFF and 0x10001: pos neg';
200 tryeq $T++, -65535 * 65537, -4294967295,
201 'multiplication: 0xFFFF and 0x10001: neg pos';
202 tryeq $T++, -65535 * -65537, 4294967295,
203 'multiplication: 0xFFFF and 0x10001: neg neg';
205 # check with 0x10001 and 0xFFFF
206 tryeq $T++, 65537 * 65535, 4294967295,
207 'multiplication: 0x10001 and 0xFFFF: pos pos';
208 tryeq $T++, 65537 * -65535, -4294967295,
209 'multiplication: 0x10001 and 0xFFFF: pos neg';
210 tryeq $T++, -65537 * 65535, -4294967295,
211 'multiplication: 0x10001 and 0xFFFF: neg pos';
212 tryeq $T++, -65537 * -65535, 4294967295,
213 'multiplication: 0x10001 and 0xFFFF: neg neg';
215 # These should all be dones as NVs
216 tryeq $T++, 65537 * 65537, 4295098369, 'multiplication: NV: pos pos';
217 tryeq $T++, 65537 * -65537, -4295098369, 'multiplication: NV: pos neg';
218 tryeq $T++, -65537 * 65537, -4295098369, 'multiplication: NV: neg pos';
219 tryeq $T++, -65537 * -65537, 4295098369, 'multiplication: NV: neg neg';
221 # will overflow an IV (in 32-bit)
222 tryeq $T++, 46340 * 46342, 0x80001218,
223 'multiplication: overflow an IV in 32-bit: pos pos';
224 tryeq $T++, 46340 * -46342, -0x80001218,
225 'multiplication: overflow an IV in 32-bit: pos neg';
226 tryeq $T++, -46340 * 46342, -0x80001218,
227 'multiplication: overflow an IV in 32-bit: neg pos';
228 tryeq $T++, -46340 * -46342, 0x80001218,
229 'multiplication: overflow an IV in 32-bit: neg neg';
231 tryeq $T++, 46342 * 46340, 0x80001218,
232 'multiplication: overflow an IV in 32-bit: pos pos';
233 tryeq $T++, 46342 * -46340, -0x80001218,
234 'multiplication: overflow an IV in 32-bit: pos neg';
235 tryeq $T++, -46342 * 46340, -0x80001218,
236 'multiplication: overflow an IV in 32-bit: neg pos';
237 tryeq $T++, -46342 * -46340, 0x80001218,
238 'multiplication: overflow an IV in 32-bit: neg neg';
240 # will overflow a positive IV (in 32-bit)
241 tryeq $T++, 65536 * 32768, 0x80000000,
242 'multiplication: overflow a positive IV in 32-bit: pos pos';
243 tryeq $T++, 65536 * -32768, -0x80000000,
244 'multiplication: overflow a positive IV in 32-bit: pos neg';
245 tryeq $T++, -65536 * 32768, -0x80000000,
246 'multiplication: overflow a positive IV in 32-bit: neg pos';
247 tryeq $T++, -65536 * -32768, 0x80000000,
248 'multiplication: overflow a positive IV in 32-bit: neg neg';
250 tryeq $T++, 32768 * 65536, 0x80000000,
251 'multiplication: overflow a positive IV in 32-bit: pos pos';
252 tryeq $T++, 32768 * -65536, -0x80000000,
253 'multiplication: overflow a positive IV in 32-bit: pos neg';
254 tryeq $T++, -32768 * 65536, -0x80000000,
255 'multiplication: overflow a positive IV in 32-bit: neg pos';
256 tryeq $T++, -32768 * -65536, 0x80000000,
257 'multiplication: overflow a positive IV in 32-bit: neg neg';
259 # 2147483647 is prime. bah.
261 tryeq $T++, 46339 * 46341, 0x7ffea80f,
262 'multiplication: hex product: pos pos';
263 tryeq $T++, 46339 * -46341, -0x7ffea80f,
264 'multiplication: hex product: pos neg';
265 tryeq $T++, -46339 * 46341, -0x7ffea80f,
266 'multiplication: hex product: neg pos';
267 tryeq $T++, -46339 * -46341, 0x7ffea80f,
268 'multiplication: hex product: neg neg';
270 # leading space should be ignored
272 tryeq $T++, 1 + " 1", 2, 'ignore leading space: addition';
273 tryeq $T++, 3 + " -1", 2, 'ignore leading space: subtraction';
274 tryeq $T++, 1.2, " 1.2", 'floating point and string equivalent: positive';
275 tryeq $T++, -1.2, " -1.2", 'floating point and string equivalent: negative';
278 tryeq $T++, 28/14, 2, 'division of two positive integers';
279 tryeq $T++, 28/-7, -4, 'division of positive integer by negative';
280 tryeq $T++, -28/4, -7, 'division of negative integer by positive';
281 tryeq $T++, -28/-2, 14, 'division of negative integer by negative';
283 tryeq $T++, 0x80000000/1, 0x80000000,
284 'division of positive hex by positive integer';
285 tryeq $T++, 0x80000000/-1, -0x80000000,
286 'division of positive hex by negative integer';
287 tryeq $T++, -0x80000000/1, -0x80000000,
288 'division of negative hex by negative integer';
289 tryeq $T++, -0x80000000/-1, 0x80000000,
290 'division of negative hex by positive integer';
292 # The example for sloppy divide, rigged to avoid the peephole optimiser.
293 tryeq_sloppy $T++, "20." / "5.", 4, 'division of floating point without fractional part';
295 tryeq $T++, 2.5 / 2, 1.25,
296 'division of positive floating point by positive integer';
297 tryeq $T++, 3.5 / -2, -1.75,
298 'division of positive floating point by negative integer';
299 tryeq $T++, -4.5 / 2, -2.25,
300 'division of negative floating point by positive integer';
301 tryeq $T++, -5.5 / -2, 2.75,
302 'division of negative floating point by negative integer';
304 # Bluuurg if your floating point can not accurately cope with powers of 2
305 # [I suspect this is parsing string->float problems, not actual arith]
306 tryeq_sloppy $T++, 18446744073709551616/1, 18446744073709551616,
307 'division of very large number by 1'; # Bluuurg
308 tryeq_sloppy $T++, 18446744073709551616/2, 9223372036854775808,
309 'division of very large number by 2';
310 tryeq_sloppy $T++, 18446744073709551616/4294967296, 4294967296,
311 'division of two very large numbers';
312 tryeq_sloppy $T++, 18446744073709551616/9223372036854775808, 2,
313 'division of two very large numbers';
316 # The peephole optimiser is wrong to think that it can substitute intops
317 # in place of regular ops, because i_multiply can overflow.
318 # Bug reported by "Sisyphus" <kalinabears@hdc.com.au>
321 my $float = ($n % 1000) * 167772160.0;
322 tryeq_sloppy $T++, $float, 21307064320, 'integer times floating point';
324 # On a 32 bit machine, if the i_multiply op is used, you will probably get
325 # -167772160. It is actually undefined behaviour, so anything may happen.
326 my $int = ($n % 1000) * 167772160;
327 tryeq $T++, $int, 21307064320, 'integer times integer';
329 my $float2 = ($n % 1000 + 0.0) * 167772160;
330 tryeq $T++, $float2, 21307064320, 'floating point times integer';
332 my $int2 = ($n % 1000 + 0) * 167772160;
333 tryeq $T++, $int2, 21307064320, 'integer plus zero times integer';
335 # zero, but in a way that ought to be able to defeat any future optimizer:
337 my $int3 = ($n % 1000 + $zero) * 167772160;
338 tryeq $T++, $int3, 21307064320, 'defeat any future optimizer';
341 my $t1000 = time() * 1000;
342 try $T++, abs($t1000 -1000 * $t) <= 2000, 'absolute value';
349 my $float = ($n % 1000) * 720575940379279360.0;
350 tryeq_sloppy $T++, $float, 9.15131444281685e+19,
351 '64 bit: integer times floating point';
353 my $int = ($n % 1000) * 720575940379279360;
354 tryeq_sloppy $T++, $int, 9.15131444281685e+19,
355 '64 bit: integer times integer';
357 my $float2 = ($n % 1000 + 0.0) * 720575940379279360;
358 tryeq_sloppy $T++, $float2, 9.15131444281685e+19,
359 '64 bit: floating point times integer';
361 my $int2 = ($n % 1000 + 0) * 720575940379279360;
362 tryeq_sloppy $T++, $int2, 9.15131444281685e+19,
363 '64 bit: integer plus zero times integer';
365 # zero, but in a way that ought to be able to defeat any future optimizer:
367 my $int3 = ($n % 1000 + $zero) * 720575940379279360;
368 tryeq_sloppy $T++, $int3, 9.15131444281685e+19,
369 '64 bit: defeat any future optimizer';
372 # [perl #109542] $1 and "$1" should be treated the same way
373 "976562500000000" =~ /(\d+)/;
376 print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" * something\n';
379 print "not "x($a ne $b), "ok ", $T++, qq ' - something * \$1 vs "\$1"\n';
380 $a = ($1 + 102400000000000);
381 $b = ("$1" + 102400000000000);
382 print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" + something\n';
383 $a = (102400000000000 + $1);
384 $b = (102400000000000 + "$1");
385 print "not "x($a ne $b), "ok ", $T++, qq ' - something + \$1 vs "\$1"\n';
386 $a = ($1 - 10240000000000000);
387 $b = ("$1" - 10240000000000000);
388 print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" - something\n';
389 $a = (10240000000000000 - $1);
390 $b = (10240000000000000 - "$1");
391 print "not "x($a ne $b), "ok ", $T++, qq ' - something - \$1 vs "\$1"\n';
392 "976562500" =~ /(\d+)/;
395 print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" ** something\n';
399 print "not "x($a ne $b), "ok ", $T++, qq ' - something ** \$1 vs "\$1"\n';
400 "97656250000000000" =~ /(\d+)/;
403 print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" / something\n';
405 $a = (97656250000000000 / $1);
406 $b = (97656250000000000 / "$1");
407 print "not "x($a ne $b), "ok ", $T++, qq ' - something / \$1 vs "\$1"\n';
408 "97656250000000000" =~ /(\d+)/;
409 $a = ($1 <=> 97656250000000001);
410 $b = ("$1" <=> 97656250000000001);
411 print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" <=> something\n';
412 $a = (97656250000000001 <=> $1);
413 $b = (97656250000000001 <=> "$1");
414 print "not "x($a ne $b), "ok ", $T++, qq ' - something <=> \$1 vs "\$1"\n';
415 "97656250000000001" =~ /(\d+)/;
416 $a = ($1 % 97656250000000002);
417 $b = ("$1" % 97656250000000002);
418 print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" % something\n';
419 $a = (97656250000000000 % $1);
420 $b = (97656250000000000 % "$1");
421 print "not "x($a ne $b), "ok ", $T++, qq ' - something % \$1 vs "\$1"\n';
426 eval {require Config; import Config};
427 $vms_no_ieee = 1 unless defined($Config{useieee});
431 print "not ok ", $T++, " # TODO VOS raises SIGFPE instead of producing infinity.\n";
433 elsif ($vms_no_ieee) {
434 print $T++, " # SKIP -- the IEEE infinity model is unavailable in this configuration.\n"
436 elsif ($^O eq 'ultrix') {
437 print "not ok ", $T++, " # TODO Ultrix enters deep nirvana instead of producing infinity.\n";
440 # The computation of $v should overflow and produce "infinity"
441 # on any system whose max exponent is less than 10**1506.
442 # The exact string used to represent infinity varies by OS,
443 # so we don't test for it; all we care is that we don't die.
445 # Perl considers it to be an error if SIGFPE is raised.
446 # Chances are the interpreter will die, since it doesn't set
447 # up a handler for SIGFPE. That's why this test is last; to
448 # minimize the number of test failures. --PG
456 print "ok ", $T++, " - infinity\n";
461 # small numbers shouldn't round to zero if they have extra floating digits
463 try $T++, 0.153e-305 != 0.0, '0.153e-305';
464 try $T++, 0.1530e-305 != 0.0, '0.1530e-305';
465 try $T++, 0.15300e-305 != 0.0, '0.15300e-305';
466 try $T++, 0.153000e-305 != 0.0, '0.153000e-305';
467 try $T++, 0.1530000e-305 != 0.0, '0.1530000e-305';
468 try $T++, 0.1530001e-305 != 0.0, '0.1530001e-305';
469 try $T++, 1.17549435100e-38 != 0.0, 'min single';
470 # For flush-to-zero systems this may flush-to-zero, see PERL_SYS_FPU_INIT
471 try $T++, 2.2250738585072014e-308 != 0.0, 'min double';
473 # string-to-nv should equal float literals
474 try $T++, "1.23" + 0 == 1.23, '1.23';
475 try $T++, " 1.23" + 0 == 1.23, '1.23 with leading space';
476 try $T++, "1.23 " + 0 == 1.23, '1.23 with trailing space';
477 try $T++, "+1.23" + 0 == 1.23, '1.23 with unary plus';
478 try $T++, "-1.23" + 0 == -1.23, '1.23 with unary minus';
479 try $T++, "1.23e4" + 0 == 12300, '1.23e4';
481 # trigger various attempts to negate IV_MIN
483 tryeq $T++, 0x80000000 / -0x80000000, -1, '(IV_MAX+1) / IV_MIN';
484 tryeq $T++, -0x80000000 / 0x80000000, -1, 'IV_MIN / (IV_MAX+1)';
485 tryeq $T++, 0x80000000 / -1, -0x80000000, '(IV_MAX+1) / -1';
486 tryeq $T++, 0 % -0x80000000, 0, '0 % IV_MIN';
487 tryeq $T++, -0x80000000 % -0x80000000, 0, 'IV_MIN % IV_MIN';