This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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_2_sub_large_imm () {
104                 int b = 0xff0f0f;
105                 return b - 0xff0f0d;
106         }
107
108         static int test_0_sub_inv_imm () {
109                 int b = 2;
110                 return 2 - b;
111         }
112
113         static int test_2_and () {
114                 int b = 2;
115                 int a = 3;
116                 return b & a;
117         }
118
119         static int test_0_and_imm () {
120                 int b = 2;
121                 return b & 0x10;
122         }
123
124         static int test_0_and_large_imm () {
125                 int b = 2;
126                 return b & 0x10000000;
127         }
128
129         static int test_0_and_large_imm2 () {
130                 int b = 2;
131                 return b & 0x100000f0;
132         }
133
134         static int test_2_div () {
135                 int b = 6;
136                 int a = 3;
137                 return b / a;
138         }
139
140         static int test_4_div_imm () {
141                 int b = 12;
142                 return b / 3;
143         }
144
145         static int test_4_divun_imm () {
146                 uint b = 12;
147                 return (int)(b / 3);
148         }
149
150         static int test_0_div_fold () {
151                 int b = -1;
152                 return b / 2;
153         }
154
155         static int test_719177_div_destreg () {
156                 int year = 1970;
157                 return ((365* (year-1)) + ((year-1)/4));
158         }
159
160         static int test_1_remun_imm () {
161                 uint b = 13;
162                 return (int)(b % 3);
163         }
164
165         static int test_2_bigremun_imm () {
166                 unchecked {
167                         uint b = (uint)-2;
168                         return (int)(b % 3);
169                 }
170         }
171
172         static int test_2_rem () {
173                 int b = 5;
174                 int a = 3;
175                 return b % a;
176         }
177
178         static int test_4_rem_imm () {
179                 int b = 12;
180                 return b % 8;
181         }
182
183         static int test_4_rem_big_imm () {
184                 int b = 10004;
185                 return b % 10000;
186         }
187
188         static int test_9_mul () {
189                 int b = 3;
190                 int a = 3;
191                 return b * a;
192         }
193
194         static int test_15_mul_imm () {
195                 int b = 3;
196                 return b * 5;
197         }
198
199         static int test_24_mul () {
200                 int a = 3;
201                 int b = 8;
202                 int res;
203
204                 res = a * b;
205                 
206                 return res;
207         }
208
209         static int test_24_mul_ovf () {
210                 int a = 3;
211                 int b = 8;
212                 int res;
213
214                 checked {
215                         res = a * b;
216                 }
217                 
218                 return res;
219         }
220
221         static int test_24_mul_un () {
222                 uint a = 3;
223                 uint b = 8;
224                 uint res;
225
226                 res = a * b;
227                 
228                 return (int)res;
229         }
230
231         static int test_24_mul_ovf_un () {
232                 uint a = 3;
233                 uint b = 8;
234                 uint res;
235
236                 checked {
237                         res = a * b;
238                 }
239                 
240                 return (int)res;
241         }
242
243         static int test_3_or () {
244                 int b = 2;
245                 int a = 3;
246                 return b | a;
247         }
248
249         static int test_3_or_un () {
250                 uint b = 2;
251                 uint a = 3;
252                 return (int)(b | a);
253         }
254
255         static int test_3_or_short_un () {
256                 ushort b = 2;
257                 ushort a = 3;
258                 return (int)(b | a);
259         }
260
261         static int test_18_or_imm () {
262                 int b = 2;
263                 return b | 0x10;
264         }
265
266         static int test_268435458_or_large_imm () {
267                 int b = 2;
268                 return b | 0x10000000;
269         }
270
271         static int test_268435459_or_large_imm2 () {
272                 int b = 2;
273                 return b | 0x10000001;
274         }
275
276         static int test_1_xor () {
277                 int b = 2;
278                 int a = 3;
279                 return b ^ a;
280         }
281
282         static int test_1_xor_imm () {
283                 int b = 2;
284                 return b ^ 3;
285         }
286
287         static int test_983041_xor_imm_large () {
288                 int b = 2;
289                 return b ^ 0xf0003;
290         }
291
292         static int test_1_neg () {
293                 int b = -2;
294                 b++;
295                 return -b;
296         }
297
298         static int test_2_not () {
299                 int b = ~2;
300                 b = ~b;
301                 return b;
302         }
303
304         static int test_16_shift () {
305                 int b = 2;
306                 int a = 3;
307                 return b << a;
308         }
309         
310         static int test_16_shift_add () {
311                 int b = 2;
312                 int a = 3;
313                 int c = 0;
314                 return b << (a + c);
315         }
316         
317         static int test_16_shift_add2 () {
318                 int b = 2;
319                 int a = 3;
320                 int c = 0;
321                 return (b + c) << a;
322         }
323         
324         static int test_16_shift_imm () {
325                 int b = 2;
326                 return b << 3;
327         }
328         
329         static int test_524288_shift_imm_large () {
330                 int b = 2;
331                 return b << 18;
332         }
333         
334         static int test_12_shift_imm_inv () {
335                 int b = 2;
336                 return 3 << 2;
337         }
338
339         static int test_12_shift_imm_inv_sbyte () {
340                 sbyte b = 2;
341                 return 3 << 2;
342         }
343
344         static int test_1_rshift_imm () {
345                 int b = 8;
346                 return b >> 3;
347         }
348         
349         static int test_2_unrshift_imm () {
350                 uint b = 16;
351                 return (int)(b >> 3);
352         }
353         
354         static int test_0_bigunrshift_imm () {
355                 unchecked {
356                         uint b = (uint)-1;
357                         b = b >> 1;
358                         if (b != 0x7fffffff)
359                                 return 1;
360                         return 0;
361                 }
362         }
363         
364         static int test_0_bigrshift_imm () {
365                 int b = -1;
366                 b = b >> 1;
367                 if (b != -1)
368                         return 1;
369                 return 0;
370         }
371         
372         static int test_1_rshift () {
373                 int b = 8;
374                 int a = 3;
375                 return b >> a;
376         }
377         
378         static int test_2_unrshift () {
379                 uint b = 16;
380                 int a = 3;
381                 return (int)(b >> a);
382         }
383         
384         static int test_0_bigunrshift () {
385                 unchecked {
386                         uint b = (uint)-1;
387                         int a = 1;
388                         b = b >> a;
389                         if (b != 0x7fffffff)
390                                 return 1;
391                         return 0;
392                 }
393         }
394         
395         static int test_0_bigrshift () {
396                 int b = -1;
397                 int a = 1;
398                 b = b >> a;
399                 if (b != -1)
400                         return 1;
401                 return 0;
402         }
403         
404         static int test_2_cond () {
405                 int b = 2, a = 3, c;
406                 if (a == b)
407                         return 0;
408                 return 2;
409         }
410         
411         static int test_2_cond_short () {
412                 short b = 2, a = 3, c;
413                 if (a == b)
414                         return 0;
415                 return 2;
416         }
417         
418         static int test_2_cond_sbyte () {
419                 sbyte b = 2, a = 3, c;
420                 if (a == b)
421                         return 0;
422                 return 2;
423         }
424         
425         static int test_6_cascade_cond () {
426                 int b = 2, a = 3, c;
427                 if (a == b)
428                         return 0;
429                 else if (b > a)
430                         return 1;
431                 else if (b != b)
432                         return 2;
433                 else {
434                         c = 1;
435                 }
436                 return a + b + c;
437         }
438         
439         static int test_6_cascade_short () {
440                 short b = 2, a = 3, c;
441                 if (a == b)
442                         return 0;
443                 else if (b > a)
444                         return 1;
445                 else if (b != b)
446                         return 2;
447                 else {
448                         c = 1;
449                 }
450                 return a + b + c;
451         }
452
453         static int test_0_short_sign_extend () {
454                 int t1 = 0xffeedd;
455                 short s1 = (short)t1;
456                 int t2 = s1;
457
458                 if ((uint)t2 != 0xffffeedd) 
459                         return 1;
460                 else
461                         return 0;
462         }               
463         
464         static int test_15_for_loop () {
465                 int i;
466                 for (i = 0; i < 15; ++i) {
467                 }
468                 return i;
469         }
470         
471         static int test_11_nested_for_loop () {
472                 int i, j = 0; /* mcs bug here if j not set */
473                 for (i = 0; i < 15; ++i) {
474                         for (j = 200; j >= 5; --j) ;
475                 }
476                 return i - j;
477         }
478
479         static int test_11_several_nested_for_loops () {
480                 int i, j = 0; /* mcs bug here if j not set */
481                 for (i = 0; i < 15; ++i) {
482                         for (j = 200; j >= 5; --j) ;
483                 }
484                 i = j = 0;
485                 for (i = 0; i < 15; ++i) {
486                         for (j = 200; j >= 5; --j) ;
487                 }
488                 return i - j;
489         }
490
491         static int test_0_conv_ovf_i1 () {
492                 int c;
493
494                 //for (int j = 0; j < 10000000; j++)
495                 checked {
496                         c = 127;
497                         sbyte b = (sbyte)c;
498                         c = -128;
499                         b = (sbyte)c;
500                 }
501                 
502                 return 0;
503         }
504         
505         static int test_0_conv_ovf_i1_un () {
506                 uint c;
507
508                 checked {
509                         c = 127;
510                         sbyte b = (sbyte)c;
511                 }
512                 
513                 return 0;
514         }
515         
516         static int test_0_conv_ovf_i2 () {
517                 int c;
518
519                 checked {
520                         c = 32767;
521                         Int16 b = (Int16)c;
522                         c = -32768;
523                         b = (Int16)c;
524                 }
525                 
526                 return 0;
527         }
528         
529         static int test_0_conv_ovf_i2_un () {
530                 uint c;
531
532                 checked {
533                         c = 32767;
534                         Int16 b = (Int16)c;
535                 }
536                 
537                 return 0;
538         }
539         
540         static int test_0_conv_ovf_u2 () {
541                 int c;
542
543                 checked {
544                         c = 65535;
545                         UInt16 b = (UInt16)c;
546                 }
547                 
548                 return 0;
549         }
550         
551         static int test_0_conv_ovf_u2_un () {
552                 uint c;
553
554                 checked {
555                         c = 65535;
556                         UInt16 b = (UInt16)c;
557                 }
558                 
559                 return 0;
560         }
561         
562         static int test_0_conv_ovf_u4 () {
563                 int c;
564
565                 checked {
566                         c = 0x7fffffff;
567                         uint b = (uint)c;
568                 }
569                 
570                 return 0;
571         }
572         
573         static int test_0_bool () {
574                 bool val = true;
575                 if (val)
576                         return 0;
577                 return 1;
578         }
579         
580         static int test_1_bool_inverted () {
581                 bool val = true;
582                 if (!val)
583                         return 0;
584                 return 1;
585         }
586
587         static int test_1_bool_assign () {
588                 bool val = true;
589                 val = !val; // this should produce a ceq
590                 if (val)
591                         return 0;
592                 return 1;
593         }
594
595         static int test_1_bool_multi () {
596                 bool val = true;
597                 bool val2 = true;
598                 val = !val;
599                 if ((val && !val2) && (!val2 && val))
600                         return 0;
601                 return 1;
602         }
603
604         static int test_16_spill () {
605                 int a = 1;
606                 int b = 2;
607                 int c = 3;
608                 int d = 4;
609                 int e = 5;
610
611                 return (1 + (a + (b + (c + (d + e)))));
612         }
613
614         static int test_1_switch () {
615                 int n = 0;
616
617                 switch (n) {
618                 case 0: return 1;
619                 case 1: return 2;
620                 case -1: return 3;
621                 default:
622                         return 4;
623                 }
624                 return 1;
625         }
626
627
628         static int test_0_while_loop_1 () {
629
630                 int value = 255;
631                 
632                 do {
633                         value = value >> 4;
634                 } while (value != 0);
635                 
636                 return 0;
637         }
638
639         static int test_0_while_loop_2 () {
640                 int value = 255;
641                 int position = 5;
642                 
643                 do {
644                         value = value >> 4;
645                 } while (value != 0 && position > 1);
646         
647                 return 0;
648         }
649
650         static int test_0_char_conv () {
651                 int i = 1;
652                 
653                 char tc = (char) ('0' + i);
654
655                 if (tc != '1')
656                         return 1;
657                 
658                 return 0;
659         }
660
661         static int test_3_shift_regalloc () {
662                 int shift = 8;
663                 int orig = 1;
664                 byte value = 0xfe;
665
666                 orig &= ~(0xff << shift);
667                 orig |= value << shift;
668
669                 if (orig == 0xfe01)
670                         return 3;
671                 return 0;
672         }
673
674         enum E {A, B};
675         
676         static int test_2_optimize_branches () {
677                 switch (E.A) {
678                 case E.A:
679                         if (E.A == E.B) {
680                         }
681                         break;
682                 }
683                 return 2;
684         }
685
686         static int test_0_checked_byte_cast () {
687                 int v = 250;
688                 int b = checked ((byte) (v));
689
690                 if (b != 250)
691                         return 1;
692                 return 0;
693         }
694
695         static int test_0_checked_byte_cast_un () {
696                 uint v = 250;
697                 uint b = checked ((byte) (v));
698
699                 if (b != 250)
700                         return 1;
701                 return 0;
702         }
703
704         static int test_0_checked_short_cast () {
705                 int v = 250;
706                 int b = checked ((ushort) (v));
707
708                 if (b != 250)
709                         return 1;
710                 return 0;
711         }
712
713         static int test_0_checked_short_cast_un () {
714                 uint v = 250;
715                 uint b = checked ((ushort) (v));
716
717                 if (b != 250)
718                         return 1;
719                 return 0;
720         }
721         
722         static int test_1_a_eq_b_plus_a () {
723                 int a = 0, b = 1;
724                 a = b + a;
725                 return a;
726         }
727
728         static int test_0_comp_unsigned () {
729                 uint a = 1;
730                 uint b = 0xffffffff;
731                 int error = 1;
732                 bool val;
733
734                 val = a < b;
735                 if (!val)
736                         return error;
737                 error++;
738
739                 val = a <= b;
740                 if (!val)
741                         return error;
742                 error++;
743
744                 val = a == b;
745                 if (val)
746                         return error;
747                 error++;
748
749                 val = a >= b;
750                 if (val)
751                         return error;
752                 error++;
753
754                 val = a > b;
755                 if (val)
756                         return error;
757                 error++;
758
759                 val = b < a;
760                 if (val)
761                         return error;
762                 error++;
763
764                 val = b <= a;
765                 if (val)
766                         return error;
767                 error++;
768
769                 val = b == a;
770                 if (val)
771                         return error;
772                 error++;
773
774                 val = b > a;
775                 if (!val)
776                         return error;
777                 error++;
778
779                 val = b >= a;
780                 if (!val)
781                         return error;
782                 error++;
783
784                 return 0;
785         }
786         
787         static int test_16_cmov () 
788         {
789                 int n = 0;
790                 if (n == 0)
791                         n = 16;
792                 
793                 return n;
794         }
795         
796 }