2004-01-23 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / exceptions.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         static int test_0_catch () {
33                 Exception x = new Exception ();
34                 
35                 try {
36                         throw x;
37                 } catch (Exception e) {
38                         if (e == x)
39                                 return 0;
40                 }
41                 return 1;
42         }
43
44         static int test_0_finally_without_exc () {
45                 int x;
46                 
47                 try {
48                         x = 1;
49                 } catch (Exception e) {
50                         x = 2;
51                 } finally {
52                         x = 0;
53                 }
54                 
55                 return x;
56         }
57
58         static int test_0_finally () {
59                 int x = 1;
60                 
61                 try {
62                         throw new Exception ();
63                 } catch (Exception e) {
64                         x = 2;
65                 } finally {
66                         x = 0;
67                 }
68                 return x;
69         }
70
71         static int test_0_nested_finally () {
72                 int a;
73
74                 try {
75                         a = 1;
76                 } finally {
77                         try {
78                                 a = 2;
79                         } finally {
80                                 a = 0;
81                         }
82                 }
83                 return a;
84         }               
85
86         static int test_0_byte_cast () {
87                 int a;
88                 long l;
89                 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                 {
1013                         int i; 
1014                         float f = 1.1f;
1015                         checked {
1016                                 i = (int) f;
1017                         }
1018                 }
1019
1020                 return 0;
1021         }
1022
1023         static int test_0_uint_cast () {
1024                 uint a;
1025                 long l;
1026                 bool failed;
1027
1028                 try {
1029                         double d =  System.UInt32.MaxValue;
1030                         failed = false;
1031                         checked {
1032                                 a = (uint)d;
1033                         }
1034                 } catch (OverflowException) {
1035                         failed = true;
1036                 }
1037                 if (failed)
1038                         return 1;
1039
1040                 try {
1041                         double d = System.UInt32.MaxValue + 1.0;
1042                         failed = true;
1043                         checked {
1044                                 a = (uint)d;
1045                         }
1046                 } catch (OverflowException) {
1047                         failed = false;
1048                 }
1049                 if (failed)
1050                         return 2;
1051
1052                 try {
1053                         double d = System.UInt32.MinValue;
1054                         failed = false;
1055                         checked {
1056                                 a = (uint)d;
1057                         }
1058                 } catch (OverflowException) {
1059                         failed = true;
1060                 }
1061                 if (failed)
1062                         return 3;
1063
1064                 try {
1065                         double d = System.UInt32.MinValue - 1.0;
1066                         failed = true;
1067                         checked {
1068                                 a = (uint)d;
1069                         }
1070                 } catch (OverflowException) {
1071                         failed = false;
1072                 }
1073                 if (failed)
1074                         return 4;
1075                 
1076                 try {
1077                         l =  System.UInt32.MaxValue;
1078                         failed = false;
1079                         checked {
1080                                 a = (uint)l;
1081                         }
1082                 } catch (OverflowException) {
1083                         failed = true;
1084                 }
1085                 if (failed)
1086                         return 5;
1087
1088                 try {
1089                         l = System.UInt32.MaxValue + (long)1;
1090                         failed = true;
1091                         checked {
1092                                 a = (uint)l;
1093                         }
1094                 } catch (OverflowException) {
1095                         failed = false;
1096                 }
1097                 if (failed)
1098                         return 6;
1099
1100                 try {
1101                         l = System.UInt32.MinValue;
1102                         failed = false;
1103                         checked {
1104                                 a = (uint)l;
1105                         }
1106                 } catch (OverflowException) {
1107                         failed = true;
1108                 }
1109                 if (failed)
1110                         return 7;
1111
1112                 try {
1113                         l = System.UInt32.MinValue - (long)1;
1114                         failed = true;
1115                         checked {
1116                                 a = (uint)l;
1117                         }
1118                 } catch (OverflowException) {
1119                         failed = false;
1120                 }
1121                 if (failed)
1122                         return 8;
1123
1124                 {
1125                         uint i; 
1126                         float f = 1.1f;
1127                         checked {
1128                                 i = (uint) f;
1129                         }
1130                 }
1131                 
1132                 return 0;
1133         }
1134         
1135         static int test_0_long_cast () {
1136                 long a;
1137                 bool failed;
1138
1139                 try {
1140                         double d = System.Int64.MaxValue - 512.0;
1141                         failed = true;
1142                         checked {
1143                                 a = (long)d;
1144                         }
1145                 } catch (OverflowException) {
1146                         failed = false;
1147                 }
1148                 if (failed)
1149                         return 1;
1150
1151                 try {
1152                         double d = System.Int64.MaxValue - 513.0;
1153                         failed = false;
1154                         checked {
1155                                 a = (long)d;
1156                         }
1157                 } catch (OverflowException) {
1158                         failed = true;
1159                 }
1160                 if (failed)
1161                         return 2;
1162                 
1163
1164                 try {
1165                         double d = System.Int64.MinValue - 1024.0;
1166                         failed = false;                 
1167                         checked {
1168                                 a = (long)d;
1169                         }
1170                 } catch (OverflowException) {
1171                         failed = true;
1172                 }
1173                 if (failed)
1174                         return 3;
1175
1176                 try {
1177                         double d = System.Int64.MinValue - 1025.0;
1178                         failed = true;
1179                         checked {
1180                                 a = (long)d;
1181                         }
1182                 } catch (OverflowException) {
1183                         failed = false;
1184                 }
1185                 if (failed)
1186                         return 4;
1187
1188                 {
1189                         long i; 
1190                         float f = 1.1f;
1191                         checked {
1192                                 i = (long) f;
1193                         }
1194                 }
1195
1196                 return 0;
1197         }
1198
1199         static int test_0_ulong_cast () {
1200                 ulong a;
1201                 bool failed;
1202
1203                 try {
1204                         double d = System.UInt64.MaxValue - 1024.0;
1205                         failed = true;
1206                         checked {
1207                                 a = (ulong)d;
1208                         }
1209                 } catch (OverflowException) {
1210                         failed = false;
1211                 }
1212                 if (failed)
1213                         return 1;
1214
1215                 try {
1216                         double d = System.UInt64.MaxValue - 1025.0;
1217                         failed = false;
1218                         checked {
1219                                 a = (ulong)d;
1220                         }
1221                 } catch (OverflowException) {
1222                         failed = true;
1223                 }
1224                 if (failed)
1225                         return 2;
1226                 
1227
1228                 try {
1229                         double d = 0;
1230                         failed = false;                 
1231                         checked {
1232                                 a = (ulong)d;
1233                         }
1234                 } catch (OverflowException) {
1235                         failed = true;
1236                 }
1237                 if (failed)
1238                         return 3;
1239
1240                 try {
1241                         double d = -1;
1242                         failed = true;
1243                         checked {
1244                                 a = (ulong)d;
1245                         }
1246                 } catch (OverflowException) {
1247                         failed = false;
1248                 }
1249                 if (failed)
1250                         return 4;
1251
1252                 {
1253                         ulong i; 
1254                         float f = 1.1f;
1255                         checked {
1256                                 i = (ulong) f;
1257                         }
1258                 }
1259
1260                 return 0;
1261         }
1262
1263         static int test_0_simple_double_casts () {
1264
1265                 double d = 0xffffffff;
1266
1267                 if ((uint)d != 4294967295)
1268                         return 1;
1269
1270                 d = 0xffffffffffffffff;
1271
1272                 if ((ulong)d != 0)
1273                         return 2;
1274
1275                 if ((ushort)d != 0)
1276                         return 3;
1277                         
1278                 if ((byte)d != 0)
1279                         return 4;
1280                         
1281                 d = 0xffff;
1282
1283                 if ((ushort)d != 0xffff)
1284                         return 5;
1285                 
1286                 if ((byte)d != 0xff)
1287                         return 6;
1288                         
1289                 return 0;
1290         }
1291         
1292         static int test_0_div_zero () {
1293                 int d = 1;
1294                 int q = 0;
1295                 int val;
1296                 bool failed;
1297
1298                 try {
1299                         failed = true;
1300                         val = d / q;
1301                 } catch (DivideByZeroException) {
1302                         failed = false;
1303                 }
1304                 if (failed)
1305                         return 1;
1306
1307                 try {
1308                         failed = true;
1309                         val = d % q;
1310                 } catch (DivideByZeroException) {
1311                         failed = false;
1312                 }
1313                 if (failed)
1314                         return 2;
1315
1316                 return 0;
1317         }
1318
1319         static int test_0_udiv_zero () {
1320                 uint d = 1;
1321                 uint q = 0;
1322                 uint val;
1323                 bool failed;
1324
1325                 try {
1326                         failed = true;
1327                         val = d / q;
1328                 } catch (DivideByZeroException) {
1329                         failed = false;
1330                 }
1331                 if (failed)
1332                         return 1;
1333
1334                 try {
1335                         failed = true;
1336                         val = d % q;
1337                 } catch (DivideByZeroException) {
1338                         failed = false;
1339                 }
1340                 if (failed)
1341                         return 2;
1342
1343                 return 0;
1344         }
1345
1346         static int test_0_long_div_zero () {
1347                 long d = 1;
1348                 long q = 0;
1349                 long val;
1350                 bool failed;
1351
1352                 try {
1353                         failed = true;
1354                         val = d / q;
1355                 } catch (DivideByZeroException) {
1356                         failed = false;
1357                 }
1358                 if (failed)
1359                         return 1;
1360
1361                 try {
1362                         failed = true;
1363                         val = d % q;
1364                 } catch (DivideByZeroException) {
1365                         failed = false;
1366                 }
1367                 if (failed)
1368                         return 2;
1369
1370                 return 0;
1371         }
1372
1373         static int test_0_ulong_div_zero () {
1374                 ulong d = 1;
1375                 ulong q = 0;
1376                 ulong val;
1377                 bool failed;
1378
1379                 try {
1380                         failed = true;
1381                         val = d / q;
1382                 } catch (DivideByZeroException) {
1383                         failed = false;
1384                 }
1385                 if (failed)
1386                         return 1;
1387
1388                 try {
1389                         failed = true;
1390                         val = d % q;
1391                 } catch (DivideByZeroException) {
1392                         failed = false;
1393                 }
1394                 if (failed)
1395                         return 2;
1396
1397                 return 0;
1398         }
1399
1400         static int test_0_float_div_zero () {
1401                 double d = 1;
1402                 double q = 0;
1403                 double val;
1404                 bool failed;
1405
1406                 try {
1407                         failed = false;
1408                         val = d / q;
1409                 } catch (DivideByZeroException) {
1410                         failed = true;
1411                 }
1412                 if (failed)
1413                         return 1;
1414
1415                 try {
1416                         failed = false;
1417                         val = d % q;
1418                 } catch (DivideByZeroException) {
1419                         failed = true;
1420                 }
1421                 if (failed)
1422                         return 2;
1423
1424                 return 0;
1425         }
1426
1427         static int test_0_invalid_unbox () {
1428
1429                 int i = 123;
1430                 object o = "Some string";
1431                 int res = 1;
1432                 
1433                 try {
1434                         // Illegal conversion; o contains a string not an int
1435                         i = (int) o;   
1436                 } catch (Exception e) {
1437                         if (i ==123)
1438                                 res = 0;
1439                 }
1440
1441                 return res;
1442         }
1443
1444         // Test that double[] can't be cast to double (bug #46027)
1445         static int test_0_invalid_unbox_arrays () {
1446                 double[] d1 = { 1.0 };
1447                 double[][] d2 = { d1 };
1448                 Array a = d2;
1449
1450                 try {
1451                         foreach (double d in a) {
1452                         }
1453                         return 1;
1454                 }
1455                 catch (InvalidCastException e) {
1456                         return 0;
1457                 }
1458         }
1459
1460         /* bug# 42190, at least mcs generates a leave for the return that
1461          * jumps out of multiple exception clauses: we used to execute just 
1462          * one enclosing finally block.
1463          */
1464         static int finally_level;
1465         static void do_something () {
1466                 int a = 0;
1467                 try {
1468                         try {
1469                                 return;
1470                         } finally {
1471                                 a = 1;
1472                         }
1473                 } finally {
1474                         finally_level++;
1475                 }
1476         }
1477
1478         static int test_2_multiple_finally_clauses () {
1479                 finally_level = 0;
1480                 do_something ();
1481                 if (finally_level == 1)
1482                         return 2;
1483                 return 0;
1484         }
1485
1486         static int test_3_checked_cast_un () {
1487                 ulong i = 0x8000000034000000;
1488                 long j;
1489
1490                 try {
1491                         checked { j = (long)i; }
1492                 } catch (OverflowException) {
1493                         j = 2;
1494                 }
1495
1496                 if (j != 2)
1497                         return 0;
1498                 return 3;
1499         }
1500         
1501         static int test_4_checked_cast () {
1502                 long i;
1503                 ulong j;
1504
1505                 unchecked { i = (long)0x8000000034000000;};
1506                 try {
1507                         checked { j = (ulong)i; }
1508                 } catch (OverflowException) {
1509                         j = 3;
1510                 }
1511
1512                 if (j != 3)
1513                         return 0;
1514                 return 4;
1515         }
1516
1517         static readonly int[] mul_dim_results = new int[] {
1518                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1519                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1520                 2, 0, 2, 1, 2, 8, 
1521                 3, 0, 3, 1, 3, 8, 
1522                 4, 0, 4, 1, 4, 8, 
1523                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1524                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1525                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1526         };
1527
1528         static int test_0_multi_dim_array_access () {
1529                 int [,] a = System.Array.CreateInstance (typeof (int),
1530                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1531                 int x, y;
1532                 int result_idx = 0;
1533                 for (x = 0; x < 8; ++x) {
1534                         for (y = 0; y < 9; ++y) {
1535                                 bool got_ex = false;
1536                                 try {
1537                                         a [x, y] = 1;
1538                                 } catch {
1539                                         got_ex = true;
1540                                 }
1541                                 if (got_ex) {
1542                                         if (result_idx >= mul_dim_results.Length)
1543                                                 return -1;
1544                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1545                                                 return result_idx + 1;
1546                                         }
1547                                         result_idx += 2;
1548                                 }
1549                         }
1550                 }
1551                 if (result_idx == mul_dim_results.Length)
1552                         return 0;
1553                 return 200;
1554         }
1555
1556         static void helper_out_obj (out object o) {
1557                 o = (object)"buddy";
1558         }
1559
1560         static void helper_out_string (out string o) {
1561                 o = "buddy";
1562         }
1563
1564         static int test_2_array_mismatch () {
1565                 string[] a = { "hello", "world" };
1566                 object[] b = a;
1567                 bool passed = false;
1568
1569                 try {
1570                         helper_out_obj (out b [1]);
1571                 } catch (ArrayTypeMismatchException) {
1572                         passed = true;
1573                 }
1574                 if (!passed)
1575                         return 0;
1576                 helper_out_string (out a [1]);
1577                 if (a [1] != "buddy")
1578                         return 1;
1579                 return 2;
1580         }
1581
1582         static int test_0_long_add_ovf () {
1583                 long l;
1584                 bool failed = true;
1585
1586                 try {
1587                         l = 999999999999999999;
1588                         
1589                         checked {
1590                                 l = l * 10;
1591                         }
1592                         
1593                 } catch (OverflowException) {
1594                         failed = false;
1595                 }
1596
1597                 if (failed)
1598                         return 1;
1599
1600                 return 0;
1601         }
1602 }
1603