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