2007-12-20 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_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_rounding () {
219                 long ticks = 631502475130080000L;
220                 long ticksperday = 864000000000L;
221
222                 double days = (double) ticks / ticksperday;
223
224                 if ((int)days != 730905)
225                         return 1;
226
227                 return 0;
228         }
229
230         /* FIXME: This only works on little-endian machines */
231         /*
232         static unsafe int test_2_negative_zero () {
233                 int result = 0;
234                 double d = -0.0;
235                 float f = -0.0f;
236
237                 byte *ptr = (byte*)&d;
238                 if (ptr [7] == 0)
239                         return result;
240                 result ++;
241
242                 ptr = (byte*)&f;
243                 if (ptr [3] == 0)
244                         return result;
245                 result ++;
246
247                 return result;
248         }
249         */
250
251         static int test_16_float_cmp () {
252                 double a = 2.0;
253                 double b = 1.0;
254                 int result = 0;
255                 bool val;
256                 
257                 val = a == a;
258                 if (!val)
259                         return result;
260                 result++;
261
262                 val = (a != a);
263                 if (val)
264                         return result;
265                 result++;
266
267                 val = a < a;
268                 if (val)
269                         return result;
270                 result++;
271
272                 val = a > a;
273                 if (val)
274                         return result;
275                 result++;
276
277                 val = a <= a;
278                 if (!val)
279                         return result;
280                 result++;
281
282                 val = a >= a;
283                 if (!val)
284                         return result;
285                 result++;
286
287                 val = b == a;
288                 if (val)
289                         return result;
290                 result++;
291
292                 val = b < a;
293                 if (!val)
294                         return result;
295                 result++;
296
297                 val = b > a;
298                 if (val)
299                         return result;
300                 result++;
301
302                 val = b <= a;
303                 if (!val)
304                         return result;
305                 result++;
306
307                 val = b >= a;
308                 if (val)
309                         return result;
310                 result++;
311
312                 val = a == b;
313                 if (val)
314                         return result;
315                 result++;
316
317                 val = a < b;
318                 if (val)
319                         return result;
320                 result++;
321
322                 val = a > b;
323                 if (!val)
324                         return result;
325                 result++;
326
327                 val = a <= b;
328                 if (val)
329                         return result;
330                 result++;
331
332                 val = a >= b;
333                 if (!val)
334                         return result;
335                 result++;
336
337                 return result;
338         }
339
340         static int test_15_float_cmp_un () {
341                 double a = Double.NaN;
342                 double b = 1.0;
343                 int result = 0;
344                 bool val;
345                 
346                 val = a == a;
347                 if (val)
348                         return result;
349                 result++;
350
351                 val = a < a;
352                 if (val)
353                         return result;
354                 result++;
355
356                 val = a > a;
357                 if (val)
358                         return result;
359                 result++;
360
361                 val = a <= a;
362                 if (val)
363                         return result;
364                 result++;
365
366                 val = a >= 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 = b < a;
377                 if (val)
378                         return result;
379                 result++;
380
381                 val = b > a;
382                 if (val)
383                         return result;
384                 result++;
385
386                 val = b <= a;
387                 if (val)
388                         return result;
389                 result++;
390
391                 val = b >= a;
392                 if (val)
393                         return result;
394                 result++;
395
396                 val = a == b;
397                 if (val)
398                         return result;
399                 result++;
400
401                 val = a < b;
402                 if (val)
403                         return result;
404                 result++;
405
406                 val = a > b;
407                 if (val)
408                         return result;
409                 result++;
410
411                 val = a <= b;
412                 if (val)
413                         return result;
414                 result++;
415
416                 val = a >= b;
417                 if (val)
418                         return result;
419                 result++;
420
421                 return result;
422         }
423
424         static int test_15_float_branch () {
425                 double a = 2.0;
426                 double b = 1.0;
427                 int result = 0;
428                 
429                 if (!(a == a))
430                         return result;
431                 result++;
432
433                 if (a < a)
434                         return result;
435                 result++;
436
437                 if (a > a)
438                         return result;
439                 result++;
440
441                 if (!(a <= a))
442                         return result;
443                 result++;
444
445                 if (!(a >= a))
446                         return result;
447                 result++;
448
449                 if (b == a)
450                         return result;
451                 result++;
452
453                 if (!(b < a))
454                         return result;
455                 result++;
456
457                 if (b > a)
458                         return result;
459                 result++;
460
461                 if (!(b <= a))
462                         return result;
463                 result++;
464
465                 if (b >= a)
466                         return result;
467                 result++;
468
469                 if (a == b)
470                         return result;
471                 result++;
472
473                 if (a < b)
474                         return result;
475                 result++;
476
477                 if (!(a > b))
478                         return result;
479                 result++;
480
481                 if (a <= b)
482                         return result;
483                 result++;
484
485                 if (!(a >= b))
486                         return result;
487                 result++;
488
489                 return result;
490         }
491
492         static int test_15_float_branch_un () {
493                 double a = Double.NaN;
494                 double b = 1.0;
495                 int result = 0;
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 (a >= 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 (b >= a)
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                 if (a >= b)
554                         return result;
555                 result++;
556
557                 return result;
558         }
559
560         static int test_0_float_precision () {
561                 float f1 = 3.40282346638528859E+38f;
562                 float f2 = 3.40282346638528859E+38f;            
563                 float PositiveInfinity =  1.0f / 0.0f;
564                 float f = f1 + f2;
565
566                 return f == PositiveInfinity ? 0 : 1;
567         }
568 }
569