2005-03-06 Zoltan Varga <vargaz@freemail.hu>
[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                 return 4;
185         }
186
187         public static int test_0_rounding () {
188                 long ticks = 631502475130080000L;
189                 long ticksperday = 864000000000L;
190
191                 double days = (double) ticks / ticksperday;
192
193                 if ((int)days != 730905)
194                         return 1;
195
196                 return 0;
197         }
198
199         /* FIXME: This only works on little-endian machines */
200         /*
201         static unsafe int test_2_negative_zero () {
202                 int result = 0;
203                 double d = -0.0;
204                 float f = -0.0f;
205
206                 byte *ptr = (byte*)&d;
207                 if (ptr [7] == 0)
208                         return result;
209                 result ++;
210
211                 ptr = (byte*)&f;
212                 if (ptr [3] == 0)
213                         return result;
214                 result ++;
215
216                 return result;
217         }
218         */
219
220         static int test_16_float_cmp () {
221                 double a = 2.0;
222                 double b = 1.0;
223                 int result = 0;
224                 bool val;
225                 
226                 val = a == a;
227                 if (!val)
228                         return result;
229                 result++;
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 = b == 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 = a == b;
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                 return result;
307         }
308
309         static int test_15_float_cmp_un () {
310                 double a = Double.NaN;
311                 double b = 1.0;
312                 int result = 0;
313                 bool val;
314                 
315                 val = a == a;
316                 if (val)
317                         return result;
318                 result++;
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 = b == 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 = a == b;
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                 return result;
391         }
392
393         static int test_15_float_branch () {
394                 double a = 2.0;
395                 double b = 1.0;
396                 int result = 0;
397                 
398                 if (!(a == a))
399                         return result;
400                 result++;
401
402                 if (a < a)
403                         return result;
404                 result++;
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 (b == a)
419                         return result;
420                 result++;
421
422                 if (!(b < 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 (a == b)
439                         return result;
440                 result++;
441
442                 if (a < b)
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                 return result;
459         }
460
461         static int test_15_float_branch_un () {
462                 double a = Double.NaN;
463                 double b = 1.0;
464                 int result = 0;
465                 
466                 if (a == a)
467                         return result;
468                 result++;
469
470                 if (a < a)
471                         return result;
472                 result++;
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 (b == a)
487                         return result;
488                 result++;
489
490                 if (b < 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 (a == b)
507                         return result;
508                 result++;
509
510                 if (a < b)
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                 return result;
527         }
528
529 }
530