[interpreter] add {exceptions,devirtualization,basic-simd}.cs to richeck target
[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         [Category ("!INTERPRETER")]
985         public static int test_0_int_cast () {
986                 int a;
987                 long l;
988                 bool failed;
989
990                 try {
991                         double d = System.Int32.MaxValue + 1.0;
992                         failed = true;
993                         checked {
994                                 a = (int)d;
995                         }
996                 } catch (OverflowException) {
997                         failed = false;
998                 }
999                 if (failed)
1000                         return 1;
1001
1002                 try {
1003                         double d = System.Int32.MaxValue;
1004                         failed = false;
1005                         checked {
1006                                 a = (int)d;
1007                         }
1008                 } catch (OverflowException) {
1009                         failed = true;
1010                 }
1011                 if (failed)
1012                         return 2;
1013                 
1014
1015                 try {
1016                         double d = System.Int32.MinValue;
1017                         failed = false;                 
1018                         checked {
1019                                 a = (int)d;
1020                         }
1021                 } catch (OverflowException) {
1022                         failed = true;
1023                 }
1024                 if (failed)
1025                         return 3;
1026
1027
1028                 try {
1029                         double d =  System.Int32.MinValue - 1.0;
1030                         failed = true;
1031                         checked {
1032                                 a = (int)d;
1033                         }
1034                 } catch (OverflowException) {
1035                         failed = false;
1036                 }
1037                 if (failed)
1038                         return 4;
1039
1040                 try {
1041                         l = System.Int32.MaxValue + (long)1;
1042                         failed = true;
1043                         checked {
1044                                 a = (int)l;
1045                         }
1046                 } catch (OverflowException) {
1047                         failed = false;
1048                 }
1049                 if (failed)
1050                         return 5;
1051
1052                 try {
1053                         l = System.Int32.MaxValue;
1054                         failed = false;
1055                         checked {
1056                                 a = (int)l;
1057                         }
1058                 } catch (OverflowException) {
1059                         failed = true;
1060                 }
1061                 if (failed)
1062                         return 6;
1063                 
1064
1065                 try {
1066                         l = System.Int32.MinValue;
1067                         failed = false;                 
1068                         checked {
1069                                 a = (int)l;
1070                         }
1071                 } catch (OverflowException) {
1072                         failed = true;
1073                 }
1074                 if (failed)
1075                         return 7;
1076
1077
1078                 try {
1079                         l =  System.Int32.MinValue - (long)1;
1080                         failed = true;
1081                         checked {
1082                                 a = (int)l;
1083                         }
1084                 } catch (OverflowException) {
1085                         failed = false;
1086                 }
1087                 if (failed)
1088                         return 8;
1089
1090                 try {
1091                         uint ui = System.UInt32.MaxValue;
1092                         failed = true;
1093                         checked {
1094                                 a = (int)ui;
1095                         }
1096                 }
1097                 catch (OverflowException) {
1098                         failed = false;
1099                 }
1100                 if (failed)
1101                         return 9;
1102
1103                 try {
1104                         ulong ul = (long)(System.Int32.MaxValue) + 1;
1105                         failed = true;
1106                         checked {
1107                                 a = (int)ul;
1108                         }
1109                 }
1110                 catch (OverflowException) {
1111                         failed = false;
1112                 }
1113                 if (failed)
1114                         return 10;
1115
1116                 try {
1117                         ulong ul = UInt64.MaxValue;
1118                         failed = true;
1119                         checked {
1120                                 a = (int)ul;
1121                         }
1122                 }
1123                 catch (OverflowException) {
1124                         failed = false;
1125                 }
1126                 if (failed)
1127                         return 11;
1128
1129                 {
1130                         int i; 
1131                         float f = 1.1f;
1132                         checked {
1133                                 i = (int) f;
1134                         }
1135                 }
1136
1137                 return 0;
1138         }
1139
1140         public static int test_0_uint_cast () {
1141                 uint a;
1142                 long l;
1143                 bool failed;
1144
1145                 try {
1146                         double d =  System.UInt32.MaxValue;
1147                         failed = false;
1148                         checked {
1149                                 a = (uint)d;
1150                         }
1151                 } catch (OverflowException) {
1152                         failed = true;
1153                 }
1154                 if (failed)
1155                         return 1;
1156
1157                 try {
1158                         double d = System.UInt32.MaxValue + 1.0;
1159                         failed = true;
1160                         checked {
1161                                 a = (uint)d;
1162                         }
1163                 } catch (OverflowException) {
1164                         failed = false;
1165                 }
1166                 if (failed)
1167                         return 2;
1168
1169                 try {
1170                         double d = System.UInt32.MinValue;
1171                         failed = false;
1172                         checked {
1173                                 a = (uint)d;
1174                         }
1175                 } catch (OverflowException) {
1176                         failed = true;
1177                 }
1178                 if (failed)
1179                         return 3;
1180
1181                 try {
1182                         double d = System.UInt32.MinValue - 1.0;
1183                         failed = true;
1184                         checked {
1185                                 a = (uint)d;
1186                         }
1187                 } catch (OverflowException) {
1188                         failed = false;
1189                 }
1190                 if (failed)
1191                         return 4;
1192                 
1193                 try {
1194                         l =  System.UInt32.MaxValue;
1195                         failed = false;
1196                         checked {
1197                                 a = (uint)l;
1198                         }
1199                 } catch (OverflowException) {
1200                         failed = true;
1201                 }
1202                 if (failed)
1203                         return 5;
1204
1205                 try {
1206                         l = System.UInt32.MaxValue + (long)1;
1207                         failed = true;
1208                         checked {
1209                                 a = (uint)l;
1210                         }
1211                 } catch (OverflowException) {
1212                         failed = false;
1213                 }
1214                 if (failed)
1215                         return 6;
1216
1217                 try {
1218                         l = System.UInt32.MinValue;
1219                         failed = false;
1220                         checked {
1221                                 a = (uint)l;
1222                         }
1223                 } catch (OverflowException) {
1224                         failed = true;
1225                 }
1226                 if (failed)
1227                         return 7;
1228
1229                 try {
1230                         l = System.UInt32.MinValue - (long)1;
1231                         failed = true;
1232                         checked {
1233                                 a = (uint)l;
1234                         }
1235                 } catch (OverflowException) {
1236                         failed = false;
1237                 }
1238                 if (failed)
1239                         return 8;
1240
1241                 try {
1242                         int i = -1;
1243                         failed = true;
1244                         checked {
1245                                 a = (uint)i;
1246                         }
1247                 }
1248                 catch (OverflowException) {
1249                         failed = false;
1250                 }
1251                 if (failed)
1252                         return 9;
1253
1254                 {
1255                         uint i; 
1256                         float f = 1.1f;
1257                         checked {
1258                                 i = (uint) f;
1259                         }
1260                 }
1261                 
1262                 return 0;
1263         }
1264         
1265         public static int test_0_long_cast () {
1266
1267                 /*
1268                  * These tests depend on properties of x86 fp arithmetic so they won't work
1269                  * on other platforms.
1270                  */
1271                 /*
1272                 long a;
1273                 bool failed;
1274
1275                 try {
1276                         double d = System.Int64.MaxValue - 512.0;
1277                         failed = true;
1278                         checked {
1279                                 a = (long)d;
1280                         }
1281                 } catch (OverflowException) {
1282                         failed = false;
1283                 }
1284                 if (failed)
1285                         return 1;
1286
1287                 try {
1288                         double d = System.Int64.MaxValue - 513.0;
1289                         failed = false;
1290                         checked {
1291                                 a = (long)d;
1292                         }
1293                 } catch (OverflowException) {
1294                         failed = true;
1295                 }
1296                 if (failed)
1297                         return 2;
1298                 
1299                 try {
1300                         double d = System.Int64.MinValue - 1024.0;
1301                         failed = false;                 
1302                         checked {
1303                                 a = (long)d;
1304                         }
1305                 } catch (OverflowException) {
1306                         failed = true;
1307                 }
1308                 if (failed)
1309                         return 3;
1310
1311                 try {
1312                         double d = System.Int64.MinValue - 1025.0;
1313                         failed = true;
1314                         checked {
1315                                 a = (long)d;
1316                         }
1317                 } catch (OverflowException) {
1318                         failed = false;
1319                 }
1320                 if (failed)
1321                         return 4;
1322                 */
1323
1324                 {
1325                         long i; 
1326                         float f = 1.1f;
1327                         checked {
1328                                 i = (long) f;
1329                         }
1330                 }
1331
1332                 return 0;
1333         }
1334
1335         public static int test_0_ulong_cast () {
1336                 ulong a;
1337                 bool failed;
1338
1339                 /*
1340                  * These tests depend on properties of x86 fp arithmetic so they won't work
1341                  * on other platforms.
1342                  */
1343
1344                 /*
1345                 try {
1346                         double d = System.UInt64.MaxValue - 1024.0;
1347                         failed = true;
1348                         checked {
1349                                 a = (ulong)d;
1350                         }
1351                 } catch (OverflowException) {
1352                         failed = false;
1353                 }
1354                 if (failed)
1355                         return 1;
1356
1357                 try {
1358                         double d = System.UInt64.MaxValue - 1025.0;
1359                         failed = false;
1360                         checked {
1361                                 a = (ulong)d;
1362                         }
1363                 } catch (OverflowException) {
1364                         failed = true;
1365                 }
1366                 if (failed)
1367                         return 2;
1368                 */      
1369
1370                 try {
1371                         double d = 0;
1372                         failed = false;                 
1373                         checked {
1374                                 a = (ulong)d;
1375                         }
1376                 } catch (OverflowException) {
1377                         failed = true;
1378                 }
1379                 if (failed)
1380                         return 3;
1381
1382                 try {
1383                         double d = -1;
1384                         failed = true;
1385                         checked {
1386                                 a = (ulong)d;
1387                         }
1388                 } catch (OverflowException) {
1389                         failed = false;
1390                 }
1391                 if (failed)
1392                         return 4;
1393
1394                 {
1395                         ulong i; 
1396                         float f = 1.1f;
1397                         checked {
1398                                 i = (ulong) f;
1399                         }
1400                 }
1401
1402                 try {
1403                         int i = -1;
1404                         failed = true;
1405                         checked {
1406                                 a = (ulong)i;
1407                         }
1408                 }
1409                 catch (OverflowException) {
1410                         failed = false;
1411                 }
1412                 if (failed)
1413                         return 5;
1414
1415                 try {
1416                         int i = Int32.MinValue;
1417                         failed = true;
1418                         checked {
1419                                 a = (ulong)i;
1420                         }
1421                 }
1422                 catch (OverflowException) {
1423                         failed = false;
1424                 }
1425                 if (failed)
1426                         return 6;
1427
1428                 return 0;
1429         }
1430
1431         public static int test_0_simple_double_casts () {
1432
1433                 double d = 0xffffffff;
1434
1435                 if ((uint)d != 4294967295)
1436                         return 1;
1437
1438                 /*
1439                  * These tests depend on properties of x86 fp arithmetic so they won't work
1440                  * on other platforms.
1441                  */
1442                 /*
1443                 d = 0xffffffffffffffff;
1444
1445                 if ((ulong)d != 0)
1446                         return 2;
1447
1448                 if ((ushort)d != 0)
1449                         return 3;
1450                         
1451                 if ((byte)d != 0)
1452                         return 4;
1453                 */
1454                         
1455                 d = 0xffff;
1456
1457                 if ((ushort)d != 0xffff)
1458                         return 5;
1459                 
1460                 if ((byte)d != 0xff)
1461                         return 6;
1462                         
1463                 return 0;
1464         }
1465         
1466         [Category ("!INTERPRETER")]
1467         [Category ("NaClDisable")]
1468         public static int test_0_div_zero () {
1469                 int d = 1;
1470                 int q = 0;
1471                 int val;
1472                 bool failed;
1473
1474                 try {
1475                         failed = true;
1476                         val = d / q;
1477                 } catch (DivideByZeroException) {
1478                         failed = false;
1479                 }
1480                 if (failed)
1481                         return 1;
1482
1483                 try {
1484                         failed = true;
1485                         val = d % q;
1486                 } catch (DivideByZeroException) {
1487                         failed = false;
1488                 }
1489                 if (failed)
1490                         return 2;
1491
1492                 try {
1493                         failed = true;
1494                         q = -1;
1495                         d = Int32.MinValue;
1496                         val = d / q;
1497                 } catch (DivideByZeroException) {
1498                         /* wrong exception */
1499                 } catch (OverflowException) {
1500                         failed = false;
1501                 }
1502                 if (failed)
1503                         return 3;
1504
1505                 try {
1506                         failed = true;
1507                         q = -1;
1508                         d = Int32.MinValue;
1509                         val = d % q;
1510                 } catch (DivideByZeroException) {
1511                         /* wrong exception */
1512                 } catch (OverflowException) {
1513                         failed = false;
1514                 }
1515                 if (failed)
1516                         return 4;
1517
1518                 return 0;
1519         }
1520
1521         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1522         static void dummy () {
1523         }
1524
1525         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1526         static int div_zero_llvm_inner (int i) {
1527                 try {
1528                         // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1529                         dummy ();
1530                         return 5 / i;
1531                 } catch (Exception ex) {
1532                         return 0;
1533                 }
1534         }
1535
1536         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1537         static long div_zero_llvm_inner_long (long l) {
1538                 try {
1539                         dummy ();
1540                         return (long)5 / l;
1541                 } catch (Exception ex) {
1542                         return 0;
1543                 }
1544         }
1545
1546         public static int test_0_div_zero_llvm () {
1547             long r = div_zero_llvm_inner (0);
1548                 if (r != 0)
1549                         return 1;
1550             r = div_zero_llvm_inner_long (0);
1551                 if (r != 0)
1552                         return 2;
1553                 return 0;
1554         }
1555
1556         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1557         static int div_overflow_llvm_inner (int i) {
1558                 try {
1559                         dummy ();
1560                         return Int32.MinValue / i;
1561                 } catch (Exception ex) {
1562                         return 0;
1563                 }
1564         }
1565
1566         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1567         static long div_overflow_llvm_inner_long (long l) {
1568                 try {
1569                         dummy ();
1570                         return Int64.MinValue / l;
1571                 } catch (Exception ex) {
1572                         return 0;
1573                 }
1574         }
1575
1576         public static int test_0_div_overflow_llvm () {
1577                 long r = div_overflow_llvm_inner (-1);
1578                 if (r != 0)
1579                         return 1;
1580                 r = div_overflow_llvm_inner_long ((long)-1);
1581                 if (r != 0)
1582                         return 2;
1583                 return 0;
1584         }
1585
1586         public static int return_55 () {
1587                 return 55;
1588         }
1589
1590         public static int test_0_cfold_div_zero () {
1591                 // Test that constant folding doesn't cause division by zero exceptions
1592                 if (return_55 () != return_55 ()) {
1593                         int d = 1;
1594                         int q = 0;
1595                         int val;                        
1596
1597                         val = d / q;
1598                         val = d % q;
1599
1600                         q = -1;
1601                         d = Int32.MinValue;
1602                         val = d / q;
1603
1604                         q = -1;
1605                         val = d % q;
1606                 }
1607
1608                 return 0;
1609         }
1610
1611         public static int test_0_udiv_zero () {
1612                 uint d = 1;
1613                 uint q = 0;
1614                 uint val;
1615                 bool failed;
1616
1617                 try {
1618                         failed = true;
1619                         val = d / q;
1620                 } catch (DivideByZeroException) {
1621                         failed = false;
1622                 }
1623                 if (failed)
1624                         return 1;
1625
1626                 try {
1627                         failed = true;
1628                         val = d % q;
1629                 } catch (DivideByZeroException) {
1630                         failed = false;
1631                 }
1632                 if (failed)
1633                         return 2;
1634
1635                 return 0;
1636         }
1637
1638         [Category ("!INTERPRETER")]
1639         [Category ("NaClDisable")]
1640         public static int test_0_long_div_zero () {
1641                 long d = 1;
1642                 long q = 0;
1643                 long val;
1644                 bool failed;
1645
1646                 try {
1647                         failed = true;
1648                         val = d / q;
1649                 } catch (DivideByZeroException) {
1650                         failed = false;
1651                 }
1652                 if (failed)
1653                         return 1;
1654
1655                 try {
1656                         failed = true;
1657                         val = d % q;
1658                 } catch (DivideByZeroException) {
1659                         failed = false;
1660                 }
1661                 if (failed)
1662                         return 2;
1663
1664                 try {
1665                         failed = true;
1666                         q = -1;
1667                         d = Int64.MinValue;
1668                         val = d / q;
1669                 } catch (DivideByZeroException) {
1670                         /* wrong exception */
1671                 } catch (ArithmeticException) {
1672                         failed = false;
1673                 }
1674                 if (failed)
1675                         return 3;
1676
1677                 try {
1678                         failed = true;
1679                         q = -1;
1680                         d = Int64.MinValue;
1681                         val = d % q;
1682                 } catch (DivideByZeroException) {
1683                         /* wrong exception */
1684                 } catch (ArithmeticException) {
1685                         failed = false;
1686                 }
1687                 if (failed)
1688                         return 4;
1689
1690                 return 0;
1691         }
1692
1693         public static int test_0_ulong_div_zero () {
1694                 ulong d = 1;
1695                 ulong q = 0;
1696                 ulong val;
1697                 bool failed;
1698
1699                 try {
1700                         failed = true;
1701                         val = d / q;
1702                 } catch (DivideByZeroException) {
1703                         failed = false;
1704                 }
1705                 if (failed)
1706                         return 1;
1707
1708                 try {
1709                         failed = true;
1710                         val = d % q;
1711                 } catch (DivideByZeroException) {
1712                         failed = false;
1713                 }
1714                 if (failed)
1715                         return 2;
1716
1717                 return 0;
1718         }
1719
1720         public static int test_0_float_div_zero () {
1721                 double d = 1;
1722                 double q = 0;
1723                 double val;
1724                 bool failed;
1725
1726                 try {
1727                         failed = false;
1728                         val = d / q;
1729                 } catch (DivideByZeroException) {
1730                         failed = true;
1731                 }
1732                 if (failed)
1733                         return 1;
1734
1735                 try {
1736                         failed = false;
1737                         val = d % q;
1738                 } catch (DivideByZeroException) {
1739                         failed = true;
1740                 }
1741                 if (failed)
1742                         return 2;
1743
1744                 return 0;
1745         }
1746
1747         public static int test_0_invalid_unbox () {
1748
1749                 int i = 123;
1750                 object o = "Some string";
1751                 int res = 1;
1752                 
1753                 try {
1754                         // Illegal conversion; o contains a string not an int
1755                         i = (int) o;   
1756                 } catch (Exception e) {
1757                         if (i ==123)
1758                                 res = 0;
1759                 }
1760
1761                 return res;
1762         }
1763
1764         // Test that double[] can't be cast to double (bug #46027)
1765         public static int test_0_invalid_unbox_arrays () {
1766                 double[] d1 = { 1.0 };
1767                 double[][] d2 = { d1 };
1768                 Array a = d2;
1769
1770                 try {
1771                         foreach (double d in a) {
1772                         }
1773                         return 1;
1774                 }
1775                 catch (InvalidCastException e) {
1776                         return 0;
1777                 }
1778         }
1779
1780         /* bug# 42190, at least mcs generates a leave for the return that
1781          * jumps out of multiple exception clauses: we used to execute just 
1782          * one enclosing finally block.
1783          */
1784         public static int finally_level;
1785         static void do_something () {
1786                 int a = 0;
1787                 try {
1788                         try {
1789                                 return;
1790                         } finally {
1791                                 a = 1;
1792                         }
1793                 } finally {
1794                         finally_level++;
1795                 }
1796         }
1797
1798         public static int test_2_multiple_finally_clauses () {
1799                 finally_level = 0;
1800                 do_something ();
1801                 if (finally_level == 1)
1802                         return 2;
1803                 return 0;
1804         }
1805
1806         [Category ("!INTERPRETER")]
1807         public static int test_3_checked_cast_un () {
1808                 ulong i = 0x8000000034000000;
1809                 long j;
1810
1811                 try {
1812                         checked { j = (long)i; }
1813                 } catch (OverflowException) {
1814                         j = 2;
1815                 }
1816
1817                 if (j != 2)
1818                         return 0;
1819                 return 3;
1820         }
1821         
1822         [Category ("!INTERPRETER")]
1823         public static int test_4_checked_cast () {
1824                 long i;
1825                 ulong j;
1826
1827                 unchecked { i = (long)0x8000000034000000;};
1828                 try {
1829                         checked { j = (ulong)i; }
1830                 } catch (OverflowException) {
1831                         j = 3;
1832                 }
1833
1834                 if (j != 3)
1835                         return 0;
1836                 return 4;
1837         }
1838
1839         static readonly int[] mul_dim_results = new int[] {
1840                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1841                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1842                 2, 0, 2, 1, 2, 8, 
1843                 3, 0, 3, 1, 3, 8, 
1844                 4, 0, 4, 1, 4, 8, 
1845                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1846                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1847                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1848         };
1849
1850         [Category ("!INTERPRETER")]
1851         public static int test_0_multi_dim_array_access () {
1852                 int [,] a = System.Array.CreateInstance (typeof (int),
1853                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1854                 int x, y;
1855                 int result_idx = 0;
1856                 for (x = 0; x < 8; ++x) {
1857                         for (y = 0; y < 9; ++y) {
1858                                 bool got_ex = false;
1859                                 try {
1860                                         a [x, y] = 1;
1861                                 } catch {
1862                                         got_ex = true;
1863                                 }
1864                                 if (got_ex) {
1865                                         if (result_idx >= mul_dim_results.Length)
1866                                                 return -1;
1867                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1868                                                 return result_idx + 1;
1869                                         }
1870                                         result_idx += 2;
1871                                 }
1872                         }
1873                 }
1874                 if (result_idx == mul_dim_results.Length)
1875                         return 0;
1876                 return 200;
1877         }
1878
1879         static void helper_out_obj (out object o) {
1880                 o = (object)"buddy";
1881         }
1882
1883         static void helper_out_string (out string o) {
1884                 o = "buddy";
1885         }
1886
1887         [Category ("!INTERPRETER")]
1888         public static int test_2_array_mismatch () {
1889                 string[] a = { "hello", "world" };
1890                 object[] b = a;
1891                 bool passed = false;
1892
1893                 try {
1894                         helper_out_obj (out b [1]);
1895                 } catch (ArrayTypeMismatchException) {
1896                         passed = true;
1897                 }
1898                 if (!passed)
1899                         return 0;
1900                 helper_out_string (out a [1]);
1901                 if (a [1] != "buddy")
1902                         return 1;
1903                 return 2;
1904         }
1905
1906         public static int test_0_ovf1 () {
1907                 int exception = 0;
1908                 
1909                 checked {
1910                         try {
1911                                 ulong a =  UInt64.MaxValue - 1;
1912                                 ulong t = a++;
1913                         } catch {
1914                                 exception = 1;
1915                         }
1916                 }
1917                 return exception;
1918         }
1919
1920         public static int test_1_ovf2 () {
1921                 int exception = 0;
1922
1923                 checked {
1924                         try {
1925                                 ulong a =  UInt64.MaxValue;
1926                                 ulong t = a++;
1927                         } catch {
1928                                 exception = 1;
1929                         }
1930                 }
1931                 return exception;
1932         }
1933
1934         public static int test_0_ovf3 () {
1935                 int exception = 0;
1936
1937                 long a = Int64.MaxValue - 1;
1938                 checked {
1939                         try {
1940                                 long t = a++;
1941                         } catch {
1942                                 exception = 1;
1943                         }
1944                 }
1945                 return exception;
1946         }
1947
1948         public static int test_1_ovf4 () {
1949                 int exception = 0;
1950
1951                 long a = Int64.MaxValue;
1952                 checked {
1953                         try {
1954                                 long t = a++;
1955                         } catch {
1956                                 exception = 1;
1957                         }
1958                 }
1959                 return exception;
1960         }
1961
1962         public static int test_0_ovf5 () {
1963                 int exception = 0;
1964
1965                 ulong a = UInt64.MaxValue - 1;
1966                 checked {
1967                         try {
1968                                 ulong t = a++;
1969                         } catch {
1970                                 exception = 1;
1971                         }
1972                 }
1973                 return exception;
1974         }
1975
1976         public static int test_1_ovf6 () {
1977                 int exception = 0;
1978
1979                 ulong a = UInt64.MaxValue;
1980                 checked {
1981                         try {
1982                                 ulong t = a++;
1983                         } catch {
1984                                 exception = 1;
1985                         }
1986                 }
1987                 return exception;
1988         }
1989
1990         public static int test_0_ovf7 () {
1991                 int exception = 0;
1992
1993                 long a = Int64.MinValue + 1;
1994                 checked {
1995                         try {
1996                                 long t = a--;
1997                         } catch {
1998                                 exception = 1;
1999                         }
2000                 }
2001                 return 0;
2002         }
2003
2004         public static int test_1_ovf8 () {
2005                 int exception = 0;
2006
2007                 long a = Int64.MinValue;
2008                 checked {
2009                         try {
2010                                 long t = a--;
2011                         } catch {
2012                                 exception = 1;
2013                         }
2014                 }
2015                 return exception;
2016         }
2017
2018         public static int test_0_ovf9 () {
2019                 int exception = 0;
2020
2021                 ulong a = UInt64.MinValue + 1;
2022                 checked {
2023                         try {
2024                                 ulong t = a--;
2025                         } catch {
2026                                 exception = 1;
2027                         }
2028                 }
2029                 return exception;
2030         }
2031
2032         public static int test_1_ovf10 () {
2033                 int exception = 0;
2034
2035                 ulong a = UInt64.MinValue;
2036                 checked {
2037                         try {
2038                                 ulong t = a--;
2039                         } catch {
2040                                 exception = 1;
2041                         }
2042                 }
2043                 return exception;
2044         }
2045
2046         public static int test_0_ovf11 () {
2047                 int exception = 0;
2048
2049                 int a = Int32.MinValue + 1;
2050                 checked {
2051                         try {
2052                                 int t = a--;
2053                         } catch {
2054                                 exception = 1;
2055                         }
2056                 }
2057                 return exception;
2058         }
2059
2060         public static int test_1_ovf12 () {
2061                 int exception = 0;
2062
2063                 int a = Int32.MinValue;
2064                 checked {
2065                         try {
2066                                 int t = a--;
2067                         } catch {
2068                                 exception = 1;
2069                         }
2070                 }
2071                 return exception;
2072         }
2073
2074         public static int test_0_ovf13 () {
2075                 int exception = 0;
2076
2077                 uint a = 1;
2078                 checked {
2079                         try {
2080                                 uint t = a--;
2081                         } catch {
2082                                 exception = 1;
2083                         }
2084                 }
2085                 return exception;
2086         }
2087
2088         public static int test_1_ovf14 () {
2089                 int exception = 0;
2090
2091                 uint a = 0;
2092                 checked {
2093                         try {
2094                                 uint t = a--;
2095                         } catch {
2096                                 exception = 1;
2097                         }
2098                 }
2099                 return exception;
2100         }
2101
2102         public static int test_0_ovf15 () {
2103                 int exception = 0;
2104
2105                 sbyte a = 126;
2106                 checked {
2107                         try {
2108                                 sbyte t = a++;
2109                         } catch {
2110                                 exception = 1;
2111                         }
2112                 }
2113                 return exception;
2114         }
2115
2116         public static int test_1_ovf16 () {
2117                 int exception = 0;
2118
2119                 sbyte a = 127;
2120                 checked {
2121                         try {
2122                                 sbyte t = a++;
2123                         } catch {
2124                                 exception = 1;
2125                         }
2126                 }
2127                 return exception;
2128         }
2129
2130         public static int test_0_ovf17 () {
2131                 int exception = 0;
2132
2133                 checked {
2134                         try {
2135                         } catch {
2136                                 exception = 1;
2137                         }
2138                 }
2139                 return exception;
2140         }
2141
2142         public static int test_0_ovf18 () {
2143                 int exception = 0;
2144
2145                 int a = 1 << 29;
2146                 checked {
2147                         try {
2148                                 int t = a*2;
2149                         } catch {
2150                                 exception = 1;
2151                         }
2152                 }
2153                 return exception;
2154         }
2155
2156         public static int test_1_ovf19 () {
2157                 int exception = 0;
2158
2159                 int a = 1 << 30;
2160                 checked {
2161                         try {
2162                                 int t = a*2;
2163                         } catch {
2164                                 exception = 1;
2165                         }
2166                 }
2167                 return exception;
2168         }
2169
2170         public static int test_0_ovf20 () {
2171                 int exception = 0;
2172
2173                 checked {
2174                         try {
2175                                 ulong a = 0xffffffffff;
2176                                 ulong t = a*0x0ffffff;
2177                         } catch {
2178                                 exception = 1;
2179                         }
2180                 }
2181                 return exception;
2182         }
2183
2184         public static int test_1_ovf21 () {
2185                 int exception = 0;
2186
2187                 ulong a = 0xffffffffff;
2188                 checked {
2189                         try {
2190                                 ulong t = a*0x0fffffff;
2191                         } catch {
2192                                 exception = 1;
2193                         }
2194                 }
2195                 return exception;
2196         }
2197
2198         public static int test_1_ovf22 () {
2199                 int exception = 0;
2200
2201                 long a = Int64.MinValue;
2202                 long b = 10;
2203                 checked {
2204                         try {
2205                                 long v = a * b;
2206                         } catch {
2207                                 exception = 1;
2208                         }
2209                 }
2210                 return exception;
2211         }
2212
2213         public static int test_1_ovf23 () {
2214                 int exception = 0;
2215
2216                 long a = 10;
2217                 long b = Int64.MinValue;
2218                 checked {
2219                         try {
2220                                 long v = a * b;
2221                         } catch {
2222                                 exception = 1;
2223                         }
2224                 }
2225                 return exception;
2226         }
2227
2228         class Broken {
2229                 public static int i;
2230
2231                 static Broken () {
2232                         throw new Exception ("Ugh!");
2233                 }
2234         
2235                 public static int DoSomething () {
2236                         return i;
2237                 }
2238         }
2239
2240         [Category ("!INTERPRETER")]
2241         public static int test_0_exception_in_cctor () {
2242                 try {
2243                         Broken.DoSomething ();
2244                 }
2245                 catch (TypeInitializationException) {
2246                         // This will only happen once even if --regression is used
2247                 }
2248                 return 0;
2249         }
2250
2251         public static int test_5_regalloc () {
2252                 int i = 0;
2253
2254                 try {
2255                         for (i = 0; i < 10; ++i) {
2256                                 if (i == 5)
2257                                         throw new Exception ();
2258                         }
2259                 }
2260                 catch (Exception) {
2261                         if (i != 5)
2262                                 return i;
2263                 }
2264
2265                 // Check that variables written in catch clauses are volatile
2266                 int j = 0;
2267                 try {
2268                         throw new Exception ();
2269                 }
2270                 catch (Exception) {
2271                         j = 5;
2272                 }
2273                 if (j != 5)
2274                         return 6;
2275
2276                 int k = 0;
2277                 try {
2278                         try {
2279                                 throw new Exception ();
2280                         }
2281                         finally {
2282                                 k = 5;
2283                         }
2284                 }
2285                 catch (Exception) {
2286                 }
2287                 if (k != 5)
2288                         return 7;
2289
2290                 return i;
2291         }
2292
2293         public static void rethrow () {
2294                 try {
2295                         throw new ApplicationException();
2296                 } catch (ApplicationException) {
2297                         try {
2298                                 throw new OverflowException();
2299                         } catch (Exception) {
2300                                 throw;
2301                         }
2302                 }
2303         }
2304
2305         // Test that a rethrow rethrows the correct exception
2306         public static int test_0_rethrow_nested () {
2307                 try {
2308                         rethrow ();
2309                 } catch (OverflowException) {
2310                         return 0;
2311                 } catch (Exception) {
2312                         return 1;
2313                 }
2314                 return 2;
2315         }
2316
2317         [MethodImplAttribute (MethodImplOptions.NoInlining)]
2318         public static void rethrow1 () {
2319                 throw new Exception ();
2320         }
2321
2322         [MethodImplAttribute (MethodImplOptions.NoInlining)]
2323         public static void rethrow2 () {
2324                 rethrow1 ();
2325                 /* This disables tailcall opts */
2326                 Console.WriteLine ();
2327         }
2328
2329         [Category ("!INTERPRETER")]
2330         [Category ("!BITCODE")]
2331         public static int test_0_rethrow_stacktrace () {
2332                 // Check that rethrowing an exception preserves the original stack trace
2333                 try {
2334                         try {
2335                                 rethrow2 ();
2336                         }
2337                         catch (Exception ex) {
2338                                 // Check that each catch clause has its own exception variable
2339                                 // If not, the throw below will overwrite the exception used
2340                                 // by the rethrow
2341                                 try {
2342                                         throw new DivideByZeroException ();
2343                                 }
2344                                 catch (Exception foo) {
2345                                 }
2346
2347                                 throw;
2348                         }
2349                 }
2350                 catch (Exception ex) {
2351                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2352                                 return 0;
2353                 }
2354
2355                 return 1;
2356         }
2357         
2358         interface IFace {}
2359         class Face : IFace {}
2360                 
2361         public static int test_1_array_mismatch_2 () {
2362                 try {
2363                         object [] o = new Face [1];
2364                         o [0] = 1;
2365                         return 0;
2366                 } catch (ArrayTypeMismatchException) {
2367                         return 1;
2368                 }
2369         }
2370         
2371         public static int test_1_array_mismatch_3 () {
2372                 try {
2373                         object [] o = new IFace [1];
2374                         o [0] = 1;
2375                         return 0;
2376                 } catch (ArrayTypeMismatchException) {
2377                         return 1;
2378                 }
2379         }
2380         
2381         public static int test_1_array_mismatch_4 () {
2382                 try {
2383                         object [][] o = new Face [5] [];
2384                         o [0] = new object [5];
2385                         
2386                         return 0;
2387                 } catch (ArrayTypeMismatchException) {
2388                         return 1;
2389                 }
2390         }
2391
2392         [Category ("!INTERPRETER")]
2393         public static int test_0_array_size () {
2394                 bool failed;
2395
2396                 try {
2397                         failed = true;
2398                         int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2399                 }
2400                 catch (OutOfMemoryException e) {
2401                         failed = false;
2402                 }
2403                 if (failed)
2404                         return 2;
2405
2406                 return 0;
2407         }
2408
2409         struct S {
2410                 int i, j, k, l, m, n;
2411         }
2412
2413         static IntPtr[] addr;
2414
2415         static unsafe void throw_func (int i, S s) {
2416                 addr [i] = new IntPtr (&i);
2417                 throw new Exception ();
2418         }
2419
2420         /* Test that arguments are correctly popped off the stack during unwinding */
2421         /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2422         /*
2423         public static int test_0_stack_unwind () {
2424                 addr = new IntPtr [1000];
2425                 S s = new S ();
2426                 for (int j = 0; j < 1000; j++) {
2427                         try {
2428                                 throw_func (j, s);
2429                         }
2430                         catch (Exception) {
2431                         }
2432                 }
2433                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2434         }
2435         */
2436
2437         static unsafe void get_sp (int i) {
2438                 addr [i] = new IntPtr (&i);
2439         }
2440
2441         /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2442         public static int test_0_throw_unwind () {
2443                 addr = new IntPtr [1000];
2444                 S s = new S ();
2445                 for (int j = 0; j < 1000; j++) {
2446                         try {
2447                                 get_sp (j);
2448                                 throw new Exception ();
2449                         }
2450                         catch (Exception) {
2451                         }
2452                 }
2453                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2454         }
2455
2456         public static int test_0_regress_73242 () {
2457                 int [] arr = new int [10];
2458                 for (int i = 0; i < 10; ++i)
2459                         arr [i] = 0;
2460                 try {
2461                         throw new Exception ();
2462                 }
2463                 catch {
2464                 }
2465                 return 0;
2466     }
2467
2468         public static int test_0_nullref () {
2469                 try {
2470                         Array foo = null;
2471                         foo.Clone();
2472                 } catch (NullReferenceException e) {
2473                         return 0;
2474                 }
2475                 return 1;
2476         }
2477
2478         public int amethod () {
2479                 return 1;
2480         }
2481
2482         public static int test_0_nonvirt_nullref_at_clause_start () {
2483                 ExceptionTests t = null;
2484                 try {
2485                         t.amethod ();
2486                 } catch (NullReferenceException) {
2487                         return 0;
2488                 }
2489
2490                 return 1;
2491         }
2492
2493         public static int throw_only () {
2494                 throw new Exception ();
2495         }
2496
2497         [MethodImpl(MethodImplOptions.NoInlining)] 
2498         public static int throw_only2 () {
2499                 return throw_only ();
2500         }
2501
2502         public static int test_0_inline_throw_only () {
2503                 try {
2504                         return throw_only2 ();
2505                 }
2506                 catch (Exception ex) {
2507                         return 0;
2508                 }
2509         }
2510
2511         public static string GetText (string s) {
2512                 return s;
2513         }
2514
2515         public static int throw_only_gettext () {
2516                 throw new Exception (GetText ("FOO"));
2517         }
2518
2519         public static int test_0_inline_throw_only_gettext () {
2520                 object o = null;
2521                 try {
2522                         o = throw_only_gettext ();
2523                 }
2524                 catch (Exception ex) {
2525                         return 0;
2526                 }
2527
2528                 return o != null ? 0 : 1;
2529         }
2530
2531         // bug #78633
2532         public static int test_0_throw_to_branch_opt_outer_clause () {
2533                 int i = 0;
2534
2535                 try {
2536                         try {
2537                                 string [] files = new string[1];
2538
2539                                 string s = files[2];
2540                         } finally {
2541                                 i ++;
2542                         }
2543                 } catch {
2544                 }
2545
2546                 return (i == 1) ? 0 : 1;
2547         }               
2548
2549         // bug #485721
2550         public static int test_0_try_inside_finally_cmov_opt () {
2551                 bool Reconect = false;
2552
2553                 object o = new object ();
2554
2555                 try {
2556                 }
2557                 catch (Exception ExCon) {
2558                         if (o != null)
2559                                 Reconect = true;
2560
2561                         try {
2562                         }
2563                         catch (Exception Last) {
2564                         }
2565                 }
2566                 finally {
2567                         if (Reconect == true) {
2568                                 try {
2569                                 }
2570                                 catch (Exception ex) {
2571                                 }
2572                         }
2573                 }
2574
2575                 return 0;
2576         }
2577
2578         public static int test_0_inline_throw () {
2579                 try {
2580                         inline_throw1 (5);
2581                         return 1;
2582                 } catch {
2583                         return 0;
2584                 }
2585         }
2586
2587         // for llvm, the end bblock is unreachable
2588         public static int inline_throw1 (int i) {
2589                 if (i == 0)
2590                         throw new Exception ();
2591                 else
2592                         return inline_throw2 (i);
2593         }
2594
2595         public static int inline_throw2 (int i) {
2596                 throw new Exception ();
2597         }
2598
2599         // bug #539550
2600         public static int test_0_lmf_filter () {
2601                 try {
2602                         // The invoke calls a runtime-invoke wrapper which has a filter clause
2603 #if __MOBILE__
2604                         typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2605 #else
2606                         typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2607 #endif
2608                 } catch (TargetInvocationException) {
2609                 }
2610                 return 0;
2611         }
2612
2613     public static void lmf_filter () {
2614         try {
2615             Connect ();
2616         }
2617         catch {
2618             throw new NotImplementedException ();
2619         }
2620     }
2621
2622     public static void Connect () {
2623         Stop ();
2624         throw new Exception();
2625     }
2626
2627     public static void Stop () {
2628         try {
2629             lock (null) {}
2630         }
2631         catch {
2632         }
2633     }
2634
2635         private static void do_raise () {
2636                 throw new System.Exception ();
2637         }
2638
2639         private static int int_func (int i) {
2640                 return i;
2641         }
2642
2643         // #559876
2644         public static int test_8_local_deadce_causes () {
2645       int myb = 4;
2646   
2647       try {
2648         myb = int_func (8);
2649         do_raise();
2650         myb = int_func (2);
2651       } catch (System.Exception) {
2652                   return myb;
2653           }
2654           return 0;
2655         }
2656
2657         public static int test_0_except_opt_two_clauses () {
2658                 int size;
2659                 size = -1;
2660                 uint ui = (uint)size;
2661                 try {
2662                         checked {
2663                                 uint v = ui * (uint)4;
2664                         }
2665                 } catch (OverflowException e) {
2666                         return 0;
2667                 } catch (Exception) {
2668                         return 1;
2669                 }
2670
2671                 return 2;
2672         }
2673
2674     class Child
2675     {
2676         public virtual long Method()
2677         {
2678             throw new Exception();
2679         }
2680     }
2681
2682         /* #612206 */
2683         public static int test_100_long_vars_in_clauses_initlocals_opt () {
2684                 Child c = new Child();
2685                 long value = 100; 
2686                 try {
2687                         value = c.Method();
2688                 }
2689                 catch {}
2690                 return (int)value;
2691         }
2692
2693         class A {
2694                 public object AnObj;
2695         }
2696
2697         public static void DoSomething (ref object o) {
2698         }
2699
2700         public static int test_0_ldflda_null () {
2701                 A a = null;
2702
2703                 try {
2704                         DoSomething (ref a.AnObj);
2705                 } catch (NullReferenceException) {
2706                         return 0;
2707                 }
2708
2709                 return 1;
2710         }
2711
2712         unsafe struct Foo
2713         {
2714                 public int i;
2715
2716                 public static Foo* pFoo;
2717         }
2718
2719         [Category ("!INTERPRETER")]
2720         /* MS.NET doesn't seem to throw in this case */
2721         public unsafe static int test_0_ldflda_null_pointer () {
2722                 int* pi = &Foo.pFoo->i;
2723
2724                 return 0;
2725         }
2726
2727         static int test_0_try_clause_in_finally_clause_regalloc () {
2728                 // Fill up registers with values
2729                 object a = new object ();
2730                 object[] arr1 = new object [1];
2731                 object[] arr2 = new object [1];
2732                 object[] arr3 = new object [1];
2733                 object[] arr4 = new object [1];
2734                 object[] arr5 = new object [1];
2735
2736                 for (int i = 0; i < 10; ++i)
2737                         arr1 [0] = a;
2738                 for (int i = 0; i < 10; ++i)
2739                         arr2 [0] = a;
2740                 for (int i = 0; i < 10; ++i)
2741                         arr3 [0] = a;
2742                 for (int i = 0; i < 10; ++i)
2743                         arr4 [0] = a;
2744                 for (int i = 0; i < 10; ++i)
2745                         arr5 [0] = a;
2746
2747                 int res = 1;
2748                 try {
2749                         try_clause_in_finally_clause_regalloc_inner (out res);
2750                 } catch (Exception) {
2751                 }
2752                 return res;             
2753         }
2754
2755         public static object Throw () {
2756                 for (int i = 0; i < 10; ++i)
2757                         ;
2758                 throw new Exception ();
2759         }
2760
2761         static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2762                 object o = null;
2763
2764                 res = 1;
2765                 try {
2766                         o = Throw ();
2767                 } catch (Exception) {
2768                         /* Make sure this doesn't branch to the finally */
2769                         throw new DivideByZeroException ();
2770                 } finally {
2771                         try {
2772                                 /* Make sure o is register allocated */
2773                                 if (o == null)
2774                                         res = 0;
2775                                 else
2776                                         res = 1;
2777                                 if (o == null)
2778                                         res = 0;
2779                                 else
2780                                         res = 1;
2781                                 if (o == null)
2782                                         res = 0;
2783                                 else
2784                                         res = 1;
2785                         } catch (DivideByZeroException) {
2786                         }
2787                 }
2788         }
2789
2790     public static bool t_1835_inner () {
2791         bool a = true;
2792         if (a) throw new Exception();
2793         return true;
2794     }
2795
2796         [MethodImpl(MethodImplOptions.NoInlining)] 
2797     public static bool t_1835_inner_2 () {
2798                 bool b = t_1835_inner ();
2799                 return b;
2800         }
2801
2802         public static int test_0_inline_retval_throw_in_branch_1835 () {
2803                 try {
2804                         t_1835_inner_2 ();
2805                 } catch {
2806                         return 0;
2807                 }
2808                 return 1;
2809         }
2810
2811         static bool finally_called = false;
2812
2813         static void regress_30472 (int a, int b) {
2814                         checked {
2815                                 try {
2816                                         int sum = a + b;
2817                                 } finally {
2818                                         finally_called = true;
2819                                 }
2820             }
2821                 }
2822
2823         public static int test_0_regress_30472 () {
2824                 finally_called = false;
2825                 try {
2826                     regress_30472 (Int32.MaxValue - 1, 2);
2827                 } catch (Exception ex) {
2828                 }
2829                 return finally_called ? 0 : 1;
2830         }
2831
2832         static int array_len_1 = 1;
2833
2834         public static int test_0_bounds_check_negative_constant () {
2835                 try {
2836                         byte[] arr = new byte [array_len_1];
2837                         byte b = arr [-1];
2838                         return 1;
2839                 } catch {
2840                 }
2841                 try {
2842                         byte[] arr = new byte [array_len_1];
2843                         arr [-1] = 1;
2844                         return 2;
2845                 } catch {
2846                 }
2847                 return 0;
2848         }
2849
2850         public static int test_0_string_bounds_check_negative_constant () {
2851                 try {
2852                         string s = "A";
2853                         char c = s [-1];
2854                         return 1;
2855                 } catch {
2856                 }
2857                 return 0;
2858         }
2859 }
2860
2861 #if !__MOBILE__
2862 class ExceptionTests : Tests
2863 {
2864 }
2865 #endif