In corlib/System.Runtime.InteropServices:
[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  * public 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         public static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         public 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         public 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         public 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         public 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         public 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         public 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                         ulong ul = 128;
568                         failed = true;
569                         checked {
570                                 b = (sbyte)ul;
571                         }
572                 }
573                 catch (OverflowException) {
574                         failed = false;
575                 }
576                 if (failed)
577                         return 21;
578                 if (b != 127)
579                         return -21;
580
581                 return 0;
582         }
583
584         public static int test_0_ushort_cast () {
585                 int a;
586                 long l;
587                 ulong ul;
588                 ushort b;
589                 bool failed;
590
591                 try {
592                         a = System.UInt16.MaxValue;
593                         failed = false;
594                         checked {
595                                 b = (ushort)a;
596                         }
597                 } catch (OverflowException) {
598                         failed = true;
599                 }
600                 if (failed)
601                         return 1;
602
603                 try {
604                         a = 0;
605                         failed = false;
606                         checked {
607                                 b = (ushort)a;
608                         }
609                 } catch (OverflowException) {
610                         failed = true;
611                 }
612                 if (failed)
613                         return 2;
614
615                 try {
616                         a = System.UInt16.MaxValue + 1;
617                         failed = true;
618                         checked {
619                                 b = (ushort)a;
620                         }
621                 } catch (OverflowException) {
622                         failed = false;
623                 }
624                 if (failed)
625                         return 3;
626
627                 try {
628                         a = -1;
629                         failed = true;
630                         checked {
631                                 b = (ushort)a;
632                         }
633                 } catch (OverflowException) {
634                         failed = false;
635                 }
636                 if (failed)
637                         return 4;
638
639                 try {
640                         double d = 0;
641                         failed = false;
642                         checked {
643                                 b = (ushort)d;
644                         }
645                 } catch (OverflowException) {
646                         failed = true;
647                 }
648                 if (failed)
649                         return 5;
650
651                 try {
652                         double d = System.UInt16.MaxValue;
653                         failed = false;
654                         checked {
655                                 b = (ushort)d;
656                         }
657                 } catch (OverflowException) {
658                         failed = true;
659                 }
660                 if (failed)
661                         return 6;
662
663                 try {
664                         double d = -1;
665                         failed = true;
666                         checked {
667                                 b = (ushort)d;
668                         }
669                 } catch (OverflowException) {
670                         failed = false;
671                 }
672                 if (failed)
673                         return 7;
674
675                 try {
676                         double d = System.UInt16.MaxValue + 1.0;
677                         failed = true;
678                         checked {
679                                 b = (ushort)d;
680                         }
681                 } catch (OverflowException) {
682                         failed = false;
683                 }
684                 if (failed)
685                         return 8;
686
687                 try {
688                         l = System.UInt16.MaxValue;
689                         failed = false;
690                         checked {
691                                 b = (ushort)l;
692                         }
693                 } catch (OverflowException) {
694                         failed = true;
695                 }
696                 if (failed)
697                         return 9;
698
699                 try {
700                         l = 0;
701                         failed = false;
702                         checked {
703                                 b = (ushort)l;
704                         }
705                 } catch (OverflowException) {
706                         failed = true;
707                 }
708                 if (failed)
709                         return 10;
710
711                 try {
712                         l = System.UInt16.MaxValue + 1;
713                         failed = true;
714                         checked {
715                                 b = (ushort)l;
716                         }
717                 } catch (OverflowException) {
718                         failed = false;
719                 }
720                 if (failed)
721                         return 11;
722
723                 try {
724                         l = -1;
725                         failed = true;
726                         checked {
727                                 b = (ushort)l;
728                         }
729                 } catch (OverflowException) {
730                         failed = false;
731                 }
732                 if (failed)
733                         return 12;
734
735                 try {
736                         ul = 0xfffff;
737                         failed = true;
738                         checked {
739                                 b = (ushort)ul;
740                         }
741                 } catch (OverflowException) {
742                         failed = false;
743                 }
744                 if (failed)
745                         return 13;
746
747                 return 0;
748         }
749         
750         public static int test_0_short_cast () {
751                 int a;
752                 long l;
753                 short b;
754                 bool failed;
755
756                 try {
757                         a = System.UInt16.MaxValue;
758                         failed = true;
759                         checked {
760                                 b = (short)a;
761                         }
762                 } catch (OverflowException) {
763                         failed = false;
764                 }
765                 if (failed)
766                         return 1;
767
768                 try {
769                         a = 0;
770                         failed = false;
771                         checked {
772                                 b = (short)a;
773                         }
774                 } catch (OverflowException) {
775                         failed = true;
776                 }
777                 if (failed)
778                         return 2;
779
780                 try {
781                         a = System.Int16.MaxValue + 1;
782                         failed = true;
783                         checked {
784                                 b = (short)a;
785                         }
786                 } catch (OverflowException) {
787                         failed = false;
788                 }
789                 if (failed)
790                         return 3;
791
792                 try {
793                         a = System.Int16.MinValue - 1;
794                         failed = true;
795                         checked {
796                                 b = (short)a;
797                         }
798                 } catch (OverflowException) {
799                         failed = false;
800                 }
801                 if (failed)
802                         return 4;
803
804                 try {
805                         a = -1;
806                         failed = false;
807                         checked {
808                                 b = (short)a;
809                         }
810                 } catch (OverflowException) {
811                         failed = true;
812                 }
813                 if (failed)
814                         return 5;
815
816                 try {
817                         a = System.Int16.MinValue;
818                         failed = false;
819                         checked {
820                                 b = (short)a;
821                         }
822                 } catch (OverflowException) {
823                         failed = true;
824                 }
825                 if (failed)
826                         return 6;
827
828                 try {
829                         a = System.Int16.MaxValue;
830                         failed = false;
831                         checked {
832                                 b = (short)a;
833                         }
834                 } catch (OverflowException) {
835                         failed = true;
836                 }
837                 if (failed)
838                         return 7;
839
840                 try {
841                         a = System.Int16.MaxValue + 1;
842                         failed = true;
843                         checked {
844                                 b = (short)a;
845                         }
846                 } catch (OverflowException) {
847                         failed = false;
848                 }
849                 if (failed)
850                         return 8;
851
852                 try {
853                         double d = System.Int16.MaxValue;
854                         failed = false;
855                         checked {
856                                 b = (short)d;
857                         }
858                 } catch (OverflowException) {
859                         failed = true;
860                 }
861                 if (failed)
862                         return 9;
863                 
864                 try {
865                         double d = System.Int16.MinValue;
866                         failed = false;
867                         checked {
868                                 b = (short)d;
869                         }
870                 } catch (OverflowException) {
871                         failed = true;
872                 }
873                 if (failed)
874                         return 10;
875                 
876                 try {
877                         double d = System.Int16.MaxValue + 1.0;
878                         failed = true;
879                         checked {
880                                 b = (short)d;
881                         }
882                 } catch (OverflowException) {
883                         failed = false;
884                 }
885                 if (failed)
886                         return 11;
887
888                 try {
889                         double d = System.Int16.MinValue - 1.0;
890                         failed = true;
891                         checked {
892                                 b = (short)d;
893                         }
894                 } catch (OverflowException) {
895                         failed = false;
896                 }
897                 if (failed)
898                         return 12;
899
900                 try {
901                         l = System.Int16.MaxValue + 1;
902                         failed = true;
903                         checked {
904                                 b = (short)l;
905                         }
906                 } catch (OverflowException) {
907                         failed = false;
908                 }
909                 if (failed)
910                         return 13;
911
912                 try {
913                         l = System.Int16.MaxValue;
914                         failed = false;
915                         checked {
916                                 b = (short)l;
917                         }
918                 } catch (OverflowException) {
919                         failed = true;
920                 }
921                 if (failed)
922                         return 14;
923
924                 try {
925                         l = System.Int16.MinValue - 1;
926                         failed = true;
927                         checked {
928                                 b = (short)l;
929                         }
930                 } catch (OverflowException) {
931                         failed = false;
932                 }
933                 if (failed)
934                         return 15;
935
936                 
937                 try {
938                         l = System.Int16.MinValue;
939                         failed = false;
940                         checked {
941                                 b = (short)l;
942                         }
943                 } catch (OverflowException) {
944                         failed = true;
945                 }
946                 if (failed)
947                         return 16;
948
949                 try {
950                         l = 0x00000000ffffffff;
951                         failed = true;
952                         checked {
953                                 b = (short)l;
954                         }
955                 } catch (OverflowException) {
956                         failed = false;
957                 }
958                 if (failed)
959                         return 17;
960
961                 try {
962                         ulong ul = 32768;
963                         failed = true;
964                         checked {
965                                 b = (short)ul;
966                         }
967                 } catch (OverflowException) {
968                         failed = false;
969                 }
970                 if (failed)
971                         return 18;
972
973                 return 0;
974         }
975         
976         public static int test_0_int_cast () {
977                 int a;
978                 long l;
979                 bool failed;
980
981                 try {
982                         double d = System.Int32.MaxValue + 1.0;
983                         failed = true;
984                         checked {
985                                 a = (int)d;
986                         }
987                 } catch (OverflowException) {
988                         failed = false;
989                 }
990                 if (failed)
991                         return 1;
992
993                 try {
994                         double d = System.Int32.MaxValue;
995                         failed = false;
996                         checked {
997                                 a = (int)d;
998                         }
999                 } catch (OverflowException) {
1000                         failed = true;
1001                 }
1002                 if (failed)
1003                         return 2;
1004                 
1005
1006                 try {
1007                         double d = System.Int32.MinValue;
1008                         failed = false;                 
1009                         checked {
1010                                 a = (int)d;
1011                         }
1012                 } catch (OverflowException) {
1013                         failed = true;
1014                 }
1015                 if (failed)
1016                         return 3;
1017
1018
1019                 try {
1020                         double d =  System.Int32.MinValue - 1.0;
1021                         failed = true;
1022                         checked {
1023                                 a = (int)d;
1024                         }
1025                 } catch (OverflowException) {
1026                         failed = false;
1027                 }
1028                 if (failed)
1029                         return 4;
1030
1031                 try {
1032                         l = System.Int32.MaxValue + (long)1;
1033                         failed = true;
1034                         checked {
1035                                 a = (int)l;
1036                         }
1037                 } catch (OverflowException) {
1038                         failed = false;
1039                 }
1040                 if (failed)
1041                         return 5;
1042
1043                 try {
1044                         l = System.Int32.MaxValue;
1045                         failed = false;
1046                         checked {
1047                                 a = (int)l;
1048                         }
1049                 } catch (OverflowException) {
1050                         failed = true;
1051                 }
1052                 if (failed)
1053                         return 6;
1054                 
1055
1056                 try {
1057                         l = System.Int32.MinValue;
1058                         failed = false;                 
1059                         checked {
1060                                 a = (int)l;
1061                         }
1062                 } catch (OverflowException) {
1063                         failed = true;
1064                 }
1065                 if (failed)
1066                         return 7;
1067
1068
1069                 try {
1070                         l =  System.Int32.MinValue - (long)1;
1071                         failed = true;
1072                         checked {
1073                                 a = (int)l;
1074                         }
1075                 } catch (OverflowException) {
1076                         failed = false;
1077                 }
1078                 if (failed)
1079                         return 8;
1080
1081                 try {
1082                         uint ui = System.UInt32.MaxValue;
1083                         failed = true;
1084                         checked {
1085                                 a = (int)ui;
1086                         }
1087                 }
1088                 catch (OverflowException) {
1089                         failed = false;
1090                 }
1091                 if (failed)
1092                         return 9;
1093
1094                 try {
1095                         ulong ul = (long)(System.Int32.MaxValue) + 1;
1096                         failed = true;
1097                         checked {
1098                                 a = (int)ul;
1099                         }
1100                 }
1101                 catch (OverflowException) {
1102                         failed = false;
1103                 }
1104                 if (failed)
1105                         return 10;
1106
1107                 try {
1108                         ulong ul = UInt64.MaxValue;
1109                         failed = true;
1110                         checked {
1111                                 a = (int)ul;
1112                         }
1113                 }
1114                 catch (OverflowException) {
1115                         failed = false;
1116                 }
1117                 if (failed)
1118                         return 11;
1119
1120                 {
1121                         int i; 
1122                         float f = 1.1f;
1123                         checked {
1124                                 i = (int) f;
1125                         }
1126                 }
1127
1128                 return 0;
1129         }
1130
1131         public static int test_0_uint_cast () {
1132                 uint a;
1133                 long l;
1134                 bool failed;
1135
1136                 try {
1137                         double d =  System.UInt32.MaxValue;
1138                         failed = false;
1139                         checked {
1140                                 a = (uint)d;
1141                         }
1142                 } catch (OverflowException) {
1143                         failed = true;
1144                 }
1145                 if (failed)
1146                         return 1;
1147
1148                 try {
1149                         double d = System.UInt32.MaxValue + 1.0;
1150                         failed = true;
1151                         checked {
1152                                 a = (uint)d;
1153                         }
1154                 } catch (OverflowException) {
1155                         failed = false;
1156                 }
1157                 if (failed)
1158                         return 2;
1159
1160                 try {
1161                         double d = System.UInt32.MinValue;
1162                         failed = false;
1163                         checked {
1164                                 a = (uint)d;
1165                         }
1166                 } catch (OverflowException) {
1167                         failed = true;
1168                 }
1169                 if (failed)
1170                         return 3;
1171
1172                 try {
1173                         double d = System.UInt32.MinValue - 1.0;
1174                         failed = true;
1175                         checked {
1176                                 a = (uint)d;
1177                         }
1178                 } catch (OverflowException) {
1179                         failed = false;
1180                 }
1181                 if (failed)
1182                         return 4;
1183                 
1184                 try {
1185                         l =  System.UInt32.MaxValue;
1186                         failed = false;
1187                         checked {
1188                                 a = (uint)l;
1189                         }
1190                 } catch (OverflowException) {
1191                         failed = true;
1192                 }
1193                 if (failed)
1194                         return 5;
1195
1196                 try {
1197                         l = System.UInt32.MaxValue + (long)1;
1198                         failed = true;
1199                         checked {
1200                                 a = (uint)l;
1201                         }
1202                 } catch (OverflowException) {
1203                         failed = false;
1204                 }
1205                 if (failed)
1206                         return 6;
1207
1208                 try {
1209                         l = System.UInt32.MinValue;
1210                         failed = false;
1211                         checked {
1212                                 a = (uint)l;
1213                         }
1214                 } catch (OverflowException) {
1215                         failed = true;
1216                 }
1217                 if (failed)
1218                         return 7;
1219
1220                 try {
1221                         l = System.UInt32.MinValue - (long)1;
1222                         failed = true;
1223                         checked {
1224                                 a = (uint)l;
1225                         }
1226                 } catch (OverflowException) {
1227                         failed = false;
1228                 }
1229                 if (failed)
1230                         return 8;
1231
1232                 try {
1233                         int i = -1;
1234                         failed = true;
1235                         checked {
1236                                 a = (uint)i;
1237                         }
1238                 }
1239                 catch (OverflowException) {
1240                         failed = false;
1241                 }
1242                 if (failed)
1243                         return 9;
1244
1245                 {
1246                         uint i; 
1247                         float f = 1.1f;
1248                         checked {
1249                                 i = (uint) f;
1250                         }
1251                 }
1252                 
1253                 return 0;
1254         }
1255         
1256         public static int test_0_long_cast () {
1257
1258                 /*
1259                  * These tests depend on properties of x86 fp arithmetic so they won't work
1260                  * on other platforms.
1261                  */
1262                 /*
1263                 long a;
1264                 bool failed;
1265
1266                 try {
1267                         double d = System.Int64.MaxValue - 512.0;
1268                         failed = true;
1269                         checked {
1270                                 a = (long)d;
1271                         }
1272                 } catch (OverflowException) {
1273                         failed = false;
1274                 }
1275                 if (failed)
1276                         return 1;
1277
1278                 try {
1279                         double d = System.Int64.MaxValue - 513.0;
1280                         failed = false;
1281                         checked {
1282                                 a = (long)d;
1283                         }
1284                 } catch (OverflowException) {
1285                         failed = true;
1286                 }
1287                 if (failed)
1288                         return 2;
1289                 
1290                 try {
1291                         double d = System.Int64.MinValue - 1024.0;
1292                         failed = false;                 
1293                         checked {
1294                                 a = (long)d;
1295                         }
1296                 } catch (OverflowException) {
1297                         failed = true;
1298                 }
1299                 if (failed)
1300                         return 3;
1301
1302                 try {
1303                         double d = System.Int64.MinValue - 1025.0;
1304                         failed = true;
1305                         checked {
1306                                 a = (long)d;
1307                         }
1308                 } catch (OverflowException) {
1309                         failed = false;
1310                 }
1311                 if (failed)
1312                         return 4;
1313                 */
1314
1315                 {
1316                         long i; 
1317                         float f = 1.1f;
1318                         checked {
1319                                 i = (long) f;
1320                         }
1321                 }
1322
1323                 return 0;
1324         }
1325
1326         public static int test_0_ulong_cast () {
1327                 ulong a;
1328                 bool failed;
1329
1330                 /*
1331                  * These tests depend on properties of x86 fp arithmetic so they won't work
1332                  * on other platforms.
1333                  */
1334
1335                 /*
1336                 try {
1337                         double d = System.UInt64.MaxValue - 1024.0;
1338                         failed = true;
1339                         checked {
1340                                 a = (ulong)d;
1341                         }
1342                 } catch (OverflowException) {
1343                         failed = false;
1344                 }
1345                 if (failed)
1346                         return 1;
1347
1348                 try {
1349                         double d = System.UInt64.MaxValue - 1025.0;
1350                         failed = false;
1351                         checked {
1352                                 a = (ulong)d;
1353                         }
1354                 } catch (OverflowException) {
1355                         failed = true;
1356                 }
1357                 if (failed)
1358                         return 2;
1359                 */      
1360
1361                 try {
1362                         double d = 0;
1363                         failed = false;                 
1364                         checked {
1365                                 a = (ulong)d;
1366                         }
1367                 } catch (OverflowException) {
1368                         failed = true;
1369                 }
1370                 if (failed)
1371                         return 3;
1372
1373                 try {
1374                         double d = -1;
1375                         failed = true;
1376                         checked {
1377                                 a = (ulong)d;
1378                         }
1379                 } catch (OverflowException) {
1380                         failed = false;
1381                 }
1382                 if (failed)
1383                         return 4;
1384
1385                 {
1386                         ulong i; 
1387                         float f = 1.1f;
1388                         checked {
1389                                 i = (ulong) f;
1390                         }
1391                 }
1392
1393                 try {
1394                         int i = -1;
1395                         failed = true;
1396                         checked {
1397                                 a = (ulong)i;
1398                         }
1399                 }
1400                 catch (OverflowException) {
1401                         failed = false;
1402                 }
1403                 if (failed)
1404                         return 5;
1405
1406                 try {
1407                         int i = Int32.MinValue;
1408                         failed = true;
1409                         checked {
1410                                 a = (ulong)i;
1411                         }
1412                 }
1413                 catch (OverflowException) {
1414                         failed = false;
1415                 }
1416                 if (failed)
1417                         return 6;
1418
1419                 return 0;
1420         }
1421
1422         public static int test_0_simple_double_casts () {
1423
1424                 double d = 0xffffffff;
1425
1426                 if ((uint)d != 4294967295)
1427                         return 1;
1428
1429                 /*
1430                  * These tests depend on properties of x86 fp arithmetic so they won't work
1431                  * on other platforms.
1432                  */
1433                 /*
1434                 d = 0xffffffffffffffff;
1435
1436                 if ((ulong)d != 0)
1437                         return 2;
1438
1439                 if ((ushort)d != 0)
1440                         return 3;
1441                         
1442                 if ((byte)d != 0)
1443                         return 4;
1444                 */
1445                         
1446                 d = 0xffff;
1447
1448                 if ((ushort)d != 0xffff)
1449                         return 5;
1450                 
1451                 if ((byte)d != 0xff)
1452                         return 6;
1453                         
1454                 return 0;
1455         }
1456         
1457         public static int test_0_div_zero () {
1458                 int d = 1;
1459                 int q = 0;
1460                 int val;
1461                 bool failed;
1462
1463                 try {
1464                         failed = true;
1465                         val = d / q;
1466                 } catch (DivideByZeroException) {
1467                         failed = false;
1468                 }
1469                 if (failed)
1470                         return 1;
1471
1472                 try {
1473                         failed = true;
1474                         val = d % q;
1475                 } catch (DivideByZeroException) {
1476                         failed = false;
1477                 }
1478                 if (failed)
1479                         return 2;
1480
1481                 try {
1482                         failed = true;
1483                         q = -1;
1484                         d = Int32.MinValue;
1485                         val = d / q;
1486                 } catch (DivideByZeroException) {
1487                         /* wrong exception */
1488                 } catch (ArithmeticException) {
1489                         failed = false;
1490                 }
1491                 if (failed)
1492                         return 3;
1493
1494                 try {
1495                         failed = true;
1496                         q = -1;
1497                         d = Int32.MinValue;
1498                         val = d % q;
1499                 } catch (DivideByZeroException) {
1500                         /* wrong exception */
1501                 } catch (ArithmeticException) {
1502                         failed = false;
1503                 }
1504                 if (failed)
1505                         return 4;
1506
1507                 return 0;
1508         }
1509
1510         public static int return_55 () {
1511                 return 55;
1512         }
1513
1514         public static int test_0_cfold_div_zero () {
1515                 // Test that constant folding doesn't cause division by zero exceptions
1516                 if (return_55 () != return_55 ()) {
1517                         int d = 1;
1518                         int q = 0;
1519                         int val;                        
1520
1521                         val = d / q;
1522                         val = d % q;
1523
1524                         q = -1;
1525                         d = Int32.MinValue;
1526                         val = d / q;
1527
1528                         q = -1;
1529                         val = d % q;
1530                 }
1531
1532                 return 0;
1533         }
1534
1535         public static int test_0_udiv_zero () {
1536                 uint d = 1;
1537                 uint q = 0;
1538                 uint val;
1539                 bool failed;
1540
1541                 try {
1542                         failed = true;
1543                         val = d / q;
1544                 } catch (DivideByZeroException) {
1545                         failed = false;
1546                 }
1547                 if (failed)
1548                         return 1;
1549
1550                 try {
1551                         failed = true;
1552                         val = d % q;
1553                 } catch (DivideByZeroException) {
1554                         failed = false;
1555                 }
1556                 if (failed)
1557                         return 2;
1558
1559                 return 0;
1560         }
1561
1562         public static int test_0_long_div_zero () {
1563                 long d = 1;
1564                 long q = 0;
1565                 long val;
1566                 bool failed;
1567
1568                 try {
1569                         failed = true;
1570                         val = d / q;
1571                 } catch (DivideByZeroException) {
1572                         failed = false;
1573                 }
1574                 if (failed)
1575                         return 1;
1576
1577                 try {
1578                         failed = true;
1579                         val = d % q;
1580                 } catch (DivideByZeroException) {
1581                         failed = false;
1582                 }
1583                 if (failed)
1584                         return 2;
1585
1586                 try {
1587                         failed = true;
1588                         q = -1;
1589                         d = Int64.MinValue;
1590                         val = d / q;
1591                 } catch (DivideByZeroException) {
1592                         /* wrong exception */
1593                 } catch (ArithmeticException) {
1594                         failed = false;
1595                 }
1596                 if (failed)
1597                         return 3;
1598
1599                 try {
1600                         failed = true;
1601                         q = -1;
1602                         d = Int64.MinValue;
1603                         val = d % q;
1604                 } catch (DivideByZeroException) {
1605                         /* wrong exception */
1606                 } catch (ArithmeticException) {
1607                         failed = false;
1608                 }
1609                 if (failed)
1610                         return 4;
1611
1612                 return 0;
1613         }
1614
1615         public static int test_0_ulong_div_zero () {
1616                 ulong d = 1;
1617                 ulong q = 0;
1618                 ulong val;
1619                 bool failed;
1620
1621                 try {
1622                         failed = true;
1623                         val = d / q;
1624                 } catch (DivideByZeroException) {
1625                         failed = false;
1626                 }
1627                 if (failed)
1628                         return 1;
1629
1630                 try {
1631                         failed = true;
1632                         val = d % q;
1633                 } catch (DivideByZeroException) {
1634                         failed = false;
1635                 }
1636                 if (failed)
1637                         return 2;
1638
1639                 return 0;
1640         }
1641
1642         public static int test_0_float_div_zero () {
1643                 double d = 1;
1644                 double q = 0;
1645                 double val;
1646                 bool failed;
1647
1648                 try {
1649                         failed = false;
1650                         val = d / q;
1651                 } catch (DivideByZeroException) {
1652                         failed = true;
1653                 }
1654                 if (failed)
1655                         return 1;
1656
1657                 try {
1658                         failed = false;
1659                         val = d % q;
1660                 } catch (DivideByZeroException) {
1661                         failed = true;
1662                 }
1663                 if (failed)
1664                         return 2;
1665
1666                 return 0;
1667         }
1668
1669         public static int test_0_invalid_unbox () {
1670
1671                 int i = 123;
1672                 object o = "Some string";
1673                 int res = 1;
1674                 
1675                 try {
1676                         // Illegal conversion; o contains a string not an int
1677                         i = (int) o;   
1678                 } catch (Exception e) {
1679                         if (i ==123)
1680                                 res = 0;
1681                 }
1682
1683                 return res;
1684         }
1685
1686         // Test that double[] can't be cast to double (bug #46027)
1687         public static int test_0_invalid_unbox_arrays () {
1688                 double[] d1 = { 1.0 };
1689                 double[][] d2 = { d1 };
1690                 Array a = d2;
1691
1692                 try {
1693                         foreach (double d in a) {
1694                         }
1695                         return 1;
1696                 }
1697                 catch (InvalidCastException e) {
1698                         return 0;
1699                 }
1700         }
1701
1702         /* bug# 42190, at least mcs generates a leave for the return that
1703          * jumps out of multiple exception clauses: we used to execute just 
1704          * one enclosing finally block.
1705          */
1706         public static int finally_level;
1707         static void do_something () {
1708                 int a = 0;
1709                 try {
1710                         try {
1711                                 return;
1712                         } finally {
1713                                 a = 1;
1714                         }
1715                 } finally {
1716                         finally_level++;
1717                 }
1718         }
1719
1720         public static int test_2_multiple_finally_clauses () {
1721                 finally_level = 0;
1722                 do_something ();
1723                 if (finally_level == 1)
1724                         return 2;
1725                 return 0;
1726         }
1727
1728         public static int test_3_checked_cast_un () {
1729                 ulong i = 0x8000000034000000;
1730                 long j;
1731
1732                 try {
1733                         checked { j = (long)i; }
1734                 } catch (OverflowException) {
1735                         j = 2;
1736                 }
1737
1738                 if (j != 2)
1739                         return 0;
1740                 return 3;
1741         }
1742         
1743         public static int test_4_checked_cast () {
1744                 long i;
1745                 ulong j;
1746
1747                 unchecked { i = (long)0x8000000034000000;};
1748                 try {
1749                         checked { j = (ulong)i; }
1750                 } catch (OverflowException) {
1751                         j = 3;
1752                 }
1753
1754                 if (j != 3)
1755                         return 0;
1756                 return 4;
1757         }
1758
1759         static readonly int[] mul_dim_results = new int[] {
1760                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1761                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1762                 2, 0, 2, 1, 2, 8, 
1763                 3, 0, 3, 1, 3, 8, 
1764                 4, 0, 4, 1, 4, 8, 
1765                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1766                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1767                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1768         };
1769
1770         public static int test_0_multi_dim_array_access () {
1771                 int [,] a = System.Array.CreateInstance (typeof (int),
1772                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1773                 int x, y;
1774                 int result_idx = 0;
1775                 for (x = 0; x < 8; ++x) {
1776                         for (y = 0; y < 9; ++y) {
1777                                 bool got_ex = false;
1778                                 try {
1779                                         a [x, y] = 1;
1780                                 } catch {
1781                                         got_ex = true;
1782                                 }
1783                                 if (got_ex) {
1784                                         if (result_idx >= mul_dim_results.Length)
1785                                                 return -1;
1786                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1787                                                 return result_idx + 1;
1788                                         }
1789                                         result_idx += 2;
1790                                 }
1791                         }
1792                 }
1793                 if (result_idx == mul_dim_results.Length)
1794                         return 0;
1795                 return 200;
1796         }
1797
1798         static void helper_out_obj (out object o) {
1799                 o = (object)"buddy";
1800         }
1801
1802         static void helper_out_string (out string o) {
1803                 o = "buddy";
1804         }
1805
1806         public static int test_2_array_mismatch () {
1807                 string[] a = { "hello", "world" };
1808                 object[] b = a;
1809                 bool passed = false;
1810
1811                 try {
1812                         helper_out_obj (out b [1]);
1813                 } catch (ArrayTypeMismatchException) {
1814                         passed = true;
1815                 }
1816                 if (!passed)
1817                         return 0;
1818                 helper_out_string (out a [1]);
1819                 if (a [1] != "buddy")
1820                         return 1;
1821                 return 2;
1822         }
1823
1824         public static int test_0_ovf1 () {
1825                 int exception = 0;
1826                 
1827                 checked {
1828                         try {
1829                                 ulong a =  UInt64.MaxValue - 1;
1830                                 ulong t = a++;
1831                         } catch {
1832                                 exception = 1;
1833                         }
1834                 }
1835                 return exception;
1836         }
1837
1838         public static int test_1_ovf2 () {
1839                 int exception = 0;
1840
1841                 checked {
1842                         try {
1843                                 ulong a =  UInt64.MaxValue;
1844                                 ulong t = a++;
1845                         } catch {
1846                                 exception = 1;
1847                         }
1848                 }
1849                 return exception;
1850         }
1851
1852         public static int test_0_ovf3 () {
1853                 int exception = 0;
1854
1855                 long a = Int64.MaxValue - 1;
1856                 checked {
1857                         try {
1858                                 long t = a++;
1859                         } catch {
1860                                 exception = 1;
1861                         }
1862                 }
1863                 return exception;
1864         }
1865
1866         public static int test_1_ovf4 () {
1867                 int exception = 0;
1868
1869                 long a = Int64.MaxValue;
1870                 checked {
1871                         try {
1872                                 long t = a++;
1873                         } catch {
1874                                 exception = 1;
1875                         }
1876                 }
1877                 return exception;
1878         }
1879
1880         public static int test_0_ovf5 () {
1881                 int exception = 0;
1882
1883                 ulong a = UInt64.MaxValue - 1;
1884                 checked {
1885                         try {
1886                                 ulong t = a++;
1887                         } catch {
1888                                 exception = 1;
1889                         }
1890                 }
1891                 return exception;
1892         }
1893
1894         public static int test_1_ovf6 () {
1895                 int exception = 0;
1896
1897                 ulong a = UInt64.MaxValue;
1898                 checked {
1899                         try {
1900                                 ulong t = a++;
1901                         } catch {
1902                                 exception = 1;
1903                         }
1904                 }
1905                 return exception;
1906         }
1907
1908         public static int test_0_ovf7 () {
1909                 int exception = 0;
1910
1911                 long a = Int64.MinValue + 1;
1912                 checked {
1913                         try {
1914                                 long t = a--;
1915                         } catch {
1916                                 exception = 1;
1917                         }
1918                 }
1919                 return 0;
1920         }
1921
1922         public static int test_1_ovf8 () {
1923                 int exception = 0;
1924
1925                 long a = Int64.MinValue;
1926                 checked {
1927                         try {
1928                                 long t = a--;
1929                         } catch {
1930                                 exception = 1;
1931                         }
1932                 }
1933                 return exception;
1934         }
1935
1936         public static int test_0_ovf9 () {
1937                 int exception = 0;
1938
1939                 ulong a = UInt64.MinValue + 1;
1940                 checked {
1941                         try {
1942                                 ulong t = a--;
1943                         } catch {
1944                                 exception = 1;
1945                         }
1946                 }
1947                 return exception;
1948         }
1949
1950         public static int test_1_ovf10 () {
1951                 int exception = 0;
1952
1953                 ulong a = UInt64.MinValue;
1954                 checked {
1955                         try {
1956                                 ulong t = a--;
1957                         } catch {
1958                                 exception = 1;
1959                         }
1960                 }
1961                 return exception;
1962         }
1963
1964         public static int test_0_ovf11 () {
1965                 int exception = 0;
1966
1967                 int a = Int32.MinValue + 1;
1968                 checked {
1969                         try {
1970                                 int t = a--;
1971                         } catch {
1972                                 exception = 1;
1973                         }
1974                 }
1975                 return exception;
1976         }
1977
1978         public static int test_1_ovf12 () {
1979                 int exception = 0;
1980
1981                 int a = Int32.MinValue;
1982                 checked {
1983                         try {
1984                                 int t = a--;
1985                         } catch {
1986                                 exception = 1;
1987                         }
1988                 }
1989                 return exception;
1990         }
1991
1992         public static int test_0_ovf13 () {
1993                 int exception = 0;
1994
1995                 uint a = 1;
1996                 checked {
1997                         try {
1998                                 uint t = a--;
1999                         } catch {
2000                                 exception = 1;
2001                         }
2002                 }
2003                 return exception;
2004         }
2005
2006         public static int test_1_ovf14 () {
2007                 int exception = 0;
2008
2009                 uint a = 0;
2010                 checked {
2011                         try {
2012                                 uint t = a--;
2013                         } catch {
2014                                 exception = 1;
2015                         }
2016                 }
2017                 return exception;
2018         }
2019
2020         public static int test_0_ovf15 () {
2021                 int exception = 0;
2022
2023                 sbyte a = 126;
2024                 checked {
2025                         try {
2026                                 sbyte t = a++;
2027                         } catch {
2028                                 exception = 1;
2029                         }
2030                 }
2031                 return exception;
2032         }
2033
2034         public static int test_1_ovf16 () {
2035                 int exception = 0;
2036
2037                 sbyte a = 127;
2038                 checked {
2039                         try {
2040                                 sbyte t = a++;
2041                         } catch {
2042                                 exception = 1;
2043                         }
2044                 }
2045                 return exception;
2046         }
2047
2048         public static int test_0_ovf17 () {
2049                 int exception = 0;
2050
2051                 checked {
2052                         try {
2053                         } catch {
2054                                 exception = 1;
2055                         }
2056                 }
2057                 return exception;
2058         }
2059
2060         public static int test_0_ovf18 () {
2061                 int exception = 0;
2062
2063                 int a = 1 << 29;
2064                 checked {
2065                         try {
2066                                 int t = a*2;
2067                         } catch {
2068                                 exception = 1;
2069                         }
2070                 }
2071                 return exception;
2072         }
2073
2074         public static int test_1_ovf19 () {
2075                 int exception = 0;
2076
2077                 int a = 1 << 30;
2078                 checked {
2079                         try {
2080                                 int t = a*2;
2081                         } catch {
2082                                 exception = 1;
2083                         }
2084                 }
2085                 return exception;
2086         }
2087
2088         public static int test_0_ovf20 () {
2089                 int exception = 0;
2090
2091                 checked {
2092                         try {
2093                                 ulong a = 0xffffffffff;
2094                                 ulong t = a*0x0ffffff;
2095                         } catch {
2096                                 exception = 1;
2097                         }
2098                 }
2099                 return exception;
2100         }
2101
2102         public static int test_1_ovf21 () {
2103                 int exception = 0;
2104
2105                 ulong a = 0xffffffffff;
2106                 checked {
2107                         try {
2108                                 ulong t = a*0x0fffffff;
2109                         } catch {
2110                                 exception = 1;
2111                         }
2112                 }
2113                 return exception;
2114         }
2115
2116         public static int test_1_ovf22 () {
2117                 int exception = 0;
2118
2119                 long a = Int64.MinValue;
2120                 long b = 10;
2121                 checked {
2122                         try {
2123                                 long v = a * b;
2124                         } catch {
2125                                 exception = 1;
2126                         }
2127                 }
2128                 return exception;
2129         }
2130
2131         public static int test_1_ovf23 () {
2132                 int exception = 0;
2133
2134                 long a = 10;
2135                 long b = Int64.MinValue;
2136                 checked {
2137                         try {
2138                                 long v = a * b;
2139                         } catch {
2140                                 exception = 1;
2141                         }
2142                 }
2143                 return exception;
2144         }
2145
2146         class Broken {
2147                 public static int i;
2148
2149                 static Broken () {
2150                         throw new Exception ("Ugh!");
2151                 }
2152         
2153                 public static int DoSomething () {
2154                         return i;
2155                 }
2156         }
2157
2158         public static int test_0_exception_in_cctor () {
2159                 try {
2160                         Broken.DoSomething ();
2161                 }
2162                 catch (TypeInitializationException) {
2163                         // This will only happen once even if --regression is used
2164                 }
2165                 return 0;
2166         }
2167
2168         public static int test_5_regalloc () {
2169                 int i = 0;
2170
2171                 try {
2172                         for (i = 0; i < 10; ++i) {
2173                                 if (i == 5)
2174                                         throw new Exception ();
2175                         }
2176                 }
2177                 catch (Exception) {
2178                         if (i != 5)
2179                                 return i;
2180                 }
2181
2182                 // Check that variables written in catch clauses are volatile
2183                 int j = 0;
2184                 try {
2185                         throw new Exception ();
2186                 }
2187                 catch (Exception) {
2188                         j = 5;
2189                 }
2190                 if (j != 5)
2191                         return 6;
2192
2193                 int k = 0;
2194                 try {
2195                         try {
2196                                 throw new Exception ();
2197                         }
2198                         finally {
2199                                 k = 5;
2200                         }
2201                 }
2202                 catch (Exception) {
2203                 }
2204                 if (k != 5)
2205                         return 7;
2206
2207                 return i;
2208         }
2209
2210         /* MarshalByRefObject prevents the methods from being inlined */
2211         class ThrowClass : MarshalByRefObject {
2212                 public static void rethrow1 () {
2213                         throw new Exception ();
2214                 }
2215
2216                 public static void rethrow2 () {
2217                         rethrow1 ();
2218                 }
2219         }
2220
2221         public static int test_0_rethrow_stacktrace () {
2222                 // Check that rethrowing an exception preserves the original stack trace
2223                 try {
2224                         try {
2225                                 ThrowClass.rethrow2 ();
2226                         }
2227                         catch (Exception ex) {
2228                                 // Check that each catch clause has its own exception variable
2229                                 // If not, the throw below will overwrite the exception used
2230                                 // by the rethrow
2231                                 try {
2232                                         throw new DivideByZeroException ();
2233                                 }
2234                                 catch (Exception foo) {
2235                                 }
2236
2237                                 throw;
2238                         }
2239                 }
2240                 catch (Exception ex) {
2241                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2242                                 return 0;
2243                 }
2244
2245                 return 1;
2246         }
2247         
2248         interface IFace {}
2249         class Face : IFace {}
2250                 
2251         public static int test_1_array_mismatch_2 () {
2252                 try {
2253                         object [] o = new Face [1];
2254                         o [0] = 1;
2255                         return 0;
2256                 } catch (ArrayTypeMismatchException) {
2257                         return 1;
2258                 }
2259         }
2260         
2261         public static int test_1_array_mismatch_3 () {
2262                 try {
2263                         object [] o = new IFace [1];
2264                         o [0] = 1;
2265                         return 0;
2266                 } catch (ArrayTypeMismatchException) {
2267                         return 1;
2268                 }
2269         }
2270         
2271         public static int test_1_array_mismatch_4 () {
2272                 try {
2273                         object [][] o = new Face [5] [];
2274                         o [0] = new object [5];
2275                         
2276                         return 0;
2277                 } catch (ArrayTypeMismatchException) {
2278                         return 1;
2279                 }
2280         }
2281
2282         public static int test_0_array_size () {
2283                 bool failed;
2284
2285                 try {
2286                         failed = true;
2287                         int[] mem1 = new int [Int32.MaxValue];
2288                 }
2289                 catch (OutOfMemoryException e) {
2290                         failed = false;
2291                 }
2292                 if (failed)
2293                         return 1;
2294
2295                 try {
2296                         failed = true;
2297                         int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2298                 }
2299                 catch (OutOfMemoryException e) {
2300                         failed = false;
2301                 }
2302                 if (failed)
2303                         return 2;
2304
2305                 return 0;
2306         }
2307
2308         struct S {
2309                 int i, j, k, l, m, n;
2310         }
2311
2312         static IntPtr[] addr;
2313
2314         static unsafe void throw_func (int i, S s) {
2315                 addr [i] = new IntPtr (&i);
2316                 throw new Exception ();
2317         }
2318
2319         /* Test that arguments are correctly popped off the stack during unwinding */
2320         public static int test_0_stack_unwind () {
2321                 addr = new IntPtr [1000];
2322                 S s = new S ();
2323                 for (int j = 0; j < 1000; j++) {
2324                         try {
2325                                 throw_func (j, s);
2326                         }
2327                         catch (Exception) {
2328                         }
2329                 }
2330                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2331         }
2332
2333         public static int test_0_regress_73242 () {
2334                 int [] arr = new int [10];
2335                 for (int i = 0; i < 10; ++i)
2336                         arr [i] = 0;
2337                 try {
2338                         throw new Exception ();
2339                 }
2340                 catch {
2341                 }
2342                 return 0;
2343     }
2344
2345         public static int test_0_nullref () {
2346                 try {
2347                         Array foo = null;
2348                         foo.Clone();
2349                 } catch (NullReferenceException e) {
2350                         return 0;
2351                 }
2352                 return 1;
2353         }
2354
2355         // bug #78633
2356         public static int test_0_throw_to_branch_opt_outer_clause () {
2357                 int i = 0;
2358
2359                 try {
2360                         try {
2361                                 string [] files = new string[1];
2362
2363                                 string s = files[2];
2364                         } finally {
2365                                 i ++;
2366                         }
2367                 } catch {
2368                 }
2369
2370                 return (i == 1) ? 0 : 1;
2371         }               
2372 }
2373