2003-09-26 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / basic.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_return () {
33                 return 0;
34         }
35
36         static int test_100000_return_large () {
37                 return 100000;
38         }
39
40         static int test_1_load_bool () {
41                 bool a = true;
42                 return a? 1: 0;
43         }
44
45         static int test_0_load_bool_false () {
46                 bool a = false;
47                 return a? 1: 0;
48         }
49
50         static int test_200_load_byte () {
51                 byte a = 200;
52                 return a;
53         }
54
55         static int test_100_load_sbyte () {
56                 sbyte a = 100;
57                 return a;
58         }
59
60         static int test_200_load_short () {
61                 short a = 200;
62                 return a;
63         }
64
65         static int test_100_load_ushort () {
66                 ushort a = 100;
67                 return a;
68         }
69
70         static int test_3_add_simple () {
71                 int a = 1; 
72                 int b = 2;
73                 return a + b;
74         }
75
76         static int test_3_add_imm () {
77                 int a = 1; 
78                 return a + 2;
79         }
80
81         static int test_13407573_add_largeimm () {
82                 int a = 1; 
83                 return a + 13407572;
84         }
85
86         static int test_1_sub_simple () {
87                 int a = 1; 
88                 int b = 2;
89                 return b - a;
90         }
91
92         static int test_1_sub_simple_un () {
93                 uint a = 1; 
94                 uint b = 2;
95                 return (int)(b - a);
96         }
97
98         static int test_1_sub_imm () {
99                 int b = 2;
100                 return b - 1;
101         }
102
103         static int test_0_sub_inv_imm () {
104                 int b = 2;
105                 return 2 - b;
106         }
107
108         static int test_2_and () {
109                 int b = 2;
110                 int a = 3;
111                 return b & a;
112         }
113
114         static int test_0_and_imm () {
115                 int b = 2;
116                 return b & 0x10;
117         }
118
119         static int test_0_and_large_imm () {
120                 int b = 2;
121                 return b & 0x10000000;
122         }
123
124         static int test_2_div () {
125                 int b = 6;
126                 int a = 3;
127                 return b / a;
128         }
129
130         static int test_4_div_imm () {
131                 int b = 12;
132                 return b / 3;
133         }
134
135         static int test_4_divun_imm () {
136                 uint b = 12;
137                 return (int)(b / 3);
138         }
139
140         static int test_0_div_fold () {
141                 int b = -1;
142                 return b / 2;
143         }
144
145         static int test_1_remun_imm () {
146                 uint b = 13;
147                 return (int)(b % 3);
148         }
149
150         static int test_2_bigremun_imm () {
151                 uint b = (uint)-2;
152                 return (int)(b % 3);
153         }
154
155         static int test_2_rem () {
156                 int b = 5;
157                 int a = 3;
158                 return b % a;
159         }
160
161         static int test_4_rem_imm () {
162                 int b = 12;
163                 return b % 8;
164         }
165
166         static int test_9_mul () {
167                 int b = 3;
168                 int a = 3;
169                 return b * a;
170         }
171
172         static int test_15_mul_imm () {
173                 int b = 3;
174                 return b * 5;
175         }
176
177         static int test_24_mul () {
178                 int a = 3;
179                 int b = 8;
180                 int res;
181
182                 res = a * b;
183                 
184                 return res;
185         }
186
187         static int test_24_mul_ovf () {
188                 int a = 3;
189                 int b = 8;
190                 int res;
191
192                 checked {
193                         res = a * b;
194                 }
195                 
196                 return res;
197         }
198
199         static int test_24_mul_un () {
200                 uint a = 3;
201                 uint b = 8;
202                 uint res;
203
204                 res = a * b;
205                 
206                 return (int)res;
207         }
208
209         static int test_24_mul_ovf_un () {
210                 uint a = 3;
211                 uint b = 8;
212                 uint res;
213
214                 checked {
215                         res = a * b;
216                 }
217                 
218                 return (int)res;
219         }
220
221         static int test_3_or () {
222                 int b = 2;
223                 int a = 3;
224                 return b | a;
225         }
226
227         static int test_3_or_un () {
228                 uint b = 2;
229                 uint a = 3;
230                 return (int)(b | a);
231         }
232
233         static int test_3_or_short_un () {
234                 ushort b = 2;
235                 ushort a = 3;
236                 return (int)(b | a);
237         }
238
239         static int test_18_or_imm () {
240                 int b = 2;
241                 return b | 0x10;
242         }
243
244         static int test_268435458_or_large_imm () {
245                 int b = 2;
246                 return b | 0x10000000;
247         }
248
249         static int test_1_neg () {
250                 int b = -2;
251                 b++;
252                 return -b;
253         }
254
255         static int test_2_not () {
256                 int b = ~2;
257                 b = ~b;
258                 return b;
259         }
260
261         static int test_16_shift () {
262                 int b = 2;
263                 int a = 3;
264                 return b << a;
265         }
266         
267         static int test_16_shift_add () {
268                 int b = 2;
269                 int a = 3;
270                 int c = 0;
271                 return b << (a + c);
272         }
273         
274         static int test_16_shift_add2 () {
275                 int b = 2;
276                 int a = 3;
277                 int c = 0;
278                 return (b + c) << a;
279         }
280         
281         static int test_16_shift_imm () {
282                 int b = 2;
283                 return b << 3;
284         }
285         
286         static int test_12_shift_imm_inv () {
287                 int b = 2;
288                 return 3 << 2;
289         }
290
291         static int test_12_shift_imm_inv_sbyte () {
292                 sbyte b = 2;
293                 return 3 << 2;
294         }
295
296         static int test_1_rshift_imm () {
297                 int b = 8;
298                 return b >> 3;
299         }
300         
301         static int test_2_unrshift_imm () {
302                 uint b = 16;
303                 return (int)(b >> 3);
304         }
305         
306         static int test_0_bigunrshift_imm () {
307                 uint b = (uint)-1;
308                 b = b >> 1;
309                 if (b != 0x7fffffff)
310                         return 1;
311                 return 0;
312         }
313         
314         static int test_0_bigrshift_imm () {
315                 int b = -1;
316                 b = b >> 1;
317                 if (b != -1)
318                         return 1;
319                 return 0;
320         }
321         
322         static int test_1_rshift () {
323                 int b = 8;
324                 int a = 3;
325                 return b >> a;
326         }
327         
328         static int test_2_unrshift () {
329                 uint b = 16;
330                 int a = 3;
331                 return (int)(b >> a);
332         }
333         
334         static int test_0_bigunrshift () {
335                 uint b = (uint)-1;
336                 int a = 1;
337                 b = b >> a;
338                 if (b != 0x7fffffff)
339                         return 1;
340                 return 0;
341         }
342         
343         static int test_0_bigrshift () {
344                 int b = -1;
345                 int a = 1;
346                 b = b >> a;
347                 if (b != -1)
348                         return 1;
349                 return 0;
350         }
351         
352         static int test_2_cond () {
353                 int b = 2, a = 3, c;
354                 if (a == b)
355                         return 0;
356                 return 2;
357         }
358         
359         static int test_2_cond_short () {
360                 short b = 2, a = 3, c;
361                 if (a == b)
362                         return 0;
363                 return 2;
364         }
365         
366         static int test_2_cond_sbyte () {
367                 sbyte b = 2, a = 3, c;
368                 if (a == b)
369                         return 0;
370                 return 2;
371         }
372         
373         static int test_6_cascade_cond () {
374                 int b = 2, a = 3, c;
375                 if (a == b)
376                         return 0;
377                 else if (b > a)
378                         return 1;
379                 else if (b != b)
380                         return 2;
381                 else {
382                         c = 1;
383                 }
384                 return a + b + c;
385         }
386         
387         static int test_6_cascade_short () {
388                 short b = 2, a = 3, c;
389                 if (a == b)
390                         return 0;
391                 else if (b > a)
392                         return 1;
393                 else if (b != b)
394                         return 2;
395                 else {
396                         c = 1;
397                 }
398                 return a + b + c;
399         }
400         
401         static int test_15_for_loop () {
402                 int i;
403                 for (i = 0; i < 15; ++i) {
404                 }
405                 return i;
406         }
407         
408         static int test_11_nested_for_loop () {
409                 int i, j = 0; /* mcs bug here if j not set */
410                 for (i = 0; i < 15; ++i) {
411                         for (j = 200; j >= 5; --j) ;
412                 }
413                 return i - j;
414         }
415
416         static int test_11_several_nested_for_loops () {
417                 int i, j = 0; /* mcs bug here if j not set */
418                 for (i = 0; i < 15; ++i) {
419                         for (j = 200; j >= 5; --j) ;
420                 }
421                 i = j = 0;
422                 for (i = 0; i < 15; ++i) {
423                         for (j = 200; j >= 5; --j) ;
424                 }
425                 return i - j;
426         }
427
428         static int test_0_conv_ovf_i1 () {
429                 int c;
430
431                 //for (int j = 0; j < 10000000; j++)
432                 checked {
433                         c = 127;
434                         sbyte b = (sbyte)c;
435                         c = -128;
436                         b = (sbyte)c;
437                 }
438                 
439                 return 0;
440         }
441         
442         static int test_0_conv_ovf_i1_un () {
443                 uint c;
444
445                 checked {
446                         c = 127;
447                         sbyte b = (sbyte)c;
448                 }
449                 
450                 return 0;
451         }
452         
453         static int test_0_conv_ovf_i2 () {
454                 int c;
455
456                 checked {
457                         c = 32767;
458                         Int16 b = (Int16)c;
459                         c = -32768;
460                         b = (Int16)c;
461                 }
462                 
463                 return 0;
464         }
465         
466         static int test_0_conv_ovf_i2_un () {
467                 uint c;
468
469                 checked {
470                         c = 32767;
471                         Int16 b = (Int16)c;
472                 }
473                 
474                 return 0;
475         }
476         
477         static int test_0_conv_ovf_u2 () {
478                 int c;
479
480                 checked {
481                         c = 65535;
482                         UInt16 b = (UInt16)c;
483                 }
484                 
485                 return 0;
486         }
487         
488         static int test_0_conv_ovf_u2_un () {
489                 uint c;
490
491                 checked {
492                         c = 65535;
493                         UInt16 b = (UInt16)c;
494                 }
495                 
496                 return 0;
497         }
498         
499         static int test_0_conv_ovf_u4 () {
500                 int c;
501
502                 checked {
503                         c = 0x7fffffff;
504                         uint b = (uint)c;
505                 }
506                 
507                 return 0;
508         }
509         
510         static int test_0_bool () {
511                 bool val = true;
512                 if (val)
513                         return 0;
514                 return 1;
515         }
516         
517         static int test_1_bool_inverted () {
518                 bool val = true;
519                 if (!val)
520                         return 0;
521                 return 1;
522         }
523
524         static int test_1_bool_assign () {
525                 bool val = true;
526                 val = !val; // this should produce a ceq
527                 if (val)
528                         return 0;
529                 return 1;
530         }
531
532         static int test_1_bool_multi () {
533                 bool val = true;
534                 bool val2 = true;
535                 val = !val;
536                 if ((val && !val2) && (!val2 && val))
537                         return 0;
538                 return 1;
539         }
540
541         static int test_16_spill () {
542                 int a = 1;
543                 int b = 2;
544                 int c = 3;
545                 int d = 4;
546                 int e = 5;
547
548                 return (1 + (a + (b + (c + (d + e)))));
549         }
550
551         static int test_1_switch () {
552                 int n = 0;
553
554                 switch (n) {
555                 case 0: return 1;
556                 case 1: return 2;
557                 case -1: return 3;
558                 default:
559                         return 4;
560                 }
561                 return 1;
562         }
563
564
565         static int test_0_while_loop_1 () {
566
567                 int value = 255;
568                 
569                 do {
570                         value = value >> 4;
571                 } while (value != 0);
572                 
573                 return 0;
574         }
575
576         static int test_0_while_loop_2 () {
577                 int value = 255;
578                 int position = 5;
579                 
580                 do {
581                         value = value >> 4;
582                 } while (value != 0 && position > 1);
583         
584                 return 0;
585         }
586
587         static int test_0_char_conv () {
588                 int i = 1;
589                 
590                 char tc = (char) ('0' + i);
591
592                 if (tc != '1')
593                         return 1;
594                 
595                 return 0;
596         }
597
598         static int test_3_shift_regalloc () {
599                 int shift = 8;
600                 int orig = 1;
601                 byte value = 0xfe;
602
603                 orig &= ~(0xff << shift);
604                 orig |= value << shift;
605
606                 if (orig == 0xfe01)
607                         return 3;
608                 return 0;
609         }
610
611         enum E {A, B};
612         
613         static int test_2_optimize_branches () {
614                 switch (E.A) {
615                 case E.A:
616                         if (E.A == E.B) {
617                         }
618                         break;
619                 }
620                 return 2;
621         }
622
623         static int test_0_checked_byte_cast () {
624                 int v = 250;
625                 int b = checked ((byte) (v));
626
627                 if (b != 250)
628                         return 1;
629                 return 0;
630         }
631
632         static int test_0_checked_byte_cast_un () {
633                 uint v = 250;
634                 uint b = checked ((byte) (v));
635
636                 if (b != 250)
637                         return 1;
638                 return 0;
639         }
640
641         static int test_0_checked_short_cast () {
642                 int v = 250;
643                 int b = checked ((ushort) (v));
644
645                 if (b != 250)
646                         return 1;
647                 return 0;
648         }
649
650         static int test_0_checked_short_cast_un () {
651                 uint v = 250;
652                 uint b = checked ((ushort) (v));
653
654                 if (b != 250)
655                         return 1;
656                 return 0;
657         }
658
659 }