2003-10-09 Dick Porter <dick@ximian.com>
[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                 byte b = 0;
90                 bool failed;
91
92                 try {
93                         a = 255;
94                         failed = false;
95                         checked {
96                                 b = (byte)a;
97                         }
98                 } catch (OverflowException) {
99                         failed = true;
100                 }
101                 if (failed)
102                         return 1;
103                 if (b != 255)
104                         return -1;
105
106                 try {
107                         a = 0;
108                         failed = false;
109                         checked {
110                                 b = (byte)a;
111                         }
112                 } catch (OverflowException) {
113                         failed = true;
114                 }
115                 if (failed)
116                         return 2;
117                 if (b != 0)
118                         return -2;
119
120                 try {
121                         a = 256;
122                         failed = true;
123                         checked {
124                                 b = (byte)a;
125                         }
126                 } catch (OverflowException) {
127                         failed = false;
128                 }
129                 if (failed)
130                         return 3;
131                 if (b != 0)
132                         return -3;
133
134                 try {
135                         a = -1;
136                         failed = true;
137                         checked {
138                                 b = (byte)a;
139                         }
140                 } catch (OverflowException) {
141                         failed = false;
142                 }
143                 if (failed)
144                         return 4;
145                 if (b != 0)
146                         return -4;
147
148                 try {
149                         double d = 0;
150                         failed = false;
151                         checked {
152                                 b = (byte)d;
153                         }
154                 } catch (OverflowException) {
155                         failed = true;
156                 }
157                 if (failed)
158                         return 5;
159                 if (b != 0)
160                         return -5;
161                 
162                 try {
163                         double d = -1;
164                         failed = true;
165                         checked {
166                                 b = (byte)d;
167                         }
168                 } catch (OverflowException) {
169                         failed = false;
170                 }
171                 if (failed)
172                         return 6;
173                 if (b != 0)
174                         return -6;
175
176                 try {
177                         double d = 255;
178                         failed = false;
179                         checked {
180                                 b = (byte)d;
181                         }
182                 } catch (OverflowException) {
183                         failed = true;
184                 }
185                 if (failed)
186                         return 7;
187                 if (b != 255)
188                         return -7;
189
190                 try {
191                         double d = 256;
192                         failed = true;
193                         checked {
194                                 b = (byte)d;
195                         }
196                 } catch (OverflowException) {
197                         failed = false;
198                 }
199                 if (failed)
200                         return 8;
201                 if (b != 255)
202                         return -8;
203
204                 try {
205                         l = 255;
206                         failed = false;
207                         checked {
208                                 b = (byte)l;
209                         }
210                 } catch (OverflowException) {
211                         failed = true;
212                 }
213                 if (failed)
214                         return 9;
215                 if (b != 255)
216                         return -9;
217
218                 try {
219                         l = 0;
220                         failed = false;
221                         checked {
222                                 b = (byte)l;
223                         }
224                 } catch (OverflowException) {
225                         failed = true;
226                 }
227                 if (failed)
228                         return 10;
229                 if (b != 0)
230                         return -10;
231
232                 try {
233                         l = 256;
234                         failed = true;
235                         checked {
236                                 b = (byte)l;
237                         }
238                 } catch (OverflowException) {
239                         failed = false;
240                 }
241                 if (failed)
242                         return 11;
243                 if (b != 0)
244                         return -11;
245
246                 try {
247                         l = -1;
248                         failed = true;
249                         checked {
250                                 b = (byte)l;
251                         }
252                 } catch (OverflowException) {
253                         failed = false;
254                 }
255                 if (failed)
256                         return 12;
257                 if (b != 0)
258                         return -12;
259                 
260                 return 0;
261         }
262         
263         static int test_0_sbyte_cast () {
264                 int a;
265                 long l;
266                 sbyte b = 0;
267                 bool failed;
268
269                 try {
270                         a = 255;
271                         failed = true;
272                         checked {
273                                 b = (sbyte)a;
274                         }
275                 } catch (OverflowException) {
276                         failed = false;
277                 }
278                 if (failed)
279                         return 1;
280                 if (b != 0)
281                         return -1;
282
283                 try {
284                         a = 0;
285                         failed = false;
286                         checked {
287                                 b = (sbyte)a;
288                         }
289                 } catch (OverflowException) {
290                         failed = true;
291                 }
292                 if (failed)
293                         return 2;
294                 if (b != 0)
295                         return -2;
296
297                 try {
298                         a = 256;
299                         failed = true;
300                         checked {
301                                 b = (sbyte)a;
302                         }
303                 } catch (OverflowException) {
304                         failed = false;
305                 }
306                 if (failed)
307                         return 3;
308                 if (b != 0)
309                         return -3;
310
311                 try {
312                         a = -129;
313                         failed = true;
314                         checked {
315                                 b = (sbyte)a;
316                         }
317                 } catch (OverflowException) {
318                         failed = false;
319                 }
320                 if (failed)
321                         return 4;
322                 if (b != 0)
323                         return -4;
324
325                 try {
326                         a = -1;
327                         failed = false;
328                         checked {
329                                 b = (sbyte)a;
330                         }
331                 } catch (OverflowException) {
332                         failed = true;
333                 }
334                 if (failed)
335                         return 5;
336                 if (b != -1)
337                         return -5;
338
339                 try {
340                         a = -128;
341                         failed = false;
342                         checked {
343                                 b = (sbyte)a;
344                         }
345                 } catch (OverflowException) {
346                         failed = true;
347                 }
348                 if (failed)
349                         return 6;
350                 if (b != -128)
351                         return -6;
352
353                 try {
354                         a = 127;
355                         failed = false;
356                         checked {
357                                 b = (sbyte)a;
358                         }
359                 } catch (OverflowException) {
360                         failed = true;
361                 }
362                 if (failed)
363                         return 7;
364                 if (b != 127)
365                         return -7;
366
367                 try {
368                         a = 128;
369                         failed = true;
370                         checked {
371                                 b = (sbyte)a;
372                         }
373                 } catch (OverflowException) {
374                         failed = false;
375                 }
376                 if (failed)
377                         return 8;
378                 if (b != 127)
379                         return -8;
380
381                 try {
382                         double d = 127;
383                         failed = false;
384                         checked {
385                                 b = (sbyte)d;
386                         }
387                 } catch (OverflowException) {
388                         failed = true;
389                 }
390                 if (failed)
391                         return 9;
392                 if (b != 127)
393                         return -9;
394
395                 try {
396                         double d = -128;
397                         failed = false;
398                         checked {
399                                 b = (sbyte)d;
400                         }
401                 } catch (OverflowException) {
402                         failed = true;
403                 }
404                 if (failed)
405                         return 10;
406                 if (b != -128)
407                         return -10;
408
409                 try {
410                         double d = 128;
411                         failed = true;
412                         checked {
413                                 b = (sbyte)d;
414                         }
415                 } catch (OverflowException) {
416                         failed = false;
417                 }
418                 if (failed)
419                         return 11;
420                 if (b != -128)
421                         return -11;
422
423                 try {
424                         double d = -129;
425                         failed = true;
426                         checked {
427                                 b = (sbyte)d;
428                         }
429                 } catch (OverflowException) {
430                         failed = false;
431                 }
432                 if (failed)
433                         return 12;
434                 if (b != -128)
435                         return -12;
436
437                 try {
438                         l = 255;
439                         failed = true;
440                         checked {
441                                 b = (sbyte)l;
442                         }
443                 } catch (OverflowException) {
444                         failed = false;
445                 }
446                 if (failed)
447                         return 13;
448                 if (b != -128)
449                         return -13;
450
451                 try {
452                         l = 0;
453                         failed = false;
454                         checked {
455                                 b = (sbyte)l;
456                         }
457                 } catch (OverflowException) {
458                         failed = true;
459                 }
460                 if (failed)
461                         return 14;
462                 if (b != 0)
463                         return -14;
464
465                 try {
466                         l = 256;
467                         failed = true;
468                         checked {
469                                 b = (sbyte)l;
470                         }
471                 } catch (OverflowException) {
472                         failed = false;
473                 }
474                 if (failed)
475                         return 15;
476                 if (b != 0)
477                         return -15;
478
479                 try {
480                         l = -129;
481                         failed = true;
482                         checked {
483                                 b = (sbyte)l;
484                         }
485                 } catch (OverflowException) {
486                         failed = false;
487                 }
488                 if (failed)
489                         return 16;
490                 if (b != 0)
491                         return -16;
492
493                 try {
494                         l = -1;
495                         failed = false;
496                         checked {
497                                 b = (sbyte)l;
498                         }
499                 } catch (OverflowException) {
500                         failed = true;
501                 }
502                 if (failed)
503                         return 17;
504                 if (b != -1)
505                         return -17;
506
507                 try {
508                         l = -128;
509                         failed = false;
510                         checked {
511                                 b = (sbyte)l;
512                         }
513                 } catch (OverflowException) {
514                         failed = true;
515                 }
516                 if (failed)
517                         return 18;
518                 if (b != -128)
519                         return -18;
520
521                 try {
522                         l = 127;
523                         failed = false;
524                         checked {
525                                 b = (sbyte)l;
526                         }
527                 } catch (OverflowException) {
528                         failed = true;
529                 }
530                 if (failed)
531                         return 19;
532                 if (b != 127)
533                         return -19;
534
535                 try {
536                         l = 128;
537                         failed = true;
538                         checked {
539                                 b = (sbyte)l;
540                         }
541                 } catch (OverflowException) {
542                         failed = false;
543                 }
544                 if (failed)
545                         return 20;
546                 if (b != 127)
547                         return -19;
548
549                 return 0;
550         }
551
552         static int test_0_ushort_cast () {
553                 int a;
554                 long l;
555                 ushort b;
556                 bool failed;
557
558                 try {
559                         a = System.UInt16.MaxValue;
560                         failed = false;
561                         checked {
562                                 b = (ushort)a;
563                         }
564                 } catch (OverflowException) {
565                         failed = true;
566                 }
567                 if (failed)
568                         return 1;
569
570                 try {
571                         a = 0;
572                         failed = false;
573                         checked {
574                                 b = (ushort)a;
575                         }
576                 } catch (OverflowException) {
577                         failed = true;
578                 }
579                 if (failed)
580                         return 2;
581
582                 try {
583                         a = System.UInt16.MaxValue + 1;
584                         failed = true;
585                         checked {
586                                 b = (ushort)a;
587                         }
588                 } catch (OverflowException) {
589                         failed = false;
590                 }
591                 if (failed)
592                         return 3;
593
594                 try {
595                         a = -1;
596                         failed = true;
597                         checked {
598                                 b = (ushort)a;
599                         }
600                 } catch (OverflowException) {
601                         failed = false;
602                 }
603                 if (failed)
604                         return 4;
605
606                 try {
607                         double d = 0;
608                         failed = false;
609                         checked {
610                                 b = (ushort)d;
611                         }
612                 } catch (OverflowException) {
613                         failed = true;
614                 }
615                 if (failed)
616                         return 5;
617
618                 try {
619                         double d = System.UInt16.MaxValue;
620                         failed = false;
621                         checked {
622                                 b = (ushort)d;
623                         }
624                 } catch (OverflowException) {
625                         failed = true;
626                 }
627                 if (failed)
628                         return 6;
629
630                 try {
631                         double d = -1;
632                         failed = true;
633                         checked {
634                                 b = (ushort)d;
635                         }
636                 } catch (OverflowException) {
637                         failed = false;
638                 }
639                 if (failed)
640                         return 7;
641
642                 try {
643                         double d = System.UInt16.MaxValue + 1.0;
644                         failed = true;
645                         checked {
646                                 b = (ushort)d;
647                         }
648                 } catch (OverflowException) {
649                         failed = false;
650                 }
651                 if (failed)
652                         return 8;
653
654                 try {
655                         l = System.UInt16.MaxValue;
656                         failed = false;
657                         checked {
658                                 b = (ushort)l;
659                         }
660                 } catch (OverflowException) {
661                         failed = true;
662                 }
663                 if (failed)
664                         return 9;
665
666                 try {
667                         l = 0;
668                         failed = false;
669                         checked {
670                                 b = (ushort)l;
671                         }
672                 } catch (OverflowException) {
673                         failed = true;
674                 }
675                 if (failed)
676                         return 10;
677
678                 try {
679                         l = System.UInt16.MaxValue + 1;
680                         failed = true;
681                         checked {
682                                 b = (ushort)l;
683                         }
684                 } catch (OverflowException) {
685                         failed = false;
686                 }
687                 if (failed)
688                         return 11;
689
690                 try {
691                         l = -1;
692                         failed = true;
693                         checked {
694                                 b = (ushort)l;
695                         }
696                 } catch (OverflowException) {
697                         failed = false;
698                 }
699                 if (failed)
700                         return 12;
701
702                 return 0;
703         }
704         
705         static int test_0_short_cast () {
706                 int a;
707                 long l;
708                 short b;
709                 bool failed;
710
711                 try {
712                         a = System.UInt16.MaxValue;
713                         failed = true;
714                         checked {
715                                 b = (short)a;
716                         }
717                 } catch (OverflowException) {
718                         failed = false;
719                 }
720                 if (failed)
721                         return 1;
722
723                 try {
724                         a = 0;
725                         failed = false;
726                         checked {
727                                 b = (short)a;
728                         }
729                 } catch (OverflowException) {
730                         failed = true;
731                 }
732                 if (failed)
733                         return 2;
734
735                 try {
736                         a = System.Int16.MaxValue + 1;
737                         failed = true;
738                         checked {
739                                 b = (short)a;
740                         }
741                 } catch (OverflowException) {
742                         failed = false;
743                 }
744                 if (failed)
745                         return 3;
746
747                 try {
748                         a = System.Int16.MinValue - 1;
749                         failed = true;
750                         checked {
751                                 b = (short)a;
752                         }
753                 } catch (OverflowException) {
754                         failed = false;
755                 }
756                 if (failed)
757                         return 4;
758
759                 try {
760                         a = -1;
761                         failed = false;
762                         checked {
763                                 b = (short)a;
764                         }
765                 } catch (OverflowException) {
766                         failed = true;
767                 }
768                 if (failed)
769                         return 5;
770
771                 try {
772                         a = System.Int16.MinValue;
773                         failed = false;
774                         checked {
775                                 b = (short)a;
776                         }
777                 } catch (OverflowException) {
778                         failed = true;
779                 }
780                 if (failed)
781                         return 6;
782
783                 try {
784                         a = System.Int16.MaxValue;
785                         failed = false;
786                         checked {
787                                 b = (short)a;
788                         }
789                 } catch (OverflowException) {
790                         failed = true;
791                 }
792                 if (failed)
793                         return 7;
794
795                 try {
796                         a = System.Int16.MaxValue + 1;
797                         failed = true;
798                         checked {
799                                 b = (short)a;
800                         }
801                 } catch (OverflowException) {
802                         failed = false;
803                 }
804                 if (failed)
805                         return 8;
806
807                 try {
808                         double d = System.Int16.MaxValue;
809                         failed = false;
810                         checked {
811                                 b = (short)d;
812                         }
813                 } catch (OverflowException) {
814                         failed = true;
815                 }
816                 if (failed)
817                         return 9;
818                 
819                 try {
820                         double d = System.Int16.MinValue;
821                         failed = false;
822                         checked {
823                                 b = (short)d;
824                         }
825                 } catch (OverflowException) {
826                         failed = true;
827                 }
828                 if (failed)
829                         return 10;
830                 
831                 try {
832                         double d = System.Int16.MaxValue + 1.0;
833                         failed = true;
834                         checked {
835                                 b = (short)d;
836                         }
837                 } catch (OverflowException) {
838                         failed = false;
839                 }
840                 if (failed)
841                         return 11;
842
843                 try {
844                         double d = System.Int16.MinValue - 1.0;
845                         failed = true;
846                         checked {
847                                 b = (short)d;
848                         }
849                 } catch (OverflowException) {
850                         failed = false;
851                 }
852                 if (failed)
853                         return 12;
854
855                 try {
856                         l = System.Int16.MaxValue + 1;
857                         failed = true;
858                         checked {
859                                 b = (short)l;
860                         }
861                 } catch (OverflowException) {
862                         failed = false;
863                 }
864                 if (failed)
865                         return 13;
866
867                 try {
868                         l = System.Int16.MaxValue;
869                         failed = false;
870                         checked {
871                                 b = (short)l;
872                         }
873                 } catch (OverflowException) {
874                         failed = true;
875                 }
876                 if (failed)
877                         return 14;
878
879                 try {
880                         l = System.Int16.MinValue - 1;
881                         failed = true;
882                         checked {
883                                 b = (short)l;
884                         }
885                 } catch (OverflowException) {
886                         failed = false;
887                 }
888                 if (failed)
889                         return 15;
890
891                 
892                 try {
893                         l = System.Int16.MinValue;
894                         failed = false;
895                         checked {
896                                 b = (short)l;
897                         }
898                 } catch (OverflowException) {
899                         failed = true;
900                 }
901                 if (failed)
902                         return 16;
903
904                 return 0;
905         }
906         
907         static int test_0_int_cast () {
908                 int a;
909                 long l;
910                 bool failed;
911
912                 try {
913                         double d = System.Int32.MaxValue + 1.0;
914                         failed = true;
915                         checked {
916                                 a = (int)d;
917                         }
918                 } catch (OverflowException) {
919                         failed = false;
920                 }
921                 if (failed)
922                         return 1;
923
924                 try {
925                         double d = System.Int32.MaxValue;
926                         failed = false;
927                         checked {
928                                 a = (int)d;
929                         }
930                 } catch (OverflowException) {
931                         failed = true;
932                 }
933                 if (failed)
934                         return 2;
935                 
936
937                 try {
938                         double d = System.Int32.MinValue;
939                         failed = false;                 
940                         checked {
941                                 a = (int)d;
942                         }
943                 } catch (OverflowException) {
944                         failed = true;
945                 }
946                 if (failed)
947                         return 3;
948
949
950                 try {
951                         double d =  System.Int32.MinValue - 1.0;
952                         failed = true;
953                         checked {
954                                 a = (int)d;
955                         }
956                 } catch (OverflowException) {
957                         failed = false;
958                 }
959                 if (failed)
960                         return 4;
961
962                 try {
963                         l = System.Int32.MaxValue + (long)1;
964                         failed = true;
965                         checked {
966                                 a = (int)l;
967                         }
968                 } catch (OverflowException) {
969                         failed = false;
970                 }
971                 if (failed)
972                         return 5;
973
974                 try {
975                         l = System.Int32.MaxValue;
976                         failed = false;
977                         checked {
978                                 a = (int)l;
979                         }
980                 } catch (OverflowException) {
981                         failed = true;
982                 }
983                 if (failed)
984                         return 6;
985                 
986
987                 try {
988                         l = System.Int32.MinValue;
989                         failed = false;                 
990                         checked {
991                                 a = (int)l;
992                         }
993                 } catch (OverflowException) {
994                         failed = true;
995                 }
996                 if (failed)
997                         return 7;
998
999
1000                 try {
1001                         l =  System.Int32.MinValue - (long)1;
1002                         failed = true;
1003                         checked {
1004                                 a = (int)l;
1005                         }
1006                 } catch (OverflowException) {
1007                         failed = false;
1008                 }
1009                 if (failed)
1010                         return 8;
1011
1012                 return 0;
1013         }
1014
1015         static int test_0_uint_cast () {
1016                 uint a;
1017                 long l;
1018                 bool failed;
1019
1020                 try {
1021                         double d =  System.UInt32.MaxValue;
1022                         failed = false;
1023                         checked {
1024                                 a = (uint)d;
1025                         }
1026                 } catch (OverflowException) {
1027                         failed = true;
1028                 }
1029                 if (failed)
1030                         return 1;
1031
1032                 try {
1033                         double d = System.UInt32.MaxValue + 1.0;
1034                         failed = true;
1035                         checked {
1036                                 a = (uint)d;
1037                         }
1038                 } catch (OverflowException) {
1039                         failed = false;
1040                 }
1041                 if (failed)
1042                         return 2;
1043
1044                 try {
1045                         double d = System.UInt32.MinValue;
1046                         failed = false;
1047                         checked {
1048                                 a = (uint)d;
1049                         }
1050                 } catch (OverflowException) {
1051                         failed = true;
1052                 }
1053                 if (failed)
1054                         return 3;
1055
1056                 try {
1057                         double d = System.UInt32.MinValue - 1.0;
1058                         failed = true;
1059                         checked {
1060                                 a = (uint)d;
1061                         }
1062                 } catch (OverflowException) {
1063                         failed = false;
1064                 }
1065                 if (failed)
1066                         return 4;
1067                 
1068                 try {
1069                         l =  System.UInt32.MaxValue;
1070                         failed = false;
1071                         checked {
1072                                 a = (uint)l;
1073                         }
1074                 } catch (OverflowException) {
1075                         failed = true;
1076                 }
1077                 if (failed)
1078                         return 5;
1079
1080                 try {
1081                         l = System.UInt32.MaxValue + (long)1;
1082                         failed = true;
1083                         checked {
1084                                 a = (uint)l;
1085                         }
1086                 } catch (OverflowException) {
1087                         failed = false;
1088                 }
1089                 if (failed)
1090                         return 6;
1091
1092                 try {
1093                         l = System.UInt32.MinValue;
1094                         failed = false;
1095                         checked {
1096                                 a = (uint)l;
1097                         }
1098                 } catch (OverflowException) {
1099                         failed = true;
1100                 }
1101                 if (failed)
1102                         return 7;
1103
1104                 try {
1105                         l = System.UInt32.MinValue - (long)1;
1106                         failed = true;
1107                         checked {
1108                                 a = (uint)l;
1109                         }
1110                 } catch (OverflowException) {
1111                         failed = false;
1112                 }
1113                 if (failed)
1114                         return 8;
1115                 
1116                 return 0;
1117         }
1118         
1119         static int test_0_long_cast () {
1120                 long a;
1121                 bool failed;
1122
1123                 try {
1124                         double d = System.Int64.MaxValue - 512.0;
1125                         failed = true;
1126                         checked {
1127                                 a = (long)d;
1128                         }
1129                 } catch (OverflowException) {
1130                         failed = false;
1131                 }
1132                 if (failed)
1133                         return 1;
1134
1135                 try {
1136                         double d = System.Int64.MaxValue - 513.0;
1137                         failed = false;
1138                         checked {
1139                                 a = (long)d;
1140                         }
1141                 } catch (OverflowException) {
1142                         failed = true;
1143                 }
1144                 if (failed)
1145                         return 2;
1146                 
1147
1148                 try {
1149                         double d = System.Int64.MinValue - 1024.0;
1150                         failed = false;                 
1151                         checked {
1152                                 a = (long)d;
1153                         }
1154                 } catch (OverflowException) {
1155                         failed = true;
1156                 }
1157                 if (failed)
1158                         return 3;
1159
1160                 try {
1161                         double d = System.Int64.MinValue - 1025.0;
1162                         failed = true;
1163                         checked {
1164                                 a = (long)d;
1165                         }
1166                 } catch (OverflowException) {
1167                         failed = false;
1168                 }
1169                 if (failed)
1170                         return 4;
1171
1172                 return 0;
1173         }
1174
1175         static int test_0_ulong_cast () {
1176                 ulong a;
1177                 bool failed;
1178
1179                 try {
1180                         double d = System.UInt64.MaxValue - 1024.0;
1181                         failed = true;
1182                         checked {
1183                                 a = (ulong)d;
1184                         }
1185                 } catch (OverflowException) {
1186                         failed = false;
1187                 }
1188                 if (failed)
1189                         return 1;
1190
1191                 try {
1192                         double d = System.UInt64.MaxValue - 1025.0;
1193                         failed = false;
1194                         checked {
1195                                 a = (ulong)d;
1196                         }
1197                 } catch (OverflowException) {
1198                         failed = true;
1199                 }
1200                 if (failed)
1201                         return 2;
1202                 
1203
1204                 try {
1205                         double d = 0;
1206                         failed = false;                 
1207                         checked {
1208                                 a = (ulong)d;
1209                         }
1210                 } catch (OverflowException) {
1211                         failed = true;
1212                 }
1213                 if (failed)
1214                         return 3;
1215
1216                 try {
1217                         double d = -1;
1218                         failed = true;
1219                         checked {
1220                                 a = (ulong)d;
1221                         }
1222                 } catch (OverflowException) {
1223                         failed = false;
1224                 }
1225                 if (failed)
1226                         return 4;
1227
1228                 return 0;
1229         }
1230
1231         static int test_0_simple_double_casts () {
1232
1233                 double d = 0xffffffff;
1234
1235                 if ((uint)d != 4294967295)
1236                         return 1;
1237
1238                 d = 0xffffffffffffffff;
1239
1240                 if ((ulong)d != 0)
1241                         return 2;
1242
1243                 if ((ushort)d != 0)
1244                         return 3;
1245                         
1246                 if ((byte)d != 0)
1247                         return 4;
1248                         
1249                 d = 0xffff;
1250
1251                 if ((ushort)d != 0xffff)
1252                         return 5;
1253                 
1254                 if ((byte)d != 0xff)
1255                         return 6;
1256                         
1257                 return 0;
1258         }
1259         
1260         static int test_0_div_zero () {
1261                 int d = 1;
1262                 int q = 0;
1263                 int val;
1264                 bool failed;
1265
1266                 try {
1267                         failed = true;
1268                         val = d / q;
1269                 } catch (DivideByZeroException) {
1270                         failed = false;
1271                 }
1272                 if (failed)
1273                         return 1;
1274
1275                 try {
1276                         failed = true;
1277                         val = d % q;
1278                 } catch (DivideByZeroException) {
1279                         failed = false;
1280                 }
1281                 if (failed)
1282                         return 2;
1283
1284                 return 0;
1285         }
1286
1287         static int test_0_udiv_zero () {
1288                 uint d = 1;
1289                 uint q = 0;
1290                 uint val;
1291                 bool failed;
1292
1293                 try {
1294                         failed = true;
1295                         val = d / q;
1296                 } catch (DivideByZeroException) {
1297                         failed = false;
1298                 }
1299                 if (failed)
1300                         return 1;
1301
1302                 try {
1303                         failed = true;
1304                         val = d % q;
1305                 } catch (DivideByZeroException) {
1306                         failed = false;
1307                 }
1308                 if (failed)
1309                         return 2;
1310
1311                 return 0;
1312         }
1313
1314         static int test_0_long_div_zero () {
1315                 long d = 1;
1316                 long q = 0;
1317                 long val;
1318                 bool failed;
1319
1320                 try {
1321                         failed = true;
1322                         val = d / q;
1323                 } catch (DivideByZeroException) {
1324                         failed = false;
1325                 }
1326                 if (failed)
1327                         return 1;
1328
1329                 try {
1330                         failed = true;
1331                         val = d % q;
1332                 } catch (DivideByZeroException) {
1333                         failed = false;
1334                 }
1335                 if (failed)
1336                         return 2;
1337
1338                 return 0;
1339         }
1340
1341         static int test_0_ulong_div_zero () {
1342                 ulong d = 1;
1343                 ulong q = 0;
1344                 ulong val;
1345                 bool failed;
1346
1347                 try {
1348                         failed = true;
1349                         val = d / q;
1350                 } catch (DivideByZeroException) {
1351                         failed = false;
1352                 }
1353                 if (failed)
1354                         return 1;
1355
1356                 try {
1357                         failed = true;
1358                         val = d % q;
1359                 } catch (DivideByZeroException) {
1360                         failed = false;
1361                 }
1362                 if (failed)
1363                         return 2;
1364
1365                 return 0;
1366         }
1367
1368         static int test_0_float_div_zero () {
1369                 double d = 1;
1370                 double q = 0;
1371                 double val;
1372                 bool failed;
1373
1374                 try {
1375                         failed = false;
1376                         val = d / q;
1377                 } catch (DivideByZeroException) {
1378                         failed = true;
1379                 }
1380                 if (failed)
1381                         return 1;
1382
1383                 try {
1384                         failed = false;
1385                         val = d % q;
1386                 } catch (DivideByZeroException) {
1387                         failed = true;
1388                 }
1389                 if (failed)
1390                         return 2;
1391
1392                 return 0;
1393         }
1394
1395         static int test_0_invalid_unbox () {
1396
1397                 int i = 123;
1398                 object o = "Some string";
1399                 int res = 1;
1400                 
1401                 try {
1402                         // Illegal conversion; o contains a string not an int
1403                         i = (int) o;   
1404                 } catch (Exception e) {
1405                         if (i ==123)
1406                                 res = 0;
1407                 }
1408
1409                 return res;
1410         }
1411
1412         // Test that double[] can't be cast to double (bug #46027)
1413         static int test_0_invalid_unbox_arrays () {
1414                 double[] d1 = { 1.0 };
1415                 double[][] d2 = { d1 };
1416                 Array a = d2;
1417
1418                 try {
1419                         foreach (double d in a) {
1420                         }
1421                         return 1;
1422                 }
1423                 catch (InvalidCastException e) {
1424                         return 0;
1425                 }
1426         }
1427
1428         /* bug# 42190, at least mcs generates a leave for the return that
1429          * jumps out of multiple exception clauses: we used to execute just 
1430          * one enclosing finally block.
1431          */
1432         static int finally_level;
1433         static void do_something () {
1434                 int a = 0;
1435                 try {
1436                         try {
1437                                 return;
1438                         } finally {
1439                                 a = 1;
1440                         }
1441                 } finally {
1442                         finally_level++;
1443                 }
1444         }
1445
1446         static int test_2_multiple_finally_clauses () {
1447                 finally_level = 0;
1448                 do_something ();
1449                 if (finally_level == 1)
1450                         return 2;
1451                 return 0;
1452         }
1453
1454         static int test_3_checked_cast_un () {
1455                 ulong i = 0x8000000034000000;
1456                 long j;
1457
1458                 try {
1459                         checked { j = (long)i; }
1460                 } catch (OverflowException) {
1461                         j = 2;
1462                 }
1463
1464                 if (j != 2)
1465                         return 0;
1466                 return 3;
1467         }
1468         
1469         static int test_4_checked_cast () {
1470                 long i;
1471                 ulong j;
1472
1473                 unchecked { i = (long)0x8000000034000000;};
1474                 try {
1475                         checked { j = (ulong)i; }
1476                 } catch (OverflowException) {
1477                         j = 3;
1478                 }
1479
1480                 if (j != 3)
1481                         return 0;
1482                 return 4;
1483         }
1484
1485         static readonly int[] mul_dim_results = new int[] {
1486                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1487                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1488                 2, 0, 2, 1, 2, 8, 
1489                 3, 0, 3, 1, 3, 8, 
1490                 4, 0, 4, 1, 4, 8, 
1491                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1492                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1493                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1494         };
1495
1496         static int test_0_multi_dim_array_access () {
1497                 int [,] a = System.Array.CreateInstance (typeof (int),
1498                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1499                 int x, y;
1500                 int result_idx = 0;
1501                 for (x = 0; x < 8; ++x) {
1502                         for (y = 0; y < 9; ++y) {
1503                                 bool got_ex = false;
1504                                 try {
1505                                         a [x, y] = 1;
1506                                 } catch {
1507                                         got_ex = true;
1508                                 }
1509                                 if (got_ex) {
1510                                         if (result_idx >= mul_dim_results.Length)
1511                                                 return -1;
1512                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1513                                                 return result_idx + 1;
1514                                         }
1515                                         result_idx += 2;
1516                                 }
1517                         }
1518                 }
1519                 if (result_idx == mul_dim_results.Length)
1520                         return 0;
1521                 return 200;
1522         }
1523
1524         static void helper_out_obj (out object o) {
1525                 o = (object)"buddy";
1526         }
1527
1528         static void helper_out_string (out string o) {
1529                 o = "buddy";
1530         }
1531
1532         static int test_2_array_mismatch () {
1533                 string[] a = { "hello", "world" };
1534                 object[] b = a;
1535                 bool passed = false;
1536
1537                 try {
1538                         helper_out_obj (out b [1]);
1539                 } catch (ArrayTypeMismatchException) {
1540                         passed = true;
1541                 }
1542                 if (!passed)
1543                         return 0;
1544                 helper_out_string (out a [1]);
1545                 if (a [1] != "buddy")
1546                         return 1;
1547                 return 2;
1548         }
1549
1550         static int test_0_long_add_ovf () {
1551                 long l;
1552                 bool failed = true;
1553
1554                 try {
1555                         l = 999999999999999999;
1556                         
1557                         checked {
1558                                 l = l * 10;
1559                         }
1560                         
1561                 } catch (OverflowException) {
1562                         failed = false;
1563                 }
1564
1565                 if (failed)
1566                         return 1;
1567
1568                 return 0;
1569         }
1570 }
1571