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