2004-09-02 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / exceptions.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_catch () {
33                 Exception x = new Exception ();
34                 
35                 try {
36                         throw x;
37                 } catch (Exception e) {
38                         if (e == x)
39                                 return 0;
40                 }
41                 return 1;
42         }
43
44         static int test_0_finally_without_exc () {
45                 int x;
46                 
47                 try {
48                         x = 1;
49                 } catch (Exception e) {
50                         x = 2;
51                 } finally {
52                         x = 0;
53                 }
54                 
55                 return x;
56         }
57
58         static int test_0_finally () {
59                 int x = 1;
60                 
61                 try {
62                         throw new Exception ();
63                 } catch (Exception e) {
64                         x = 2;
65                 } finally {
66                         x = 0;
67                 }
68                 return x;
69         }
70
71         static int test_0_nested_finally () {
72                 int a;
73
74                 try {
75                         a = 1;
76                 } finally {
77                         try {
78                                 a = 2;
79                         } finally {
80                                 a = 0;
81                         }
82                 }
83                 return a;
84         }               
85
86         static int test_0_byte_cast () {
87                 int a;
88                 long l;
89                 ulong ul;
90                 byte b = 0;
91                 bool failed;
92
93                 try {
94                         a = 255;
95                         failed = false;
96                         checked {
97                                 b = (byte)a;
98                         }
99                 } catch (OverflowException) {
100                         failed = true;
101                 }
102                 if (failed)
103                         return 1;
104                 if (b != 255)
105                         return -1;
106
107                 try {
108                         a = 0;
109                         failed = false;
110                         checked {
111                                 b = (byte)a;
112                         }
113                 } catch (OverflowException) {
114                         failed = true;
115                 }
116                 if (failed)
117                         return 2;
118                 if (b != 0)
119                         return -2;
120
121
122                 try {
123                         a = 256;
124                         failed = true;
125                         checked {
126                                 b = (byte)a;
127                         }
128                 } catch (OverflowException) {
129                         failed = false;
130                 }
131                 if (failed)
132                         return 3;
133                 if (b != 0)
134                         return -3;
135
136                 try {
137                         a = -1;
138                         failed = true;
139                         checked {
140                                 b = (byte)a;
141                         }
142                 } catch (OverflowException) {
143                         failed = false;
144                 }
145                 if (failed)
146                         return 4;
147                 if (b != 0)
148                         return -4;
149
150                 try {
151                         double d = 0;
152                         failed = false;
153                         checked {
154                                 b = (byte)d;
155                         }
156                 } catch (OverflowException) {
157                         failed = true;
158                 }
159                 if (failed)
160                         return 5;
161                 if (b != 0)
162                         return -5;
163                 
164                 try {
165                         double d = -1;
166                         failed = true;
167                         checked {
168                                 b = (byte)d;
169                         }
170                 } catch (OverflowException) {
171                         failed = false;
172                 }
173                 if (failed)
174                         return 6;
175                 if (b != 0)
176                         return -6;
177
178                 try {
179                         double d = 255;
180                         failed = false;
181                         checked {
182                                 b = (byte)d;
183                         }
184                 } catch (OverflowException) {
185                         failed = true;
186                 }
187                 if (failed)
188                         return 7;
189                 if (b != 255)
190                         return -7;
191
192                 try {
193                         double d = 256;
194                         failed = true;
195                         checked {
196                                 b = (byte)d;
197                         }
198                 } catch (OverflowException) {
199                         failed = false;
200                 }
201                 if (failed)
202                         return 8;
203                 if (b != 255)
204                         return -8;
205
206                 try {
207                         l = 255;
208                         failed = false;
209                         checked {
210                                 b = (byte)l;
211                         }
212                 } catch (OverflowException) {
213                         failed = true;
214                 }
215                 if (failed)
216                         return 9;
217                 if (b != 255)
218                         return -9;
219
220                 try {
221                         l = 0;
222                         failed = false;
223                         checked {
224                                 b = (byte)l;
225                         }
226                 } catch (OverflowException) {
227                         failed = true;
228                 }
229                 if (failed)
230                         return 10;
231                 if (b != 0)
232                         return -10;
233
234                 try {
235                         l = 256;
236                         failed = true;
237                         checked {
238                                 b = (byte)l;
239                         }
240                 } catch (OverflowException) {
241                         failed = false;
242                 }
243                 if (failed)
244                         return 11;
245                 if (b != 0)
246                         return -11;
247
248                 try {
249                         l = -1;
250                         failed = true;
251                         checked {
252                                 b = (byte)l;
253                         }
254                 } catch (OverflowException) {
255                         failed = false;
256                 }
257                 if (failed)
258                         return 12;
259                 if (b != 0)
260                         return -12;
261
262                 try {
263                         ul = 256;
264                         failed = true;
265                         checked {
266                                 b = (byte)ul;
267                         }
268                 }
269                 catch (OverflowException) {
270                         failed = false;
271                 }
272                 if (failed)
273                         return 13;
274                 if (b != 0)
275                         return -13;
276
277                 return 0;
278         }
279         
280         static int test_0_sbyte_cast () {
281                 int a;
282                 long l;
283                 sbyte b = 0;
284                 bool failed;
285
286                 try {
287                         a = 255;
288                         failed = true;
289                         checked {
290                                 b = (sbyte)a;
291                         }
292                 } catch (OverflowException) {
293                         failed = false;
294                 }
295                 if (failed)
296                         return 1;
297                 if (b != 0)
298                         return -1;
299
300                 try {
301                         a = 0;
302                         failed = false;
303                         checked {
304                                 b = (sbyte)a;
305                         }
306                 } catch (OverflowException) {
307                         failed = true;
308                 }
309                 if (failed)
310                         return 2;
311                 if (b != 0)
312                         return -2;
313
314                 try {
315                         a = 256;
316                         failed = true;
317                         checked {
318                                 b = (sbyte)a;
319                         }
320                 } catch (OverflowException) {
321                         failed = false;
322                 }
323                 if (failed)
324                         return 3;
325                 if (b != 0)
326                         return -3;
327
328                 try {
329                         a = -129;
330                         failed = true;
331                         checked {
332                                 b = (sbyte)a;
333                         }
334                 } catch (OverflowException) {
335                         failed = false;
336                 }
337                 if (failed)
338                         return 4;
339                 if (b != 0)
340                         return -4;
341
342                 try {
343                         a = -1;
344                         failed = false;
345                         checked {
346                                 b = (sbyte)a;
347                         }
348                 } catch (OverflowException) {
349                         failed = true;
350                 }
351                 if (failed)
352                         return 5;
353                 if (b != -1)
354                         return -5;
355
356                 try {
357                         a = -128;
358                         failed = false;
359                         checked {
360                                 b = (sbyte)a;
361                         }
362                 } catch (OverflowException) {
363                         failed = true;
364                 }
365                 if (failed)
366                         return 6;
367                 if (b != -128)
368                         return -6;
369
370                 try {
371                         a = 127;
372                         failed = false;
373                         checked {
374                                 b = (sbyte)a;
375                         }
376                 } catch (OverflowException) {
377                         failed = true;
378                 }
379                 if (failed)
380                         return 7;
381                 if (b != 127)
382                         return -7;
383
384                 try {
385                         a = 128;
386                         failed = true;
387                         checked {
388                                 b = (sbyte)a;
389                         }
390                 } catch (OverflowException) {
391                         failed = false;
392                 }
393                 if (failed)
394                         return 8;
395                 if (b != 127)
396                         return -8;
397
398                 try {
399                         double d = 127;
400                         failed = false;
401                         checked {
402                                 b = (sbyte)d;
403                         }
404                 } catch (OverflowException) {
405                         failed = true;
406                 }
407                 if (failed)
408                         return 9;
409                 if (b != 127)
410                         return -9;
411
412                 try {
413                         double d = -128;
414                         failed = false;
415                         checked {
416                                 b = (sbyte)d;
417                         }
418                 } catch (OverflowException) {
419                         failed = true;
420                 }
421                 if (failed)
422                         return 10;
423                 if (b != -128)
424                         return -10;
425
426                 try {
427                         double d = 128;
428                         failed = true;
429                         checked {
430                                 b = (sbyte)d;
431                         }
432                 } catch (OverflowException) {
433                         failed = false;
434                 }
435                 if (failed)
436                         return 11;
437                 if (b != -128)
438                         return -11;
439
440                 try {
441                         double d = -129;
442                         failed = true;
443                         checked {
444                                 b = (sbyte)d;
445                         }
446                 } catch (OverflowException) {
447                         failed = false;
448                 }
449                 if (failed)
450                         return 12;
451                 if (b != -128)
452                         return -12;
453
454                 try {
455                         l = 255;
456                         failed = true;
457                         checked {
458                                 b = (sbyte)l;
459                         }
460                 } catch (OverflowException) {
461                         failed = false;
462                 }
463                 if (failed)
464                         return 13;
465                 if (b != -128)
466                         return -13;
467
468                 try {
469                         l = 0;
470                         failed = false;
471                         checked {
472                                 b = (sbyte)l;
473                         }
474                 } catch (OverflowException) {
475                         failed = true;
476                 }
477                 if (failed)
478                         return 14;
479                 if (b != 0)
480                         return -14;
481
482                 try {
483                         l = 256;
484                         failed = true;
485                         checked {
486                                 b = (sbyte)l;
487                         }
488                 } catch (OverflowException) {
489                         failed = false;
490                 }
491                 if (failed)
492                         return 15;
493                 if (b != 0)
494                         return -15;
495
496                 try {
497                         l = -129;
498                         failed = true;
499                         checked {
500                                 b = (sbyte)l;
501                         }
502                 } catch (OverflowException) {
503                         failed = false;
504                 }
505                 if (failed)
506                         return 16;
507                 if (b != 0)
508                         return -16;
509
510                 try {
511                         l = -1;
512                         failed = false;
513                         checked {
514                                 b = (sbyte)l;
515                         }
516                 } catch (OverflowException) {
517                         failed = true;
518                 }
519                 if (failed)
520                         return 17;
521                 if (b != -1)
522                         return -17;
523
524                 try {
525                         l = -128;
526                         failed = false;
527                         checked {
528                                 b = (sbyte)l;
529                         }
530                 } catch (OverflowException) {
531                         failed = true;
532                 }
533                 if (failed)
534                         return 18;
535                 if (b != -128)
536                         return -18;
537
538                 try {
539                         l = 127;
540                         failed = false;
541                         checked {
542                                 b = (sbyte)l;
543                         }
544                 } catch (OverflowException) {
545                         failed = true;
546                 }
547                 if (failed)
548                         return 19;
549                 if (b != 127)
550                         return -19;
551
552                 try {
553                         l = 128;
554                         failed = true;
555                         checked {
556                                 b = (sbyte)l;
557                         }
558                 } catch (OverflowException) {
559                         failed = false;
560                 }
561                 if (failed)
562                         return 20;
563                 if (b != 127)
564                         return -20;
565
566                 try {
567                         l = 0x00000000ffffffff;
568                         failed = true;
569                         checked {
570                                 b = (sbyte)l;
571                         }
572                 } catch (OverflowException) {
573                         failed = false;
574                 }
575                 if (failed)
576                         return 21;
577                 if (b != 127)
578                         return -21;
579
580                 return 0;
581         }
582
583         static int test_0_ushort_cast () {
584                 int a;
585                 long l;
586                 ulong ul;
587                 ushort b;
588                 bool failed;
589
590                 try {
591                         a = System.UInt16.MaxValue;
592                         failed = false;
593                         checked {
594                                 b = (ushort)a;
595                         }
596                 } catch (OverflowException) {
597                         failed = true;
598                 }
599                 if (failed)
600                         return 1;
601
602                 try {
603                         a = 0;
604                         failed = false;
605                         checked {
606                                 b = (ushort)a;
607                         }
608                 } catch (OverflowException) {
609                         failed = true;
610                 }
611                 if (failed)
612                         return 2;
613
614                 try {
615                         a = System.UInt16.MaxValue + 1;
616                         failed = true;
617                         checked {
618                                 b = (ushort)a;
619                         }
620                 } catch (OverflowException) {
621                         failed = false;
622                 }
623                 if (failed)
624                         return 3;
625
626                 try {
627                         a = -1;
628                         failed = true;
629                         checked {
630                                 b = (ushort)a;
631                         }
632                 } catch (OverflowException) {
633                         failed = false;
634                 }
635                 if (failed)
636                         return 4;
637
638                 try {
639                         double d = 0;
640                         failed = false;
641                         checked {
642                                 b = (ushort)d;
643                         }
644                 } catch (OverflowException) {
645                         failed = true;
646                 }
647                 if (failed)
648                         return 5;
649
650                 try {
651                         double d = System.UInt16.MaxValue;
652                         failed = false;
653                         checked {
654                                 b = (ushort)d;
655                         }
656                 } catch (OverflowException) {
657                         failed = true;
658                 }
659                 if (failed)
660                         return 6;
661
662                 try {
663                         double d = -1;
664                         failed = true;
665                         checked {
666                                 b = (ushort)d;
667                         }
668                 } catch (OverflowException) {
669                         failed = false;
670                 }
671                 if (failed)
672                         return 7;
673
674                 try {
675                         double d = System.UInt16.MaxValue + 1.0;
676                         failed = true;
677                         checked {
678                                 b = (ushort)d;
679                         }
680                 } catch (OverflowException) {
681                         failed = false;
682                 }
683                 if (failed)
684                         return 8;
685
686                 try {
687                         l = System.UInt16.MaxValue;
688                         failed = false;
689                         checked {
690                                 b = (ushort)l;
691                         }
692                 } catch (OverflowException) {
693                         failed = true;
694                 }
695                 if (failed)
696                         return 9;
697
698                 try {
699                         l = 0;
700                         failed = false;
701                         checked {
702                                 b = (ushort)l;
703                         }
704                 } catch (OverflowException) {
705                         failed = true;
706                 }
707                 if (failed)
708                         return 10;
709
710                 try {
711                         l = System.UInt16.MaxValue + 1;
712                         failed = true;
713                         checked {
714                                 b = (ushort)l;
715                         }
716                 } catch (OverflowException) {
717                         failed = false;
718                 }
719                 if (failed)
720                         return 11;
721
722                 try {
723                         l = -1;
724                         failed = true;
725                         checked {
726                                 b = (ushort)l;
727                         }
728                 } catch (OverflowException) {
729                         failed = false;
730                 }
731                 if (failed)
732                         return 12;
733
734                 try {
735                         ul = 0xfffff;
736                         failed = true;
737                         checked {
738                                 b = (ushort)ul;
739                         }
740                 } catch (OverflowException) {
741                         failed = false;
742                 }
743                 if (failed)
744                         return 13;
745
746                 return 0;
747         }
748         
749         static int test_0_short_cast () {
750                 int a;
751                 long l;
752                 short b;
753                 bool failed;
754
755                 try {
756                         a = System.UInt16.MaxValue;
757                         failed = true;
758                         checked {
759                                 b = (short)a;
760                         }
761                 } catch (OverflowException) {
762                         failed = false;
763                 }
764                 if (failed)
765                         return 1;
766
767                 try {
768                         a = 0;
769                         failed = false;
770                         checked {
771                                 b = (short)a;
772                         }
773                 } catch (OverflowException) {
774                         failed = true;
775                 }
776                 if (failed)
777                         return 2;
778
779                 try {
780                         a = System.Int16.MaxValue + 1;
781                         failed = true;
782                         checked {
783                                 b = (short)a;
784                         }
785                 } catch (OverflowException) {
786                         failed = false;
787                 }
788                 if (failed)
789                         return 3;
790
791                 try {
792                         a = System.Int16.MinValue - 1;
793                         failed = true;
794                         checked {
795                                 b = (short)a;
796                         }
797                 } catch (OverflowException) {
798                         failed = false;
799                 }
800                 if (failed)
801                         return 4;
802
803                 try {
804                         a = -1;
805                         failed = false;
806                         checked {
807                                 b = (short)a;
808                         }
809                 } catch (OverflowException) {
810                         failed = true;
811                 }
812                 if (failed)
813                         return 5;
814
815                 try {
816                         a = System.Int16.MinValue;
817                         failed = false;
818                         checked {
819                                 b = (short)a;
820                         }
821                 } catch (OverflowException) {
822                         failed = true;
823                 }
824                 if (failed)
825                         return 6;
826
827                 try {
828                         a = System.Int16.MaxValue;
829                         failed = false;
830                         checked {
831                                 b = (short)a;
832                         }
833                 } catch (OverflowException) {
834                         failed = true;
835                 }
836                 if (failed)
837                         return 7;
838
839                 try {
840                         a = System.Int16.MaxValue + 1;
841                         failed = true;
842                         checked {
843                                 b = (short)a;
844                         }
845                 } catch (OverflowException) {
846                         failed = false;
847                 }
848                 if (failed)
849                         return 8;
850
851                 try {
852                         double d = System.Int16.MaxValue;
853                         failed = false;
854                         checked {
855                                 b = (short)d;
856                         }
857                 } catch (OverflowException) {
858                         failed = true;
859                 }
860                 if (failed)
861                         return 9;
862                 
863                 try {
864                         double d = System.Int16.MinValue;
865                         failed = false;
866                         checked {
867                                 b = (short)d;
868                         }
869                 } catch (OverflowException) {
870                         failed = true;
871                 }
872                 if (failed)
873                         return 10;
874                 
875                 try {
876                         double d = System.Int16.MaxValue + 1.0;
877                         failed = true;
878                         checked {
879                                 b = (short)d;
880                         }
881                 } catch (OverflowException) {
882                         failed = false;
883                 }
884                 if (failed)
885                         return 11;
886
887                 try {
888                         double d = System.Int16.MinValue - 1.0;
889                         failed = true;
890                         checked {
891                                 b = (short)d;
892                         }
893                 } catch (OverflowException) {
894                         failed = false;
895                 }
896                 if (failed)
897                         return 12;
898
899                 try {
900                         l = System.Int16.MaxValue + 1;
901                         failed = true;
902                         checked {
903                                 b = (short)l;
904                         }
905                 } catch (OverflowException) {
906                         failed = false;
907                 }
908                 if (failed)
909                         return 13;
910
911                 try {
912                         l = System.Int16.MaxValue;
913                         failed = false;
914                         checked {
915                                 b = (short)l;
916                         }
917                 } catch (OverflowException) {
918                         failed = true;
919                 }
920                 if (failed)
921                         return 14;
922
923                 try {
924                         l = System.Int16.MinValue - 1;
925                         failed = true;
926                         checked {
927                                 b = (short)l;
928                         }
929                 } catch (OverflowException) {
930                         failed = false;
931                 }
932                 if (failed)
933                         return 15;
934
935                 
936                 try {
937                         l = System.Int16.MinValue;
938                         failed = false;
939                         checked {
940                                 b = (short)l;
941                         }
942                 } catch (OverflowException) {
943                         failed = true;
944                 }
945                 if (failed)
946                         return 16;
947
948                 return 0;
949         }
950         
951         static int test_0_int_cast () {
952                 int a;
953                 long l;
954                 bool failed;
955
956                 try {
957                         double d = System.Int32.MaxValue + 1.0;
958                         failed = true;
959                         checked {
960                                 a = (int)d;
961                         }
962                 } catch (OverflowException) {
963                         failed = false;
964                 }
965                 if (failed)
966                         return 1;
967
968                 try {
969                         double d = System.Int32.MaxValue;
970                         failed = false;
971                         checked {
972                                 a = (int)d;
973                         }
974                 } catch (OverflowException) {
975                         failed = true;
976                 }
977                 if (failed)
978                         return 2;
979                 
980
981                 try {
982                         double d = System.Int32.MinValue;
983                         failed = false;                 
984                         checked {
985                                 a = (int)d;
986                         }
987                 } catch (OverflowException) {
988                         failed = true;
989                 }
990                 if (failed)
991                         return 3;
992
993
994                 try {
995                         double d =  System.Int32.MinValue - 1.0;
996                         failed = true;
997                         checked {
998                                 a = (int)d;
999                         }
1000                 } catch (OverflowException) {
1001                         failed = false;
1002                 }
1003                 if (failed)
1004                         return 4;
1005
1006                 try {
1007                         l = System.Int32.MaxValue + (long)1;
1008                         failed = true;
1009                         checked {
1010                                 a = (int)l;
1011                         }
1012                 } catch (OverflowException) {
1013                         failed = false;
1014                 }
1015                 if (failed)
1016                         return 5;
1017
1018                 try {
1019                         l = System.Int32.MaxValue;
1020                         failed = false;
1021                         checked {
1022                                 a = (int)l;
1023                         }
1024                 } catch (OverflowException) {
1025                         failed = true;
1026                 }
1027                 if (failed)
1028                         return 6;
1029                 
1030
1031                 try {
1032                         l = System.Int32.MinValue;
1033                         failed = false;                 
1034                         checked {
1035                                 a = (int)l;
1036                         }
1037                 } catch (OverflowException) {
1038                         failed = true;
1039                 }
1040                 if (failed)
1041                         return 7;
1042
1043
1044                 try {
1045                         l =  System.Int32.MinValue - (long)1;
1046                         failed = true;
1047                         checked {
1048                                 a = (int)l;
1049                         }
1050                 } catch (OverflowException) {
1051                         failed = false;
1052                 }
1053                 if (failed)
1054                         return 8;
1055
1056                 try {
1057                         uint ui = System.UInt32.MaxValue;
1058                         failed = true;
1059                         checked {
1060                                 a = (int)ui;
1061                         }
1062                 }
1063                 catch (OverflowException) {
1064                         failed = false;
1065                 }
1066                 if (failed)
1067                         return 9;
1068
1069                 {
1070                         int i; 
1071                         float f = 1.1f;
1072                         checked {
1073                                 i = (int) f;
1074                         }
1075                 }
1076
1077                 return 0;
1078         }
1079
1080         static int test_0_uint_cast () {
1081                 uint a;
1082                 long l;
1083                 bool failed;
1084
1085                 try {
1086                         double d =  System.UInt32.MaxValue;
1087                         failed = false;
1088                         checked {
1089                                 a = (uint)d;
1090                         }
1091                 } catch (OverflowException) {
1092                         failed = true;
1093                 }
1094                 if (failed)
1095                         return 1;
1096
1097                 try {
1098                         double d = System.UInt32.MaxValue + 1.0;
1099                         failed = true;
1100                         checked {
1101                                 a = (uint)d;
1102                         }
1103                 } catch (OverflowException) {
1104                         failed = false;
1105                 }
1106                 if (failed)
1107                         return 2;
1108
1109                 try {
1110                         double d = System.UInt32.MinValue;
1111                         failed = false;
1112                         checked {
1113                                 a = (uint)d;
1114                         }
1115                 } catch (OverflowException) {
1116                         failed = true;
1117                 }
1118                 if (failed)
1119                         return 3;
1120
1121                 try {
1122                         double d = System.UInt32.MinValue - 1.0;
1123                         failed = true;
1124                         checked {
1125                                 a = (uint)d;
1126                         }
1127                 } catch (OverflowException) {
1128                         failed = false;
1129                 }
1130                 if (failed)
1131                         return 4;
1132                 
1133                 try {
1134                         l =  System.UInt32.MaxValue;
1135                         failed = false;
1136                         checked {
1137                                 a = (uint)l;
1138                         }
1139                 } catch (OverflowException) {
1140                         failed = true;
1141                 }
1142                 if (failed)
1143                         return 5;
1144
1145                 try {
1146                         l = System.UInt32.MaxValue + (long)1;
1147                         failed = true;
1148                         checked {
1149                                 a = (uint)l;
1150                         }
1151                 } catch (OverflowException) {
1152                         failed = false;
1153                 }
1154                 if (failed)
1155                         return 6;
1156
1157                 try {
1158                         l = System.UInt32.MinValue;
1159                         failed = false;
1160                         checked {
1161                                 a = (uint)l;
1162                         }
1163                 } catch (OverflowException) {
1164                         failed = true;
1165                 }
1166                 if (failed)
1167                         return 7;
1168
1169                 try {
1170                         l = System.UInt32.MinValue - (long)1;
1171                         failed = true;
1172                         checked {
1173                                 a = (uint)l;
1174                         }
1175                 } catch (OverflowException) {
1176                         failed = false;
1177                 }
1178                 if (failed)
1179                         return 8;
1180
1181                 try {
1182                         int i = -1;
1183                         failed = true;
1184                         checked {
1185                                 a = (uint)i;
1186                         }
1187                 }
1188                 catch (OverflowException) {
1189                         failed = false;
1190                 }
1191                 if (failed)
1192                         return 9;
1193
1194                 {
1195                         uint i; 
1196                         float f = 1.1f;
1197                         checked {
1198                                 i = (uint) f;
1199                         }
1200                 }
1201                 
1202                 return 0;
1203         }
1204         
1205         static int test_0_long_cast () {
1206                 long a;
1207                 bool failed;
1208
1209                 try {
1210                         double d = System.Int64.MaxValue - 512.0;
1211                         failed = true;
1212                         checked {
1213                                 a = (long)d;
1214                         }
1215                 } catch (OverflowException) {
1216                         failed = false;
1217                 }
1218                 if (failed)
1219                         return 1;
1220
1221                 try {
1222                         double d = System.Int64.MaxValue - 513.0;
1223                         failed = false;
1224                         checked {
1225                                 a = (long)d;
1226                         }
1227                 } catch (OverflowException) {
1228                         failed = true;
1229                 }
1230                 if (failed)
1231                         return 2;
1232                 
1233
1234                 try {
1235                         double d = System.Int64.MinValue - 1024.0;
1236                         failed = false;                 
1237                         checked {
1238                                 a = (long)d;
1239                         }
1240                 } catch (OverflowException) {
1241                         failed = true;
1242                 }
1243                 if (failed)
1244                         return 3;
1245
1246                 try {
1247                         double d = System.Int64.MinValue - 1025.0;
1248                         failed = true;
1249                         checked {
1250                                 a = (long)d;
1251                         }
1252                 } catch (OverflowException) {
1253                         failed = false;
1254                 }
1255                 if (failed)
1256                         return 4;
1257
1258                 {
1259                         long i; 
1260                         float f = 1.1f;
1261                         checked {
1262                                 i = (long) f;
1263                         }
1264                 }
1265
1266                 return 0;
1267         }
1268
1269         static int test_0_ulong_cast () {
1270                 ulong a;
1271                 bool failed;
1272
1273                 try {
1274                         double d = System.UInt64.MaxValue - 1024.0;
1275                         failed = true;
1276                         checked {
1277                                 a = (ulong)d;
1278                         }
1279                 } catch (OverflowException) {
1280                         failed = false;
1281                 }
1282                 if (failed)
1283                         return 1;
1284
1285                 try {
1286                         double d = System.UInt64.MaxValue - 1025.0;
1287                         failed = false;
1288                         checked {
1289                                 a = (ulong)d;
1290                         }
1291                 } catch (OverflowException) {
1292                         failed = true;
1293                 }
1294                 if (failed)
1295                         return 2;
1296                 
1297
1298                 try {
1299                         double d = 0;
1300                         failed = false;                 
1301                         checked {
1302                                 a = (ulong)d;
1303                         }
1304                 } catch (OverflowException) {
1305                         failed = true;
1306                 }
1307                 if (failed)
1308                         return 3;
1309
1310                 try {
1311                         double d = -1;
1312                         failed = true;
1313                         checked {
1314                                 a = (ulong)d;
1315                         }
1316                 } catch (OverflowException) {
1317                         failed = false;
1318                 }
1319                 if (failed)
1320                         return 4;
1321
1322                 {
1323                         ulong i; 
1324                         float f = 1.1f;
1325                         checked {
1326                                 i = (ulong) f;
1327                         }
1328                 }
1329
1330                 try {
1331                         int i = -1;
1332                         failed = true;
1333                         checked {
1334                                 a = (ulong)i;
1335                         }
1336                 }
1337                 catch (OverflowException) {
1338                         failed = false;
1339                 }
1340                 if (failed)
1341                         return 5;
1342
1343                 try {
1344                         int i = Int32.MinValue;
1345                         failed = true;
1346                         checked {
1347                                 a = (ulong)i;
1348                         }
1349                 }
1350                 catch (OverflowException) {
1351                         failed = false;
1352                 }
1353                 if (failed)
1354                         return 6;
1355
1356                 return 0;
1357         }
1358
1359         static int test_0_simple_double_casts () {
1360
1361                 double d = 0xffffffff;
1362
1363                 if ((uint)d != 4294967295)
1364                         return 1;
1365
1366                 d = 0xffffffffffffffff;
1367
1368                 if ((ulong)d != 0)
1369                         return 2;
1370
1371                 if ((ushort)d != 0)
1372                         return 3;
1373                         
1374                 if ((byte)d != 0)
1375                         return 4;
1376                         
1377                 d = 0xffff;
1378
1379                 if ((ushort)d != 0xffff)
1380                         return 5;
1381                 
1382                 if ((byte)d != 0xff)
1383                         return 6;
1384                         
1385                 return 0;
1386         }
1387         
1388         static int test_0_div_zero () {
1389                 int d = 1;
1390                 int q = 0;
1391                 int val;
1392                 bool failed;
1393
1394                 try {
1395                         failed = true;
1396                         val = d / q;
1397                 } catch (DivideByZeroException) {
1398                         failed = false;
1399                 }
1400                 if (failed)
1401                         return 1;
1402
1403                 try {
1404                         failed = true;
1405                         val = d % q;
1406                 } catch (DivideByZeroException) {
1407                         failed = false;
1408                 }
1409                 if (failed)
1410                         return 2;
1411
1412                 try {
1413                         failed = true;
1414                         q = -1;
1415                         d = Int32.MinValue;
1416                         val = d / q;
1417                 } catch (DivideByZeroException) {
1418                         /* wrong exception */
1419                 } catch (ArithmeticException) {
1420                         failed = false;
1421                 }
1422                 if (failed)
1423                         return 3;
1424
1425                 try {
1426                         failed = true;
1427                         q = -1;
1428                         d = Int32.MinValue;
1429                         val = d % q;
1430                 } catch (DivideByZeroException) {
1431                         /* wrong exception */
1432                 } catch (ArithmeticException) {
1433                         failed = false;
1434                 }
1435                 if (failed)
1436                         return 4;
1437
1438                 return 0;
1439         }
1440
1441         static int return_55 () {
1442                 return 55;
1443         }
1444
1445         static int test_0_cfold_div_zero () {
1446                 // Test that constant folding doesn't cause division by zero exceptions
1447                 if (return_55 () != return_55 ()) {
1448                         int d = 1;
1449                         int q = 0;
1450                         int val;                        
1451
1452                         val = d / q;
1453                         val = d % q;
1454
1455                         q = -1;
1456                         d = Int32.MinValue;
1457                         val = d / q;
1458
1459                         q = -1;
1460                         val = d % q;
1461                 }
1462
1463                 return 0;
1464         }
1465
1466         static int test_0_udiv_zero () {
1467                 uint d = 1;
1468                 uint q = 0;
1469                 uint val;
1470                 bool failed;
1471
1472                 try {
1473                         failed = true;
1474                         val = d / q;
1475                 } catch (DivideByZeroException) {
1476                         failed = false;
1477                 }
1478                 if (failed)
1479                         return 1;
1480
1481                 try {
1482                         failed = true;
1483                         val = d % q;
1484                 } catch (DivideByZeroException) {
1485                         failed = false;
1486                 }
1487                 if (failed)
1488                         return 2;
1489
1490                 return 0;
1491         }
1492
1493         static int test_0_long_div_zero () {
1494                 long d = 1;
1495                 long q = 0;
1496                 long val;
1497                 bool failed;
1498
1499                 try {
1500                         failed = true;
1501                         val = d / q;
1502                 } catch (DivideByZeroException) {
1503                         failed = false;
1504                 }
1505                 if (failed)
1506                         return 1;
1507
1508                 try {
1509                         failed = true;
1510                         val = d % q;
1511                 } catch (DivideByZeroException) {
1512                         failed = false;
1513                 }
1514                 if (failed)
1515                         return 2;
1516
1517                 try {
1518                         failed = true;
1519                         q = -1;
1520                         d = Int64.MinValue;
1521                         val = d / q;
1522                 } catch (DivideByZeroException) {
1523                         /* wrong exception */
1524                 } catch (ArithmeticException) {
1525                         failed = false;
1526                 }
1527                 if (failed)
1528                         return 3;
1529
1530                 try {
1531                         failed = true;
1532                         q = -1;
1533                         d = Int64.MinValue;
1534                         val = d % q;
1535                 } catch (DivideByZeroException) {
1536                         /* wrong exception */
1537                 } catch (ArithmeticException) {
1538                         failed = false;
1539                 }
1540                 if (failed)
1541                         return 4;
1542
1543                 return 0;
1544         }
1545
1546         static int test_0_ulong_div_zero () {
1547                 ulong d = 1;
1548                 ulong q = 0;
1549                 ulong val;
1550                 bool failed;
1551
1552                 try {
1553                         failed = true;
1554                         val = d / q;
1555                 } catch (DivideByZeroException) {
1556                         failed = false;
1557                 }
1558                 if (failed)
1559                         return 1;
1560
1561                 try {
1562                         failed = true;
1563                         val = d % q;
1564                 } catch (DivideByZeroException) {
1565                         failed = false;
1566                 }
1567                 if (failed)
1568                         return 2;
1569
1570                 return 0;
1571         }
1572
1573         static int test_0_float_div_zero () {
1574                 double d = 1;
1575                 double q = 0;
1576                 double val;
1577                 bool failed;
1578
1579                 try {
1580                         failed = false;
1581                         val = d / q;
1582                 } catch (DivideByZeroException) {
1583                         failed = true;
1584                 }
1585                 if (failed)
1586                         return 1;
1587
1588                 try {
1589                         failed = false;
1590                         val = d % q;
1591                 } catch (DivideByZeroException) {
1592                         failed = true;
1593                 }
1594                 if (failed)
1595                         return 2;
1596
1597                 return 0;
1598         }
1599
1600         static int test_0_invalid_unbox () {
1601
1602                 int i = 123;
1603                 object o = "Some string";
1604                 int res = 1;
1605                 
1606                 try {
1607                         // Illegal conversion; o contains a string not an int
1608                         i = (int) o;   
1609                 } catch (Exception e) {
1610                         if (i ==123)
1611                                 res = 0;
1612                 }
1613
1614                 return res;
1615         }
1616
1617         // Test that double[] can't be cast to double (bug #46027)
1618         static int test_0_invalid_unbox_arrays () {
1619                 double[] d1 = { 1.0 };
1620                 double[][] d2 = { d1 };
1621                 Array a = d2;
1622
1623                 try {
1624                         foreach (double d in a) {
1625                         }
1626                         return 1;
1627                 }
1628                 catch (InvalidCastException e) {
1629                         return 0;
1630                 }
1631         }
1632
1633         /* bug# 42190, at least mcs generates a leave for the return that
1634          * jumps out of multiple exception clauses: we used to execute just 
1635          * one enclosing finally block.
1636          */
1637         static int finally_level;
1638         static void do_something () {
1639                 int a = 0;
1640                 try {
1641                         try {
1642                                 return;
1643                         } finally {
1644                                 a = 1;
1645                         }
1646                 } finally {
1647                         finally_level++;
1648                 }
1649         }
1650
1651         static int test_2_multiple_finally_clauses () {
1652                 finally_level = 0;
1653                 do_something ();
1654                 if (finally_level == 1)
1655                         return 2;
1656                 return 0;
1657         }
1658
1659         static int test_3_checked_cast_un () {
1660                 ulong i = 0x8000000034000000;
1661                 long j;
1662
1663                 try {
1664                         checked { j = (long)i; }
1665                 } catch (OverflowException) {
1666                         j = 2;
1667                 }
1668
1669                 if (j != 2)
1670                         return 0;
1671                 return 3;
1672         }
1673         
1674         static int test_4_checked_cast () {
1675                 long i;
1676                 ulong j;
1677
1678                 unchecked { i = (long)0x8000000034000000;};
1679                 try {
1680                         checked { j = (ulong)i; }
1681                 } catch (OverflowException) {
1682                         j = 3;
1683                 }
1684
1685                 if (j != 3)
1686                         return 0;
1687                 return 4;
1688         }
1689
1690         static readonly int[] mul_dim_results = new int[] {
1691                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1692                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1693                 2, 0, 2, 1, 2, 8, 
1694                 3, 0, 3, 1, 3, 8, 
1695                 4, 0, 4, 1, 4, 8, 
1696                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1697                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1698                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1699         };
1700
1701         static int test_0_multi_dim_array_access () {
1702                 int [,] a = System.Array.CreateInstance (typeof (int),
1703                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1704                 int x, y;
1705                 int result_idx = 0;
1706                 for (x = 0; x < 8; ++x) {
1707                         for (y = 0; y < 9; ++y) {
1708                                 bool got_ex = false;
1709                                 try {
1710                                         a [x, y] = 1;
1711                                 } catch {
1712                                         got_ex = true;
1713                                 }
1714                                 if (got_ex) {
1715                                         if (result_idx >= mul_dim_results.Length)
1716                                                 return -1;
1717                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1718                                                 return result_idx + 1;
1719                                         }
1720                                         result_idx += 2;
1721                                 }
1722                         }
1723                 }
1724                 if (result_idx == mul_dim_results.Length)
1725                         return 0;
1726                 return 200;
1727         }
1728
1729         static void helper_out_obj (out object o) {
1730                 o = (object)"buddy";
1731         }
1732
1733         static void helper_out_string (out string o) {
1734                 o = "buddy";
1735         }
1736
1737         static int test_2_array_mismatch () {
1738                 string[] a = { "hello", "world" };
1739                 object[] b = a;
1740                 bool passed = false;
1741
1742                 try {
1743                         helper_out_obj (out b [1]);
1744                 } catch (ArrayTypeMismatchException) {
1745                         passed = true;
1746                 }
1747                 if (!passed)
1748                         return 0;
1749                 helper_out_string (out a [1]);
1750                 if (a [1] != "buddy")
1751                         return 1;
1752                 return 2;
1753         }
1754
1755         static int test_0_ovf () {
1756                 int ocount = 0;
1757                 
1758                 checked {
1759
1760                         ocount = 0;
1761                         try {
1762                                 ulong a =  UInt64.MaxValue - 1;
1763                                 ulong t = a++;
1764                         } catch {
1765                                 ocount++;
1766                         }
1767                         if (ocount != 0)
1768                                 return 1;
1769
1770                         ocount = 0;
1771                         try {
1772                                 ulong a =  UInt64.MaxValue;
1773                                 ulong t = a++;
1774                         } catch {
1775                                 ocount++;
1776                         }
1777                         if (ocount != 1)
1778                                 return 2;
1779
1780                         ocount = 0;
1781                         try {
1782                                 long a = Int64.MaxValue - 1;
1783                                 long t = a++;
1784                         } catch {
1785                                 ocount++;
1786                         }
1787                         if (ocount != 0)
1788                                 return 3;
1789
1790                         try {
1791                                 long a = Int64.MaxValue;
1792                                 long t = a++;
1793                         } catch {
1794                                 ocount++;
1795                         }
1796                         if (ocount != 1)
1797                                 return 4;
1798
1799                         ocount = 0;
1800                         try {
1801                                 ulong a = UInt64.MaxValue - 1;
1802                                 ulong t = a++;
1803                         } catch {
1804                                 ocount++;
1805                         }
1806                         if (ocount != 0)
1807                                 return 5;
1808
1809                         try {
1810                                 ulong a = UInt64.MaxValue;
1811                                 ulong t = a++;
1812                         } catch {
1813                                 ocount++;
1814                         }
1815                         if (ocount != 1)
1816                                 return 6;
1817
1818                         ocount = 0;
1819                         try {
1820                                 long a = Int64.MinValue + 1;
1821                                 long t = a--;
1822                         } catch {
1823                                 ocount++;
1824                         }
1825                         if (ocount != 0)
1826                                 return 7;
1827
1828                         ocount = 0;
1829                         try {
1830                                 long a = Int64.MinValue;
1831                                 long t = a--;
1832                         } catch {
1833                                 ocount++;
1834                         }
1835                         if (ocount != 1)
1836                                 return 8;
1837
1838                         ocount = 0;
1839                         try {
1840                                 ulong a = UInt64.MinValue + 1;
1841                                 ulong t = a--;
1842                         } catch {
1843                                 ocount++;
1844                         }
1845                         if (ocount != 0)
1846                                 return 9;
1847
1848                         ocount = 0;
1849                         try {
1850                                 ulong a = UInt64.MinValue;
1851                                 ulong t = a--;
1852                         } catch {
1853                                 ocount++;
1854                         }
1855                         if (ocount != 1)
1856                                 return 10;
1857
1858                         ocount = 0;
1859                         try {
1860                                 int a = Int32.MinValue + 1;
1861                                 int t = a--;
1862                         } catch {
1863                                 ocount++;
1864                         }
1865                         if (ocount != 0)
1866                                 return 11;
1867
1868                         ocount = 0;
1869                         try {
1870                                 int a = Int32.MinValue;
1871                                 int t = a--;
1872                         } catch {
1873                                 ocount++;
1874                         }
1875                         if (ocount != 1)
1876                                 return 12;
1877
1878                         ocount = 0;
1879                         try {
1880                                 uint a = 1;
1881                                 uint t = a--;
1882                         } catch {
1883                                 ocount++;
1884                         }
1885                         if (ocount != 0)
1886                                 return 13;
1887
1888                         ocount = 0;
1889                         try {
1890                                 uint a = 0;
1891                                 uint t = a--;
1892                         } catch {
1893                                 ocount++;
1894                         }
1895                         if (ocount != 1)
1896                                 return 14;
1897
1898                         ocount = 0;
1899                         try {
1900                                 sbyte a = 126;
1901                                 sbyte t = a++;
1902                         } catch {
1903                                 ocount++;
1904                         }
1905                         if (ocount != 0)
1906                                 return 15;
1907
1908                         ocount = 0;
1909                         try {
1910                                 sbyte a = 127;
1911                                 sbyte t = a++;
1912                         } catch {
1913                                 ocount++;
1914                         }
1915                         if (ocount != 1)
1916                                 return 16;
1917
1918                         ocount = 0;
1919                         try {
1920                         } catch {
1921                                 ocount++;
1922                         }
1923                         if (ocount != 0)
1924                                 return 17;
1925
1926                         ocount = 0;
1927                         try {
1928                                 int a = 1 << 29;
1929                                 int t = a*2;
1930                         } catch {
1931                                 ocount++;
1932                         }
1933                         if (ocount != 0)
1934                                 return 18;
1935
1936                         ocount = 0;
1937                         try {
1938                                 int a = 1 << 30;
1939                                 int t = a*2;
1940                         } catch {
1941                                 ocount++;
1942                         }
1943                         if (ocount != 1)
1944                                 return 19;
1945
1946                         ocount = 0;
1947                         try {
1948                                 ulong a = 0xffffffffff;
1949                                 ulong t = a*0x0ffffff;
1950                         } catch {
1951                                 ocount++;
1952                         }
1953                         if (ocount != 0)
1954                                 return 20;
1955
1956                         ocount = 0;
1957                         try {
1958                                 ulong a = 0xffffffffff;
1959                                 ulong t = a*0x0fffffff;
1960                         } catch {
1961                                 ocount++;
1962                         }
1963                         if (ocount != 1)
1964                                 return 21;
1965
1966                         ocount = 0;
1967                         try {
1968                                 long a = Int64.MinValue;
1969                                 long b = 10;
1970                                 long v = a * b;
1971                         } catch {
1972                                 ocount ++;
1973                         }
1974                         if (ocount != 1)
1975                                 return 22;
1976
1977                         ocount = 0;
1978                         try {
1979                                 long a = 10;
1980                                 long b = Int64.MinValue;
1981                                 long v = a * b;
1982                         } catch {
1983                                 ocount ++;
1984                         }
1985                         if (ocount != 1)
1986                                 return 23;
1987                 }
1988                 
1989                 return 0;
1990         }
1991
1992         class Broken {
1993                 static int i;
1994
1995                 static Broken () {
1996                         throw new Exception ("Ugh!");
1997                 }
1998         
1999                 public static int DoSomething () {
2000                         return i;
2001                 }
2002         }
2003
2004         static int test_0_exception_in_cctor () {
2005                 try {
2006                         Broken.DoSomething ();
2007                 }
2008                 catch (TypeInitializationException) {
2009                         // This will only happen once even if --regression is used
2010                 }
2011                 return 0;
2012         }
2013
2014         static int test_5_regalloc () {
2015                 int i = 0;
2016
2017                 try {
2018                         for (i = 0; i < 10; ++i) {
2019                                 if (i == 5)
2020                                         throw new Exception ();
2021                         }
2022                 }
2023                 catch (Exception) {
2024                         if (i != 5)
2025                                 return i;
2026                 }
2027
2028                 // Check that variables written in catch clauses are volatile
2029                 int j = 0;
2030                 try {
2031                         throw new Exception ();
2032                 }
2033                 catch (Exception) {
2034                         j = 5;
2035                 }
2036                 if (j != 5)
2037                         return 6;
2038
2039                 int k = 0;
2040                 try {
2041                         try {
2042                                 throw new Exception ();
2043                         }
2044                         finally {
2045                                 k = 5;
2046                         }
2047                 }
2048                 catch (Exception) {
2049                 }
2050                 if (k != 5)
2051                         return 7;
2052
2053                 return i;
2054         }
2055
2056         /* MarshalByRefObject prevents the methods from being inlined */
2057         class ThrowClass : MarshalByRefObject {
2058                 public static void rethrow1 () {
2059                         throw new Exception ();
2060                 }
2061
2062                 public static void rethrow2 () {
2063                         rethrow1 ();
2064                 }
2065         }
2066
2067         static int test_0_rethrow_stacktrace () {
2068                 // Check that rethrowing an exception preserves the original stack trace
2069                 try {
2070                         try {
2071                                 ThrowClass.rethrow2 ();
2072                         }
2073                         catch (Exception ex) {
2074                                 throw;
2075                         }
2076                 }
2077                 catch (Exception ex) {
2078                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2079                                 return 0;
2080                 }
2081
2082                 return 1;
2083         }
2084         
2085         interface IFace {}
2086         class Face : IFace {}
2087                 
2088         static int test_1_array_mismatch_2 () {
2089                 try {
2090                         object [] o = new Face [1];
2091                         o [0] = 1;
2092                         return 0;
2093                 } catch (ArrayTypeMismatchException) {
2094                         return 1;
2095                 }
2096         }
2097         
2098         static int test_1_array_mismatch_3 () {
2099                 try {
2100                         object [] o = new IFace [1];
2101                         o [0] = 1;
2102                         return 0;
2103                 } catch (ArrayTypeMismatchException) {
2104                         return 1;
2105                 }
2106         }
2107         
2108         static int test_1_array_mismatch_4 () {
2109                 try {
2110                         object [][] o = new Face [5] [];
2111                         o [0] = new object [5];
2112                         
2113                         return 0;
2114                 } catch (ArrayTypeMismatchException) {
2115                         return 1;
2116                 }
2117         }
2118
2119         static int test_0_array_size () {
2120                 bool failed;
2121
2122                 try {
2123                         failed = true;
2124                         int[] mem1 = new int [Int32.MaxValue];
2125                 }
2126                 catch (OutOfMemoryException e) {
2127                         failed = false;
2128                 }
2129                 if (failed)
2130                         return 1;
2131
2132                 try {
2133                         failed = true;
2134                         int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2135                 }
2136                 catch (OutOfMemoryException e) {
2137                         failed = false;
2138                 }
2139                 if (failed)
2140                         return 2;
2141
2142                 return 0;
2143         }
2144 }
2145