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