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