2010-05-11 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / basic-float.cs
blob96f1ef5f317996fc5b84a5e91c57331616bd99fb
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 * public 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 /* A comparison made to same variable. */
27 #pragma warning disable 1718
29 class Tests {
31 public static int Main () {
32 return TestDriver.RunTests (typeof (Tests));
35 public static int test_0_beq () {
36 double a = 2.0;
37 if (a != 2.0)
38 return 1;
39 return 0;
42 public static int test_0_bne_un () {
43 double a = 2.0;
44 if (a == 1.0)
45 return 1;
46 return 0;
49 public static int test_0_conv_r8 () {
50 double a = 2;
51 if (a != 2.0)
52 return 1;
53 return 0;
56 public static int test_0_conv_i () {
57 double a = 2.0;
58 int i = (int)a;
59 if (i != 2)
60 return 1;
61 uint ui = (uint)a;
62 if (ui != 2)
63 return 2;
64 short s = (short)a;
65 if (s != 2)
66 return 3;
67 ushort us = (ushort)a;
68 if (us != 2)
69 return 4;
70 byte b = (byte)a;
71 if (b != 2)
72 return 5;
73 sbyte sb = (sbyte)a;
74 if (sb != 2)
75 return 6;
76 return 0;
79 public static int test_5_conv_r4 () {
80 int i = 5;
81 float f = (float)i;
82 return (int)f;
85 public static int test_0_conv_r4_m1 () {
86 int i = -1;
87 float f = (float)i;
88 return (int)f + 1;
91 public static int test_5_double_conv_r4 () {
92 double d = 5.0;
93 float f = (float)d;
94 return (int)f;
97 public static int test_5_float_conv_r8 () {
98 float f = 5.0F;
99 double d = (double)f;
100 return (int)d;
103 public static int test_5_conv_r8 () {
104 int i = 5;
105 double f = (double)i;
106 return (int)f;
109 public static int test_5_add () {
110 double a = 2.0;
111 double b = 3.0;
112 return (int)(a + b);
115 public static int test_5_sub () {
116 double a = 8.0;
117 double b = 3.0;
118 return (int)(a - b);
121 public static int test_24_mul () {
122 double a = 8.0;
123 double b = 3.0;
124 return (int)(a * b);
127 public static int test_4_div () {
128 double a = 8.0;
129 double b = 2.0;
130 return (int)(a / b);
133 public static int test_2_rem () {
134 double a = 8.0;
135 double b = 3.0;
136 return (int)(a % b);
139 public static int test_2_neg () {
140 double a = -2.0;
141 return (int)(-a);
144 public static int test_46_float_add_spill () {
145 // we overflow the FP stack
146 double a = 1;
147 double b = 2;
148 double c = 3;
149 double d = 4;
150 double e = 5;
151 double f = 6;
152 double g = 7;
153 double h = 8;
154 double i = 9;
156 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
159 public static int test_4_float_sub_spill () {
160 // we overflow the FP stack
161 double a = 1;
162 double b = 2;
163 double c = 3;
164 double d = 4;
165 double e = 5;
166 double f = 6;
167 double g = 7;
168 double h = 8;
169 double i = 9;
171 return -(int)(1.0 - (a - (b - (c - (d - (e - (f - (g - (h - i)))))))));
172 ////// -(int)(1.0 - (1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - 9)))))))));
175 public static int test_362880_float_mul_spill () {
176 // we overflow the FP stack
177 double a = 1;
178 double b = 2;
179 double c = 3;
180 double d = 4;
181 double e = 5;
182 double f = 6;
183 double g = 7;
184 double h = 8;
185 double i = 9;
187 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
190 public static int test_4_long_cast () {
191 long a = 1000;
192 double d = (double)a;
193 long b = (long)d;
194 if (b != 1000)
195 return 0;
196 a = -1;
197 d = (double)a;
198 b = (long)d;
199 if (b != -1)
200 return 1;
201 return 4;
204 public static int test_4_ulong_cast () {
205 ulong a = 1000;
206 double d = (double)a;
207 ulong b = (ulong)d;
208 if (b != 1000)
209 return 0;
210 return 4;
213 public static int test_4_single_long_cast () {
214 long a = 1000;
215 float d = (float)a;
216 long b = (long)d;
217 if (b != 1000)
218 return 0;
219 a = -1;
220 d = (float)a;
221 b = (long)d;
222 if (b != -1)
223 return 1;
224 return 4;
227 public static int test_0_lconv_to_r8 () {
228 long a = 150;
229 double b = (double) a;
231 if (b != 150.0)
232 return 1;
233 return 0;
236 public static int test_0_lconv_to_r4 () {
237 long a = 3000;
238 float b = (float) a;
240 if (b != 3000.0F)
241 return 1;
242 return 0;
245 static void doit (double value, out long m) {
246 m = (long) value;
249 public static int test_0_ftol_clobber () {
250 long m;
251 doit (1.3, out m);
252 if (m != 1)
253 return 2;
254 return 0;
257 public static int test_0_rounding () {
258 long ticks = 631502475130080000L;
259 long ticksperday = 864000000000L;
261 double days = (double) ticks / ticksperday;
263 if ((int)days != 730905)
264 return 1;
266 return 0;
269 /* FIXME: This only works on little-endian machines */
271 static unsafe int test_2_negative_zero () {
272 int result = 0;
273 double d = -0.0;
274 float f = -0.0f;
276 byte *ptr = (byte*)&d;
277 if (ptr [7] == 0)
278 return result;
279 result ++;
281 ptr = (byte*)&f;
282 if (ptr [3] == 0)
283 return result;
284 result ++;
286 return result;
290 public static int test_16_float_cmp () {
291 double a = 2.0;
292 double b = 1.0;
293 int result = 0;
294 bool val;
296 val = a == a;
297 if (!val)
298 return result;
299 result++;
301 val = (a != a);
302 if (val)
303 return result;
304 result++;
306 val = a < a;
307 if (val)
308 return result;
309 result++;
311 val = a > a;
312 if (val)
313 return result;
314 result++;
316 val = a <= a;
317 if (!val)
318 return result;
319 result++;
321 val = a >= a;
322 if (!val)
323 return result;
324 result++;
326 val = b == a;
327 if (val)
328 return result;
329 result++;
331 val = b < a;
332 if (!val)
333 return result;
334 result++;
336 val = b > a;
337 if (val)
338 return result;
339 result++;
341 val = b <= a;
342 if (!val)
343 return result;
344 result++;
346 val = b >= a;
347 if (val)
348 return result;
349 result++;
351 val = a == b;
352 if (val)
353 return result;
354 result++;
356 val = a < b;
357 if (val)
358 return result;
359 result++;
361 val = a > b;
362 if (!val)
363 return result;
364 result++;
366 val = a <= b;
367 if (val)
368 return result;
369 result++;
371 val = a >= b;
372 if (!val)
373 return result;
374 result++;
376 return result;
379 public static int test_15_float_cmp_un () {
380 double a = Double.NaN;
381 double b = 1.0;
382 int result = 0;
383 bool val;
385 val = a == a;
386 if (val)
387 return result;
388 result++;
390 val = a < a;
391 if (val)
392 return result;
393 result++;
395 val = a > a;
396 if (val)
397 return result;
398 result++;
400 val = a <= a;
401 if (val)
402 return result;
403 result++;
405 val = a >= a;
406 if (val)
407 return result;
408 result++;
410 val = b == a;
411 if (val)
412 return result;
413 result++;
415 val = b < a;
416 if (val)
417 return result;
418 result++;
420 val = b > a;
421 if (val)
422 return result;
423 result++;
425 val = b <= a;
426 if (val)
427 return result;
428 result++;
430 val = b >= a;
431 if (val)
432 return result;
433 result++;
435 val = a == b;
436 if (val)
437 return result;
438 result++;
440 val = a < b;
441 if (val)
442 return result;
443 result++;
445 val = a > b;
446 if (val)
447 return result;
448 result++;
450 val = a <= b;
451 if (val)
452 return result;
453 result++;
455 val = a >= b;
456 if (val)
457 return result;
458 result++;
460 return result;
463 public static int test_15_float_branch () {
464 double a = 2.0;
465 double b = 1.0;
466 int result = 0;
468 if (!(a == a))
469 return result;
470 result++;
472 if (a < a)
473 return result;
474 result++;
476 if (a > a)
477 return result;
478 result++;
480 if (!(a <= a))
481 return result;
482 result++;
484 if (!(a >= a))
485 return result;
486 result++;
488 if (b == a)
489 return result;
490 result++;
492 if (!(b < a))
493 return result;
494 result++;
496 if (b > a)
497 return result;
498 result++;
500 if (!(b <= a))
501 return result;
502 result++;
504 if (b >= a)
505 return result;
506 result++;
508 if (a == b)
509 return result;
510 result++;
512 if (a < b)
513 return result;
514 result++;
516 if (!(a > b))
517 return result;
518 result++;
520 if (a <= b)
521 return result;
522 result++;
524 if (!(a >= b))
525 return result;
526 result++;
528 return result;
531 public static int test_15_float_branch_un () {
532 double a = Double.NaN;
533 double b = 1.0;
534 int result = 0;
536 if (a == a)
537 return result;
538 result++;
540 if (a < a)
541 return result;
542 result++;
544 if (a > a)
545 return result;
546 result++;
548 if (a <= a)
549 return result;
550 result++;
552 if (a >= a)
553 return result;
554 result++;
556 if (b == a)
557 return result;
558 result++;
560 if (b < a)
561 return result;
562 result++;
564 if (b > a)
565 return result;
566 result++;
568 if (b <= a)
569 return result;
570 result++;
572 if (b >= a)
573 return result;
574 result++;
576 if (a == b)
577 return result;
578 result++;
580 if (a < b)
581 return result;
582 result++;
584 if (a > b)
585 return result;
586 result++;
588 if (a <= b)
589 return result;
590 result++;
592 if (a >= b)
593 return result;
594 result++;
596 return result;
599 public static int test_0_float_precision () {
600 float f1 = 3.40282346638528859E+38f;
601 float f2 = 3.40282346638528859E+38f;
602 float PositiveInfinity = 1.0f / 0.0f;
603 float f = f1 + f2;
605 return f == PositiveInfinity ? 0 : 1;
609 Disabled until they can be fixed to run on amd64
611 static double VALUE = 0.19975845134874831D;
613 public static int test_0_float_conversion_reduces_double_precision () {
614 double d = (float)VALUE;
615 if (d != 0.19975845515727997d)
616 return 1;
618 return 0;
622 public static int test_0_long_to_double_conversion ()
624 long l = 9223372036854775807L;
625 long conv = (long)((double)l);
626 if (conv != -9223372036854775808L)
627 return 1;
629 return 0;
632 public static int INT_VAL = 0x13456799;
634 public static int test_0_int4_to_float_convertion ()
636 double d = (double)(float)INT_VAL;
638 if (d != 323315616)
639 return 1;
640 return 0;