Fix LLVM build.
[mono-project/dkf.git] / mono / mini / exceptions.cs
blob6affc7d25bfdfe8cd940081a9ce9d83e1e9d5fc8
1 using System;
2 using System.Reflection;
3 using System.Runtime.CompilerServices;
5 /*
6 * Regression tests for the mono JIT.
8 * Each test needs to be of the form:
10 * public static int test_<result>_<name> ();
12 * where <result> is an integer (the value that needs to be returned by
13 * the method to make it pass.
14 * <name> is a user-displayed name used to identify the test.
16 * The tests can be driven in two ways:
17 * *) running the program directly: Main() uses reflection to find and invoke
18 * the test methods (this is useful mostly to check that the tests are correct)
19 * *) with the --regression switch of the jit (this is the preferred way since
20 * all the tests will be run with optimizations on and off)
22 * The reflection logic could be moved to a .dll since we need at least another
23 * regression test file written in IL code to have better control on how
24 * the IL code looks.
27 class Tests {
29 public static int Main () {
30 return TestDriver.RunTests (typeof (Tests));
33 public static int test_0_catch () {
34 Exception x = new Exception ();
36 try {
37 throw x;
38 } catch (Exception e) {
39 if (e == x)
40 return 0;
42 return 1;
45 public static int test_0_finally_without_exc () {
46 int x;
48 try {
49 x = 1;
50 } catch (Exception e) {
51 x = 2;
52 } finally {
53 x = 0;
56 return x;
59 public static int test_0_finally () {
60 int x = 1;
62 try {
63 throw new Exception ();
64 } catch (Exception e) {
65 x = 2;
66 } finally {
67 x = 0;
69 return x;
72 public static int test_0_nested_finally () {
73 int a;
75 try {
76 a = 1;
77 } finally {
78 try {
79 a = 2;
80 } finally {
81 a = 0;
84 return a;
87 public static int test_0_byte_cast () {
88 int a;
89 long l;
90 ulong ul;
91 byte b = 0;
92 bool failed;
94 try {
95 a = 255;
96 failed = false;
97 checked {
98 b = (byte)a;
100 } catch (OverflowException) {
101 failed = true;
103 if (failed)
104 return 1;
105 if (b != 255)
106 return -1;
108 try {
109 a = 0;
110 failed = false;
111 checked {
112 b = (byte)a;
114 } catch (OverflowException) {
115 failed = true;
117 if (failed)
118 return 2;
119 if (b != 0)
120 return -2;
123 try {
124 a = 256;
125 failed = true;
126 checked {
127 b = (byte)a;
129 } catch (OverflowException) {
130 failed = false;
132 if (failed)
133 return 3;
134 if (b != 0)
135 return -3;
137 try {
138 a = -1;
139 failed = true;
140 checked {
141 b = (byte)a;
143 } catch (OverflowException) {
144 failed = false;
146 if (failed)
147 return 4;
148 if (b != 0)
149 return -4;
151 try {
152 double d = 0;
153 failed = false;
154 checked {
155 b = (byte)d;
157 } catch (OverflowException) {
158 failed = true;
160 if (failed)
161 return 5;
162 if (b != 0)
163 return -5;
165 try {
166 double d = -1;
167 failed = true;
168 checked {
169 b = (byte)d;
171 } catch (OverflowException) {
172 failed = false;
174 if (failed)
175 return 6;
176 if (b != 0)
177 return -6;
179 try {
180 double d = 255;
181 failed = false;
182 checked {
183 b = (byte)d;
185 } catch (OverflowException) {
186 failed = true;
188 if (failed)
189 return 7;
190 if (b != 255)
191 return -7;
193 try {
194 double d = 256;
195 failed = true;
196 checked {
197 b = (byte)d;
199 } catch (OverflowException) {
200 failed = false;
202 if (failed)
203 return 8;
204 if (b != 255)
205 return -8;
207 try {
208 l = 255;
209 failed = false;
210 checked {
211 b = (byte)l;
213 } catch (OverflowException) {
214 failed = true;
216 if (failed)
217 return 9;
218 if (b != 255)
219 return -9;
221 try {
222 l = 0;
223 failed = false;
224 checked {
225 b = (byte)l;
227 } catch (OverflowException) {
228 failed = true;
230 if (failed)
231 return 10;
232 if (b != 0)
233 return -10;
235 try {
236 l = 256;
237 failed = true;
238 checked {
239 b = (byte)l;
241 } catch (OverflowException) {
242 failed = false;
244 if (failed)
245 return 11;
246 if (b != 0)
247 return -11;
249 try {
250 l = -1;
251 failed = true;
252 checked {
253 b = (byte)l;
255 } catch (OverflowException) {
256 failed = false;
258 if (failed)
259 return 12;
260 if (b != 0)
261 return -12;
263 try {
264 ul = 256;
265 failed = true;
266 checked {
267 b = (byte)ul;
270 catch (OverflowException) {
271 failed = false;
273 if (failed)
274 return 13;
275 if (b != 0)
276 return -13;
278 return 0;
281 public static int test_0_sbyte_cast () {
282 int a;
283 long l;
284 sbyte b = 0;
285 bool failed;
287 try {
288 a = 255;
289 failed = true;
290 checked {
291 b = (sbyte)a;
293 } catch (OverflowException) {
294 failed = false;
296 if (failed)
297 return 1;
298 if (b != 0)
299 return -1;
301 try {
302 a = 0;
303 failed = false;
304 checked {
305 b = (sbyte)a;
307 } catch (OverflowException) {
308 failed = true;
310 if (failed)
311 return 2;
312 if (b != 0)
313 return -2;
315 try {
316 a = 256;
317 failed = true;
318 checked {
319 b = (sbyte)a;
321 } catch (OverflowException) {
322 failed = false;
324 if (failed)
325 return 3;
326 if (b != 0)
327 return -3;
329 try {
330 a = -129;
331 failed = true;
332 checked {
333 b = (sbyte)a;
335 } catch (OverflowException) {
336 failed = false;
338 if (failed)
339 return 4;
340 if (b != 0)
341 return -4;
343 try {
344 a = -1;
345 failed = false;
346 checked {
347 b = (sbyte)a;
349 } catch (OverflowException) {
350 failed = true;
352 if (failed)
353 return 5;
354 if (b != -1)
355 return -5;
357 try {
358 a = -128;
359 failed = false;
360 checked {
361 b = (sbyte)a;
363 } catch (OverflowException) {
364 failed = true;
366 if (failed)
367 return 6;
368 if (b != -128)
369 return -6;
371 try {
372 a = 127;
373 failed = false;
374 checked {
375 b = (sbyte)a;
377 } catch (OverflowException) {
378 failed = true;
380 if (failed)
381 return 7;
382 if (b != 127)
383 return -7;
385 try {
386 a = 128;
387 failed = true;
388 checked {
389 b = (sbyte)a;
391 } catch (OverflowException) {
392 failed = false;
394 if (failed)
395 return 8;
396 if (b != 127)
397 return -8;
399 try {
400 double d = 127;
401 failed = false;
402 checked {
403 b = (sbyte)d;
405 } catch (OverflowException) {
406 failed = true;
408 if (failed)
409 return 9;
410 if (b != 127)
411 return -9;
413 try {
414 double d = -128;
415 failed = false;
416 checked {
417 b = (sbyte)d;
419 } catch (OverflowException) {
420 failed = true;
422 if (failed)
423 return 10;
424 if (b != -128)
425 return -10;
427 try {
428 double d = 128;
429 failed = true;
430 checked {
431 b = (sbyte)d;
433 } catch (OverflowException) {
434 failed = false;
436 if (failed)
437 return 11;
438 if (b != -128)
439 return -11;
441 try {
442 double d = -129;
443 failed = true;
444 checked {
445 b = (sbyte)d;
447 } catch (OverflowException) {
448 failed = false;
450 if (failed)
451 return 12;
452 if (b != -128)
453 return -12;
455 try {
456 l = 255;
457 failed = true;
458 checked {
459 b = (sbyte)l;
461 } catch (OverflowException) {
462 failed = false;
464 if (failed)
465 return 13;
466 if (b != -128)
467 return -13;
469 try {
470 l = 0;
471 failed = false;
472 checked {
473 b = (sbyte)l;
475 } catch (OverflowException) {
476 failed = true;
478 if (failed)
479 return 14;
480 if (b != 0)
481 return -14;
483 try {
484 l = 256;
485 failed = true;
486 checked {
487 b = (sbyte)l;
489 } catch (OverflowException) {
490 failed = false;
492 if (failed)
493 return 15;
494 if (b != 0)
495 return -15;
497 try {
498 l = -129;
499 failed = true;
500 checked {
501 b = (sbyte)l;
503 } catch (OverflowException) {
504 failed = false;
506 if (failed)
507 return 16;
508 if (b != 0)
509 return -16;
511 try {
512 l = -1;
513 failed = false;
514 checked {
515 b = (sbyte)l;
517 } catch (OverflowException) {
518 failed = true;
520 if (failed)
521 return 17;
522 if (b != -1)
523 return -17;
525 try {
526 l = -128;
527 failed = false;
528 checked {
529 b = (sbyte)l;
531 } catch (OverflowException) {
532 failed = true;
534 if (failed)
535 return 18;
536 if (b != -128)
537 return -18;
539 try {
540 l = 127;
541 failed = false;
542 checked {
543 b = (sbyte)l;
545 } catch (OverflowException) {
546 failed = true;
548 if (failed)
549 return 19;
550 if (b != 127)
551 return -19;
553 try {
554 l = 128;
555 failed = true;
556 checked {
557 b = (sbyte)l;
559 } catch (OverflowException) {
560 failed = false;
562 if (failed)
563 return 20;
564 if (b != 127)
565 return -20;
567 try {
568 ulong ul = 128;
569 failed = true;
570 checked {
571 b = (sbyte)ul;
574 catch (OverflowException) {
575 failed = false;
577 if (failed)
578 return 21;
579 if (b != 127)
580 return -21;
582 return 0;
585 public static int test_0_ushort_cast () {
586 int a;
587 long l;
588 ulong ul;
589 ushort b;
590 bool failed;
592 try {
593 a = System.UInt16.MaxValue;
594 failed = false;
595 checked {
596 b = (ushort)a;
598 } catch (OverflowException) {
599 failed = true;
601 if (failed)
602 return 1;
604 try {
605 a = 0;
606 failed = false;
607 checked {
608 b = (ushort)a;
610 } catch (OverflowException) {
611 failed = true;
613 if (failed)
614 return 2;
616 try {
617 a = System.UInt16.MaxValue + 1;
618 failed = true;
619 checked {
620 b = (ushort)a;
622 } catch (OverflowException) {
623 failed = false;
625 if (failed)
626 return 3;
628 try {
629 a = -1;
630 failed = true;
631 checked {
632 b = (ushort)a;
634 } catch (OverflowException) {
635 failed = false;
637 if (failed)
638 return 4;
640 try {
641 double d = 0;
642 failed = false;
643 checked {
644 b = (ushort)d;
646 } catch (OverflowException) {
647 failed = true;
649 if (failed)
650 return 5;
652 try {
653 double d = System.UInt16.MaxValue;
654 failed = false;
655 checked {
656 b = (ushort)d;
658 } catch (OverflowException) {
659 failed = true;
661 if (failed)
662 return 6;
664 try {
665 double d = -1;
666 failed = true;
667 checked {
668 b = (ushort)d;
670 } catch (OverflowException) {
671 failed = false;
673 if (failed)
674 return 7;
676 try {
677 double d = System.UInt16.MaxValue + 1.0;
678 failed = true;
679 checked {
680 b = (ushort)d;
682 } catch (OverflowException) {
683 failed = false;
685 if (failed)
686 return 8;
688 try {
689 l = System.UInt16.MaxValue;
690 failed = false;
691 checked {
692 b = (ushort)l;
694 } catch (OverflowException) {
695 failed = true;
697 if (failed)
698 return 9;
700 try {
701 l = 0;
702 failed = false;
703 checked {
704 b = (ushort)l;
706 } catch (OverflowException) {
707 failed = true;
709 if (failed)
710 return 10;
712 try {
713 l = System.UInt16.MaxValue + 1;
714 failed = true;
715 checked {
716 b = (ushort)l;
718 } catch (OverflowException) {
719 failed = false;
721 if (failed)
722 return 11;
724 try {
725 l = -1;
726 failed = true;
727 checked {
728 b = (ushort)l;
730 } catch (OverflowException) {
731 failed = false;
733 if (failed)
734 return 12;
736 try {
737 ul = 0xfffff;
738 failed = true;
739 checked {
740 b = (ushort)ul;
742 } catch (OverflowException) {
743 failed = false;
745 if (failed)
746 return 13;
748 return 0;
751 public static int test_0_short_cast () {
752 int a;
753 long l;
754 short b;
755 bool failed;
757 try {
758 a = System.UInt16.MaxValue;
759 failed = true;
760 checked {
761 b = (short)a;
763 } catch (OverflowException) {
764 failed = false;
766 if (failed)
767 return 1;
769 try {
770 a = 0;
771 failed = false;
772 checked {
773 b = (short)a;
775 } catch (OverflowException) {
776 failed = true;
778 if (failed)
779 return 2;
781 try {
782 a = System.Int16.MaxValue + 1;
783 failed = true;
784 checked {
785 b = (short)a;
787 } catch (OverflowException) {
788 failed = false;
790 if (failed)
791 return 3;
793 try {
794 a = System.Int16.MinValue - 1;
795 failed = true;
796 checked {
797 b = (short)a;
799 } catch (OverflowException) {
800 failed = false;
802 if (failed)
803 return 4;
805 try {
806 a = -1;
807 failed = false;
808 checked {
809 b = (short)a;
811 } catch (OverflowException) {
812 failed = true;
814 if (failed)
815 return 5;
817 try {
818 a = System.Int16.MinValue;
819 failed = false;
820 checked {
821 b = (short)a;
823 } catch (OverflowException) {
824 failed = true;
826 if (failed)
827 return 6;
829 try {
830 a = System.Int16.MaxValue;
831 failed = false;
832 checked {
833 b = (short)a;
835 } catch (OverflowException) {
836 failed = true;
838 if (failed)
839 return 7;
841 try {
842 a = System.Int16.MaxValue + 1;
843 failed = true;
844 checked {
845 b = (short)a;
847 } catch (OverflowException) {
848 failed = false;
850 if (failed)
851 return 8;
853 try {
854 double d = System.Int16.MaxValue;
855 failed = false;
856 checked {
857 b = (short)d;
859 } catch (OverflowException) {
860 failed = true;
862 if (failed)
863 return 9;
865 try {
866 double d = System.Int16.MinValue;
867 failed = false;
868 checked {
869 b = (short)d;
871 } catch (OverflowException) {
872 failed = true;
874 if (failed)
875 return 10;
877 try {
878 double d = System.Int16.MaxValue + 1.0;
879 failed = true;
880 checked {
881 b = (short)d;
883 } catch (OverflowException) {
884 failed = false;
886 if (failed)
887 return 11;
889 try {
890 double d = System.Int16.MinValue - 1.0;
891 failed = true;
892 checked {
893 b = (short)d;
895 } catch (OverflowException) {
896 failed = false;
898 if (failed)
899 return 12;
901 try {
902 l = System.Int16.MaxValue + 1;
903 failed = true;
904 checked {
905 b = (short)l;
907 } catch (OverflowException) {
908 failed = false;
910 if (failed)
911 return 13;
913 try {
914 l = System.Int16.MaxValue;
915 failed = false;
916 checked {
917 b = (short)l;
919 } catch (OverflowException) {
920 failed = true;
922 if (failed)
923 return 14;
925 try {
926 l = System.Int16.MinValue - 1;
927 failed = true;
928 checked {
929 b = (short)l;
931 } catch (OverflowException) {
932 failed = false;
934 if (failed)
935 return 15;
938 try {
939 l = System.Int16.MinValue;
940 failed = false;
941 checked {
942 b = (short)l;
944 } catch (OverflowException) {
945 failed = true;
947 if (failed)
948 return 16;
950 try {
951 l = 0x00000000ffffffff;
952 failed = true;
953 checked {
954 b = (short)l;
956 } catch (OverflowException) {
957 failed = false;
959 if (failed)
960 return 17;
962 try {
963 ulong ul = 32768;
964 failed = true;
965 checked {
966 b = (short)ul;
968 } catch (OverflowException) {
969 failed = false;
971 if (failed)
972 return 18;
974 return 0;
977 public static int test_0_int_cast () {
978 int a;
979 long l;
980 bool failed;
982 try {
983 double d = System.Int32.MaxValue + 1.0;
984 failed = true;
985 checked {
986 a = (int)d;
988 } catch (OverflowException) {
989 failed = false;
991 if (failed)
992 return 1;
994 try {
995 double d = System.Int32.MaxValue;
996 failed = false;
997 checked {
998 a = (int)d;
1000 } catch (OverflowException) {
1001 failed = true;
1003 if (failed)
1004 return 2;
1007 try {
1008 double d = System.Int32.MinValue;
1009 failed = false;
1010 checked {
1011 a = (int)d;
1013 } catch (OverflowException) {
1014 failed = true;
1016 if (failed)
1017 return 3;
1020 try {
1021 double d = System.Int32.MinValue - 1.0;
1022 failed = true;
1023 checked {
1024 a = (int)d;
1026 } catch (OverflowException) {
1027 failed = false;
1029 if (failed)
1030 return 4;
1032 try {
1033 l = System.Int32.MaxValue + (long)1;
1034 failed = true;
1035 checked {
1036 a = (int)l;
1038 } catch (OverflowException) {
1039 failed = false;
1041 if (failed)
1042 return 5;
1044 try {
1045 l = System.Int32.MaxValue;
1046 failed = false;
1047 checked {
1048 a = (int)l;
1050 } catch (OverflowException) {
1051 failed = true;
1053 if (failed)
1054 return 6;
1057 try {
1058 l = System.Int32.MinValue;
1059 failed = false;
1060 checked {
1061 a = (int)l;
1063 } catch (OverflowException) {
1064 failed = true;
1066 if (failed)
1067 return 7;
1070 try {
1071 l = System.Int32.MinValue - (long)1;
1072 failed = true;
1073 checked {
1074 a = (int)l;
1076 } catch (OverflowException) {
1077 failed = false;
1079 if (failed)
1080 return 8;
1082 try {
1083 uint ui = System.UInt32.MaxValue;
1084 failed = true;
1085 checked {
1086 a = (int)ui;
1089 catch (OverflowException) {
1090 failed = false;
1092 if (failed)
1093 return 9;
1095 try {
1096 ulong ul = (long)(System.Int32.MaxValue) + 1;
1097 failed = true;
1098 checked {
1099 a = (int)ul;
1102 catch (OverflowException) {
1103 failed = false;
1105 if (failed)
1106 return 10;
1108 try {
1109 ulong ul = UInt64.MaxValue;
1110 failed = true;
1111 checked {
1112 a = (int)ul;
1115 catch (OverflowException) {
1116 failed = false;
1118 if (failed)
1119 return 11;
1122 int i;
1123 float f = 1.1f;
1124 checked {
1125 i = (int) f;
1129 return 0;
1132 public static int test_0_uint_cast () {
1133 uint a;
1134 long l;
1135 bool failed;
1137 try {
1138 double d = System.UInt32.MaxValue;
1139 failed = false;
1140 checked {
1141 a = (uint)d;
1143 } catch (OverflowException) {
1144 failed = true;
1146 if (failed)
1147 return 1;
1149 try {
1150 double d = System.UInt32.MaxValue + 1.0;
1151 failed = true;
1152 checked {
1153 a = (uint)d;
1155 } catch (OverflowException) {
1156 failed = false;
1158 if (failed)
1159 return 2;
1161 try {
1162 double d = System.UInt32.MinValue;
1163 failed = false;
1164 checked {
1165 a = (uint)d;
1167 } catch (OverflowException) {
1168 failed = true;
1170 if (failed)
1171 return 3;
1173 try {
1174 double d = System.UInt32.MinValue - 1.0;
1175 failed = true;
1176 checked {
1177 a = (uint)d;
1179 } catch (OverflowException) {
1180 failed = false;
1182 if (failed)
1183 return 4;
1185 try {
1186 l = System.UInt32.MaxValue;
1187 failed = false;
1188 checked {
1189 a = (uint)l;
1191 } catch (OverflowException) {
1192 failed = true;
1194 if (failed)
1195 return 5;
1197 try {
1198 l = System.UInt32.MaxValue + (long)1;
1199 failed = true;
1200 checked {
1201 a = (uint)l;
1203 } catch (OverflowException) {
1204 failed = false;
1206 if (failed)
1207 return 6;
1209 try {
1210 l = System.UInt32.MinValue;
1211 failed = false;
1212 checked {
1213 a = (uint)l;
1215 } catch (OverflowException) {
1216 failed = true;
1218 if (failed)
1219 return 7;
1221 try {
1222 l = System.UInt32.MinValue - (long)1;
1223 failed = true;
1224 checked {
1225 a = (uint)l;
1227 } catch (OverflowException) {
1228 failed = false;
1230 if (failed)
1231 return 8;
1233 try {
1234 int i = -1;
1235 failed = true;
1236 checked {
1237 a = (uint)i;
1240 catch (OverflowException) {
1241 failed = false;
1243 if (failed)
1244 return 9;
1247 uint i;
1248 float f = 1.1f;
1249 checked {
1250 i = (uint) f;
1254 return 0;
1257 public static int test_0_long_cast () {
1260 * These tests depend on properties of x86 fp arithmetic so they won't work
1261 * on other platforms.
1264 long a;
1265 bool failed;
1267 try {
1268 double d = System.Int64.MaxValue - 512.0;
1269 failed = true;
1270 checked {
1271 a = (long)d;
1273 } catch (OverflowException) {
1274 failed = false;
1276 if (failed)
1277 return 1;
1279 try {
1280 double d = System.Int64.MaxValue - 513.0;
1281 failed = false;
1282 checked {
1283 a = (long)d;
1285 } catch (OverflowException) {
1286 failed = true;
1288 if (failed)
1289 return 2;
1291 try {
1292 double d = System.Int64.MinValue - 1024.0;
1293 failed = false;
1294 checked {
1295 a = (long)d;
1297 } catch (OverflowException) {
1298 failed = true;
1300 if (failed)
1301 return 3;
1303 try {
1304 double d = System.Int64.MinValue - 1025.0;
1305 failed = true;
1306 checked {
1307 a = (long)d;
1309 } catch (OverflowException) {
1310 failed = false;
1312 if (failed)
1313 return 4;
1317 long i;
1318 float f = 1.1f;
1319 checked {
1320 i = (long) f;
1324 return 0;
1327 public static int test_0_ulong_cast () {
1328 ulong a;
1329 bool failed;
1332 * These tests depend on properties of x86 fp arithmetic so they won't work
1333 * on other platforms.
1337 try {
1338 double d = System.UInt64.MaxValue - 1024.0;
1339 failed = true;
1340 checked {
1341 a = (ulong)d;
1343 } catch (OverflowException) {
1344 failed = false;
1346 if (failed)
1347 return 1;
1349 try {
1350 double d = System.UInt64.MaxValue - 1025.0;
1351 failed = false;
1352 checked {
1353 a = (ulong)d;
1355 } catch (OverflowException) {
1356 failed = true;
1358 if (failed)
1359 return 2;
1362 try {
1363 double d = 0;
1364 failed = false;
1365 checked {
1366 a = (ulong)d;
1368 } catch (OverflowException) {
1369 failed = true;
1371 if (failed)
1372 return 3;
1374 try {
1375 double d = -1;
1376 failed = true;
1377 checked {
1378 a = (ulong)d;
1380 } catch (OverflowException) {
1381 failed = false;
1383 if (failed)
1384 return 4;
1387 ulong i;
1388 float f = 1.1f;
1389 checked {
1390 i = (ulong) f;
1394 try {
1395 int i = -1;
1396 failed = true;
1397 checked {
1398 a = (ulong)i;
1401 catch (OverflowException) {
1402 failed = false;
1404 if (failed)
1405 return 5;
1407 try {
1408 int i = Int32.MinValue;
1409 failed = true;
1410 checked {
1411 a = (ulong)i;
1414 catch (OverflowException) {
1415 failed = false;
1417 if (failed)
1418 return 6;
1420 return 0;
1423 public static int test_0_simple_double_casts () {
1425 double d = 0xffffffff;
1427 if ((uint)d != 4294967295)
1428 return 1;
1431 * These tests depend on properties of x86 fp arithmetic so they won't work
1432 * on other platforms.
1435 d = 0xffffffffffffffff;
1437 if ((ulong)d != 0)
1438 return 2;
1440 if ((ushort)d != 0)
1441 return 3;
1443 if ((byte)d != 0)
1444 return 4;
1447 d = 0xffff;
1449 if ((ushort)d != 0xffff)
1450 return 5;
1452 if ((byte)d != 0xff)
1453 return 6;
1455 return 0;
1458 public static int test_0_div_zero () {
1459 int d = 1;
1460 int q = 0;
1461 int val;
1462 bool failed;
1464 try {
1465 failed = true;
1466 val = d / q;
1467 } catch (DivideByZeroException) {
1468 failed = false;
1470 if (failed)
1471 return 1;
1473 try {
1474 failed = true;
1475 val = d % q;
1476 } catch (DivideByZeroException) {
1477 failed = false;
1479 if (failed)
1480 return 2;
1482 try {
1483 failed = true;
1484 q = -1;
1485 d = Int32.MinValue;
1486 val = d / q;
1487 } catch (DivideByZeroException) {
1488 /* wrong exception */
1489 } catch (OverflowException) {
1490 failed = false;
1492 if (failed)
1493 return 3;
1495 try {
1496 failed = true;
1497 q = -1;
1498 d = Int32.MinValue;
1499 val = d % q;
1500 } catch (DivideByZeroException) {
1501 /* wrong exception */
1502 } catch (OverflowException) {
1503 failed = false;
1505 if (failed)
1506 return 4;
1508 return 0;
1511 public static int return_55 () {
1512 return 55;
1515 public static int test_0_cfold_div_zero () {
1516 // Test that constant folding doesn't cause division by zero exceptions
1517 if (return_55 () != return_55 ()) {
1518 int d = 1;
1519 int q = 0;
1520 int val;
1522 val = d / q;
1523 val = d % q;
1525 q = -1;
1526 d = Int32.MinValue;
1527 val = d / q;
1529 q = -1;
1530 val = d % q;
1533 return 0;
1536 public static int test_0_udiv_zero () {
1537 uint d = 1;
1538 uint q = 0;
1539 uint val;
1540 bool failed;
1542 try {
1543 failed = true;
1544 val = d / q;
1545 } catch (DivideByZeroException) {
1546 failed = false;
1548 if (failed)
1549 return 1;
1551 try {
1552 failed = true;
1553 val = d % q;
1554 } catch (DivideByZeroException) {
1555 failed = false;
1557 if (failed)
1558 return 2;
1560 return 0;
1563 public static int test_0_long_div_zero () {
1564 long d = 1;
1565 long q = 0;
1566 long val;
1567 bool failed;
1569 try {
1570 failed = true;
1571 val = d / q;
1572 } catch (DivideByZeroException) {
1573 failed = false;
1575 if (failed)
1576 return 1;
1578 try {
1579 failed = true;
1580 val = d % q;
1581 } catch (DivideByZeroException) {
1582 failed = false;
1584 if (failed)
1585 return 2;
1587 try {
1588 failed = true;
1589 q = -1;
1590 d = Int64.MinValue;
1591 val = d / q;
1592 } catch (DivideByZeroException) {
1593 /* wrong exception */
1594 } catch (ArithmeticException) {
1595 failed = false;
1597 if (failed)
1598 return 3;
1600 try {
1601 failed = true;
1602 q = -1;
1603 d = Int64.MinValue;
1604 val = d % q;
1605 } catch (DivideByZeroException) {
1606 /* wrong exception */
1607 } catch (ArithmeticException) {
1608 failed = false;
1610 if (failed)
1611 return 4;
1613 return 0;
1616 public static int test_0_ulong_div_zero () {
1617 ulong d = 1;
1618 ulong q = 0;
1619 ulong val;
1620 bool failed;
1622 try {
1623 failed = true;
1624 val = d / q;
1625 } catch (DivideByZeroException) {
1626 failed = false;
1628 if (failed)
1629 return 1;
1631 try {
1632 failed = true;
1633 val = d % q;
1634 } catch (DivideByZeroException) {
1635 failed = false;
1637 if (failed)
1638 return 2;
1640 return 0;
1643 public static int test_0_float_div_zero () {
1644 double d = 1;
1645 double q = 0;
1646 double val;
1647 bool failed;
1649 try {
1650 failed = false;
1651 val = d / q;
1652 } catch (DivideByZeroException) {
1653 failed = true;
1655 if (failed)
1656 return 1;
1658 try {
1659 failed = false;
1660 val = d % q;
1661 } catch (DivideByZeroException) {
1662 failed = true;
1664 if (failed)
1665 return 2;
1667 return 0;
1670 public static int test_0_invalid_unbox () {
1672 int i = 123;
1673 object o = "Some string";
1674 int res = 1;
1676 try {
1677 // Illegal conversion; o contains a string not an int
1678 i = (int) o;
1679 } catch (Exception e) {
1680 if (i ==123)
1681 res = 0;
1684 return res;
1687 // Test that double[] can't be cast to double (bug #46027)
1688 public static int test_0_invalid_unbox_arrays () {
1689 double[] d1 = { 1.0 };
1690 double[][] d2 = { d1 };
1691 Array a = d2;
1693 try {
1694 foreach (double d in a) {
1696 return 1;
1698 catch (InvalidCastException e) {
1699 return 0;
1703 /* bug# 42190, at least mcs generates a leave for the return that
1704 * jumps out of multiple exception clauses: we used to execute just
1705 * one enclosing finally block.
1707 public static int finally_level;
1708 static void do_something () {
1709 int a = 0;
1710 try {
1711 try {
1712 return;
1713 } finally {
1714 a = 1;
1716 } finally {
1717 finally_level++;
1721 public static int test_2_multiple_finally_clauses () {
1722 finally_level = 0;
1723 do_something ();
1724 if (finally_level == 1)
1725 return 2;
1726 return 0;
1729 public static int test_3_checked_cast_un () {
1730 ulong i = 0x8000000034000000;
1731 long j;
1733 try {
1734 checked { j = (long)i; }
1735 } catch (OverflowException) {
1736 j = 2;
1739 if (j != 2)
1740 return 0;
1741 return 3;
1744 public static int test_4_checked_cast () {
1745 long i;
1746 ulong j;
1748 unchecked { i = (long)0x8000000034000000;};
1749 try {
1750 checked { j = (ulong)i; }
1751 } catch (OverflowException) {
1752 j = 3;
1755 if (j != 3)
1756 return 0;
1757 return 4;
1760 static readonly int[] mul_dim_results = new int[] {
1761 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1762 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1763 2, 0, 2, 1, 2, 8,
1764 3, 0, 3, 1, 3, 8,
1765 4, 0, 4, 1, 4, 8,
1766 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1767 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1768 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1771 public static int test_0_multi_dim_array_access () {
1772 int [,] a = System.Array.CreateInstance (typeof (int),
1773 new int [] {3,6}, new int [] {2,2 }) as int[,];
1774 int x, y;
1775 int result_idx = 0;
1776 for (x = 0; x < 8; ++x) {
1777 for (y = 0; y < 9; ++y) {
1778 bool got_ex = false;
1779 try {
1780 a [x, y] = 1;
1781 } catch {
1782 got_ex = true;
1784 if (got_ex) {
1785 if (result_idx >= mul_dim_results.Length)
1786 return -1;
1787 if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1788 return result_idx + 1;
1790 result_idx += 2;
1794 if (result_idx == mul_dim_results.Length)
1795 return 0;
1796 return 200;
1799 static void helper_out_obj (out object o) {
1800 o = (object)"buddy";
1803 static void helper_out_string (out string o) {
1804 o = "buddy";
1807 public static int test_2_array_mismatch () {
1808 string[] a = { "hello", "world" };
1809 object[] b = a;
1810 bool passed = false;
1812 try {
1813 helper_out_obj (out b [1]);
1814 } catch (ArrayTypeMismatchException) {
1815 passed = true;
1817 if (!passed)
1818 return 0;
1819 helper_out_string (out a [1]);
1820 if (a [1] != "buddy")
1821 return 1;
1822 return 2;
1825 public static int test_0_ovf1 () {
1826 int exception = 0;
1828 checked {
1829 try {
1830 ulong a = UInt64.MaxValue - 1;
1831 ulong t = a++;
1832 } catch {
1833 exception = 1;
1836 return exception;
1839 public static int test_1_ovf2 () {
1840 int exception = 0;
1842 checked {
1843 try {
1844 ulong a = UInt64.MaxValue;
1845 ulong t = a++;
1846 } catch {
1847 exception = 1;
1850 return exception;
1853 public static int test_0_ovf3 () {
1854 int exception = 0;
1856 long a = Int64.MaxValue - 1;
1857 checked {
1858 try {
1859 long t = a++;
1860 } catch {
1861 exception = 1;
1864 return exception;
1867 public static int test_1_ovf4 () {
1868 int exception = 0;
1870 long a = Int64.MaxValue;
1871 checked {
1872 try {
1873 long t = a++;
1874 } catch {
1875 exception = 1;
1878 return exception;
1881 public static int test_0_ovf5 () {
1882 int exception = 0;
1884 ulong a = UInt64.MaxValue - 1;
1885 checked {
1886 try {
1887 ulong t = a++;
1888 } catch {
1889 exception = 1;
1892 return exception;
1895 public static int test_1_ovf6 () {
1896 int exception = 0;
1898 ulong a = UInt64.MaxValue;
1899 checked {
1900 try {
1901 ulong t = a++;
1902 } catch {
1903 exception = 1;
1906 return exception;
1909 public static int test_0_ovf7 () {
1910 int exception = 0;
1912 long a = Int64.MinValue + 1;
1913 checked {
1914 try {
1915 long t = a--;
1916 } catch {
1917 exception = 1;
1920 return 0;
1923 public static int test_1_ovf8 () {
1924 int exception = 0;
1926 long a = Int64.MinValue;
1927 checked {
1928 try {
1929 long t = a--;
1930 } catch {
1931 exception = 1;
1934 return exception;
1937 public static int test_0_ovf9 () {
1938 int exception = 0;
1940 ulong a = UInt64.MinValue + 1;
1941 checked {
1942 try {
1943 ulong t = a--;
1944 } catch {
1945 exception = 1;
1948 return exception;
1951 public static int test_1_ovf10 () {
1952 int exception = 0;
1954 ulong a = UInt64.MinValue;
1955 checked {
1956 try {
1957 ulong t = a--;
1958 } catch {
1959 exception = 1;
1962 return exception;
1965 public static int test_0_ovf11 () {
1966 int exception = 0;
1968 int a = Int32.MinValue + 1;
1969 checked {
1970 try {
1971 int t = a--;
1972 } catch {
1973 exception = 1;
1976 return exception;
1979 public static int test_1_ovf12 () {
1980 int exception = 0;
1982 int a = Int32.MinValue;
1983 checked {
1984 try {
1985 int t = a--;
1986 } catch {
1987 exception = 1;
1990 return exception;
1993 public static int test_0_ovf13 () {
1994 int exception = 0;
1996 uint a = 1;
1997 checked {
1998 try {
1999 uint t = a--;
2000 } catch {
2001 exception = 1;
2004 return exception;
2007 public static int test_1_ovf14 () {
2008 int exception = 0;
2010 uint a = 0;
2011 checked {
2012 try {
2013 uint t = a--;
2014 } catch {
2015 exception = 1;
2018 return exception;
2021 public static int test_0_ovf15 () {
2022 int exception = 0;
2024 sbyte a = 126;
2025 checked {
2026 try {
2027 sbyte t = a++;
2028 } catch {
2029 exception = 1;
2032 return exception;
2035 public static int test_1_ovf16 () {
2036 int exception = 0;
2038 sbyte a = 127;
2039 checked {
2040 try {
2041 sbyte t = a++;
2042 } catch {
2043 exception = 1;
2046 return exception;
2049 public static int test_0_ovf17 () {
2050 int exception = 0;
2052 checked {
2053 try {
2054 } catch {
2055 exception = 1;
2058 return exception;
2061 public static int test_0_ovf18 () {
2062 int exception = 0;
2064 int a = 1 << 29;
2065 checked {
2066 try {
2067 int t = a*2;
2068 } catch {
2069 exception = 1;
2072 return exception;
2075 public static int test_1_ovf19 () {
2076 int exception = 0;
2078 int a = 1 << 30;
2079 checked {
2080 try {
2081 int t = a*2;
2082 } catch {
2083 exception = 1;
2086 return exception;
2089 public static int test_0_ovf20 () {
2090 int exception = 0;
2092 checked {
2093 try {
2094 ulong a = 0xffffffffff;
2095 ulong t = a*0x0ffffff;
2096 } catch {
2097 exception = 1;
2100 return exception;
2103 public static int test_1_ovf21 () {
2104 int exception = 0;
2106 ulong a = 0xffffffffff;
2107 checked {
2108 try {
2109 ulong t = a*0x0fffffff;
2110 } catch {
2111 exception = 1;
2114 return exception;
2117 public static int test_1_ovf22 () {
2118 int exception = 0;
2120 long a = Int64.MinValue;
2121 long b = 10;
2122 checked {
2123 try {
2124 long v = a * b;
2125 } catch {
2126 exception = 1;
2129 return exception;
2132 public static int test_1_ovf23 () {
2133 int exception = 0;
2135 long a = 10;
2136 long b = Int64.MinValue;
2137 checked {
2138 try {
2139 long v = a * b;
2140 } catch {
2141 exception = 1;
2144 return exception;
2147 class Broken {
2148 public static int i;
2150 static Broken () {
2151 throw new Exception ("Ugh!");
2154 public static int DoSomething () {
2155 return i;
2159 public static int test_0_exception_in_cctor () {
2160 try {
2161 Broken.DoSomething ();
2163 catch (TypeInitializationException) {
2164 // This will only happen once even if --regression is used
2166 return 0;
2169 public static int test_5_regalloc () {
2170 int i = 0;
2172 try {
2173 for (i = 0; i < 10; ++i) {
2174 if (i == 5)
2175 throw new Exception ();
2178 catch (Exception) {
2179 if (i != 5)
2180 return i;
2183 // Check that variables written in catch clauses are volatile
2184 int j = 0;
2185 try {
2186 throw new Exception ();
2188 catch (Exception) {
2189 j = 5;
2191 if (j != 5)
2192 return 6;
2194 int k = 0;
2195 try {
2196 try {
2197 throw new Exception ();
2199 finally {
2200 k = 5;
2203 catch (Exception) {
2205 if (k != 5)
2206 return 7;
2208 return i;
2211 public static void rethrow () {
2212 try {
2213 throw new ApplicationException();
2214 } catch (ApplicationException) {
2215 try {
2216 throw new OverflowException();
2217 } catch (Exception) {
2218 throw;
2223 // Test that a rethrow rethrows the correct exception
2224 public static int test_0_rethrow_nested () {
2225 try {
2226 rethrow ();
2227 } catch (OverflowException) {
2228 return 0;
2229 } catch (Exception) {
2230 return 1;
2232 return 2;
2235 /* MarshalByRefObject prevents the methods from being inlined */
2236 class ThrowClass : MarshalByRefObject {
2237 public static void rethrow1 () {
2238 throw new Exception ();
2241 public static void rethrow2 () {
2242 rethrow1 ();
2243 /* This disables tailcall opts */
2244 Console.WriteLine ();
2248 public static int test_0_rethrow_stacktrace () {
2249 // Check that rethrowing an exception preserves the original stack trace
2250 try {
2251 try {
2252 ThrowClass.rethrow2 ();
2254 catch (Exception ex) {
2255 // Check that each catch clause has its own exception variable
2256 // If not, the throw below will overwrite the exception used
2257 // by the rethrow
2258 try {
2259 throw new DivideByZeroException ();
2261 catch (Exception foo) {
2264 throw;
2267 catch (Exception ex) {
2268 if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2269 return 0;
2272 return 1;
2275 interface IFace {}
2276 class Face : IFace {}
2278 public static int test_1_array_mismatch_2 () {
2279 try {
2280 object [] o = new Face [1];
2281 o [0] = 1;
2282 return 0;
2283 } catch (ArrayTypeMismatchException) {
2284 return 1;
2288 public static int test_1_array_mismatch_3 () {
2289 try {
2290 object [] o = new IFace [1];
2291 o [0] = 1;
2292 return 0;
2293 } catch (ArrayTypeMismatchException) {
2294 return 1;
2298 public static int test_1_array_mismatch_4 () {
2299 try {
2300 object [][] o = new Face [5] [];
2301 o [0] = new object [5];
2303 return 0;
2304 } catch (ArrayTypeMismatchException) {
2305 return 1;
2309 public static int test_0_array_size () {
2310 bool failed;
2312 try {
2313 failed = true;
2314 int[] mem1 = new int [Int32.MaxValue];
2316 catch (OutOfMemoryException e) {
2317 failed = false;
2319 if (failed)
2320 return 1;
2322 try {
2323 failed = true;
2324 int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2326 catch (OutOfMemoryException e) {
2327 failed = false;
2329 if (failed)
2330 return 2;
2332 return 0;
2335 struct S {
2336 int i, j, k, l, m, n;
2339 static IntPtr[] addr;
2341 static unsafe void throw_func (int i, S s) {
2342 addr [i] = new IntPtr (&i);
2343 throw new Exception ();
2346 /* Test that arguments are correctly popped off the stack during unwinding */
2347 public static int test_0_stack_unwind () {
2348 addr = new IntPtr [1000];
2349 S s = new S ();
2350 for (int j = 0; j < 1000; j++) {
2351 try {
2352 throw_func (j, s);
2354 catch (Exception) {
2357 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2360 static unsafe void get_sp (int i) {
2361 addr [i] = new IntPtr (&i);
2364 /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2365 public static int test_0_throw_unwind () {
2366 addr = new IntPtr [1000];
2367 S s = new S ();
2368 for (int j = 0; j < 1000; j++) {
2369 try {
2370 get_sp (j);
2371 throw new Exception ();
2373 catch (Exception) {
2376 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2379 public static int test_0_regress_73242 () {
2380 int [] arr = new int [10];
2381 for (int i = 0; i < 10; ++i)
2382 arr [i] = 0;
2383 try {
2384 throw new Exception ();
2386 catch {
2388 return 0;
2391 public static int test_0_nullref () {
2392 try {
2393 Array foo = null;
2394 foo.Clone();
2395 } catch (NullReferenceException e) {
2396 return 0;
2398 return 1;
2401 public int amethod () {
2402 return 1;
2405 public static int test_0_nonvirt_nullref_at_clause_start () {
2406 Tests t = null;
2407 try {
2408 t.amethod ();
2409 } catch (NullReferenceException) {
2410 return 0;
2413 return 1;
2416 public static int throw_only () {
2417 throw new Exception ();
2420 [MethodImpl(MethodImplOptions.NoInlining)]
2421 public static int throw_only2 () {
2422 return throw_only ();
2425 public static int test_0_inline_throw_only () {
2426 try {
2427 return throw_only2 ();
2429 catch (Exception ex) {
2430 return 0;
2434 public static string GetText (string s) {
2435 return s;
2438 public static int throw_only_gettext () {
2439 throw new Exception (GetText ("FOO"));
2442 public static int test_0_inline_throw_only_gettext () {
2443 object o = null;
2444 try {
2445 o = throw_only_gettext ();
2447 catch (Exception ex) {
2448 return 0;
2451 return o != null ? 0 : 1;
2454 // bug #78633
2455 public static int test_0_throw_to_branch_opt_outer_clause () {
2456 int i = 0;
2458 try {
2459 try {
2460 string [] files = new string[1];
2462 string s = files[2];
2463 } finally {
2464 i ++;
2466 } catch {
2469 return (i == 1) ? 0 : 1;
2472 // bug #485721
2473 public static int test_0_try_inside_finally_cmov_opt () {
2474 bool Reconect = false;
2476 object o = new object ();
2478 try {
2480 catch (Exception ExCon) {
2481 if (o != null)
2482 Reconect = true;
2484 try {
2486 catch (Exception Last) {
2489 finally {
2490 if (Reconect == true) {
2491 try {
2493 catch (Exception ex) {
2498 return 0;
2501 public static int test_0_inline_throw () {
2502 try {
2503 inline_throw1 (5);
2504 return 1;
2505 } catch {
2506 return 0;
2510 // for llvm, the end bblock is unreachable
2511 public static int inline_throw1 (int i) {
2512 if (i == 0)
2513 throw new Exception ();
2514 else
2515 return inline_throw2 (i);
2518 public static int inline_throw2 (int i) {
2519 throw new Exception ();
2522 // bug #539550
2523 public static int test_0_lmf_filter () {
2524 try {
2525 // The invoke calls a runtime-invoke wrapper which has a filter clause
2526 typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2527 } catch (TargetInvocationException) {
2529 return 0;
2532 public static void lmf_filter () {
2533 try {
2534 Connect ();
2536 catch {
2537 throw new NotImplementedException ();
2541 public static void Connect () {
2542 Stop ();
2543 throw new Exception();
2546 public static void Stop () {
2547 try {
2548 lock (null) {}
2550 catch {
2554 private static void do_raise () {
2555 throw new System.Exception ();
2558 private static int int_func (int i) {
2559 return i;
2562 // #559876
2563 public static int test_8_local_deadce_causes () {
2564 int myb = 4;
2566 try {
2567 myb = int_func (8);
2568 do_raise();
2569 myb = int_func (2);
2570 } catch (System.Exception) {
2571 return myb;
2573 return 0;
2576 public static int test_0_except_opt_two_clauses () {
2577 int size;
2578 size = -1;
2579 uint ui = (uint)size;
2580 try {
2581 checked {
2582 uint v = ui * (uint)4;
2584 } catch (OverflowException e) {
2585 return 0;
2586 } catch (Exception) {
2587 return 1;
2590 return 2;
2593 class Child
2595 public virtual long Method()
2597 throw new Exception();
2601 /* #612206 */
2602 public static int test_100_long_vars_in_clauses_initlocals_opt () {
2603 Child c = new Child();
2604 long value = 100;
2605 try {
2606 value = c.Method();
2608 catch {}
2609 return (int)value;
2612 class A {
2613 public object AnObj;
2616 public static void DoSomething (ref object o) {
2619 public static int test_0_ldflda_null () {
2620 A a = null;
2622 try {
2623 DoSomething (ref a.AnObj);
2624 } catch (NullReferenceException) {
2625 return 0;
2628 return 1;
2631 unsafe struct Foo
2633 public int i;
2635 public static Foo* pFoo;
2638 /* MS.NET doesn't seem to throw in this case */
2639 public unsafe static int test_0_ldflda_null_pointer () {
2640 int* pi = &Foo.pFoo->i;
2642 return 0;