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