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