updating to the latest module.
[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                 return 0;
71         }
72
73         static int test_5_conv_r4 () {
74                 int i = 5;
75                 float f = (float)i;
76                 return (int)f;
77         }
78
79         static int test_5_double_conv_r4 () {
80                 double d = 5.0;
81                 float f = (float)d;
82                 return (int)f;
83         }
84
85         static int test_5_float_conv_r8 () {
86                 float f = 5.0F;
87                 double d = (double)f;
88                 return (int)d;
89         }
90
91         static int test_5_conv_r8 () {
92                 int i = 5;
93                 double f = (double)i;
94                 return (int)f;
95         }
96
97         static int test_5_add () {
98                 double a = 2.0;
99                 double b = 3.0;         
100                 return (int)(a + b);
101         }
102
103         static int test_5_sub () {
104                 double a = 8.0;
105                 double b = 3.0;         
106                 return (int)(a - b);
107         }       
108
109         static int test_24_mul () {
110                 double a = 8.0;
111                 double b = 3.0;         
112                 return (int)(a * b);
113         }       
114
115         static int test_4_div () {
116                 double a = 8.0;
117                 double b = 2.0;         
118                 return (int)(a / b);
119         }       
120
121         static int test_2_rem () {
122                 double a = 8.0;
123                 double b = 3.0;         
124                 return (int)(a % b);
125         }       
126
127         static int test_2_neg () {
128                 double a = -2.0;                
129                 return (int)(-a);
130         }
131         
132         static int test_46_float_add_spill () {
133                 // we overflow the FP stack
134                 double a = 1;
135                 double b = 2;
136                 double c = 3;
137                 double d = 4;
138                 double e = 5;
139                 double f = 6;
140                 double g = 7;
141                 double h = 8;
142                 double i = 9;
143
144                 return (int)(1.0 + (a + (b + (c + (d + (e + (f + (g + (h + i)))))))));
145         }
146
147         static int test_4_float_sub_spill () {
148                 // we overflow the FP stack
149                 double a = 1;
150                 double b = 2;
151                 double c = 3;
152                 double d = 4;
153                 double e = 5;
154                 double f = 6;
155                 double g = 7;
156                 double h = 8;
157                 double i = 9;
158
159                 return -(int)(1.0 - (a - (b - (c - (d - (e - (f - (g - (h - i)))))))));
160                 ////// -(int)(1.0 - (1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - 9)))))))));
161         }
162
163         static int test_362880_float_mul_spill () {
164                 // we overflow the FP stack
165                 double a = 1;
166                 double b = 2;
167                 double c = 3;
168                 double d = 4;
169                 double e = 5;
170                 double f = 6;
171                 double g = 7;
172                 double h = 8;
173                 double i = 9;
174
175                 return (int)(1.0 * (a * (b * (c * (d * (e * (f * (g * (h * i)))))))));
176         }
177
178         static int test_4_long_cast () {
179                 long a = 1000;
180                 double d = (double)a;
181                 long b = (long)d;
182                 if (b != 1000)
183                         return 0;
184                 a = -1;
185                 d = (double)a;
186                 b = (long)d;
187                 if (b != -1)
188                         return 1;
189                 return 4;
190         }
191
192         public static int test_0_rounding () {
193                 long ticks = 631502475130080000L;
194                 long ticksperday = 864000000000L;
195
196                 double days = (double) ticks / ticksperday;
197
198                 if ((int)days != 730905)
199                         return 1;
200
201                 return 0;
202         }
203
204         /* FIXME: This only works on little-endian machines */
205         /*
206         static unsafe int test_2_negative_zero () {
207                 int result = 0;
208                 double d = -0.0;
209                 float f = -0.0f;
210
211                 byte *ptr = (byte*)&d;
212                 if (ptr [7] == 0)
213                         return result;
214                 result ++;
215
216                 ptr = (byte*)&f;
217                 if (ptr [3] == 0)
218                         return result;
219                 result ++;
220
221                 return result;
222         }
223         */
224
225         static int test_16_float_cmp () {
226                 double a = 2.0;
227                 double b = 1.0;
228                 int result = 0;
229                 bool val;
230                 
231                 val = a == a;
232                 if (!val)
233                         return result;
234                 result++;
235
236                 val = (a != a);
237                 if (val)
238                         return result;
239                 result++;
240
241                 val = a < a;
242                 if (val)
243                         return result;
244                 result++;
245
246                 val = a > a;
247                 if (val)
248                         return result;
249                 result++;
250
251                 val = a <= a;
252                 if (!val)
253                         return result;
254                 result++;
255
256                 val = a >= a;
257                 if (!val)
258                         return result;
259                 result++;
260
261                 val = b == a;
262                 if (val)
263                         return result;
264                 result++;
265
266                 val = b < a;
267                 if (!val)
268                         return result;
269                 result++;
270
271                 val = b > a;
272                 if (val)
273                         return result;
274                 result++;
275
276                 val = b <= a;
277                 if (!val)
278                         return result;
279                 result++;
280
281                 val = b >= a;
282                 if (val)
283                         return result;
284                 result++;
285
286                 val = a == b;
287                 if (val)
288                         return result;
289                 result++;
290
291                 val = a < b;
292                 if (val)
293                         return result;
294                 result++;
295
296                 val = a > b;
297                 if (!val)
298                         return result;
299                 result++;
300
301                 val = a <= b;
302                 if (val)
303                         return result;
304                 result++;
305
306                 val = a >= b;
307                 if (!val)
308                         return result;
309                 result++;
310
311                 return result;
312         }
313
314         static int test_15_float_cmp_un () {
315                 double a = Double.NaN;
316                 double b = 1.0;
317                 int result = 0;
318                 bool val;
319                 
320                 val = a == a;
321                 if (val)
322                         return result;
323                 result++;
324
325                 val = a < a;
326                 if (val)
327                         return result;
328                 result++;
329
330                 val = a > a;
331                 if (val)
332                         return result;
333                 result++;
334
335                 val = a <= a;
336                 if (val)
337                         return result;
338                 result++;
339
340                 val = a >= a;
341                 if (val)
342                         return result;
343                 result++;
344
345                 val = b == a;
346                 if (val)
347                         return result;
348                 result++;
349
350                 val = b < a;
351                 if (val)
352                         return result;
353                 result++;
354
355                 val = b > a;
356                 if (val)
357                         return result;
358                 result++;
359
360                 val = b <= a;
361                 if (val)
362                         return result;
363                 result++;
364
365                 val = b >= a;
366                 if (val)
367                         return result;
368                 result++;
369
370                 val = a == b;
371                 if (val)
372                         return result;
373                 result++;
374
375                 val = a < b;
376                 if (val)
377                         return result;
378                 result++;
379
380                 val = a > b;
381                 if (val)
382                         return result;
383                 result++;
384
385                 val = a <= b;
386                 if (val)
387                         return result;
388                 result++;
389
390                 val = a >= b;
391                 if (val)
392                         return result;
393                 result++;
394
395                 return result;
396         }
397
398         static int test_15_float_branch () {
399                 double a = 2.0;
400                 double b = 1.0;
401                 int result = 0;
402                 
403                 if (!(a == a))
404                         return result;
405                 result++;
406
407                 if (a < a)
408                         return result;
409                 result++;
410
411                 if (a > a)
412                         return result;
413                 result++;
414
415                 if (!(a <= a))
416                         return result;
417                 result++;
418
419                 if (!(a >= a))
420                         return result;
421                 result++;
422
423                 if (b == a)
424                         return result;
425                 result++;
426
427                 if (!(b < a))
428                         return result;
429                 result++;
430
431                 if (b > a)
432                         return result;
433                 result++;
434
435                 if (!(b <= a))
436                         return result;
437                 result++;
438
439                 if (b >= a)
440                         return result;
441                 result++;
442
443                 if (a == b)
444                         return result;
445                 result++;
446
447                 if (a < b)
448                         return result;
449                 result++;
450
451                 if (!(a > b))
452                         return result;
453                 result++;
454
455                 if (a <= b)
456                         return result;
457                 result++;
458
459                 if (!(a >= b))
460                         return result;
461                 result++;
462
463                 return result;
464         }
465
466         static int test_15_float_branch_un () {
467                 double a = Double.NaN;
468                 double b = 1.0;
469                 int result = 0;
470                 
471                 if (a == a)
472                         return result;
473                 result++;
474
475                 if (a < a)
476                         return result;
477                 result++;
478
479                 if (a > a)
480                         return result;
481                 result++;
482
483                 if (a <= a)
484                         return result;
485                 result++;
486
487                 if (a >= 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 (b > a)
500                         return result;
501                 result++;
502
503                 if (b <= a)
504                         return result;
505                 result++;
506
507                 if (b >= a)
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                 if (a > b)
520                         return result;
521                 result++;
522
523                 if (a <= b)
524                         return result;
525                 result++;
526
527                 if (a >= b)
528                         return result;
529                 result++;
530
531                 return result;
532         }
533
534 }
535