imported everything from my branch (which is slightly harmless).
[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         public static int test_0_return () {
33                 return 0;
34         }
35
36         public static int test_100000_return_large () {
37                 return 100000;
38         }
39
40         public static int test_1_load_bool () {
41                 bool a = true;
42                 return a? 1: 0;
43         }
44
45         public static int test_0_load_bool_false () {
46                 bool a = false;
47                 return a? 1: 0;
48         }
49
50         public static int test_200_load_byte () {
51                 byte a = 200;
52                 return a;
53         }
54
55         public static int test_100_load_sbyte () {
56                 sbyte a = 100;
57                 return a;
58         }
59
60         public static int test_200_load_short () {
61                 short a = 200;
62                 return a;
63         }
64
65         public static int test_100_load_ushort () {
66                 ushort a = 100;
67                 return a;
68         }
69
70         public static int test_3_add_simple () {
71                 int a = 1; 
72                 int b = 2;
73                 return a + b;
74         }
75
76         public static int test_3_add_imm () {
77                 int a = 1; 
78                 return a + 2;
79         }
80
81         public static int test_13407573_add_largeimm () {
82                 int a = 1; 
83                 return a + 13407572;
84         }
85
86         public static int test_1_sub_simple () {
87                 int a = 1; 
88                 int b = 2;
89                 return b - a;
90         }
91
92         public static int test_1_sub_simple_un () {
93                 uint a = 1; 
94                 uint b = 2;
95                 return (int)(b - a);
96         }
97
98         public static int test_1_sub_imm () {
99                 int b = 2;
100                 return b - 1;
101         }
102
103         public static int test_2_sub_large_imm () {
104                 int b = 0xff0f0f;
105                 return b - 0xff0f0d;
106         }
107
108         public static int test_0_sub_inv_imm () {
109                 int b = 2;
110                 return 2 - b;
111         }
112
113         public static int test_2_and () {
114                 int b = 2;
115                 int a = 3;
116                 return b & a;
117         }
118
119         public static int test_0_and_imm () {
120                 int b = 2;
121                 return b & 0x10;
122         }
123
124         public static int test_0_and_large_imm () {
125                 int b = 2;
126                 return b & 0x10000000;
127         }
128
129         public static int test_0_and_large_imm2 () {
130                 int b = 2;
131                 return b & 0x100000f0;
132         }
133
134         public static int test_2_div () {
135                 int b = 6;
136                 int a = 3;
137                 return b / a;
138         }
139
140         public static int test_4_div_imm () {
141                 int b = 12;
142                 return b / 3;
143         }
144
145         public static int test_4_divun_imm () {
146                 uint b = 12;
147                 return (int)(b / 3);
148         }
149
150         public static int test_0_div_fold () {
151                 int b = -1;
152                 return b / 2;
153         }
154
155         public static int test_719177_div_destreg () {
156                 int year = 1970;
157                 return ((365* (year-1)) + ((year-1)/4));
158         }
159
160         public static int test_1_remun_imm () {
161                 uint b = 13;
162                 return (int)(b % 3);
163         }
164
165         public static int test_2_bigremun_imm () {
166                 unchecked {
167                         uint b = (uint)-2;
168                         return (int)(b % 3);
169                 }
170         }
171
172         public static int test_2_rem () {
173                 int b = 5;
174                 int a = 3;
175                 return b % a;
176         }
177
178         public static int test_4_rem_imm () {
179                 int b = 12;
180                 return b % 8;
181         }
182
183         public static int test_4_rem_big_imm () {
184                 int b = 10004;
185                 return b % 10000;
186         }
187
188         public static int test_9_mul () {
189                 int b = 3;
190                 int a = 3;
191                 return b * a;
192         }
193
194         public static int test_15_mul_imm () {
195                 int b = 3;
196                 return b * 5;
197         }
198
199         public 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         public 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         public 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         public 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         public static int test_0_add_ovf () {
244                 int i, j, k;
245
246                 checked {
247                         i = System.Int32.MinValue;
248                         j = 0;
249                         k = i + j;
250                 }
251
252                 if (k != System.Int32.MinValue)
253                         return 1;
254
255                 checked {
256                         i = System.Int32.MaxValue;
257                         j = 0;
258                         k = i + j;
259                 }
260
261                 if (k != System.Int32.MaxValue)
262                         return 2;
263
264                 checked {
265                         i = System.Int32.MinValue;
266                         j = System.Int32.MaxValue;
267                         k = i + j;
268                 }
269
270                 if (k != -1)
271                         return 3;
272
273                 checked {
274                         i = System.Int32.MaxValue;
275                         j = System.Int32.MinValue;
276                         k = i + j;
277                 }
278
279                 if (k != -1)
280                         return 4;
281
282                 checked {
283                         i = System.Int32.MinValue + 1234;
284                         j = -1234;
285                         k = i + j;
286                 }
287
288                 if (k != System.Int32.MinValue)
289                         return 5;
290
291                 checked {
292                         i = System.Int32.MaxValue - 1234;
293                         j = 1234;
294                         k = i + j;
295                 }
296
297                 if (k != System.Int32.MaxValue)
298                         return 6;
299
300                 return 0;
301         }
302
303         public static int test_0_add_un_ovf () {
304                 uint n = (uint)134217728 * 16;
305                 uint number = checked (n + (uint)0);
306
307                 return number == n ? 0 : 1;
308         }
309
310         public static int test_0_sub_ovf () {
311                 int i, j, k;
312
313                 checked {
314                         i = System.Int32.MinValue;
315                         j = 0;
316                         k = i - j;
317                 }
318
319                 if (k != System.Int32.MinValue)
320                         return 1;
321
322                 checked {
323                         i = System.Int32.MaxValue;
324                         j = 0;
325                         k = i - j;
326                 }
327
328                 if (k != System.Int32.MaxValue)
329                         return 2;
330
331                 checked {
332                         i = System.Int32.MinValue;
333                         j = System.Int32.MinValue + 1234;
334                         k = i - j;
335                 }
336
337                 if (k != -1234)
338                         return 3;
339
340                 checked {
341                         i = System.Int32.MaxValue;
342                         j = 1234;
343                         k = i - j;
344                 }
345
346                 if (k != System.Int32.MaxValue - 1234)
347                         return 4;
348
349                 checked {
350                         i = System.Int32.MaxValue - 1234;
351                         j = -1234;
352                         k = i - j;
353                 }
354
355                 if (k != System.Int32.MaxValue)
356                         return 5;
357
358                 checked {
359                         i = System.Int32.MinValue + 1234;
360                         j = 1234;
361                         k = i - j;
362                 }
363
364                 if (k != System.Int32.MinValue)
365                         return 6;
366
367                 return 0;
368         }
369
370         public static int test_0_sub_ovf_un () {
371                 uint i, j, k;
372
373                 checked {
374                         i = System.UInt32.MaxValue;
375                         j = 0;
376                         k = i - j;
377                 }
378
379                 if (k != System.UInt32.MaxValue)
380                         return 1;
381
382                 checked {
383                         i = System.UInt32.MaxValue;
384                         j = System.UInt32.MaxValue;
385                         k = i - j;
386                 }
387
388                 if (k != 0)
389                         return 2;
390
391                 return 0;
392         }
393
394         public static int test_3_or () {
395                 int b = 2;
396                 int a = 3;
397                 return b | a;
398         }
399
400         public static int test_3_or_un () {
401                 uint b = 2;
402                 uint a = 3;
403                 return (int)(b | a);
404         }
405
406         public static int test_3_or_short_un () {
407                 ushort b = 2;
408                 ushort a = 3;
409                 return (int)(b | a);
410         }
411
412         public static int test_18_or_imm () {
413                 int b = 2;
414                 return b | 0x10;
415         }
416
417         public static int test_268435458_or_large_imm () {
418                 int b = 2;
419                 return b | 0x10000000;
420         }
421
422         public static int test_268435459_or_large_imm2 () {
423                 int b = 2;
424                 return b | 0x10000001;
425         }
426
427         public static int test_1_xor () {
428                 int b = 2;
429                 int a = 3;
430                 return b ^ a;
431         }
432
433         public static int test_1_xor_imm () {
434                 int b = 2;
435                 return b ^ 3;
436         }
437
438         public static int test_983041_xor_imm_large () {
439                 int b = 2;
440                 return b ^ 0xf0003;
441         }
442
443         public static int test_1_neg () {
444                 int b = -2;
445                 b++;
446                 return -b;
447         }
448
449         public static int test_2_not () {
450                 int b = ~2;
451                 b = ~b;
452                 return b;
453         }
454
455         public static int test_16_shift () {
456                 int b = 2;
457                 int a = 3;
458                 return b << a;
459         }
460         
461         public static int test_16_shift_add () {
462                 int b = 2;
463                 int a = 3;
464                 int c = 0;
465                 return b << (a + c);
466         }
467         
468         public static int test_16_shift_add2 () {
469                 int b = 2;
470                 int a = 3;
471                 int c = 0;
472                 return (b + c) << a;
473         }
474         
475         public static int test_16_shift_imm () {
476                 int b = 2;
477                 return b << 3;
478         }
479         
480         public static int test_524288_shift_imm_large () {
481                 int b = 2;
482                 return b << 18;
483         }
484         
485         public static int test_12_shift_imm_inv () {
486                 int b = 2;
487                 return 3 << 2;
488         }
489
490         public static int test_12_shift_imm_inv_sbyte () {
491                 sbyte b = 2;
492                 return 3 << 2;
493         }
494
495         public static int test_1_rshift_imm () {
496                 int b = 8;
497                 return b >> 3;
498         }
499         
500         public static int test_2_unrshift_imm () {
501                 uint b = 16;
502                 return (int)(b >> 3);
503         }
504         
505         public static int test_0_bigunrshift_imm () {
506                 unchecked {
507                         uint b = (uint)-1;
508                         b = b >> 1;
509                         if (b != 0x7fffffff)
510                                 return 1;
511                         return 0;
512                 }
513         }
514         
515         public static int test_0_bigrshift_imm () {
516                 int b = -1;
517                 b = b >> 1;
518                 if (b != -1)
519                         return 1;
520                 return 0;
521         }
522         
523         public static int test_1_rshift () {
524                 int b = 8;
525                 int a = 3;
526                 return b >> a;
527         }
528         
529         public static int test_2_unrshift () {
530                 uint b = 16;
531                 int a = 3;
532                 return (int)(b >> a);
533         }
534         
535         public static int test_0_bigunrshift () {
536                 unchecked {
537                         uint b = (uint)-1;
538                         int a = 1;
539                         b = b >> a;
540                         if (b != 0x7fffffff)
541                                 return 1;
542                         return 0;
543                 }
544         }
545         
546         public static int test_0_bigrshift () {
547                 int b = -1;
548                 int a = 1;
549                 b = b >> a;
550                 if (b != -1)
551                         return 1;
552                 return 0;
553         }
554         
555         public static int test_2_cond () {
556                 int b = 2, a = 3, c;
557                 if (a == b)
558                         return 0;
559                 return 2;
560         }
561         
562         public static int test_2_cond_short () {
563                 short b = 2, a = 3, c;
564                 if (a == b)
565                         return 0;
566                 return 2;
567         }
568         
569         public static int test_2_cond_sbyte () {
570                 sbyte b = 2, a = 3, c;
571                 if (a == b)
572                         return 0;
573                 return 2;
574         }
575         
576         public static int test_6_cascade_cond () {
577                 int b = 2, a = 3, c;
578                 if (a == b)
579                         return 0;
580                 else if (b > a)
581                         return 1;
582                 else if (b != b)
583                         return 2;
584                 else {
585                         c = 1;
586                 }
587                 return a + b + c;
588         }
589         
590         public static int test_6_cascade_short () {
591                 short b = 2, a = 3, c;
592                 if (a == b)
593                         return 0;
594                 else if (b > a)
595                         return 1;
596                 else if (b != b)
597                         return 2;
598                 else {
599                         c = 1;
600                 }
601                 return a + b + c;
602         }
603
604         public static int test_0_short_sign_extend () {
605                 int t1 = 0xffeedd;
606                 short s1 = (short)t1;
607                 int t2 = s1;
608
609                 if ((uint)t2 != 0xffffeedd) 
610                         return 1;
611                 else
612                         return 0;
613         }               
614         
615         public static int test_15_for_loop () {
616                 int i;
617                 for (i = 0; i < 15; ++i) {
618                 }
619                 return i;
620         }
621         
622         public static int test_11_nested_for_loop () {
623                 int i, j = 0; /* mcs bug here if j not set */
624                 for (i = 0; i < 15; ++i) {
625                         for (j = 200; j >= 5; --j) ;
626                 }
627                 return i - j;
628         }
629
630         public static int test_11_several_nested_for_loops () {
631                 int i, j = 0; /* mcs bug here if j not set */
632                 for (i = 0; i < 15; ++i) {
633                         for (j = 200; j >= 5; --j) ;
634                 }
635                 i = j = 0;
636                 for (i = 0; i < 15; ++i) {
637                         for (j = 200; j >= 5; --j) ;
638                 }
639                 return i - j;
640         }
641
642         public static int test_0_conv_ovf_i1 () {
643                 int c;
644
645                 //for (int j = 0; j < 10000000; j++)
646                 checked {
647                         c = 127;
648                         sbyte b = (sbyte)c;
649                         c = -128;
650                         b = (sbyte)c;
651                 }
652
653                 return 0;
654         }
655         
656         public static int test_0_conv_ovf_i1_un () {
657                 uint c;
658
659                 checked {
660                         c = 127;
661                         sbyte b = (sbyte)c;
662                 }
663                 
664                 return 0;
665         }
666         
667         public static int test_0_conv_ovf_i2 () {
668                 int c;
669
670                 checked {
671                         c = 32767;
672                         Int16 b = (Int16)c;
673                         c = -32768;
674                         b = (Int16)c;
675                         unchecked {
676                                 uint u = 0xfffffffd;
677                                 c = (int)u;
678                         }
679                         b = (Int16)c;
680                 }
681                 
682                 return 0;
683         }
684         
685         public static int test_0_conv_ovf_i2_un () {
686                 uint c;
687
688                 checked {
689                         c = 32767;
690                         Int16 b = (Int16)c;
691                 }
692                 
693                 return 0;
694         }
695         
696         public static int test_0_conv_ovf_u2 () {
697                 int c;
698
699                 checked {
700                         c = 65535;
701                         UInt16 b = (UInt16)c;
702                 }
703                 
704                 return 0;
705         }
706         
707         public static int test_0_conv_ovf_u2_un () {
708                 uint c;
709
710                 checked {
711                         c = 65535;
712                         UInt16 b = (UInt16)c;
713                 }
714                 
715                 return 0;
716         }
717         
718         public static int test_0_conv_ovf_u4 () {
719                 int c;
720
721                 checked {
722                         c = 0x7fffffff;
723                         uint b = (uint)c;
724                 }
725                 
726                 return 0;
727         }
728
729         public static int test_0_conv_ovf_i4_un () {
730                 uint c;
731
732                 checked {
733                         c = 0x7fffffff;
734                         int b = (int)c;
735                 }
736
737                 return 0;
738         }
739         
740         public static int test_0_bool () {
741                 bool val = true;
742                 if (val)
743                         return 0;
744                 return 1;
745         }
746         
747         public static int test_1_bool_inverted () {
748                 bool val = true;
749                 if (!val)
750                         return 0;
751                 return 1;
752         }
753
754         public static int test_1_bool_assign () {
755                 bool val = true;
756                 val = !val; // this should produce a ceq
757                 if (val)
758                         return 0;
759                 return 1;
760         }
761
762         public static int test_1_bool_multi () {
763                 bool val = true;
764                 bool val2 = true;
765                 val = !val;
766                 if ((val && !val2) && (!val2 && val))
767                         return 0;
768                 return 1;
769         }
770
771         public static int test_16_spill () {
772                 int a = 1;
773                 int b = 2;
774                 int c = 3;
775                 int d = 4;
776                 int e = 5;
777
778                 return (1 + (a + (b + (c + (d + e)))));
779         }
780
781         public static int test_1_switch () {
782                 int n = 0;
783
784                 switch (n) {
785                 case 0: return 1;
786                 case 1: return 2;
787                 case -1: return 3;
788                 default:
789                         return 4;
790                 }
791                 return 1;
792         }
793
794         public static int test_0_switch_constprop () {
795                 int n = -1;
796
797                 switch (n) {
798                 case 0: return 2;
799                 case 1: return 3;
800                 case 2: return 3;                       
801                 default:
802                         return 0;
803                 }
804                 return 3;
805         }
806
807         public static int test_0_switch_constprop2 () {
808                 int n = 3;
809
810                 switch (n) {
811                 case 0: return 2;
812                 case 1: return 3;
813                 case 2: return 3;                       
814                 default:
815                         return 0;
816                 }
817                 return 3;
818         }
819
820         public static int test_0_while_loop_1 () {
821
822                 int value = 255;
823                 
824                 do {
825                         value = value >> 4;
826                 } while (value != 0);
827                 
828                 return 0;
829         }
830
831         public static int test_0_while_loop_2 () {
832                 int value = 255;
833                 int position = 5;
834                 
835                 do {
836                         value = value >> 4;
837                 } while (value != 0 && position > 1);
838         
839                 return 0;
840         }
841
842         public static int test_0_char_conv () {
843                 int i = 1;
844                 
845                 char tc = (char) ('0' + i);
846
847                 if (tc != '1')
848                         return 1;
849                 
850                 return 0;
851         }
852
853         public static int test_3_shift_regalloc () {
854                 int shift = 8;
855                 int orig = 1;
856                 byte value = 0xfe;
857
858                 orig &= ~(0xff << shift);
859                 orig |= value << shift;
860
861                 if (orig == 0xfe01)
862                         return 3;
863                 return 0;
864         }
865
866         enum E {A, B};
867         
868         public static int test_2_optimize_branches () {
869                 switch (E.A) {
870                 case E.A:
871                         if (E.A == E.B) {
872                         }
873                         break;
874                 }
875                 return 2;
876         }
877
878         public static int test_0_checked_byte_cast () {
879                 int v = 250;
880                 int b = checked ((byte) (v));
881
882                 if (b != 250)
883                         return 1;
884                 return 0;
885         }
886
887         public static int test_0_checked_byte_cast_un () {
888                 uint v = 250;
889                 uint b = checked ((byte) (v));
890
891                 if (b != 250)
892                         return 1;
893                 return 0;
894         }
895
896         public static int test_0_checked_short_cast () {
897                 int v = 250;
898                 int b = checked ((ushort) (v));
899
900                 if (b != 250)
901                         return 1;
902                 return 0;
903         }
904
905         public static int test_0_checked_short_cast_un () {
906                 uint v = 250;
907                 uint b = checked ((ushort) (v));
908
909                 if (b != 250)
910                         return 1;
911                 return 0;
912         }
913         
914         public static int test_1_a_eq_b_plus_a () {
915                 int a = 0, b = 1;
916                 a = b + a;
917                 return a;
918         }
919
920         public static int test_0_comp () {
921                 int a = 0;
922                 int b = -1;
923                 int error = 1;
924                 bool val;
925
926                 val = a < b;
927                 if (val)
928                         return error;
929                 error++;
930
931                 val = a > b;
932                 if (!val)
933                         return error;
934                 error ++;
935
936                 val = a == b;
937                 if (val)
938                         return error;
939                 error ++;
940
941                 val = a == a;
942                 if (!val)
943                         return error;
944                 error ++;
945
946                 return 0;
947         }
948
949         public static int test_0_comp_unsigned () {
950                 uint a = 1;
951                 uint b = 0xffffffff;
952                 int error = 1;
953                 bool val;
954
955                 val = a < b;
956                 if (!val)
957                         return error;
958                 error++;
959
960                 val = a <= b;
961                 if (!val)
962                         return error;
963                 error++;
964
965                 val = a == b;
966                 if (val)
967                         return error;
968                 error++;
969
970                 val = a >= b;
971                 if (val)
972                         return error;
973                 error++;
974
975                 val = a > b;
976                 if (val)
977                         return error;
978                 error++;
979
980                 val = b < a;
981                 if (val)
982                         return error;
983                 error++;
984
985                 val = b <= a;
986                 if (val)
987                         return error;
988                 error++;
989
990                 val = b == a;
991                 if (val)
992                         return error;
993                 error++;
994
995                 val = b > a;
996                 if (!val)
997                         return error;
998                 error++;
999
1000                 val = b >= a;
1001                 if (!val)
1002                         return error;
1003                 error++;
1004
1005                 return 0;
1006         }
1007         
1008         public static int test_16_cmov () 
1009         {
1010                 int n = 0;
1011                 if (n == 0)
1012                         n = 16;
1013                 
1014                 return n;
1015         }
1016
1017         public static int test_0_and_cmp ()
1018         {
1019                 /* test esi, imm */
1020                 int local = 0x01020304;
1021                 
1022                 if ((local & 0x01020304) == 0)
1023                         return 7;
1024                 
1025                 if ((local & 0x00000304) == 0)
1026                         return 8;
1027                 
1028                 if ((local & 0x00000004) == 0)
1029                         return 9;
1030                 
1031                 if ((local & 0x00000300) == 0)
1032                         return 10;
1033                 
1034                 if ((local & 0x00020000) == 0)
1035                         return 11;
1036                 
1037                 if ((local & 0x01000000) == 0)
1038                         return 12;
1039
1040                 return 0;
1041         }
1042         
1043         public static int test_0_cne ()
1044         {
1045                 int x = 0;
1046                 int y = 1;
1047                 
1048                 bool b = x != y;
1049                 bool bb = x != x;
1050                 
1051                 if (!b)
1052                         return 1;
1053                 if (bb)
1054                         return 2;
1055                 
1056                 return 0;
1057         }
1058         
1059         static byte b;
1060         public static int test_0_byte_compares ()
1061         {
1062                 b = 0xff;
1063                 if (b == -1)
1064                         return 1;
1065                 b = 0;
1066                 if (!(b < System.Byte.MaxValue))
1067                         return 2;
1068                 
1069                 if (!(b <= System.Byte.MaxValue))
1070                         return 3;
1071                 
1072                 return 0;
1073         }
1074         public static int test_0_cmp_regvar_zero ()
1075         {
1076                 int n = 10;
1077                 
1078                 if (!(n > 0 && n >= 0 && n != 0))
1079                         return 1;
1080                 if (n < 0 || n <= 0 || n == 0)
1081                         return 1;
1082                 
1083                 return 0;
1084         }
1085
1086 }