5ebd3b04cd254ed510eb5fa9324b1402c547bff4
[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  * 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 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31         
32         static int test_0_beq () {
33                 double a = 2.0;
34                 if (a != 2.0)
35                         return 1;
36                 return 0;
37         }
38
39         static int test_0_bne_un () {
40                 double a = 2.0;
41                 if (a == 1.0)
42                         return 1;
43                 return 0;
44         }
45
46         static int test_0_conv_r8 () {
47                 double a = 2;
48                 if (a != 2.0)
49                         return 1;
50                 return 0;
51         }
52
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;
74         }
75
76         static int test_5_conv_r4 () {
77                 int i = 5;
78                 float f = (float)i;
79                 return (int)f;
80         }
81
82         static int test_5_double_conv_r4 () {
83                 double d = 5.0;
84                 float f = (float)d;
85                 return (int)f;
86         }
87
88         static int test_5_float_conv_r8 () {
89                 float f = 5.0F;
90                 double d = (double)f;
91                 return (int)d;
92         }
93
94         static int test_5_conv_r8 () {
95                 int i = 5;
96                 double f = (double)i;
97                 return (int)f;
98         }
99
100         static int test_5_add () {
101                 double a = 2.0;
102                 double b = 3.0;         
103                 return (int)(a + b);
104         }
105
106         static int test_5_sub () {
107                 double a = 8.0;
108                 double b = 3.0;         
109                 return (int)(a - b);
110         }       
111
112         static int test_24_mul () {
113                 double a = 8.0;
114                 double b = 3.0;         
115                 return (int)(a * b);
116         }       
117
118         static int test_4_div () {
119                 double a = 8.0;
120                 double b = 2.0;         
121                 return (int)(a / b);
122         }       
123
124         static int test_2_rem () {
125                 double a = 8.0;
126                 double b = 3.0;         
127                 return (int)(a % b);
128         }       
129
130         static int test_2_neg () {
131                 double a = -2.0;                
132                 return (int)(-a);
133         }
134         
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;
146
147                 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
148         }
149
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;
161
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)))))))));
164         }
165
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;
177
178                 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
179         }
180
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;
193         }
194
195         static int test_4_ulong_cast () {
196                 ulong a = 1000;
197                 double d = (double)a;
198                 ulong b = (ulong)d;
199                 if (b != 1000)
200                         return 0;
201                 return 4;
202         }
203
204         static int test_4_single_long_cast () {
205                 long a = 1000;
206                 float d = (float)a;
207                 long b = (long)d;
208                 if (b != 1000)
209                         return 0;
210                 a = -1;
211                 d = (float)a;
212                 b = (long)d;
213                 if (b != -1)
214                         return 1;
215                 return 4;
216         }
217
218         public static int test_0_lconv_to_r8 () {
219                 long a = 150;
220                 double b = (double) a;
221
222                 if (b != 150.0)
223                         return 1;
224                 return 0;
225         }
226
227         public static int test_0_lconv_to_r4 () {
228                 long a = 3000;
229                 float b = (float) a;
230
231                 if (b != 3000.0F)
232                         return 1;
233                 return 0;
234         }
235
236         static void doit (double value, out long m) {
237                 m = (long) value;
238         }
239
240         public static int test_0_ftol_clobber () {
241                 long m;
242                 doit (1.3, out m);
243                 if (m != 1)
244                         return 2;
245                 return 0;
246         }
247
248         public static int test_0_rounding () {
249                 long ticks = 631502475130080000L;
250                 long ticksperday = 864000000000L;
251
252                 double days = (double) ticks / ticksperday;
253
254                 if ((int)days != 730905)
255                         return 1;
256
257                 return 0;
258         }
259
260         /* FIXME: This only works on little-endian machines */
261         /*
262         static unsafe int test_2_negative_zero () {
263                 int result = 0;
264                 double d = -0.0;
265                 float f = -0.0f;
266
267                 byte *ptr = (byte*)&d;
268                 if (ptr [7] == 0)
269                         return result;
270                 result ++;
271
272                 ptr = (byte*)&f;
273                 if (ptr [3] == 0)
274                         return result;
275                 result ++;
276
277                 return result;
278         }
279         */
280
281         static int test_16_float_cmp () {
282                 double a = 2.0;
283                 double b = 1.0;
284                 int result = 0;
285                 bool val;
286                 
287                 val = a == a;
288                 if (!val)
289                         return result;
290                 result++;
291
292                 val = (a != a);
293                 if (val)
294                         return result;
295                 result++;
296
297                 val = a < a;
298                 if (val)
299                         return result;
300                 result++;
301
302                 val = a > a;
303                 if (val)
304                         return result;
305                 result++;
306
307                 val = a <= a;
308                 if (!val)
309                         return result;
310                 result++;
311
312                 val = a >= a;
313                 if (!val)
314                         return result;
315                 result++;
316
317                 val = b == a;
318                 if (val)
319                         return result;
320                 result++;
321
322                 val = b < a;
323                 if (!val)
324                         return result;
325                 result++;
326
327                 val = b > a;
328                 if (val)
329                         return result;
330                 result++;
331
332                 val = b <= a;
333                 if (!val)
334                         return result;
335                 result++;
336
337                 val = b >= a;
338                 if (val)
339                         return result;
340                 result++;
341
342                 val = a == b;
343                 if (val)
344                         return result;
345                 result++;
346
347                 val = a < b;
348                 if (val)
349                         return result;
350                 result++;
351
352                 val = a > b;
353                 if (!val)
354                         return result;
355                 result++;
356
357                 val = a <= b;
358                 if (val)
359                         return result;
360                 result++;
361
362                 val = a >= b;
363                 if (!val)
364                         return result;
365                 result++;
366
367                 return result;
368         }
369
370         static int test_15_float_cmp_un () {
371                 double a = Double.NaN;
372                 double b = 1.0;
373                 int result = 0;
374                 bool val;
375                 
376                 val = a == a;
377                 if (val)
378                         return result;
379                 result++;
380
381                 val = a < a;
382                 if (val)
383                         return result;
384                 result++;
385
386                 val = a > a;
387                 if (val)
388                         return result;
389                 result++;
390
391                 val = a <= a;
392                 if (val)
393                         return result;
394                 result++;
395
396                 val = a >= a;
397                 if (val)
398                         return result;
399                 result++;
400
401                 val = b == a;
402                 if (val)
403                         return result;
404                 result++;
405
406                 val = b < a;
407                 if (val)
408                         return result;
409                 result++;
410
411                 val = b > a;
412                 if (val)
413                         return result;
414                 result++;
415
416                 val = b <= a;
417                 if (val)
418                         return result;
419                 result++;
420
421                 val = b >= a;
422                 if (val)
423                         return result;
424                 result++;
425
426                 val = a == b;
427                 if (val)
428                         return result;
429                 result++;
430
431                 val = a < b;
432                 if (val)
433                         return result;
434                 result++;
435
436                 val = a > b;
437                 if (val)
438                         return result;
439                 result++;
440
441                 val = a <= b;
442                 if (val)
443                         return result;
444                 result++;
445
446                 val = a >= b;
447                 if (val)
448                         return result;
449                 result++;
450
451                 return result;
452         }
453
454         static int test_15_float_branch () {
455                 double a = 2.0;
456                 double b = 1.0;
457                 int result = 0;
458                 
459                 if (!(a == a))
460                         return result;
461                 result++;
462
463                 if (a < a)
464                         return result;
465                 result++;
466
467                 if (a > a)
468                         return result;
469                 result++;
470
471                 if (!(a <= a))
472                         return result;
473                 result++;
474
475                 if (!(a >= a))
476                         return result;
477                 result++;
478
479                 if (b == a)
480                         return result;
481                 result++;
482
483                 if (!(b < a))
484                         return result;
485                 result++;
486
487                 if (b > a)
488                         return result;
489                 result++;
490
491                 if (!(b <= a))
492                         return result;
493                 result++;
494
495                 if (b >= a)
496                         return result;
497                 result++;
498
499                 if (a == b)
500                         return result;
501                 result++;
502
503                 if (a < b)
504                         return result;
505                 result++;
506
507                 if (!(a > b))
508                         return result;
509                 result++;
510
511                 if (a <= b)
512                         return result;
513                 result++;
514
515                 if (!(a >= b))
516                         return result;
517                 result++;
518
519                 return result;
520         }
521
522         static int test_15_float_branch_un () {
523                 double a = Double.NaN;
524                 double b = 1.0;
525                 int result = 0;
526                 
527                 if (a == a)
528                         return result;
529                 result++;
530
531                 if (a < a)
532                         return result;
533                 result++;
534
535                 if (a > a)
536                         return result;
537                 result++;
538
539                 if (a <= a)
540                         return result;
541                 result++;
542
543                 if (a >= a)
544                         return result;
545                 result++;
546
547                 if (b == a)
548                         return result;
549                 result++;
550
551                 if (b < a)
552                         return result;
553                 result++;
554
555                 if (b > a)
556                         return result;
557                 result++;
558
559                 if (b <= a)
560                         return result;
561                 result++;
562
563                 if (b >= a)
564                         return result;
565                 result++;
566
567                 if (a == b)
568                         return result;
569                 result++;
570
571                 if (a < b)
572                         return result;
573                 result++;
574
575                 if (a > b)
576                         return result;
577                 result++;
578
579                 if (a <= b)
580                         return result;
581                 result++;
582
583                 if (a >= b)
584                         return result;
585                 result++;
586
587                 return result;
588         }
589
590         static int test_0_float_precision () {
591                 float f1 = 3.40282346638528859E+38f;
592                 float f2 = 3.40282346638528859E+38f;            
593                 float PositiveInfinity =  1.0f / 0.0f;
594                 float f = f1 + f2;
595
596                 return f == PositiveInfinity ? 0 : 1;
597         }
598
599         static double VALUE = 0.19975845134874831D;
600
601         static int test_0_float_conversion_reduces_double_precision () {
602                 double d = (float)VALUE;
603                 if (d != 0.19975845515727997d)
604                         return 1;
605
606                 return 0;
607         }
608
609 }
610