2007-03-28 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mono / mini / basic-float.cs
blobfb731f687789ef1547c1504cd5221dba3eab6fde
1 using System;
2 using System.Reflection;
4 /*
5 * Regression tests for the mono JIT.
7 * Each test needs to be of the form:
9 * static int test_<result>_<name> ();
11 * where <result> is an integer (the value that needs to be returned by
12 * the method to make it pass.
13 * <name> is a user-displayed name used to identify the test.
15 * The tests can be driven in two ways:
16 * *) running the program directly: Main() uses reflection to find and invoke
17 * the test methods (this is useful mostly to check that the tests are correct)
18 * *) with the --regression switch of the jit (this is the preferred way since
19 * all the tests will be run with optimizations on and off)
21 * The reflection logic could be moved to a .dll since we need at least another
22 * regression test file written in IL code to have better control on how
23 * the IL code looks.
26 class Tests {
28 static int Main () {
29 return TestDriver.RunTests (typeof (Tests));
32 static int test_0_beq () {
33 double a = 2.0;
34 if (a != 2.0)
35 return 1;
36 return 0;
39 static int test_0_bne_un () {
40 double a = 2.0;
41 if (a == 1.0)
42 return 1;
43 return 0;
46 static int test_0_conv_r8 () {
47 double a = 2;
48 if (a != 2.0)
49 return 1;
50 return 0;
53 static int test_0_conv_i () {
54 double a = 2.0;
55 int i = (int)a;
56 if (i != 2)
57 return 1;
58 uint ui = (uint)a;
59 if (ui != 2)
60 return 2;
61 short s = (short)a;
62 if (s != 2)
63 return 3;
64 ushort us = (ushort)a;
65 if (us != 2)
66 return 4;
67 byte b = (byte)a;
68 if (b != 2)
69 return 5;
70 sbyte sb = (sbyte)a;
71 if (sb != 2)
72 return 6;
73 return 0;
76 static int test_5_conv_r4 () {
77 int i = 5;
78 float f = (float)i;
79 return (int)f;
82 static int test_5_double_conv_r4 () {
83 double d = 5.0;
84 float f = (float)d;
85 return (int)f;
88 static int test_5_float_conv_r8 () {
89 float f = 5.0F;
90 double d = (double)f;
91 return (int)d;
94 static int test_5_conv_r8 () {
95 int i = 5;
96 double f = (double)i;
97 return (int)f;
100 static int test_5_add () {
101 double a = 2.0;
102 double b = 3.0;
103 return (int)(a + b);
106 static int test_5_sub () {
107 double a = 8.0;
108 double b = 3.0;
109 return (int)(a - b);
112 static int test_24_mul () {
113 double a = 8.0;
114 double b = 3.0;
115 return (int)(a * b);
118 static int test_4_div () {
119 double a = 8.0;
120 double b = 2.0;
121 return (int)(a / b);
124 static int test_2_rem () {
125 double a = 8.0;
126 double b = 3.0;
127 return (int)(a % b);
130 static int test_2_neg () {
131 double a = -2.0;
132 return (int)(-a);
135 static int test_46_float_add_spill () {
136 // we overflow the FP stack
137 double a = 1;
138 double b = 2;
139 double c = 3;
140 double d = 4;
141 double e = 5;
142 double f = 6;
143 double g = 7;
144 double h = 8;
145 double i = 9;
147 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
150 static int test_4_float_sub_spill () {
151 // we overflow the FP stack
152 double a = 1;
153 double b = 2;
154 double c = 3;
155 double d = 4;
156 double e = 5;
157 double f = 6;
158 double g = 7;
159 double h = 8;
160 double i = 9;
162 return -(int)(1.0 - (a - (b - (c - (d - (e - (f - (g - (h - i)))))))));
163 ////// -(int)(1.0 - (1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - 9)))))))));
166 static int test_362880_float_mul_spill () {
167 // we overflow the FP stack
168 double a = 1;
169 double b = 2;
170 double c = 3;
171 double d = 4;
172 double e = 5;
173 double f = 6;
174 double g = 7;
175 double h = 8;
176 double i = 9;
178 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
181 static int test_4_long_cast () {
182 long a = 1000;
183 double d = (double)a;
184 long b = (long)d;
185 if (b != 1000)
186 return 0;
187 a = -1;
188 d = (double)a;
189 b = (long)d;
190 if (b != -1)
191 return 1;
192 return 4;
195 static int test_4_single_long_cast () {
196 long a = 1000;
197 float d = (float)a;
198 long b = (long)d;
199 if (b != 1000)
200 return 0;
201 a = -1;
202 d = (float)a;
203 b = (long)d;
204 if (b != -1)
205 return 1;
206 return 4;
209 public static int test_0_rounding () {
210 long ticks = 631502475130080000L;
211 long ticksperday = 864000000000L;
213 double days = (double) ticks / ticksperday;
215 if ((int)days != 730905)
216 return 1;
218 return 0;
221 /* FIXME: This only works on little-endian machines */
223 static unsafe int test_2_negative_zero () {
224 int result = 0;
225 double d = -0.0;
226 float f = -0.0f;
228 byte *ptr = (byte*)&d;
229 if (ptr [7] == 0)
230 return result;
231 result ++;
233 ptr = (byte*)&f;
234 if (ptr [3] == 0)
235 return result;
236 result ++;
238 return result;
242 static int test_16_float_cmp () {
243 double a = 2.0;
244 double b = 1.0;
245 int result = 0;
246 bool val;
248 val = a == a;
249 if (!val)
250 return result;
251 result++;
253 val = (a != a);
254 if (val)
255 return result;
256 result++;
258 val = a < a;
259 if (val)
260 return result;
261 result++;
263 val = a > a;
264 if (val)
265 return result;
266 result++;
268 val = a <= a;
269 if (!val)
270 return result;
271 result++;
273 val = a >= a;
274 if (!val)
275 return result;
276 result++;
278 val = b == a;
279 if (val)
280 return result;
281 result++;
283 val = b < a;
284 if (!val)
285 return result;
286 result++;
288 val = b > a;
289 if (val)
290 return result;
291 result++;
293 val = b <= a;
294 if (!val)
295 return result;
296 result++;
298 val = b >= a;
299 if (val)
300 return result;
301 result++;
303 val = a == b;
304 if (val)
305 return result;
306 result++;
308 val = a < b;
309 if (val)
310 return result;
311 result++;
313 val = a > b;
314 if (!val)
315 return result;
316 result++;
318 val = a <= b;
319 if (val)
320 return result;
321 result++;
323 val = a >= b;
324 if (!val)
325 return result;
326 result++;
328 return result;
331 static int test_15_float_cmp_un () {
332 double a = Double.NaN;
333 double b = 1.0;
334 int result = 0;
335 bool val;
337 val = a == a;
338 if (val)
339 return result;
340 result++;
342 val = a < a;
343 if (val)
344 return result;
345 result++;
347 val = a > a;
348 if (val)
349 return result;
350 result++;
352 val = a <= a;
353 if (val)
354 return result;
355 result++;
357 val = a >= a;
358 if (val)
359 return result;
360 result++;
362 val = b == a;
363 if (val)
364 return result;
365 result++;
367 val = b < a;
368 if (val)
369 return result;
370 result++;
372 val = b > a;
373 if (val)
374 return result;
375 result++;
377 val = b <= a;
378 if (val)
379 return result;
380 result++;
382 val = b >= a;
383 if (val)
384 return result;
385 result++;
387 val = a == b;
388 if (val)
389 return result;
390 result++;
392 val = a < b;
393 if (val)
394 return result;
395 result++;
397 val = a > b;
398 if (val)
399 return result;
400 result++;
402 val = a <= b;
403 if (val)
404 return result;
405 result++;
407 val = a >= b;
408 if (val)
409 return result;
410 result++;
412 return result;
415 static int test_15_float_branch () {
416 double a = 2.0;
417 double b = 1.0;
418 int result = 0;
420 if (!(a == a))
421 return result;
422 result++;
424 if (a < a)
425 return result;
426 result++;
428 if (a > a)
429 return result;
430 result++;
432 if (!(a <= a))
433 return result;
434 result++;
436 if (!(a >= a))
437 return result;
438 result++;
440 if (b == a)
441 return result;
442 result++;
444 if (!(b < a))
445 return result;
446 result++;
448 if (b > a)
449 return result;
450 result++;
452 if (!(b <= a))
453 return result;
454 result++;
456 if (b >= a)
457 return result;
458 result++;
460 if (a == b)
461 return result;
462 result++;
464 if (a < b)
465 return result;
466 result++;
468 if (!(a > b))
469 return result;
470 result++;
472 if (a <= b)
473 return result;
474 result++;
476 if (!(a >= b))
477 return result;
478 result++;
480 return result;
483 static int test_15_float_branch_un () {
484 double a = Double.NaN;
485 double b = 1.0;
486 int result = 0;
488 if (a == a)
489 return result;
490 result++;
492 if (a < a)
493 return result;
494 result++;
496 if (a > a)
497 return result;
498 result++;
500 if (a <= a)
501 return result;
502 result++;
504 if (a >= a)
505 return result;
506 result++;
508 if (b == a)
509 return result;
510 result++;
512 if (b < a)
513 return result;
514 result++;
516 if (b > a)
517 return result;
518 result++;
520 if (b <= a)
521 return result;
522 result++;
524 if (b >= a)
525 return result;
526 result++;
528 if (a == b)
529 return result;
530 result++;
532 if (a < b)
533 return result;
534 result++;
536 if (a > b)
537 return result;
538 result++;
540 if (a <= b)
541 return result;
542 result++;
544 if (a >= b)
545 return result;
546 result++;
548 return result;
551 static int test_0_float_precision () {
552 float f1 = 3.40282346638528859E+38f;
553 float f2 = 3.40282346638528859E+38f;
554 float PositiveInfinity = 1.0f / 0.0f;
555 float f = f1 + f2;
557 return f == PositiveInfinity ? 0 : 1;