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