Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mono / mini / basic-float.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * public static int test_<result>_<name> ();
10  *
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.
14  *
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)
20  *
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.
24  */
25
26 /* A comparison made to same variable. */
27 #pragma warning disable 1718
28
29 #if MOBILE
30 class FloatTests
31 #else
32 class Tests
33 #endif
34 {
35
36 #if !MOBILE
37         public static int Main (string[] args) {
38                 return TestDriver.RunTests (typeof (Tests), args);
39         }
40 #endif
41         
42         public static int test_0_beq () {
43                 double a = 2.0;
44                 if (a != 2.0)
45                         return 1;
46                 return 0;
47         }
48
49         public static int test_0_bne_un () {
50                 double a = 2.0;
51                 if (a == 1.0)
52                         return 1;
53                 return 0;
54         }
55
56         public static int test_0_conv_r8 () {
57                 double a = 2;
58                 if (a != 2.0)
59                         return 1;
60                 return 0;
61         }
62
63         public static int test_0_conv_i () {
64                 double a = 2.0;
65                 int i = (int)a;
66                 if (i != 2)
67                         return 1;
68                 uint ui = (uint)a;
69                 if (ui != 2)
70                         return 2;
71                 short s = (short)a;
72                 if (s != 2)
73                         return 3;
74                 ushort us = (ushort)a;
75                 if (us != 2)
76                         return 4;
77                 byte b = (byte)a;
78                 if (b != 2)
79                         return 5;
80                 sbyte sb = (sbyte)a;
81                 if (sb != 2)
82                         return 6;
83                 /* MS.NET special cases these */
84                 double d = Double.NaN;
85                 ui = (uint)d;
86                 if (ui != 0)
87                         return 7;
88                 d = Double.PositiveInfinity;
89                 ui = (uint)d;
90                 if (ui != 0)
91                         return 8;
92                 d = Double.NegativeInfinity;
93                 ui = (uint)d;
94                 if (ui != 0)
95                         return 9;
96
97                 return 0;
98         }
99
100         public static int test_5_conv_r4 () {
101                 int i = 5;
102                 float f = (float)i;
103                 return (int)f;
104         }
105
106         public static int test_0_conv_r4_m1 () {
107                 int i = -1;
108                 float f = (float)i;
109                 return (int)f + 1;
110         }
111
112         public static int test_5_double_conv_r4 () {
113                 double d = 5.0;
114                 float f = (float)d;
115                 return (int)f;
116         }
117
118         public static int test_5_float_conv_r8 () {
119                 float f = 5.0F;
120                 double d = (double)f;
121                 return (int)d;
122         }
123
124         public static int test_5_conv_r8 () {
125                 int i = 5;
126                 double f = (double)i;
127                 return (int)f;
128         }
129
130         public static int test_5_add () {
131                 double a = 2.0;
132                 double b = 3.0;         
133                 return (int)(a + b);
134         }
135
136         public static int test_5_sub () {
137                 double a = 8.0;
138                 double b = 3.0;         
139                 return (int)(a - b);
140         }       
141
142         public static int test_24_mul () {
143                 double a = 8.0;
144                 double b = 3.0;         
145                 return (int)(a * b);
146         }       
147
148         public static int test_4_div () {
149                 double a = 8.0;
150                 double b = 2.0;         
151                 return (int)(a / b);
152         }       
153
154         public static int test_2_rem () {
155                 double a = 8.0;
156                 double b = 3.0;         
157                 return (int)(a % b);
158         }       
159
160         public static int test_2_neg () {
161                 double a = -2.0;                
162                 return (int)(-a);
163         }
164         
165         public static int test_46_float_add_spill () {
166                 // we overflow the FP stack
167                 double a = 1;
168                 double b = 2;
169                 double c = 3;
170                 double d = 4;
171                 double e = 5;
172                 double f = 6;
173                 double g = 7;
174                 double h = 8;
175                 double i = 9;
176
177                 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
178         }
179
180         public static int test_4_float_sub_spill () {
181                 // we overflow the FP stack
182                 double a = 1;
183                 double b = 2;
184                 double c = 3;
185                 double d = 4;
186                 double e = 5;
187                 double f = 6;
188                 double g = 7;
189                 double h = 8;
190                 double i = 9;
191
192                 return -(int)(1.0 - (a - (b - (c - (d - (e - (f - (g - (h - i)))))))));
193                 ////// -(int)(1.0 - (1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - 9)))))))));
194         }
195
196         public static int test_362880_float_mul_spill () {
197                 // we overflow the FP stack
198                 double a = 1;
199                 double b = 2;
200                 double c = 3;
201                 double d = 4;
202                 double e = 5;
203                 double f = 6;
204                 double g = 7;
205                 double h = 8;
206                 double i = 9;
207
208                 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
209         }
210
211         public static int test_4_long_cast () {
212                 long a = 1000;
213                 double d = (double)a;
214                 long b = (long)d;
215                 if (b != 1000)
216                         return 0;
217                 a = -1;
218                 d = (double)a;
219                 b = (long)d;
220                 if (b != -1)
221                         return 1;
222                 return 4;
223         }
224
225         public static int test_4_ulong_cast () {
226                 ulong a = 1000;
227                 double d = (double)a;
228                 ulong b = (ulong)d;
229                 if (b != 1000)
230                         return 0;
231                 a = 0xffffffffffffffff;
232                 float f = (float)a;
233                 if (!(f > 0f))
234                         return 1;
235                 return 4;
236         }
237
238         public static int test_4_single_long_cast () {
239                 long a = 1000;
240                 float d = (float)a;
241                 long b = (long)d;
242                 if (b != 1000)
243                         return 0;
244                 a = -1;
245                 d = (float)a;
246                 b = (long)d;
247                 if (b != -1)
248                         return 1;
249                 return 4;
250         }
251
252         public static int test_0_lconv_to_r8 () {
253                 long a = 150;
254                 double b = (double) a;
255
256                 if (b != 150.0)
257                         return 1;
258                 return 0;
259         }
260
261         public static int test_0_lconv_to_r4 () {
262                 long a = 3000;
263                 float b = (float) a;
264
265                 if (b != 3000.0F)
266                         return 1;
267                 return 0;
268         }
269
270         static void doit (double value, out long m) {
271                 m = (long) value;
272         }
273
274         public static int test_0_ftol_clobber () {
275                 long m;
276                 doit (1.3, out m);
277                 if (m != 1)
278                         return 2;
279                 return 0;
280         }
281
282         public static int test_0_rounding () {
283                 long ticks = 631502475130080000L;
284                 long ticksperday = 864000000000L;
285
286                 double days = (double) ticks / ticksperday;
287
288                 if ((int)days != 730905)
289                         return 1;
290
291                 return 0;
292         }
293
294         /* FIXME: This only works on little-endian machines */
295         /*
296         static unsafe int test_2_negative_zero () {
297                 int result = 0;
298                 double d = -0.0;
299                 float f = -0.0f;
300
301                 byte *ptr = (byte*)&d;
302                 if (ptr [7] == 0)
303                         return result;
304                 result ++;
305
306                 ptr = (byte*)&f;
307                 if (ptr [3] == 0)
308                         return result;
309                 result ++;
310
311                 return result;
312         }
313         */
314
315         public static int test_16_float_cmp () {
316                 double a = 2.0;
317                 double b = 1.0;
318                 int result = 0;
319                 bool val;
320                 
321                 val = a == a;
322                 if (!val)
323                         return result;
324                 result++;
325
326                 val = (a != a);
327                 if (val)
328                         return result;
329                 result++;
330
331                 val = a < a;
332                 if (val)
333                         return result;
334                 result++;
335
336                 val = a > a;
337                 if (val)
338                         return result;
339                 result++;
340
341                 val = a <= a;
342                 if (!val)
343                         return result;
344                 result++;
345
346                 val = a >= a;
347                 if (!val)
348                         return result;
349                 result++;
350
351                 val = b == a;
352                 if (val)
353                         return result;
354                 result++;
355
356                 val = b < a;
357                 if (!val)
358                         return result;
359                 result++;
360
361                 val = b > a;
362                 if (val)
363                         return result;
364                 result++;
365
366                 val = b <= a;
367                 if (!val)
368                         return result;
369                 result++;
370
371                 val = b >= a;
372                 if (val)
373                         return result;
374                 result++;
375
376                 val = a == b;
377                 if (val)
378                         return result;
379                 result++;
380
381                 val = a < b;
382                 if (val)
383                         return result;
384                 result++;
385
386                 val = a > b;
387                 if (!val)
388                         return result;
389                 result++;
390
391                 val = a <= b;
392                 if (val)
393                         return result;
394                 result++;
395
396                 val = a >= b;
397                 if (!val)
398                         return result;
399                 result++;
400
401                 return result;
402         }
403
404         public static int test_15_float_cmp_un () {
405                 double a = Double.NaN;
406                 double b = 1.0;
407                 int result = 0;
408                 bool val;
409                 
410                 val = a == a;
411                 if (val)
412                         return result;
413                 result++;
414
415                 val = a < a;
416                 if (val)
417                         return result;
418                 result++;
419
420                 val = a > a;
421                 if (val)
422                         return result;
423                 result++;
424
425                 val = a <= a;
426                 if (val)
427                         return result;
428                 result++;
429
430                 val = a >= a;
431                 if (val)
432                         return result;
433                 result++;
434
435                 val = b == a;
436                 if (val)
437                         return result;
438                 result++;
439
440                 val = b < a;
441                 if (val)
442                         return result;
443                 result++;
444
445                 val = b > a;
446                 if (val)
447                         return result;
448                 result++;
449
450                 val = b <= a;
451                 if (val)
452                         return result;
453                 result++;
454
455                 val = b >= a;
456                 if (val)
457                         return result;
458                 result++;
459
460                 val = a == b;
461                 if (val)
462                         return result;
463                 result++;
464
465                 val = a < b;
466                 if (val)
467                         return result;
468                 result++;
469
470                 val = a > b;
471                 if (val)
472                         return result;
473                 result++;
474
475                 val = a <= b;
476                 if (val)
477                         return result;
478                 result++;
479
480                 val = a >= b;
481                 if (val)
482                         return result;
483                 result++;
484
485                 return result;
486         }
487
488         public static int test_15_float_branch () {
489                 double a = 2.0;
490                 double b = 1.0;
491                 int result = 0;
492                 
493                 if (!(a == a))
494                         return result;
495                 result++;
496
497                 if (a < a)
498                         return result;
499                 result++;
500
501                 if (a > a)
502                         return result;
503                 result++;
504
505                 if (!(a <= a))
506                         return result;
507                 result++;
508
509                 if (!(a >= a))
510                         return result;
511                 result++;
512
513                 if (b == a)
514                         return result;
515                 result++;
516
517                 if (!(b < a))
518                         return result;
519                 result++;
520
521                 if (b > a)
522                         return result;
523                 result++;
524
525                 if (!(b <= a))
526                         return result;
527                 result++;
528
529                 if (b >= a)
530                         return result;
531                 result++;
532
533                 if (a == b)
534                         return result;
535                 result++;
536
537                 if (a < b)
538                         return result;
539                 result++;
540
541                 if (!(a > b))
542                         return result;
543                 result++;
544
545                 if (a <= b)
546                         return result;
547                 result++;
548
549                 if (!(a >= b))
550                         return result;
551                 result++;
552
553                 return result;
554         }
555
556         public static int test_15_float_branch_un () {
557                 double a = Double.NaN;
558                 double b = 1.0;
559                 int result = 0;
560                 
561                 if (a == a)
562                         return result;
563                 result++;
564
565                 if (a < a)
566                         return result;
567                 result++;
568
569                 if (a > a)
570                         return result;
571                 result++;
572
573                 if (a <= a)
574                         return result;
575                 result++;
576
577                 if (a >= a)
578                         return result;
579                 result++;
580
581                 if (b == a)
582                         return result;
583                 result++;
584
585                 if (b < a)
586                         return result;
587                 result++;
588
589                 if (b > a)
590                         return result;
591                 result++;
592
593                 if (b <= a)
594                         return result;
595                 result++;
596
597                 if (b >= a)
598                         return result;
599                 result++;
600
601                 if (a == b)
602                         return result;
603                 result++;
604
605                 if (a < b)
606                         return result;
607                 result++;
608
609                 if (a > b)
610                         return result;
611                 result++;
612
613                 if (a <= b)
614                         return result;
615                 result++;
616
617                 if (a >= b)
618                         return result;
619                 result++;
620
621                 return result;
622         }
623
624         public static int test_0_float_precision () {
625                 float f1 = 3.40282346638528859E+38f;
626                 float f2 = 3.40282346638528859E+38f;            
627                 float PositiveInfinity =  1.0f / 0.0f;
628                 float f = f1 + f2;
629
630                 return f == PositiveInfinity ? 0 : 1;
631         }
632
633         static double VALUE = 0.19975845134874831D;
634
635         public static int test_0_float_conversion_reduces_double_precision () {
636                 double d = (float)VALUE;
637                 if (d != 0.19975845515727997d)
638                         return 1;
639
640                 return 0;
641         }
642
643
644     public static int test_0_long_to_double_conversion ()
645     {
646                 long l = 9223372036854775807L;
647                 long conv = (long)((double)l);
648                 if (conv != -9223372036854775808L)
649                         return 1;
650
651                 return 0;
652     }
653
654         public static int INT_VAL = 0x13456799;
655
656         public static int test_0_int4_to_float_convertion ()
657     {
658                 double d = (double)(float)INT_VAL;
659
660                 if (d != 323315616)
661                         return 1;
662                 return 0;
663         }
664
665         public static int test_0_int8_to_float_convertion ()
666     {
667                 double d = (double)(float)(long)INT_VAL;
668
669                 if (d != 323315616)
670                         return 1;
671                 return 0;
672         }
673 }
674