2004-05-25 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                 try {
1317                         failed = true;
1318                         q = -1;
1319                         d = Int32.MinValue;
1320                         val = d / q;
1321                 } catch (ArithmeticException) {
1322                         failed = false;
1323                 } catch (DivideByZeroException) {
1324                         /* wrong exception */
1325                 }
1326                 if (failed)
1327                         return 3;
1328
1329                 try {
1330                         failed = true;
1331                         q = -1;
1332                         d = Int32.MinValue;
1333                         val = d % q;
1334                 } catch (ArithmeticException) {
1335                         failed = false;
1336                 } catch (DivideByZeroException) {
1337                         /* wrong exception */
1338                 }
1339                 if (failed)
1340                         return 4;
1341
1342                 return 0;
1343         }
1344
1345         static int test_0_udiv_zero () {
1346                 uint d = 1;
1347                 uint q = 0;
1348                 uint val;
1349                 bool failed;
1350
1351                 try {
1352                         failed = true;
1353                         val = d / q;
1354                 } catch (DivideByZeroException) {
1355                         failed = false;
1356                 }
1357                 if (failed)
1358                         return 1;
1359
1360                 try {
1361                         failed = true;
1362                         val = d % q;
1363                 } catch (DivideByZeroException) {
1364                         failed = false;
1365                 }
1366                 if (failed)
1367                         return 2;
1368
1369                 return 0;
1370         }
1371
1372         static int test_0_long_div_zero () {
1373                 long d = 1;
1374                 long q = 0;
1375                 long val;
1376                 bool failed;
1377
1378                 try {
1379                         failed = true;
1380                         val = d / q;
1381                 } catch (DivideByZeroException) {
1382                         failed = false;
1383                 }
1384                 if (failed)
1385                         return 1;
1386
1387                 try {
1388                         failed = true;
1389                         val = d % q;
1390                 } catch (DivideByZeroException) {
1391                         failed = false;
1392                 }
1393                 if (failed)
1394                         return 2;
1395
1396                 try {
1397                         failed = true;
1398                         q = -1;
1399                         d = Int64.MinValue;
1400                         val = d / q;
1401                 } catch (ArithmeticException) {
1402                         failed = false;
1403                 } catch (DivideByZeroException) {
1404                         /* wrong exception */
1405                 }
1406                 if (failed)
1407                         return 3;
1408
1409                 try {
1410                         failed = true;
1411                         q = -1;
1412                         d = Int64.MinValue;
1413                         val = d % q;
1414                 } catch (ArithmeticException) {
1415                         failed = false;
1416                 } catch (DivideByZeroException) {
1417                         /* wrong exception */
1418                 }
1419                 if (failed)
1420                         return 4;
1421
1422                 return 0;
1423         }
1424
1425         static int test_0_ulong_div_zero () {
1426                 ulong d = 1;
1427                 ulong q = 0;
1428                 ulong val;
1429                 bool failed;
1430
1431                 try {
1432                         failed = true;
1433                         val = d / q;
1434                 } catch (DivideByZeroException) {
1435                         failed = false;
1436                 }
1437                 if (failed)
1438                         return 1;
1439
1440                 try {
1441                         failed = true;
1442                         val = d % q;
1443                 } catch (DivideByZeroException) {
1444                         failed = false;
1445                 }
1446                 if (failed)
1447                         return 2;
1448
1449                 return 0;
1450         }
1451
1452         static int test_0_float_div_zero () {
1453                 double d = 1;
1454                 double q = 0;
1455                 double val;
1456                 bool failed;
1457
1458                 try {
1459                         failed = false;
1460                         val = d / q;
1461                 } catch (DivideByZeroException) {
1462                         failed = true;
1463                 }
1464                 if (failed)
1465                         return 1;
1466
1467                 try {
1468                         failed = false;
1469                         val = d % q;
1470                 } catch (DivideByZeroException) {
1471                         failed = true;
1472                 }
1473                 if (failed)
1474                         return 2;
1475
1476                 return 0;
1477         }
1478
1479         static int test_0_invalid_unbox () {
1480
1481                 int i = 123;
1482                 object o = "Some string";
1483                 int res = 1;
1484                 
1485                 try {
1486                         // Illegal conversion; o contains a string not an int
1487                         i = (int) o;   
1488                 } catch (Exception e) {
1489                         if (i ==123)
1490                                 res = 0;
1491                 }
1492
1493                 return res;
1494         }
1495
1496         // Test that double[] can't be cast to double (bug #46027)
1497         static int test_0_invalid_unbox_arrays () {
1498                 double[] d1 = { 1.0 };
1499                 double[][] d2 = { d1 };
1500                 Array a = d2;
1501
1502                 try {
1503                         foreach (double d in a) {
1504                         }
1505                         return 1;
1506                 }
1507                 catch (InvalidCastException e) {
1508                         return 0;
1509                 }
1510         }
1511
1512         /* bug# 42190, at least mcs generates a leave for the return that
1513          * jumps out of multiple exception clauses: we used to execute just 
1514          * one enclosing finally block.
1515          */
1516         static int finally_level;
1517         static void do_something () {
1518                 int a = 0;
1519                 try {
1520                         try {
1521                                 return;
1522                         } finally {
1523                                 a = 1;
1524                         }
1525                 } finally {
1526                         finally_level++;
1527                 }
1528         }
1529
1530         static int test_2_multiple_finally_clauses () {
1531                 finally_level = 0;
1532                 do_something ();
1533                 if (finally_level == 1)
1534                         return 2;
1535                 return 0;
1536         }
1537
1538         static int test_3_checked_cast_un () {
1539                 ulong i = 0x8000000034000000;
1540                 long j;
1541
1542                 try {
1543                         checked { j = (long)i; }
1544                 } catch (OverflowException) {
1545                         j = 2;
1546                 }
1547
1548                 if (j != 2)
1549                         return 0;
1550                 return 3;
1551         }
1552         
1553         static int test_4_checked_cast () {
1554                 long i;
1555                 ulong j;
1556
1557                 unchecked { i = (long)0x8000000034000000;};
1558                 try {
1559                         checked { j = (ulong)i; }
1560                 } catch (OverflowException) {
1561                         j = 3;
1562                 }
1563
1564                 if (j != 3)
1565                         return 0;
1566                 return 4;
1567         }
1568
1569         static readonly int[] mul_dim_results = new int[] {
1570                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1571                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1572                 2, 0, 2, 1, 2, 8, 
1573                 3, 0, 3, 1, 3, 8, 
1574                 4, 0, 4, 1, 4, 8, 
1575                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1576                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1577                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1578         };
1579
1580         static int test_0_multi_dim_array_access () {
1581                 int [,] a = System.Array.CreateInstance (typeof (int),
1582                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1583                 int x, y;
1584                 int result_idx = 0;
1585                 for (x = 0; x < 8; ++x) {
1586                         for (y = 0; y < 9; ++y) {
1587                                 bool got_ex = false;
1588                                 try {
1589                                         a [x, y] = 1;
1590                                 } catch {
1591                                         got_ex = true;
1592                                 }
1593                                 if (got_ex) {
1594                                         if (result_idx >= mul_dim_results.Length)
1595                                                 return -1;
1596                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1597                                                 return result_idx + 1;
1598                                         }
1599                                         result_idx += 2;
1600                                 }
1601                         }
1602                 }
1603                 if (result_idx == mul_dim_results.Length)
1604                         return 0;
1605                 return 200;
1606         }
1607
1608         static void helper_out_obj (out object o) {
1609                 o = (object)"buddy";
1610         }
1611
1612         static void helper_out_string (out string o) {
1613                 o = "buddy";
1614         }
1615
1616         static int test_2_array_mismatch () {
1617                 string[] a = { "hello", "world" };
1618                 object[] b = a;
1619                 bool passed = false;
1620
1621                 try {
1622                         helper_out_obj (out b [1]);
1623                 } catch (ArrayTypeMismatchException) {
1624                         passed = true;
1625                 }
1626                 if (!passed)
1627                         return 0;
1628                 helper_out_string (out a [1]);
1629                 if (a [1] != "buddy")
1630                         return 1;
1631                 return 2;
1632         }
1633
1634         static int test_0_ovf () {
1635                 int ocount = 0;
1636                 
1637                 checked {
1638
1639                         ocount = 0;
1640                         try {
1641                                 ulong a =  UInt64.MaxValue - 1;
1642                                 ulong t = a++;
1643                         } catch {
1644                                 ocount++;
1645                         }
1646                         if (ocount != 0)
1647                                 return 1;
1648
1649                         ocount = 0;
1650                         try {
1651                                 ulong a =  UInt64.MaxValue;
1652                                 ulong t = a++;
1653                         } catch {
1654                                 ocount++;
1655                         }
1656                         if (ocount != 1)
1657                                 return 2;
1658
1659                         ocount = 0;
1660                         try {
1661                                 long a = Int64.MaxValue - 1;
1662                                 long t = a++;
1663                         } catch {
1664                                 ocount++;
1665                         }
1666                         if (ocount != 0)
1667                                 return 3;
1668
1669                         try {
1670                                 long a = Int64.MaxValue;
1671                                 long t = a++;
1672                         } catch {
1673                                 ocount++;
1674                         }
1675                         if (ocount != 1)
1676                                 return 4;
1677
1678                         ocount = 0;
1679                         try {
1680                                 ulong a = UInt64.MaxValue - 1;
1681                                 ulong t = a++;
1682                         } catch {
1683                                 ocount++;
1684                         }
1685                         if (ocount != 0)
1686                                 return 5;
1687
1688                         try {
1689                                 ulong a = UInt64.MaxValue;
1690                                 ulong t = a++;
1691                         } catch {
1692                                 ocount++;
1693                         }
1694                         if (ocount != 1)
1695                                 return 6;
1696
1697                         ocount = 0;
1698                         try {
1699                                 long a = Int64.MinValue + 1;
1700                                 long t = a--;
1701                         } catch {
1702                                 ocount++;
1703                         }
1704                         if (ocount != 0)
1705                                 return 7;
1706
1707                         ocount = 0;
1708                         try {
1709                                 long a = Int64.MinValue;
1710                                 long t = a--;
1711                         } catch {
1712                                 ocount++;
1713                         }
1714                         if (ocount != 1)
1715                                 return 8;
1716
1717                         ocount = 0;
1718                         try {
1719                                 ulong a = UInt64.MinValue + 1;
1720                                 ulong t = a--;
1721                         } catch {
1722                                 ocount++;
1723                         }
1724                         if (ocount != 0)
1725                                 return 9;
1726
1727                         ocount = 0;
1728                         try {
1729                                 ulong a = UInt64.MinValue;
1730                                 ulong t = a--;
1731                         } catch {
1732                                 ocount++;
1733                         }
1734                         if (ocount != 1)
1735                                 return 10;
1736
1737                         ocount = 0;
1738                         try {
1739                                 int a = Int32.MinValue + 1;
1740                                 int t = a--;
1741                         } catch {
1742                                 ocount++;
1743                         }
1744                         if (ocount != 0)
1745                                 return 11;
1746
1747                         ocount = 0;
1748                         try {
1749                                 int a = Int32.MinValue;
1750                                 int t = a--;
1751                         } catch {
1752                                 ocount++;
1753                         }
1754                         if (ocount != 1)
1755                                 return 12;
1756
1757                         ocount = 0;
1758                         try {
1759                                 uint a = 1;
1760                                 uint t = a--;
1761                         } catch {
1762                                 ocount++;
1763                         }
1764                         if (ocount != 0)
1765                                 return 13;
1766
1767                         ocount = 0;
1768                         try {
1769                                 uint a = 0;
1770                                 uint t = a--;
1771                         } catch {
1772                                 ocount++;
1773                         }
1774                         if (ocount != 1)
1775                                 return 14;
1776
1777                         ocount = 0;
1778                         try {
1779                                 sbyte a = 126;
1780                                 sbyte t = a++;
1781                         } catch {
1782                                 ocount++;
1783                         }
1784                         if (ocount != 0)
1785                                 return 15;
1786
1787                         ocount = 0;
1788                         try {
1789                                 sbyte a = 127;
1790                                 sbyte t = a++;
1791                         } catch {
1792                                 ocount++;
1793                         }
1794                         if (ocount != 1)
1795                                 return 16;
1796
1797                         ocount = 0;
1798                         try {
1799                         } catch {
1800                                 ocount++;
1801                         }
1802                         if (ocount != 0)
1803                                 return 17;
1804
1805                         ocount = 0;
1806                         try {
1807                                 int a = 1 << 29;
1808                                 int t = a*2;
1809                         } catch {
1810                                 ocount++;
1811                         }
1812                         if (ocount != 0)
1813                                 return 18;
1814
1815                         ocount = 0;
1816                         try {
1817                                 int a = 1 << 30;
1818                                 int t = a*2;
1819                         } catch {
1820                                 ocount++;
1821                         }
1822                         if (ocount != 1)
1823                                 return 19;
1824
1825                         ocount = 0;
1826                         try {
1827                                 ulong a = 0xffffffffff;
1828                                 ulong t = a*0x0ffffff;
1829                         } catch {
1830                                 ocount++;
1831                         }
1832                         if (ocount != 0)
1833                                 return 20;
1834
1835                         ocount = 0;
1836                         try {
1837                                 ulong a = 0xffffffffff;
1838                                 ulong t = a*0x0fffffff;
1839                         } catch {
1840                                 ocount++;
1841                         }
1842                         if (ocount != 1)
1843                                 return 21;
1844                 }
1845                 
1846                 return 0;
1847         }
1848
1849         class Broken {
1850                 static int i;
1851
1852                 static Broken () {
1853                         throw new Exception ("Ugh!");
1854                 }
1855         
1856                 public static int DoSomething () {
1857                         return i;
1858                 }
1859         }
1860
1861         static int test_0_exception_in_cctor () {
1862                 try {
1863                         Broken.DoSomething ();
1864                 }
1865                 catch (TypeInitializationException) {
1866                         // This will only happen once even if --regression is used
1867                 }
1868                 return 0;
1869         }
1870
1871         static int test_5_regalloc () {
1872                 int i = 0;
1873
1874                 try {
1875                         for (i = 0; i < 10; ++i) {
1876                                 if (i == 5)
1877                                         throw new Exception ();
1878                         }
1879                 }
1880                 catch (Exception) {
1881                         if (i != 5)
1882                                 return i;
1883                 }
1884
1885                 // Check that variables written in catch clauses are volatile
1886                 int j = 0;
1887                 try {
1888                         throw new Exception ();
1889                 }
1890                 catch (Exception) {
1891                         j = 5;
1892                 }
1893                 if (j != 5)
1894                         return 6;
1895
1896                 int k = 0;
1897                 try {
1898                         try {
1899                                 throw new Exception ();
1900                         }
1901                         finally {
1902                                 k = 5;
1903                         }
1904                 }
1905                 catch (Exception) {
1906                 }
1907                 if (k != 5)
1908                         return 7;
1909
1910                 return i;
1911         }
1912
1913         /* MarshalByRefObject prevents the methods from being inlined */
1914         class ThrowClass : MarshalByRefObject {
1915                 public static void rethrow1 () {
1916                         throw new Exception ();
1917                 }
1918
1919                 public static void rethrow2 () {
1920                         rethrow1 ();
1921                 }
1922         }
1923
1924         static int test_0_rethrow_stacktrace () {
1925                 // Check that rethrowing an exception preserves the original stack trace
1926                 try {
1927                         try {
1928                                 ThrowClass.rethrow2 ();
1929                         }
1930                         catch (Exception ex) {
1931                                 throw;
1932                         }
1933                 }
1934                 catch (Exception ex) {
1935                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
1936                                 return 0;
1937                 }
1938
1939                 return 1;
1940         }                       
1941 }
1942