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