This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test case for C<undef %File::Glob::>
[perl5.git] / t / op / arith.t
CommitLineData
7dca457a 1#!./perl -w
54310121 2
7dca457a 3print "1..109\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}
54310121 15
7dca457a
NC
16tryeq 1, 13 % 4, 1;
17tryeq 2, -13 % 4, 3;
18tryeq 3, 13 % -4, -3;
19tryeq 4, -13 % -4, -1;
65843c0f
JH
20
21my $limit = 1e6;
22
23# Division (and modulo) of floating point numbers
24# seem to be rather sloppy in Cray.
25$limit = 1e8 if $^O eq 'unicos';
26
27try 5, abs( 13e21 % 4e21 - 1e21) < $limit;
28try 6, abs(-13e21 % 4e21 - 3e21) < $limit;
29try 7, abs( 13e21 % -4e21 - -3e21) < $limit;
30try 8, abs(-13e21 % -4e21 - -1e21) < $limit;
d658dc55
GS
31
32# UVs should behave properly
33
7dca457a
NC
34tryeq 9, 4063328477 % 65535, 27407;
35tryeq 10, 4063328477 % 4063328476, 1;
36tryeq 11, 4063328477 % 2031664238, 1;
37tryeq 12, 2031664238 % 4063328477, 2031664238;
38
39# These should trigger wrapping on 32 bit IVs and UVs
40
41tryeq 13, 2147483647 + 0, 2147483647;
42
43# IV + IV promote to UV
44tryeq 14, 2147483647 + 1, 2147483648;
45tryeq 15, 2147483640 + 10, 2147483650;
46tryeq 16, 2147483647 + 2147483647, 4294967294;
47# IV + UV promote to NV
48tryeq 17, 2147483647 + 2147483649, 4294967296;
49# UV + IV promote to NV
50tryeq 18, 4294967294 + 2, 4294967296;
51# UV + UV promote to NV
52tryeq 19, 4294967295 + 4294967295, 8589934590;
53
54# UV + IV to IV
55tryeq 20, 2147483648 + -1, 2147483647;
56tryeq 21, 2147483650 + -10, 2147483640;
57# IV + UV to IV
58tryeq 22, -1 + 2147483648, 2147483647;
59tryeq 23, -10 + 4294967294, 4294967284;
60# IV + IV to NV
61tryeq 24, -2147483648 + -2147483648, -4294967296;
62tryeq 25, -2147483640 + -10, -2147483650;
63
64# Hmm. Don't forget the simple stuff
65tryeq 26, 1 + 1, 2;
66tryeq 27, 4 + -2, 2;
67tryeq 28, -10 + 100, 90;
68tryeq 29, -7 + -9, -16;
69tryeq 30, -63 + +2, -61;
70tryeq 31, 4 + -1, 3;
71tryeq 32, -1 + 1, 0;
72tryeq 33, +29 + -29, 0;
73tryeq 34, -1 + 4, 3;
74tryeq 35, +4 + -17, -13;
75
76# subtraction
77tryeq 36, 3 - 1, 2;
78tryeq 37, 3 - 15, -12;
79tryeq 38, 3 - -7, 10;
80tryeq 39, -156 - 5, -161;
81tryeq 40, -156 - -5, -151;
82tryeq 41, -5 - -12, 7;
83tryeq 42, -3 - -3, 0;
84tryeq 43, 15 - 15, 0;
85
86tryeq 44, 2147483647 - 0, 2147483647;
87tryeq 45, 2147483648 - 0, 2147483648;
88tryeq 46, -2147483648 - 0, -2147483648;
89
90tryeq 47, 0 - -2147483647, 2147483647;
91tryeq 48, -1 - -2147483648, 2147483647;
92tryeq 49, 2 - -2147483648, 2147483650;
93
94tryeq 50, 4294967294 - 3, 4294967291;
95tryeq 51, -2147483648 - -1, -2147483647;
96
97# IV - IV promote to UV
98tryeq 52, 2147483647 - -1, 2147483648;
99tryeq 53, 2147483647 - -2147483648, 4294967295;
100# UV - IV promote to NV
101tryeq 54, 4294967294 - -3, 4294967297;
102# IV - IV promote to NV
103tryeq 55, -2147483648 - +1, -2147483649;
104# UV - UV promote to IV
105tryeq 56, 2147483648 - 2147483650, -2;
106# IV - UV promote to IV
107tryeq 57, 2000000000 - 4000000000, -2000000000;
108
109# No warnings should appear;
110my $a;
111$a += 1;
112tryeq 58, $a, 1;
113undef $a;
114$a += -1;
115tryeq 59, $a, -1;
116undef $a;
117$a += 4294967290;
118tryeq 60, $a, 4294967290;
119undef $a;
120$a += -4294967290;
121tryeq 61, $a, -4294967290;
122undef $a;
123$a += 4294967297;
124tryeq 62, $a, 4294967297;
125undef $a;
126$a += -4294967297;
127tryeq 63, $a, -4294967297;
128
129my $s;
130$s -= 1;
131tryeq 64, $s, -1;
132undef $s;
133$s -= -1;
134tryeq 65, $s, +1;
135undef $s;
136$s -= -4294967290;
137tryeq 66, $s, +4294967290;
138undef $s;
139$s -= 4294967290;
140tryeq 67, $s, -4294967290;
141undef $s;
142$s -= 4294967297;
143tryeq 68, $s, -4294967297;
144undef $s;
145$s -= -4294967297;
146tryeq 69, $s, +4294967297;
147
148# Multiplication
149
150tryeq 70, 1 * 3, 3;
151tryeq 71, -2 * 3, -6;
152tryeq 72, 3 * -3, -9;
153tryeq 73, -4 * -3, 12;
154
155# check with 0xFFFF and 0xFFFF
156tryeq 74, 65535 * 65535, 4294836225;
157tryeq 75, 65535 * -65535, -4294836225;
158tryeq 76, -65535 * 65535, -4294836225;
159tryeq 77, -65535 * -65535, 4294836225;
160
161# check with 0xFFFF and 0x10001
162tryeq 78, 65535 * 65537, 4294967295;
163tryeq 79, 65535 * -65537, -4294967295;
164tryeq 80, -65535 * 65537, -4294967295;
165tryeq 81, -65535 * -65537, 4294967295;
166
167# check with 0x10001 and 0xFFFF
168tryeq 82, 65537 * 65535, 4294967295;
169tryeq 83, 65537 * -65535, -4294967295;
170tryeq 84, -65537 * 65535, -4294967295;
171tryeq 85, -65537 * -65535, 4294967295;
172
173# These should all be dones as NVs
174tryeq 86, 65537 * 65537, 4295098369;
175tryeq 87, 65537 * -65537, -4295098369;
176tryeq 88, -65537 * 65537, -4295098369;
177tryeq 89, -65537 * -65537, 4295098369;
178
179# will overflow an IV (in 32-bit)
180tryeq 90, 46340 * 46342, 0x80001218;
181tryeq 91, 46340 * -46342, -0x80001218;
182tryeq 92, -46340 * 46342, -0x80001218;
183tryeq 93, -46340 * -46342, 0x80001218;
184
185tryeq 94, 46342 * 46340, 0x80001218;
186tryeq 95, 46342 * -46340, -0x80001218;
187tryeq 96, -46342 * 46340, -0x80001218;
188tryeq 97, -46342 * -46340, 0x80001218;
189
190# will overflow a positive IV (in 32-bit)
191tryeq 98, 65536 * 32768, 0x80000000;
192tryeq 99, 65536 * -32768, -0x80000000;
193tryeq 100, -65536 * 32768, -0x80000000;
194tryeq 101, -65536 * -32768, 0x80000000;
195
196tryeq 102, 32768 * 65536, 0x80000000;
197tryeq 103, 32768 * -65536, -0x80000000;
198tryeq 104, -32768 * 65536, -0x80000000;
199tryeq 105, -32768 * -65536, 0x80000000;
200
201# 2147483647 is prime. bah.
202
203tryeq 106, 46339 * 46341, 0x7ffea80f;
204tryeq 107, 46339 * -46341, -0x7ffea80f;
205tryeq 108, -46339 * 46341, -0x7ffea80f;
206tryeq 109, -46339 * -46341, 0x7ffea80f;