This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
question about fs.t
[perl5.git] / t / op / arith.t
1 #!./perl -w
2
3 print "1..133\n";
4
5 sub try ($$) {
6    print +($_[1] ? "ok" : "not ok"), " $_[0]\n";
7 }
8 sub tryeq ($$$) {
9   if ($_[1] == $_[2]) {
10     print "ok $_[0]\n";
11   } else {
12     print "not ok $_[0] # $_[1] != $_[2]\n";
13   }
14 }
15 sub 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 }
27
28 tryeq 1,  13 %  4, 1;
29 tryeq 2, -13 %  4, 3;
30 tryeq 3,  13 % -4, -3;
31 tryeq 4, -13 % -4, -1;
32
33 my $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
39 try 5, abs( 13e21 %  4e21 -  1e21) < $limit;
40 try 6, abs(-13e21 %  4e21 -  3e21) < $limit;
41 try 7, abs( 13e21 % -4e21 - -3e21) < $limit;
42 try 8, abs(-13e21 % -4e21 - -1e21) < $limit;
43
44 # UVs should behave properly
45
46 tryeq 9, 4063328477 % 65535, 27407;
47 tryeq 10, 4063328477 % 4063328476, 1;
48 tryeq 11, 4063328477 % 2031664238, 1;
49 tryeq 12, 2031664238 % 4063328477, 2031664238;
50
51 # These should trigger wrapping on 32 bit IVs and UVs
52
53 tryeq 13, 2147483647 + 0, 2147483647;
54
55 # IV + IV promote to UV
56 tryeq 14, 2147483647 + 1, 2147483648;
57 tryeq 15, 2147483640 + 10, 2147483650;
58 tryeq 16, 2147483647 + 2147483647, 4294967294;
59 # IV + UV promote to NV
60 tryeq 17, 2147483647 + 2147483649, 4294967296;
61 # UV + IV promote to NV
62 tryeq 18, 4294967294 + 2, 4294967296;
63 # UV + UV promote to NV
64 tryeq 19, 4294967295 + 4294967295, 8589934590;
65
66 # UV + IV to IV
67 tryeq 20, 2147483648 + -1, 2147483647;
68 tryeq 21, 2147483650 + -10, 2147483640;
69 # IV + UV to IV
70 tryeq 22, -1 + 2147483648, 2147483647;
71 tryeq 23, -10 + 4294967294, 4294967284;
72 # IV + IV to NV
73 tryeq 24, -2147483648 + -2147483648, -4294967296;
74 tryeq 25, -2147483640 + -10, -2147483650;
75
76 # Hmm. Don't forget the simple stuff
77 tryeq 26, 1 + 1, 2;
78 tryeq 27, 4 + -2, 2;
79 tryeq 28, -10 + 100, 90;
80 tryeq 29, -7 + -9, -16;
81 tryeq 30, -63 + +2, -61;
82 tryeq 31, 4 + -1, 3;
83 tryeq 32, -1 + 1, 0;
84 tryeq 33, +29 + -29, 0;
85 tryeq 34, -1 + 4, 3;
86 tryeq 35, +4 + -17, -13;
87
88 # subtraction
89 tryeq 36, 3 - 1, 2;
90 tryeq 37, 3 - 15, -12;
91 tryeq 38, 3 - -7, 10;
92 tryeq 39, -156 - 5, -161;
93 tryeq 40, -156 - -5, -151;
94 tryeq 41, -5 - -12, 7;
95 tryeq 42, -3 - -3, 0;
96 tryeq 43, 15 - 15, 0;
97
98 tryeq 44, 2147483647 - 0, 2147483647;
99 tryeq 45, 2147483648 - 0, 2147483648;
100 tryeq 46, -2147483648 - 0, -2147483648;
101
102 tryeq 47, 0 - -2147483647, 2147483647;
103 tryeq 48, -1 - -2147483648, 2147483647;
104 tryeq 49, 2 - -2147483648, 2147483650;
105
106 tryeq 50, 4294967294 - 3, 4294967291;
107 tryeq 51, -2147483648 - -1, -2147483647;
108
109 # IV - IV promote to UV
110 tryeq 52, 2147483647 - -1, 2147483648;
111 tryeq 53, 2147483647 - -2147483648, 4294967295;
112 # UV - IV promote to NV
113 tryeq 54, 4294967294 - -3, 4294967297;
114 # IV - IV promote to NV
115 tryeq 55, -2147483648 - +1, -2147483649;
116 # UV - UV promote to IV
117 tryeq 56, 2147483648 - 2147483650, -2;
118 # IV - UV promote to IV
119 tryeq 57, 2000000000 - 4000000000, -2000000000;
120
121 # No warnings should appear;
122 my $a;
123 $a += 1;
124 tryeq 58, $a, 1;
125 undef $a;
126 $a += -1;
127 tryeq 59, $a, -1;
128 undef $a;
129 $a += 4294967290;
130 tryeq 60, $a, 4294967290;
131 undef $a;
132 $a += -4294967290;
133 tryeq 61, $a, -4294967290;
134 undef $a;
135 $a += 4294967297;
136 tryeq 62, $a, 4294967297;
137 undef $a;
138 $a += -4294967297;
139 tryeq 63, $a, -4294967297;
140
141 my $s;
142 $s -= 1;
143 tryeq 64, $s, -1;
144 undef $s;
145 $s -= -1;
146 tryeq 65, $s, +1;
147 undef $s;
148 $s -= -4294967290;
149 tryeq 66, $s, +4294967290;
150 undef $s;
151 $s -= 4294967290;
152 tryeq 67, $s, -4294967290;
153 undef $s;
154 $s -= 4294967297;
155 tryeq 68, $s, -4294967297;
156 undef $s;
157 $s -= -4294967297;
158 tryeq 69, $s, +4294967297;
159
160 # Multiplication
161
162 tryeq 70, 1 * 3, 3;
163 tryeq 71, -2 * 3, -6;
164 tryeq 72, 3 * -3, -9;
165 tryeq 73, -4 * -3, 12;
166
167 # check with 0xFFFF and 0xFFFF
168 tryeq 74, 65535 * 65535, 4294836225;
169 tryeq 75, 65535 * -65535, -4294836225;
170 tryeq 76, -65535 * 65535, -4294836225;
171 tryeq 77, -65535 * -65535, 4294836225;
172
173 # check with 0xFFFF and 0x10001
174 tryeq 78, 65535 * 65537, 4294967295;
175 tryeq 79, 65535 * -65537, -4294967295;
176 tryeq 80, -65535 * 65537, -4294967295;
177 tryeq 81, -65535 * -65537, 4294967295;
178
179 # check with 0x10001 and 0xFFFF
180 tryeq 82, 65537 * 65535, 4294967295;
181 tryeq 83, 65537 * -65535, -4294967295;
182 tryeq 84, -65537 * 65535, -4294967295;
183 tryeq 85, -65537 * -65535, 4294967295;
184
185 # These should all be dones as NVs
186 tryeq 86, 65537 * 65537, 4295098369;
187 tryeq 87, 65537 * -65537, -4295098369;
188 tryeq 88, -65537 * 65537, -4295098369;
189 tryeq 89, -65537 * -65537, 4295098369;
190
191 # will overflow an IV (in 32-bit)
192 tryeq 90, 46340 * 46342, 0x80001218;
193 tryeq 91, 46340 * -46342, -0x80001218;
194 tryeq 92, -46340 * 46342, -0x80001218;
195 tryeq 93, -46340 * -46342, 0x80001218;
196
197 tryeq 94, 46342 * 46340, 0x80001218;
198 tryeq 95, 46342 * -46340, -0x80001218;
199 tryeq 96, -46342 * 46340, -0x80001218;
200 tryeq 97, -46342 * -46340, 0x80001218;
201
202 # will overflow a positive IV (in 32-bit)
203 tryeq 98, 65536 * 32768, 0x80000000;
204 tryeq 99, 65536 * -32768, -0x80000000;
205 tryeq 100, -65536 * 32768, -0x80000000;
206 tryeq 101, -65536 * -32768, 0x80000000;
207
208 tryeq 102, 32768 * 65536, 0x80000000;
209 tryeq 103, 32768 * -65536, -0x80000000;
210 tryeq 104, -32768 * 65536, -0x80000000;
211 tryeq 105, -32768 * -65536, 0x80000000;
212
213 # 2147483647 is prime. bah.
214
215 tryeq 106, 46339 * 46341, 0x7ffea80f;
216 tryeq 107, 46339 * -46341, -0x7ffea80f;
217 tryeq 108, -46339 * 46341, -0x7ffea80f;
218 tryeq 109, -46339 * -46341, 0x7ffea80f;
219
220 # leading space should be ignored
221
222 tryeq 110, 1 + " 1", 2;
223 tryeq 111, 3 + " -1", 2;
224 tryeq 112, 1.2, " 1.2";
225 tryeq 113, -1.2, " -1.2";
226
227 # divide
228
229 tryeq 114, 28/14, 2;
230 tryeq 115, 28/-7, -4;
231 tryeq 116, -28/4, -7;
232 tryeq 117, -28/-2, 14;
233
234 tryeq 118, 0x80000000/1, 0x80000000;
235 tryeq 119, 0x80000000/-1, -0x80000000;
236 tryeq 120, -0x80000000/1, -0x80000000;
237 tryeq 121, -0x80000000/-1, 0x80000000;
238
239 # The example for sloppy divide, rigged to avoid the peephole optimiser.
240 tryeq 122, "20." / "5.", 4;
241
242 tryeq 123, 2.5 / 2, 1.25;
243 tryeq 124, 3.5 / -2, -1.75;
244 tryeq 125, -4.5 / 2, -2.25;
245 tryeq 126, -5.5 / -2, 2.75;
246
247 # Bluuurg if your floating point can't accurately cope with powers of 2
248 # [I suspect this is parsing string->float problems, not actual arith]
249 tryeq_sloppy 127, 18446744073709551616/1, 18446744073709551616; # Bluuurg
250 tryeq 128, 18446744073709551616/2, 9223372036854775808;
251 tryeq 129, 18446744073709551616/4294967296, 4294967296;
252 tryeq 130, 18446744073709551616/9223372036854775808, 2;
253
254 {
255   # The peephole optimiser is wrong to think that it can substitute intops
256   # in place of regular ops, because i_multiply can overflow.
257   # Bug reported by "Sisyphus" <kalinabears@hdc.com.au>
258   my $n = 1127;
259
260   my $float = ($n % 1000) * 167772160.0;
261   tryeq 131, $float, 21307064320;
262
263   # On a 32 bit machine, if the i_multiply op is used, you will probably get
264   # -167772160. It's actually undefined behaviour, so anything may happen.
265   my $int = ($n % 1000) * 167772160;
266   tryeq 132, $int, 21307064320;
267
268   my $t = time;
269   my $t1000 = time() * 1000;
270   try 133, abs($t1000 -1000 * $t) <= 2000;
271 }