aa5ca277ac544e5e57bdeffbbfd454a0c95de9ab
[mono.git] / mono / mini / exceptions.cs
1 using System;
2 using System.Reflection;
3 using System.Runtime.CompilerServices;
4
5 /*
6  * Regression tests for the mono JIT.
7  *
8  * Each test needs to be of the form:
9  *
10  * public static int test_<result>_<name> ();
11  *
12  * where <result> is an integer (the value that needs to be returned by
13  * the method to make it pass.
14  * <name> is a user-displayed name used to identify the test.
15  *
16  * The tests can be driven in two ways:
17  * *) running the program directly: Main() uses reflection to find and invoke
18  *      the test methods (this is useful mostly to check that the tests are correct)
19  * *) with the --regression switch of the jit (this is the preferred way since
20  *      all the tests will be run with optimizations on and off)
21  *
22  * The reflection logic could be moved to a .dll since we need at least another
23  * regression test file written in IL code to have better control on how
24  * the IL code looks.
25  */
26
27 #if __MOBILE__
28 class ExceptionTests
29 #else
30 class Tests
31 #endif
32 {
33
34 #if !__MOBILE__
35         public static int Main (string[] args) {
36                 return TestDriver.RunTests (typeof (Tests), args);
37         }
38 #endif
39
40         public static int test_0_catch () {
41                 Exception x = new Exception ();
42                 
43                 try {
44                         throw x;
45                 } catch (Exception e) {
46                         if (e == x)
47                                 return 0;
48                 }
49                 return 1;
50         }
51
52         public static int test_0_finally_without_exc () {
53                 int x;
54                 
55                 try {
56                         x = 1;
57                 } catch (Exception e) {
58                         x = 2;
59                 } finally {
60                         x = 0;
61                 }
62                 
63                 return x;
64         }
65
66         public static int test_0_finally () {
67                 int x = 1;
68                 
69                 try {
70                         throw new Exception ();
71                 } catch (Exception e) {
72                         x = 2;
73                 } finally {
74                         x = 0;
75                 }
76                 return x;
77         }
78
79         public static int test_0_nested_finally () {
80                 int a;
81
82                 try {
83                         a = 1;
84                 } finally {
85                         try {
86                                 a = 2;
87                         } finally {
88                                 a = 0;
89                         }
90                 }
91                 return a;
92         }               
93
94         public static int test_0_byte_cast () {
95                 int a;
96                 long l;
97                 ulong ul;
98                 byte b = 0;
99                 bool failed;
100
101                 try {
102                         a = 255;
103                         failed = false;
104                         checked {
105                                 b = (byte)a;
106                         }
107                 } catch (OverflowException) {
108                         failed = true;
109                 }
110                 if (failed)
111                         return 1;
112                 if (b != 255)
113                         return -1;
114
115                 try {
116                         a = 0;
117                         failed = false;
118                         checked {
119                                 b = (byte)a;
120                         }
121                 } catch (OverflowException) {
122                         failed = true;
123                 }
124                 if (failed)
125                         return 2;
126                 if (b != 0)
127                         return -2;
128
129
130                 try {
131                         a = 256;
132                         failed = true;
133                         checked {
134                                 b = (byte)a;
135                         }
136                 } catch (OverflowException) {
137                         failed = false;
138                 }
139                 if (failed)
140                         return 3;
141                 if (b != 0)
142                         return -3;
143
144                 try {
145                         a = -1;
146                         failed = true;
147                         checked {
148                                 b = (byte)a;
149                         }
150                 } catch (OverflowException) {
151                         failed = false;
152                 }
153                 if (failed)
154                         return 4;
155                 if (b != 0)
156                         return -4;
157
158                 try {
159                         double d = 0;
160                         failed = false;
161                         checked {
162                                 b = (byte)d;
163                         }
164                 } catch (OverflowException) {
165                         failed = true;
166                 }
167                 if (failed)
168                         return 5;
169                 if (b != 0)
170                         return -5;
171                 
172                 try {
173                         double d = -1;
174                         failed = true;
175                         checked {
176                                 b = (byte)d;
177                         }
178                 } catch (OverflowException) {
179                         failed = false;
180                 }
181                 if (failed)
182                         return 6;
183                 if (b != 0)
184                         return -6;
185
186                 try {
187                         double d = 255;
188                         failed = false;
189                         checked {
190                                 b = (byte)d;
191                         }
192                 } catch (OverflowException) {
193                         failed = true;
194                 }
195                 if (failed)
196                         return 7;
197                 if (b != 255)
198                         return -7;
199
200                 try {
201                         double d = 256;
202                         failed = true;
203                         checked {
204                                 b = (byte)d;
205                         }
206                 } catch (OverflowException) {
207                         failed = false;
208                 }
209                 if (failed)
210                         return 8;
211                 if (b != 255)
212                         return -8;
213
214                 try {
215                         l = 255;
216                         failed = false;
217                         checked {
218                                 b = (byte)l;
219                         }
220                 } catch (OverflowException) {
221                         failed = true;
222                 }
223                 if (failed)
224                         return 9;
225                 if (b != 255)
226                         return -9;
227
228                 try {
229                         l = 0;
230                         failed = false;
231                         checked {
232                                 b = (byte)l;
233                         }
234                 } catch (OverflowException) {
235                         failed = true;
236                 }
237                 if (failed)
238                         return 10;
239                 if (b != 0)
240                         return -10;
241
242                 try {
243                         l = 256;
244                         failed = true;
245                         checked {
246                                 b = (byte)l;
247                         }
248                 } catch (OverflowException) {
249                         failed = false;
250                 }
251                 if (failed)
252                         return 11;
253                 if (b != 0)
254                         return -11;
255
256                 try {
257                         l = -1;
258                         failed = true;
259                         checked {
260                                 b = (byte)l;
261                         }
262                 } catch (OverflowException) {
263                         failed = false;
264                 }
265                 if (failed)
266                         return 12;
267                 if (b != 0)
268                         return -12;
269
270                 try {
271                         ul = 256;
272                         failed = true;
273                         checked {
274                                 b = (byte)ul;
275                         }
276                 }
277                 catch (OverflowException) {
278                         failed = false;
279                 }
280                 if (failed)
281                         return 13;
282                 if (b != 0)
283                         return -13;
284
285                 return 0;
286         }
287         
288         public static int test_0_sbyte_cast () {
289                 int a;
290                 long l;
291                 sbyte b = 0;
292                 bool failed;
293
294                 try {
295                         a = 255;
296                         failed = true;
297                         checked {
298                                 b = (sbyte)a;
299                         }
300                 } catch (OverflowException) {
301                         failed = false;
302                 }
303                 if (failed)
304                         return 1;
305                 if (b != 0)
306                         return -1;
307
308                 try {
309                         a = 0;
310                         failed = false;
311                         checked {
312                                 b = (sbyte)a;
313                         }
314                 } catch (OverflowException) {
315                         failed = true;
316                 }
317                 if (failed)
318                         return 2;
319                 if (b != 0)
320                         return -2;
321
322                 try {
323                         a = 256;
324                         failed = true;
325                         checked {
326                                 b = (sbyte)a;
327                         }
328                 } catch (OverflowException) {
329                         failed = false;
330                 }
331                 if (failed)
332                         return 3;
333                 if (b != 0)
334                         return -3;
335
336                 try {
337                         a = -129;
338                         failed = true;
339                         checked {
340                                 b = (sbyte)a;
341                         }
342                 } catch (OverflowException) {
343                         failed = false;
344                 }
345                 if (failed)
346                         return 4;
347                 if (b != 0)
348                         return -4;
349
350                 try {
351                         a = -1;
352                         failed = false;
353                         checked {
354                                 b = (sbyte)a;
355                         }
356                 } catch (OverflowException) {
357                         failed = true;
358                 }
359                 if (failed)
360                         return 5;
361                 if (b != -1)
362                         return -5;
363
364                 try {
365                         a = -128;
366                         failed = false;
367                         checked {
368                                 b = (sbyte)a;
369                         }
370                 } catch (OverflowException) {
371                         failed = true;
372                 }
373                 if (failed)
374                         return 6;
375                 if (b != -128)
376                         return -6;
377
378                 try {
379                         a = 127;
380                         failed = false;
381                         checked {
382                                 b = (sbyte)a;
383                         }
384                 } catch (OverflowException) {
385                         failed = true;
386                 }
387                 if (failed)
388                         return 7;
389                 if (b != 127)
390                         return -7;
391
392                 try {
393                         a = 128;
394                         failed = true;
395                         checked {
396                                 b = (sbyte)a;
397                         }
398                 } catch (OverflowException) {
399                         failed = false;
400                 }
401                 if (failed)
402                         return 8;
403                 if (b != 127)
404                         return -8;
405
406                 try {
407                         double d = 127;
408                         failed = false;
409                         checked {
410                                 b = (sbyte)d;
411                         }
412                 } catch (OverflowException) {
413                         failed = true;
414                 }
415                 if (failed)
416                         return 9;
417                 if (b != 127)
418                         return -9;
419
420                 try {
421                         double d = -128;
422                         failed = false;
423                         checked {
424                                 b = (sbyte)d;
425                         }
426                 } catch (OverflowException) {
427                         failed = true;
428                 }
429                 if (failed)
430                         return 10;
431                 if (b != -128)
432                         return -10;
433
434                 try {
435                         double d = 128;
436                         failed = true;
437                         checked {
438                                 b = (sbyte)d;
439                         }
440                 } catch (OverflowException) {
441                         failed = false;
442                 }
443                 if (failed)
444                         return 11;
445                 if (b != -128)
446                         return -11;
447
448                 try {
449                         double d = -129;
450                         failed = true;
451                         checked {
452                                 b = (sbyte)d;
453                         }
454                 } catch (OverflowException) {
455                         failed = false;
456                 }
457                 if (failed)
458                         return 12;
459                 if (b != -128)
460                         return -12;
461
462                 try {
463                         l = 255;
464                         failed = true;
465                         checked {
466                                 b = (sbyte)l;
467                         }
468                 } catch (OverflowException) {
469                         failed = false;
470                 }
471                 if (failed)
472                         return 13;
473                 if (b != -128)
474                         return -13;
475
476                 try {
477                         l = 0;
478                         failed = false;
479                         checked {
480                                 b = (sbyte)l;
481                         }
482                 } catch (OverflowException) {
483                         failed = true;
484                 }
485                 if (failed)
486                         return 14;
487                 if (b != 0)
488                         return -14;
489
490                 try {
491                         l = 256;
492                         failed = true;
493                         checked {
494                                 b = (sbyte)l;
495                         }
496                 } catch (OverflowException) {
497                         failed = false;
498                 }
499                 if (failed)
500                         return 15;
501                 if (b != 0)
502                         return -15;
503
504                 try {
505                         l = -129;
506                         failed = true;
507                         checked {
508                                 b = (sbyte)l;
509                         }
510                 } catch (OverflowException) {
511                         failed = false;
512                 }
513                 if (failed)
514                         return 16;
515                 if (b != 0)
516                         return -16;
517
518                 try {
519                         l = -1;
520                         failed = false;
521                         checked {
522                                 b = (sbyte)l;
523                         }
524                 } catch (OverflowException) {
525                         failed = true;
526                 }
527                 if (failed)
528                         return 17;
529                 if (b != -1)
530                         return -17;
531
532                 try {
533                         l = -128;
534                         failed = false;
535                         checked {
536                                 b = (sbyte)l;
537                         }
538                 } catch (OverflowException) {
539                         failed = true;
540                 }
541                 if (failed)
542                         return 18;
543                 if (b != -128)
544                         return -18;
545
546                 try {
547                         l = 127;
548                         failed = false;
549                         checked {
550                                 b = (sbyte)l;
551                         }
552                 } catch (OverflowException) {
553                         failed = true;
554                 }
555                 if (failed)
556                         return 19;
557                 if (b != 127)
558                         return -19;
559
560                 try {
561                         l = 128;
562                         failed = true;
563                         checked {
564                                 b = (sbyte)l;
565                         }
566                 } catch (OverflowException) {
567                         failed = false;
568                 }
569                 if (failed)
570                         return 20;
571                 if (b != 127)
572                         return -20;
573
574                 try {
575                         ulong ul = 128;
576                         failed = true;
577                         checked {
578                                 b = (sbyte)ul;
579                         }
580                 }
581                 catch (OverflowException) {
582                         failed = false;
583                 }
584                 if (failed)
585                         return 21;
586                 if (b != 127)
587                         return -21;
588
589                 return 0;
590         }
591
592         public static int test_0_ushort_cast () {
593                 int a;
594                 long l;
595                 ulong ul;
596                 ushort b;
597                 bool failed;
598
599                 try {
600                         a = System.UInt16.MaxValue;
601                         failed = false;
602                         checked {
603                                 b = (ushort)a;
604                         }
605                 } catch (OverflowException) {
606                         failed = true;
607                 }
608                 if (failed)
609                         return 1;
610
611                 try {
612                         a = 0;
613                         failed = false;
614                         checked {
615                                 b = (ushort)a;
616                         }
617                 } catch (OverflowException) {
618                         failed = true;
619                 }
620                 if (failed)
621                         return 2;
622
623                 try {
624                         a = System.UInt16.MaxValue + 1;
625                         failed = true;
626                         checked {
627                                 b = (ushort)a;
628                         }
629                 } catch (OverflowException) {
630                         failed = false;
631                 }
632                 if (failed)
633                         return 3;
634
635                 try {
636                         a = -1;
637                         failed = true;
638                         checked {
639                                 b = (ushort)a;
640                         }
641                 } catch (OverflowException) {
642                         failed = false;
643                 }
644                 if (failed)
645                         return 4;
646
647                 try {
648                         double d = 0;
649                         failed = false;
650                         checked {
651                                 b = (ushort)d;
652                         }
653                 } catch (OverflowException) {
654                         failed = true;
655                 }
656                 if (failed)
657                         return 5;
658
659                 try {
660                         double d = System.UInt16.MaxValue;
661                         failed = false;
662                         checked {
663                                 b = (ushort)d;
664                         }
665                 } catch (OverflowException) {
666                         failed = true;
667                 }
668                 if (failed)
669                         return 6;
670
671                 try {
672                         double d = -1;
673                         failed = true;
674                         checked {
675                                 b = (ushort)d;
676                         }
677                 } catch (OverflowException) {
678                         failed = false;
679                 }
680                 if (failed)
681                         return 7;
682
683                 try {
684                         double d = System.UInt16.MaxValue + 1.0;
685                         failed = true;
686                         checked {
687                                 b = (ushort)d;
688                         }
689                 } catch (OverflowException) {
690                         failed = false;
691                 }
692                 if (failed)
693                         return 8;
694
695                 try {
696                         l = System.UInt16.MaxValue;
697                         failed = false;
698                         checked {
699                                 b = (ushort)l;
700                         }
701                 } catch (OverflowException) {
702                         failed = true;
703                 }
704                 if (failed)
705                         return 9;
706
707                 try {
708                         l = 0;
709                         failed = false;
710                         checked {
711                                 b = (ushort)l;
712                         }
713                 } catch (OverflowException) {
714                         failed = true;
715                 }
716                 if (failed)
717                         return 10;
718
719                 try {
720                         l = System.UInt16.MaxValue + 1;
721                         failed = true;
722                         checked {
723                                 b = (ushort)l;
724                         }
725                 } catch (OverflowException) {
726                         failed = false;
727                 }
728                 if (failed)
729                         return 11;
730
731                 try {
732                         l = -1;
733                         failed = true;
734                         checked {
735                                 b = (ushort)l;
736                         }
737                 } catch (OverflowException) {
738                         failed = false;
739                 }
740                 if (failed)
741                         return 12;
742
743                 try {
744                         ul = 0xfffff;
745                         failed = true;
746                         checked {
747                                 b = (ushort)ul;
748                         }
749                 } catch (OverflowException) {
750                         failed = false;
751                 }
752                 if (failed)
753                         return 13;
754
755                 return 0;
756         }
757         
758         public static int test_0_short_cast () {
759                 int a;
760                 long l;
761                 short b;
762                 bool failed;
763
764                 try {
765                         a = System.UInt16.MaxValue;
766                         failed = true;
767                         checked {
768                                 b = (short)a;
769                         }
770                 } catch (OverflowException) {
771                         failed = false;
772                 }
773                 if (failed)
774                         return 1;
775
776                 try {
777                         a = 0;
778                         failed = false;
779                         checked {
780                                 b = (short)a;
781                         }
782                 } catch (OverflowException) {
783                         failed = true;
784                 }
785                 if (failed)
786                         return 2;
787
788                 try {
789                         a = System.Int16.MaxValue + 1;
790                         failed = true;
791                         checked {
792                                 b = (short)a;
793                         }
794                 } catch (OverflowException) {
795                         failed = false;
796                 }
797                 if (failed)
798                         return 3;
799
800                 try {
801                         a = System.Int16.MinValue - 1;
802                         failed = true;
803                         checked {
804                                 b = (short)a;
805                         }
806                 } catch (OverflowException) {
807                         failed = false;
808                 }
809                 if (failed)
810                         return 4;
811
812                 try {
813                         a = -1;
814                         failed = false;
815                         checked {
816                                 b = (short)a;
817                         }
818                 } catch (OverflowException) {
819                         failed = true;
820                 }
821                 if (failed)
822                         return 5;
823
824                 try {
825                         a = System.Int16.MinValue;
826                         failed = false;
827                         checked {
828                                 b = (short)a;
829                         }
830                 } catch (OverflowException) {
831                         failed = true;
832                 }
833                 if (failed)
834                         return 6;
835
836                 try {
837                         a = System.Int16.MaxValue;
838                         failed = false;
839                         checked {
840                                 b = (short)a;
841                         }
842                 } catch (OverflowException) {
843                         failed = true;
844                 }
845                 if (failed)
846                         return 7;
847
848                 try {
849                         a = System.Int16.MaxValue + 1;
850                         failed = true;
851                         checked {
852                                 b = (short)a;
853                         }
854                 } catch (OverflowException) {
855                         failed = false;
856                 }
857                 if (failed)
858                         return 8;
859
860                 try {
861                         double d = System.Int16.MaxValue;
862                         failed = false;
863                         checked {
864                                 b = (short)d;
865                         }
866                 } catch (OverflowException) {
867                         failed = true;
868                 }
869                 if (failed)
870                         return 9;
871                 
872                 try {
873                         double d = System.Int16.MinValue;
874                         failed = false;
875                         checked {
876                                 b = (short)d;
877                         }
878                 } catch (OverflowException) {
879                         failed = true;
880                 }
881                 if (failed)
882                         return 10;
883                 
884                 try {
885                         double d = System.Int16.MaxValue + 1.0;
886                         failed = true;
887                         checked {
888                                 b = (short)d;
889                         }
890                 } catch (OverflowException) {
891                         failed = false;
892                 }
893                 if (failed)
894                         return 11;
895
896                 try {
897                         double d = System.Int16.MinValue - 1.0;
898                         failed = true;
899                         checked {
900                                 b = (short)d;
901                         }
902                 } catch (OverflowException) {
903                         failed = false;
904                 }
905                 if (failed)
906                         return 12;
907
908                 try {
909                         l = System.Int16.MaxValue + 1;
910                         failed = true;
911                         checked {
912                                 b = (short)l;
913                         }
914                 } catch (OverflowException) {
915                         failed = false;
916                 }
917                 if (failed)
918                         return 13;
919
920                 try {
921                         l = System.Int16.MaxValue;
922                         failed = false;
923                         checked {
924                                 b = (short)l;
925                         }
926                 } catch (OverflowException) {
927                         failed = true;
928                 }
929                 if (failed)
930                         return 14;
931
932                 try {
933                         l = System.Int16.MinValue - 1;
934                         failed = true;
935                         checked {
936                                 b = (short)l;
937                         }
938                 } catch (OverflowException) {
939                         failed = false;
940                 }
941                 if (failed)
942                         return 15;
943
944                 
945                 try {
946                         l = System.Int16.MinValue;
947                         failed = false;
948                         checked {
949                                 b = (short)l;
950                         }
951                 } catch (OverflowException) {
952                         failed = true;
953                 }
954                 if (failed)
955                         return 16;
956
957                 try {
958                         l = 0x00000000ffffffff;
959                         failed = true;
960                         checked {
961                                 b = (short)l;
962                         }
963                 } catch (OverflowException) {
964                         failed = false;
965                 }
966                 if (failed)
967                         return 17;
968
969                 try {
970                         ulong ul = 32768;
971                         failed = true;
972                         checked {
973                                 b = (short)ul;
974                         }
975                 } catch (OverflowException) {
976                         failed = false;
977                 }
978                 if (failed)
979                         return 18;
980
981                 return 0;
982         }
983         
984         public static int test_0_int_cast () {
985                 int a;
986                 long l;
987                 bool failed;
988
989                 try {
990                         double d = System.Int32.MaxValue + 1.0;
991                         failed = true;
992                         checked {
993                                 a = (int)d;
994                         }
995                 } catch (OverflowException) {
996                         failed = false;
997                 }
998                 if (failed)
999                         return 1;
1000
1001                 try {
1002                         double d = System.Int32.MaxValue;
1003                         failed = false;
1004                         checked {
1005                                 a = (int)d;
1006                         }
1007                 } catch (OverflowException) {
1008                         failed = true;
1009                 }
1010                 if (failed)
1011                         return 2;
1012                 
1013
1014                 try {
1015                         double d = System.Int32.MinValue;
1016                         failed = false;                 
1017                         checked {
1018                                 a = (int)d;
1019                         }
1020                 } catch (OverflowException) {
1021                         failed = true;
1022                 }
1023                 if (failed)
1024                         return 3;
1025
1026
1027                 try {
1028                         double d =  System.Int32.MinValue - 1.0;
1029                         failed = true;
1030                         checked {
1031                                 a = (int)d;
1032                         }
1033                 } catch (OverflowException) {
1034                         failed = false;
1035                 }
1036                 if (failed)
1037                         return 4;
1038
1039                 try {
1040                         l = System.Int32.MaxValue + (long)1;
1041                         failed = true;
1042                         checked {
1043                                 a = (int)l;
1044                         }
1045                 } catch (OverflowException) {
1046                         failed = false;
1047                 }
1048                 if (failed)
1049                         return 5;
1050
1051                 try {
1052                         l = System.Int32.MaxValue;
1053                         failed = false;
1054                         checked {
1055                                 a = (int)l;
1056                         }
1057                 } catch (OverflowException) {
1058                         failed = true;
1059                 }
1060                 if (failed)
1061                         return 6;
1062                 
1063
1064                 try {
1065                         l = System.Int32.MinValue;
1066                         failed = false;                 
1067                         checked {
1068                                 a = (int)l;
1069                         }
1070                 } catch (OverflowException) {
1071                         failed = true;
1072                 }
1073                 if (failed)
1074                         return 7;
1075
1076
1077                 try {
1078                         l =  System.Int32.MinValue - (long)1;
1079                         failed = true;
1080                         checked {
1081                                 a = (int)l;
1082                         }
1083                 } catch (OverflowException) {
1084                         failed = false;
1085                 }
1086                 if (failed)
1087                         return 8;
1088
1089                 try {
1090                         uint ui = System.UInt32.MaxValue;
1091                         failed = true;
1092                         checked {
1093                                 a = (int)ui;
1094                         }
1095                 }
1096                 catch (OverflowException) {
1097                         failed = false;
1098                 }
1099                 if (failed)
1100                         return 9;
1101
1102                 try {
1103                         ulong ul = (long)(System.Int32.MaxValue) + 1;
1104                         failed = true;
1105                         checked {
1106                                 a = (int)ul;
1107                         }
1108                 }
1109                 catch (OverflowException) {
1110                         failed = false;
1111                 }
1112                 if (failed)
1113                         return 10;
1114
1115                 try {
1116                         ulong ul = UInt64.MaxValue;
1117                         failed = true;
1118                         checked {
1119                                 a = (int)ul;
1120                         }
1121                 }
1122                 catch (OverflowException) {
1123                         failed = false;
1124                 }
1125                 if (failed)
1126                         return 11;
1127
1128                 {
1129                         int i; 
1130                         float f = 1.1f;
1131                         checked {
1132                                 i = (int) f;
1133                         }
1134                 }
1135
1136                 return 0;
1137         }
1138
1139         public static int test_0_uint_cast () {
1140                 uint a;
1141                 long l;
1142                 bool failed;
1143
1144                 try {
1145                         double d =  System.UInt32.MaxValue;
1146                         failed = false;
1147                         checked {
1148                                 a = (uint)d;
1149                         }
1150                 } catch (OverflowException) {
1151                         failed = true;
1152                 }
1153                 if (failed)
1154                         return 1;
1155
1156                 try {
1157                         double d = System.UInt32.MaxValue + 1.0;
1158                         failed = true;
1159                         checked {
1160                                 a = (uint)d;
1161                         }
1162                 } catch (OverflowException) {
1163                         failed = false;
1164                 }
1165                 if (failed)
1166                         return 2;
1167
1168                 try {
1169                         double d = System.UInt32.MinValue;
1170                         failed = false;
1171                         checked {
1172                                 a = (uint)d;
1173                         }
1174                 } catch (OverflowException) {
1175                         failed = true;
1176                 }
1177                 if (failed)
1178                         return 3;
1179
1180                 try {
1181                         double d = System.UInt32.MinValue - 1.0;
1182                         failed = true;
1183                         checked {
1184                                 a = (uint)d;
1185                         }
1186                 } catch (OverflowException) {
1187                         failed = false;
1188                 }
1189                 if (failed)
1190                         return 4;
1191                 
1192                 try {
1193                         l =  System.UInt32.MaxValue;
1194                         failed = false;
1195                         checked {
1196                                 a = (uint)l;
1197                         }
1198                 } catch (OverflowException) {
1199                         failed = true;
1200                 }
1201                 if (failed)
1202                         return 5;
1203
1204                 try {
1205                         l = System.UInt32.MaxValue + (long)1;
1206                         failed = true;
1207                         checked {
1208                                 a = (uint)l;
1209                         }
1210                 } catch (OverflowException) {
1211                         failed = false;
1212                 }
1213                 if (failed)
1214                         return 6;
1215
1216                 try {
1217                         l = System.UInt32.MinValue;
1218                         failed = false;
1219                         checked {
1220                                 a = (uint)l;
1221                         }
1222                 } catch (OverflowException) {
1223                         failed = true;
1224                 }
1225                 if (failed)
1226                         return 7;
1227
1228                 try {
1229                         l = System.UInt32.MinValue - (long)1;
1230                         failed = true;
1231                         checked {
1232                                 a = (uint)l;
1233                         }
1234                 } catch (OverflowException) {
1235                         failed = false;
1236                 }
1237                 if (failed)
1238                         return 8;
1239
1240                 try {
1241                         int i = -1;
1242                         failed = true;
1243                         checked {
1244                                 a = (uint)i;
1245                         }
1246                 }
1247                 catch (OverflowException) {
1248                         failed = false;
1249                 }
1250                 if (failed)
1251                         return 9;
1252
1253                 {
1254                         uint i; 
1255                         float f = 1.1f;
1256                         checked {
1257                                 i = (uint) f;
1258                         }
1259                 }
1260                 
1261                 return 0;
1262         }
1263         
1264         public static int test_0_long_cast () {
1265
1266                 /*
1267                  * These tests depend on properties of x86 fp arithmetic so they won't work
1268                  * on other platforms.
1269                  */
1270                 /*
1271                 long a;
1272                 bool failed;
1273
1274                 try {
1275                         double d = System.Int64.MaxValue - 512.0;
1276                         failed = true;
1277                         checked {
1278                                 a = (long)d;
1279                         }
1280                 } catch (OverflowException) {
1281                         failed = false;
1282                 }
1283                 if (failed)
1284                         return 1;
1285
1286                 try {
1287                         double d = System.Int64.MaxValue - 513.0;
1288                         failed = false;
1289                         checked {
1290                                 a = (long)d;
1291                         }
1292                 } catch (OverflowException) {
1293                         failed = true;
1294                 }
1295                 if (failed)
1296                         return 2;
1297                 
1298                 try {
1299                         double d = System.Int64.MinValue - 1024.0;
1300                         failed = false;                 
1301                         checked {
1302                                 a = (long)d;
1303                         }
1304                 } catch (OverflowException) {
1305                         failed = true;
1306                 }
1307                 if (failed)
1308                         return 3;
1309
1310                 try {
1311                         double d = System.Int64.MinValue - 1025.0;
1312                         failed = true;
1313                         checked {
1314                                 a = (long)d;
1315                         }
1316                 } catch (OverflowException) {
1317                         failed = false;
1318                 }
1319                 if (failed)
1320                         return 4;
1321                 */
1322
1323                 {
1324                         long i; 
1325                         float f = 1.1f;
1326                         checked {
1327                                 i = (long) f;
1328                         }
1329                 }
1330
1331                 return 0;
1332         }
1333
1334         public static int test_0_ulong_cast () {
1335                 ulong a;
1336                 bool failed;
1337
1338                 /*
1339                  * These tests depend on properties of x86 fp arithmetic so they won't work
1340                  * on other platforms.
1341                  */
1342
1343                 /*
1344                 try {
1345                         double d = System.UInt64.MaxValue - 1024.0;
1346                         failed = true;
1347                         checked {
1348                                 a = (ulong)d;
1349                         }
1350                 } catch (OverflowException) {
1351                         failed = false;
1352                 }
1353                 if (failed)
1354                         return 1;
1355
1356                 try {
1357                         double d = System.UInt64.MaxValue - 1025.0;
1358                         failed = false;
1359                         checked {
1360                                 a = (ulong)d;
1361                         }
1362                 } catch (OverflowException) {
1363                         failed = true;
1364                 }
1365                 if (failed)
1366                         return 2;
1367                 */      
1368
1369                 try {
1370                         double d = 0;
1371                         failed = false;                 
1372                         checked {
1373                                 a = (ulong)d;
1374                         }
1375                 } catch (OverflowException) {
1376                         failed = true;
1377                 }
1378                 if (failed)
1379                         return 3;
1380
1381                 try {
1382                         double d = -1;
1383                         failed = true;
1384                         checked {
1385                                 a = (ulong)d;
1386                         }
1387                 } catch (OverflowException) {
1388                         failed = false;
1389                 }
1390                 if (failed)
1391                         return 4;
1392
1393                 {
1394                         ulong i; 
1395                         float f = 1.1f;
1396                         checked {
1397                                 i = (ulong) f;
1398                         }
1399                 }
1400
1401                 try {
1402                         int i = -1;
1403                         failed = true;
1404                         checked {
1405                                 a = (ulong)i;
1406                         }
1407                 }
1408                 catch (OverflowException) {
1409                         failed = false;
1410                 }
1411                 if (failed)
1412                         return 5;
1413
1414                 try {
1415                         int i = Int32.MinValue;
1416                         failed = true;
1417                         checked {
1418                                 a = (ulong)i;
1419                         }
1420                 }
1421                 catch (OverflowException) {
1422                         failed = false;
1423                 }
1424                 if (failed)
1425                         return 6;
1426
1427                 return 0;
1428         }
1429
1430         public static int test_0_simple_double_casts () {
1431
1432                 double d = 0xffffffff;
1433
1434                 if ((uint)d != 4294967295)
1435                         return 1;
1436
1437                 /*
1438                  * These tests depend on properties of x86 fp arithmetic so they won't work
1439                  * on other platforms.
1440                  */
1441                 /*
1442                 d = 0xffffffffffffffff;
1443
1444                 if ((ulong)d != 0)
1445                         return 2;
1446
1447                 if ((ushort)d != 0)
1448                         return 3;
1449                         
1450                 if ((byte)d != 0)
1451                         return 4;
1452                 */
1453                         
1454                 d = 0xffff;
1455
1456                 if ((ushort)d != 0xffff)
1457                         return 5;
1458                 
1459                 if ((byte)d != 0xff)
1460                         return 6;
1461                         
1462                 return 0;
1463         }
1464         
1465         public static int test_0_div_zero () {
1466                 int d = 1;
1467                 int q = 0;
1468                 int val;
1469                 bool failed;
1470
1471                 try {
1472                         failed = true;
1473                         val = d / q;
1474                 } catch (DivideByZeroException) {
1475                         failed = false;
1476                 }
1477                 if (failed)
1478                         return 1;
1479
1480                 try {
1481                         failed = true;
1482                         val = d % q;
1483                 } catch (DivideByZeroException) {
1484                         failed = false;
1485                 }
1486                 if (failed)
1487                         return 2;
1488
1489                 try {
1490                         failed = true;
1491                         q = -1;
1492                         d = Int32.MinValue;
1493                         val = d / q;
1494                 } catch (DivideByZeroException) {
1495                         /* wrong exception */
1496                 } catch (OverflowException) {
1497                         failed = false;
1498                 }
1499                 if (failed)
1500                         return 3;
1501
1502                 try {
1503                         failed = true;
1504                         q = -1;
1505                         d = Int32.MinValue;
1506                         val = d % q;
1507                 } catch (DivideByZeroException) {
1508                         /* wrong exception */
1509                 } catch (OverflowException) {
1510                         failed = false;
1511                 }
1512                 if (failed)
1513                         return 4;
1514
1515                 return 0;
1516         }
1517
1518         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1519         static void dummy () {
1520         }
1521
1522         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1523         static int div_zero_llvm_inner (int i) {
1524                 try {
1525                         // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1526                         dummy ();
1527                         return 5 / i;
1528                 } catch (Exception ex) {
1529                         return 0;
1530                 }
1531         }
1532
1533         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1534         static long div_zero_llvm_inner_long (long l) {
1535                 try {
1536                         dummy ();
1537                         return (long)5 / l;
1538                 } catch (Exception ex) {
1539                         return 0;
1540                 }
1541         }
1542
1543         public static int test_0_div_zero_llvm () {
1544             long r = div_zero_llvm_inner (0);
1545                 if (r != 0)
1546                         return 1;
1547             r = div_zero_llvm_inner_long (0);
1548                 if (r != 0)
1549                         return 2;
1550                 return 0;
1551         }
1552
1553         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1554         static int div_overflow_llvm_inner (int i) {
1555                 try {
1556                         dummy ();
1557                         return Int32.MinValue / i;
1558                 } catch (Exception ex) {
1559                         return 0;
1560                 }
1561         }
1562
1563         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1564         static long div_overflow_llvm_inner_long (long l) {
1565                 try {
1566                         dummy ();
1567                         return Int64.MinValue / l;
1568                 } catch (Exception ex) {
1569                         return 0;
1570                 }
1571         }
1572
1573         public static int test_0_div_overflow_llvm () {
1574                 long r = div_overflow_llvm_inner (-1);
1575                 if (r != 0)
1576                         return 1;
1577                 r = div_overflow_llvm_inner_long ((long)-1);
1578                 if (r != 0)
1579                         return 2;
1580                 return 0;
1581         }
1582
1583         public static int return_55 () {
1584                 return 55;
1585         }
1586
1587         public static int test_0_cfold_div_zero () {
1588                 // Test that constant folding doesn't cause division by zero exceptions
1589                 if (return_55 () != return_55 ()) {
1590                         int d = 1;
1591                         int q = 0;
1592                         int val;                        
1593
1594                         val = d / q;
1595                         val = d % q;
1596
1597                         q = -1;
1598                         d = Int32.MinValue;
1599                         val = d / q;
1600
1601                         q = -1;
1602                         val = d % q;
1603                 }
1604
1605                 return 0;
1606         }
1607
1608         public static int test_0_udiv_zero () {
1609                 uint d = 1;
1610                 uint q = 0;
1611                 uint val;
1612                 bool failed;
1613
1614                 try {
1615                         failed = true;
1616                         val = d / q;
1617                 } catch (DivideByZeroException) {
1618                         failed = false;
1619                 }
1620                 if (failed)
1621                         return 1;
1622
1623                 try {
1624                         failed = true;
1625                         val = d % q;
1626                 } catch (DivideByZeroException) {
1627                         failed = false;
1628                 }
1629                 if (failed)
1630                         return 2;
1631
1632                 return 0;
1633         }
1634
1635         public static int test_0_long_div_zero () {
1636                 long d = 1;
1637                 long q = 0;
1638                 long val;
1639                 bool failed;
1640
1641                 try {
1642                         failed = true;
1643                         val = d / q;
1644                 } catch (DivideByZeroException) {
1645                         failed = false;
1646                 }
1647                 if (failed)
1648                         return 1;
1649
1650                 try {
1651                         failed = true;
1652                         val = d % q;
1653                 } catch (DivideByZeroException) {
1654                         failed = false;
1655                 }
1656                 if (failed)
1657                         return 2;
1658
1659                 try {
1660                         failed = true;
1661                         q = -1;
1662                         d = Int64.MinValue;
1663                         val = d / q;
1664                 } catch (DivideByZeroException) {
1665                         /* wrong exception */
1666                 } catch (ArithmeticException) {
1667                         failed = false;
1668                 }
1669                 if (failed)
1670                         return 3;
1671
1672                 try {
1673                         failed = true;
1674                         q = -1;
1675                         d = Int64.MinValue;
1676                         val = d % q;
1677                 } catch (DivideByZeroException) {
1678                         /* wrong exception */
1679                 } catch (ArithmeticException) {
1680                         failed = false;
1681                 }
1682                 if (failed)
1683                         return 4;
1684
1685                 return 0;
1686         }
1687
1688         public static int test_0_ulong_div_zero () {
1689                 ulong d = 1;
1690                 ulong q = 0;
1691                 ulong val;
1692                 bool failed;
1693
1694                 try {
1695                         failed = true;
1696                         val = d / q;
1697                 } catch (DivideByZeroException) {
1698                         failed = false;
1699                 }
1700                 if (failed)
1701                         return 1;
1702
1703                 try {
1704                         failed = true;
1705                         val = d % q;
1706                 } catch (DivideByZeroException) {
1707                         failed = false;
1708                 }
1709                 if (failed)
1710                         return 2;
1711
1712                 return 0;
1713         }
1714
1715         public static int test_0_float_div_zero () {
1716                 double d = 1;
1717                 double q = 0;
1718                 double val;
1719                 bool failed;
1720
1721                 try {
1722                         failed = false;
1723                         val = d / q;
1724                 } catch (DivideByZeroException) {
1725                         failed = true;
1726                 }
1727                 if (failed)
1728                         return 1;
1729
1730                 try {
1731                         failed = false;
1732                         val = d % q;
1733                 } catch (DivideByZeroException) {
1734                         failed = true;
1735                 }
1736                 if (failed)
1737                         return 2;
1738
1739                 return 0;
1740         }
1741
1742         public static int test_0_invalid_unbox () {
1743
1744                 int i = 123;
1745                 object o = "Some string";
1746                 int res = 1;
1747                 
1748                 try {
1749                         // Illegal conversion; o contains a string not an int
1750                         i = (int) o;   
1751                 } catch (Exception e) {
1752                         if (i ==123)
1753                                 res = 0;
1754                 }
1755
1756                 return res;
1757         }
1758
1759         // Test that double[] can't be cast to double (bug #46027)
1760         public static int test_0_invalid_unbox_arrays () {
1761                 double[] d1 = { 1.0 };
1762                 double[][] d2 = { d1 };
1763                 Array a = d2;
1764
1765                 try {
1766                         foreach (double d in a) {
1767                         }
1768                         return 1;
1769                 }
1770                 catch (InvalidCastException e) {
1771                         return 0;
1772                 }
1773         }
1774
1775         /* bug# 42190, at least mcs generates a leave for the return that
1776          * jumps out of multiple exception clauses: we used to execute just 
1777          * one enclosing finally block.
1778          */
1779         public static int finally_level;
1780         static void do_something () {
1781                 int a = 0;
1782                 try {
1783                         try {
1784                                 return;
1785                         } finally {
1786                                 a = 1;
1787                         }
1788                 } finally {
1789                         finally_level++;
1790                 }
1791         }
1792
1793         public static int test_2_multiple_finally_clauses () {
1794                 finally_level = 0;
1795                 do_something ();
1796                 if (finally_level == 1)
1797                         return 2;
1798                 return 0;
1799         }
1800
1801         public static int test_3_checked_cast_un () {
1802                 ulong i = 0x8000000034000000;
1803                 long j;
1804
1805                 try {
1806                         checked { j = (long)i; }
1807                 } catch (OverflowException) {
1808                         j = 2;
1809                 }
1810
1811                 if (j != 2)
1812                         return 0;
1813                 return 3;
1814         }
1815         
1816         public static int test_4_checked_cast () {
1817                 long i;
1818                 ulong j;
1819
1820                 unchecked { i = (long)0x8000000034000000;};
1821                 try {
1822                         checked { j = (ulong)i; }
1823                 } catch (OverflowException) {
1824                         j = 3;
1825                 }
1826
1827                 if (j != 3)
1828                         return 0;
1829                 return 4;
1830         }
1831
1832         static readonly int[] mul_dim_results = new int[] {
1833                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1834                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1835                 2, 0, 2, 1, 2, 8, 
1836                 3, 0, 3, 1, 3, 8, 
1837                 4, 0, 4, 1, 4, 8, 
1838                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1839                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1840                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1841         };
1842
1843         public static int test_0_multi_dim_array_access () {
1844                 int [,] a = System.Array.CreateInstance (typeof (int),
1845                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1846                 int x, y;
1847                 int result_idx = 0;
1848                 for (x = 0; x < 8; ++x) {
1849                         for (y = 0; y < 9; ++y) {
1850                                 bool got_ex = false;
1851                                 try {
1852                                         a [x, y] = 1;
1853                                 } catch {
1854                                         got_ex = true;
1855                                 }
1856                                 if (got_ex) {
1857                                         if (result_idx >= mul_dim_results.Length)
1858                                                 return -1;
1859                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1860                                                 return result_idx + 1;
1861                                         }
1862                                         result_idx += 2;
1863                                 }
1864                         }
1865                 }
1866                 if (result_idx == mul_dim_results.Length)
1867                         return 0;
1868                 return 200;
1869         }
1870
1871         static void helper_out_obj (out object o) {
1872                 o = (object)"buddy";
1873         }
1874
1875         static void helper_out_string (out string o) {
1876                 o = "buddy";
1877         }
1878
1879         public static int test_2_array_mismatch () {
1880                 string[] a = { "hello", "world" };
1881                 object[] b = a;
1882                 bool passed = false;
1883
1884                 try {
1885                         helper_out_obj (out b [1]);
1886                 } catch (ArrayTypeMismatchException) {
1887                         passed = true;
1888                 }
1889                 if (!passed)
1890                         return 0;
1891                 helper_out_string (out a [1]);
1892                 if (a [1] != "buddy")
1893                         return 1;
1894                 return 2;
1895         }
1896
1897         public static int test_0_ovf1 () {
1898                 int exception = 0;
1899                 
1900                 checked {
1901                         try {
1902                                 ulong a =  UInt64.MaxValue - 1;
1903                                 ulong t = a++;
1904                         } catch {
1905                                 exception = 1;
1906                         }
1907                 }
1908                 return exception;
1909         }
1910
1911         public static int test_1_ovf2 () {
1912                 int exception = 0;
1913
1914                 checked {
1915                         try {
1916                                 ulong a =  UInt64.MaxValue;
1917                                 ulong t = a++;
1918                         } catch {
1919                                 exception = 1;
1920                         }
1921                 }
1922                 return exception;
1923         }
1924
1925         public static int test_0_ovf3 () {
1926                 int exception = 0;
1927
1928                 long a = Int64.MaxValue - 1;
1929                 checked {
1930                         try {
1931                                 long t = a++;
1932                         } catch {
1933                                 exception = 1;
1934                         }
1935                 }
1936                 return exception;
1937         }
1938
1939         public static int test_1_ovf4 () {
1940                 int exception = 0;
1941
1942                 long a = Int64.MaxValue;
1943                 checked {
1944                         try {
1945                                 long t = a++;
1946                         } catch {
1947                                 exception = 1;
1948                         }
1949                 }
1950                 return exception;
1951         }
1952
1953         public static int test_0_ovf5 () {
1954                 int exception = 0;
1955
1956                 ulong a = UInt64.MaxValue - 1;
1957                 checked {
1958                         try {
1959                                 ulong t = a++;
1960                         } catch {
1961                                 exception = 1;
1962                         }
1963                 }
1964                 return exception;
1965         }
1966
1967         public static int test_1_ovf6 () {
1968                 int exception = 0;
1969
1970                 ulong a = UInt64.MaxValue;
1971                 checked {
1972                         try {
1973                                 ulong t = a++;
1974                         } catch {
1975                                 exception = 1;
1976                         }
1977                 }
1978                 return exception;
1979         }
1980
1981         public static int test_0_ovf7 () {
1982                 int exception = 0;
1983
1984                 long a = Int64.MinValue + 1;
1985                 checked {
1986                         try {
1987                                 long t = a--;
1988                         } catch {
1989                                 exception = 1;
1990                         }
1991                 }
1992                 return 0;
1993         }
1994
1995         public static int test_1_ovf8 () {
1996                 int exception = 0;
1997
1998                 long a = Int64.MinValue;
1999                 checked {
2000                         try {
2001                                 long t = a--;
2002                         } catch {
2003                                 exception = 1;
2004                         }
2005                 }
2006                 return exception;
2007         }
2008
2009         public static int test_0_ovf9 () {
2010                 int exception = 0;
2011
2012                 ulong a = UInt64.MinValue + 1;
2013                 checked {
2014                         try {
2015                                 ulong t = a--;
2016                         } catch {
2017                                 exception = 1;
2018                         }
2019                 }
2020                 return exception;
2021         }
2022
2023         public static int test_1_ovf10 () {
2024                 int exception = 0;
2025
2026                 ulong a = UInt64.MinValue;
2027                 checked {
2028                         try {
2029                                 ulong t = a--;
2030                         } catch {
2031                                 exception = 1;
2032                         }
2033                 }
2034                 return exception;
2035         }
2036
2037         public static int test_0_ovf11 () {
2038                 int exception = 0;
2039
2040                 int a = Int32.MinValue + 1;
2041                 checked {
2042                         try {
2043                                 int t = a--;
2044                         } catch {
2045                                 exception = 1;
2046                         }
2047                 }
2048                 return exception;
2049         }
2050
2051         public static int test_1_ovf12 () {
2052                 int exception = 0;
2053
2054                 int a = Int32.MinValue;
2055                 checked {
2056                         try {
2057                                 int t = a--;
2058                         } catch {
2059                                 exception = 1;
2060                         }
2061                 }
2062                 return exception;
2063         }
2064
2065         public static int test_0_ovf13 () {
2066                 int exception = 0;
2067
2068                 uint a = 1;
2069                 checked {
2070                         try {
2071                                 uint t = a--;
2072                         } catch {
2073                                 exception = 1;
2074                         }
2075                 }
2076                 return exception;
2077         }
2078
2079         public static int test_1_ovf14 () {
2080                 int exception = 0;
2081
2082                 uint a = 0;
2083                 checked {
2084                         try {
2085                                 uint t = a--;
2086                         } catch {
2087                                 exception = 1;
2088                         }
2089                 }
2090                 return exception;
2091         }
2092
2093         public static int test_0_ovf15 () {
2094                 int exception = 0;
2095
2096                 sbyte a = 126;
2097                 checked {
2098                         try {
2099                                 sbyte t = a++;
2100                         } catch {
2101                                 exception = 1;
2102                         }
2103                 }
2104                 return exception;
2105         }
2106
2107         public static int test_1_ovf16 () {
2108                 int exception = 0;
2109
2110                 sbyte a = 127;
2111                 checked {
2112                         try {
2113                                 sbyte t = a++;
2114                         } catch {
2115                                 exception = 1;
2116                         }
2117                 }
2118                 return exception;
2119         }
2120
2121         public static int test_0_ovf17 () {
2122                 int exception = 0;
2123
2124                 checked {
2125                         try {
2126                         } catch {
2127                                 exception = 1;
2128                         }
2129                 }
2130                 return exception;
2131         }
2132
2133         public static int test_0_ovf18 () {
2134                 int exception = 0;
2135
2136                 int a = 1 << 29;
2137                 checked {
2138                         try {
2139                                 int t = a*2;
2140                         } catch {
2141                                 exception = 1;
2142                         }
2143                 }
2144                 return exception;
2145         }
2146
2147         public static int test_1_ovf19 () {
2148                 int exception = 0;
2149
2150                 int a = 1 << 30;
2151                 checked {
2152                         try {
2153                                 int t = a*2;
2154                         } catch {
2155                                 exception = 1;
2156                         }
2157                 }
2158                 return exception;
2159         }
2160
2161         public static int test_0_ovf20 () {
2162                 int exception = 0;
2163
2164                 checked {
2165                         try {
2166                                 ulong a = 0xffffffffff;
2167                                 ulong t = a*0x0ffffff;
2168                         } catch {
2169                                 exception = 1;
2170                         }
2171                 }
2172                 return exception;
2173         }
2174
2175         public static int test_1_ovf21 () {
2176                 int exception = 0;
2177
2178                 ulong a = 0xffffffffff;
2179                 checked {
2180                         try {
2181                                 ulong t = a*0x0fffffff;
2182                         } catch {
2183                                 exception = 1;
2184                         }
2185                 }
2186                 return exception;
2187         }
2188
2189         public static int test_1_ovf22 () {
2190                 int exception = 0;
2191
2192                 long a = Int64.MinValue;
2193                 long b = 10;
2194                 checked {
2195                         try {
2196                                 long v = a * b;
2197                         } catch {
2198                                 exception = 1;
2199                         }
2200                 }
2201                 return exception;
2202         }
2203
2204         public static int test_1_ovf23 () {
2205                 int exception = 0;
2206
2207                 long a = 10;
2208                 long b = Int64.MinValue;
2209                 checked {
2210                         try {
2211                                 long v = a * b;
2212                         } catch {
2213                                 exception = 1;
2214                         }
2215                 }
2216                 return exception;
2217         }
2218
2219         class Broken {
2220                 public static int i;
2221
2222                 static Broken () {
2223                         throw new Exception ("Ugh!");
2224                 }
2225         
2226                 public static int DoSomething () {
2227                         return i;
2228                 }
2229         }
2230
2231         public static int test_0_exception_in_cctor () {
2232                 try {
2233                         Broken.DoSomething ();
2234                 }
2235                 catch (TypeInitializationException) {
2236                         // This will only happen once even if --regression is used
2237                 }
2238                 return 0;
2239         }
2240
2241         public static int test_5_regalloc () {
2242                 int i = 0;
2243
2244                 try {
2245                         for (i = 0; i < 10; ++i) {
2246                                 if (i == 5)
2247                                         throw new Exception ();
2248                         }
2249                 }
2250                 catch (Exception) {
2251                         if (i != 5)
2252                                 return i;
2253                 }
2254
2255                 // Check that variables written in catch clauses are volatile
2256                 int j = 0;
2257                 try {
2258                         throw new Exception ();
2259                 }
2260                 catch (Exception) {
2261                         j = 5;
2262                 }
2263                 if (j != 5)
2264                         return 6;
2265
2266                 int k = 0;
2267                 try {
2268                         try {
2269                                 throw new Exception ();
2270                         }
2271                         finally {
2272                                 k = 5;
2273                         }
2274                 }
2275                 catch (Exception) {
2276                 }
2277                 if (k != 5)
2278                         return 7;
2279
2280                 return i;
2281         }
2282
2283         public static void rethrow () {
2284                 try {
2285                         throw new ApplicationException();
2286                 } catch (ApplicationException) {
2287                         try {
2288                                 throw new OverflowException();
2289                         } catch (Exception) {
2290                                 throw;
2291                         }
2292                 }
2293         }
2294
2295         // Test that a rethrow rethrows the correct exception
2296         public static int test_0_rethrow_nested () {
2297                 try {
2298                         rethrow ();
2299                 } catch (OverflowException) {
2300                         return 0;
2301                 } catch (Exception) {
2302                         return 1;
2303                 }
2304                 return 2;
2305         }
2306
2307         [MethodImplAttribute (MethodImplOptions.NoInlining)]
2308         public static void rethrow1 () {
2309                 throw new Exception ();
2310         }
2311
2312         [MethodImplAttribute (MethodImplOptions.NoInlining)]
2313         public static void rethrow2 () {
2314                 rethrow1 ();
2315                 /* This disables tailcall opts */
2316                 Console.WriteLine ();
2317         }
2318
2319         [Category ("!BITCODE")]
2320         public static int test_0_rethrow_stacktrace () {
2321                 // Check that rethrowing an exception preserves the original stack trace
2322                 try {
2323                         try {
2324                                 rethrow2 ();
2325                         }
2326                         catch (Exception ex) {
2327                                 // Check that each catch clause has its own exception variable
2328                                 // If not, the throw below will overwrite the exception used
2329                                 // by the rethrow
2330                                 try {
2331                                         throw new DivideByZeroException ();
2332                                 }
2333                                 catch (Exception foo) {
2334                                 }
2335
2336                                 throw;
2337                         }
2338                 }
2339                 catch (Exception ex) {
2340                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2341                                 return 0;
2342                 }
2343
2344                 return 1;
2345         }
2346         
2347         interface IFace {}
2348         class Face : IFace {}
2349                 
2350         public static int test_1_array_mismatch_2 () {
2351                 try {
2352                         object [] o = new Face [1];
2353                         o [0] = 1;
2354                         return 0;
2355                 } catch (ArrayTypeMismatchException) {
2356                         return 1;
2357                 }
2358         }
2359         
2360         public static int test_1_array_mismatch_3 () {
2361                 try {
2362                         object [] o = new IFace [1];
2363                         o [0] = 1;
2364                         return 0;
2365                 } catch (ArrayTypeMismatchException) {
2366                         return 1;
2367                 }
2368         }
2369         
2370         public static int test_1_array_mismatch_4 () {
2371                 try {
2372                         object [][] o = new Face [5] [];
2373                         o [0] = new object [5];
2374                         
2375                         return 0;
2376                 } catch (ArrayTypeMismatchException) {
2377                         return 1;
2378                 }
2379         }
2380
2381         public static int test_0_array_size () {
2382                 bool failed;
2383
2384                 try {
2385                         failed = true;
2386                         int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2387                 }
2388                 catch (OutOfMemoryException e) {
2389                         failed = false;
2390                 }
2391                 if (failed)
2392                         return 2;
2393
2394                 return 0;
2395         }
2396
2397         struct S {
2398                 int i, j, k, l, m, n;
2399         }
2400
2401         static IntPtr[] addr;
2402
2403         static unsafe void throw_func (int i, S s) {
2404                 addr [i] = new IntPtr (&i);
2405                 throw new Exception ();
2406         }
2407
2408         /* Test that arguments are correctly popped off the stack during unwinding */
2409         /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2410         /*
2411         public static int test_0_stack_unwind () {
2412                 addr = new IntPtr [1000];
2413                 S s = new S ();
2414                 for (int j = 0; j < 1000; j++) {
2415                         try {
2416                                 throw_func (j, s);
2417                         }
2418                         catch (Exception) {
2419                         }
2420                 }
2421                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2422         }
2423         */
2424
2425         static unsafe void get_sp (int i) {
2426                 addr [i] = new IntPtr (&i);
2427         }
2428
2429         /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2430         public static int test_0_throw_unwind () {
2431                 addr = new IntPtr [1000];
2432                 S s = new S ();
2433                 for (int j = 0; j < 1000; j++) {
2434                         try {
2435                                 get_sp (j);
2436                                 throw new Exception ();
2437                         }
2438                         catch (Exception) {
2439                         }
2440                 }
2441                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2442         }
2443
2444         public static int test_0_regress_73242 () {
2445                 int [] arr = new int [10];
2446                 for (int i = 0; i < 10; ++i)
2447                         arr [i] = 0;
2448                 try {
2449                         throw new Exception ();
2450                 }
2451                 catch {
2452                 }
2453                 return 0;
2454     }
2455
2456         public static int test_0_nullref () {
2457                 try {
2458                         Array foo = null;
2459                         foo.Clone();
2460                 } catch (NullReferenceException e) {
2461                         return 0;
2462                 }
2463                 return 1;
2464         }
2465
2466         public int amethod () {
2467                 return 1;
2468         }
2469
2470         public static int test_0_nonvirt_nullref_at_clause_start () {
2471                 ExceptionTests t = null;
2472                 try {
2473                         t.amethod ();
2474                 } catch (NullReferenceException) {
2475                         return 0;
2476                 }
2477
2478                 return 1;
2479         }
2480
2481         public static int throw_only () {
2482                 throw new Exception ();
2483         }
2484
2485         [MethodImpl(MethodImplOptions.NoInlining)] 
2486         public static int throw_only2 () {
2487                 return throw_only ();
2488         }
2489
2490         public static int test_0_inline_throw_only () {
2491                 try {
2492                         return throw_only2 ();
2493                 }
2494                 catch (Exception ex) {
2495                         return 0;
2496                 }
2497         }
2498
2499         public static string GetText (string s) {
2500                 return s;
2501         }
2502
2503         public static int throw_only_gettext () {
2504                 throw new Exception (GetText ("FOO"));
2505         }
2506
2507         public static int test_0_inline_throw_only_gettext () {
2508                 object o = null;
2509                 try {
2510                         o = throw_only_gettext ();
2511                 }
2512                 catch (Exception ex) {
2513                         return 0;
2514                 }
2515
2516                 return o != null ? 0 : 1;
2517         }
2518
2519         // bug #78633
2520         public static int test_0_throw_to_branch_opt_outer_clause () {
2521                 int i = 0;
2522
2523                 try {
2524                         try {
2525                                 string [] files = new string[1];
2526
2527                                 string s = files[2];
2528                         } finally {
2529                                 i ++;
2530                         }
2531                 } catch {
2532                 }
2533
2534                 return (i == 1) ? 0 : 1;
2535         }               
2536
2537         // bug #485721
2538         public static int test_0_try_inside_finally_cmov_opt () {
2539                 bool Reconect = false;
2540
2541                 object o = new object ();
2542
2543                 try {
2544                 }
2545                 catch (Exception ExCon) {
2546                         if (o != null)
2547                                 Reconect = true;
2548
2549                         try {
2550                         }
2551                         catch (Exception Last) {
2552                         }
2553                 }
2554                 finally {
2555                         if (Reconect == true) {
2556                                 try {
2557                                 }
2558                                 catch (Exception ex) {
2559                                 }
2560                         }
2561                 }
2562
2563                 return 0;
2564         }
2565
2566         public static int test_0_inline_throw () {
2567                 try {
2568                         inline_throw1 (5);
2569                         return 1;
2570                 } catch {
2571                         return 0;
2572                 }
2573         }
2574
2575         // for llvm, the end bblock is unreachable
2576         public static int inline_throw1 (int i) {
2577                 if (i == 0)
2578                         throw new Exception ();
2579                 else
2580                         return inline_throw2 (i);
2581         }
2582
2583         public static int inline_throw2 (int i) {
2584                 throw new Exception ();
2585         }
2586
2587         // bug #539550
2588         public static int test_0_lmf_filter () {
2589                 try {
2590                         // The invoke calls a runtime-invoke wrapper which has a filter clause
2591 #if __MOBILE__
2592                         typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2593 #else
2594                         typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2595 #endif
2596                 } catch (TargetInvocationException) {
2597                 }
2598                 return 0;
2599         }
2600
2601     public static void lmf_filter () {
2602         try {
2603             Connect ();
2604         }
2605         catch {
2606             throw new NotImplementedException ();
2607         }
2608     }
2609
2610     public static void Connect () {
2611         Stop ();
2612         throw new Exception();
2613     }
2614
2615     public static void Stop () {
2616         try {
2617             lock (null) {}
2618         }
2619         catch {
2620         }
2621     }
2622
2623         private static void do_raise () {
2624                 throw new System.Exception ();
2625         }
2626
2627         private static int int_func (int i) {
2628                 return i;
2629         }
2630
2631         // #559876
2632         public static int test_8_local_deadce_causes () {
2633       int myb = 4;
2634   
2635       try {
2636         myb = int_func (8);
2637         do_raise();
2638         myb = int_func (2);
2639       } catch (System.Exception) {
2640                   return myb;
2641           }
2642           return 0;
2643         }
2644
2645         public static int test_0_except_opt_two_clauses () {
2646                 int size;
2647                 size = -1;
2648                 uint ui = (uint)size;
2649                 try {
2650                         checked {
2651                                 uint v = ui * (uint)4;
2652                         }
2653                 } catch (OverflowException e) {
2654                         return 0;
2655                 } catch (Exception) {
2656                         return 1;
2657                 }
2658
2659                 return 2;
2660         }
2661
2662     class Child
2663     {
2664         public virtual long Method()
2665         {
2666             throw new Exception();
2667         }
2668     }
2669
2670         /* #612206 */
2671         public static int test_100_long_vars_in_clauses_initlocals_opt () {
2672                 Child c = new Child();
2673                 long value = 100; 
2674                 try {
2675                         value = c.Method();
2676                 }
2677                 catch {}
2678                 return (int)value;
2679         }
2680
2681         class A {
2682                 public object AnObj;
2683         }
2684
2685         public static void DoSomething (ref object o) {
2686         }
2687
2688         public static int test_0_ldflda_null () {
2689                 A a = null;
2690
2691                 try {
2692                         DoSomething (ref a.AnObj);
2693                 } catch (NullReferenceException) {
2694                         return 0;
2695                 }
2696
2697                 return 1;
2698         }
2699
2700         unsafe struct Foo
2701         {
2702                 public int i;
2703
2704                 public static Foo* pFoo;
2705         }
2706
2707         /* MS.NET doesn't seem to throw in this case */
2708         public unsafe static int test_0_ldflda_null_pointer () {
2709                 int* pi = &Foo.pFoo->i;
2710
2711                 return 0;
2712         }
2713
2714         static int test_0_try_clause_in_finally_clause_regalloc () {
2715                 // Fill up registers with values
2716                 object a = new object ();
2717                 object[] arr1 = new object [1];
2718                 object[] arr2 = new object [1];
2719                 object[] arr3 = new object [1];
2720                 object[] arr4 = new object [1];
2721                 object[] arr5 = new object [1];
2722
2723                 for (int i = 0; i < 10; ++i)
2724                         arr1 [0] = a;
2725                 for (int i = 0; i < 10; ++i)
2726                         arr2 [0] = a;
2727                 for (int i = 0; i < 10; ++i)
2728                         arr3 [0] = a;
2729                 for (int i = 0; i < 10; ++i)
2730                         arr4 [0] = a;
2731                 for (int i = 0; i < 10; ++i)
2732                         arr5 [0] = a;
2733
2734                 int res = 1;
2735                 try {
2736                         try_clause_in_finally_clause_regalloc_inner (out res);
2737                 } catch (Exception) {
2738                 }
2739                 return res;             
2740         }
2741
2742         public static object Throw () {
2743                 for (int i = 0; i < 10; ++i)
2744                         ;
2745                 throw new Exception ();
2746         }
2747
2748         static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2749                 object o = null;
2750
2751                 res = 1;
2752                 try {
2753                         o = Throw ();
2754                 } catch (Exception) {
2755                         /* Make sure this doesn't branch to the finally */
2756                         throw new DivideByZeroException ();
2757                 } finally {
2758                         try {
2759                                 /* Make sure o is register allocated */
2760                                 if (o == null)
2761                                         res = 0;
2762                                 else
2763                                         res = 1;
2764                                 if (o == null)
2765                                         res = 0;
2766                                 else
2767                                         res = 1;
2768                                 if (o == null)
2769                                         res = 0;
2770                                 else
2771                                         res = 1;
2772                         } catch (DivideByZeroException) {
2773                         }
2774                 }
2775         }
2776
2777     public static bool t_1835_inner () {
2778         bool a = true;
2779         if (a) throw new Exception();
2780         return true;
2781     }
2782
2783         [MethodImpl(MethodImplOptions.NoInlining)] 
2784     public static bool t_1835_inner_2 () {
2785                 bool b = t_1835_inner ();
2786                 return b;
2787         }
2788
2789         public static int test_0_inline_retval_throw_in_branch_1835 () {
2790                 try {
2791                         t_1835_inner_2 ();
2792                 } catch {
2793                         return 0;
2794                 }
2795                 return 1;
2796         }
2797
2798         static bool finally_called = false;
2799
2800         static void regress_30472 (int a, int b) {
2801                         checked {
2802                                 try {
2803                                         int sum = a + b;
2804                                 } finally {
2805                                         finally_called = true;
2806                                 }
2807             }
2808                 }
2809
2810         public static int test_0_regress_30472 () {
2811                 finally_called = false;
2812                 try {
2813                     regress_30472 (Int32.MaxValue - 1, 2);
2814                 } catch (Exception ex) {
2815                 }
2816                 return finally_called ? 0 : 1;
2817         }
2818
2819         static int array_len_1 = 1;
2820
2821         public static int test_0_bounds_check_negative_constant () {
2822                 try {
2823                         byte[] arr = new byte [array_len_1];
2824                         byte b = arr [-1];
2825                         return 1;
2826                 } catch {
2827                 }
2828                 try {
2829                         byte[] arr = new byte [array_len_1];
2830                         arr [-1] = 1;
2831                         return 2;
2832                 } catch {
2833                 }
2834                 return 0;
2835         }
2836
2837         public static int test_0_string_bounds_check_negative_constant () {
2838                 try {
2839                         string s = "A";
2840                         char c = s [-1];
2841                         return 1;
2842                 } catch {
2843                 }
2844                 return 0;
2845         }
2846
2847         public class MyException : Exception {
2848                 public int marker = 0;
2849                 public string res = "";
2850
2851                 public MyException (String res) {
2852                         this.res = res;
2853                 }
2854
2855                 public bool FilterWithoutState () {
2856                         return this.marker == 0x666;
2857                 }
2858
2859                 public bool FilterWithState () {
2860                         bool ret = this.marker == 0x566;
2861                         this.marker += 0x100;
2862                         return ret;
2863                 }
2864
2865                 public bool FilterWithStringState () {
2866                         bool ret = this.marker == 0x777;
2867                         this.res = "fromFilter_" + this.res;
2868                         return ret;
2869                 }
2870         }
2871
2872         [Category ("!BITCODE")]
2873         public static int test_1_basic_filter_catch () {
2874                 try {
2875                         MyException e = new MyException ("");
2876                         e.marker = 0x1337;
2877                         throw e;
2878                 } catch (MyException ex) when (ex.marker == 0x1337) {
2879                         return 1;
2880                 }
2881                 return 0;
2882         }
2883
2884         [Category ("!BITCODE")]
2885         public static int test_1234_complicated_filter_catch () {
2886                 string res = "init";
2887                 try {
2888                         MyException e = new MyException (res);
2889                         e.marker = 0x566;
2890                         try {
2891                                 try {
2892                                         throw e;
2893                                 } catch (MyException ex) when (ex.FilterWithoutState ()) {
2894                                         res = "WRONG_" + res;
2895                                 } finally {
2896                                         e.marker = 0x777;
2897                                         res = "innerFinally_" + res;
2898                                 }
2899                         } catch (MyException ex) when (ex.FilterWithState ()) {
2900                                 res = "2ndcatch_" + res;
2901                         }
2902                         // "2ndcatch_innerFinally_init"
2903                         // Console.WriteLine ("res1: " + res);
2904                         e.res = res;
2905                         throw e;
2906                 } catch (MyException ex) when (ex.FilterWithStringState ()) {
2907                         res = "fwos_" + ex.res;
2908                 } finally {
2909                         res = "outerFinally_" + res;
2910                 }
2911                 // Console.WriteLine ("res2: " + res);
2912                 return "outerFinally_fwos_fromFilter_2ndcatch_innerFinally_init" == res ? 1234 : 0;
2913         }
2914
2915     public struct FooStruct
2916     {
2917         public long Part1 { get; }
2918         public long Part2 { get; }
2919
2920         public byte Part3 { get; }
2921     }
2922
2923     [MethodImpl( MethodImplOptions.NoInlining )]
2924     private static bool ExceptionFilter( byte x, FooStruct item ) => true;
2925
2926         [Category ("!BITCODE")]
2927         public static int test_0_filter_caller_area () {
2928         try {
2929             throw new Exception();
2930         }
2931         catch (Exception) when (ExceptionFilter (default(byte), default (FooStruct))) {
2932         }
2933                 return 0;
2934         }
2935 }
2936
2937 #if !__MOBILE__
2938 class ExceptionTests : Tests
2939 {
2940 }
2941 #endif