* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[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_0_add_un_ovf () {
244                 uint n = (uint)134217728 * 16;
245                 uint number = checked (n + (uint)0);
246
247                 return number == n ? 0 : 1;
248         }
249
250         static int test_3_or () {
251                 int b = 2;
252                 int a = 3;
253                 return b | a;
254         }
255
256         static int test_3_or_un () {
257                 uint b = 2;
258                 uint a = 3;
259                 return (int)(b | a);
260         }
261
262         static int test_3_or_short_un () {
263                 ushort b = 2;
264                 ushort a = 3;
265                 return (int)(b | a);
266         }
267
268         static int test_18_or_imm () {
269                 int b = 2;
270                 return b | 0x10;
271         }
272
273         static int test_268435458_or_large_imm () {
274                 int b = 2;
275                 return b | 0x10000000;
276         }
277
278         static int test_268435459_or_large_imm2 () {
279                 int b = 2;
280                 return b | 0x10000001;
281         }
282
283         static int test_1_xor () {
284                 int b = 2;
285                 int a = 3;
286                 return b ^ a;
287         }
288
289         static int test_1_xor_imm () {
290                 int b = 2;
291                 return b ^ 3;
292         }
293
294         static int test_983041_xor_imm_large () {
295                 int b = 2;
296                 return b ^ 0xf0003;
297         }
298
299         static int test_1_neg () {
300                 int b = -2;
301                 b++;
302                 return -b;
303         }
304
305         static int test_2_not () {
306                 int b = ~2;
307                 b = ~b;
308                 return b;
309         }
310
311         static int test_16_shift () {
312                 int b = 2;
313                 int a = 3;
314                 return b << a;
315         }
316         
317         static int test_16_shift_add () {
318                 int b = 2;
319                 int a = 3;
320                 int c = 0;
321                 return b << (a + c);
322         }
323         
324         static int test_16_shift_add2 () {
325                 int b = 2;
326                 int a = 3;
327                 int c = 0;
328                 return (b + c) << a;
329         }
330         
331         static int test_16_shift_imm () {
332                 int b = 2;
333                 return b << 3;
334         }
335         
336         static int test_524288_shift_imm_large () {
337                 int b = 2;
338                 return b << 18;
339         }
340         
341         static int test_12_shift_imm_inv () {
342                 int b = 2;
343                 return 3 << 2;
344         }
345
346         static int test_12_shift_imm_inv_sbyte () {
347                 sbyte b = 2;
348                 return 3 << 2;
349         }
350
351         static int test_1_rshift_imm () {
352                 int b = 8;
353                 return b >> 3;
354         }
355         
356         static int test_2_unrshift_imm () {
357                 uint b = 16;
358                 return (int)(b >> 3);
359         }
360         
361         static int test_0_bigunrshift_imm () {
362                 unchecked {
363                         uint b = (uint)-1;
364                         b = b >> 1;
365                         if (b != 0x7fffffff)
366                                 return 1;
367                         return 0;
368                 }
369         }
370         
371         static int test_0_bigrshift_imm () {
372                 int b = -1;
373                 b = b >> 1;
374                 if (b != -1)
375                         return 1;
376                 return 0;
377         }
378         
379         static int test_1_rshift () {
380                 int b = 8;
381                 int a = 3;
382                 return b >> a;
383         }
384         
385         static int test_2_unrshift () {
386                 uint b = 16;
387                 int a = 3;
388                 return (int)(b >> a);
389         }
390         
391         static int test_0_bigunrshift () {
392                 unchecked {
393                         uint b = (uint)-1;
394                         int a = 1;
395                         b = b >> a;
396                         if (b != 0x7fffffff)
397                                 return 1;
398                         return 0;
399                 }
400         }
401         
402         static int test_0_bigrshift () {
403                 int b = -1;
404                 int a = 1;
405                 b = b >> a;
406                 if (b != -1)
407                         return 1;
408                 return 0;
409         }
410         
411         static int test_2_cond () {
412                 int b = 2, a = 3, c;
413                 if (a == b)
414                         return 0;
415                 return 2;
416         }
417         
418         static int test_2_cond_short () {
419                 short b = 2, a = 3, c;
420                 if (a == b)
421                         return 0;
422                 return 2;
423         }
424         
425         static int test_2_cond_sbyte () {
426                 sbyte b = 2, a = 3, c;
427                 if (a == b)
428                         return 0;
429                 return 2;
430         }
431         
432         static int test_6_cascade_cond () {
433                 int b = 2, a = 3, c;
434                 if (a == b)
435                         return 0;
436                 else if (b > a)
437                         return 1;
438                 else if (b != b)
439                         return 2;
440                 else {
441                         c = 1;
442                 }
443                 return a + b + c;
444         }
445         
446         static int test_6_cascade_short () {
447                 short b = 2, a = 3, c;
448                 if (a == b)
449                         return 0;
450                 else if (b > a)
451                         return 1;
452                 else if (b != b)
453                         return 2;
454                 else {
455                         c = 1;
456                 }
457                 return a + b + c;
458         }
459
460         static int test_0_short_sign_extend () {
461                 int t1 = 0xffeedd;
462                 short s1 = (short)t1;
463                 int t2 = s1;
464
465                 if ((uint)t2 != 0xffffeedd) 
466                         return 1;
467                 else
468                         return 0;
469         }               
470         
471         static int test_15_for_loop () {
472                 int i;
473                 for (i = 0; i < 15; ++i) {
474                 }
475                 return i;
476         }
477         
478         static int test_11_nested_for_loop () {
479                 int i, j = 0; /* mcs bug here if j not set */
480                 for (i = 0; i < 15; ++i) {
481                         for (j = 200; j >= 5; --j) ;
482                 }
483                 return i - j;
484         }
485
486         static int test_11_several_nested_for_loops () {
487                 int i, j = 0; /* mcs bug here if j not set */
488                 for (i = 0; i < 15; ++i) {
489                         for (j = 200; j >= 5; --j) ;
490                 }
491                 i = j = 0;
492                 for (i = 0; i < 15; ++i) {
493                         for (j = 200; j >= 5; --j) ;
494                 }
495                 return i - j;
496         }
497
498         static int test_0_conv_ovf_i1 () {
499                 int c;
500
501                 //for (int j = 0; j < 10000000; j++)
502                 checked {
503                         c = 127;
504                         sbyte b = (sbyte)c;
505                         c = -128;
506                         b = (sbyte)c;
507                 }
508
509                 return 0;
510         }
511         
512         static int test_0_conv_ovf_i1_un () {
513                 uint c;
514
515                 checked {
516                         c = 127;
517                         sbyte b = (sbyte)c;
518                 }
519                 
520                 return 0;
521         }
522         
523         static int test_0_conv_ovf_i2 () {
524                 int c;
525
526                 checked {
527                         c = 32767;
528                         Int16 b = (Int16)c;
529                         c = -32768;
530                         b = (Int16)c;
531                         unchecked {
532                                 uint u = 0xfffffffd;
533                                 c = (int)u;
534                         }
535                         b = (Int16)c;
536                 }
537                 
538                 return 0;
539         }
540         
541         static int test_0_conv_ovf_i2_un () {
542                 uint c;
543
544                 checked {
545                         c = 32767;
546                         Int16 b = (Int16)c;
547                 }
548                 
549                 return 0;
550         }
551         
552         static int test_0_conv_ovf_u2 () {
553                 int c;
554
555                 checked {
556                         c = 65535;
557                         UInt16 b = (UInt16)c;
558                 }
559                 
560                 return 0;
561         }
562         
563         static int test_0_conv_ovf_u2_un () {
564                 uint c;
565
566                 checked {
567                         c = 65535;
568                         UInt16 b = (UInt16)c;
569                 }
570                 
571                 return 0;
572         }
573         
574         static int test_0_conv_ovf_u4 () {
575                 int c;
576
577                 checked {
578                         c = 0x7fffffff;
579                         uint b = (uint)c;
580                 }
581                 
582                 return 0;
583         }
584         
585         static int test_0_bool () {
586                 bool val = true;
587                 if (val)
588                         return 0;
589                 return 1;
590         }
591         
592         static int test_1_bool_inverted () {
593                 bool val = true;
594                 if (!val)
595                         return 0;
596                 return 1;
597         }
598
599         static int test_1_bool_assign () {
600                 bool val = true;
601                 val = !val; // this should produce a ceq
602                 if (val)
603                         return 0;
604                 return 1;
605         }
606
607         static int test_1_bool_multi () {
608                 bool val = true;
609                 bool val2 = true;
610                 val = !val;
611                 if ((val && !val2) && (!val2 && val))
612                         return 0;
613                 return 1;
614         }
615
616         static int test_16_spill () {
617                 int a = 1;
618                 int b = 2;
619                 int c = 3;
620                 int d = 4;
621                 int e = 5;
622
623                 return (1 + (a + (b + (c + (d + e)))));
624         }
625
626         static int test_1_switch () {
627                 int n = 0;
628
629                 switch (n) {
630                 case 0: return 1;
631                 case 1: return 2;
632                 case -1: return 3;
633                 default:
634                         return 4;
635                 }
636                 return 1;
637         }
638
639
640         static int test_0_while_loop_1 () {
641
642                 int value = 255;
643                 
644                 do {
645                         value = value >> 4;
646                 } while (value != 0);
647                 
648                 return 0;
649         }
650
651         static int test_0_while_loop_2 () {
652                 int value = 255;
653                 int position = 5;
654                 
655                 do {
656                         value = value >> 4;
657                 } while (value != 0 && position > 1);
658         
659                 return 0;
660         }
661
662         static int test_0_char_conv () {
663                 int i = 1;
664                 
665                 char tc = (char) ('0' + i);
666
667                 if (tc != '1')
668                         return 1;
669                 
670                 return 0;
671         }
672
673         static unsafe int test_0_pin_string () {
674                 string x = "xxx";
675                 fixed (char *c = x) {
676                         if (*c != 'x')
677                                 return 1;
678                 }
679                 return 0;
680         }
681
682         static int test_3_shift_regalloc () {
683                 int shift = 8;
684                 int orig = 1;
685                 byte value = 0xfe;
686
687                 orig &= ~(0xff << shift);
688                 orig |= value << shift;
689
690                 if (orig == 0xfe01)
691                         return 3;
692                 return 0;
693         }
694
695         enum E {A, B};
696         
697         static int test_2_optimize_branches () {
698                 switch (E.A) {
699                 case E.A:
700                         if (E.A == E.B) {
701                         }
702                         break;
703                 }
704                 return 2;
705         }
706
707         static int test_0_checked_byte_cast () {
708                 int v = 250;
709                 int b = checked ((byte) (v));
710
711                 if (b != 250)
712                         return 1;
713                 return 0;
714         }
715
716         static int test_0_checked_byte_cast_un () {
717                 uint v = 250;
718                 uint b = checked ((byte) (v));
719
720                 if (b != 250)
721                         return 1;
722                 return 0;
723         }
724
725         static int test_0_checked_short_cast () {
726                 int v = 250;
727                 int b = checked ((ushort) (v));
728
729                 if (b != 250)
730                         return 1;
731                 return 0;
732         }
733
734         static int test_0_checked_short_cast_un () {
735                 uint v = 250;
736                 uint b = checked ((ushort) (v));
737
738                 if (b != 250)
739                         return 1;
740                 return 0;
741         }
742         
743         static int test_1_a_eq_b_plus_a () {
744                 int a = 0, b = 1;
745                 a = b + a;
746                 return a;
747         }
748
749         static int test_0_comp () {
750                 int a = 0;
751                 int b = -1;
752                 int error = 1;
753                 bool val;
754
755                 val = a < b;
756                 if (val)
757                         return error;
758                 error++;
759
760                 val = a > b;
761                 if (!val)
762                         return error;
763                 error ++;
764
765                 val = a == b;
766                 if (val)
767                         return error;
768                 error ++;
769
770                 val = a == a;
771                 if (!val)
772                         return error;
773                 error ++;
774
775                 return 0;
776         }
777
778         static int test_0_comp_unsigned () {
779                 uint a = 1;
780                 uint b = 0xffffffff;
781                 int error = 1;
782                 bool val;
783
784                 val = a < b;
785                 if (!val)
786                         return error;
787                 error++;
788
789                 val = a <= b;
790                 if (!val)
791                         return error;
792                 error++;
793
794                 val = a == b;
795                 if (val)
796                         return error;
797                 error++;
798
799                 val = a >= b;
800                 if (val)
801                         return error;
802                 error++;
803
804                 val = a > b;
805                 if (val)
806                         return error;
807                 error++;
808
809                 val = b < a;
810                 if (val)
811                         return error;
812                 error++;
813
814                 val = b <= a;
815                 if (val)
816                         return error;
817                 error++;
818
819                 val = b == a;
820                 if (val)
821                         return error;
822                 error++;
823
824                 val = b > a;
825                 if (!val)
826                         return error;
827                 error++;
828
829                 val = b >= a;
830                 if (!val)
831                         return error;
832                 error++;
833
834                 return 0;
835         }
836         
837         static int test_16_cmov () 
838         {
839                 int n = 0;
840                 if (n == 0)
841                         n = 16;
842                 
843                 return n;
844         }
845         
846         static int my_flags;
847         static int test_0_and_cmp ()
848         {
849                 
850                 /* various forms of test [mem], imm */
851                 
852                 my_flags = 0x01020304;
853                 
854                 if ((my_flags & 0x01020304) == 0)
855                         return 1;
856                 
857                 if ((my_flags & 0x00000304) == 0)
858                         return 2;
859                 
860                 if ((my_flags & 0x00000004) == 0)
861                         return 3;
862                 
863                 if ((my_flags & 0x00000300) == 0)
864                         return 4;
865                 
866                 if ((my_flags & 0x00020000) == 0)
867                         return 5;
868                 
869                 if ((my_flags & 0x01000000) == 0)
870                         return 6;
871                 
872                 /* test esi, imm */
873                 int local = 0x01020304;
874                 
875                 if ((local & 0x01020304) == 0)
876                         return 7;
877                 
878                 if ((local & 0x00000304) == 0)
879                         return 8;
880                 
881                 if ((local & 0x00000004) == 0)
882                         return 9;
883                 
884                 if ((local & 0x00000300) == 0)
885                         return 10;
886                 
887                 if ((local & 0x00020000) == 0)
888                         return 11;
889                 
890                 if ((local & 0x01000000) == 0)
891                         return 12;
892                 
893                 return 0;
894         }
895         
896         static int test_0_cne ()
897         {
898                 int x = 0;
899                 int y = 1;
900                 
901                 bool b = x != y;
902                 bool bb = x != x;
903                 
904                 if (!b)
905                         return 1;
906                 if (bb)
907                         return 2;
908                 
909                 return 0;
910         }
911         
912         static byte b;
913         static int test_0_byte_compares ()
914         {
915                 b = 0xff;
916                 if (b == -1)
917                         return 1;
918                 b = 0;
919                 if (!(b < System.Byte.MaxValue))
920                         return 2;
921                 
922                 if (!(b <= System.Byte.MaxValue))
923                         return 3;
924                 
925                 return 0;
926         }
927         static int test_0_cmp_regvar_zero ()
928         {
929                 int n = 10;
930                 
931                 if (!(n > 0 && n >= 0 && n != 0))
932                         return 1;
933                 if (n < 0 || n <= 0 || n == 0)
934                         return 1;
935                 
936                 return 0;
937         }
938
939 }