2004-05-17 Radek Doulik <rodo@ximian.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                 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_362880_float_mul_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         }
161
162         static int test_4_long_cast () {
163                 long a = 1000;
164                 double d = (double)a;
165                 long b = (long)d;
166                 if (b != 1000)
167                         return 0;
168                 return 4;
169         }
170
171         /* FIXME: This only works on little-endian machines */
172         /*
173         static unsafe int test_2_negative_zero () {
174                 int result = 0;
175                 double d = -0.0;
176                 float f = -0.0f;
177
178                 byte *ptr = (byte*)&d;
179                 if (ptr [7] == 0)
180                         return result;
181                 result ++;
182
183                 ptr = (byte*)&f;
184                 if (ptr [3] == 0)
185                         return result;
186                 result ++;
187
188                 return result;
189         }
190         */
191
192         static int test_16_float_cmp () {
193                 double a = 2.0;
194                 double b = 1.0;
195                 int result = 0;
196                 bool val;
197                 
198                 val = a == a;
199                 if (!val)
200                         return result;
201                 result++;
202
203                 val = (a != a);
204                 if (val)
205                         return result;
206                 result++;
207
208                 val = a < a;
209                 if (val)
210                         return result;
211                 result++;
212
213                 val = a > a;
214                 if (val)
215                         return result;
216                 result++;
217
218                 val = a <= a;
219                 if (!val)
220                         return result;
221                 result++;
222
223                 val = a >= a;
224                 if (!val)
225                         return result;
226                 result++;
227
228                 val = b == a;
229                 if (val)
230                         return result;
231                 result++;
232
233                 val = b < a;
234                 if (!val)
235                         return result;
236                 result++;
237
238                 val = b > a;
239                 if (val)
240                         return result;
241                 result++;
242
243                 val = b <= a;
244                 if (!val)
245                         return result;
246                 result++;
247
248                 val = b >= a;
249                 if (val)
250                         return result;
251                 result++;
252
253                 val = a == b;
254                 if (val)
255                         return result;
256                 result++;
257
258                 val = a < b;
259                 if (val)
260                         return result;
261                 result++;
262
263                 val = a > b;
264                 if (!val)
265                         return result;
266                 result++;
267
268                 val = a <= b;
269                 if (val)
270                         return result;
271                 result++;
272
273                 val = a >= b;
274                 if (!val)
275                         return result;
276                 result++;
277
278                 return result;
279         }
280
281         static int test_15_float_cmp_un () {
282                 double a = Double.NaN;
283                 double b = 1.0;
284                 int result = 0;
285                 bool val;
286                 
287                 val = a == a;
288                 if (val)
289                         return result;
290                 result++;
291
292                 val = a < a;
293                 if (val)
294                         return result;
295                 result++;
296
297                 val = a > a;
298                 if (val)
299                         return result;
300                 result++;
301
302                 val = a <= a;
303                 if (val)
304                         return result;
305                 result++;
306
307                 val = a >= a;
308                 if (val)
309                         return result;
310                 result++;
311
312                 val = b == a;
313                 if (val)
314                         return result;
315                 result++;
316
317                 val = b < a;
318                 if (val)
319                         return result;
320                 result++;
321
322                 val = b > a;
323                 if (val)
324                         return result;
325                 result++;
326
327                 val = b <= a;
328                 if (val)
329                         return result;
330                 result++;
331
332                 val = b >= a;
333                 if (val)
334                         return result;
335                 result++;
336
337                 val = a == b;
338                 if (val)
339                         return result;
340                 result++;
341
342                 val = a < b;
343                 if (val)
344                         return result;
345                 result++;
346
347                 val = a > b;
348                 if (val)
349                         return result;
350                 result++;
351
352                 val = a <= b;
353                 if (val)
354                         return result;
355                 result++;
356
357                 val = a >= b;
358                 if (val)
359                         return result;
360                 result++;
361
362                 return result;
363         }
364
365         static int test_15_float_branch () {
366                 double a = 2.0;
367                 double b = 1.0;
368                 int result = 0;
369                 
370                 if (!(a == a))
371                         return result;
372                 result++;
373
374                 if (a < a)
375                         return result;
376                 result++;
377
378                 if (a > a)
379                         return result;
380                 result++;
381
382                 if (!(a <= a))
383                         return result;
384                 result++;
385
386                 if (!(a >= a))
387                         return result;
388                 result++;
389
390                 if (b == a)
391                         return result;
392                 result++;
393
394                 if (!(b < a))
395                         return result;
396                 result++;
397
398                 if (b > a)
399                         return result;
400                 result++;
401
402                 if (!(b <= a))
403                         return result;
404                 result++;
405
406                 if (b >= a)
407                         return result;
408                 result++;
409
410                 if (a == b)
411                         return result;
412                 result++;
413
414                 if (a < b)
415                         return result;
416                 result++;
417
418                 if (!(a > b))
419                         return result;
420                 result++;
421
422                 if (a <= b)
423                         return result;
424                 result++;
425
426                 if (!(a >= b))
427                         return result;
428                 result++;
429
430                 return result;
431         }
432
433         static int test_15_float_branch_un () {
434                 double a = Double.NaN;
435                 double b = 1.0;
436                 int result = 0;
437                 
438                 if (a == a)
439                         return result;
440                 result++;
441
442                 if (a < a)
443                         return result;
444                 result++;
445
446                 if (a > a)
447                         return result;
448                 result++;
449
450                 if (a <= a)
451                         return result;
452                 result++;
453
454                 if (a >= a)
455                         return result;
456                 result++;
457
458                 if (b == a)
459                         return result;
460                 result++;
461
462                 if (b < a)
463                         return result;
464                 result++;
465
466                 if (b > a)
467                         return result;
468                 result++;
469
470                 if (b <= a)
471                         return result;
472                 result++;
473
474                 if (b >= a)
475                         return result;
476                 result++;
477
478                 if (a == b)
479                         return result;
480                 result++;
481
482                 if (a < b)
483                         return result;
484                 result++;
485
486                 if (a > b)
487                         return result;
488                 result++;
489
490                 if (a <= b)
491                         return result;
492                 result++;
493
494                 if (a >= b)
495                         return result;
496                 result++;
497
498                 return result;
499         }
500
501 }
502