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