merge from trunk revisions 58933, 58935, 58936
[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         public static int test_0_rounding () {
196                 long ticks = 631502475130080000L;
197                 long ticksperday = 864000000000L;
198
199                 double days = (double) ticks / ticksperday;
200
201                 if ((int)days != 730905)
202                         return 1;
203
204                 return 0;
205         }
206
207         /* FIXME: This only works on little-endian machines */
208         /*
209         static unsafe int test_2_negative_zero () {
210                 int result = 0;
211                 double d = -0.0;
212                 float f = -0.0f;
213
214                 byte *ptr = (byte*)&d;
215                 if (ptr [7] == 0)
216                         return result;
217                 result ++;
218
219                 ptr = (byte*)&f;
220                 if (ptr [3] == 0)
221                         return result;
222                 result ++;
223
224                 return result;
225         }
226         */
227
228         static int test_16_float_cmp () {
229                 double a = 2.0;
230                 double b = 1.0;
231                 int result = 0;
232                 bool val;
233                 
234                 val = a == a;
235                 if (!val)
236                         return result;
237                 result++;
238
239                 val = (a != a);
240                 if (val)
241                         return result;
242                 result++;
243
244                 val = a < a;
245                 if (val)
246                         return result;
247                 result++;
248
249                 val = a > a;
250                 if (val)
251                         return result;
252                 result++;
253
254                 val = a <= a;
255                 if (!val)
256                         return result;
257                 result++;
258
259                 val = a >= a;
260                 if (!val)
261                         return result;
262                 result++;
263
264                 val = b == a;
265                 if (val)
266                         return result;
267                 result++;
268
269                 val = b < a;
270                 if (!val)
271                         return result;
272                 result++;
273
274                 val = b > a;
275                 if (val)
276                         return result;
277                 result++;
278
279                 val = b <= a;
280                 if (!val)
281                         return result;
282                 result++;
283
284                 val = b >= a;
285                 if (val)
286                         return result;
287                 result++;
288
289                 val = a == b;
290                 if (val)
291                         return result;
292                 result++;
293
294                 val = a < b;
295                 if (val)
296                         return result;
297                 result++;
298
299                 val = a > b;
300                 if (!val)
301                         return result;
302                 result++;
303
304                 val = a <= b;
305                 if (val)
306                         return result;
307                 result++;
308
309                 val = a >= b;
310                 if (!val)
311                         return result;
312                 result++;
313
314                 return result;
315         }
316
317         static int test_15_float_cmp_un () {
318                 double a = Double.NaN;
319                 double b = 1.0;
320                 int result = 0;
321                 bool val;
322                 
323                 val = a == a;
324                 if (val)
325                         return result;
326                 result++;
327
328                 val = a < a;
329                 if (val)
330                         return result;
331                 result++;
332
333                 val = a > a;
334                 if (val)
335                         return result;
336                 result++;
337
338                 val = a <= a;
339                 if (val)
340                         return result;
341                 result++;
342
343                 val = a >= a;
344                 if (val)
345                         return result;
346                 result++;
347
348                 val = b == a;
349                 if (val)
350                         return result;
351                 result++;
352
353                 val = b < a;
354                 if (val)
355                         return result;
356                 result++;
357
358                 val = b > a;
359                 if (val)
360                         return result;
361                 result++;
362
363                 val = b <= a;
364                 if (val)
365                         return result;
366                 result++;
367
368                 val = b >= a;
369                 if (val)
370                         return result;
371                 result++;
372
373                 val = a == b;
374                 if (val)
375                         return result;
376                 result++;
377
378                 val = a < b;
379                 if (val)
380                         return result;
381                 result++;
382
383                 val = a > b;
384                 if (val)
385                         return result;
386                 result++;
387
388                 val = a <= b;
389                 if (val)
390                         return result;
391                 result++;
392
393                 val = a >= b;
394                 if (val)
395                         return result;
396                 result++;
397
398                 return result;
399         }
400
401         static int test_15_float_branch () {
402                 double a = 2.0;
403                 double b = 1.0;
404                 int result = 0;
405                 
406                 if (!(a == a))
407                         return result;
408                 result++;
409
410                 if (a < a)
411                         return result;
412                 result++;
413
414                 if (a > a)
415                         return result;
416                 result++;
417
418                 if (!(a <= a))
419                         return result;
420                 result++;
421
422                 if (!(a >= a))
423                         return result;
424                 result++;
425
426                 if (b == a)
427                         return result;
428                 result++;
429
430                 if (!(b < a))
431                         return result;
432                 result++;
433
434                 if (b > a)
435                         return result;
436                 result++;
437
438                 if (!(b <= a))
439                         return result;
440                 result++;
441
442                 if (b >= a)
443                         return result;
444                 result++;
445
446                 if (a == b)
447                         return result;
448                 result++;
449
450                 if (a < b)
451                         return result;
452                 result++;
453
454                 if (!(a > b))
455                         return result;
456                 result++;
457
458                 if (a <= b)
459                         return result;
460                 result++;
461
462                 if (!(a >= b))
463                         return result;
464                 result++;
465
466                 return result;
467         }
468
469         static int test_15_float_branch_un () {
470                 double a = Double.NaN;
471                 double b = 1.0;
472                 int result = 0;
473                 
474                 if (a == a)
475                         return result;
476                 result++;
477
478                 if (a < a)
479                         return result;
480                 result++;
481
482                 if (a > a)
483                         return result;
484                 result++;
485
486                 if (a <= a)
487                         return result;
488                 result++;
489
490                 if (a >= a)
491                         return result;
492                 result++;
493
494                 if (b == a)
495                         return result;
496                 result++;
497
498                 if (b < a)
499                         return result;
500                 result++;
501
502                 if (b > a)
503                         return result;
504                 result++;
505
506                 if (b <= a)
507                         return result;
508                 result++;
509
510                 if (b >= a)
511                         return result;
512                 result++;
513
514                 if (a == b)
515                         return result;
516                 result++;
517
518                 if (a < b)
519                         return result;
520                 result++;
521
522                 if (a > b)
523                         return result;
524                 result++;
525
526                 if (a <= b)
527                         return result;
528                 result++;
529
530                 if (a >= b)
531                         return result;
532                 result++;
533
534                 return result;
535         }
536
537         static int test_0_float_precision () {
538                 float f1 = 3.40282346638528859E+38f;
539                 float f2 = 3.40282346638528859E+38f;            
540                 float PositiveInfinity =  1.0f / 0.0f;
541                 float f = f1 + f2;
542
543                 return f == PositiveInfinity ? 0 : 1;
544         }
545 }
546