2004-08-23 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / exceptions.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         static int test_0_catch () {
33                 Exception x = new Exception ();
34                 
35                 try {
36                         throw x;
37                 } catch (Exception e) {
38                         if (e == x)
39                                 return 0;
40                 }
41                 return 1;
42         }
43
44         static int test_0_finally_without_exc () {
45                 int x;
46                 
47                 try {
48                         x = 1;
49                 } catch (Exception e) {
50                         x = 2;
51                 } finally {
52                         x = 0;
53                 }
54                 
55                 return x;
56         }
57
58         static int test_0_finally () {
59                 int x = 1;
60                 
61                 try {
62                         throw new Exception ();
63                 } catch (Exception e) {
64                         x = 2;
65                 } finally {
66                         x = 0;
67                 }
68                 return x;
69         }
70
71         static int test_0_nested_finally () {
72                 int a;
73
74                 try {
75                         a = 1;
76                 } finally {
77                         try {
78                                 a = 2;
79                         } finally {
80                                 a = 0;
81                         }
82                 }
83                 return a;
84         }               
85
86         static int test_0_byte_cast () {
87                 int a;
88                 long l;
89                 ulong ul;
90                 byte b = 0;
91                 bool failed;
92
93                 try {
94                         a = 255;
95                         failed = false;
96                         checked {
97                                 b = (byte)a;
98                         }
99                 } catch (OverflowException) {
100                         failed = true;
101                 }
102                 if (failed)
103                         return 1;
104                 if (b != 255)
105                         return -1;
106
107                 try {
108                         a = 0;
109                         failed = false;
110                         checked {
111                                 b = (byte)a;
112                         }
113                 } catch (OverflowException) {
114                         failed = true;
115                 }
116                 if (failed)
117                         return 2;
118                 if (b != 0)
119                         return -2;
120
121
122                 try {
123                         a = 256;
124                         failed = true;
125                         checked {
126                                 b = (byte)a;
127                         }
128                 } catch (OverflowException) {
129                         failed = false;
130                 }
131                 if (failed)
132                         return 3;
133                 if (b != 0)
134                         return -3;
135
136                 try {
137                         a = -1;
138                         failed = true;
139                         checked {
140                                 b = (byte)a;
141                         }
142                 } catch (OverflowException) {
143                         failed = false;
144                 }
145                 if (failed)
146                         return 4;
147                 if (b != 0)
148                         return -4;
149
150                 try {
151                         double d = 0;
152                         failed = false;
153                         checked {
154                                 b = (byte)d;
155                         }
156                 } catch (OverflowException) {
157                         failed = true;
158                 }
159                 if (failed)
160                         return 5;
161                 if (b != 0)
162                         return -5;
163                 
164                 try {
165                         double d = -1;
166                         failed = true;
167                         checked {
168                                 b = (byte)d;
169                         }
170                 } catch (OverflowException) {
171                         failed = false;
172                 }
173                 if (failed)
174                         return 6;
175                 if (b != 0)
176                         return -6;
177
178                 try {
179                         double d = 255;
180                         failed = false;
181                         checked {
182                                 b = (byte)d;
183                         }
184                 } catch (OverflowException) {
185                         failed = true;
186                 }
187                 if (failed)
188                         return 7;
189                 if (b != 255)
190                         return -7;
191
192                 try {
193                         double d = 256;
194                         failed = true;
195                         checked {
196                                 b = (byte)d;
197                         }
198                 } catch (OverflowException) {
199                         failed = false;
200                 }
201                 if (failed)
202                         return 8;
203                 if (b != 255)
204                         return -8;
205
206                 try {
207                         l = 255;
208                         failed = false;
209                         checked {
210                                 b = (byte)l;
211                         }
212                 } catch (OverflowException) {
213                         failed = true;
214                 }
215                 if (failed)
216                         return 9;
217                 if (b != 255)
218                         return -9;
219
220                 try {
221                         l = 0;
222                         failed = false;
223                         checked {
224                                 b = (byte)l;
225                         }
226                 } catch (OverflowException) {
227                         failed = true;
228                 }
229                 if (failed)
230                         return 10;
231                 if (b != 0)
232                         return -10;
233
234                 try {
235                         l = 256;
236                         failed = true;
237                         checked {
238                                 b = (byte)l;
239                         }
240                 } catch (OverflowException) {
241                         failed = false;
242                 }
243                 if (failed)
244                         return 11;
245                 if (b != 0)
246                         return -11;
247
248                 try {
249                         l = -1;
250                         failed = true;
251                         checked {
252                                 b = (byte)l;
253                         }
254                 } catch (OverflowException) {
255                         failed = false;
256                 }
257                 if (failed)
258                         return 12;
259                 if (b != 0)
260                         return -12;
261
262                 try {
263                         ul = 256;
264                         failed = true;
265                         checked {
266                                 b = (byte)ul;
267                         }
268                 }
269                 catch (OverflowException) {
270                         failed = false;
271                 }
272                 if (failed)
273                         return 13;
274                 if (b != 0)
275                         return -13;
276
277                 return 0;
278         }
279         
280         static int test_0_sbyte_cast () {
281                 int a;
282                 long l;
283                 sbyte b = 0;
284                 bool failed;
285
286                 try {
287                         a = 255;
288                         failed = true;
289                         checked {
290                                 b = (sbyte)a;
291                         }
292                 } catch (OverflowException) {
293                         failed = false;
294                 }
295                 if (failed)
296                         return 1;
297                 if (b != 0)
298                         return -1;
299
300                 try {
301                         a = 0;
302                         failed = false;
303                         checked {
304                                 b = (sbyte)a;
305                         }
306                 } catch (OverflowException) {
307                         failed = true;
308                 }
309                 if (failed)
310                         return 2;
311                 if (b != 0)
312                         return -2;
313
314                 try {
315                         a = 256;
316                         failed = true;
317                         checked {
318                                 b = (sbyte)a;
319                         }
320                 } catch (OverflowException) {
321                         failed = false;
322                 }
323                 if (failed)
324                         return 3;
325                 if (b != 0)
326                         return -3;
327
328                 try {
329                         a = -129;
330                         failed = true;
331                         checked {
332                                 b = (sbyte)a;
333                         }
334                 } catch (OverflowException) {
335                         failed = false;
336                 }
337                 if (failed)
338                         return 4;
339                 if (b != 0)
340                         return -4;
341
342                 try {
343                         a = -1;
344                         failed = false;
345                         checked {
346                                 b = (sbyte)a;
347                         }
348                 } catch (OverflowException) {
349                         failed = true;
350                 }
351                 if (failed)
352                         return 5;
353                 if (b != -1)
354                         return -5;
355
356                 try {
357                         a = -128;
358                         failed = false;
359                         checked {
360                                 b = (sbyte)a;
361                         }
362                 } catch (OverflowException) {
363                         failed = true;
364                 }
365                 if (failed)
366                         return 6;
367                 if (b != -128)
368                         return -6;
369
370                 try {
371                         a = 127;
372                         failed = false;
373                         checked {
374                                 b = (sbyte)a;
375                         }
376                 } catch (OverflowException) {
377                         failed = true;
378                 }
379                 if (failed)
380                         return 7;
381                 if (b != 127)
382                         return -7;
383
384                 try {
385                         a = 128;
386                         failed = true;
387                         checked {
388                                 b = (sbyte)a;
389                         }
390                 } catch (OverflowException) {
391                         failed = false;
392                 }
393                 if (failed)
394                         return 8;
395                 if (b != 127)
396                         return -8;
397
398                 try {
399                         double d = 127;
400                         failed = false;
401                         checked {
402                                 b = (sbyte)d;
403                         }
404                 } catch (OverflowException) {
405                         failed = true;
406                 }
407                 if (failed)
408                         return 9;
409                 if (b != 127)
410                         return -9;
411
412                 try {
413                         double d = -128;
414                         failed = false;
415                         checked {
416                                 b = (sbyte)d;
417                         }
418                 } catch (OverflowException) {
419                         failed = true;
420                 }
421                 if (failed)
422                         return 10;
423                 if (b != -128)
424                         return -10;
425
426                 try {
427                         double d = 128;
428                         failed = true;
429                         checked {
430                                 b = (sbyte)d;
431                         }
432                 } catch (OverflowException) {
433                         failed = false;
434                 }
435                 if (failed)
436                         return 11;
437                 if (b != -128)
438                         return -11;
439
440                 try {
441                         double d = -129;
442                         failed = true;
443                         checked {
444                                 b = (sbyte)d;
445                         }
446                 } catch (OverflowException) {
447                         failed = false;
448                 }
449                 if (failed)
450                         return 12;
451                 if (b != -128)
452                         return -12;
453
454                 try {
455                         l = 255;
456                         failed = true;
457                         checked {
458                                 b = (sbyte)l;
459                         }
460                 } catch (OverflowException) {
461                         failed = false;
462                 }
463                 if (failed)
464                         return 13;
465                 if (b != -128)
466                         return -13;
467
468                 try {
469                         l = 0;
470                         failed = false;
471                         checked {
472                                 b = (sbyte)l;
473                         }
474                 } catch (OverflowException) {
475                         failed = true;
476                 }
477                 if (failed)
478                         return 14;
479                 if (b != 0)
480                         return -14;
481
482                 try {
483                         l = 256;
484                         failed = true;
485                         checked {
486                                 b = (sbyte)l;
487                         }
488                 } catch (OverflowException) {
489                         failed = false;
490                 }
491                 if (failed)
492                         return 15;
493                 if (b != 0)
494                         return -15;
495
496                 try {
497                         l = -129;
498                         failed = true;
499                         checked {
500                                 b = (sbyte)l;
501                         }
502                 } catch (OverflowException) {
503                         failed = false;
504                 }
505                 if (failed)
506                         return 16;
507                 if (b != 0)
508                         return -16;
509
510                 try {
511                         l = -1;
512                         failed = false;
513                         checked {
514                                 b = (sbyte)l;
515                         }
516                 } catch (OverflowException) {
517                         failed = true;
518                 }
519                 if (failed)
520                         return 17;
521                 if (b != -1)
522                         return -17;
523
524                 try {
525                         l = -128;
526                         failed = false;
527                         checked {
528                                 b = (sbyte)l;
529                         }
530                 } catch (OverflowException) {
531                         failed = true;
532                 }
533                 if (failed)
534                         return 18;
535                 if (b != -128)
536                         return -18;
537
538                 try {
539                         l = 127;
540                         failed = false;
541                         checked {
542                                 b = (sbyte)l;
543                         }
544                 } catch (OverflowException) {
545                         failed = true;
546                 }
547                 if (failed)
548                         return 19;
549                 if (b != 127)
550                         return -19;
551
552                 try {
553                         l = 128;
554                         failed = true;
555                         checked {
556                                 b = (sbyte)l;
557                         }
558                 } catch (OverflowException) {
559                         failed = false;
560                 }
561                 if (failed)
562                         return 20;
563                 if (b != 127)
564                         return -19;
565
566                 return 0;
567         }
568
569         static int test_0_ushort_cast () {
570                 int a;
571                 long l;
572                 ulong ul;
573                 ushort b;
574                 bool failed;
575
576                 try {
577                         a = System.UInt16.MaxValue;
578                         failed = false;
579                         checked {
580                                 b = (ushort)a;
581                         }
582                 } catch (OverflowException) {
583                         failed = true;
584                 }
585                 if (failed)
586                         return 1;
587
588                 try {
589                         a = 0;
590                         failed = false;
591                         checked {
592                                 b = (ushort)a;
593                         }
594                 } catch (OverflowException) {
595                         failed = true;
596                 }
597                 if (failed)
598                         return 2;
599
600                 try {
601                         a = System.UInt16.MaxValue + 1;
602                         failed = true;
603                         checked {
604                                 b = (ushort)a;
605                         }
606                 } catch (OverflowException) {
607                         failed = false;
608                 }
609                 if (failed)
610                         return 3;
611
612                 try {
613                         a = -1;
614                         failed = true;
615                         checked {
616                                 b = (ushort)a;
617                         }
618                 } catch (OverflowException) {
619                         failed = false;
620                 }
621                 if (failed)
622                         return 4;
623
624                 try {
625                         double d = 0;
626                         failed = false;
627                         checked {
628                                 b = (ushort)d;
629                         }
630                 } catch (OverflowException) {
631                         failed = true;
632                 }
633                 if (failed)
634                         return 5;
635
636                 try {
637                         double d = System.UInt16.MaxValue;
638                         failed = false;
639                         checked {
640                                 b = (ushort)d;
641                         }
642                 } catch (OverflowException) {
643                         failed = true;
644                 }
645                 if (failed)
646                         return 6;
647
648                 try {
649                         double d = -1;
650                         failed = true;
651                         checked {
652                                 b = (ushort)d;
653                         }
654                 } catch (OverflowException) {
655                         failed = false;
656                 }
657                 if (failed)
658                         return 7;
659
660                 try {
661                         double d = System.UInt16.MaxValue + 1.0;
662                         failed = true;
663                         checked {
664                                 b = (ushort)d;
665                         }
666                 } catch (OverflowException) {
667                         failed = false;
668                 }
669                 if (failed)
670                         return 8;
671
672                 try {
673                         l = System.UInt16.MaxValue;
674                         failed = false;
675                         checked {
676                                 b = (ushort)l;
677                         }
678                 } catch (OverflowException) {
679                         failed = true;
680                 }
681                 if (failed)
682                         return 9;
683
684                 try {
685                         l = 0;
686                         failed = false;
687                         checked {
688                                 b = (ushort)l;
689                         }
690                 } catch (OverflowException) {
691                         failed = true;
692                 }
693                 if (failed)
694                         return 10;
695
696                 try {
697                         l = System.UInt16.MaxValue + 1;
698                         failed = true;
699                         checked {
700                                 b = (ushort)l;
701                         }
702                 } catch (OverflowException) {
703                         failed = false;
704                 }
705                 if (failed)
706                         return 11;
707
708                 try {
709                         l = -1;
710                         failed = true;
711                         checked {
712                                 b = (ushort)l;
713                         }
714                 } catch (OverflowException) {
715                         failed = false;
716                 }
717                 if (failed)
718                         return 12;
719
720                 try {
721                         ul = 0xfffff;
722                         failed = true;
723                         checked {
724                                 b = (ushort)ul;
725                         }
726                 } catch (OverflowException) {
727                         failed = false;
728                 }
729                 if (failed)
730                         return 13;
731
732                 return 0;
733         }
734         
735         static int test_0_short_cast () {
736                 int a;
737                 long l;
738                 short b;
739                 bool failed;
740
741                 try {
742                         a = System.UInt16.MaxValue;
743                         failed = true;
744                         checked {
745                                 b = (short)a;
746                         }
747                 } catch (OverflowException) {
748                         failed = false;
749                 }
750                 if (failed)
751                         return 1;
752
753                 try {
754                         a = 0;
755                         failed = false;
756                         checked {
757                                 b = (short)a;
758                         }
759                 } catch (OverflowException) {
760                         failed = true;
761                 }
762                 if (failed)
763                         return 2;
764
765                 try {
766                         a = System.Int16.MaxValue + 1;
767                         failed = true;
768                         checked {
769                                 b = (short)a;
770                         }
771                 } catch (OverflowException) {
772                         failed = false;
773                 }
774                 if (failed)
775                         return 3;
776
777                 try {
778                         a = System.Int16.MinValue - 1;
779                         failed = true;
780                         checked {
781                                 b = (short)a;
782                         }
783                 } catch (OverflowException) {
784                         failed = false;
785                 }
786                 if (failed)
787                         return 4;
788
789                 try {
790                         a = -1;
791                         failed = false;
792                         checked {
793                                 b = (short)a;
794                         }
795                 } catch (OverflowException) {
796                         failed = true;
797                 }
798                 if (failed)
799                         return 5;
800
801                 try {
802                         a = System.Int16.MinValue;
803                         failed = false;
804                         checked {
805                                 b = (short)a;
806                         }
807                 } catch (OverflowException) {
808                         failed = true;
809                 }
810                 if (failed)
811                         return 6;
812
813                 try {
814                         a = System.Int16.MaxValue;
815                         failed = false;
816                         checked {
817                                 b = (short)a;
818                         }
819                 } catch (OverflowException) {
820                         failed = true;
821                 }
822                 if (failed)
823                         return 7;
824
825                 try {
826                         a = System.Int16.MaxValue + 1;
827                         failed = true;
828                         checked {
829                                 b = (short)a;
830                         }
831                 } catch (OverflowException) {
832                         failed = false;
833                 }
834                 if (failed)
835                         return 8;
836
837                 try {
838                         double d = System.Int16.MaxValue;
839                         failed = false;
840                         checked {
841                                 b = (short)d;
842                         }
843                 } catch (OverflowException) {
844                         failed = true;
845                 }
846                 if (failed)
847                         return 9;
848                 
849                 try {
850                         double d = System.Int16.MinValue;
851                         failed = false;
852                         checked {
853                                 b = (short)d;
854                         }
855                 } catch (OverflowException) {
856                         failed = true;
857                 }
858                 if (failed)
859                         return 10;
860                 
861                 try {
862                         double d = System.Int16.MaxValue + 1.0;
863                         failed = true;
864                         checked {
865                                 b = (short)d;
866                         }
867                 } catch (OverflowException) {
868                         failed = false;
869                 }
870                 if (failed)
871                         return 11;
872
873                 try {
874                         double d = System.Int16.MinValue - 1.0;
875                         failed = true;
876                         checked {
877                                 b = (short)d;
878                         }
879                 } catch (OverflowException) {
880                         failed = false;
881                 }
882                 if (failed)
883                         return 12;
884
885                 try {
886                         l = System.Int16.MaxValue + 1;
887                         failed = true;
888                         checked {
889                                 b = (short)l;
890                         }
891                 } catch (OverflowException) {
892                         failed = false;
893                 }
894                 if (failed)
895                         return 13;
896
897                 try {
898                         l = System.Int16.MaxValue;
899                         failed = false;
900                         checked {
901                                 b = (short)l;
902                         }
903                 } catch (OverflowException) {
904                         failed = true;
905                 }
906                 if (failed)
907                         return 14;
908
909                 try {
910                         l = System.Int16.MinValue - 1;
911                         failed = true;
912                         checked {
913                                 b = (short)l;
914                         }
915                 } catch (OverflowException) {
916                         failed = false;
917                 }
918                 if (failed)
919                         return 15;
920
921                 
922                 try {
923                         l = System.Int16.MinValue;
924                         failed = false;
925                         checked {
926                                 b = (short)l;
927                         }
928                 } catch (OverflowException) {
929                         failed = true;
930                 }
931                 if (failed)
932                         return 16;
933
934                 return 0;
935         }
936         
937         static int test_0_int_cast () {
938                 int a;
939                 long l;
940                 bool failed;
941
942                 try {
943                         double d = System.Int32.MaxValue + 1.0;
944                         failed = true;
945                         checked {
946                                 a = (int)d;
947                         }
948                 } catch (OverflowException) {
949                         failed = false;
950                 }
951                 if (failed)
952                         return 1;
953
954                 try {
955                         double d = System.Int32.MaxValue;
956                         failed = false;
957                         checked {
958                                 a = (int)d;
959                         }
960                 } catch (OverflowException) {
961                         failed = true;
962                 }
963                 if (failed)
964                         return 2;
965                 
966
967                 try {
968                         double d = System.Int32.MinValue;
969                         failed = false;                 
970                         checked {
971                                 a = (int)d;
972                         }
973                 } catch (OverflowException) {
974                         failed = true;
975                 }
976                 if (failed)
977                         return 3;
978
979
980                 try {
981                         double d =  System.Int32.MinValue - 1.0;
982                         failed = true;
983                         checked {
984                                 a = (int)d;
985                         }
986                 } catch (OverflowException) {
987                         failed = false;
988                 }
989                 if (failed)
990                         return 4;
991
992                 try {
993                         l = System.Int32.MaxValue + (long)1;
994                         failed = true;
995                         checked {
996                                 a = (int)l;
997                         }
998                 } catch (OverflowException) {
999                         failed = false;
1000                 }
1001                 if (failed)
1002                         return 5;
1003
1004                 try {
1005                         l = System.Int32.MaxValue;
1006                         failed = false;
1007                         checked {
1008                                 a = (int)l;
1009                         }
1010                 } catch (OverflowException) {
1011                         failed = true;
1012                 }
1013                 if (failed)
1014                         return 6;
1015                 
1016
1017                 try {
1018                         l = System.Int32.MinValue;
1019                         failed = false;                 
1020                         checked {
1021                                 a = (int)l;
1022                         }
1023                 } catch (OverflowException) {
1024                         failed = true;
1025                 }
1026                 if (failed)
1027                         return 7;
1028
1029
1030                 try {
1031                         l =  System.Int32.MinValue - (long)1;
1032                         failed = true;
1033                         checked {
1034                                 a = (int)l;
1035                         }
1036                 } catch (OverflowException) {
1037                         failed = false;
1038                 }
1039                 if (failed)
1040                         return 8;
1041
1042                 {
1043                         int i; 
1044                         float f = 1.1f;
1045                         checked {
1046                                 i = (int) f;
1047                         }
1048                 }
1049
1050                 return 0;
1051         }
1052
1053         static int test_0_uint_cast () {
1054                 uint a;
1055                 long l;
1056                 bool failed;
1057
1058                 try {
1059                         double d =  System.UInt32.MaxValue;
1060                         failed = false;
1061                         checked {
1062                                 a = (uint)d;
1063                         }
1064                 } catch (OverflowException) {
1065                         failed = true;
1066                 }
1067                 if (failed)
1068                         return 1;
1069
1070                 try {
1071                         double d = System.UInt32.MaxValue + 1.0;
1072                         failed = true;
1073                         checked {
1074                                 a = (uint)d;
1075                         }
1076                 } catch (OverflowException) {
1077                         failed = false;
1078                 }
1079                 if (failed)
1080                         return 2;
1081
1082                 try {
1083                         double d = System.UInt32.MinValue;
1084                         failed = false;
1085                         checked {
1086                                 a = (uint)d;
1087                         }
1088                 } catch (OverflowException) {
1089                         failed = true;
1090                 }
1091                 if (failed)
1092                         return 3;
1093
1094                 try {
1095                         double d = System.UInt32.MinValue - 1.0;
1096                         failed = true;
1097                         checked {
1098                                 a = (uint)d;
1099                         }
1100                 } catch (OverflowException) {
1101                         failed = false;
1102                 }
1103                 if (failed)
1104                         return 4;
1105                 
1106                 try {
1107                         l =  System.UInt32.MaxValue;
1108                         failed = false;
1109                         checked {
1110                                 a = (uint)l;
1111                         }
1112                 } catch (OverflowException) {
1113                         failed = true;
1114                 }
1115                 if (failed)
1116                         return 5;
1117
1118                 try {
1119                         l = System.UInt32.MaxValue + (long)1;
1120                         failed = true;
1121                         checked {
1122                                 a = (uint)l;
1123                         }
1124                 } catch (OverflowException) {
1125                         failed = false;
1126                 }
1127                 if (failed)
1128                         return 6;
1129
1130                 try {
1131                         l = System.UInt32.MinValue;
1132                         failed = false;
1133                         checked {
1134                                 a = (uint)l;
1135                         }
1136                 } catch (OverflowException) {
1137                         failed = true;
1138                 }
1139                 if (failed)
1140                         return 7;
1141
1142                 try {
1143                         l = System.UInt32.MinValue - (long)1;
1144                         failed = true;
1145                         checked {
1146                                 a = (uint)l;
1147                         }
1148                 } catch (OverflowException) {
1149                         failed = false;
1150                 }
1151                 if (failed)
1152                         return 8;
1153
1154                 {
1155                         uint i; 
1156                         float f = 1.1f;
1157                         checked {
1158                                 i = (uint) f;
1159                         }
1160                 }
1161                 
1162                 return 0;
1163         }
1164         
1165         static int test_0_long_cast () {
1166                 long a;
1167                 bool failed;
1168
1169                 try {
1170                         double d = System.Int64.MaxValue - 512.0;
1171                         failed = true;
1172                         checked {
1173                                 a = (long)d;
1174                         }
1175                 } catch (OverflowException) {
1176                         failed = false;
1177                 }
1178                 if (failed)
1179                         return 1;
1180
1181                 try {
1182                         double d = System.Int64.MaxValue - 513.0;
1183                         failed = false;
1184                         checked {
1185                                 a = (long)d;
1186                         }
1187                 } catch (OverflowException) {
1188                         failed = true;
1189                 }
1190                 if (failed)
1191                         return 2;
1192                 
1193
1194                 try {
1195                         double d = System.Int64.MinValue - 1024.0;
1196                         failed = false;                 
1197                         checked {
1198                                 a = (long)d;
1199                         }
1200                 } catch (OverflowException) {
1201                         failed = true;
1202                 }
1203                 if (failed)
1204                         return 3;
1205
1206                 try {
1207                         double d = System.Int64.MinValue - 1025.0;
1208                         failed = true;
1209                         checked {
1210                                 a = (long)d;
1211                         }
1212                 } catch (OverflowException) {
1213                         failed = false;
1214                 }
1215                 if (failed)
1216                         return 4;
1217
1218                 {
1219                         long i; 
1220                         float f = 1.1f;
1221                         checked {
1222                                 i = (long) f;
1223                         }
1224                 }
1225
1226                 return 0;
1227         }
1228
1229         static int test_0_ulong_cast () {
1230                 ulong a;
1231                 bool failed;
1232
1233                 try {
1234                         double d = System.UInt64.MaxValue - 1024.0;
1235                         failed = true;
1236                         checked {
1237                                 a = (ulong)d;
1238                         }
1239                 } catch (OverflowException) {
1240                         failed = false;
1241                 }
1242                 if (failed)
1243                         return 1;
1244
1245                 try {
1246                         double d = System.UInt64.MaxValue - 1025.0;
1247                         failed = false;
1248                         checked {
1249                                 a = (ulong)d;
1250                         }
1251                 } catch (OverflowException) {
1252                         failed = true;
1253                 }
1254                 if (failed)
1255                         return 2;
1256                 
1257
1258                 try {
1259                         double d = 0;
1260                         failed = false;                 
1261                         checked {
1262                                 a = (ulong)d;
1263                         }
1264                 } catch (OverflowException) {
1265                         failed = true;
1266                 }
1267                 if (failed)
1268                         return 3;
1269
1270                 try {
1271                         double d = -1;
1272                         failed = true;
1273                         checked {
1274                                 a = (ulong)d;
1275                         }
1276                 } catch (OverflowException) {
1277                         failed = false;
1278                 }
1279                 if (failed)
1280                         return 4;
1281
1282                 {
1283                         ulong i; 
1284                         float f = 1.1f;
1285                         checked {
1286                                 i = (ulong) f;
1287                         }
1288                 }
1289
1290                 try {
1291                         int i = -1;
1292                         failed = true;
1293                         checked {
1294                                 a = (ulong)i;
1295                         }
1296                 }
1297                 catch (OverflowException) {
1298                         failed = false;
1299                 }
1300                 if (failed)
1301                         return 5;
1302
1303                 try {
1304                         int i = Int32.MinValue;
1305                         failed = true;
1306                         checked {
1307                                 a = (ulong)i;
1308                         }
1309                 }
1310                 catch (OverflowException) {
1311                         failed = false;
1312                 }
1313                 if (failed)
1314                         return 6;
1315
1316                 return 0;
1317         }
1318
1319         static int test_0_simple_double_casts () {
1320
1321                 double d = 0xffffffff;
1322
1323                 if ((uint)d != 4294967295)
1324                         return 1;
1325
1326                 d = 0xffffffffffffffff;
1327
1328                 if ((ulong)d != 0)
1329                         return 2;
1330
1331                 if ((ushort)d != 0)
1332                         return 3;
1333                         
1334                 if ((byte)d != 0)
1335                         return 4;
1336                         
1337                 d = 0xffff;
1338
1339                 if ((ushort)d != 0xffff)
1340                         return 5;
1341                 
1342                 if ((byte)d != 0xff)
1343                         return 6;
1344                         
1345                 return 0;
1346         }
1347         
1348         static int test_0_div_zero () {
1349                 int d = 1;
1350                 int q = 0;
1351                 int val;
1352                 bool failed;
1353
1354                 try {
1355                         failed = true;
1356                         val = d / q;
1357                 } catch (DivideByZeroException) {
1358                         failed = false;
1359                 }
1360                 if (failed)
1361                         return 1;
1362
1363                 try {
1364                         failed = true;
1365                         val = d % q;
1366                 } catch (DivideByZeroException) {
1367                         failed = false;
1368                 }
1369                 if (failed)
1370                         return 2;
1371
1372                 try {
1373                         failed = true;
1374                         q = -1;
1375                         d = Int32.MinValue;
1376                         val = d / q;
1377                 } catch (DivideByZeroException) {
1378                         /* wrong exception */
1379                 } catch (ArithmeticException) {
1380                         failed = false;
1381                 }
1382                 if (failed)
1383                         return 3;
1384
1385                 try {
1386                         failed = true;
1387                         q = -1;
1388                         d = Int32.MinValue;
1389                         val = d % q;
1390                 } catch (DivideByZeroException) {
1391                         /* wrong exception */
1392                 } catch (ArithmeticException) {
1393                         failed = false;
1394                 }
1395                 if (failed)
1396                         return 4;
1397
1398                 return 0;
1399         }
1400
1401         static int return_55 () {
1402                 return 55;
1403         }
1404
1405         static int test_0_cfold_div_zero () {
1406                 // Test that constant folding doesn't cause division by zero exceptions
1407                 if (return_55 () != return_55 ()) {
1408                         int d = 1;
1409                         int q = 0;
1410                         int val;                        
1411
1412                         val = d / q;
1413                         val = d % q;
1414
1415                         q = -1;
1416                         d = Int32.MinValue;
1417                         val = d / q;
1418
1419                         q = -1;
1420                         val = d % q;
1421                 }
1422
1423                 return 0;
1424         }
1425
1426         static int test_0_udiv_zero () {
1427                 uint d = 1;
1428                 uint q = 0;
1429                 uint val;
1430                 bool failed;
1431
1432                 try {
1433                         failed = true;
1434                         val = d / q;
1435                 } catch (DivideByZeroException) {
1436                         failed = false;
1437                 }
1438                 if (failed)
1439                         return 1;
1440
1441                 try {
1442                         failed = true;
1443                         val = d % q;
1444                 } catch (DivideByZeroException) {
1445                         failed = false;
1446                 }
1447                 if (failed)
1448                         return 2;
1449
1450                 return 0;
1451         }
1452
1453         static int test_0_long_div_zero () {
1454                 long d = 1;
1455                 long q = 0;
1456                 long val;
1457                 bool failed;
1458
1459                 try {
1460                         failed = true;
1461                         val = d / q;
1462                 } catch (DivideByZeroException) {
1463                         failed = false;
1464                 }
1465                 if (failed)
1466                         return 1;
1467
1468                 try {
1469                         failed = true;
1470                         val = d % q;
1471                 } catch (DivideByZeroException) {
1472                         failed = false;
1473                 }
1474                 if (failed)
1475                         return 2;
1476
1477                 try {
1478                         failed = true;
1479                         q = -1;
1480                         d = Int64.MinValue;
1481                         val = d / q;
1482                 } catch (DivideByZeroException) {
1483                         /* wrong exception */
1484                 } catch (ArithmeticException) {
1485                         failed = false;
1486                 }
1487                 if (failed)
1488                         return 3;
1489
1490                 try {
1491                         failed = true;
1492                         q = -1;
1493                         d = Int64.MinValue;
1494                         val = d % q;
1495                 } catch (DivideByZeroException) {
1496                         /* wrong exception */
1497                 } catch (ArithmeticException) {
1498                         failed = false;
1499                 }
1500                 if (failed)
1501                         return 4;
1502
1503                 return 0;
1504         }
1505
1506         static int test_0_ulong_div_zero () {
1507                 ulong d = 1;
1508                 ulong q = 0;
1509                 ulong val;
1510                 bool failed;
1511
1512                 try {
1513                         failed = true;
1514                         val = d / q;
1515                 } catch (DivideByZeroException) {
1516                         failed = false;
1517                 }
1518                 if (failed)
1519                         return 1;
1520
1521                 try {
1522                         failed = true;
1523                         val = d % q;
1524                 } catch (DivideByZeroException) {
1525                         failed = false;
1526                 }
1527                 if (failed)
1528                         return 2;
1529
1530                 return 0;
1531         }
1532
1533         static int test_0_float_div_zero () {
1534                 double d = 1;
1535                 double q = 0;
1536                 double val;
1537                 bool failed;
1538
1539                 try {
1540                         failed = false;
1541                         val = d / q;
1542                 } catch (DivideByZeroException) {
1543                         failed = true;
1544                 }
1545                 if (failed)
1546                         return 1;
1547
1548                 try {
1549                         failed = false;
1550                         val = d % q;
1551                 } catch (DivideByZeroException) {
1552                         failed = true;
1553                 }
1554                 if (failed)
1555                         return 2;
1556
1557                 return 0;
1558         }
1559
1560         static int test_0_invalid_unbox () {
1561
1562                 int i = 123;
1563                 object o = "Some string";
1564                 int res = 1;
1565                 
1566                 try {
1567                         // Illegal conversion; o contains a string not an int
1568                         i = (int) o;   
1569                 } catch (Exception e) {
1570                         if (i ==123)
1571                                 res = 0;
1572                 }
1573
1574                 return res;
1575         }
1576
1577         // Test that double[] can't be cast to double (bug #46027)
1578         static int test_0_invalid_unbox_arrays () {
1579                 double[] d1 = { 1.0 };
1580                 double[][] d2 = { d1 };
1581                 Array a = d2;
1582
1583                 try {
1584                         foreach (double d in a) {
1585                         }
1586                         return 1;
1587                 }
1588                 catch (InvalidCastException e) {
1589                         return 0;
1590                 }
1591         }
1592
1593         /* bug# 42190, at least mcs generates a leave for the return that
1594          * jumps out of multiple exception clauses: we used to execute just 
1595          * one enclosing finally block.
1596          */
1597         static int finally_level;
1598         static void do_something () {
1599                 int a = 0;
1600                 try {
1601                         try {
1602                                 return;
1603                         } finally {
1604                                 a = 1;
1605                         }
1606                 } finally {
1607                         finally_level++;
1608                 }
1609         }
1610
1611         static int test_2_multiple_finally_clauses () {
1612                 finally_level = 0;
1613                 do_something ();
1614                 if (finally_level == 1)
1615                         return 2;
1616                 return 0;
1617         }
1618
1619         static int test_3_checked_cast_un () {
1620                 ulong i = 0x8000000034000000;
1621                 long j;
1622
1623                 try {
1624                         checked { j = (long)i; }
1625                 } catch (OverflowException) {
1626                         j = 2;
1627                 }
1628
1629                 if (j != 2)
1630                         return 0;
1631                 return 3;
1632         }
1633         
1634         static int test_4_checked_cast () {
1635                 long i;
1636                 ulong j;
1637
1638                 unchecked { i = (long)0x8000000034000000;};
1639                 try {
1640                         checked { j = (ulong)i; }
1641                 } catch (OverflowException) {
1642                         j = 3;
1643                 }
1644
1645                 if (j != 3)
1646                         return 0;
1647                 return 4;
1648         }
1649
1650         static readonly int[] mul_dim_results = new int[] {
1651                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1652                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1653                 2, 0, 2, 1, 2, 8, 
1654                 3, 0, 3, 1, 3, 8, 
1655                 4, 0, 4, 1, 4, 8, 
1656                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1657                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1658                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1659         };
1660
1661         static int test_0_multi_dim_array_access () {
1662                 int [,] a = System.Array.CreateInstance (typeof (int),
1663                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1664                 int x, y;
1665                 int result_idx = 0;
1666                 for (x = 0; x < 8; ++x) {
1667                         for (y = 0; y < 9; ++y) {
1668                                 bool got_ex = false;
1669                                 try {
1670                                         a [x, y] = 1;
1671                                 } catch {
1672                                         got_ex = true;
1673                                 }
1674                                 if (got_ex) {
1675                                         if (result_idx >= mul_dim_results.Length)
1676                                                 return -1;
1677                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1678                                                 return result_idx + 1;
1679                                         }
1680                                         result_idx += 2;
1681                                 }
1682                         }
1683                 }
1684                 if (result_idx == mul_dim_results.Length)
1685                         return 0;
1686                 return 200;
1687         }
1688
1689         static void helper_out_obj (out object o) {
1690                 o = (object)"buddy";
1691         }
1692
1693         static void helper_out_string (out string o) {
1694                 o = "buddy";
1695         }
1696
1697         static int test_2_array_mismatch () {
1698                 string[] a = { "hello", "world" };
1699                 object[] b = a;
1700                 bool passed = false;
1701
1702                 try {
1703                         helper_out_obj (out b [1]);
1704                 } catch (ArrayTypeMismatchException) {
1705                         passed = true;
1706                 }
1707                 if (!passed)
1708                         return 0;
1709                 helper_out_string (out a [1]);
1710                 if (a [1] != "buddy")
1711                         return 1;
1712                 return 2;
1713         }
1714
1715         static int test_0_ovf () {
1716                 int ocount = 0;
1717                 
1718                 checked {
1719
1720                         ocount = 0;
1721                         try {
1722                                 ulong a =  UInt64.MaxValue - 1;
1723                                 ulong t = a++;
1724                         } catch {
1725                                 ocount++;
1726                         }
1727                         if (ocount != 0)
1728                                 return 1;
1729
1730                         ocount = 0;
1731                         try {
1732                                 ulong a =  UInt64.MaxValue;
1733                                 ulong t = a++;
1734                         } catch {
1735                                 ocount++;
1736                         }
1737                         if (ocount != 1)
1738                                 return 2;
1739
1740                         ocount = 0;
1741                         try {
1742                                 long a = Int64.MaxValue - 1;
1743                                 long t = a++;
1744                         } catch {
1745                                 ocount++;
1746                         }
1747                         if (ocount != 0)
1748                                 return 3;
1749
1750                         try {
1751                                 long a = Int64.MaxValue;
1752                                 long t = a++;
1753                         } catch {
1754                                 ocount++;
1755                         }
1756                         if (ocount != 1)
1757                                 return 4;
1758
1759                         ocount = 0;
1760                         try {
1761                                 ulong a = UInt64.MaxValue - 1;
1762                                 ulong t = a++;
1763                         } catch {
1764                                 ocount++;
1765                         }
1766                         if (ocount != 0)
1767                                 return 5;
1768
1769                         try {
1770                                 ulong a = UInt64.MaxValue;
1771                                 ulong t = a++;
1772                         } catch {
1773                                 ocount++;
1774                         }
1775                         if (ocount != 1)
1776                                 return 6;
1777
1778                         ocount = 0;
1779                         try {
1780                                 long a = Int64.MinValue + 1;
1781                                 long t = a--;
1782                         } catch {
1783                                 ocount++;
1784                         }
1785                         if (ocount != 0)
1786                                 return 7;
1787
1788                         ocount = 0;
1789                         try {
1790                                 long a = Int64.MinValue;
1791                                 long t = a--;
1792                         } catch {
1793                                 ocount++;
1794                         }
1795                         if (ocount != 1)
1796                                 return 8;
1797
1798                         ocount = 0;
1799                         try {
1800                                 ulong a = UInt64.MinValue + 1;
1801                                 ulong t = a--;
1802                         } catch {
1803                                 ocount++;
1804                         }
1805                         if (ocount != 0)
1806                                 return 9;
1807
1808                         ocount = 0;
1809                         try {
1810                                 ulong a = UInt64.MinValue;
1811                                 ulong t = a--;
1812                         } catch {
1813                                 ocount++;
1814                         }
1815                         if (ocount != 1)
1816                                 return 10;
1817
1818                         ocount = 0;
1819                         try {
1820                                 int a = Int32.MinValue + 1;
1821                                 int t = a--;
1822                         } catch {
1823                                 ocount++;
1824                         }
1825                         if (ocount != 0)
1826                                 return 11;
1827
1828                         ocount = 0;
1829                         try {
1830                                 int a = Int32.MinValue;
1831                                 int t = a--;
1832                         } catch {
1833                                 ocount++;
1834                         }
1835                         if (ocount != 1)
1836                                 return 12;
1837
1838                         ocount = 0;
1839                         try {
1840                                 uint a = 1;
1841                                 uint t = a--;
1842                         } catch {
1843                                 ocount++;
1844                         }
1845                         if (ocount != 0)
1846                                 return 13;
1847
1848                         ocount = 0;
1849                         try {
1850                                 uint a = 0;
1851                                 uint t = a--;
1852                         } catch {
1853                                 ocount++;
1854                         }
1855                         if (ocount != 1)
1856                                 return 14;
1857
1858                         ocount = 0;
1859                         try {
1860                                 sbyte a = 126;
1861                                 sbyte t = a++;
1862                         } catch {
1863                                 ocount++;
1864                         }
1865                         if (ocount != 0)
1866                                 return 15;
1867
1868                         ocount = 0;
1869                         try {
1870                                 sbyte a = 127;
1871                                 sbyte t = a++;
1872                         } catch {
1873                                 ocount++;
1874                         }
1875                         if (ocount != 1)
1876                                 return 16;
1877
1878                         ocount = 0;
1879                         try {
1880                         } catch {
1881                                 ocount++;
1882                         }
1883                         if (ocount != 0)
1884                                 return 17;
1885
1886                         ocount = 0;
1887                         try {
1888                                 int a = 1 << 29;
1889                                 int t = a*2;
1890                         } catch {
1891                                 ocount++;
1892                         }
1893                         if (ocount != 0)
1894                                 return 18;
1895
1896                         ocount = 0;
1897                         try {
1898                                 int a = 1 << 30;
1899                                 int t = a*2;
1900                         } catch {
1901                                 ocount++;
1902                         }
1903                         if (ocount != 1)
1904                                 return 19;
1905
1906                         ocount = 0;
1907                         try {
1908                                 ulong a = 0xffffffffff;
1909                                 ulong t = a*0x0ffffff;
1910                         } catch {
1911                                 ocount++;
1912                         }
1913                         if (ocount != 0)
1914                                 return 20;
1915
1916                         ocount = 0;
1917                         try {
1918                                 ulong a = 0xffffffffff;
1919                                 ulong t = a*0x0fffffff;
1920                         } catch {
1921                                 ocount++;
1922                         }
1923                         if (ocount != 1)
1924                                 return 21;
1925
1926                         ocount = 0;
1927                         try {
1928                                 long a = Int64.MinValue;
1929                                 long b = 10;
1930                                 long v = a * b;
1931                         } catch {
1932                                 ocount ++;
1933                         }
1934                         if (ocount != 1)
1935                                 return 22;
1936
1937                         ocount = 0;
1938                         try {
1939                                 long a = 10;
1940                                 long b = Int64.MinValue;
1941                                 long v = a * b;
1942                         } catch {
1943                                 ocount ++;
1944                         }
1945                         if (ocount != 1)
1946                                 return 23;
1947                 }
1948                 
1949                 return 0;
1950         }
1951
1952         class Broken {
1953                 static int i;
1954
1955                 static Broken () {
1956                         throw new Exception ("Ugh!");
1957                 }
1958         
1959                 public static int DoSomething () {
1960                         return i;
1961                 }
1962         }
1963
1964         static int test_0_exception_in_cctor () {
1965                 try {
1966                         Broken.DoSomething ();
1967                 }
1968                 catch (TypeInitializationException) {
1969                         // This will only happen once even if --regression is used
1970                 }
1971                 return 0;
1972         }
1973
1974         static int test_5_regalloc () {
1975                 int i = 0;
1976
1977                 try {
1978                         for (i = 0; i < 10; ++i) {
1979                                 if (i == 5)
1980                                         throw new Exception ();
1981                         }
1982                 }
1983                 catch (Exception) {
1984                         if (i != 5)
1985                                 return i;
1986                 }
1987
1988                 // Check that variables written in catch clauses are volatile
1989                 int j = 0;
1990                 try {
1991                         throw new Exception ();
1992                 }
1993                 catch (Exception) {
1994                         j = 5;
1995                 }
1996                 if (j != 5)
1997                         return 6;
1998
1999                 int k = 0;
2000                 try {
2001                         try {
2002                                 throw new Exception ();
2003                         }
2004                         finally {
2005                                 k = 5;
2006                         }
2007                 }
2008                 catch (Exception) {
2009                 }
2010                 if (k != 5)
2011                         return 7;
2012
2013                 return i;
2014         }
2015
2016         /* MarshalByRefObject prevents the methods from being inlined */
2017         class ThrowClass : MarshalByRefObject {
2018                 public static void rethrow1 () {
2019                         throw new Exception ();
2020                 }
2021
2022                 public static void rethrow2 () {
2023                         rethrow1 ();
2024                 }
2025         }
2026
2027         static int test_0_rethrow_stacktrace () {
2028                 // Check that rethrowing an exception preserves the original stack trace
2029                 try {
2030                         try {
2031                                 ThrowClass.rethrow2 ();
2032                         }
2033                         catch (Exception ex) {
2034                                 throw;
2035                         }
2036                 }
2037                 catch (Exception ex) {
2038                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2039                                 return 0;
2040                 }
2041
2042                 return 1;
2043         }
2044         
2045         interface IFace {}
2046         class Face : IFace {}
2047                 
2048         static int test_1_array_mismatch_2 () {
2049                 try {
2050                         object [] o = new Face [1];
2051                         o [0] = 1;
2052                         return 0;
2053                 } catch (ArrayTypeMismatchException) {
2054                         return 1;
2055                 }
2056         }
2057         
2058         static int test_1_array_mismatch_3 () {
2059                 try {
2060                         object [] o = new IFace [1];
2061                         o [0] = 1;
2062                         return 0;
2063                 } catch (ArrayTypeMismatchException) {
2064                         return 1;
2065                 }
2066         }
2067         
2068         static int test_1_array_mismatch_4 () {
2069                 try {
2070                         object [][] o = new Face [5] [];
2071                         o [0] = new object [5];
2072                         
2073                         return 0;
2074                 } catch (ArrayTypeMismatchException) {
2075                         return 1;
2076                 }
2077         }
2078 }
2079