xref: /openbsd-src/gnu/usr.bin/perl/t/opbasic/arith.t (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
191f110e0Safresh1#!./perl -w
291f110e0Safresh1
391f110e0Safresh1# This file has been placed in t/opbasic to indicate that it should not use
491f110e0Safresh1# functions imported from t/test.pl or Test::More, as those programs/libraries
591f110e0Safresh1# use operators which are what is being tested in this file.
691f110e0Safresh1
7*3d61058aSafresh1print "1..183\n";
891f110e0Safresh1
991f110e0Safresh1sub try ($$$) {
10b46d8ef2Safresh1   print +($_[1] ? "ok" : "not ok") . " $_[0] - $_[2]\n";
1191f110e0Safresh1}
1291f110e0Safresh1sub tryeq ($$$$) {
13b46d8ef2Safresh1  my $status;
1491f110e0Safresh1  if ($_[1] == $_[2]) {
15b46d8ef2Safresh1    $status = "ok $_[0]";
1691f110e0Safresh1  } else {
17b46d8ef2Safresh1    $status = "not ok $_[0] # $_[1] != $_[2]";
1891f110e0Safresh1  }
19b46d8ef2Safresh1  print "$status - $_[3]\n";
2091f110e0Safresh1}
2191f110e0Safresh1sub tryeq_sloppy ($$$$) {
22b46d8ef2Safresh1  my $status;
2391f110e0Safresh1  if ($_[1] == $_[2]) {
24b46d8ef2Safresh1    $status = "ok $_[0]";
2591f110e0Safresh1  } else {
2691f110e0Safresh1    my $error = abs (($_[1] - $_[2]) / $_[1]);
2791f110e0Safresh1    if ($error < 1e-9) {
28b46d8ef2Safresh1      $status = "ok $_[0] # $_[1] is close to $_[2], \$^O eq $^O";
2991f110e0Safresh1    } else {
30b46d8ef2Safresh1      $status = "not ok $_[0] # $_[1] != $_[2]";
3191f110e0Safresh1    }
3291f110e0Safresh1  }
33b46d8ef2Safresh1  print "$status - $_[3]\n";
3491f110e0Safresh1}
3591f110e0Safresh1
3691f110e0Safresh1my $T = 1;
3791f110e0Safresh1tryeq $T++,  13 %  4, 1, 'modulo: positive positive';
3891f110e0Safresh1tryeq $T++, -13 %  4, 3, 'modulo: negative positive';
3991f110e0Safresh1tryeq $T++,  13 % -4, -3, 'modulo: positive negative';
4091f110e0Safresh1tryeq $T++, -13 % -4, -1, 'modulo: negative negative';
4191f110e0Safresh1
42eac174f2Safresh1# Exercise some of the dright/dleft logic in pp_modulo
43eac174f2Safresh1
44eac174f2Safresh1tryeq $T++, 13.333333 % 5.333333, 3, 'modulo: 13.333333 % 5.333333';
45eac174f2Safresh1tryeq $T++, 13.333333 % 5,        3, 'modulo: 13.333333 % 5';
46eac174f2Safresh1tryeq $T++, 13 % 5.333333,        3, 'modulo: 13 % 5.333333';
47eac174f2Safresh1
4891f110e0Safresh1# Give abs() a good work-out before using it in anger
4991f110e0Safresh1tryeq $T++, abs(0), 0, 'abs(): 0 0';
5091f110e0Safresh1tryeq $T++, abs(1), 1, 'abs(): 1 1';
5191f110e0Safresh1tryeq $T++, abs(-1), 1, 'abs(): -1 1';
5291f110e0Safresh1tryeq $T++, abs(2147483647), 2147483647, 'abs(): 2**31-1: pos pos';
5391f110e0Safresh1tryeq $T++, abs(-2147483647), 2147483647, 'abs(): 2**31-1: neg pos';
5491f110e0Safresh1tryeq $T++, abs(4294967295), 4294967295, 'abs(): 2**32-1: pos pos';
5591f110e0Safresh1tryeq $T++, abs(-4294967295), 4294967295, 'abs(): 2**32-1: neg pos';
5691f110e0Safresh1tryeq $T++, abs(9223372036854775807), 9223372036854775807,
5791f110e0Safresh1    'abs(): 2**63-1: pos pos';
5891f110e0Safresh1tryeq $T++, abs(-9223372036854775807), 9223372036854775807,
5991f110e0Safresh1    'abs(): 2**63-1: neg pos';
6091f110e0Safresh1# Assume no change whatever; no slop needed
6191f110e0Safresh1tryeq $T++, abs(1e50), 1e50, 'abs(): 1e50: pos pos';
6291f110e0Safresh1# Assume only sign bit flipped
6391f110e0Safresh1tryeq $T++, abs(-1e50), 1e50, 'abs(): 1e50: neg pos';
6491f110e0Safresh1
6591f110e0Safresh1my $limit = 1e6;
6691f110e0Safresh1
6791f110e0Safresh1# Division (and modulo) of floating point numbers
6891f110e0Safresh1# seem to be rather sloppy in Cray.
6991f110e0Safresh1$limit = 1e8 if $^O eq 'unicos';
7091f110e0Safresh1
7191f110e0Safresh1try $T++, abs( 13e21 %  4e21 -  1e21) < $limit, 'abs() for floating point';
7291f110e0Safresh1try $T++, abs(-13e21 %  4e21 -  3e21) < $limit, 'abs() for floating point';
7391f110e0Safresh1try $T++, abs( 13e21 % -4e21 - -3e21) < $limit, 'abs() for floating point';
7491f110e0Safresh1try $T++, abs(-13e21 % -4e21 - -1e21) < $limit, 'abs() for floating point';
7591f110e0Safresh1
7691f110e0Safresh1tryeq $T++, 4063328477 % 65535, 27407, 'UV behaves properly: modulo';
7791f110e0Safresh1tryeq $T++, 4063328477 % 4063328476, 1, 'UV behaves properly: modulo';
7891f110e0Safresh1tryeq $T++, 4063328477 % 2031664238, 1, 'UV behaves properly: modulo';
7991f110e0Safresh1tryeq $T++, 2031664238 % 4063328477, 2031664238,
8091f110e0Safresh1    'UV behaves properly: modulo';
8191f110e0Safresh1
8291f110e0Safresh1tryeq $T++, 2147483647 + 0, 2147483647,
8391f110e0Safresh1    'trigger wrapping on 32 bit IVs and UVs';
8491f110e0Safresh1
8591f110e0Safresh1tryeq $T++, 2147483647 + 1, 2147483648, 'IV + IV promotes to UV';
8691f110e0Safresh1tryeq $T++, 2147483640 + 10, 2147483650, 'IV + IV promotes to UV';
8791f110e0Safresh1tryeq $T++, 2147483647 + 2147483647, 4294967294, 'IV + IV promotes to UV';
8891f110e0Safresh1tryeq $T++, 2147483647 + 2147483649, 4294967296, 'IV + UV promotes to NV';
8991f110e0Safresh1tryeq $T++, 4294967294 + 2, 4294967296, 'UV + IV promotes to NV';
9091f110e0Safresh1tryeq $T++, 4294967295 + 4294967295, 8589934590, 'UV + UV promotes to NV';
9191f110e0Safresh1
9291f110e0Safresh1tryeq $T++, 2147483648 + -1, 2147483647, 'UV + IV promotes to IV';
9391f110e0Safresh1tryeq $T++, 2147483650 + -10, 2147483640, 'UV + IV promotes to IV';
9491f110e0Safresh1tryeq $T++, -1 + 2147483648, 2147483647, 'IV + UV promotes to IV';
9591f110e0Safresh1tryeq $T++, -10 + 4294967294, 4294967284, 'IV + UV promotes to IV';
9691f110e0Safresh1tryeq $T++, -2147483648 + -2147483648, -4294967296, 'IV + IV promotes to NV';
9791f110e0Safresh1tryeq $T++, -2147483640 + -10, -2147483650, 'IV + IV promotes to NV';
9891f110e0Safresh1
9991f110e0Safresh1# Hmm. Do not forget the simple stuff
10091f110e0Safresh1# addition
10191f110e0Safresh1tryeq $T++, 1 + 1, 2, 'addition of 2 positive integers';
10291f110e0Safresh1tryeq $T++, 4 + -2, 2, 'addition of positive and negative integer';
10391f110e0Safresh1tryeq $T++, -10 + 100, 90, 'addition of negative and positive integer';
10491f110e0Safresh1tryeq $T++, -7 + -9, -16, 'addition of 2 negative integers';
10591f110e0Safresh1tryeq $T++, -63 + +2, -61, 'addition of signed negative and positive integers';
10691f110e0Safresh1tryeq $T++, 4 + -1, 3, 'addition of positive and negative integer';
10791f110e0Safresh1tryeq $T++, -1 + 1, 0, 'addition which sums to 0';
10891f110e0Safresh1tryeq $T++, +29 + -29, 0, 'addition which sums to 0';
10991f110e0Safresh1tryeq $T++, -1 + 4, 3, 'addition of signed negative and positive integers';
11091f110e0Safresh1tryeq $T++, +4 + -17, -13, 'addition of signed positive and negative integers';
11191f110e0Safresh1
11291f110e0Safresh1# subtraction
11391f110e0Safresh1tryeq $T++, 3 - 1, 2, 'subtraction of two positive integers';
11491f110e0Safresh1tryeq $T++, 3 - 15, -12,
11591f110e0Safresh1    'subtraction of two positive integers: minuend smaller';
11691f110e0Safresh1tryeq $T++, 3 - -7, 10, 'subtraction of positive and negative integer';
11791f110e0Safresh1tryeq $T++, -156 - 5, -161, 'subtraction of negative and positive integer';
11891f110e0Safresh1tryeq $T++, -156 - -5, -151, 'subtraction of two negative integers';
11991f110e0Safresh1tryeq $T++, -5 - -12, 7,
12091f110e0Safresh1    'subtraction of two negative integers: minuend smaller';
12191f110e0Safresh1tryeq $T++, -3 - -3, 0, 'subtraction of two negative integers with result of 0';
12291f110e0Safresh1tryeq $T++, 15 - 15, 0, 'subtraction of two positive integers with result of 0';
12391f110e0Safresh1tryeq $T++, 2147483647 - 0, 2147483647, 'subtraction from large integer';
12491f110e0Safresh1tryeq $T++, 2147483648 - 0, 2147483648, 'subtraction from large integer';
12591f110e0Safresh1tryeq $T++, -2147483648 - 0, -2147483648,
12691f110e0Safresh1    'subtraction from large negative integer';
12791f110e0Safresh1tryeq $T++, 0 - -2147483647, 2147483647,
12891f110e0Safresh1    'subtraction of large negative integer from 0';
12991f110e0Safresh1tryeq $T++, -1 - -2147483648, 2147483647,
13091f110e0Safresh1    'subtraction of large negative integer from negative integer';
13191f110e0Safresh1tryeq $T++, 2 - -2147483648, 2147483650,
13291f110e0Safresh1    'subtraction of large negative integer from positive integer';
13391f110e0Safresh1tryeq $T++, 4294967294 - 3, 4294967291, 'subtraction from large integer';
13491f110e0Safresh1tryeq $T++, -2147483648 - -1, -2147483647,
13591f110e0Safresh1    'subtraction from large negative integer';
13691f110e0Safresh1tryeq $T++, 2147483647 - -1, 2147483648, 'IV - IV promote to UV';
13791f110e0Safresh1tryeq $T++, 2147483647 - -2147483648, 4294967295, 'IV - IV promote to UV';
13891f110e0Safresh1tryeq $T++, 4294967294 - -3, 4294967297, 'UV - IV promote to NV';
13991f110e0Safresh1tryeq $T++, -2147483648 - +1, -2147483649, 'IV - IV promote to NV';
14091f110e0Safresh1tryeq $T++, 2147483648 - 2147483650, -2, 'UV - UV promote to IV';
14191f110e0Safresh1tryeq $T++, 2000000000 - 4000000000, -2000000000, 'IV - UV promote to IV';
14291f110e0Safresh1
14391f110e0Safresh1# No warnings should appear;
14491f110e0Safresh1my $a;
14591f110e0Safresh1$a += 1;
14691f110e0Safresh1tryeq $T++, $a, 1, '+= with positive';
14791f110e0Safresh1undef $a;
14891f110e0Safresh1$a += -1;
14991f110e0Safresh1tryeq $T++, $a, -1, '+= with negative';
15091f110e0Safresh1undef $a;
15191f110e0Safresh1$a += 4294967290;
15291f110e0Safresh1tryeq $T++, $a, 4294967290, '+= with positive';
15391f110e0Safresh1undef $a;
15491f110e0Safresh1$a += -4294967290;
15591f110e0Safresh1tryeq $T++, $a, -4294967290, '+= with negative';
15691f110e0Safresh1undef $a;
15791f110e0Safresh1$a += 4294967297;
15891f110e0Safresh1tryeq $T++, $a, 4294967297, '+= with positive';
15991f110e0Safresh1undef $a;
16091f110e0Safresh1$a += -4294967297;
16191f110e0Safresh1tryeq $T++, $a, -4294967297, '+= with negative';
16291f110e0Safresh1
16391f110e0Safresh1my $s;
16491f110e0Safresh1$s -= 1;
16591f110e0Safresh1tryeq $T++, $s, -1, '-= with positive';
16691f110e0Safresh1undef $s;
16791f110e0Safresh1$s -= -1;
16891f110e0Safresh1tryeq $T++, $s, +1, '-= with negative';
16991f110e0Safresh1undef $s;
17091f110e0Safresh1$s -= -4294967290;
17191f110e0Safresh1tryeq $T++, $s, +4294967290, '-= with negative';
17291f110e0Safresh1undef $s;
17391f110e0Safresh1$s -= 4294967290;
17491f110e0Safresh1tryeq $T++, $s, -4294967290, '-= with negative';
17591f110e0Safresh1undef $s;
17691f110e0Safresh1$s -= 4294967297;
17791f110e0Safresh1tryeq $T++, $s, -4294967297, '-= with positive';
17891f110e0Safresh1undef $s;
17991f110e0Safresh1$s -= -4294967297;
18091f110e0Safresh1tryeq $T++, $s, +4294967297, '-= with positive';
18191f110e0Safresh1
18291f110e0Safresh1# multiplication
18391f110e0Safresh1tryeq $T++, 1 * 3, 3, 'multiplication of two positive integers';
18491f110e0Safresh1tryeq $T++, -2 * 3, -6, 'multiplication of negative and positive integer';
18591f110e0Safresh1tryeq $T++, 3 * -3, -9, 'multiplication of positive and negative integer';
18691f110e0Safresh1tryeq $T++, -4 * -3, 12, 'multiplication of two negative integers';
18791f110e0Safresh1
18891f110e0Safresh1# check with 0xFFFF and 0xFFFF
18991f110e0Safresh1tryeq $T++, 65535 * 65535, 4294836225,
19091f110e0Safresh1    'multiplication: 0xFFFF and 0xFFFF: pos pos';
19191f110e0Safresh1tryeq $T++, 65535 * -65535, -4294836225,
19291f110e0Safresh1    'multiplication: 0xFFFF and 0xFFFF: pos neg';
19391f110e0Safresh1tryeq $T++, -65535 * 65535, -4294836225,
19491f110e0Safresh1    'multiplication: 0xFFFF and 0xFFFF: pos neg';
19591f110e0Safresh1tryeq $T++, -65535 * -65535, 4294836225,
19691f110e0Safresh1    'multiplication: 0xFFFF and 0xFFFF: neg neg';
19791f110e0Safresh1
19891f110e0Safresh1# check with 0xFFFF and 0x10001
19991f110e0Safresh1tryeq $T++, 65535 * 65537, 4294967295,
20091f110e0Safresh1    'multiplication: 0xFFFF and 0x10001: pos pos';
20191f110e0Safresh1tryeq $T++, 65535 * -65537, -4294967295,
20291f110e0Safresh1    'multiplication: 0xFFFF and 0x10001: pos neg';
20391f110e0Safresh1tryeq $T++, -65535 * 65537, -4294967295,
20491f110e0Safresh1    'multiplication: 0xFFFF and 0x10001: neg pos';
20591f110e0Safresh1tryeq $T++, -65535 * -65537, 4294967295,
20691f110e0Safresh1    'multiplication: 0xFFFF and 0x10001: neg neg';
20791f110e0Safresh1
20891f110e0Safresh1# check with 0x10001 and 0xFFFF
20991f110e0Safresh1tryeq $T++, 65537 * 65535, 4294967295,
21091f110e0Safresh1    'multiplication: 0x10001 and 0xFFFF: pos pos';
21191f110e0Safresh1tryeq $T++, 65537 * -65535, -4294967295,
21291f110e0Safresh1    'multiplication: 0x10001 and 0xFFFF: pos neg';
21391f110e0Safresh1tryeq $T++, -65537 * 65535, -4294967295,
21491f110e0Safresh1    'multiplication: 0x10001 and 0xFFFF: neg pos';
21591f110e0Safresh1tryeq $T++, -65537 * -65535, 4294967295,
21691f110e0Safresh1    'multiplication: 0x10001 and 0xFFFF: neg neg';
21791f110e0Safresh1
21891f110e0Safresh1# These should all be dones as NVs
21991f110e0Safresh1tryeq $T++, 65537 * 65537, 4295098369, 'multiplication: NV: pos pos';
22091f110e0Safresh1tryeq $T++, 65537 * -65537, -4295098369, 'multiplication: NV: pos neg';
22191f110e0Safresh1tryeq $T++, -65537 * 65537, -4295098369, 'multiplication: NV: neg pos';
22291f110e0Safresh1tryeq $T++, -65537 * -65537, 4295098369, 'multiplication: NV: neg neg';
22391f110e0Safresh1
22491f110e0Safresh1# will overflow an IV (in 32-bit)
22591f110e0Safresh1tryeq $T++, 46340 * 46342, 0x80001218,
22691f110e0Safresh1    'multiplication: overflow an IV in 32-bit: pos pos';
22791f110e0Safresh1tryeq $T++, 46340 * -46342, -0x80001218,
22891f110e0Safresh1    'multiplication: overflow an IV in 32-bit: pos neg';
22991f110e0Safresh1tryeq $T++, -46340 * 46342, -0x80001218,
23091f110e0Safresh1    'multiplication: overflow an IV in 32-bit: neg pos';
23191f110e0Safresh1tryeq $T++, -46340 * -46342, 0x80001218,
23291f110e0Safresh1    'multiplication: overflow an IV in 32-bit: neg neg';
23391f110e0Safresh1
23491f110e0Safresh1tryeq $T++, 46342 * 46340, 0x80001218,
23591f110e0Safresh1    'multiplication: overflow an IV in 32-bit: pos pos';
23691f110e0Safresh1tryeq $T++, 46342 * -46340, -0x80001218,
23791f110e0Safresh1    'multiplication: overflow an IV in 32-bit: pos neg';
23891f110e0Safresh1tryeq $T++, -46342 * 46340, -0x80001218,
23991f110e0Safresh1    'multiplication: overflow an IV in 32-bit: neg pos';
24091f110e0Safresh1tryeq $T++, -46342 * -46340, 0x80001218,
24191f110e0Safresh1    'multiplication: overflow an IV in 32-bit: neg neg';
24291f110e0Safresh1
24391f110e0Safresh1# will overflow a positive IV (in 32-bit)
24491f110e0Safresh1tryeq $T++, 65536 * 32768, 0x80000000,
24591f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: pos pos';
24691f110e0Safresh1tryeq $T++, 65536 * -32768, -0x80000000,
24791f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: pos neg';
24891f110e0Safresh1tryeq $T++, -65536 * 32768, -0x80000000,
24991f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: neg pos';
25091f110e0Safresh1tryeq $T++, -65536 * -32768, 0x80000000,
25191f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: neg neg';
25291f110e0Safresh1
25391f110e0Safresh1tryeq $T++, 32768 * 65536, 0x80000000,
25491f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: pos pos';
25591f110e0Safresh1tryeq $T++, 32768 * -65536, -0x80000000,
25691f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: pos neg';
25791f110e0Safresh1tryeq $T++, -32768 * 65536, -0x80000000,
25891f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: neg pos';
25991f110e0Safresh1tryeq $T++, -32768 * -65536, 0x80000000,
26091f110e0Safresh1    'multiplication: overflow a positive IV in 32-bit: neg neg';
26191f110e0Safresh1
26291f110e0Safresh1# 2147483647 is prime. bah.
26391f110e0Safresh1
26491f110e0Safresh1tryeq $T++, 46339 * 46341, 0x7ffea80f,
26591f110e0Safresh1    'multiplication: hex product: pos pos';
26691f110e0Safresh1tryeq $T++, 46339 * -46341, -0x7ffea80f,
26791f110e0Safresh1    'multiplication: hex product: pos neg';
26891f110e0Safresh1tryeq $T++, -46339 * 46341, -0x7ffea80f,
26991f110e0Safresh1    'multiplication: hex product: neg pos';
27091f110e0Safresh1tryeq $T++, -46339 * -46341, 0x7ffea80f,
27191f110e0Safresh1    'multiplication: hex product: neg neg';
27291f110e0Safresh1
27391f110e0Safresh1# leading space should be ignored
27491f110e0Safresh1
27591f110e0Safresh1tryeq $T++, 1 + " 1", 2, 'ignore leading space: addition';
27691f110e0Safresh1tryeq $T++, 3 + " -1", 2, 'ignore leading space: subtraction';
27791f110e0Safresh1tryeq $T++, 1.2, " 1.2", 'floating point and string equivalent: positive';
27891f110e0Safresh1tryeq $T++, -1.2, " -1.2", 'floating point and string equivalent: negative';
27991f110e0Safresh1
28091f110e0Safresh1# division
28191f110e0Safresh1tryeq $T++, 28/14, 2, 'division of two positive integers';
28291f110e0Safresh1tryeq $T++, 28/-7, -4, 'division of positive integer by negative';
28391f110e0Safresh1tryeq $T++, -28/4, -7, 'division of negative integer by positive';
28491f110e0Safresh1tryeq $T++, -28/-2, 14, 'division of negative integer by negative';
28591f110e0Safresh1
28691f110e0Safresh1tryeq $T++, 0x80000000/1, 0x80000000,
28791f110e0Safresh1    'division of positive hex by positive integer';
28891f110e0Safresh1tryeq $T++, 0x80000000/-1, -0x80000000,
28991f110e0Safresh1    'division of positive hex by negative integer';
29091f110e0Safresh1tryeq $T++, -0x80000000/1, -0x80000000,
29191f110e0Safresh1    'division of negative hex by negative integer';
29291f110e0Safresh1tryeq $T++, -0x80000000/-1, 0x80000000,
29391f110e0Safresh1    'division of negative hex by positive integer';
29491f110e0Safresh1
29591f110e0Safresh1# The example for sloppy divide, rigged to avoid the peephole optimiser.
29691f110e0Safresh1tryeq_sloppy $T++, "20." / "5.", 4, 'division of floating point without fractional part';
29791f110e0Safresh1
29891f110e0Safresh1tryeq $T++, 2.5 / 2, 1.25,
29991f110e0Safresh1    'division of positive floating point by positive integer';
30091f110e0Safresh1tryeq $T++, 3.5 / -2, -1.75,
30191f110e0Safresh1    'division of positive floating point by negative integer';
30291f110e0Safresh1tryeq $T++, -4.5 / 2, -2.25,
30391f110e0Safresh1    'division of negative floating point by positive integer';
30491f110e0Safresh1tryeq $T++, -5.5 / -2, 2.75,
30591f110e0Safresh1    'division of negative floating point by negative integer';
30691f110e0Safresh1
30791f110e0Safresh1# Bluuurg if your floating point can not accurately cope with powers of 2
30891f110e0Safresh1# [I suspect this is parsing string->float problems, not actual arith]
30991f110e0Safresh1tryeq_sloppy $T++, 18446744073709551616/1, 18446744073709551616,
31091f110e0Safresh1    'division of very large number by 1'; # Bluuurg
31191f110e0Safresh1tryeq_sloppy $T++, 18446744073709551616/2, 9223372036854775808,
31291f110e0Safresh1    'division of very large number by 2';
31391f110e0Safresh1tryeq_sloppy $T++, 18446744073709551616/4294967296, 4294967296,
31491f110e0Safresh1    'division of two very large numbers';
31591f110e0Safresh1tryeq_sloppy $T++, 18446744073709551616/9223372036854775808, 2,
31691f110e0Safresh1    'division of two very large numbers';
31791f110e0Safresh1
31891f110e0Safresh1{
31991f110e0Safresh1  # The peephole optimiser is wrong to think that it can substitute intops
32091f110e0Safresh1  # in place of regular ops, because i_multiply can overflow.
32191f110e0Safresh1  # Bug reported by "Sisyphus" <kalinabears@hdc.com.au>
32291f110e0Safresh1  my $n = 1127;
32391f110e0Safresh1
32491f110e0Safresh1  my $float = ($n % 1000) * 167772160.0;
32591f110e0Safresh1  tryeq_sloppy $T++, $float, 21307064320, 'integer times floating point';
32691f110e0Safresh1
32791f110e0Safresh1  # On a 32 bit machine, if the i_multiply op is used, you will probably get
32891f110e0Safresh1  # -167772160. It is actually undefined behaviour, so anything may happen.
32991f110e0Safresh1  my $int = ($n % 1000) * 167772160;
33091f110e0Safresh1  tryeq $T++, $int, 21307064320, 'integer times integer';
33191f110e0Safresh1
33291f110e0Safresh1  my $float2 = ($n % 1000 + 0.0) * 167772160;
33391f110e0Safresh1  tryeq $T++, $float2, 21307064320, 'floating point times integer';
33491f110e0Safresh1
33591f110e0Safresh1  my $int2 = ($n % 1000 + 0) * 167772160;
33691f110e0Safresh1  tryeq $T++, $int2, 21307064320, 'integer plus zero times integer';
33791f110e0Safresh1
33891f110e0Safresh1  # zero, but in a way that ought to be able to defeat any future optimizer:
33991f110e0Safresh1  my $zero = $$ - $$;
34091f110e0Safresh1  my $int3 = ($n % 1000 + $zero) * 167772160;
34191f110e0Safresh1  tryeq $T++, $int3, 21307064320, 'defeat any future optimizer';
34291f110e0Safresh1
34391f110e0Safresh1  my $t = time;
34491f110e0Safresh1  my $t1000 = time() * 1000;
34591f110e0Safresh1  try $T++, abs($t1000 -1000 * $t) <= 2000, 'absolute value';
34691f110e0Safresh1}
34791f110e0Safresh1
34891f110e0Safresh1{
34991f110e0Safresh1  # 64 bit variants
35091f110e0Safresh1  my $n = 1127;
35191f110e0Safresh1
35291f110e0Safresh1  my $float = ($n % 1000) * 720575940379279360.0;
35391f110e0Safresh1  tryeq_sloppy $T++, $float, 9.15131444281685e+19,
35491f110e0Safresh1    '64 bit: integer times floating point';
35591f110e0Safresh1
35691f110e0Safresh1  my $int = ($n % 1000) * 720575940379279360;
35791f110e0Safresh1  tryeq_sloppy $T++, $int, 9.15131444281685e+19,
35891f110e0Safresh1    '64 bit: integer times integer';
35991f110e0Safresh1
36091f110e0Safresh1  my $float2 = ($n % 1000 + 0.0) * 720575940379279360;
36191f110e0Safresh1  tryeq_sloppy $T++, $float2, 9.15131444281685e+19,
36291f110e0Safresh1    '64 bit: floating point times integer';
36391f110e0Safresh1
36491f110e0Safresh1  my $int2 = ($n % 1000 + 0) * 720575940379279360;
36591f110e0Safresh1  tryeq_sloppy $T++, $int2, 9.15131444281685e+19,
36691f110e0Safresh1    '64 bit: integer plus zero times integer';
36791f110e0Safresh1
36891f110e0Safresh1  # zero, but in a way that ought to be able to defeat any future optimizer:
36991f110e0Safresh1  my $zero = $$ - $$;
37091f110e0Safresh1  my $int3 = ($n % 1000 + $zero) * 720575940379279360;
37191f110e0Safresh1  tryeq_sloppy $T++, $int3, 9.15131444281685e+19,
37291f110e0Safresh1    '64 bit: defeat any future optimizer';
37391f110e0Safresh1}
37491f110e0Safresh1
37591f110e0Safresh1# [perl #109542] $1 and "$1" should be treated the same way
37691f110e0Safresh1"976562500000000" =~ /(\d+)/;
37791f110e0Safresh1$a = ($1 * 1024);
37891f110e0Safresh1$b = ("$1" * 1024);
37991f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" * something\n';
38091f110e0Safresh1$a = (1024 * $1);
38191f110e0Safresh1$b = (1024 * "$1");
38291f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - something * \$1 vs "\$1"\n';
38391f110e0Safresh1$a = ($1 + 102400000000000);
38491f110e0Safresh1$b = ("$1" + 102400000000000);
38591f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" + something\n';
38691f110e0Safresh1$a = (102400000000000 + $1);
38791f110e0Safresh1$b = (102400000000000 + "$1");
38891f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - something + \$1 vs "\$1"\n';
38991f110e0Safresh1$a = ($1 - 10240000000000000);
39091f110e0Safresh1$b = ("$1" - 10240000000000000);
39191f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" - something\n';
39291f110e0Safresh1$a = (10240000000000000 - $1);
39391f110e0Safresh1$b = (10240000000000000 - "$1");
39491f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - something - \$1 vs "\$1"\n';
39591f110e0Safresh1"976562500" =~ /(\d+)/;
39691f110e0Safresh1$a = ($1 ** 2);
39791f110e0Safresh1$b = ("$1" ** 2);
39891f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" ** something\n';
39991f110e0Safresh1"32" =~ /(\d+)/;
40091f110e0Safresh1$a = (3 ** $1);
40191f110e0Safresh1$b = (3 ** "$1");
40291f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - something ** \$1 vs "\$1"\n';
40391f110e0Safresh1"97656250000000000" =~ /(\d+)/;
40491f110e0Safresh1$a = ($1 / 10);
40591f110e0Safresh1$b = ("$1" / 10);
40691f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" / something\n';
40791f110e0Safresh1"10" =~ /(\d+)/;
40891f110e0Safresh1$a = (97656250000000000 / $1);
40991f110e0Safresh1$b = (97656250000000000 / "$1");
41091f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - something / \$1 vs "\$1"\n';
41191f110e0Safresh1"97656250000000000" =~ /(\d+)/;
41291f110e0Safresh1$a = ($1 <=> 97656250000000001);
41391f110e0Safresh1$b = ("$1" <=> 97656250000000001);
41491f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" <=> something\n';
41591f110e0Safresh1$a = (97656250000000001 <=> $1);
41691f110e0Safresh1$b = (97656250000000001 <=> "$1");
41791f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - something <=> \$1 vs "\$1"\n';
41891f110e0Safresh1"97656250000000001" =~ /(\d+)/;
41991f110e0Safresh1$a = ($1 % 97656250000000002);
42091f110e0Safresh1$b = ("$1" % 97656250000000002);
42191f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - \$1 vs "\$1" % something\n';
42291f110e0Safresh1$a = (97656250000000000 % $1);
42391f110e0Safresh1$b = (97656250000000000 % "$1");
42491f110e0Safresh1print "not "x($a ne $b), "ok ", $T++, qq ' - something % \$1 vs "\$1"\n';
42591f110e0Safresh1
426b8851fccSafresh1# string-to-nv should equal float literals
427b8851fccSafresh1try $T++, "1.23"   + 0 ==  1.23,  '1.23';
428b8851fccSafresh1try $T++, " 1.23"  + 0 ==  1.23,  '1.23 with leading space';
429b8851fccSafresh1try $T++, "1.23 "  + 0 ==  1.23,  '1.23 with trailing space';
430b8851fccSafresh1try $T++, "+1.23"  + 0 ==  1.23,  '1.23 with unary plus';
431b8851fccSafresh1try $T++, "-1.23"  + 0 == -1.23,  '1.23 with unary minus';
432b8851fccSafresh1try $T++, "1.23e4" + 0 ==  12300, '1.23e4';
433b8851fccSafresh1
434b8851fccSafresh1# trigger various attempts to negate IV_MIN
435b8851fccSafresh1
436b8851fccSafresh1tryeq $T++,  0x80000000 / -0x80000000, -1, '(IV_MAX+1) / IV_MIN';
437b8851fccSafresh1tryeq $T++, -0x80000000 /  0x80000000, -1, 'IV_MIN / (IV_MAX+1)';
438b8851fccSafresh1tryeq $T++,  0x80000000 / -1, -0x80000000, '(IV_MAX+1) / -1';
439b8851fccSafresh1tryeq $T++,           0 % -0x80000000,  0, '0 % IV_MIN';
440b8851fccSafresh1tryeq $T++, -0x80000000 % -0x80000000,  0, 'IV_MIN % IV_MIN';
441*3d61058aSafresh1
442*3d61058aSafresh1tryeq $T++,  0x3ffffffe % -0xc0000000, -0x80000002, 'modulo is (IV_MIN-2)';
443*3d61058aSafresh1tryeq $T++,  0x3fffffff % -0xc0000000, -0x80000001, 'modulo is (IV_MIN-1)';
444*3d61058aSafresh1tryeq $T++,  0x40000000 % -0xc0000000, -0x80000000, 'modulo is IV_MIN';
445