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