* mini-s390[x].c (is_regsize_var): Support PTR/FNPTR too.
[mono.git] / mono / mini / exceptions.cs
blob839aa4c81369bc17ced6df4af85b7e3008e9daab
1 using System;
2 using System.Reflection;
4 /*
5 * Regression tests for the mono JIT.
7 * Each test needs to be of the form:
9 * public static int test_<result>_<name> ();
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.
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)
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.
26 class Tests {
28 public static int Main () {
29 return TestDriver.RunTests (typeof (Tests));
32 public static int test_0_catch () {
33 Exception x = new Exception ();
35 try {
36 throw x;
37 } catch (Exception e) {
38 if (e == x)
39 return 0;
41 return 1;
44 public static int test_0_finally_without_exc () {
45 int x;
47 try {
48 x = 1;
49 } catch (Exception e) {
50 x = 2;
51 } finally {
52 x = 0;
55 return x;
58 public static int test_0_finally () {
59 int x = 1;
61 try {
62 throw new Exception ();
63 } catch (Exception e) {
64 x = 2;
65 } finally {
66 x = 0;
68 return x;
71 public static int test_0_nested_finally () {
72 int a;
74 try {
75 a = 1;
76 } finally {
77 try {
78 a = 2;
79 } finally {
80 a = 0;
83 return a;
86 public static int test_0_byte_cast () {
87 int a;
88 long l;
89 ulong ul;
90 byte b = 0;
91 bool failed;
93 try {
94 a = 255;
95 failed = false;
96 checked {
97 b = (byte)a;
99 } catch (OverflowException) {
100 failed = true;
102 if (failed)
103 return 1;
104 if (b != 255)
105 return -1;
107 try {
108 a = 0;
109 failed = false;
110 checked {
111 b = (byte)a;
113 } catch (OverflowException) {
114 failed = true;
116 if (failed)
117 return 2;
118 if (b != 0)
119 return -2;
122 try {
123 a = 256;
124 failed = true;
125 checked {
126 b = (byte)a;
128 } catch (OverflowException) {
129 failed = false;
131 if (failed)
132 return 3;
133 if (b != 0)
134 return -3;
136 try {
137 a = -1;
138 failed = true;
139 checked {
140 b = (byte)a;
142 } catch (OverflowException) {
143 failed = false;
145 if (failed)
146 return 4;
147 if (b != 0)
148 return -4;
150 try {
151 double d = 0;
152 failed = false;
153 checked {
154 b = (byte)d;
156 } catch (OverflowException) {
157 failed = true;
159 if (failed)
160 return 5;
161 if (b != 0)
162 return -5;
164 try {
165 double d = -1;
166 failed = true;
167 checked {
168 b = (byte)d;
170 } catch (OverflowException) {
171 failed = false;
173 if (failed)
174 return 6;
175 if (b != 0)
176 return -6;
178 try {
179 double d = 255;
180 failed = false;
181 checked {
182 b = (byte)d;
184 } catch (OverflowException) {
185 failed = true;
187 if (failed)
188 return 7;
189 if (b != 255)
190 return -7;
192 try {
193 double d = 256;
194 failed = true;
195 checked {
196 b = (byte)d;
198 } catch (OverflowException) {
199 failed = false;
201 if (failed)
202 return 8;
203 if (b != 255)
204 return -8;
206 try {
207 l = 255;
208 failed = false;
209 checked {
210 b = (byte)l;
212 } catch (OverflowException) {
213 failed = true;
215 if (failed)
216 return 9;
217 if (b != 255)
218 return -9;
220 try {
221 l = 0;
222 failed = false;
223 checked {
224 b = (byte)l;
226 } catch (OverflowException) {
227 failed = true;
229 if (failed)
230 return 10;
231 if (b != 0)
232 return -10;
234 try {
235 l = 256;
236 failed = true;
237 checked {
238 b = (byte)l;
240 } catch (OverflowException) {
241 failed = false;
243 if (failed)
244 return 11;
245 if (b != 0)
246 return -11;
248 try {
249 l = -1;
250 failed = true;
251 checked {
252 b = (byte)l;
254 } catch (OverflowException) {
255 failed = false;
257 if (failed)
258 return 12;
259 if (b != 0)
260 return -12;
262 try {
263 ul = 256;
264 failed = true;
265 checked {
266 b = (byte)ul;
269 catch (OverflowException) {
270 failed = false;
272 if (failed)
273 return 13;
274 if (b != 0)
275 return -13;
277 return 0;
280 public static int test_0_sbyte_cast () {
281 int a;
282 long l;
283 sbyte b = 0;
284 bool failed;
286 try {
287 a = 255;
288 failed = true;
289 checked {
290 b = (sbyte)a;
292 } catch (OverflowException) {
293 failed = false;
295 if (failed)
296 return 1;
297 if (b != 0)
298 return -1;
300 try {
301 a = 0;
302 failed = false;
303 checked {
304 b = (sbyte)a;
306 } catch (OverflowException) {
307 failed = true;
309 if (failed)
310 return 2;
311 if (b != 0)
312 return -2;
314 try {
315 a = 256;
316 failed = true;
317 checked {
318 b = (sbyte)a;
320 } catch (OverflowException) {
321 failed = false;
323 if (failed)
324 return 3;
325 if (b != 0)
326 return -3;
328 try {
329 a = -129;
330 failed = true;
331 checked {
332 b = (sbyte)a;
334 } catch (OverflowException) {
335 failed = false;
337 if (failed)
338 return 4;
339 if (b != 0)
340 return -4;
342 try {
343 a = -1;
344 failed = false;
345 checked {
346 b = (sbyte)a;
348 } catch (OverflowException) {
349 failed = true;
351 if (failed)
352 return 5;
353 if (b != -1)
354 return -5;
356 try {
357 a = -128;
358 failed = false;
359 checked {
360 b = (sbyte)a;
362 } catch (OverflowException) {
363 failed = true;
365 if (failed)
366 return 6;
367 if (b != -128)
368 return -6;
370 try {
371 a = 127;
372 failed = false;
373 checked {
374 b = (sbyte)a;
376 } catch (OverflowException) {
377 failed = true;
379 if (failed)
380 return 7;
381 if (b != 127)
382 return -7;
384 try {
385 a = 128;
386 failed = true;
387 checked {
388 b = (sbyte)a;
390 } catch (OverflowException) {
391 failed = false;
393 if (failed)
394 return 8;
395 if (b != 127)
396 return -8;
398 try {
399 double d = 127;
400 failed = false;
401 checked {
402 b = (sbyte)d;
404 } catch (OverflowException) {
405 failed = true;
407 if (failed)
408 return 9;
409 if (b != 127)
410 return -9;
412 try {
413 double d = -128;
414 failed = false;
415 checked {
416 b = (sbyte)d;
418 } catch (OverflowException) {
419 failed = true;
421 if (failed)
422 return 10;
423 if (b != -128)
424 return -10;
426 try {
427 double d = 128;
428 failed = true;
429 checked {
430 b = (sbyte)d;
432 } catch (OverflowException) {
433 failed = false;
435 if (failed)
436 return 11;
437 if (b != -128)
438 return -11;
440 try {
441 double d = -129;
442 failed = true;
443 checked {
444 b = (sbyte)d;
446 } catch (OverflowException) {
447 failed = false;
449 if (failed)
450 return 12;
451 if (b != -128)
452 return -12;
454 try {
455 l = 255;
456 failed = true;
457 checked {
458 b = (sbyte)l;
460 } catch (OverflowException) {
461 failed = false;
463 if (failed)
464 return 13;
465 if (b != -128)
466 return -13;
468 try {
469 l = 0;
470 failed = false;
471 checked {
472 b = (sbyte)l;
474 } catch (OverflowException) {
475 failed = true;
477 if (failed)
478 return 14;
479 if (b != 0)
480 return -14;
482 try {
483 l = 256;
484 failed = true;
485 checked {
486 b = (sbyte)l;
488 } catch (OverflowException) {
489 failed = false;
491 if (failed)
492 return 15;
493 if (b != 0)
494 return -15;
496 try {
497 l = -129;
498 failed = true;
499 checked {
500 b = (sbyte)l;
502 } catch (OverflowException) {
503 failed = false;
505 if (failed)
506 return 16;
507 if (b != 0)
508 return -16;
510 try {
511 l = -1;
512 failed = false;
513 checked {
514 b = (sbyte)l;
516 } catch (OverflowException) {
517 failed = true;
519 if (failed)
520 return 17;
521 if (b != -1)
522 return -17;
524 try {
525 l = -128;
526 failed = false;
527 checked {
528 b = (sbyte)l;
530 } catch (OverflowException) {
531 failed = true;
533 if (failed)
534 return 18;
535 if (b != -128)
536 return -18;
538 try {
539 l = 127;
540 failed = false;
541 checked {
542 b = (sbyte)l;
544 } catch (OverflowException) {
545 failed = true;
547 if (failed)
548 return 19;
549 if (b != 127)
550 return -19;
552 try {
553 l = 128;
554 failed = true;
555 checked {
556 b = (sbyte)l;
558 } catch (OverflowException) {
559 failed = false;
561 if (failed)
562 return 20;
563 if (b != 127)
564 return -20;
566 try {
567 ulong ul = 128;
568 failed = true;
569 checked {
570 b = (sbyte)ul;
573 catch (OverflowException) {
574 failed = false;
576 if (failed)
577 return 21;
578 if (b != 127)
579 return -21;
581 return 0;
584 public static int test_0_ushort_cast () {
585 int a;
586 long l;
587 ulong ul;
588 ushort b;
589 bool failed;
591 try {
592 a = System.UInt16.MaxValue;
593 failed = false;
594 checked {
595 b = (ushort)a;
597 } catch (OverflowException) {
598 failed = true;
600 if (failed)
601 return 1;
603 try {
604 a = 0;
605 failed = false;
606 checked {
607 b = (ushort)a;
609 } catch (OverflowException) {
610 failed = true;
612 if (failed)
613 return 2;
615 try {
616 a = System.UInt16.MaxValue + 1;
617 failed = true;
618 checked {
619 b = (ushort)a;
621 } catch (OverflowException) {
622 failed = false;
624 if (failed)
625 return 3;
627 try {
628 a = -1;
629 failed = true;
630 checked {
631 b = (ushort)a;
633 } catch (OverflowException) {
634 failed = false;
636 if (failed)
637 return 4;
639 try {
640 double d = 0;
641 failed = false;
642 checked {
643 b = (ushort)d;
645 } catch (OverflowException) {
646 failed = true;
648 if (failed)
649 return 5;
651 try {
652 double d = System.UInt16.MaxValue;
653 failed = false;
654 checked {
655 b = (ushort)d;
657 } catch (OverflowException) {
658 failed = true;
660 if (failed)
661 return 6;
663 try {
664 double d = -1;
665 failed = true;
666 checked {
667 b = (ushort)d;
669 } catch (OverflowException) {
670 failed = false;
672 if (failed)
673 return 7;
675 try {
676 double d = System.UInt16.MaxValue + 1.0;
677 failed = true;
678 checked {
679 b = (ushort)d;
681 } catch (OverflowException) {
682 failed = false;
684 if (failed)
685 return 8;
687 try {
688 l = System.UInt16.MaxValue;
689 failed = false;
690 checked {
691 b = (ushort)l;
693 } catch (OverflowException) {
694 failed = true;
696 if (failed)
697 return 9;
699 try {
700 l = 0;
701 failed = false;
702 checked {
703 b = (ushort)l;
705 } catch (OverflowException) {
706 failed = true;
708 if (failed)
709 return 10;
711 try {
712 l = System.UInt16.MaxValue + 1;
713 failed = true;
714 checked {
715 b = (ushort)l;
717 } catch (OverflowException) {
718 failed = false;
720 if (failed)
721 return 11;
723 try {
724 l = -1;
725 failed = true;
726 checked {
727 b = (ushort)l;
729 } catch (OverflowException) {
730 failed = false;
732 if (failed)
733 return 12;
735 try {
736 ul = 0xfffff;
737 failed = true;
738 checked {
739 b = (ushort)ul;
741 } catch (OverflowException) {
742 failed = false;
744 if (failed)
745 return 13;
747 return 0;
750 public static int test_0_short_cast () {
751 int a;
752 long l;
753 short b;
754 bool failed;
756 try {
757 a = System.UInt16.MaxValue;
758 failed = true;
759 checked {
760 b = (short)a;
762 } catch (OverflowException) {
763 failed = false;
765 if (failed)
766 return 1;
768 try {
769 a = 0;
770 failed = false;
771 checked {
772 b = (short)a;
774 } catch (OverflowException) {
775 failed = true;
777 if (failed)
778 return 2;
780 try {
781 a = System.Int16.MaxValue + 1;
782 failed = true;
783 checked {
784 b = (short)a;
786 } catch (OverflowException) {
787 failed = false;
789 if (failed)
790 return 3;
792 try {
793 a = System.Int16.MinValue - 1;
794 failed = true;
795 checked {
796 b = (short)a;
798 } catch (OverflowException) {
799 failed = false;
801 if (failed)
802 return 4;
804 try {
805 a = -1;
806 failed = false;
807 checked {
808 b = (short)a;
810 } catch (OverflowException) {
811 failed = true;
813 if (failed)
814 return 5;
816 try {
817 a = System.Int16.MinValue;
818 failed = false;
819 checked {
820 b = (short)a;
822 } catch (OverflowException) {
823 failed = true;
825 if (failed)
826 return 6;
828 try {
829 a = System.Int16.MaxValue;
830 failed = false;
831 checked {
832 b = (short)a;
834 } catch (OverflowException) {
835 failed = true;
837 if (failed)
838 return 7;
840 try {
841 a = System.Int16.MaxValue + 1;
842 failed = true;
843 checked {
844 b = (short)a;
846 } catch (OverflowException) {
847 failed = false;
849 if (failed)
850 return 8;
852 try {
853 double d = System.Int16.MaxValue;
854 failed = false;
855 checked {
856 b = (short)d;
858 } catch (OverflowException) {
859 failed = true;
861 if (failed)
862 return 9;
864 try {
865 double d = System.Int16.MinValue;
866 failed = false;
867 checked {
868 b = (short)d;
870 } catch (OverflowException) {
871 failed = true;
873 if (failed)
874 return 10;
876 try {
877 double d = System.Int16.MaxValue + 1.0;
878 failed = true;
879 checked {
880 b = (short)d;
882 } catch (OverflowException) {
883 failed = false;
885 if (failed)
886 return 11;
888 try {
889 double d = System.Int16.MinValue - 1.0;
890 failed = true;
891 checked {
892 b = (short)d;
894 } catch (OverflowException) {
895 failed = false;
897 if (failed)
898 return 12;
900 try {
901 l = System.Int16.MaxValue + 1;
902 failed = true;
903 checked {
904 b = (short)l;
906 } catch (OverflowException) {
907 failed = false;
909 if (failed)
910 return 13;
912 try {
913 l = System.Int16.MaxValue;
914 failed = false;
915 checked {
916 b = (short)l;
918 } catch (OverflowException) {
919 failed = true;
921 if (failed)
922 return 14;
924 try {
925 l = System.Int16.MinValue - 1;
926 failed = true;
927 checked {
928 b = (short)l;
930 } catch (OverflowException) {
931 failed = false;
933 if (failed)
934 return 15;
937 try {
938 l = System.Int16.MinValue;
939 failed = false;
940 checked {
941 b = (short)l;
943 } catch (OverflowException) {
944 failed = true;
946 if (failed)
947 return 16;
949 try {
950 l = 0x00000000ffffffff;
951 failed = true;
952 checked {
953 b = (short)l;
955 } catch (OverflowException) {
956 failed = false;
958 if (failed)
959 return 17;
961 try {
962 ulong ul = 32768;
963 failed = true;
964 checked {
965 b = (short)ul;
967 } catch (OverflowException) {
968 failed = false;
970 if (failed)
971 return 18;
973 return 0;
976 public static int test_0_int_cast () {
977 int a;
978 long l;
979 bool failed;
981 try {
982 double d = System.Int32.MaxValue + 1.0;
983 failed = true;
984 checked {
985 a = (int)d;
987 } catch (OverflowException) {
988 failed = false;
990 if (failed)
991 return 1;
993 try {
994 double d = System.Int32.MaxValue;
995 failed = false;
996 checked {
997 a = (int)d;
999 } catch (OverflowException) {
1000 failed = true;
1002 if (failed)
1003 return 2;
1006 try {
1007 double d = System.Int32.MinValue;
1008 failed = false;
1009 checked {
1010 a = (int)d;
1012 } catch (OverflowException) {
1013 failed = true;
1015 if (failed)
1016 return 3;
1019 try {
1020 double d = System.Int32.MinValue - 1.0;
1021 failed = true;
1022 checked {
1023 a = (int)d;
1025 } catch (OverflowException) {
1026 failed = false;
1028 if (failed)
1029 return 4;
1031 try {
1032 l = System.Int32.MaxValue + (long)1;
1033 failed = true;
1034 checked {
1035 a = (int)l;
1037 } catch (OverflowException) {
1038 failed = false;
1040 if (failed)
1041 return 5;
1043 try {
1044 l = System.Int32.MaxValue;
1045 failed = false;
1046 checked {
1047 a = (int)l;
1049 } catch (OverflowException) {
1050 failed = true;
1052 if (failed)
1053 return 6;
1056 try {
1057 l = System.Int32.MinValue;
1058 failed = false;
1059 checked {
1060 a = (int)l;
1062 } catch (OverflowException) {
1063 failed = true;
1065 if (failed)
1066 return 7;
1069 try {
1070 l = System.Int32.MinValue - (long)1;
1071 failed = true;
1072 checked {
1073 a = (int)l;
1075 } catch (OverflowException) {
1076 failed = false;
1078 if (failed)
1079 return 8;
1081 try {
1082 uint ui = System.UInt32.MaxValue;
1083 failed = true;
1084 checked {
1085 a = (int)ui;
1088 catch (OverflowException) {
1089 failed = false;
1091 if (failed)
1092 return 9;
1094 try {
1095 ulong ul = (long)(System.Int32.MaxValue) + 1;
1096 failed = true;
1097 checked {
1098 a = (int)ul;
1101 catch (OverflowException) {
1102 failed = false;
1104 if (failed)
1105 return 10;
1107 try {
1108 ulong ul = UInt64.MaxValue;
1109 failed = true;
1110 checked {
1111 a = (int)ul;
1114 catch (OverflowException) {
1115 failed = false;
1117 if (failed)
1118 return 11;
1121 int i;
1122 float f = 1.1f;
1123 checked {
1124 i = (int) f;
1128 return 0;
1131 public static int test_0_uint_cast () {
1132 uint a;
1133 long l;
1134 bool failed;
1136 try {
1137 double d = System.UInt32.MaxValue;
1138 failed = false;
1139 checked {
1140 a = (uint)d;
1142 } catch (OverflowException) {
1143 failed = true;
1145 if (failed)
1146 return 1;
1148 try {
1149 double d = System.UInt32.MaxValue + 1.0;
1150 failed = true;
1151 checked {
1152 a = (uint)d;
1154 } catch (OverflowException) {
1155 failed = false;
1157 if (failed)
1158 return 2;
1160 try {
1161 double d = System.UInt32.MinValue;
1162 failed = false;
1163 checked {
1164 a = (uint)d;
1166 } catch (OverflowException) {
1167 failed = true;
1169 if (failed)
1170 return 3;
1172 try {
1173 double d = System.UInt32.MinValue - 1.0;
1174 failed = true;
1175 checked {
1176 a = (uint)d;
1178 } catch (OverflowException) {
1179 failed = false;
1181 if (failed)
1182 return 4;
1184 try {
1185 l = System.UInt32.MaxValue;
1186 failed = false;
1187 checked {
1188 a = (uint)l;
1190 } catch (OverflowException) {
1191 failed = true;
1193 if (failed)
1194 return 5;
1196 try {
1197 l = System.UInt32.MaxValue + (long)1;
1198 failed = true;
1199 checked {
1200 a = (uint)l;
1202 } catch (OverflowException) {
1203 failed = false;
1205 if (failed)
1206 return 6;
1208 try {
1209 l = System.UInt32.MinValue;
1210 failed = false;
1211 checked {
1212 a = (uint)l;
1214 } catch (OverflowException) {
1215 failed = true;
1217 if (failed)
1218 return 7;
1220 try {
1221 l = System.UInt32.MinValue - (long)1;
1222 failed = true;
1223 checked {
1224 a = (uint)l;
1226 } catch (OverflowException) {
1227 failed = false;
1229 if (failed)
1230 return 8;
1232 try {
1233 int i = -1;
1234 failed = true;
1235 checked {
1236 a = (uint)i;
1239 catch (OverflowException) {
1240 failed = false;
1242 if (failed)
1243 return 9;
1246 uint i;
1247 float f = 1.1f;
1248 checked {
1249 i = (uint) f;
1253 return 0;
1256 public static int test_0_long_cast () {
1259 * These tests depend on properties of x86 fp arithmetic so they won't work
1260 * on other platforms.
1263 long a;
1264 bool failed;
1266 try {
1267 double d = System.Int64.MaxValue - 512.0;
1268 failed = true;
1269 checked {
1270 a = (long)d;
1272 } catch (OverflowException) {
1273 failed = false;
1275 if (failed)
1276 return 1;
1278 try {
1279 double d = System.Int64.MaxValue - 513.0;
1280 failed = false;
1281 checked {
1282 a = (long)d;
1284 } catch (OverflowException) {
1285 failed = true;
1287 if (failed)
1288 return 2;
1290 try {
1291 double d = System.Int64.MinValue - 1024.0;
1292 failed = false;
1293 checked {
1294 a = (long)d;
1296 } catch (OverflowException) {
1297 failed = true;
1299 if (failed)
1300 return 3;
1302 try {
1303 double d = System.Int64.MinValue - 1025.0;
1304 failed = true;
1305 checked {
1306 a = (long)d;
1308 } catch (OverflowException) {
1309 failed = false;
1311 if (failed)
1312 return 4;
1316 long i;
1317 float f = 1.1f;
1318 checked {
1319 i = (long) f;
1323 return 0;
1326 public static int test_0_ulong_cast () {
1327 ulong a;
1328 bool failed;
1331 * These tests depend on properties of x86 fp arithmetic so they won't work
1332 * on other platforms.
1336 try {
1337 double d = System.UInt64.MaxValue - 1024.0;
1338 failed = true;
1339 checked {
1340 a = (ulong)d;
1342 } catch (OverflowException) {
1343 failed = false;
1345 if (failed)
1346 return 1;
1348 try {
1349 double d = System.UInt64.MaxValue - 1025.0;
1350 failed = false;
1351 checked {
1352 a = (ulong)d;
1354 } catch (OverflowException) {
1355 failed = true;
1357 if (failed)
1358 return 2;
1361 try {
1362 double d = 0;
1363 failed = false;
1364 checked {
1365 a = (ulong)d;
1367 } catch (OverflowException) {
1368 failed = true;
1370 if (failed)
1371 return 3;
1373 try {
1374 double d = -1;
1375 failed = true;
1376 checked {
1377 a = (ulong)d;
1379 } catch (OverflowException) {
1380 failed = false;
1382 if (failed)
1383 return 4;
1386 ulong i;
1387 float f = 1.1f;
1388 checked {
1389 i = (ulong) f;
1393 try {
1394 int i = -1;
1395 failed = true;
1396 checked {
1397 a = (ulong)i;
1400 catch (OverflowException) {
1401 failed = false;
1403 if (failed)
1404 return 5;
1406 try {
1407 int i = Int32.MinValue;
1408 failed = true;
1409 checked {
1410 a = (ulong)i;
1413 catch (OverflowException) {
1414 failed = false;
1416 if (failed)
1417 return 6;
1419 return 0;
1422 public static int test_0_simple_double_casts () {
1424 double d = 0xffffffff;
1426 if ((uint)d != 4294967295)
1427 return 1;
1430 * These tests depend on properties of x86 fp arithmetic so they won't work
1431 * on other platforms.
1434 d = 0xffffffffffffffff;
1436 if ((ulong)d != 0)
1437 return 2;
1439 if ((ushort)d != 0)
1440 return 3;
1442 if ((byte)d != 0)
1443 return 4;
1446 d = 0xffff;
1448 if ((ushort)d != 0xffff)
1449 return 5;
1451 if ((byte)d != 0xff)
1452 return 6;
1454 return 0;
1457 public static int test_0_div_zero () {
1458 int d = 1;
1459 int q = 0;
1460 int val;
1461 bool failed;
1463 try {
1464 failed = true;
1465 val = d / q;
1466 } catch (DivideByZeroException) {
1467 failed = false;
1469 if (failed)
1470 return 1;
1472 try {
1473 failed = true;
1474 val = d % q;
1475 } catch (DivideByZeroException) {
1476 failed = false;
1478 if (failed)
1479 return 2;
1481 try {
1482 failed = true;
1483 q = -1;
1484 d = Int32.MinValue;
1485 val = d / q;
1486 } catch (DivideByZeroException) {
1487 /* wrong exception */
1488 } catch (ArithmeticException) {
1489 failed = false;
1491 if (failed)
1492 return 3;
1494 try {
1495 failed = true;
1496 q = -1;
1497 d = Int32.MinValue;
1498 val = d % q;
1499 } catch (DivideByZeroException) {
1500 /* wrong exception */
1501 } catch (ArithmeticException) {
1502 failed = false;
1504 if (failed)
1505 return 4;
1507 return 0;
1510 public static int return_55 () {
1511 return 55;
1514 public static int test_0_cfold_div_zero () {
1515 // Test that constant folding doesn't cause division by zero exceptions
1516 if (return_55 () != return_55 ()) {
1517 int d = 1;
1518 int q = 0;
1519 int val;
1521 val = d / q;
1522 val = d % q;
1524 q = -1;
1525 d = Int32.MinValue;
1526 val = d / q;
1528 q = -1;
1529 val = d % q;
1532 return 0;
1535 public static int test_0_udiv_zero () {
1536 uint d = 1;
1537 uint q = 0;
1538 uint val;
1539 bool failed;
1541 try {
1542 failed = true;
1543 val = d / q;
1544 } catch (DivideByZeroException) {
1545 failed = false;
1547 if (failed)
1548 return 1;
1550 try {
1551 failed = true;
1552 val = d % q;
1553 } catch (DivideByZeroException) {
1554 failed = false;
1556 if (failed)
1557 return 2;
1559 return 0;
1562 public static int test_0_long_div_zero () {
1563 long d = 1;
1564 long q = 0;
1565 long val;
1566 bool failed;
1568 try {
1569 failed = true;
1570 val = d / q;
1571 } catch (DivideByZeroException) {
1572 failed = false;
1574 if (failed)
1575 return 1;
1577 try {
1578 failed = true;
1579 val = d % q;
1580 } catch (DivideByZeroException) {
1581 failed = false;
1583 if (failed)
1584 return 2;
1586 try {
1587 failed = true;
1588 q = -1;
1589 d = Int64.MinValue;
1590 val = d / q;
1591 } catch (DivideByZeroException) {
1592 /* wrong exception */
1593 } catch (ArithmeticException) {
1594 failed = false;
1596 if (failed)
1597 return 3;
1599 try {
1600 failed = true;
1601 q = -1;
1602 d = Int64.MinValue;
1603 val = d % q;
1604 } catch (DivideByZeroException) {
1605 /* wrong exception */
1606 } catch (ArithmeticException) {
1607 failed = false;
1609 if (failed)
1610 return 4;
1612 return 0;
1615 public static int test_0_ulong_div_zero () {
1616 ulong d = 1;
1617 ulong q = 0;
1618 ulong val;
1619 bool failed;
1621 try {
1622 failed = true;
1623 val = d / q;
1624 } catch (DivideByZeroException) {
1625 failed = false;
1627 if (failed)
1628 return 1;
1630 try {
1631 failed = true;
1632 val = d % q;
1633 } catch (DivideByZeroException) {
1634 failed = false;
1636 if (failed)
1637 return 2;
1639 return 0;
1642 public static int test_0_float_div_zero () {
1643 double d = 1;
1644 double q = 0;
1645 double val;
1646 bool failed;
1648 try {
1649 failed = false;
1650 val = d / q;
1651 } catch (DivideByZeroException) {
1652 failed = true;
1654 if (failed)
1655 return 1;
1657 try {
1658 failed = false;
1659 val = d % q;
1660 } catch (DivideByZeroException) {
1661 failed = true;
1663 if (failed)
1664 return 2;
1666 return 0;
1669 public static int test_0_invalid_unbox () {
1671 int i = 123;
1672 object o = "Some string";
1673 int res = 1;
1675 try {
1676 // Illegal conversion; o contains a string not an int
1677 i = (int) o;
1678 } catch (Exception e) {
1679 if (i ==123)
1680 res = 0;
1683 return res;
1686 // Test that double[] can't be cast to double (bug #46027)
1687 public static int test_0_invalid_unbox_arrays () {
1688 double[] d1 = { 1.0 };
1689 double[][] d2 = { d1 };
1690 Array a = d2;
1692 try {
1693 foreach (double d in a) {
1695 return 1;
1697 catch (InvalidCastException e) {
1698 return 0;
1702 /* bug# 42190, at least mcs generates a leave for the return that
1703 * jumps out of multiple exception clauses: we used to execute just
1704 * one enclosing finally block.
1706 public static int finally_level;
1707 static void do_something () {
1708 int a = 0;
1709 try {
1710 try {
1711 return;
1712 } finally {
1713 a = 1;
1715 } finally {
1716 finally_level++;
1720 public static int test_2_multiple_finally_clauses () {
1721 finally_level = 0;
1722 do_something ();
1723 if (finally_level == 1)
1724 return 2;
1725 return 0;
1728 public static int test_3_checked_cast_un () {
1729 ulong i = 0x8000000034000000;
1730 long j;
1732 try {
1733 checked { j = (long)i; }
1734 } catch (OverflowException) {
1735 j = 2;
1738 if (j != 2)
1739 return 0;
1740 return 3;
1743 public static int test_4_checked_cast () {
1744 long i;
1745 ulong j;
1747 unchecked { i = (long)0x8000000034000000;};
1748 try {
1749 checked { j = (ulong)i; }
1750 } catch (OverflowException) {
1751 j = 3;
1754 if (j != 3)
1755 return 0;
1756 return 4;
1759 static readonly int[] mul_dim_results = new int[] {
1760 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1761 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1762 2, 0, 2, 1, 2, 8,
1763 3, 0, 3, 1, 3, 8,
1764 4, 0, 4, 1, 4, 8,
1765 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1766 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1767 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1770 public static int test_0_multi_dim_array_access () {
1771 int [,] a = System.Array.CreateInstance (typeof (int),
1772 new int [] {3,6}, new int [] {2,2 }) as int[,];
1773 int x, y;
1774 int result_idx = 0;
1775 for (x = 0; x < 8; ++x) {
1776 for (y = 0; y < 9; ++y) {
1777 bool got_ex = false;
1778 try {
1779 a [x, y] = 1;
1780 } catch {
1781 got_ex = true;
1783 if (got_ex) {
1784 if (result_idx >= mul_dim_results.Length)
1785 return -1;
1786 if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1787 return result_idx + 1;
1789 result_idx += 2;
1793 if (result_idx == mul_dim_results.Length)
1794 return 0;
1795 return 200;
1798 static void helper_out_obj (out object o) {
1799 o = (object)"buddy";
1802 static void helper_out_string (out string o) {
1803 o = "buddy";
1806 public static int test_2_array_mismatch () {
1807 string[] a = { "hello", "world" };
1808 object[] b = a;
1809 bool passed = false;
1811 try {
1812 helper_out_obj (out b [1]);
1813 } catch (ArrayTypeMismatchException) {
1814 passed = true;
1816 if (!passed)
1817 return 0;
1818 helper_out_string (out a [1]);
1819 if (a [1] != "buddy")
1820 return 1;
1821 return 2;
1824 public static int test_0_ovf () {
1825 int ocount = 0;
1827 checked {
1829 ocount = 0;
1830 try {
1831 ulong a = UInt64.MaxValue - 1;
1832 ulong t = a++;
1833 } catch {
1834 ocount++;
1836 if (ocount != 0)
1837 return 1;
1839 ocount = 0;
1840 try {
1841 ulong a = UInt64.MaxValue;
1842 ulong t = a++;
1843 } catch {
1844 ocount++;
1846 if (ocount != 1)
1847 return 2;
1849 ocount = 0;
1850 try {
1851 long a = Int64.MaxValue - 1;
1852 long t = a++;
1853 } catch {
1854 ocount++;
1856 if (ocount != 0)
1857 return 3;
1859 try {
1860 long a = Int64.MaxValue;
1861 long t = a++;
1862 } catch {
1863 ocount++;
1865 if (ocount != 1)
1866 return 4;
1868 ocount = 0;
1869 try {
1870 ulong a = UInt64.MaxValue - 1;
1871 ulong t = a++;
1872 } catch {
1873 ocount++;
1875 if (ocount != 0)
1876 return 5;
1878 try {
1879 ulong a = UInt64.MaxValue;
1880 ulong t = a++;
1881 } catch {
1882 ocount++;
1884 if (ocount != 1)
1885 return 6;
1887 ocount = 0;
1888 try {
1889 long a = Int64.MinValue + 1;
1890 long t = a--;
1891 } catch {
1892 ocount++;
1894 if (ocount != 0)
1895 return 7;
1897 ocount = 0;
1898 try {
1899 long a = Int64.MinValue;
1900 long t = a--;
1901 } catch {
1902 ocount++;
1904 if (ocount != 1)
1905 return 8;
1907 ocount = 0;
1908 try {
1909 ulong a = UInt64.MinValue + 1;
1910 ulong t = a--;
1911 } catch {
1912 ocount++;
1914 if (ocount != 0)
1915 return 9;
1917 ocount = 0;
1918 try {
1919 ulong a = UInt64.MinValue;
1920 ulong t = a--;
1921 } catch {
1922 ocount++;
1924 if (ocount != 1)
1925 return 10;
1927 ocount = 0;
1928 try {
1929 int a = Int32.MinValue + 1;
1930 int t = a--;
1931 } catch {
1932 ocount++;
1934 if (ocount != 0)
1935 return 11;
1937 ocount = 0;
1938 try {
1939 int a = Int32.MinValue;
1940 int t = a--;
1941 } catch {
1942 ocount++;
1944 if (ocount != 1)
1945 return 12;
1947 ocount = 0;
1948 try {
1949 uint a = 1;
1950 uint t = a--;
1951 } catch {
1952 ocount++;
1954 if (ocount != 0)
1955 return 13;
1957 ocount = 0;
1958 try {
1959 uint a = 0;
1960 uint t = a--;
1961 } catch {
1962 ocount++;
1964 if (ocount != 1)
1965 return 14;
1967 ocount = 0;
1968 try {
1969 sbyte a = 126;
1970 sbyte t = a++;
1971 } catch {
1972 ocount++;
1974 if (ocount != 0)
1975 return 15;
1977 ocount = 0;
1978 try {
1979 sbyte a = 127;
1980 sbyte t = a++;
1981 } catch {
1982 ocount++;
1984 if (ocount != 1)
1985 return 16;
1987 ocount = 0;
1988 try {
1989 } catch {
1990 ocount++;
1992 if (ocount != 0)
1993 return 17;
1995 ocount = 0;
1996 try {
1997 int a = 1 << 29;
1998 int t = a*2;
1999 } catch {
2000 ocount++;
2002 if (ocount != 0)
2003 return 18;
2005 ocount = 0;
2006 try {
2007 int a = 1 << 30;
2008 int t = a*2;
2009 } catch {
2010 ocount++;
2012 if (ocount != 1)
2013 return 19;
2015 ocount = 0;
2016 try {
2017 ulong a = 0xffffffffff;
2018 ulong t = a*0x0ffffff;
2019 } catch {
2020 ocount++;
2022 if (ocount != 0)
2023 return 20;
2025 ocount = 0;
2026 try {
2027 ulong a = 0xffffffffff;
2028 ulong t = a*0x0fffffff;
2029 } catch {
2030 ocount++;
2032 if (ocount != 1)
2033 return 21;
2035 ocount = 0;
2036 try {
2037 long a = Int64.MinValue;
2038 long b = 10;
2039 long v = a * b;
2040 } catch {
2041 ocount ++;
2043 if (ocount != 1)
2044 return 22;
2046 ocount = 0;
2047 try {
2048 long a = 10;
2049 long b = Int64.MinValue;
2050 long v = a * b;
2051 } catch {
2052 ocount ++;
2054 if (ocount != 1)
2055 return 23;
2058 return 0;
2061 class Broken {
2062 public static int i;
2064 static Broken () {
2065 throw new Exception ("Ugh!");
2068 public static int DoSomething () {
2069 return i;
2073 public static int test_0_exception_in_cctor () {
2074 try {
2075 Broken.DoSomething ();
2077 catch (TypeInitializationException) {
2078 // This will only happen once even if --regression is used
2080 return 0;
2083 public static int test_5_regalloc () {
2084 int i = 0;
2086 try {
2087 for (i = 0; i < 10; ++i) {
2088 if (i == 5)
2089 throw new Exception ();
2092 catch (Exception) {
2093 if (i != 5)
2094 return i;
2097 // Check that variables written in catch clauses are volatile
2098 int j = 0;
2099 try {
2100 throw new Exception ();
2102 catch (Exception) {
2103 j = 5;
2105 if (j != 5)
2106 return 6;
2108 int k = 0;
2109 try {
2110 try {
2111 throw new Exception ();
2113 finally {
2114 k = 5;
2117 catch (Exception) {
2119 if (k != 5)
2120 return 7;
2122 return i;
2125 /* MarshalByRefObject prevents the methods from being inlined */
2126 class ThrowClass : MarshalByRefObject {
2127 public static void rethrow1 () {
2128 throw new Exception ();
2131 public static void rethrow2 () {
2132 rethrow1 ();
2136 public static int test_0_rethrow_stacktrace () {
2137 // Check that rethrowing an exception preserves the original stack trace
2138 try {
2139 try {
2140 ThrowClass.rethrow2 ();
2142 catch (Exception ex) {
2143 // Check that each catch clause has its own exception variable
2144 // If not, the throw below will overwrite the exception used
2145 // by the rethrow
2146 try {
2147 throw new DivideByZeroException ();
2149 catch (Exception foo) {
2152 throw;
2155 catch (Exception ex) {
2156 if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2157 return 0;
2160 return 1;
2163 interface IFace {}
2164 class Face : IFace {}
2166 public static int test_1_array_mismatch_2 () {
2167 try {
2168 object [] o = new Face [1];
2169 o [0] = 1;
2170 return 0;
2171 } catch (ArrayTypeMismatchException) {
2172 return 1;
2176 public static int test_1_array_mismatch_3 () {
2177 try {
2178 object [] o = new IFace [1];
2179 o [0] = 1;
2180 return 0;
2181 } catch (ArrayTypeMismatchException) {
2182 return 1;
2186 public static int test_1_array_mismatch_4 () {
2187 try {
2188 object [][] o = new Face [5] [];
2189 o [0] = new object [5];
2191 return 0;
2192 } catch (ArrayTypeMismatchException) {
2193 return 1;
2197 public static int test_0_array_size () {
2198 bool failed;
2200 try {
2201 failed = true;
2202 int[] mem1 = new int [Int32.MaxValue];
2204 catch (OutOfMemoryException e) {
2205 failed = false;
2207 if (failed)
2208 return 1;
2210 try {
2211 failed = true;
2212 int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2214 catch (OutOfMemoryException e) {
2215 failed = false;
2217 if (failed)
2218 return 2;
2220 return 0;
2223 struct S {
2224 int i, j, k, l, m, n;
2227 static IntPtr[] addr;
2229 static unsafe void throw_func (int i, S s) {
2230 addr [i] = new IntPtr (&i);
2231 throw new Exception ();
2234 /* Test that arguments are correctly popped off the stack during unwinding */
2235 public static int test_0_stack_unwind () {
2236 addr = new IntPtr [1000];
2237 S s = new S ();
2238 for (int j = 0; j < 1000; j++) {
2239 try {
2240 throw_func (j, s);
2242 catch (Exception) {
2245 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2248 public static int test_0_regress_73242 () {
2249 int [] arr = new int [10];
2250 for (int i = 0; i < 10; ++i)
2251 arr [i] = 0;
2252 try {
2253 throw new Exception ();
2255 catch {
2257 return 0;
2260 public static int test_0_nullref () {
2261 try {
2262 Array foo = null;
2263 foo.Clone();
2264 } catch (NullReferenceException e) {
2265 return 0;
2267 return 1;
2270 // bug #78633
2271 public static int test_0_throw_to_branch_opt_outer_clause () {
2272 int i = 0;
2274 try {
2275 try {
2276 string [] files = new string[1];
2278 string s = files[2];
2279 } finally {
2280 i ++;
2282 } catch {
2285 return (i == 1) ? 0 : 1;