Merge pull request #5714 from alexischr/update_bockbuild
[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         [Category ("!WASM")] // reported as https://github.com/kripken/emscripten/issues/5603
1431         public static int test_0_simple_double_casts () {
1432
1433                 double d = 0xffffffff;
1434
1435                 if ((uint)d != 4294967295)
1436                         return 1;
1437
1438                 /*
1439                  * These tests depend on properties of x86 fp arithmetic so they won't work
1440                  * on other platforms.
1441                  */
1442                 /*
1443                 d = 0xffffffffffffffff;
1444
1445                 if ((ulong)d != 0)
1446                         return 2;
1447
1448                 if ((ushort)d != 0)
1449                         return 3;
1450                         
1451                 if ((byte)d != 0)
1452                         return 4;
1453                 */
1454                         
1455                 d = 0xffff;
1456
1457                 if ((ushort)d != 0xffff)
1458                         return 5;
1459                 
1460                 if ((byte)d != 0xff)
1461                         return 6;
1462                         
1463                 return 0;
1464         }
1465         
1466         public static int test_0_div_zero () {
1467                 int d = 1;
1468                 int q = 0;
1469                 int val;
1470                 bool failed;
1471
1472                 try {
1473                         failed = true;
1474                         val = d / q;
1475                 } catch (DivideByZeroException) {
1476                         failed = false;
1477                 }
1478                 if (failed)
1479                         return 1;
1480
1481                 try {
1482                         failed = true;
1483                         val = d % q;
1484                 } catch (DivideByZeroException) {
1485                         failed = false;
1486                 }
1487                 if (failed)
1488                         return 2;
1489
1490                 try {
1491                         failed = true;
1492                         q = -1;
1493                         d = Int32.MinValue;
1494                         val = d / q;
1495                 } catch (DivideByZeroException) {
1496                         /* wrong exception */
1497                 } catch (OverflowException) {
1498                         failed = false;
1499                 }
1500                 if (failed)
1501                         return 3;
1502
1503                 try {
1504                         failed = true;
1505                         q = -1;
1506                         d = Int32.MinValue;
1507                         val = d % q;
1508                 } catch (DivideByZeroException) {
1509                         /* wrong exception */
1510                 } catch (OverflowException) {
1511                         failed = false;
1512                 }
1513                 if (failed)
1514                         return 4;
1515
1516                 return 0;
1517         }
1518
1519         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1520         static void dummy () {
1521         }
1522
1523         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1524         static int div_zero_llvm_inner (int i) {
1525                 try {
1526                         // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1527                         dummy ();
1528                         return 5 / i;
1529                 } catch (Exception ex) {
1530                         return 0;
1531                 }
1532         }
1533
1534         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1535         static long div_zero_llvm_inner_long (long l) {
1536                 try {
1537                         dummy ();
1538                         return (long)5 / l;
1539                 } catch (Exception ex) {
1540                         return 0;
1541                 }
1542         }
1543
1544         public static int test_0_div_zero_llvm () {
1545             long r = div_zero_llvm_inner (0);
1546                 if (r != 0)
1547                         return 1;
1548             r = div_zero_llvm_inner_long (0);
1549                 if (r != 0)
1550                         return 2;
1551                 return 0;
1552         }
1553
1554         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1555         static int div_overflow_llvm_inner (int i) {
1556                 try {
1557                         dummy ();
1558                         return Int32.MinValue / i;
1559                 } catch (Exception ex) {
1560                         return 0;
1561                 }
1562         }
1563
1564         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1565         static long div_overflow_llvm_inner_long (long l) {
1566                 try {
1567                         dummy ();
1568                         return Int64.MinValue / l;
1569                 } catch (Exception ex) {
1570                         return 0;
1571                 }
1572         }
1573
1574         public static int test_0_div_overflow_llvm () {
1575                 long r = div_overflow_llvm_inner (-1);
1576                 if (r != 0)
1577                         return 1;
1578                 r = div_overflow_llvm_inner_long ((long)-1);
1579                 if (r != 0)
1580                         return 2;
1581                 return 0;
1582         }
1583
1584         public static int return_55 () {
1585                 return 55;
1586         }
1587
1588         public static int test_0_cfold_div_zero () {
1589                 // Test that constant folding doesn't cause division by zero exceptions
1590                 if (return_55 () != return_55 ()) {
1591                         int d = 1;
1592                         int q = 0;
1593                         int val;                        
1594
1595                         val = d / q;
1596                         val = d % q;
1597
1598                         q = -1;
1599                         d = Int32.MinValue;
1600                         val = d / q;
1601
1602                         q = -1;
1603                         val = d % q;
1604                 }
1605
1606                 return 0;
1607         }
1608
1609         public static int test_0_udiv_zero () {
1610                 uint d = 1;
1611                 uint q = 0;
1612                 uint val;
1613                 bool failed;
1614
1615                 try {
1616                         failed = true;
1617                         val = d / q;
1618                 } catch (DivideByZeroException) {
1619                         failed = false;
1620                 }
1621                 if (failed)
1622                         return 1;
1623
1624                 try {
1625                         failed = true;
1626                         val = d % q;
1627                 } catch (DivideByZeroException) {
1628                         failed = false;
1629                 }
1630                 if (failed)
1631                         return 2;
1632
1633                 return 0;
1634         }
1635
1636         public static int test_0_long_div_zero () {
1637                 long d = 1;
1638                 long q = 0;
1639                 long val;
1640                 bool failed;
1641
1642                 try {
1643                         failed = true;
1644                         val = d / q;
1645                 } catch (DivideByZeroException) {
1646                         failed = false;
1647                 }
1648                 if (failed)
1649                         return 1;
1650
1651                 try {
1652                         failed = true;
1653                         val = d % q;
1654                 } catch (DivideByZeroException) {
1655                         failed = false;
1656                 }
1657                 if (failed)
1658                         return 2;
1659
1660                 try {
1661                         failed = true;
1662                         q = -1;
1663                         d = Int64.MinValue;
1664                         val = d / q;
1665                 } catch (DivideByZeroException) {
1666                         /* wrong exception */
1667                 } catch (ArithmeticException) {
1668                         failed = false;
1669                 }
1670                 if (failed)
1671                         return 3;
1672
1673                 try {
1674                         failed = true;
1675                         q = -1;
1676                         d = Int64.MinValue;
1677                         val = d % q;
1678                 } catch (DivideByZeroException) {
1679                         /* wrong exception */
1680                 } catch (ArithmeticException) {
1681                         failed = false;
1682                 }
1683                 if (failed)
1684                         return 4;
1685
1686                 return 0;
1687         }
1688
1689         public static int test_0_ulong_div_zero () {
1690                 ulong d = 1;
1691                 ulong q = 0;
1692                 ulong val;
1693                 bool failed;
1694
1695                 try {
1696                         failed = true;
1697                         val = d / q;
1698                 } catch (DivideByZeroException) {
1699                         failed = false;
1700                 }
1701                 if (failed)
1702                         return 1;
1703
1704                 try {
1705                         failed = true;
1706                         val = d % q;
1707                 } catch (DivideByZeroException) {
1708                         failed = false;
1709                 }
1710                 if (failed)
1711                         return 2;
1712
1713                 return 0;
1714         }
1715
1716         public static int test_0_float_div_zero () {
1717                 double d = 1;
1718                 double q = 0;
1719                 double val;
1720                 bool failed;
1721
1722                 try {
1723                         failed = false;
1724                         val = d / q;
1725                 } catch (DivideByZeroException) {
1726                         failed = true;
1727                 }
1728                 if (failed)
1729                         return 1;
1730
1731                 try {
1732                         failed = false;
1733                         val = d % q;
1734                 } catch (DivideByZeroException) {
1735                         failed = true;
1736                 }
1737                 if (failed)
1738                         return 2;
1739
1740                 return 0;
1741         }
1742
1743         public static int test_0_invalid_unbox () {
1744
1745                 int i = 123;
1746                 object o = "Some string";
1747                 int res = 1;
1748                 
1749                 try {
1750                         // Illegal conversion; o contains a string not an int
1751                         i = (int) o;   
1752                 } catch (Exception e) {
1753                         if (i ==123)
1754                                 res = 0;
1755                 }
1756
1757                 return res;
1758         }
1759
1760         // Test that double[] can't be cast to double (bug #46027)
1761         public static int test_0_invalid_unbox_arrays () {
1762                 double[] d1 = { 1.0 };
1763                 double[][] d2 = { d1 };
1764                 Array a = d2;
1765
1766                 try {
1767                         foreach (double d in a) {
1768                         }
1769                         return 1;
1770                 }
1771                 catch (InvalidCastException e) {
1772                         return 0;
1773                 }
1774         }
1775
1776         /* bug# 42190, at least mcs generates a leave for the return that
1777          * jumps out of multiple exception clauses: we used to execute just 
1778          * one enclosing finally block.
1779          */
1780         public static int finally_level;
1781         static void do_something () {
1782                 int a = 0;
1783                 try {
1784                         try {
1785                                 return;
1786                         } finally {
1787                                 a = 1;
1788                         }
1789                 } finally {
1790                         finally_level++;
1791                 }
1792         }
1793
1794         public static int test_2_multiple_finally_clauses () {
1795                 finally_level = 0;
1796                 do_something ();
1797                 if (finally_level == 1)
1798                         return 2;
1799                 return 0;
1800         }
1801
1802         public static int test_3_checked_cast_un () {
1803                 ulong i = 0x8000000034000000;
1804                 long j;
1805
1806                 try {
1807                         checked { j = (long)i; }
1808                 } catch (OverflowException) {
1809                         j = 2;
1810                 }
1811
1812                 if (j != 2)
1813                         return 0;
1814                 return 3;
1815         }
1816         
1817         public static int test_4_checked_cast () {
1818                 long i;
1819                 ulong j;
1820
1821                 unchecked { i = (long)0x8000000034000000;};
1822                 try {
1823                         checked { j = (ulong)i; }
1824                 } catch (OverflowException) {
1825                         j = 3;
1826                 }
1827
1828                 if (j != 3)
1829                         return 0;
1830                 return 4;
1831         }
1832
1833         static readonly int[] mul_dim_results = new int[] {
1834                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1835                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1836                 2, 0, 2, 1, 2, 8, 
1837                 3, 0, 3, 1, 3, 8, 
1838                 4, 0, 4, 1, 4, 8, 
1839                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1840                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1841                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1842         };
1843
1844         public static int test_0_multi_dim_array_access () {
1845                 int [,] a = System.Array.CreateInstance (typeof (int),
1846                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1847                 int x, y;
1848                 int result_idx = 0;
1849                 for (x = 0; x < 8; ++x) {
1850                         for (y = 0; y < 9; ++y) {
1851                                 bool got_ex = false;
1852                                 try {
1853                                         a [x, y] = 1;
1854                                 } catch {
1855                                         got_ex = true;
1856                                 }
1857                                 if (got_ex) {
1858                                         if (result_idx >= mul_dim_results.Length)
1859                                                 return -1;
1860                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1861                                                 return result_idx + 1;
1862                                         }
1863                                         result_idx += 2;
1864                                 }
1865                         }
1866                 }
1867                 if (result_idx == mul_dim_results.Length)
1868                         return 0;
1869                 return 200;
1870         }
1871
1872         static void helper_out_obj (out object o) {
1873                 o = (object)"buddy";
1874         }
1875
1876         static void helper_out_string (out string o) {
1877                 o = "buddy";
1878         }
1879
1880         public static int test_2_array_mismatch () {
1881                 string[] a = { "hello", "world" };
1882                 object[] b = a;
1883                 bool passed = false;
1884
1885                 try {
1886                         helper_out_obj (out b [1]);
1887                 } catch (ArrayTypeMismatchException) {
1888                         passed = true;
1889                 }
1890                 if (!passed)
1891                         return 0;
1892                 helper_out_string (out a [1]);
1893                 if (a [1] != "buddy")
1894                         return 1;
1895                 return 2;
1896         }
1897
1898         public static int test_0_ovf1 () {
1899                 int exception = 0;
1900                 
1901                 checked {
1902                         try {
1903                                 ulong a =  UInt64.MaxValue - 1;
1904                                 ulong t = a++;
1905                         } catch {
1906                                 exception = 1;
1907                         }
1908                 }
1909                 return exception;
1910         }
1911
1912         public static int test_1_ovf2 () {
1913                 int exception = 0;
1914
1915                 checked {
1916                         try {
1917                                 ulong a =  UInt64.MaxValue;
1918                                 ulong t = a++;
1919                         } catch {
1920                                 exception = 1;
1921                         }
1922                 }
1923                 return exception;
1924         }
1925
1926         public static int test_0_ovf3 () {
1927                 int exception = 0;
1928
1929                 long a = Int64.MaxValue - 1;
1930                 checked {
1931                         try {
1932                                 long t = a++;
1933                         } catch {
1934                                 exception = 1;
1935                         }
1936                 }
1937                 return exception;
1938         }
1939
1940         public static int test_1_ovf4 () {
1941                 int exception = 0;
1942
1943                 long a = Int64.MaxValue;
1944                 checked {
1945                         try {
1946                                 long t = a++;
1947                         } catch {
1948                                 exception = 1;
1949                         }
1950                 }
1951                 return exception;
1952         }
1953
1954         public static int test_0_ovf5 () {
1955                 int exception = 0;
1956
1957                 ulong a = UInt64.MaxValue - 1;
1958                 checked {
1959                         try {
1960                                 ulong t = a++;
1961                         } catch {
1962                                 exception = 1;
1963                         }
1964                 }
1965                 return exception;
1966         }
1967
1968         public static int test_1_ovf6 () {
1969                 int exception = 0;
1970
1971                 ulong a = UInt64.MaxValue;
1972                 checked {
1973                         try {
1974                                 ulong t = a++;
1975                         } catch {
1976                                 exception = 1;
1977                         }
1978                 }
1979                 return exception;
1980         }
1981
1982         public static int test_0_ovf7 () {
1983                 int exception = 0;
1984
1985                 long a = Int64.MinValue + 1;
1986                 checked {
1987                         try {
1988                                 long t = a--;
1989                         } catch {
1990                                 exception = 1;
1991                         }
1992                 }
1993                 return 0;
1994         }
1995
1996         public static int test_1_ovf8 () {
1997                 int exception = 0;
1998
1999                 long a = Int64.MinValue;
2000                 checked {
2001                         try {
2002                                 long t = a--;
2003                         } catch {
2004                                 exception = 1;
2005                         }
2006                 }
2007                 return exception;
2008         }
2009
2010         public static int test_0_ovf9 () {
2011                 int exception = 0;
2012
2013                 ulong a = UInt64.MinValue + 1;
2014                 checked {
2015                         try {
2016                                 ulong t = a--;
2017                         } catch {
2018                                 exception = 1;
2019                         }
2020                 }
2021                 return exception;
2022         }
2023
2024         public static int test_1_ovf10 () {
2025                 int exception = 0;
2026
2027                 ulong a = UInt64.MinValue;
2028                 checked {
2029                         try {
2030                                 ulong t = a--;
2031                         } catch {
2032                                 exception = 1;
2033                         }
2034                 }
2035                 return exception;
2036         }
2037
2038         public static int test_0_ovf11 () {
2039                 int exception = 0;
2040
2041                 int a = Int32.MinValue + 1;
2042                 checked {
2043                         try {
2044                                 int t = a--;
2045                         } catch {
2046                                 exception = 1;
2047                         }
2048                 }
2049                 return exception;
2050         }
2051
2052         public static int test_1_ovf12 () {
2053                 int exception = 0;
2054
2055                 int a = Int32.MinValue;
2056                 checked {
2057                         try {
2058                                 int t = a--;
2059                         } catch {
2060                                 exception = 1;
2061                         }
2062                 }
2063                 return exception;
2064         }
2065
2066         public static int test_0_ovf13 () {
2067                 int exception = 0;
2068
2069                 uint a = 1;
2070                 checked {
2071                         try {
2072                                 uint t = a--;
2073                         } catch {
2074                                 exception = 1;
2075                         }
2076                 }
2077                 return exception;
2078         }
2079
2080         public static int test_1_ovf14 () {
2081                 int exception = 0;
2082
2083                 uint a = 0;
2084                 checked {
2085                         try {
2086                                 uint t = a--;
2087                         } catch {
2088                                 exception = 1;
2089                         }
2090                 }
2091                 return exception;
2092         }
2093
2094         public static int test_0_ovf15 () {
2095                 int exception = 0;
2096
2097                 sbyte a = 126;
2098                 checked {
2099                         try {
2100                                 sbyte t = a++;
2101                         } catch {
2102                                 exception = 1;
2103                         }
2104                 }
2105                 return exception;
2106         }
2107
2108         public static int test_1_ovf16 () {
2109                 int exception = 0;
2110
2111                 sbyte a = 127;
2112                 checked {
2113                         try {
2114                                 sbyte t = a++;
2115                         } catch {
2116                                 exception = 1;
2117                         }
2118                 }
2119                 return exception;
2120         }
2121
2122         public static int test_0_ovf17 () {
2123                 int exception = 0;
2124
2125                 checked {
2126                         try {
2127                         } catch {
2128                                 exception = 1;
2129                         }
2130                 }
2131                 return exception;
2132         }
2133
2134         public static int test_0_ovf18 () {
2135                 int exception = 0;
2136
2137                 int a = 1 << 29;
2138                 checked {
2139                         try {
2140                                 int t = a*2;
2141                         } catch {
2142                                 exception = 1;
2143                         }
2144                 }
2145                 return exception;
2146         }
2147
2148         public static int test_1_ovf19 () {
2149                 int exception = 0;
2150
2151                 int a = 1 << 30;
2152                 checked {
2153                         try {
2154                                 int t = a*2;
2155                         } catch {
2156                                 exception = 1;
2157                         }
2158                 }
2159                 return exception;
2160         }
2161
2162         public static int test_0_ovf20 () {
2163                 int exception = 0;
2164
2165                 checked {
2166                         try {
2167                                 ulong a = 0xffffffffff;
2168                                 ulong t = a*0x0ffffff;
2169                         } catch {
2170                                 exception = 1;
2171                         }
2172                 }
2173                 return exception;
2174         }
2175
2176         public static int test_1_ovf21 () {
2177                 int exception = 0;
2178
2179                 ulong a = 0xffffffffff;
2180                 checked {
2181                         try {
2182                                 ulong t = a*0x0fffffff;
2183                         } catch {
2184                                 exception = 1;
2185                         }
2186                 }
2187                 return exception;
2188         }
2189
2190         public static int test_1_ovf22 () {
2191                 int exception = 0;
2192
2193                 long a = Int64.MinValue;
2194                 long b = 10;
2195                 checked {
2196                         try {
2197                                 long v = a * b;
2198                         } catch {
2199                                 exception = 1;
2200                         }
2201                 }
2202                 return exception;
2203         }
2204
2205         public static int test_1_ovf23 () {
2206                 int exception = 0;
2207
2208                 long a = 10;
2209                 long b = Int64.MinValue;
2210                 checked {
2211                         try {
2212                                 long v = a * b;
2213                         } catch {
2214                                 exception = 1;
2215                         }
2216                 }
2217                 return exception;
2218         }
2219
2220         class Broken {
2221                 public static int i;
2222
2223                 static Broken () {
2224                         throw new Exception ("Ugh!");
2225                 }
2226         
2227                 public static int DoSomething () {
2228                         return i;
2229                 }
2230         }
2231
2232         public static int test_0_exception_in_cctor () {
2233                 try {
2234                         Broken.DoSomething ();
2235                 }
2236                 catch (TypeInitializationException) {
2237                         // This will only happen once even if --regression is used
2238                 }
2239                 return 0;
2240         }
2241
2242         public static int test_5_regalloc () {
2243                 int i = 0;
2244
2245                 try {
2246                         for (i = 0; i < 10; ++i) {
2247                                 if (i == 5)
2248                                         throw new Exception ();
2249                         }
2250                 }
2251                 catch (Exception) {
2252                         if (i != 5)
2253                                 return i;
2254                 }
2255
2256                 // Check that variables written in catch clauses are volatile
2257                 int j = 0;
2258                 try {
2259                         throw new Exception ();
2260                 }
2261                 catch (Exception) {
2262                         j = 5;
2263                 }
2264                 if (j != 5)
2265                         return 6;
2266
2267                 int k = 0;
2268                 try {
2269                         try {
2270                                 throw new Exception ();
2271                         }
2272                         finally {
2273                                 k = 5;
2274                         }
2275                 }
2276                 catch (Exception) {
2277                 }
2278                 if (k != 5)
2279                         return 7;
2280
2281                 return i;
2282         }
2283
2284         public static void rethrow () {
2285                 try {
2286                         throw new ApplicationException();
2287                 } catch (ApplicationException) {
2288                         try {
2289                                 throw new OverflowException();
2290                         } catch (Exception) {
2291                                 throw;
2292                         }
2293                 }
2294         }
2295
2296         // Test that a rethrow rethrows the correct exception
2297         public static int test_0_rethrow_nested () {
2298                 try {
2299                         rethrow ();
2300                 } catch (OverflowException) {
2301                         return 0;
2302                 } catch (Exception) {
2303                         return 1;
2304                 }
2305                 return 2;
2306         }
2307
2308         [MethodImplAttribute (MethodImplOptions.NoInlining)]
2309         public static void rethrow1 () {
2310                 throw new Exception ();
2311         }
2312
2313         [MethodImplAttribute (MethodImplOptions.NoInlining)]
2314         public static void rethrow2 () {
2315                 rethrow1 ();
2316                 /* This disables tailcall opts */
2317                 Console.WriteLine ();
2318         }
2319
2320         [Category ("!BITCODE")]
2321         public static int test_0_rethrow_stacktrace () {
2322                 // Check that rethrowing an exception preserves the original stack trace
2323                 try {
2324                         try {
2325                                 rethrow2 ();
2326                         }
2327                         catch (Exception ex) {
2328                                 // Check that each catch clause has its own exception variable
2329                                 // If not, the throw below will overwrite the exception used
2330                                 // by the rethrow
2331                                 try {
2332                                         throw new DivideByZeroException ();
2333                                 }
2334                                 catch (Exception foo) {
2335                                 }
2336
2337                                 throw;
2338                         }
2339                 }
2340                 catch (Exception ex) {
2341                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2342                                 return 0;
2343                 }
2344
2345                 return 1;
2346         }
2347         
2348         interface IFace {}
2349         class Face : IFace {}
2350                 
2351         public static int test_1_array_mismatch_2 () {
2352                 try {
2353                         object [] o = new Face [1];
2354                         o [0] = 1;
2355                         return 0;
2356                 } catch (ArrayTypeMismatchException) {
2357                         return 1;
2358                 }
2359         }
2360         
2361         public static int test_1_array_mismatch_3 () {
2362                 try {
2363                         object [] o = new IFace [1];
2364                         o [0] = 1;
2365                         return 0;
2366                 } catch (ArrayTypeMismatchException) {
2367                         return 1;
2368                 }
2369         }
2370         
2371         public static int test_1_array_mismatch_4 () {
2372                 try {
2373                         object [][] o = new Face [5] [];
2374                         o [0] = new object [5];
2375                         
2376                         return 0;
2377                 } catch (ArrayTypeMismatchException) {
2378                         return 1;
2379                 }
2380         }
2381
2382         public static int test_0_array_size () {
2383                 bool failed;
2384
2385                 try {
2386                         failed = true;
2387                         int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2388                 }
2389                 catch (OutOfMemoryException e) {
2390                         failed = false;
2391                 }
2392                 if (failed)
2393                         return 2;
2394
2395                 return 0;
2396         }
2397
2398         struct S {
2399                 int i, j, k, l, m, n;
2400         }
2401
2402         static IntPtr[] addr;
2403
2404         static unsafe void throw_func (int i, S s) {
2405                 addr [i] = new IntPtr (&i);
2406                 throw new Exception ();
2407         }
2408
2409         /* Test that arguments are correctly popped off the stack during unwinding */
2410         /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2411         /*
2412         public static int test_0_stack_unwind () {
2413                 addr = new IntPtr [1000];
2414                 S s = new S ();
2415                 for (int j = 0; j < 1000; j++) {
2416                         try {
2417                                 throw_func (j, s);
2418                         }
2419                         catch (Exception) {
2420                         }
2421                 }
2422                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2423         }
2424         */
2425
2426         static unsafe void get_sp (int i) {
2427                 addr [i] = new IntPtr (&i);
2428         }
2429
2430         /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2431         public static int test_0_throw_unwind () {
2432                 addr = new IntPtr [1000];
2433                 S s = new S ();
2434                 for (int j = 0; j < 1000; j++) {
2435                         try {
2436                                 get_sp (j);
2437                                 throw new Exception ();
2438                         }
2439                         catch (Exception) {
2440                         }
2441                 }
2442                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2443         }
2444
2445         public static int test_0_regress_73242 () {
2446                 int [] arr = new int [10];
2447                 for (int i = 0; i < 10; ++i)
2448                         arr [i] = 0;
2449                 try {
2450                         throw new Exception ();
2451                 }
2452                 catch {
2453                 }
2454                 return 0;
2455     }
2456
2457         public static int test_0_nullref () {
2458                 try {
2459                         Array foo = null;
2460                         foo.Clone();
2461                 } catch (NullReferenceException e) {
2462                         return 0;
2463                 }
2464                 return 1;
2465         }
2466
2467         public int amethod () {
2468                 return 1;
2469         }
2470
2471         public static int test_0_nonvirt_nullref_at_clause_start () {
2472                 ExceptionTests t = null;
2473                 try {
2474                         t.amethod ();
2475                 } catch (NullReferenceException) {
2476                         return 0;
2477                 }
2478
2479                 return 1;
2480         }
2481
2482         public static int throw_only () {
2483                 throw new Exception ();
2484         }
2485
2486         [MethodImpl(MethodImplOptions.NoInlining)] 
2487         public static int throw_only2 () {
2488                 return throw_only ();
2489         }
2490
2491         public static int test_0_inline_throw_only () {
2492                 try {
2493                         return throw_only2 ();
2494                 }
2495                 catch (Exception ex) {
2496                         return 0;
2497                 }
2498         }
2499
2500         public static string GetText (string s) {
2501                 return s;
2502         }
2503
2504         public static int throw_only_gettext () {
2505                 throw new Exception (GetText ("FOO"));
2506         }
2507
2508         public static int test_0_inline_throw_only_gettext () {
2509                 object o = null;
2510                 try {
2511                         o = throw_only_gettext ();
2512                 }
2513                 catch (Exception ex) {
2514                         return 0;
2515                 }
2516
2517                 return o != null ? 0 : 1;
2518         }
2519
2520         // bug #78633
2521         public static int test_0_throw_to_branch_opt_outer_clause () {
2522                 int i = 0;
2523
2524                 try {
2525                         try {
2526                                 string [] files = new string[1];
2527
2528                                 string s = files[2];
2529                         } finally {
2530                                 i ++;
2531                         }
2532                 } catch {
2533                 }
2534
2535                 return (i == 1) ? 0 : 1;
2536         }               
2537
2538         // bug #485721
2539         public static int test_0_try_inside_finally_cmov_opt () {
2540                 bool Reconect = false;
2541
2542                 object o = new object ();
2543
2544                 try {
2545                 }
2546                 catch (Exception ExCon) {
2547                         if (o != null)
2548                                 Reconect = true;
2549
2550                         try {
2551                         }
2552                         catch (Exception Last) {
2553                         }
2554                 }
2555                 finally {
2556                         if (Reconect == true) {
2557                                 try {
2558                                 }
2559                                 catch (Exception ex) {
2560                                 }
2561                         }
2562                 }
2563
2564                 return 0;
2565         }
2566
2567         public static int test_0_inline_throw () {
2568                 try {
2569                         inline_throw1 (5);
2570                         return 1;
2571                 } catch {
2572                         return 0;
2573                 }
2574         }
2575
2576         // for llvm, the end bblock is unreachable
2577         public static int inline_throw1 (int i) {
2578                 if (i == 0)
2579                         throw new Exception ();
2580                 else
2581                         return inline_throw2 (i);
2582         }
2583
2584         public static int inline_throw2 (int i) {
2585                 throw new Exception ();
2586         }
2587
2588         // bug #539550
2589         public static int test_0_lmf_filter () {
2590                 try {
2591                         // The invoke calls a runtime-invoke wrapper which has a filter clause
2592 #if __MOBILE__
2593                         typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2594 #else
2595                         typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2596 #endif
2597                 } catch (TargetInvocationException) {
2598                 }
2599                 return 0;
2600         }
2601
2602     public static void lmf_filter () {
2603         try {
2604             Connect ();
2605         }
2606         catch {
2607             throw new NotImplementedException ();
2608         }
2609     }
2610
2611     public static void Connect () {
2612         Stop ();
2613         throw new Exception();
2614     }
2615
2616     public static void Stop () {
2617         try {
2618             lock (null) {}
2619         }
2620         catch {
2621         }
2622     }
2623
2624         private static void do_raise () {
2625                 throw new System.Exception ();
2626         }
2627
2628         private static int int_func (int i) {
2629                 return i;
2630         }
2631
2632         // #559876
2633         public static int test_8_local_deadce_causes () {
2634       int myb = 4;
2635   
2636       try {
2637         myb = int_func (8);
2638         do_raise();
2639         myb = int_func (2);
2640       } catch (System.Exception) {
2641                   return myb;
2642           }
2643           return 0;
2644         }
2645
2646         public static int test_0_except_opt_two_clauses () {
2647                 int size;
2648                 size = -1;
2649                 uint ui = (uint)size;
2650                 try {
2651                         checked {
2652                                 uint v = ui * (uint)4;
2653                         }
2654                 } catch (OverflowException e) {
2655                         return 0;
2656                 } catch (Exception) {
2657                         return 1;
2658                 }
2659
2660                 return 2;
2661         }
2662
2663     class Child
2664     {
2665         public virtual long Method()
2666         {
2667             throw new Exception();
2668         }
2669     }
2670
2671         /* #612206 */
2672         public static int test_100_long_vars_in_clauses_initlocals_opt () {
2673                 Child c = new Child();
2674                 long value = 100; 
2675                 try {
2676                         value = c.Method();
2677                 }
2678                 catch {}
2679                 return (int)value;
2680         }
2681
2682         class A {
2683                 public object AnObj;
2684         }
2685
2686         public static void DoSomething (ref object o) {
2687         }
2688
2689         public static int test_0_ldflda_null () {
2690                 A a = null;
2691
2692                 try {
2693                         DoSomething (ref a.AnObj);
2694                 } catch (NullReferenceException) {
2695                         return 0;
2696                 }
2697
2698                 return 1;
2699         }
2700
2701         unsafe struct Foo
2702         {
2703                 public int i;
2704
2705                 public static Foo* pFoo;
2706         }
2707
2708         /* MS.NET doesn't seem to throw in this case */
2709         public unsafe static int test_0_ldflda_null_pointer () {
2710                 int* pi = &Foo.pFoo->i;
2711
2712                 return 0;
2713         }
2714
2715         static int test_0_try_clause_in_finally_clause_regalloc () {
2716                 // Fill up registers with values
2717                 object a = new object ();
2718                 object[] arr1 = new object [1];
2719                 object[] arr2 = new object [1];
2720                 object[] arr3 = new object [1];
2721                 object[] arr4 = new object [1];
2722                 object[] arr5 = new object [1];
2723
2724                 for (int i = 0; i < 10; ++i)
2725                         arr1 [0] = a;
2726                 for (int i = 0; i < 10; ++i)
2727                         arr2 [0] = a;
2728                 for (int i = 0; i < 10; ++i)
2729                         arr3 [0] = a;
2730                 for (int i = 0; i < 10; ++i)
2731                         arr4 [0] = a;
2732                 for (int i = 0; i < 10; ++i)
2733                         arr5 [0] = a;
2734
2735                 int res = 1;
2736                 try {
2737                         try_clause_in_finally_clause_regalloc_inner (out res);
2738                 } catch (Exception) {
2739                 }
2740                 return res;             
2741         }
2742
2743         public static object Throw () {
2744                 for (int i = 0; i < 10; ++i)
2745                         ;
2746                 throw new Exception ();
2747         }
2748
2749         static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2750                 object o = null;
2751
2752                 res = 1;
2753                 try {
2754                         o = Throw ();
2755                 } catch (Exception) {
2756                         /* Make sure this doesn't branch to the finally */
2757                         throw new DivideByZeroException ();
2758                 } finally {
2759                         try {
2760                                 /* Make sure o is register allocated */
2761                                 if (o == null)
2762                                         res = 0;
2763                                 else
2764                                         res = 1;
2765                                 if (o == null)
2766                                         res = 0;
2767                                 else
2768                                         res = 1;
2769                                 if (o == null)
2770                                         res = 0;
2771                                 else
2772                                         res = 1;
2773                         } catch (DivideByZeroException) {
2774                         }
2775                 }
2776         }
2777
2778     public static bool t_1835_inner () {
2779         bool a = true;
2780         if (a) throw new Exception();
2781         return true;
2782     }
2783
2784         [MethodImpl(MethodImplOptions.NoInlining)] 
2785     public static bool t_1835_inner_2 () {
2786                 bool b = t_1835_inner ();
2787                 return b;
2788         }
2789
2790         public static int test_0_inline_retval_throw_in_branch_1835 () {
2791                 try {
2792                         t_1835_inner_2 ();
2793                 } catch {
2794                         return 0;
2795                 }
2796                 return 1;
2797         }
2798
2799         static bool finally_called = false;
2800
2801         static void regress_30472 (int a, int b) {
2802                         checked {
2803                                 try {
2804                                         int sum = a + b;
2805                                 } finally {
2806                                         finally_called = true;
2807                                 }
2808             }
2809                 }
2810
2811         public static int test_0_regress_30472 () {
2812                 finally_called = false;
2813                 try {
2814                     regress_30472 (Int32.MaxValue - 1, 2);
2815                 } catch (Exception ex) {
2816                 }
2817                 return finally_called ? 0 : 1;
2818         }
2819
2820         static int array_len_1 = 1;
2821
2822         public static int test_0_bounds_check_negative_constant () {
2823                 try {
2824                         byte[] arr = new byte [array_len_1];
2825                         byte b = arr [-1];
2826                         return 1;
2827                 } catch {
2828                 }
2829                 try {
2830                         byte[] arr = new byte [array_len_1];
2831                         arr [-1] = 1;
2832                         return 2;
2833                 } catch {
2834                 }
2835                 return 0;
2836         }
2837
2838         public static int test_0_string_bounds_check_negative_constant () {
2839                 try {
2840                         string s = "A";
2841                         char c = s [-1];
2842                         return 1;
2843                 } catch {
2844                 }
2845                 return 0;
2846         }
2847
2848         public class MyException : Exception {
2849                 public int marker = 0;
2850                 public string res = "";
2851
2852                 public MyException (String res) {
2853                         this.res = res;
2854                 }
2855
2856                 public bool FilterWithoutState () {
2857                         return this.marker == 0x666;
2858                 }
2859
2860                 public bool FilterWithState () {
2861                         bool ret = this.marker == 0x566;
2862                         this.marker += 0x100;
2863                         return ret;
2864                 }
2865
2866                 public bool FilterWithStringState () {
2867                         bool ret = this.marker == 0x777;
2868                         this.res = "fromFilter_" + this.res;
2869                         return ret;
2870                 }
2871         }
2872
2873         [Category ("!BITCODE")]
2874         public static int test_1_basic_filter_catch () {
2875                 try {
2876                         MyException e = new MyException ("");
2877                         e.marker = 0x1337;
2878                         throw e;
2879                 } catch (MyException ex) when (ex.marker == 0x1337) {
2880                         return 1;
2881                 }
2882                 return 0;
2883         }
2884
2885         [Category ("!BITCODE")]
2886         public static int test_1234_complicated_filter_catch () {
2887                 string res = "init";
2888                 try {
2889                         MyException e = new MyException (res);
2890                         e.marker = 0x566;
2891                         try {
2892                                 try {
2893                                         throw e;
2894                                 } catch (MyException ex) when (ex.FilterWithoutState ()) {
2895                                         res = "WRONG_" + res;
2896                                 } finally {
2897                                         e.marker = 0x777;
2898                                         res = "innerFinally_" + res;
2899                                 }
2900                         } catch (MyException ex) when (ex.FilterWithState ()) {
2901                                 res = "2ndcatch_" + res;
2902                         }
2903                         // "2ndcatch_innerFinally_init"
2904                         // Console.WriteLine ("res1: " + res);
2905                         e.res = res;
2906                         throw e;
2907                 } catch (MyException ex) when (ex.FilterWithStringState ()) {
2908                         res = "fwos_" + ex.res;
2909                 } finally {
2910                         res = "outerFinally_" + res;
2911                 }
2912                 // Console.WriteLine ("res2: " + res);
2913                 return "outerFinally_fwos_fromFilter_2ndcatch_innerFinally_init" == res ? 1234 : 0;
2914         }
2915
2916     public struct FooStruct
2917     {
2918         public long Part1 { get; }
2919         public long Part2 { get; }
2920
2921         public byte Part3 { get; }
2922     }
2923
2924     [MethodImpl( MethodImplOptions.NoInlining )]
2925     private static bool ExceptionFilter( byte x, FooStruct item ) => true;
2926
2927         [Category ("!BITCODE")]
2928         public static int test_0_filter_caller_area () {
2929         try {
2930             throw new Exception();
2931         }
2932         catch (Exception) when (ExceptionFilter (default(byte), default (FooStruct))) {
2933         }
2934                 return 0;
2935         }
2936 }
2937
2938 #if !__MOBILE__
2939 class ExceptionTests : Tests
2940 {
2941 }
2942 #endif