2004-01-29 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / basic.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31         
32         static int test_0_return () {
33                 return 0;
34         }
35
36         static int test_100000_return_large () {
37                 return 100000;
38         }
39
40         static int test_1_load_bool () {
41                 bool a = true;
42                 return a? 1: 0;
43         }
44
45         static int test_0_load_bool_false () {
46                 bool a = false;
47                 return a? 1: 0;
48         }
49
50         static int test_200_load_byte () {
51                 byte a = 200;
52                 return a;
53         }
54
55         static int test_100_load_sbyte () {
56                 sbyte a = 100;
57                 return a;
58         }
59
60         static int test_200_load_short () {
61                 short a = 200;
62                 return a;
63         }
64
65         static int test_100_load_ushort () {
66                 ushort a = 100;
67                 return a;
68         }
69
70         static int test_3_add_simple () {
71                 int a = 1; 
72                 int b = 2;
73                 return a + b;
74         }
75
76         static int test_3_add_imm () {
77                 int a = 1; 
78                 return a + 2;
79         }
80
81         static int test_13407573_add_largeimm () {
82                 int a = 1; 
83                 return a + 13407572;
84         }
85
86         static int test_1_sub_simple () {
87                 int a = 1; 
88                 int b = 2;
89                 return b - a;
90         }
91
92         static int test_1_sub_simple_un () {
93                 uint a = 1; 
94                 uint b = 2;
95                 return (int)(b - a);
96         }
97
98         static int test_1_sub_imm () {
99                 int b = 2;
100                 return b - 1;
101         }
102
103         static int test_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_9_mul () {
182                 int b = 3;
183                 int a = 3;
184                 return b * a;
185         }
186
187         static int test_15_mul_imm () {
188                 int b = 3;
189                 return b * 5;
190         }
191
192         static int test_24_mul () {
193                 int a = 3;
194                 int b = 8;
195                 int res;
196
197                 res = a * b;
198                 
199                 return res;
200         }
201
202         static int test_24_mul_ovf () {
203                 int a = 3;
204                 int b = 8;
205                 int res;
206
207                 checked {
208                         res = a * b;
209                 }
210                 
211                 return res;
212         }
213
214         static int test_24_mul_un () {
215                 uint a = 3;
216                 uint b = 8;
217                 uint res;
218
219                 res = a * b;
220                 
221                 return (int)res;
222         }
223
224         static int test_24_mul_ovf_un () {
225                 uint a = 3;
226                 uint b = 8;
227                 uint res;
228
229                 checked {
230                         res = a * b;
231                 }
232                 
233                 return (int)res;
234         }
235
236         static int test_3_or () {
237                 int b = 2;
238                 int a = 3;
239                 return b | a;
240         }
241
242         static int test_3_or_un () {
243                 uint b = 2;
244                 uint a = 3;
245                 return (int)(b | a);
246         }
247
248         static int test_3_or_short_un () {
249                 ushort b = 2;
250                 ushort a = 3;
251                 return (int)(b | a);
252         }
253
254         static int test_18_or_imm () {
255                 int b = 2;
256                 return b | 0x10;
257         }
258
259         static int test_268435458_or_large_imm () {
260                 int b = 2;
261                 return b | 0x10000000;
262         }
263
264         static int test_268435459_or_large_imm2 () {
265                 int b = 2;
266                 return b | 0x10000001;
267         }
268
269         static int test_1_xor () {
270                 int b = 2;
271                 int a = 3;
272                 return b ^ a;
273         }
274
275         static int test_1_xor_imm () {
276                 int b = 2;
277                 return b ^ 3;
278         }
279
280         static int test_983041_xor_imm_large () {
281                 int b = 2;
282                 return b ^ 0xf0003;
283         }
284
285         static int test_1_neg () {
286                 int b = -2;
287                 b++;
288                 return -b;
289         }
290
291         static int test_2_not () {
292                 int b = ~2;
293                 b = ~b;
294                 return b;
295         }
296
297         static int test_16_shift () {
298                 int b = 2;
299                 int a = 3;
300                 return b << a;
301         }
302         
303         static int test_16_shift_add () {
304                 int b = 2;
305                 int a = 3;
306                 int c = 0;
307                 return b << (a + c);
308         }
309         
310         static int test_16_shift_add2 () {
311                 int b = 2;
312                 int a = 3;
313                 int c = 0;
314                 return (b + c) << a;
315         }
316         
317         static int test_16_shift_imm () {
318                 int b = 2;
319                 return b << 3;
320         }
321         
322         static int test_524288_shift_imm_large () {
323                 int b = 2;
324                 return b << 18;
325         }
326         
327         static int test_12_shift_imm_inv () {
328                 int b = 2;
329                 return 3 << 2;
330         }
331
332         static int test_12_shift_imm_inv_sbyte () {
333                 sbyte b = 2;
334                 return 3 << 2;
335         }
336
337         static int test_1_rshift_imm () {
338                 int b = 8;
339                 return b >> 3;
340         }
341         
342         static int test_2_unrshift_imm () {
343                 uint b = 16;
344                 return (int)(b >> 3);
345         }
346         
347         static int test_0_bigunrshift_imm () {
348                 uint b = (uint)-1;
349                 b = b >> 1;
350                 if (b != 0x7fffffff)
351                         return 1;
352                 return 0;
353         }
354         
355         static int test_0_bigrshift_imm () {
356                 int b = -1;
357                 b = b >> 1;
358                 if (b != -1)
359                         return 1;
360                 return 0;
361         }
362         
363         static int test_1_rshift () {
364                 int b = 8;
365                 int a = 3;
366                 return b >> a;
367         }
368         
369         static int test_2_unrshift () {
370                 uint b = 16;
371                 int a = 3;
372                 return (int)(b >> a);
373         }
374         
375         static int test_0_bigunrshift () {
376                 uint b = (uint)-1;
377                 int a = 1;
378                 b = b >> a;
379                 if (b != 0x7fffffff)
380                         return 1;
381                 return 0;
382         }
383         
384         static int test_0_bigrshift () {
385                 int b = -1;
386                 int a = 1;
387                 b = b >> a;
388                 if (b != -1)
389                         return 1;
390                 return 0;
391         }
392         
393         static int test_2_cond () {
394                 int b = 2, a = 3, c;
395                 if (a == b)
396                         return 0;
397                 return 2;
398         }
399         
400         static int test_2_cond_short () {
401                 short b = 2, a = 3, c;
402                 if (a == b)
403                         return 0;
404                 return 2;
405         }
406         
407         static int test_2_cond_sbyte () {
408                 sbyte b = 2, a = 3, c;
409                 if (a == b)
410                         return 0;
411                 return 2;
412         }
413         
414         static int test_6_cascade_cond () {
415                 int b = 2, a = 3, c;
416                 if (a == b)
417                         return 0;
418                 else if (b > a)
419                         return 1;
420                 else if (b != b)
421                         return 2;
422                 else {
423                         c = 1;
424                 }
425                 return a + b + c;
426         }
427         
428         static int test_6_cascade_short () {
429                 short b = 2, a = 3, c;
430                 if (a == b)
431                         return 0;
432                 else if (b > a)
433                         return 1;
434                 else if (b != b)
435                         return 2;
436                 else {
437                         c = 1;
438                 }
439                 return a + b + c;
440         }
441         
442         static int test_15_for_loop () {
443                 int i;
444                 for (i = 0; i < 15; ++i) {
445                 }
446                 return i;
447         }
448         
449         static int test_11_nested_for_loop () {
450                 int i, j = 0; /* mcs bug here if j not set */
451                 for (i = 0; i < 15; ++i) {
452                         for (j = 200; j >= 5; --j) ;
453                 }
454                 return i - j;
455         }
456
457         static int test_11_several_nested_for_loops () {
458                 int i, j = 0; /* mcs bug here if j not set */
459                 for (i = 0; i < 15; ++i) {
460                         for (j = 200; j >= 5; --j) ;
461                 }
462                 i = j = 0;
463                 for (i = 0; i < 15; ++i) {
464                         for (j = 200; j >= 5; --j) ;
465                 }
466                 return i - j;
467         }
468
469         static int test_0_conv_ovf_i1 () {
470                 int c;
471
472                 //for (int j = 0; j < 10000000; j++)
473                 checked {
474                         c = 127;
475                         sbyte b = (sbyte)c;
476                         c = -128;
477                         b = (sbyte)c;
478                 }
479                 
480                 return 0;
481         }
482         
483         static int test_0_conv_ovf_i1_un () {
484                 uint c;
485
486                 checked {
487                         c = 127;
488                         sbyte b = (sbyte)c;
489                 }
490                 
491                 return 0;
492         }
493         
494         static int test_0_conv_ovf_i2 () {
495                 int c;
496
497                 checked {
498                         c = 32767;
499                         Int16 b = (Int16)c;
500                         c = -32768;
501                         b = (Int16)c;
502                 }
503                 
504                 return 0;
505         }
506         
507         static int test_0_conv_ovf_i2_un () {
508                 uint c;
509
510                 checked {
511                         c = 32767;
512                         Int16 b = (Int16)c;
513                 }
514                 
515                 return 0;
516         }
517         
518         static int test_0_conv_ovf_u2 () {
519                 int c;
520
521                 checked {
522                         c = 65535;
523                         UInt16 b = (UInt16)c;
524                 }
525                 
526                 return 0;
527         }
528         
529         static int test_0_conv_ovf_u2_un () {
530                 uint c;
531
532                 checked {
533                         c = 65535;
534                         UInt16 b = (UInt16)c;
535                 }
536                 
537                 return 0;
538         }
539         
540         static int test_0_conv_ovf_u4 () {
541                 int c;
542
543                 checked {
544                         c = 0x7fffffff;
545                         uint b = (uint)c;
546                 }
547                 
548                 return 0;
549         }
550         
551         static int test_0_bool () {
552                 bool val = true;
553                 if (val)
554                         return 0;
555                 return 1;
556         }
557         
558         static int test_1_bool_inverted () {
559                 bool val = true;
560                 if (!val)
561                         return 0;
562                 return 1;
563         }
564
565         static int test_1_bool_assign () {
566                 bool val = true;
567                 val = !val; // this should produce a ceq
568                 if (val)
569                         return 0;
570                 return 1;
571         }
572
573         static int test_1_bool_multi () {
574                 bool val = true;
575                 bool val2 = true;
576                 val = !val;
577                 if ((val && !val2) && (!val2 && val))
578                         return 0;
579                 return 1;
580         }
581
582         static int test_16_spill () {
583                 int a = 1;
584                 int b = 2;
585                 int c = 3;
586                 int d = 4;
587                 int e = 5;
588
589                 return (1 + (a + (b + (c + (d + e)))));
590         }
591
592         static int test_1_switch () {
593                 int n = 0;
594
595                 switch (n) {
596                 case 0: return 1;
597                 case 1: return 2;
598                 case -1: return 3;
599                 default:
600                         return 4;
601                 }
602                 return 1;
603         }
604
605
606         static int test_0_while_loop_1 () {
607
608                 int value = 255;
609                 
610                 do {
611                         value = value >> 4;
612                 } while (value != 0);
613                 
614                 return 0;
615         }
616
617         static int test_0_while_loop_2 () {
618                 int value = 255;
619                 int position = 5;
620                 
621                 do {
622                         value = value >> 4;
623                 } while (value != 0 && position > 1);
624         
625                 return 0;
626         }
627
628         static int test_0_char_conv () {
629                 int i = 1;
630                 
631                 char tc = (char) ('0' + i);
632
633                 if (tc != '1')
634                         return 1;
635                 
636                 return 0;
637         }
638
639         static int test_3_shift_regalloc () {
640                 int shift = 8;
641                 int orig = 1;
642                 byte value = 0xfe;
643
644                 orig &= ~(0xff << shift);
645                 orig |= value << shift;
646
647                 if (orig == 0xfe01)
648                         return 3;
649                 return 0;
650         }
651
652         enum E {A, B};
653         
654         static int test_2_optimize_branches () {
655                 switch (E.A) {
656                 case E.A:
657                         if (E.A == E.B) {
658                         }
659                         break;
660                 }
661                 return 2;
662         }
663
664         static int test_0_checked_byte_cast () {
665                 int v = 250;
666                 int b = checked ((byte) (v));
667
668                 if (b != 250)
669                         return 1;
670                 return 0;
671         }
672
673         static int test_0_checked_byte_cast_un () {
674                 uint v = 250;
675                 uint b = checked ((byte) (v));
676
677                 if (b != 250)
678                         return 1;
679                 return 0;
680         }
681
682         static int test_0_checked_short_cast () {
683                 int v = 250;
684                 int b = checked ((ushort) (v));
685
686                 if (b != 250)
687                         return 1;
688                 return 0;
689         }
690
691         static int test_0_checked_short_cast_un () {
692                 uint v = 250;
693                 uint b = checked ((ushort) (v));
694
695                 if (b != 250)
696                         return 1;
697                 return 0;
698         }
699
700 }