Mixed testing (#9021)
[mono-project.git] / mono / mini / exceptions.cs
blobded6c50afb151f087897610190386c14d2ce0d09
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 #if __MOBILE__
28 class ExceptionTests
29 #else
30 class Tests
31 #endif
34 #if !__MOBILE__
35 public static int Main (string[] args) {
36 return TestDriver.RunTests (typeof (Tests), args);
38 #endif
40 public static int test_0_catch () {
41 Exception x = new Exception ();
43 try {
44 throw x;
45 } catch (Exception e) {
46 if (e == x)
47 return 0;
49 return 1;
52 public static int test_0_finally_without_exc () {
53 int x;
55 try {
56 x = 1;
57 } catch (Exception e) {
58 x = 2;
59 } finally {
60 x = 0;
63 return x;
66 public static int test_0_finally () {
67 int x = 1;
69 try {
70 throw new Exception ();
71 } catch (Exception e) {
72 x = 2;
73 } finally {
74 x = 0;
76 return x;
79 public static int test_0_nested_finally () {
80 int a;
82 try {
83 a = 1;
84 } finally {
85 try {
86 a = 2;
87 } finally {
88 a = 0;
91 return a;
94 public static int test_0_byte_cast () {
95 int a;
96 long l;
97 ulong ul;
98 byte b = 0;
99 bool failed;
101 try {
102 a = 255;
103 failed = false;
104 checked {
105 b = (byte)a;
107 } catch (OverflowException) {
108 failed = true;
110 if (failed)
111 return 1;
112 if (b != 255)
113 return -1;
115 try {
116 a = 0;
117 failed = false;
118 checked {
119 b = (byte)a;
121 } catch (OverflowException) {
122 failed = true;
124 if (failed)
125 return 2;
126 if (b != 0)
127 return -2;
130 try {
131 a = 256;
132 failed = true;
133 checked {
134 b = (byte)a;
136 } catch (OverflowException) {
137 failed = false;
139 if (failed)
140 return 3;
141 if (b != 0)
142 return -3;
144 try {
145 a = -1;
146 failed = true;
147 checked {
148 b = (byte)a;
150 } catch (OverflowException) {
151 failed = false;
153 if (failed)
154 return 4;
155 if (b != 0)
156 return -4;
158 try {
159 double d = 0;
160 failed = false;
161 checked {
162 b = (byte)d;
164 } catch (OverflowException) {
165 failed = true;
167 if (failed)
168 return 5;
169 if (b != 0)
170 return -5;
172 try {
173 double d = -1;
174 failed = true;
175 checked {
176 b = (byte)d;
178 } catch (OverflowException) {
179 failed = false;
181 if (failed)
182 return 6;
183 if (b != 0)
184 return -6;
186 try {
187 double d = 255;
188 failed = false;
189 checked {
190 b = (byte)d;
192 } catch (OverflowException) {
193 failed = true;
195 if (failed)
196 return 7;
197 if (b != 255)
198 return -7;
200 try {
201 double d = 256;
202 failed = true;
203 checked {
204 b = (byte)d;
206 } catch (OverflowException) {
207 failed = false;
209 if (failed)
210 return 8;
211 if (b != 255)
212 return -8;
214 try {
215 l = 255;
216 failed = false;
217 checked {
218 b = (byte)l;
220 } catch (OverflowException) {
221 failed = true;
223 if (failed)
224 return 9;
225 if (b != 255)
226 return -9;
228 try {
229 l = 0;
230 failed = false;
231 checked {
232 b = (byte)l;
234 } catch (OverflowException) {
235 failed = true;
237 if (failed)
238 return 10;
239 if (b != 0)
240 return -10;
242 try {
243 l = 256;
244 failed = true;
245 checked {
246 b = (byte)l;
248 } catch (OverflowException) {
249 failed = false;
251 if (failed)
252 return 11;
253 if (b != 0)
254 return -11;
256 try {
257 l = -1;
258 failed = true;
259 checked {
260 b = (byte)l;
262 } catch (OverflowException) {
263 failed = false;
265 if (failed)
266 return 12;
267 if (b != 0)
268 return -12;
270 try {
271 ul = 256;
272 failed = true;
273 checked {
274 b = (byte)ul;
277 catch (OverflowException) {
278 failed = false;
280 if (failed)
281 return 13;
282 if (b != 0)
283 return -13;
285 return 0;
288 public static int test_0_sbyte_cast () {
289 int a;
290 long l;
291 sbyte b = 0;
292 bool failed;
294 try {
295 a = 255;
296 failed = true;
297 checked {
298 b = (sbyte)a;
300 } catch (OverflowException) {
301 failed = false;
303 if (failed)
304 return 1;
305 if (b != 0)
306 return -1;
308 try {
309 a = 0;
310 failed = false;
311 checked {
312 b = (sbyte)a;
314 } catch (OverflowException) {
315 failed = true;
317 if (failed)
318 return 2;
319 if (b != 0)
320 return -2;
322 try {
323 a = 256;
324 failed = true;
325 checked {
326 b = (sbyte)a;
328 } catch (OverflowException) {
329 failed = false;
331 if (failed)
332 return 3;
333 if (b != 0)
334 return -3;
336 try {
337 a = -129;
338 failed = true;
339 checked {
340 b = (sbyte)a;
342 } catch (OverflowException) {
343 failed = false;
345 if (failed)
346 return 4;
347 if (b != 0)
348 return -4;
350 try {
351 a = -1;
352 failed = false;
353 checked {
354 b = (sbyte)a;
356 } catch (OverflowException) {
357 failed = true;
359 if (failed)
360 return 5;
361 if (b != -1)
362 return -5;
364 try {
365 a = -128;
366 failed = false;
367 checked {
368 b = (sbyte)a;
370 } catch (OverflowException) {
371 failed = true;
373 if (failed)
374 return 6;
375 if (b != -128)
376 return -6;
378 try {
379 a = 127;
380 failed = false;
381 checked {
382 b = (sbyte)a;
384 } catch (OverflowException) {
385 failed = true;
387 if (failed)
388 return 7;
389 if (b != 127)
390 return -7;
392 try {
393 a = 128;
394 failed = true;
395 checked {
396 b = (sbyte)a;
398 } catch (OverflowException) {
399 failed = false;
401 if (failed)
402 return 8;
403 if (b != 127)
404 return -8;
406 try {
407 double d = 127;
408 failed = false;
409 checked {
410 b = (sbyte)d;
412 } catch (OverflowException) {
413 failed = true;
415 if (failed)
416 return 9;
417 if (b != 127)
418 return -9;
420 try {
421 double d = -128;
422 failed = false;
423 checked {
424 b = (sbyte)d;
426 } catch (OverflowException) {
427 failed = true;
429 if (failed)
430 return 10;
431 if (b != -128)
432 return -10;
434 try {
435 double d = 128;
436 failed = true;
437 checked {
438 b = (sbyte)d;
440 } catch (OverflowException) {
441 failed = false;
443 if (failed)
444 return 11;
445 if (b != -128)
446 return -11;
448 try {
449 double d = -129;
450 failed = true;
451 checked {
452 b = (sbyte)d;
454 } catch (OverflowException) {
455 failed = false;
457 if (failed)
458 return 12;
459 if (b != -128)
460 return -12;
462 try {
463 l = 255;
464 failed = true;
465 checked {
466 b = (sbyte)l;
468 } catch (OverflowException) {
469 failed = false;
471 if (failed)
472 return 13;
473 if (b != -128)
474 return -13;
476 try {
477 l = 0;
478 failed = false;
479 checked {
480 b = (sbyte)l;
482 } catch (OverflowException) {
483 failed = true;
485 if (failed)
486 return 14;
487 if (b != 0)
488 return -14;
490 try {
491 l = 256;
492 failed = true;
493 checked {
494 b = (sbyte)l;
496 } catch (OverflowException) {
497 failed = false;
499 if (failed)
500 return 15;
501 if (b != 0)
502 return -15;
504 try {
505 l = -129;
506 failed = true;
507 checked {
508 b = (sbyte)l;
510 } catch (OverflowException) {
511 failed = false;
513 if (failed)
514 return 16;
515 if (b != 0)
516 return -16;
518 try {
519 l = -1;
520 failed = false;
521 checked {
522 b = (sbyte)l;
524 } catch (OverflowException) {
525 failed = true;
527 if (failed)
528 return 17;
529 if (b != -1)
530 return -17;
532 try {
533 l = -128;
534 failed = false;
535 checked {
536 b = (sbyte)l;
538 } catch (OverflowException) {
539 failed = true;
541 if (failed)
542 return 18;
543 if (b != -128)
544 return -18;
546 try {
547 l = 127;
548 failed = false;
549 checked {
550 b = (sbyte)l;
552 } catch (OverflowException) {
553 failed = true;
555 if (failed)
556 return 19;
557 if (b != 127)
558 return -19;
560 try {
561 l = 128;
562 failed = true;
563 checked {
564 b = (sbyte)l;
566 } catch (OverflowException) {
567 failed = false;
569 if (failed)
570 return 20;
571 if (b != 127)
572 return -20;
574 try {
575 ulong ul = 128;
576 failed = true;
577 checked {
578 b = (sbyte)ul;
581 catch (OverflowException) {
582 failed = false;
584 if (failed)
585 return 21;
586 if (b != 127)
587 return -21;
589 return 0;
592 public static int test_0_ushort_cast () {
593 int a;
594 long l;
595 ulong ul;
596 ushort b;
597 bool failed;
599 try {
600 a = System.UInt16.MaxValue;
601 failed = false;
602 checked {
603 b = (ushort)a;
605 } catch (OverflowException) {
606 failed = true;
608 if (failed)
609 return 1;
611 try {
612 a = 0;
613 failed = false;
614 checked {
615 b = (ushort)a;
617 } catch (OverflowException) {
618 failed = true;
620 if (failed)
621 return 2;
623 try {
624 a = System.UInt16.MaxValue + 1;
625 failed = true;
626 checked {
627 b = (ushort)a;
629 } catch (OverflowException) {
630 failed = false;
632 if (failed)
633 return 3;
635 try {
636 a = -1;
637 failed = true;
638 checked {
639 b = (ushort)a;
641 } catch (OverflowException) {
642 failed = false;
644 if (failed)
645 return 4;
647 try {
648 double d = 0;
649 failed = false;
650 checked {
651 b = (ushort)d;
653 } catch (OverflowException) {
654 failed = true;
656 if (failed)
657 return 5;
659 try {
660 double d = System.UInt16.MaxValue;
661 failed = false;
662 checked {
663 b = (ushort)d;
665 } catch (OverflowException) {
666 failed = true;
668 if (failed)
669 return 6;
671 try {
672 double d = -1;
673 failed = true;
674 checked {
675 b = (ushort)d;
677 } catch (OverflowException) {
678 failed = false;
680 if (failed)
681 return 7;
683 try {
684 double d = System.UInt16.MaxValue + 1.0;
685 failed = true;
686 checked {
687 b = (ushort)d;
689 } catch (OverflowException) {
690 failed = false;
692 if (failed)
693 return 8;
695 try {
696 l = System.UInt16.MaxValue;
697 failed = false;
698 checked {
699 b = (ushort)l;
701 } catch (OverflowException) {
702 failed = true;
704 if (failed)
705 return 9;
707 try {
708 l = 0;
709 failed = false;
710 checked {
711 b = (ushort)l;
713 } catch (OverflowException) {
714 failed = true;
716 if (failed)
717 return 10;
719 try {
720 l = System.UInt16.MaxValue + 1;
721 failed = true;
722 checked {
723 b = (ushort)l;
725 } catch (OverflowException) {
726 failed = false;
728 if (failed)
729 return 11;
731 try {
732 l = -1;
733 failed = true;
734 checked {
735 b = (ushort)l;
737 } catch (OverflowException) {
738 failed = false;
740 if (failed)
741 return 12;
743 try {
744 ul = 0xfffff;
745 failed = true;
746 checked {
747 b = (ushort)ul;
749 } catch (OverflowException) {
750 failed = false;
752 if (failed)
753 return 13;
755 return 0;
758 public static int test_0_short_cast () {
759 int a;
760 long l;
761 short b;
762 bool failed;
764 try {
765 a = System.UInt16.MaxValue;
766 failed = true;
767 checked {
768 b = (short)a;
770 } catch (OverflowException) {
771 failed = false;
773 if (failed)
774 return 1;
776 try {
777 a = 0;
778 failed = false;
779 checked {
780 b = (short)a;
782 } catch (OverflowException) {
783 failed = true;
785 if (failed)
786 return 2;
788 try {
789 a = System.Int16.MaxValue + 1;
790 failed = true;
791 checked {
792 b = (short)a;
794 } catch (OverflowException) {
795 failed = false;
797 if (failed)
798 return 3;
800 try {
801 a = System.Int16.MinValue - 1;
802 failed = true;
803 checked {
804 b = (short)a;
806 } catch (OverflowException) {
807 failed = false;
809 if (failed)
810 return 4;
812 try {
813 a = -1;
814 failed = false;
815 checked {
816 b = (short)a;
818 } catch (OverflowException) {
819 failed = true;
821 if (failed)
822 return 5;
824 try {
825 a = System.Int16.MinValue;
826 failed = false;
827 checked {
828 b = (short)a;
830 } catch (OverflowException) {
831 failed = true;
833 if (failed)
834 return 6;
836 try {
837 a = System.Int16.MaxValue;
838 failed = false;
839 checked {
840 b = (short)a;
842 } catch (OverflowException) {
843 failed = true;
845 if (failed)
846 return 7;
848 try {
849 a = System.Int16.MaxValue + 1;
850 failed = true;
851 checked {
852 b = (short)a;
854 } catch (OverflowException) {
855 failed = false;
857 if (failed)
858 return 8;
860 try {
861 double d = System.Int16.MaxValue;
862 failed = false;
863 checked {
864 b = (short)d;
866 } catch (OverflowException) {
867 failed = true;
869 if (failed)
870 return 9;
872 try {
873 double d = System.Int16.MinValue;
874 failed = false;
875 checked {
876 b = (short)d;
878 } catch (OverflowException) {
879 failed = true;
881 if (failed)
882 return 10;
884 try {
885 double d = System.Int16.MaxValue + 1.0;
886 failed = true;
887 checked {
888 b = (short)d;
890 } catch (OverflowException) {
891 failed = false;
893 if (failed)
894 return 11;
896 try {
897 double d = System.Int16.MinValue - 1.0;
898 failed = true;
899 checked {
900 b = (short)d;
902 } catch (OverflowException) {
903 failed = false;
905 if (failed)
906 return 12;
908 try {
909 l = System.Int16.MaxValue + 1;
910 failed = true;
911 checked {
912 b = (short)l;
914 } catch (OverflowException) {
915 failed = false;
917 if (failed)
918 return 13;
920 try {
921 l = System.Int16.MaxValue;
922 failed = false;
923 checked {
924 b = (short)l;
926 } catch (OverflowException) {
927 failed = true;
929 if (failed)
930 return 14;
932 try {
933 l = System.Int16.MinValue - 1;
934 failed = true;
935 checked {
936 b = (short)l;
938 } catch (OverflowException) {
939 failed = false;
941 if (failed)
942 return 15;
945 try {
946 l = System.Int16.MinValue;
947 failed = false;
948 checked {
949 b = (short)l;
951 } catch (OverflowException) {
952 failed = true;
954 if (failed)
955 return 16;
957 try {
958 l = 0x00000000ffffffff;
959 failed = true;
960 checked {
961 b = (short)l;
963 } catch (OverflowException) {
964 failed = false;
966 if (failed)
967 return 17;
969 try {
970 ulong ul = 32768;
971 failed = true;
972 checked {
973 b = (short)ul;
975 } catch (OverflowException) {
976 failed = false;
978 if (failed)
979 return 18;
981 return 0;
984 public static int test_0_int_cast () {
985 int a;
986 long l;
987 bool failed;
989 try {
990 double d = System.Int32.MaxValue + 1.0;
991 failed = true;
992 checked {
993 a = (int)d;
995 } catch (OverflowException) {
996 failed = false;
998 if (failed)
999 return 1;
1001 try {
1002 double d = System.Int32.MaxValue;
1003 failed = false;
1004 checked {
1005 a = (int)d;
1007 } catch (OverflowException) {
1008 failed = true;
1010 if (failed)
1011 return 2;
1014 try {
1015 double d = System.Int32.MinValue;
1016 failed = false;
1017 checked {
1018 a = (int)d;
1020 } catch (OverflowException) {
1021 failed = true;
1023 if (failed)
1024 return 3;
1027 try {
1028 double d = System.Int32.MinValue - 1.0;
1029 failed = true;
1030 checked {
1031 a = (int)d;
1033 } catch (OverflowException) {
1034 failed = false;
1036 if (failed)
1037 return 4;
1039 try {
1040 l = System.Int32.MaxValue + (long)1;
1041 failed = true;
1042 checked {
1043 a = (int)l;
1045 } catch (OverflowException) {
1046 failed = false;
1048 if (failed)
1049 return 5;
1051 try {
1052 l = System.Int32.MaxValue;
1053 failed = false;
1054 checked {
1055 a = (int)l;
1057 } catch (OverflowException) {
1058 failed = true;
1060 if (failed)
1061 return 6;
1064 try {
1065 l = System.Int32.MinValue;
1066 failed = false;
1067 checked {
1068 a = (int)l;
1070 } catch (OverflowException) {
1071 failed = true;
1073 if (failed)
1074 return 7;
1077 try {
1078 l = System.Int32.MinValue - (long)1;
1079 failed = true;
1080 checked {
1081 a = (int)l;
1083 } catch (OverflowException) {
1084 failed = false;
1086 if (failed)
1087 return 8;
1089 try {
1090 uint ui = System.UInt32.MaxValue;
1091 failed = true;
1092 checked {
1093 a = (int)ui;
1096 catch (OverflowException) {
1097 failed = false;
1099 if (failed)
1100 return 9;
1102 try {
1103 ulong ul = (long)(System.Int32.MaxValue) + 1;
1104 failed = true;
1105 checked {
1106 a = (int)ul;
1109 catch (OverflowException) {
1110 failed = false;
1112 if (failed)
1113 return 10;
1115 try {
1116 ulong ul = UInt64.MaxValue;
1117 failed = true;
1118 checked {
1119 a = (int)ul;
1122 catch (OverflowException) {
1123 failed = false;
1125 if (failed)
1126 return 11;
1129 int i;
1130 float f = 1.1f;
1131 checked {
1132 i = (int) f;
1136 return 0;
1139 public static int test_0_uint_cast () {
1140 uint a;
1141 long l;
1142 bool failed;
1144 try {
1145 double d = System.UInt32.MaxValue;
1146 failed = false;
1147 checked {
1148 a = (uint)d;
1150 } catch (OverflowException) {
1151 failed = true;
1153 if (failed)
1154 return 1;
1156 try {
1157 double d = System.UInt32.MaxValue + 1.0;
1158 failed = true;
1159 checked {
1160 a = (uint)d;
1162 } catch (OverflowException) {
1163 failed = false;
1165 if (failed)
1166 return 2;
1168 try {
1169 double d = System.UInt32.MinValue;
1170 failed = false;
1171 checked {
1172 a = (uint)d;
1174 } catch (OverflowException) {
1175 failed = true;
1177 if (failed)
1178 return 3;
1180 try {
1181 double d = System.UInt32.MinValue - 1.0;
1182 failed = true;
1183 checked {
1184 a = (uint)d;
1186 } catch (OverflowException) {
1187 failed = false;
1189 if (failed)
1190 return 4;
1192 try {
1193 l = System.UInt32.MaxValue;
1194 failed = false;
1195 checked {
1196 a = (uint)l;
1198 } catch (OverflowException) {
1199 failed = true;
1201 if (failed)
1202 return 5;
1204 try {
1205 l = System.UInt32.MaxValue + (long)1;
1206 failed = true;
1207 checked {
1208 a = (uint)l;
1210 } catch (OverflowException) {
1211 failed = false;
1213 if (failed)
1214 return 6;
1216 try {
1217 l = System.UInt32.MinValue;
1218 failed = false;
1219 checked {
1220 a = (uint)l;
1222 } catch (OverflowException) {
1223 failed = true;
1225 if (failed)
1226 return 7;
1228 try {
1229 l = System.UInt32.MinValue - (long)1;
1230 failed = true;
1231 checked {
1232 a = (uint)l;
1234 } catch (OverflowException) {
1235 failed = false;
1237 if (failed)
1238 return 8;
1240 try {
1241 int i = -1;
1242 failed = true;
1243 checked {
1244 a = (uint)i;
1247 catch (OverflowException) {
1248 failed = false;
1250 if (failed)
1251 return 9;
1254 uint i;
1255 float f = 1.1f;
1256 checked {
1257 i = (uint) f;
1261 return 0;
1264 public static int test_0_long_cast () {
1267 * These tests depend on properties of x86 fp arithmetic so they won't work
1268 * on other platforms.
1271 long a;
1272 bool failed;
1274 try {
1275 double d = System.Int64.MaxValue - 512.0;
1276 failed = true;
1277 checked {
1278 a = (long)d;
1280 } catch (OverflowException) {
1281 failed = false;
1283 if (failed)
1284 return 1;
1286 try {
1287 double d = System.Int64.MaxValue - 513.0;
1288 failed = false;
1289 checked {
1290 a = (long)d;
1292 } catch (OverflowException) {
1293 failed = true;
1295 if (failed)
1296 return 2;
1298 try {
1299 double d = System.Int64.MinValue - 1024.0;
1300 failed = false;
1301 checked {
1302 a = (long)d;
1304 } catch (OverflowException) {
1305 failed = true;
1307 if (failed)
1308 return 3;
1310 try {
1311 double d = System.Int64.MinValue - 1025.0;
1312 failed = true;
1313 checked {
1314 a = (long)d;
1316 } catch (OverflowException) {
1317 failed = false;
1319 if (failed)
1320 return 4;
1324 long i;
1325 float f = 1.1f;
1326 checked {
1327 i = (long) f;
1331 return 0;
1334 public static int test_0_ulong_cast () {
1335 ulong a;
1336 bool failed;
1339 * These tests depend on properties of x86 fp arithmetic so they won't work
1340 * on other platforms.
1344 try {
1345 double d = System.UInt64.MaxValue - 1024.0;
1346 failed = true;
1347 checked {
1348 a = (ulong)d;
1350 } catch (OverflowException) {
1351 failed = false;
1353 if (failed)
1354 return 1;
1356 try {
1357 double d = System.UInt64.MaxValue - 1025.0;
1358 failed = false;
1359 checked {
1360 a = (ulong)d;
1362 } catch (OverflowException) {
1363 failed = true;
1365 if (failed)
1366 return 2;
1369 try {
1370 double d = 0;
1371 failed = false;
1372 checked {
1373 a = (ulong)d;
1375 } catch (OverflowException) {
1376 failed = true;
1378 if (failed)
1379 return 3;
1381 try {
1382 double d = -1;
1383 failed = true;
1384 checked {
1385 a = (ulong)d;
1387 } catch (OverflowException) {
1388 failed = false;
1390 if (failed)
1391 return 4;
1394 ulong i;
1395 float f = 1.1f;
1396 checked {
1397 i = (ulong) f;
1401 try {
1402 int i = -1;
1403 failed = true;
1404 checked {
1405 a = (ulong)i;
1408 catch (OverflowException) {
1409 failed = false;
1411 if (failed)
1412 return 5;
1414 try {
1415 int i = Int32.MinValue;
1416 failed = true;
1417 checked {
1418 a = (ulong)i;
1421 catch (OverflowException) {
1422 failed = false;
1424 if (failed)
1425 return 6;
1427 return 0;
1430 [Category ("!WASM")] // reported as https://github.com/kripken/emscripten/issues/5603
1431 public static int test_0_simple_double_casts () {
1433 double d = 0xffffffff;
1435 if ((uint)d != 4294967295)
1436 return 1;
1439 * These tests depend on properties of x86 fp arithmetic so they won't work
1440 * on other platforms.
1443 d = 0xffffffffffffffff;
1445 if ((ulong)d != 0)
1446 return 2;
1448 if ((ushort)d != 0)
1449 return 3;
1451 if ((byte)d != 0)
1452 return 4;
1455 d = 0xffff;
1457 if ((ushort)d != 0xffff)
1458 return 5;
1460 if ((byte)d != 0xff)
1461 return 6;
1463 return 0;
1466 public static int test_0_div_zero () {
1467 int d = 1;
1468 int q = 0;
1469 int val;
1470 bool failed;
1472 try {
1473 failed = true;
1474 val = d / q;
1475 } catch (DivideByZeroException) {
1476 failed = false;
1478 if (failed)
1479 return 1;
1481 try {
1482 failed = true;
1483 val = d % q;
1484 } catch (DivideByZeroException) {
1485 failed = false;
1487 if (failed)
1488 return 2;
1490 try {
1491 failed = true;
1492 q = -1;
1493 d = Int32.MinValue;
1494 val = d / q;
1495 } catch (DivideByZeroException) {
1496 /* wrong exception */
1497 } catch (OverflowException) {
1498 failed = false;
1500 if (failed)
1501 return 3;
1503 try {
1504 failed = true;
1505 q = -1;
1506 d = Int32.MinValue;
1507 val = d % q;
1508 } catch (DivideByZeroException) {
1509 /* wrong exception */
1510 } catch (OverflowException) {
1511 failed = false;
1513 if (failed)
1514 return 4;
1516 return 0;
1519 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1520 static void dummy () {
1523 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1524 static int div_zero_llvm_inner (int i) {
1525 try {
1526 // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1527 dummy ();
1528 return 5 / i;
1529 } catch (Exception ex) {
1530 return 0;
1534 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1535 static long div_zero_llvm_inner_long (long l) {
1536 try {
1537 dummy ();
1538 return (long)5 / l;
1539 } catch (Exception ex) {
1540 return 0;
1544 public static int test_0_div_zero_llvm () {
1545 long r = div_zero_llvm_inner (0);
1546 if (r != 0)
1547 return 1;
1548 r = div_zero_llvm_inner_long (0);
1549 if (r != 0)
1550 return 2;
1551 return 0;
1554 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1555 static int div_overflow_llvm_inner (int i) {
1556 try {
1557 dummy ();
1558 return Int32.MinValue / i;
1559 } catch (Exception ex) {
1560 return 0;
1564 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1565 static long div_overflow_llvm_inner_long (long l) {
1566 try {
1567 dummy ();
1568 return Int64.MinValue / l;
1569 } catch (Exception ex) {
1570 return 0;
1574 public static int test_0_div_overflow_llvm () {
1575 long r = div_overflow_llvm_inner (-1);
1576 if (r != 0)
1577 return 1;
1578 r = div_overflow_llvm_inner_long ((long)-1);
1579 if (r != 0)
1580 return 2;
1581 return 0;
1584 public static int return_55 () {
1585 return 55;
1588 public static int test_0_cfold_div_zero () {
1589 // Test that constant folding doesn't cause division by zero exceptions
1590 if (return_55 () != return_55 ()) {
1591 int d = 1;
1592 int q = 0;
1593 int val;
1595 val = d / q;
1596 val = d % q;
1598 q = -1;
1599 d = Int32.MinValue;
1600 val = d / q;
1602 q = -1;
1603 val = d % q;
1606 return 0;
1609 public static int test_0_udiv_zero () {
1610 uint d = 1;
1611 uint q = 0;
1612 uint val;
1613 bool failed;
1615 try {
1616 failed = true;
1617 val = d / q;
1618 } catch (DivideByZeroException) {
1619 failed = false;
1621 if (failed)
1622 return 1;
1624 try {
1625 failed = true;
1626 val = d % q;
1627 } catch (DivideByZeroException) {
1628 failed = false;
1630 if (failed)
1631 return 2;
1633 return 0;
1636 public static int test_0_long_div_zero () {
1637 long d = 1;
1638 long q = 0;
1639 long val;
1640 bool failed;
1642 try {
1643 failed = true;
1644 val = d / q;
1645 } catch (DivideByZeroException) {
1646 failed = false;
1648 if (failed)
1649 return 1;
1651 try {
1652 failed = true;
1653 val = d % q;
1654 } catch (DivideByZeroException) {
1655 failed = false;
1657 if (failed)
1658 return 2;
1660 try {
1661 failed = true;
1662 q = -1;
1663 d = Int64.MinValue;
1664 val = d / q;
1665 } catch (DivideByZeroException) {
1666 /* wrong exception */
1667 } catch (OverflowException) {
1668 failed = false;
1670 if (failed)
1671 return 3;
1673 try {
1674 failed = true;
1675 q = -1;
1676 d = Int64.MinValue;
1677 val = d % q;
1678 } catch (DivideByZeroException) {
1679 /* wrong exception */
1680 } catch (OverflowException) {
1681 failed = false;
1683 if (failed)
1684 return 4;
1686 return 0;
1689 public static int test_0_ulong_div_zero () {
1690 ulong d = 1;
1691 ulong q = 0;
1692 ulong val;
1693 bool failed;
1695 try {
1696 failed = true;
1697 val = d / q;
1698 } catch (DivideByZeroException) {
1699 failed = false;
1701 if (failed)
1702 return 1;
1704 try {
1705 failed = true;
1706 val = d % q;
1707 } catch (DivideByZeroException) {
1708 failed = false;
1710 if (failed)
1711 return 2;
1713 return 0;
1716 public static int test_0_float_div_zero () {
1717 double d = 1;
1718 double q = 0;
1719 double val;
1720 bool failed;
1722 try {
1723 failed = false;
1724 val = d / q;
1725 } catch (DivideByZeroException) {
1726 failed = true;
1728 if (failed)
1729 return 1;
1731 try {
1732 failed = false;
1733 val = d % q;
1734 } catch (DivideByZeroException) {
1735 failed = true;
1737 if (failed)
1738 return 2;
1740 return 0;
1743 public static int test_0_invalid_unbox () {
1745 int i = 123;
1746 object o = "Some string";
1747 int res = 1;
1749 try {
1750 // Illegal conversion; o contains a string not an int
1751 i = (int) o;
1752 } catch (Exception e) {
1753 if (i ==123)
1754 res = 0;
1757 return res;
1760 // Test that double[] can't be cast to double (bug #46027)
1761 public static int test_0_invalid_unbox_arrays () {
1762 double[] d1 = { 1.0 };
1763 double[][] d2 = { d1 };
1764 Array a = d2;
1766 try {
1767 foreach (double d in a) {
1769 return 1;
1771 catch (InvalidCastException e) {
1772 return 0;
1776 /* bug# 42190, at least mcs generates a leave for the return that
1777 * jumps out of multiple exception clauses: we used to execute just
1778 * one enclosing finally block.
1780 public static int finally_level;
1781 static void do_something () {
1782 int a = 0;
1783 try {
1784 try {
1785 return;
1786 } finally {
1787 a = 1;
1789 } finally {
1790 finally_level++;
1794 public static int test_2_multiple_finally_clauses () {
1795 finally_level = 0;
1796 do_something ();
1797 if (finally_level == 1)
1798 return 2;
1799 return 0;
1802 public static int test_3_checked_cast_un () {
1803 ulong i = 0x8000000034000000;
1804 long j;
1806 try {
1807 checked { j = (long)i; }
1808 } catch (OverflowException) {
1809 j = 2;
1812 if (j != 2)
1813 return 0;
1814 return 3;
1817 public static int test_4_checked_cast () {
1818 long i;
1819 ulong j;
1821 unchecked { i = (long)0x8000000034000000;};
1822 try {
1823 checked { j = (ulong)i; }
1824 } catch (OverflowException) {
1825 j = 3;
1828 if (j != 3)
1829 return 0;
1830 return 4;
1833 static readonly int[] mul_dim_results = new int[] {
1834 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1835 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1836 2, 0, 2, 1, 2, 8,
1837 3, 0, 3, 1, 3, 8,
1838 4, 0, 4, 1, 4, 8,
1839 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1840 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1841 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1844 public static int test_0_multi_dim_array_access () {
1845 int [,] a = System.Array.CreateInstance (typeof (int),
1846 new int [] {3,6}, new int [] {2,2 }) as int[,];
1847 int x, y;
1848 int result_idx = 0;
1849 for (x = 0; x < 8; ++x) {
1850 for (y = 0; y < 9; ++y) {
1851 bool got_ex = false;
1852 try {
1853 a [x, y] = 1;
1854 } catch {
1855 got_ex = true;
1857 if (got_ex) {
1858 if (result_idx >= mul_dim_results.Length)
1859 return -1;
1860 if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1861 return result_idx + 1;
1863 result_idx += 2;
1867 if (result_idx == mul_dim_results.Length)
1868 return 0;
1869 return 200;
1872 static void helper_out_obj (out object o) {
1873 o = (object)"buddy";
1876 static void helper_out_string (out string o) {
1877 o = "buddy";
1880 public static int test_2_array_mismatch () {
1881 string[] a = { "hello", "world" };
1882 object[] b = a;
1883 bool passed = false;
1885 try {
1886 helper_out_obj (out b [1]);
1887 } catch (ArrayTypeMismatchException) {
1888 passed = true;
1890 if (!passed)
1891 return 0;
1892 helper_out_string (out a [1]);
1893 if (a [1] != "buddy")
1894 return 1;
1895 return 2;
1898 public static int test_0_ovf1 () {
1899 int exception = 0;
1901 checked {
1902 try {
1903 ulong a = UInt64.MaxValue - 1;
1904 ulong t = a++;
1905 } catch {
1906 exception = 1;
1909 return exception;
1912 public static int test_1_ovf2 () {
1913 int exception = 0;
1915 checked {
1916 try {
1917 ulong a = UInt64.MaxValue;
1918 ulong t = a++;
1919 } catch {
1920 exception = 1;
1923 return exception;
1926 public static int test_0_ovf3 () {
1927 int exception = 0;
1929 long a = Int64.MaxValue - 1;
1930 checked {
1931 try {
1932 long t = a++;
1933 } catch {
1934 exception = 1;
1937 return exception;
1940 public static int test_1_ovf4 () {
1941 int exception = 0;
1943 long a = Int64.MaxValue;
1944 checked {
1945 try {
1946 long t = a++;
1947 } catch {
1948 exception = 1;
1951 return exception;
1954 public static int test_0_ovf5 () {
1955 int exception = 0;
1957 ulong a = UInt64.MaxValue - 1;
1958 checked {
1959 try {
1960 ulong t = a++;
1961 } catch {
1962 exception = 1;
1965 return exception;
1968 public static int test_1_ovf6 () {
1969 int exception = 0;
1971 ulong a = UInt64.MaxValue;
1972 checked {
1973 try {
1974 ulong t = a++;
1975 } catch {
1976 exception = 1;
1979 return exception;
1982 public static int test_0_ovf7 () {
1983 int exception = 0;
1985 long a = Int64.MinValue + 1;
1986 checked {
1987 try {
1988 long t = a--;
1989 } catch {
1990 exception = 1;
1993 return 0;
1996 public static int test_1_ovf8 () {
1997 int exception = 0;
1999 long a = Int64.MinValue;
2000 checked {
2001 try {
2002 long t = a--;
2003 } catch {
2004 exception = 1;
2007 return exception;
2010 public static int test_0_ovf9 () {
2011 int exception = 0;
2013 ulong a = UInt64.MinValue + 1;
2014 checked {
2015 try {
2016 ulong t = a--;
2017 } catch {
2018 exception = 1;
2021 return exception;
2024 public static int test_1_ovf10 () {
2025 int exception = 0;
2027 ulong a = UInt64.MinValue;
2028 checked {
2029 try {
2030 ulong t = a--;
2031 } catch {
2032 exception = 1;
2035 return exception;
2038 public static int test_0_ovf11 () {
2039 int exception = 0;
2041 int a = Int32.MinValue + 1;
2042 checked {
2043 try {
2044 int t = a--;
2045 } catch {
2046 exception = 1;
2049 return exception;
2052 public static int test_1_ovf12 () {
2053 int exception = 0;
2055 int a = Int32.MinValue;
2056 checked {
2057 try {
2058 int t = a--;
2059 } catch {
2060 exception = 1;
2063 return exception;
2066 public static int test_0_ovf13 () {
2067 int exception = 0;
2069 uint a = 1;
2070 checked {
2071 try {
2072 uint t = a--;
2073 } catch {
2074 exception = 1;
2077 return exception;
2080 public static int test_1_ovf14 () {
2081 int exception = 0;
2083 uint a = 0;
2084 checked {
2085 try {
2086 uint t = a--;
2087 } catch {
2088 exception = 1;
2091 return exception;
2094 public static int test_0_ovf15 () {
2095 int exception = 0;
2097 sbyte a = 126;
2098 checked {
2099 try {
2100 sbyte t = a++;
2101 } catch {
2102 exception = 1;
2105 return exception;
2108 public static int test_1_ovf16 () {
2109 int exception = 0;
2111 sbyte a = 127;
2112 checked {
2113 try {
2114 sbyte t = a++;
2115 } catch {
2116 exception = 1;
2119 return exception;
2122 public static int test_0_ovf17 () {
2123 int exception = 0;
2125 checked {
2126 try {
2127 } catch {
2128 exception = 1;
2131 return exception;
2134 public static int test_0_ovf18 () {
2135 int exception = 0;
2137 int a = 1 << 29;
2138 checked {
2139 try {
2140 int t = a*2;
2141 } catch {
2142 exception = 1;
2145 return exception;
2148 public static int test_1_ovf19 () {
2149 int exception = 0;
2151 int a = 1 << 30;
2152 checked {
2153 try {
2154 int t = a*2;
2155 } catch {
2156 exception = 1;
2159 return exception;
2162 public static int test_0_ovf20 () {
2163 int exception = 0;
2165 checked {
2166 try {
2167 ulong a = 0xffffffffff;
2168 ulong t = a*0x0ffffff;
2169 } catch {
2170 exception = 1;
2173 return exception;
2176 public static int test_1_ovf21 () {
2177 int exception = 0;
2179 ulong a = 0xffffffffff;
2180 checked {
2181 try {
2182 ulong t = a*0x0fffffff;
2183 } catch {
2184 exception = 1;
2187 return exception;
2190 public static int test_1_ovf22 () {
2191 int exception = 0;
2193 long a = Int64.MinValue;
2194 long b = 10;
2195 checked {
2196 try {
2197 long v = a * b;
2198 } catch {
2199 exception = 1;
2202 return exception;
2205 public static int test_1_ovf23 () {
2206 int exception = 0;
2208 long a = 10;
2209 long b = Int64.MinValue;
2210 checked {
2211 try {
2212 long v = a * b;
2213 } catch {
2214 exception = 1;
2217 return exception;
2220 class Broken {
2221 public static int i;
2223 static Broken () {
2224 throw new Exception ("Ugh!");
2227 public static int DoSomething () {
2228 return i;
2232 public static int test_0_exception_in_cctor () {
2233 try {
2234 Broken.DoSomething ();
2236 catch (TypeInitializationException) {
2237 // This will only happen once even if --regression is used
2239 return 0;
2242 public static int test_5_regalloc () {
2243 int i = 0;
2245 try {
2246 for (i = 0; i < 10; ++i) {
2247 if (i == 5)
2248 throw new Exception ();
2251 catch (Exception) {
2252 if (i != 5)
2253 return i;
2256 // Check that variables written in catch clauses are volatile
2257 int j = 0;
2258 try {
2259 throw new Exception ();
2261 catch (Exception) {
2262 j = 5;
2264 if (j != 5)
2265 return 6;
2267 int k = 0;
2268 try {
2269 try {
2270 throw new Exception ();
2272 finally {
2273 k = 5;
2276 catch (Exception) {
2278 if (k != 5)
2279 return 7;
2281 return i;
2284 public static void rethrow () {
2285 try {
2286 throw new ApplicationException();
2287 } catch (ApplicationException) {
2288 try {
2289 throw new OverflowException();
2290 } catch (Exception) {
2291 throw;
2296 // Test that a rethrow rethrows the correct exception
2297 public static int test_0_rethrow_nested () {
2298 try {
2299 rethrow ();
2300 } catch (OverflowException) {
2301 return 0;
2302 } catch (Exception) {
2303 return 1;
2305 return 2;
2308 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2309 public static void rethrow1 () {
2310 throw new Exception ();
2313 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2314 public static void rethrow2 () {
2315 rethrow1 ();
2316 /* This disables tailcall opts */
2317 Console.WriteLine ();
2320 [Category ("!BITCODE")]
2321 public static int test_0_rethrow_stacktrace () {
2322 // Check that rethrowing an exception preserves the original stack trace
2323 try {
2324 try {
2325 rethrow2 ();
2327 catch (Exception ex) {
2328 // Check that each catch clause has its own exception variable
2329 // If not, the throw below will overwrite the exception used
2330 // by the rethrow
2331 try {
2332 throw new DivideByZeroException ();
2334 catch (Exception foo) {
2337 throw;
2340 catch (Exception ex) {
2341 if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2342 return 0;
2345 return 1;
2348 interface IFace {}
2349 class Face : IFace {}
2351 public static int test_1_array_mismatch_2 () {
2352 try {
2353 object [] o = new Face [1];
2354 o [0] = 1;
2355 return 0;
2356 } catch (ArrayTypeMismatchException) {
2357 return 1;
2361 public static int test_1_array_mismatch_3 () {
2362 try {
2363 object [] o = new IFace [1];
2364 o [0] = 1;
2365 return 0;
2366 } catch (ArrayTypeMismatchException) {
2367 return 1;
2371 public static int test_1_array_mismatch_4 () {
2372 try {
2373 object [][] o = new Face [5] [];
2374 o [0] = new object [5];
2376 return 0;
2377 } catch (ArrayTypeMismatchException) {
2378 return 1;
2382 public static int test_0_array_size () {
2383 bool failed;
2385 try {
2386 failed = true;
2387 int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2389 catch (OutOfMemoryException e) {
2390 failed = false;
2392 if (failed)
2393 return 2;
2395 return 0;
2398 struct S {
2399 int i, j, k, l, m, n;
2402 static IntPtr[] addr;
2404 static unsafe void throw_func (int i, S s) {
2405 addr [i] = new IntPtr (&i);
2406 throw new Exception ();
2409 /* Test that arguments are correctly popped off the stack during unwinding */
2410 /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2412 public static int test_0_stack_unwind () {
2413 addr = new IntPtr [1000];
2414 S s = new S ();
2415 for (int j = 0; j < 1000; j++) {
2416 try {
2417 throw_func (j, s);
2419 catch (Exception) {
2422 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2426 static unsafe void get_sp (int i) {
2427 addr [i] = new IntPtr (&i);
2430 /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2431 public static int test_0_throw_unwind () {
2432 addr = new IntPtr [1000];
2433 S s = new S ();
2434 for (int j = 0; j < 1000; j++) {
2435 try {
2436 get_sp (j);
2437 throw new Exception ();
2439 catch (Exception) {
2442 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2445 public static int test_0_regress_73242 () {
2446 int [] arr = new int [10];
2447 for (int i = 0; i < 10; ++i)
2448 arr [i] = 0;
2449 try {
2450 throw new Exception ();
2452 catch {
2454 return 0;
2457 public static int test_0_nullref () {
2458 try {
2459 Array foo = null;
2460 foo.Clone();
2461 } catch (NullReferenceException e) {
2462 return 0;
2464 return 1;
2467 public int amethod () {
2468 return 1;
2471 public static int test_0_nonvirt_nullref_at_clause_start () {
2472 ExceptionTests t = null;
2473 try {
2474 t.amethod ();
2475 } catch (NullReferenceException) {
2476 return 0;
2479 return 1;
2482 public static int throw_only () {
2483 throw new Exception ();
2486 [MethodImpl(MethodImplOptions.NoInlining)]
2487 public static int throw_only2 () {
2488 return throw_only ();
2491 public static int test_0_inline_throw_only () {
2492 try {
2493 return throw_only2 ();
2495 catch (Exception ex) {
2496 return 0;
2500 public static string GetText (string s) {
2501 return s;
2504 public static int throw_only_gettext () {
2505 throw new Exception (GetText ("FOO"));
2508 public static int test_0_inline_throw_only_gettext () {
2509 object o = null;
2510 try {
2511 o = throw_only_gettext ();
2513 catch (Exception ex) {
2514 return 0;
2517 return o != null ? 0 : 1;
2520 // bug #78633
2521 public static int test_0_throw_to_branch_opt_outer_clause () {
2522 int i = 0;
2524 try {
2525 try {
2526 string [] files = new string[1];
2528 string s = files[2];
2529 } finally {
2530 i ++;
2532 } catch {
2535 return (i == 1) ? 0 : 1;
2538 // bug #485721
2539 public static int test_0_try_inside_finally_cmov_opt () {
2540 bool Reconect = false;
2542 object o = new object ();
2544 try {
2546 catch (Exception ExCon) {
2547 if (o != null)
2548 Reconect = true;
2550 try {
2552 catch (Exception Last) {
2555 finally {
2556 if (Reconect == true) {
2557 try {
2559 catch (Exception ex) {
2564 return 0;
2567 public static int test_0_inline_throw () {
2568 try {
2569 inline_throw1 (5);
2570 return 1;
2571 } catch {
2572 return 0;
2576 // for llvm, the end bblock is unreachable
2577 public static int inline_throw1 (int i) {
2578 if (i == 0)
2579 throw new Exception ();
2580 else
2581 return inline_throw2 (i);
2584 public static int inline_throw2 (int i) {
2585 throw new Exception ();
2588 // bug #539550
2589 public static int test_0_lmf_filter () {
2590 try {
2591 // The invoke calls a runtime-invoke wrapper which has a filter clause
2592 #if __MOBILE__
2593 typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2594 #else
2595 typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2596 #endif
2597 } catch (TargetInvocationException) {
2599 return 0;
2602 public static void lmf_filter () {
2603 try {
2604 Connect ();
2606 catch {
2607 throw new NotImplementedException ();
2611 public static void Connect () {
2612 Stop ();
2613 throw new Exception();
2616 public static void Stop () {
2617 try {
2618 lock (null) {}
2620 catch {
2624 private static void do_raise () {
2625 throw new System.Exception ();
2628 private static int int_func (int i) {
2629 return i;
2632 // #559876
2633 public static int test_8_local_deadce_causes () {
2634 int myb = 4;
2636 try {
2637 myb = int_func (8);
2638 do_raise();
2639 myb = int_func (2);
2640 } catch (System.Exception) {
2641 return myb;
2643 return 0;
2646 public static int test_0_except_opt_two_clauses () {
2647 int size;
2648 size = -1;
2649 uint ui = (uint)size;
2650 try {
2651 checked {
2652 uint v = ui * (uint)4;
2654 } catch (OverflowException e) {
2655 return 0;
2656 } catch (Exception) {
2657 return 1;
2660 return 2;
2663 class Child
2665 public virtual long Method()
2667 throw new Exception();
2671 /* #612206 */
2672 public static int test_100_long_vars_in_clauses_initlocals_opt () {
2673 Child c = new Child();
2674 long value = 100;
2675 try {
2676 value = c.Method();
2678 catch {}
2679 return (int)value;
2682 class A {
2683 public object AnObj;
2686 public static void DoSomething (ref object o) {
2689 public static int test_0_ldflda_null () {
2690 A a = null;
2692 try {
2693 DoSomething (ref a.AnObj);
2694 } catch (NullReferenceException) {
2695 return 0;
2698 return 1;
2701 unsafe struct Foo
2703 public int i;
2705 public static Foo* pFoo;
2708 /* MS.NET doesn't seem to throw in this case */
2709 public unsafe static int test_0_ldflda_null_pointer () {
2710 int* pi = &Foo.pFoo->i;
2712 return 0;
2715 static int test_0_try_clause_in_finally_clause_regalloc () {
2716 // Fill up registers with values
2717 object a = new object ();
2718 object[] arr1 = new object [1];
2719 object[] arr2 = new object [1];
2720 object[] arr3 = new object [1];
2721 object[] arr4 = new object [1];
2722 object[] arr5 = new object [1];
2724 for (int i = 0; i < 10; ++i)
2725 arr1 [0] = a;
2726 for (int i = 0; i < 10; ++i)
2727 arr2 [0] = a;
2728 for (int i = 0; i < 10; ++i)
2729 arr3 [0] = a;
2730 for (int i = 0; i < 10; ++i)
2731 arr4 [0] = a;
2732 for (int i = 0; i < 10; ++i)
2733 arr5 [0] = a;
2735 int res = 1;
2736 try {
2737 try_clause_in_finally_clause_regalloc_inner (out res);
2738 } catch (Exception) {
2740 return res;
2743 public static object Throw () {
2744 for (int i = 0; i < 10; ++i)
2746 throw new Exception ();
2749 static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2750 object o = null;
2752 res = 1;
2753 try {
2754 o = Throw ();
2755 } catch (Exception) {
2756 /* Make sure this doesn't branch to the finally */
2757 throw new DivideByZeroException ();
2758 } finally {
2759 try {
2760 /* Make sure o is register allocated */
2761 if (o == null)
2762 res = 0;
2763 else
2764 res = 1;
2765 if (o == null)
2766 res = 0;
2767 else
2768 res = 1;
2769 if (o == null)
2770 res = 0;
2771 else
2772 res = 1;
2773 } catch (DivideByZeroException) {
2778 public static bool t_1835_inner () {
2779 bool a = true;
2780 if (a) throw new Exception();
2781 return true;
2784 [MethodImpl(MethodImplOptions.NoInlining)]
2785 public static bool t_1835_inner_2 () {
2786 bool b = t_1835_inner ();
2787 return b;
2790 public static int test_0_inline_retval_throw_in_branch_1835 () {
2791 try {
2792 t_1835_inner_2 ();
2793 } catch {
2794 return 0;
2796 return 1;
2799 static bool finally_called = false;
2801 static void regress_30472 (int a, int b) {
2802 checked {
2803 try {
2804 int sum = a + b;
2805 } finally {
2806 finally_called = true;
2811 public static int test_0_regress_30472 () {
2812 finally_called = false;
2813 try {
2814 regress_30472 (Int32.MaxValue - 1, 2);
2815 } catch (Exception ex) {
2817 return finally_called ? 0 : 1;
2820 static int array_len_1 = 1;
2822 public static int test_0_bounds_check_negative_constant () {
2823 try {
2824 byte[] arr = new byte [array_len_1];
2825 byte b = arr [-1];
2826 return 1;
2827 } catch {
2829 try {
2830 byte[] arr = new byte [array_len_1];
2831 arr [-1] = 1;
2832 return 2;
2833 } catch {
2835 return 0;
2838 public static int test_0_string_bounds_check_negative_constant () {
2839 try {
2840 string s = "A";
2841 char c = s [-1];
2842 return 1;
2843 } catch {
2845 return 0;
2848 public class MyException : Exception {
2849 public int marker = 0;
2850 public string res = "";
2852 public MyException (String res) {
2853 this.res = res;
2856 public bool FilterWithoutState () {
2857 return this.marker == 0x666;
2860 public bool FilterWithState () {
2861 bool ret = this.marker == 0x566;
2862 this.marker += 0x100;
2863 return ret;
2866 public bool FilterWithStringState () {
2867 bool ret = this.marker == 0x777;
2868 this.res = "fromFilter_" + this.res;
2869 return ret;
2873 [Category ("!BITCODE")]
2874 public static int test_1_basic_filter_catch () {
2875 try {
2876 MyException e = new MyException ("");
2877 e.marker = 0x1337;
2878 throw e;
2879 } catch (MyException ex) when (ex.marker == 0x1337) {
2880 return 1;
2882 return 0;
2885 [Category ("!BITCODE")]
2886 public static int test_1234_complicated_filter_catch () {
2887 string res = "init";
2888 try {
2889 MyException e = new MyException (res);
2890 e.marker = 0x566;
2891 try {
2892 try {
2893 throw e;
2894 } catch (MyException ex) when (ex.FilterWithoutState ()) {
2895 res = "WRONG_" + res;
2896 } finally {
2897 e.marker = 0x777;
2898 res = "innerFinally_" + res;
2900 } catch (MyException ex) when (ex.FilterWithState ()) {
2901 res = "2ndcatch_" + res;
2903 // "2ndcatch_innerFinally_init"
2904 // Console.WriteLine ("res1: " + res);
2905 e.res = res;
2906 throw e;
2907 } catch (MyException ex) when (ex.FilterWithStringState ()) {
2908 res = "fwos_" + ex.res;
2909 } finally {
2910 res = "outerFinally_" + res;
2912 // Console.WriteLine ("res2: " + res);
2913 return "outerFinally_fwos_fromFilter_2ndcatch_innerFinally_init" == res ? 1234 : 0;
2916 public struct FooStruct
2918 public long Part1 { get; }
2919 public long Part2 { get; }
2921 public byte Part3 { get; }
2924 [MethodImpl( MethodImplOptions.NoInlining )]
2925 private static bool ExceptionFilter( byte x, FooStruct item ) => true;
2927 [Category ("!BITCODE")]
2928 public static int test_0_filter_caller_area () {
2929 try {
2930 throw new Exception();
2932 catch (Exception) when (ExceptionFilter (default(byte), default (FooStruct))) {
2934 return 0;
2937 public static int test_0_signed_ct_div () {
2938 int n = 2147483647;
2939 bool divide_by_zero = false;
2940 bool overflow = false;
2942 n = -n;
2943 n--; /* MinValue */
2944 try {
2945 int r = n / (-1);
2946 } catch (OverflowException) {
2947 overflow = true;
2949 if (!overflow)
2950 return 7;
2952 try {
2953 int r = n / 0;
2954 } catch (DivideByZeroException) {
2955 divide_by_zero = true;
2957 if (!divide_by_zero)
2958 return 8;
2960 if ((n / 35) != -61356675)
2961 return 9;
2962 if ((n / -35) != 61356675)
2963 return 10;
2964 n = -(n + 1); /* MaxValue */
2965 if ((n / 35) != 61356675)
2966 return 11;
2967 if ((n / -35) != -61356675)
2968 return 12;
2970 return 0;
2973 public static int test_0_unsigned_ct_div () {
2974 uint n = 4294967295;
2975 bool divide_by_zero = false;
2977 try {
2978 uint a = n / 0;
2979 } catch (DivideByZeroException) {
2980 divide_by_zero = true;
2983 if (!divide_by_zero)
2984 return 5;
2986 if ((n / 35) != 122713351)
2987 return 9;
2989 return 0;
2993 #if !__MOBILE__
2994 class ExceptionTests : Tests
2997 #endif