This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline
[perl5.git] / t / op / arith.t
CommitLineData
7dca457a 1#!./perl -w
54310121 2
5479d192 3print "1..130\n";
54310121 4
5sub try ($$) {
6 print +($_[1] ? "ok" : "not ok"), " $_[0]\n";
7}
7dca457a
NC
8sub tryeq ($$$) {
9 if ($_[1] == $_[2]) {
10 print "ok $_[0]\n";
11 } else {
12 print "not ok $_[0] # $_[1] != $_[2]\n";
13 }
14}
800e6488
JH
15sub tryeq_sloppy ($$$) {
16 if ($_[1] == $_[2]) {
17 print "ok $_[0]\n";
18 } else {
19 my $error = abs ($_[1] - $_[2]) / $_[1];
20 if ($error < 1e-10) {
21 print "ok $_[0] # $_[1] is close to $_[2], \$^O eq $^O\n";
22 } else {
23 print "not ok $_[0] # $_[1] != $_[2]\n";
24 }
25 }
26}
54310121 27
7dca457a
NC
28tryeq 1, 13 % 4, 1;
29tryeq 2, -13 % 4, 3;
30tryeq 3, 13 % -4, -3;
31tryeq 4, -13 % -4, -1;
65843c0f
JH
32
33my $limit = 1e6;
34
35# Division (and modulo) of floating point numbers
36# seem to be rather sloppy in Cray.
37$limit = 1e8 if $^O eq 'unicos';
38
39try 5, abs( 13e21 % 4e21 - 1e21) < $limit;
40try 6, abs(-13e21 % 4e21 - 3e21) < $limit;
41try 7, abs( 13e21 % -4e21 - -3e21) < $limit;
42try 8, abs(-13e21 % -4e21 - -1e21) < $limit;
d658dc55
GS
43
44# UVs should behave properly
45
7dca457a
NC
46tryeq 9, 4063328477 % 65535, 27407;
47tryeq 10, 4063328477 % 4063328476, 1;
48tryeq 11, 4063328477 % 2031664238, 1;
49tryeq 12, 2031664238 % 4063328477, 2031664238;
50
51# These should trigger wrapping on 32 bit IVs and UVs
52
53tryeq 13, 2147483647 + 0, 2147483647;
54
55# IV + IV promote to UV
56tryeq 14, 2147483647 + 1, 2147483648;
57tryeq 15, 2147483640 + 10, 2147483650;
58tryeq 16, 2147483647 + 2147483647, 4294967294;
59# IV + UV promote to NV
60tryeq 17, 2147483647 + 2147483649, 4294967296;
61# UV + IV promote to NV
62tryeq 18, 4294967294 + 2, 4294967296;
63# UV + UV promote to NV
64tryeq 19, 4294967295 + 4294967295, 8589934590;
65
66# UV + IV to IV
67tryeq 20, 2147483648 + -1, 2147483647;
68tryeq 21, 2147483650 + -10, 2147483640;
69# IV + UV to IV
70tryeq 22, -1 + 2147483648, 2147483647;
71tryeq 23, -10 + 4294967294, 4294967284;
72# IV + IV to NV
73tryeq 24, -2147483648 + -2147483648, -4294967296;
74tryeq 25, -2147483640 + -10, -2147483650;
75
76# Hmm. Don't forget the simple stuff
77tryeq 26, 1 + 1, 2;
78tryeq 27, 4 + -2, 2;
79tryeq 28, -10 + 100, 90;
80tryeq 29, -7 + -9, -16;
81tryeq 30, -63 + +2, -61;
82tryeq 31, 4 + -1, 3;
83tryeq 32, -1 + 1, 0;
84tryeq 33, +29 + -29, 0;
85tryeq 34, -1 + 4, 3;
86tryeq 35, +4 + -17, -13;
87
88# subtraction
89tryeq 36, 3 - 1, 2;
90tryeq 37, 3 - 15, -12;
91tryeq 38, 3 - -7, 10;
92tryeq 39, -156 - 5, -161;
93tryeq 40, -156 - -5, -151;
94tryeq 41, -5 - -12, 7;
95tryeq 42, -3 - -3, 0;
96tryeq 43, 15 - 15, 0;
97
98tryeq 44, 2147483647 - 0, 2147483647;
99tryeq 45, 2147483648 - 0, 2147483648;
100tryeq 46, -2147483648 - 0, -2147483648;
101
102tryeq 47, 0 - -2147483647, 2147483647;
103tryeq 48, -1 - -2147483648, 2147483647;
104tryeq 49, 2 - -2147483648, 2147483650;
105
106tryeq 50, 4294967294 - 3, 4294967291;
107tryeq 51, -2147483648 - -1, -2147483647;
108
109# IV - IV promote to UV
110tryeq 52, 2147483647 - -1, 2147483648;
111tryeq 53, 2147483647 - -2147483648, 4294967295;
112# UV - IV promote to NV
113tryeq 54, 4294967294 - -3, 4294967297;
114# IV - IV promote to NV
115tryeq 55, -2147483648 - +1, -2147483649;
116# UV - UV promote to IV
117tryeq 56, 2147483648 - 2147483650, -2;
118# IV - UV promote to IV
119tryeq 57, 2000000000 - 4000000000, -2000000000;
120
121# No warnings should appear;
122my $a;
123$a += 1;
124tryeq 58, $a, 1;
125undef $a;
126$a += -1;
127tryeq 59, $a, -1;
128undef $a;
129$a += 4294967290;
130tryeq 60, $a, 4294967290;
131undef $a;
132$a += -4294967290;
133tryeq 61, $a, -4294967290;
134undef $a;
135$a += 4294967297;
136tryeq 62, $a, 4294967297;
137undef $a;
138$a += -4294967297;
139tryeq 63, $a, -4294967297;
140
141my $s;
142$s -= 1;
143tryeq 64, $s, -1;
144undef $s;
145$s -= -1;
146tryeq 65, $s, +1;
147undef $s;
148$s -= -4294967290;
149tryeq 66, $s, +4294967290;
150undef $s;
151$s -= 4294967290;
152tryeq 67, $s, -4294967290;
153undef $s;
154$s -= 4294967297;
155tryeq 68, $s, -4294967297;
156undef $s;
157$s -= -4294967297;
158tryeq 69, $s, +4294967297;
159
160# Multiplication
161
162tryeq 70, 1 * 3, 3;
163tryeq 71, -2 * 3, -6;
164tryeq 72, 3 * -3, -9;
165tryeq 73, -4 * -3, 12;
166
167# check with 0xFFFF and 0xFFFF
168tryeq 74, 65535 * 65535, 4294836225;
169tryeq 75, 65535 * -65535, -4294836225;
170tryeq 76, -65535 * 65535, -4294836225;
171tryeq 77, -65535 * -65535, 4294836225;
172
173# check with 0xFFFF and 0x10001
174tryeq 78, 65535 * 65537, 4294967295;
175tryeq 79, 65535 * -65537, -4294967295;
176tryeq 80, -65535 * 65537, -4294967295;
177tryeq 81, -65535 * -65537, 4294967295;
178
179# check with 0x10001 and 0xFFFF
180tryeq 82, 65537 * 65535, 4294967295;
181tryeq 83, 65537 * -65535, -4294967295;
182tryeq 84, -65537 * 65535, -4294967295;
183tryeq 85, -65537 * -65535, 4294967295;
184
185# These should all be dones as NVs
186tryeq 86, 65537 * 65537, 4295098369;
187tryeq 87, 65537 * -65537, -4295098369;
188tryeq 88, -65537 * 65537, -4295098369;
189tryeq 89, -65537 * -65537, 4295098369;
190
191# will overflow an IV (in 32-bit)
192tryeq 90, 46340 * 46342, 0x80001218;
193tryeq 91, 46340 * -46342, -0x80001218;
194tryeq 92, -46340 * 46342, -0x80001218;
195tryeq 93, -46340 * -46342, 0x80001218;
196
197tryeq 94, 46342 * 46340, 0x80001218;
198tryeq 95, 46342 * -46340, -0x80001218;
199tryeq 96, -46342 * 46340, -0x80001218;
200tryeq 97, -46342 * -46340, 0x80001218;
201
202# will overflow a positive IV (in 32-bit)
203tryeq 98, 65536 * 32768, 0x80000000;
204tryeq 99, 65536 * -32768, -0x80000000;
205tryeq 100, -65536 * 32768, -0x80000000;
206tryeq 101, -65536 * -32768, 0x80000000;
207
208tryeq 102, 32768 * 65536, 0x80000000;
209tryeq 103, 32768 * -65536, -0x80000000;
210tryeq 104, -32768 * 65536, -0x80000000;
211tryeq 105, -32768 * -65536, 0x80000000;
212
213# 2147483647 is prime. bah.
214
215tryeq 106, 46339 * 46341, 0x7ffea80f;
216tryeq 107, 46339 * -46341, -0x7ffea80f;
217tryeq 108, -46339 * 46341, -0x7ffea80f;
218tryeq 109, -46339 * -46341, 0x7ffea80f;
96a05aee
HS
219
220# leading space should be ignored
221
222tryeq 110, 1 + " 1", 2;
223tryeq 111, 3 + " -1", 2;
224tryeq 112, 1.2, " 1.2";
225tryeq 113, -1.2, " -1.2";
5479d192
NC
226
227# divide
228
229tryeq 114, 28/14, 2;
230tryeq 115, 28/-7, -4;
231tryeq 116, -28/4, -7;
232tryeq 117, -28/-2, 14;
233
234tryeq 118, 0x80000000/1, 0x80000000;
235tryeq 119, 0x80000000/-1, -0x80000000;
236tryeq 120, -0x80000000/1, -0x80000000;
237tryeq 121, -0x80000000/-1, 0x80000000;
238
239# The example for sloppy divide, rigged to avoid the peephole optimiser.
240tryeq 122, "20." / "5.", 4;
241
242tryeq 123, 2.5 / 2, 1.25;
243tryeq 124, 3.5 / -2, -1.75;
244tryeq 125, -4.5 / 2, -2.25;
245tryeq 126, -5.5 / -2, 2.75;
246
247# Bluuurg if your floating point can't accurately cope with powers of 2
800e6488 248tryeq_sloppy 127, 18446744073709551616/1, 18446744073709551616; # Bluuurg
5479d192
NC
249tryeq 128, 18446744073709551616/2, 9223372036854775808;
250tryeq 129, 18446744073709551616/4294967296, 4294967296;
251tryeq 130, 18446744073709551616/9223372036854775808, 2;