2004-08-29 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                 try {
1043                         uint ui = System.UInt32.MaxValue;
1044                         failed = true;
1045                         checked {
1046                                 a = (int)ui;
1047                         }
1048                 }
1049                 catch (OverflowException) {
1050                         failed = false;
1051                 }
1052                 if (failed)
1053                         return 9;
1054
1055                 {
1056                         int i; 
1057                         float f = 1.1f;
1058                         checked {
1059                                 i = (int) f;
1060                         }
1061                 }
1062
1063                 return 0;
1064         }
1065
1066         static int test_0_uint_cast () {
1067                 uint a;
1068                 long l;
1069                 bool failed;
1070
1071                 try {
1072                         double d =  System.UInt32.MaxValue;
1073                         failed = false;
1074                         checked {
1075                                 a = (uint)d;
1076                         }
1077                 } catch (OverflowException) {
1078                         failed = true;
1079                 }
1080                 if (failed)
1081                         return 1;
1082
1083                 try {
1084                         double d = System.UInt32.MaxValue + 1.0;
1085                         failed = true;
1086                         checked {
1087                                 a = (uint)d;
1088                         }
1089                 } catch (OverflowException) {
1090                         failed = false;
1091                 }
1092                 if (failed)
1093                         return 2;
1094
1095                 try {
1096                         double d = System.UInt32.MinValue;
1097                         failed = false;
1098                         checked {
1099                                 a = (uint)d;
1100                         }
1101                 } catch (OverflowException) {
1102                         failed = true;
1103                 }
1104                 if (failed)
1105                         return 3;
1106
1107                 try {
1108                         double d = System.UInt32.MinValue - 1.0;
1109                         failed = true;
1110                         checked {
1111                                 a = (uint)d;
1112                         }
1113                 } catch (OverflowException) {
1114                         failed = false;
1115                 }
1116                 if (failed)
1117                         return 4;
1118                 
1119                 try {
1120                         l =  System.UInt32.MaxValue;
1121                         failed = false;
1122                         checked {
1123                                 a = (uint)l;
1124                         }
1125                 } catch (OverflowException) {
1126                         failed = true;
1127                 }
1128                 if (failed)
1129                         return 5;
1130
1131                 try {
1132                         l = System.UInt32.MaxValue + (long)1;
1133                         failed = true;
1134                         checked {
1135                                 a = (uint)l;
1136                         }
1137                 } catch (OverflowException) {
1138                         failed = false;
1139                 }
1140                 if (failed)
1141                         return 6;
1142
1143                 try {
1144                         l = System.UInt32.MinValue;
1145                         failed = false;
1146                         checked {
1147                                 a = (uint)l;
1148                         }
1149                 } catch (OverflowException) {
1150                         failed = true;
1151                 }
1152                 if (failed)
1153                         return 7;
1154
1155                 try {
1156                         l = System.UInt32.MinValue - (long)1;
1157                         failed = true;
1158                         checked {
1159                                 a = (uint)l;
1160                         }
1161                 } catch (OverflowException) {
1162                         failed = false;
1163                 }
1164                 if (failed)
1165                         return 8;
1166
1167                 try {
1168                         int i = -1;
1169                         failed = true;
1170                         checked {
1171                                 a = (uint)i;
1172                         }
1173                 }
1174                 catch (OverflowException) {
1175                         failed = false;
1176                 }
1177                 if (failed)
1178                         return 9;
1179
1180                 {
1181                         uint i; 
1182                         float f = 1.1f;
1183                         checked {
1184                                 i = (uint) f;
1185                         }
1186                 }
1187                 
1188                 return 0;
1189         }
1190         
1191         static int test_0_long_cast () {
1192                 long a;
1193                 bool failed;
1194
1195                 try {
1196                         double d = System.Int64.MaxValue - 512.0;
1197                         failed = true;
1198                         checked {
1199                                 a = (long)d;
1200                         }
1201                 } catch (OverflowException) {
1202                         failed = false;
1203                 }
1204                 if (failed)
1205                         return 1;
1206
1207                 try {
1208                         double d = System.Int64.MaxValue - 513.0;
1209                         failed = false;
1210                         checked {
1211                                 a = (long)d;
1212                         }
1213                 } catch (OverflowException) {
1214                         failed = true;
1215                 }
1216                 if (failed)
1217                         return 2;
1218                 
1219
1220                 try {
1221                         double d = System.Int64.MinValue - 1024.0;
1222                         failed = false;                 
1223                         checked {
1224                                 a = (long)d;
1225                         }
1226                 } catch (OverflowException) {
1227                         failed = true;
1228                 }
1229                 if (failed)
1230                         return 3;
1231
1232                 try {
1233                         double d = System.Int64.MinValue - 1025.0;
1234                         failed = true;
1235                         checked {
1236                                 a = (long)d;
1237                         }
1238                 } catch (OverflowException) {
1239                         failed = false;
1240                 }
1241                 if (failed)
1242                         return 4;
1243
1244                 {
1245                         long i; 
1246                         float f = 1.1f;
1247                         checked {
1248                                 i = (long) f;
1249                         }
1250                 }
1251
1252                 return 0;
1253         }
1254
1255         static int test_0_ulong_cast () {
1256                 ulong a;
1257                 bool failed;
1258
1259                 try {
1260                         double d = System.UInt64.MaxValue - 1024.0;
1261                         failed = true;
1262                         checked {
1263                                 a = (ulong)d;
1264                         }
1265                 } catch (OverflowException) {
1266                         failed = false;
1267                 }
1268                 if (failed)
1269                         return 1;
1270
1271                 try {
1272                         double d = System.UInt64.MaxValue - 1025.0;
1273                         failed = false;
1274                         checked {
1275                                 a = (ulong)d;
1276                         }
1277                 } catch (OverflowException) {
1278                         failed = true;
1279                 }
1280                 if (failed)
1281                         return 2;
1282                 
1283
1284                 try {
1285                         double d = 0;
1286                         failed = false;                 
1287                         checked {
1288                                 a = (ulong)d;
1289                         }
1290                 } catch (OverflowException) {
1291                         failed = true;
1292                 }
1293                 if (failed)
1294                         return 3;
1295
1296                 try {
1297                         double d = -1;
1298                         failed = true;
1299                         checked {
1300                                 a = (ulong)d;
1301                         }
1302                 } catch (OverflowException) {
1303                         failed = false;
1304                 }
1305                 if (failed)
1306                         return 4;
1307
1308                 {
1309                         ulong i; 
1310                         float f = 1.1f;
1311                         checked {
1312                                 i = (ulong) f;
1313                         }
1314                 }
1315
1316                 try {
1317                         int i = -1;
1318                         failed = true;
1319                         checked {
1320                                 a = (ulong)i;
1321                         }
1322                 }
1323                 catch (OverflowException) {
1324                         failed = false;
1325                 }
1326                 if (failed)
1327                         return 5;
1328
1329                 try {
1330                         int i = Int32.MinValue;
1331                         failed = true;
1332                         checked {
1333                                 a = (ulong)i;
1334                         }
1335                 }
1336                 catch (OverflowException) {
1337                         failed = false;
1338                 }
1339                 if (failed)
1340                         return 6;
1341
1342                 return 0;
1343         }
1344
1345         static int test_0_simple_double_casts () {
1346
1347                 double d = 0xffffffff;
1348
1349                 if ((uint)d != 4294967295)
1350                         return 1;
1351
1352                 d = 0xffffffffffffffff;
1353
1354                 if ((ulong)d != 0)
1355                         return 2;
1356
1357                 if ((ushort)d != 0)
1358                         return 3;
1359                         
1360                 if ((byte)d != 0)
1361                         return 4;
1362                         
1363                 d = 0xffff;
1364
1365                 if ((ushort)d != 0xffff)
1366                         return 5;
1367                 
1368                 if ((byte)d != 0xff)
1369                         return 6;
1370                         
1371                 return 0;
1372         }
1373         
1374         static int test_0_div_zero () {
1375                 int d = 1;
1376                 int q = 0;
1377                 int val;
1378                 bool failed;
1379
1380                 try {
1381                         failed = true;
1382                         val = d / q;
1383                 } catch (DivideByZeroException) {
1384                         failed = false;
1385                 }
1386                 if (failed)
1387                         return 1;
1388
1389                 try {
1390                         failed = true;
1391                         val = d % q;
1392                 } catch (DivideByZeroException) {
1393                         failed = false;
1394                 }
1395                 if (failed)
1396                         return 2;
1397
1398                 try {
1399                         failed = true;
1400                         q = -1;
1401                         d = Int32.MinValue;
1402                         val = d / q;
1403                 } catch (DivideByZeroException) {
1404                         /* wrong exception */
1405                 } catch (ArithmeticException) {
1406                         failed = false;
1407                 }
1408                 if (failed)
1409                         return 3;
1410
1411                 try {
1412                         failed = true;
1413                         q = -1;
1414                         d = Int32.MinValue;
1415                         val = d % q;
1416                 } catch (DivideByZeroException) {
1417                         /* wrong exception */
1418                 } catch (ArithmeticException) {
1419                         failed = false;
1420                 }
1421                 if (failed)
1422                         return 4;
1423
1424                 return 0;
1425         }
1426
1427         static int return_55 () {
1428                 return 55;
1429         }
1430
1431         static int test_0_cfold_div_zero () {
1432                 // Test that constant folding doesn't cause division by zero exceptions
1433                 if (return_55 () != return_55 ()) {
1434                         int d = 1;
1435                         int q = 0;
1436                         int val;                        
1437
1438                         val = d / q;
1439                         val = d % q;
1440
1441                         q = -1;
1442                         d = Int32.MinValue;
1443                         val = d / q;
1444
1445                         q = -1;
1446                         val = d % q;
1447                 }
1448
1449                 return 0;
1450         }
1451
1452         static int test_0_udiv_zero () {
1453                 uint d = 1;
1454                 uint q = 0;
1455                 uint val;
1456                 bool failed;
1457
1458                 try {
1459                         failed = true;
1460                         val = d / q;
1461                 } catch (DivideByZeroException) {
1462                         failed = false;
1463                 }
1464                 if (failed)
1465                         return 1;
1466
1467                 try {
1468                         failed = true;
1469                         val = d % q;
1470                 } catch (DivideByZeroException) {
1471                         failed = false;
1472                 }
1473                 if (failed)
1474                         return 2;
1475
1476                 return 0;
1477         }
1478
1479         static int test_0_long_div_zero () {
1480                 long d = 1;
1481                 long q = 0;
1482                 long val;
1483                 bool failed;
1484
1485                 try {
1486                         failed = true;
1487                         val = d / q;
1488                 } catch (DivideByZeroException) {
1489                         failed = false;
1490                 }
1491                 if (failed)
1492                         return 1;
1493
1494                 try {
1495                         failed = true;
1496                         val = d % q;
1497                 } catch (DivideByZeroException) {
1498                         failed = false;
1499                 }
1500                 if (failed)
1501                         return 2;
1502
1503                 try {
1504                         failed = true;
1505                         q = -1;
1506                         d = Int64.MinValue;
1507                         val = d / q;
1508                 } catch (DivideByZeroException) {
1509                         /* wrong exception */
1510                 } catch (ArithmeticException) {
1511                         failed = false;
1512                 }
1513                 if (failed)
1514                         return 3;
1515
1516                 try {
1517                         failed = true;
1518                         q = -1;
1519                         d = Int64.MinValue;
1520                         val = d % q;
1521                 } catch (DivideByZeroException) {
1522                         /* wrong exception */
1523                 } catch (ArithmeticException) {
1524                         failed = false;
1525                 }
1526                 if (failed)
1527                         return 4;
1528
1529                 return 0;
1530         }
1531
1532         static int test_0_ulong_div_zero () {
1533                 ulong d = 1;
1534                 ulong q = 0;
1535                 ulong val;
1536                 bool failed;
1537
1538                 try {
1539                         failed = true;
1540                         val = d / q;
1541                 } catch (DivideByZeroException) {
1542                         failed = false;
1543                 }
1544                 if (failed)
1545                         return 1;
1546
1547                 try {
1548                         failed = true;
1549                         val = d % q;
1550                 } catch (DivideByZeroException) {
1551                         failed = false;
1552                 }
1553                 if (failed)
1554                         return 2;
1555
1556                 return 0;
1557         }
1558
1559         static int test_0_float_div_zero () {
1560                 double d = 1;
1561                 double q = 0;
1562                 double val;
1563                 bool failed;
1564
1565                 try {
1566                         failed = false;
1567                         val = d / q;
1568                 } catch (DivideByZeroException) {
1569                         failed = true;
1570                 }
1571                 if (failed)
1572                         return 1;
1573
1574                 try {
1575                         failed = false;
1576                         val = d % q;
1577                 } catch (DivideByZeroException) {
1578                         failed = true;
1579                 }
1580                 if (failed)
1581                         return 2;
1582
1583                 return 0;
1584         }
1585
1586         static int test_0_invalid_unbox () {
1587
1588                 int i = 123;
1589                 object o = "Some string";
1590                 int res = 1;
1591                 
1592                 try {
1593                         // Illegal conversion; o contains a string not an int
1594                         i = (int) o;   
1595                 } catch (Exception e) {
1596                         if (i ==123)
1597                                 res = 0;
1598                 }
1599
1600                 return res;
1601         }
1602
1603         // Test that double[] can't be cast to double (bug #46027)
1604         static int test_0_invalid_unbox_arrays () {
1605                 double[] d1 = { 1.0 };
1606                 double[][] d2 = { d1 };
1607                 Array a = d2;
1608
1609                 try {
1610                         foreach (double d in a) {
1611                         }
1612                         return 1;
1613                 }
1614                 catch (InvalidCastException e) {
1615                         return 0;
1616                 }
1617         }
1618
1619         /* bug# 42190, at least mcs generates a leave for the return that
1620          * jumps out of multiple exception clauses: we used to execute just 
1621          * one enclosing finally block.
1622          */
1623         static int finally_level;
1624         static void do_something () {
1625                 int a = 0;
1626                 try {
1627                         try {
1628                                 return;
1629                         } finally {
1630                                 a = 1;
1631                         }
1632                 } finally {
1633                         finally_level++;
1634                 }
1635         }
1636
1637         static int test_2_multiple_finally_clauses () {
1638                 finally_level = 0;
1639                 do_something ();
1640                 if (finally_level == 1)
1641                         return 2;
1642                 return 0;
1643         }
1644
1645         static int test_3_checked_cast_un () {
1646                 ulong i = 0x8000000034000000;
1647                 long j;
1648
1649                 try {
1650                         checked { j = (long)i; }
1651                 } catch (OverflowException) {
1652                         j = 2;
1653                 }
1654
1655                 if (j != 2)
1656                         return 0;
1657                 return 3;
1658         }
1659         
1660         static int test_4_checked_cast () {
1661                 long i;
1662                 ulong j;
1663
1664                 unchecked { i = (long)0x8000000034000000;};
1665                 try {
1666                         checked { j = (ulong)i; }
1667                 } catch (OverflowException) {
1668                         j = 3;
1669                 }
1670
1671                 if (j != 3)
1672                         return 0;
1673                 return 4;
1674         }
1675
1676         static readonly int[] mul_dim_results = new int[] {
1677                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1678                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1679                 2, 0, 2, 1, 2, 8, 
1680                 3, 0, 3, 1, 3, 8, 
1681                 4, 0, 4, 1, 4, 8, 
1682                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1683                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1684                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1685         };
1686
1687         static int test_0_multi_dim_array_access () {
1688                 int [,] a = System.Array.CreateInstance (typeof (int),
1689                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1690                 int x, y;
1691                 int result_idx = 0;
1692                 for (x = 0; x < 8; ++x) {
1693                         for (y = 0; y < 9; ++y) {
1694                                 bool got_ex = false;
1695                                 try {
1696                                         a [x, y] = 1;
1697                                 } catch {
1698                                         got_ex = true;
1699                                 }
1700                                 if (got_ex) {
1701                                         if (result_idx >= mul_dim_results.Length)
1702                                                 return -1;
1703                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1704                                                 return result_idx + 1;
1705                                         }
1706                                         result_idx += 2;
1707                                 }
1708                         }
1709                 }
1710                 if (result_idx == mul_dim_results.Length)
1711                         return 0;
1712                 return 200;
1713         }
1714
1715         static void helper_out_obj (out object o) {
1716                 o = (object)"buddy";
1717         }
1718
1719         static void helper_out_string (out string o) {
1720                 o = "buddy";
1721         }
1722
1723         static int test_2_array_mismatch () {
1724                 string[] a = { "hello", "world" };
1725                 object[] b = a;
1726                 bool passed = false;
1727
1728                 try {
1729                         helper_out_obj (out b [1]);
1730                 } catch (ArrayTypeMismatchException) {
1731                         passed = true;
1732                 }
1733                 if (!passed)
1734                         return 0;
1735                 helper_out_string (out a [1]);
1736                 if (a [1] != "buddy")
1737                         return 1;
1738                 return 2;
1739         }
1740
1741         static int test_0_ovf () {
1742                 int ocount = 0;
1743                 
1744                 checked {
1745
1746                         ocount = 0;
1747                         try {
1748                                 ulong a =  UInt64.MaxValue - 1;
1749                                 ulong t = a++;
1750                         } catch {
1751                                 ocount++;
1752                         }
1753                         if (ocount != 0)
1754                                 return 1;
1755
1756                         ocount = 0;
1757                         try {
1758                                 ulong a =  UInt64.MaxValue;
1759                                 ulong t = a++;
1760                         } catch {
1761                                 ocount++;
1762                         }
1763                         if (ocount != 1)
1764                                 return 2;
1765
1766                         ocount = 0;
1767                         try {
1768                                 long a = Int64.MaxValue - 1;
1769                                 long t = a++;
1770                         } catch {
1771                                 ocount++;
1772                         }
1773                         if (ocount != 0)
1774                                 return 3;
1775
1776                         try {
1777                                 long a = Int64.MaxValue;
1778                                 long t = a++;
1779                         } catch {
1780                                 ocount++;
1781                         }
1782                         if (ocount != 1)
1783                                 return 4;
1784
1785                         ocount = 0;
1786                         try {
1787                                 ulong a = UInt64.MaxValue - 1;
1788                                 ulong t = a++;
1789                         } catch {
1790                                 ocount++;
1791                         }
1792                         if (ocount != 0)
1793                                 return 5;
1794
1795                         try {
1796                                 ulong a = UInt64.MaxValue;
1797                                 ulong t = a++;
1798                         } catch {
1799                                 ocount++;
1800                         }
1801                         if (ocount != 1)
1802                                 return 6;
1803
1804                         ocount = 0;
1805                         try {
1806                                 long a = Int64.MinValue + 1;
1807                                 long t = a--;
1808                         } catch {
1809                                 ocount++;
1810                         }
1811                         if (ocount != 0)
1812                                 return 7;
1813
1814                         ocount = 0;
1815                         try {
1816                                 long a = Int64.MinValue;
1817                                 long t = a--;
1818                         } catch {
1819                                 ocount++;
1820                         }
1821                         if (ocount != 1)
1822                                 return 8;
1823
1824                         ocount = 0;
1825                         try {
1826                                 ulong a = UInt64.MinValue + 1;
1827                                 ulong t = a--;
1828                         } catch {
1829                                 ocount++;
1830                         }
1831                         if (ocount != 0)
1832                                 return 9;
1833
1834                         ocount = 0;
1835                         try {
1836                                 ulong a = UInt64.MinValue;
1837                                 ulong t = a--;
1838                         } catch {
1839                                 ocount++;
1840                         }
1841                         if (ocount != 1)
1842                                 return 10;
1843
1844                         ocount = 0;
1845                         try {
1846                                 int a = Int32.MinValue + 1;
1847                                 int t = a--;
1848                         } catch {
1849                                 ocount++;
1850                         }
1851                         if (ocount != 0)
1852                                 return 11;
1853
1854                         ocount = 0;
1855                         try {
1856                                 int a = Int32.MinValue;
1857                                 int t = a--;
1858                         } catch {
1859                                 ocount++;
1860                         }
1861                         if (ocount != 1)
1862                                 return 12;
1863
1864                         ocount = 0;
1865                         try {
1866                                 uint a = 1;
1867                                 uint t = a--;
1868                         } catch {
1869                                 ocount++;
1870                         }
1871                         if (ocount != 0)
1872                                 return 13;
1873
1874                         ocount = 0;
1875                         try {
1876                                 uint a = 0;
1877                                 uint t = a--;
1878                         } catch {
1879                                 ocount++;
1880                         }
1881                         if (ocount != 1)
1882                                 return 14;
1883
1884                         ocount = 0;
1885                         try {
1886                                 sbyte a = 126;
1887                                 sbyte t = a++;
1888                         } catch {
1889                                 ocount++;
1890                         }
1891                         if (ocount != 0)
1892                                 return 15;
1893
1894                         ocount = 0;
1895                         try {
1896                                 sbyte a = 127;
1897                                 sbyte t = a++;
1898                         } catch {
1899                                 ocount++;
1900                         }
1901                         if (ocount != 1)
1902                                 return 16;
1903
1904                         ocount = 0;
1905                         try {
1906                         } catch {
1907                                 ocount++;
1908                         }
1909                         if (ocount != 0)
1910                                 return 17;
1911
1912                         ocount = 0;
1913                         try {
1914                                 int a = 1 << 29;
1915                                 int t = a*2;
1916                         } catch {
1917                                 ocount++;
1918                         }
1919                         if (ocount != 0)
1920                                 return 18;
1921
1922                         ocount = 0;
1923                         try {
1924                                 int a = 1 << 30;
1925                                 int t = a*2;
1926                         } catch {
1927                                 ocount++;
1928                         }
1929                         if (ocount != 1)
1930                                 return 19;
1931
1932                         ocount = 0;
1933                         try {
1934                                 ulong a = 0xffffffffff;
1935                                 ulong t = a*0x0ffffff;
1936                         } catch {
1937                                 ocount++;
1938                         }
1939                         if (ocount != 0)
1940                                 return 20;
1941
1942                         ocount = 0;
1943                         try {
1944                                 ulong a = 0xffffffffff;
1945                                 ulong t = a*0x0fffffff;
1946                         } catch {
1947                                 ocount++;
1948                         }
1949                         if (ocount != 1)
1950                                 return 21;
1951
1952                         ocount = 0;
1953                         try {
1954                                 long a = Int64.MinValue;
1955                                 long b = 10;
1956                                 long v = a * b;
1957                         } catch {
1958                                 ocount ++;
1959                         }
1960                         if (ocount != 1)
1961                                 return 22;
1962
1963                         ocount = 0;
1964                         try {
1965                                 long a = 10;
1966                                 long b = Int64.MinValue;
1967                                 long v = a * b;
1968                         } catch {
1969                                 ocount ++;
1970                         }
1971                         if (ocount != 1)
1972                                 return 23;
1973                 }
1974                 
1975                 return 0;
1976         }
1977
1978         class Broken {
1979                 static int i;
1980
1981                 static Broken () {
1982                         throw new Exception ("Ugh!");
1983                 }
1984         
1985                 public static int DoSomething () {
1986                         return i;
1987                 }
1988         }
1989
1990         static int test_0_exception_in_cctor () {
1991                 try {
1992                         Broken.DoSomething ();
1993                 }
1994                 catch (TypeInitializationException) {
1995                         // This will only happen once even if --regression is used
1996                 }
1997                 return 0;
1998         }
1999
2000         static int test_5_regalloc () {
2001                 int i = 0;
2002
2003                 try {
2004                         for (i = 0; i < 10; ++i) {
2005                                 if (i == 5)
2006                                         throw new Exception ();
2007                         }
2008                 }
2009                 catch (Exception) {
2010                         if (i != 5)
2011                                 return i;
2012                 }
2013
2014                 // Check that variables written in catch clauses are volatile
2015                 int j = 0;
2016                 try {
2017                         throw new Exception ();
2018                 }
2019                 catch (Exception) {
2020                         j = 5;
2021                 }
2022                 if (j != 5)
2023                         return 6;
2024
2025                 int k = 0;
2026                 try {
2027                         try {
2028                                 throw new Exception ();
2029                         }
2030                         finally {
2031                                 k = 5;
2032                         }
2033                 }
2034                 catch (Exception) {
2035                 }
2036                 if (k != 5)
2037                         return 7;
2038
2039                 return i;
2040         }
2041
2042         /* MarshalByRefObject prevents the methods from being inlined */
2043         class ThrowClass : MarshalByRefObject {
2044                 public static void rethrow1 () {
2045                         throw new Exception ();
2046                 }
2047
2048                 public static void rethrow2 () {
2049                         rethrow1 ();
2050                 }
2051         }
2052
2053         static int test_0_rethrow_stacktrace () {
2054                 // Check that rethrowing an exception preserves the original stack trace
2055                 try {
2056                         try {
2057                                 ThrowClass.rethrow2 ();
2058                         }
2059                         catch (Exception ex) {
2060                                 throw;
2061                         }
2062                 }
2063                 catch (Exception ex) {
2064                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2065                                 return 0;
2066                 }
2067
2068                 return 1;
2069         }
2070         
2071         interface IFace {}
2072         class Face : IFace {}
2073                 
2074         static int test_1_array_mismatch_2 () {
2075                 try {
2076                         object [] o = new Face [1];
2077                         o [0] = 1;
2078                         return 0;
2079                 } catch (ArrayTypeMismatchException) {
2080                         return 1;
2081                 }
2082         }
2083         
2084         static int test_1_array_mismatch_3 () {
2085                 try {
2086                         object [] o = new IFace [1];
2087                         o [0] = 1;
2088                         return 0;
2089                 } catch (ArrayTypeMismatchException) {
2090                         return 1;
2091                 }
2092         }
2093         
2094         static int test_1_array_mismatch_4 () {
2095                 try {
2096                         object [][] o = new Face [5] [];
2097                         o [0] = new object [5];
2098                         
2099                         return 0;
2100                 } catch (ArrayTypeMismatchException) {
2101                         return 1;
2102                 }
2103         }
2104 }
2105