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