Propagate error in mono_unicode_to_external (#14879)
[mono-project.git] / mono / mini / exceptions.cs
blob5db7e5e6f308883d9da3ba28a926720105a78dfd
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 /* Github issue 13284 */
1335 public static int test_0_ulong_ovf_spilling () {
1336 checked {
1337 ulong x = 2UL;
1338 ulong y = 1UL;
1339 ulong z = 3UL;
1340 ulong t = x - y;
1342 try {
1343 var a = x - y >= z;
1344 if (a)
1345 return 1;
1346 // Console.WriteLine ($"u64 ({x} - {y} >= {z}) => {a} [{(a == false ? "OK" : "NG")}]");
1347 } catch (OverflowException) {
1348 return 2;
1349 // Console.WriteLine ($"u64 ({x} - {y} >= {z}) => overflow [NG]");
1352 try {
1353 var a = t >= z;
1354 if (a)
1355 return 3;
1356 // Console.WriteLine ($"u64 ({t} >= {z}) => {a} [{(a == false ? "OK" : "NG")}]");
1357 } catch (OverflowException) {
1358 return 4;
1359 // Console.WriteLine ($"u64 ({t} >= {z}) => overflow [NG]");
1362 try {
1363 var a = x - y - z >= 0;
1364 if (a)
1365 return 5;
1366 else
1367 return 6;
1368 // Console.WriteLine ($"u64 ({x} - {y} - {z} >= 0) => {a} [NG]");
1369 } catch (OverflowException) {
1370 return 0;
1371 // Console.WriteLine ($"u64 ({x} - {y} - {z} >= 0) => overflow [OK]");
1376 public static int test_0_ulong_cast () {
1377 ulong a;
1378 bool failed;
1381 * These tests depend on properties of x86 fp arithmetic so they won't work
1382 * on other platforms.
1386 try {
1387 double d = System.UInt64.MaxValue - 1024.0;
1388 failed = true;
1389 checked {
1390 a = (ulong)d;
1392 } catch (OverflowException) {
1393 failed = false;
1395 if (failed)
1396 return 1;
1398 try {
1399 double d = System.UInt64.MaxValue - 1025.0;
1400 failed = false;
1401 checked {
1402 a = (ulong)d;
1404 } catch (OverflowException) {
1405 failed = true;
1407 if (failed)
1408 return 2;
1411 try {
1412 double d = 0;
1413 failed = false;
1414 checked {
1415 a = (ulong)d;
1417 } catch (OverflowException) {
1418 failed = true;
1420 if (failed)
1421 return 3;
1423 try {
1424 double d = -1;
1425 failed = true;
1426 checked {
1427 a = (ulong)d;
1429 } catch (OverflowException) {
1430 failed = false;
1432 if (failed)
1433 return 4;
1436 ulong i;
1437 float f = 1.1f;
1438 checked {
1439 i = (ulong) f;
1443 try {
1444 int i = -1;
1445 failed = true;
1446 checked {
1447 a = (ulong)i;
1450 catch (OverflowException) {
1451 failed = false;
1453 if (failed)
1454 return 5;
1456 try {
1457 int i = Int32.MinValue;
1458 failed = true;
1459 checked {
1460 a = (ulong)i;
1463 catch (OverflowException) {
1464 failed = false;
1466 if (failed)
1467 return 6;
1469 return 0;
1472 [Category ("!WASM")] // reported as https://github.com/kripken/emscripten/issues/5603
1473 public static int test_0_simple_double_casts () {
1475 double d = 0xffffffff;
1477 if ((uint)d != 4294967295)
1478 return 1;
1481 * These tests depend on properties of x86 fp arithmetic so they won't work
1482 * on other platforms.
1485 d = 0xffffffffffffffff;
1487 if ((ulong)d != 0)
1488 return 2;
1490 if ((ushort)d != 0)
1491 return 3;
1493 if ((byte)d != 0)
1494 return 4;
1497 d = 0xffff;
1499 if ((ushort)d != 0xffff)
1500 return 5;
1502 if ((byte)d != 0xff)
1503 return 6;
1505 return 0;
1508 public static int test_0_div_zero () {
1509 int d = 1;
1510 int q = 0;
1511 int val;
1512 bool failed;
1514 try {
1515 failed = true;
1516 val = d / q;
1517 } catch (DivideByZeroException) {
1518 failed = false;
1520 if (failed)
1521 return 1;
1523 try {
1524 failed = true;
1525 val = d % q;
1526 } catch (DivideByZeroException) {
1527 failed = false;
1529 if (failed)
1530 return 2;
1532 try {
1533 failed = true;
1534 q = -1;
1535 d = Int32.MinValue;
1536 val = d / q;
1537 } catch (DivideByZeroException) {
1538 /* wrong exception */
1539 } catch (OverflowException) {
1540 failed = false;
1542 if (failed)
1543 return 3;
1545 try {
1546 failed = true;
1547 q = -1;
1548 d = Int32.MinValue;
1549 val = d % q;
1550 } catch (DivideByZeroException) {
1551 /* wrong exception */
1552 } catch (OverflowException) {
1553 failed = false;
1555 if (failed)
1556 return 4;
1558 return 0;
1561 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1562 static void dummy () {
1565 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1566 static int div_zero_llvm_inner (int i) {
1567 try {
1568 // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1569 dummy ();
1570 return 5 / i;
1571 } catch (Exception ex) {
1572 return 0;
1576 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1577 static long div_zero_llvm_inner_long (long l) {
1578 try {
1579 dummy ();
1580 return (long)5 / l;
1581 } catch (Exception ex) {
1582 return 0;
1586 public static int test_0_div_zero_llvm () {
1587 long r = div_zero_llvm_inner (0);
1588 if (r != 0)
1589 return 1;
1590 r = div_zero_llvm_inner_long (0);
1591 if (r != 0)
1592 return 2;
1593 return 0;
1596 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1597 static int div_overflow_llvm_inner (int i) {
1598 try {
1599 dummy ();
1600 return Int32.MinValue / i;
1601 } catch (Exception ex) {
1602 return 0;
1606 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1607 static long div_overflow_llvm_inner_long (long l) {
1608 try {
1609 dummy ();
1610 return Int64.MinValue / l;
1611 } catch (Exception ex) {
1612 return 0;
1616 public static int test_0_div_overflow_llvm () {
1617 long r = div_overflow_llvm_inner (-1);
1618 if (r != 0)
1619 return 1;
1620 r = div_overflow_llvm_inner_long ((long)-1);
1621 if (r != 0)
1622 return 2;
1623 return 0;
1626 public static int return_55 () {
1627 return 55;
1630 public static int test_0_cfold_div_zero () {
1631 // Test that constant folding doesn't cause division by zero exceptions
1632 if (return_55 () != return_55 ()) {
1633 int d = 1;
1634 int q = 0;
1635 int val;
1637 val = d / q;
1638 val = d % q;
1640 q = -1;
1641 d = Int32.MinValue;
1642 val = d / q;
1644 q = -1;
1645 val = d % q;
1648 return 0;
1651 public static int test_0_udiv_zero () {
1652 uint d = 1;
1653 uint q = 0;
1654 uint val;
1655 bool failed;
1657 try {
1658 failed = true;
1659 val = d / q;
1660 } catch (DivideByZeroException) {
1661 failed = false;
1663 if (failed)
1664 return 1;
1666 try {
1667 failed = true;
1668 val = d % q;
1669 } catch (DivideByZeroException) {
1670 failed = false;
1672 if (failed)
1673 return 2;
1675 return 0;
1678 public static int test_0_long_div_zero () {
1679 long d = 1;
1680 long q = 0;
1681 long val;
1682 bool failed;
1684 try {
1685 failed = true;
1686 val = d / q;
1687 } catch (DivideByZeroException) {
1688 failed = false;
1690 if (failed)
1691 return 1;
1693 try {
1694 failed = true;
1695 val = d % q;
1696 } catch (DivideByZeroException) {
1697 failed = false;
1699 if (failed)
1700 return 2;
1702 try {
1703 failed = true;
1704 q = -1;
1705 d = Int64.MinValue;
1706 val = d / q;
1707 } catch (DivideByZeroException) {
1708 /* wrong exception */
1709 } catch (OverflowException) {
1710 failed = false;
1712 if (failed)
1713 return 3;
1715 try {
1716 failed = true;
1717 q = -1;
1718 d = Int64.MinValue;
1719 val = d % q;
1720 } catch (DivideByZeroException) {
1721 /* wrong exception */
1722 } catch (OverflowException) {
1723 failed = false;
1725 if (failed)
1726 return 4;
1728 return 0;
1731 public static int test_0_ulong_div_zero () {
1732 ulong d = 1;
1733 ulong q = 0;
1734 ulong val;
1735 bool failed;
1737 try {
1738 failed = true;
1739 val = d / q;
1740 } catch (DivideByZeroException) {
1741 failed = false;
1743 if (failed)
1744 return 1;
1746 try {
1747 failed = true;
1748 val = d % q;
1749 } catch (DivideByZeroException) {
1750 failed = false;
1752 if (failed)
1753 return 2;
1755 return 0;
1758 public static int test_0_float_div_zero () {
1759 double d = 1;
1760 double q = 0;
1761 double val;
1762 bool failed;
1764 try {
1765 failed = false;
1766 val = d / q;
1767 } catch (DivideByZeroException) {
1768 failed = true;
1770 if (failed)
1771 return 1;
1773 try {
1774 failed = false;
1775 val = d % q;
1776 } catch (DivideByZeroException) {
1777 failed = true;
1779 if (failed)
1780 return 2;
1782 return 0;
1785 public static int test_0_invalid_unbox () {
1787 int i = 123;
1788 object o = "Some string";
1789 int res = 1;
1791 try {
1792 // Illegal conversion; o contains a string not an int
1793 i = (int) o;
1794 } catch (Exception e) {
1795 if (i ==123)
1796 res = 0;
1799 return res;
1802 // Test that double[] can't be cast to double (bug #46027)
1803 public static int test_0_invalid_unbox_arrays () {
1804 double[] d1 = { 1.0 };
1805 double[][] d2 = { d1 };
1806 Array a = d2;
1808 try {
1809 foreach (double d in a) {
1811 return 1;
1813 catch (InvalidCastException e) {
1814 return 0;
1818 /* bug# 42190, at least mcs generates a leave for the return that
1819 * jumps out of multiple exception clauses: we used to execute just
1820 * one enclosing finally block.
1822 public static int finally_level;
1823 static void do_something () {
1824 int a = 0;
1825 try {
1826 try {
1827 return;
1828 } finally {
1829 a = 1;
1831 } finally {
1832 finally_level++;
1836 public static int test_2_multiple_finally_clauses () {
1837 finally_level = 0;
1838 do_something ();
1839 if (finally_level == 1)
1840 return 2;
1841 return 0;
1844 public static int test_3_checked_cast_un () {
1845 ulong i = 0x8000000034000000;
1846 long j;
1848 try {
1849 checked { j = (long)i; }
1850 } catch (OverflowException) {
1851 j = 2;
1854 if (j != 2)
1855 return 0;
1856 return 3;
1859 public static int test_4_checked_cast () {
1860 long i;
1861 ulong j;
1863 unchecked { i = (long)0x8000000034000000;};
1864 try {
1865 checked { j = (ulong)i; }
1866 } catch (OverflowException) {
1867 j = 3;
1870 if (j != 3)
1871 return 0;
1872 return 4;
1875 static readonly int[] mul_dim_results = new int[] {
1876 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1877 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1878 2, 0, 2, 1, 2, 8,
1879 3, 0, 3, 1, 3, 8,
1880 4, 0, 4, 1, 4, 8,
1881 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1882 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1883 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1886 public static int test_0_multi_dim_array_access () {
1887 int [,] a = System.Array.CreateInstance (typeof (int),
1888 new int [] {3,6}, new int [] {2,2 }) as int[,];
1889 int x, y;
1890 int result_idx = 0;
1891 for (x = 0; x < 8; ++x) {
1892 for (y = 0; y < 9; ++y) {
1893 bool got_ex = false;
1894 try {
1895 a [x, y] = 1;
1896 } catch {
1897 got_ex = true;
1899 if (got_ex) {
1900 if (result_idx >= mul_dim_results.Length)
1901 return -1;
1902 if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1903 return result_idx + 1;
1905 result_idx += 2;
1909 if (result_idx == mul_dim_results.Length)
1910 return 0;
1911 return 200;
1914 static void helper_out_obj (out object o) {
1915 o = (object)"buddy";
1918 static void helper_out_string (out string o) {
1919 o = "buddy";
1922 public static int test_2_array_mismatch () {
1923 string[] a = { "hello", "world" };
1924 object[] b = a;
1925 bool passed = false;
1927 try {
1928 helper_out_obj (out b [1]);
1929 } catch (ArrayTypeMismatchException) {
1930 passed = true;
1932 if (!passed)
1933 return 0;
1934 helper_out_string (out a [1]);
1935 if (a [1] != "buddy")
1936 return 1;
1937 return 2;
1940 public static int test_0_ovf1 () {
1941 int exception = 0;
1943 checked {
1944 try {
1945 ulong a = UInt64.MaxValue - 1;
1946 ulong t = a++;
1947 } catch {
1948 exception = 1;
1951 return exception;
1954 public static int test_1_ovf2 () {
1955 int exception = 0;
1957 checked {
1958 try {
1959 ulong a = UInt64.MaxValue;
1960 ulong t = a++;
1961 } catch {
1962 exception = 1;
1965 return exception;
1968 public static int test_0_ovf3 () {
1969 int exception = 0;
1971 long a = Int64.MaxValue - 1;
1972 checked {
1973 try {
1974 long t = a++;
1975 } catch {
1976 exception = 1;
1979 return exception;
1982 public static int test_1_ovf4 () {
1983 int exception = 0;
1985 long a = Int64.MaxValue;
1986 checked {
1987 try {
1988 long t = a++;
1989 } catch {
1990 exception = 1;
1993 return exception;
1996 public static int test_0_ovf5 () {
1997 int exception = 0;
1999 ulong a = UInt64.MaxValue - 1;
2000 checked {
2001 try {
2002 ulong t = a++;
2003 } catch {
2004 exception = 1;
2007 return exception;
2010 public static int test_1_ovf6 () {
2011 int exception = 0;
2013 ulong a = UInt64.MaxValue;
2014 checked {
2015 try {
2016 ulong t = a++;
2017 } catch {
2018 exception = 1;
2021 return exception;
2024 public static int test_0_ovf7 () {
2025 int exception = 0;
2027 long a = Int64.MinValue + 1;
2028 checked {
2029 try {
2030 long t = a--;
2031 } catch {
2032 exception = 1;
2035 return 0;
2038 public static int test_1_ovf8 () {
2039 int exception = 0;
2041 long a = Int64.MinValue;
2042 checked {
2043 try {
2044 long t = a--;
2045 } catch {
2046 exception = 1;
2049 return exception;
2052 public static int test_0_ovf9 () {
2053 int exception = 0;
2055 ulong a = UInt64.MinValue + 1;
2056 checked {
2057 try {
2058 ulong t = a--;
2059 } catch {
2060 exception = 1;
2063 return exception;
2066 public static int test_1_ovf10 () {
2067 int exception = 0;
2069 ulong a = UInt64.MinValue;
2070 checked {
2071 try {
2072 ulong t = a--;
2073 } catch {
2074 exception = 1;
2077 return exception;
2080 public static int test_0_ovf11 () {
2081 int exception = 0;
2083 int a = Int32.MinValue + 1;
2084 checked {
2085 try {
2086 int t = a--;
2087 } catch {
2088 exception = 1;
2091 return exception;
2094 public static int test_1_ovf12 () {
2095 int exception = 0;
2097 int a = Int32.MinValue;
2098 checked {
2099 try {
2100 int t = a--;
2101 } catch {
2102 exception = 1;
2105 return exception;
2108 public static int test_0_ovf13 () {
2109 int exception = 0;
2111 uint a = 1;
2112 checked {
2113 try {
2114 uint t = a--;
2115 } catch {
2116 exception = 1;
2119 return exception;
2122 public static int test_1_ovf14 () {
2123 int exception = 0;
2125 uint a = 0;
2126 checked {
2127 try {
2128 uint t = a--;
2129 } catch {
2130 exception = 1;
2133 return exception;
2136 public static int test_0_ovf15 () {
2137 int exception = 0;
2139 sbyte a = 126;
2140 checked {
2141 try {
2142 sbyte t = a++;
2143 } catch {
2144 exception = 1;
2147 return exception;
2150 public static int test_1_ovf16 () {
2151 int exception = 0;
2153 sbyte a = 127;
2154 checked {
2155 try {
2156 sbyte t = a++;
2157 } catch {
2158 exception = 1;
2161 return exception;
2164 public static int test_0_ovf17 () {
2165 int exception = 0;
2167 checked {
2168 try {
2169 } catch {
2170 exception = 1;
2173 return exception;
2176 public static int test_0_ovf18 () {
2177 int exception = 0;
2179 int a = 1 << 29;
2180 checked {
2181 try {
2182 int t = a*2;
2183 } catch {
2184 exception = 1;
2187 return exception;
2190 public static int test_1_ovf19 () {
2191 int exception = 0;
2193 int a = 1 << 30;
2194 checked {
2195 try {
2196 int t = a*2;
2197 } catch {
2198 exception = 1;
2201 return exception;
2204 public static int test_0_ovf20 () {
2205 int exception = 0;
2207 checked {
2208 try {
2209 ulong a = 0xffffffffff;
2210 ulong t = a*0x0ffffff;
2211 } catch {
2212 exception = 1;
2215 return exception;
2218 public static int test_1_ovf21 () {
2219 int exception = 0;
2221 ulong a = 0xffffffffff;
2222 checked {
2223 try {
2224 ulong t = a*0x0fffffff;
2225 } catch {
2226 exception = 1;
2229 return exception;
2232 public static int test_1_ovf22 () {
2233 int exception = 0;
2235 long a = Int64.MinValue;
2236 long b = 10;
2237 checked {
2238 try {
2239 long v = a * b;
2240 } catch {
2241 exception = 1;
2244 return exception;
2247 public static int test_1_ovf23 () {
2248 int exception = 0;
2250 long a = 10;
2251 long b = Int64.MinValue;
2252 checked {
2253 try {
2254 long v = a * b;
2255 } catch {
2256 exception = 1;
2259 return exception;
2262 class Broken {
2263 public static int i;
2265 static Broken () {
2266 throw new Exception ("Ugh!");
2269 public static int DoSomething () {
2270 return i;
2274 public static int test_0_exception_in_cctor () {
2275 try {
2276 Broken.DoSomething ();
2278 catch (TypeInitializationException) {
2279 // This will only happen once even if --regression is used
2281 return 0;
2284 public static int test_5_regalloc () {
2285 int i = 0;
2287 try {
2288 for (i = 0; i < 10; ++i) {
2289 if (i == 5)
2290 throw new Exception ();
2293 catch (Exception) {
2294 if (i != 5)
2295 return i;
2298 // Check that variables written in catch clauses are volatile
2299 int j = 0;
2300 try {
2301 throw new Exception ();
2303 catch (Exception) {
2304 j = 5;
2306 if (j != 5)
2307 return 6;
2309 int k = 0;
2310 try {
2311 try {
2312 throw new Exception ();
2314 finally {
2315 k = 5;
2318 catch (Exception) {
2320 if (k != 5)
2321 return 7;
2323 return i;
2326 public static void rethrow () {
2327 try {
2328 throw new ApplicationException();
2329 } catch (ApplicationException) {
2330 try {
2331 throw new OverflowException();
2332 } catch (Exception) {
2333 throw;
2338 // Test that a rethrow rethrows the correct exception
2339 public static int test_0_rethrow_nested () {
2340 try {
2341 rethrow ();
2342 } catch (OverflowException) {
2343 return 0;
2344 } catch (Exception) {
2345 return 1;
2347 return 2;
2350 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2351 public static void rethrow1 () {
2352 throw new Exception ();
2355 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2356 public static void rethrow2 () {
2357 rethrow1 ();
2358 /* This disables tailcall opts */
2359 Console.WriteLine ();
2362 [Category ("!BITCODE")]
2363 public static int test_0_rethrow_stacktrace () {
2364 // Check that rethrowing an exception preserves the original stack trace
2365 try {
2366 try {
2367 rethrow2 ();
2369 catch (Exception ex) {
2370 // Check that each catch clause has its own exception variable
2371 // If not, the throw below will overwrite the exception used
2372 // by the rethrow
2373 try {
2374 throw new DivideByZeroException ();
2376 catch (Exception foo) {
2379 throw;
2382 catch (Exception ex) {
2383 if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2384 return 0;
2387 return 1;
2390 interface IFace {}
2391 class Face : IFace {}
2393 public static int test_1_array_mismatch_2 () {
2394 try {
2395 object [] o = new Face [1];
2396 o [0] = 1;
2397 return 0;
2398 } catch (ArrayTypeMismatchException) {
2399 return 1;
2403 public static int test_1_array_mismatch_3 () {
2404 try {
2405 object [] o = new IFace [1];
2406 o [0] = 1;
2407 return 0;
2408 } catch (ArrayTypeMismatchException) {
2409 return 1;
2413 public static int test_1_array_mismatch_4 () {
2414 try {
2415 object [][] o = new Face [5] [];
2416 o [0] = new object [5];
2418 return 0;
2419 } catch (ArrayTypeMismatchException) {
2420 return 1;
2424 public static int test_0_array_size () {
2425 bool failed;
2427 try {
2428 failed = true;
2429 int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2431 catch (OutOfMemoryException e) {
2432 failed = false;
2434 if (failed)
2435 return 2;
2437 return 0;
2440 struct S {
2441 int i, j, k, l, m, n;
2444 static IntPtr[] addr;
2446 static unsafe void throw_func (int i, S s) {
2447 addr [i] = new IntPtr (&i);
2448 throw new Exception ();
2451 /* Test that arguments are correctly popped off the stack during unwinding */
2452 /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2454 public static int test_0_stack_unwind () {
2455 addr = new IntPtr [1000];
2456 S s = new S ();
2457 for (int j = 0; j < 1000; j++) {
2458 try {
2459 throw_func (j, s);
2461 catch (Exception) {
2464 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2468 static unsafe void get_sp (int i) {
2469 addr [i] = new IntPtr (&i);
2472 /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2473 public static int test_0_throw_unwind () {
2474 addr = new IntPtr [1000];
2475 S s = new S ();
2476 for (int j = 0; j < 1000; j++) {
2477 try {
2478 get_sp (j);
2479 throw new Exception ();
2481 catch (Exception) {
2484 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2487 public static int test_0_regress_73242 () {
2488 int [] arr = new int [10];
2489 for (int i = 0; i < 10; ++i)
2490 arr [i] = 0;
2491 try {
2492 throw new Exception ();
2494 catch {
2496 return 0;
2499 public static int test_0_nullref () {
2500 try {
2501 Array foo = null;
2502 foo.Clone();
2503 } catch (NullReferenceException e) {
2504 return 0;
2506 return 1;
2509 public int amethod () {
2510 return 1;
2513 public static int test_0_nonvirt_nullref_at_clause_start () {
2514 ExceptionTests t = null;
2515 try {
2516 t.amethod ();
2517 } catch (NullReferenceException) {
2518 return 0;
2521 return 1;
2524 public static int throw_only () {
2525 throw new Exception ();
2528 [MethodImpl(MethodImplOptions.NoInlining)]
2529 public static int throw_only2 () {
2530 return throw_only ();
2533 public static int test_0_inline_throw_only () {
2534 try {
2535 return throw_only2 ();
2537 catch (Exception ex) {
2538 return 0;
2542 public static string GetText (string s) {
2543 return s;
2546 public static int throw_only_gettext () {
2547 throw new Exception (GetText ("FOO"));
2550 public static int test_0_inline_throw_only_gettext () {
2551 object o = null;
2552 try {
2553 o = throw_only_gettext ();
2555 catch (Exception ex) {
2556 return 0;
2559 return o != null ? 0 : 1;
2562 // bug #78633
2563 public static int test_0_throw_to_branch_opt_outer_clause () {
2564 int i = 0;
2566 try {
2567 try {
2568 string [] files = new string[1];
2570 string s = files[2];
2571 } finally {
2572 i ++;
2574 } catch {
2577 return (i == 1) ? 0 : 1;
2580 // bug #485721
2581 public static int test_0_try_inside_finally_cmov_opt () {
2582 bool Reconect = false;
2584 object o = new object ();
2586 try {
2588 catch (Exception ExCon) {
2589 if (o != null)
2590 Reconect = true;
2592 try {
2594 catch (Exception Last) {
2597 finally {
2598 if (Reconect == true) {
2599 try {
2601 catch (Exception ex) {
2606 return 0;
2609 public static int test_0_inline_throw () {
2610 try {
2611 inline_throw1 (5);
2612 return 1;
2613 } catch {
2614 return 0;
2618 // for llvm, the end bblock is unreachable
2619 public static int inline_throw1 (int i) {
2620 if (i == 0)
2621 throw new Exception ();
2622 else
2623 return inline_throw2 (i);
2626 public static int inline_throw2 (int i) {
2627 throw new Exception ();
2630 // bug #539550
2631 public static int test_0_lmf_filter () {
2632 try {
2633 // The invoke calls a runtime-invoke wrapper which has a filter clause
2634 #if __MOBILE__
2635 typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2636 #else
2637 typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2638 #endif
2639 } catch (TargetInvocationException) {
2641 return 0;
2644 public static void lmf_filter () {
2645 try {
2646 Connect ();
2648 catch {
2649 throw new NotImplementedException ();
2653 public static void Connect () {
2654 Stop ();
2655 throw new Exception();
2658 public static void Stop () {
2659 try {
2660 lock (null) {}
2662 catch {
2666 private static void do_raise () {
2667 throw new System.Exception ();
2670 private static int int_func (int i) {
2671 return i;
2674 // #559876
2675 public static int test_8_local_deadce_causes () {
2676 int myb = 4;
2678 try {
2679 myb = int_func (8);
2680 do_raise();
2681 myb = int_func (2);
2682 } catch (System.Exception) {
2683 return myb;
2685 return 0;
2688 public static int test_0_except_opt_two_clauses () {
2689 int size;
2690 size = -1;
2691 uint ui = (uint)size;
2692 try {
2693 checked {
2694 uint v = ui * (uint)4;
2696 } catch (OverflowException e) {
2697 return 0;
2698 } catch (Exception) {
2699 return 1;
2702 return 2;
2705 class Child
2707 public virtual long Method()
2709 throw new Exception();
2713 /* #612206 */
2714 public static int test_100_long_vars_in_clauses_initlocals_opt () {
2715 Child c = new Child();
2716 long value = 100;
2717 try {
2718 value = c.Method();
2720 catch {}
2721 return (int)value;
2724 class A {
2725 public object AnObj;
2728 public static void DoSomething (ref object o) {
2731 public static int test_0_ldflda_null () {
2732 A a = null;
2734 try {
2735 DoSomething (ref a.AnObj);
2736 } catch (NullReferenceException) {
2737 return 0;
2740 return 1;
2743 unsafe struct Foo
2745 public int i;
2747 public static Foo* pFoo;
2750 /* MS.NET doesn't seem to throw in this case */
2751 public unsafe static int test_0_ldflda_null_pointer () {
2752 int* pi = &Foo.pFoo->i;
2754 return 0;
2757 static int test_0_try_clause_in_finally_clause_regalloc () {
2758 // Fill up registers with values
2759 object a = new object ();
2760 object[] arr1 = new object [1];
2761 object[] arr2 = new object [1];
2762 object[] arr3 = new object [1];
2763 object[] arr4 = new object [1];
2764 object[] arr5 = new object [1];
2766 for (int i = 0; i < 10; ++i)
2767 arr1 [0] = a;
2768 for (int i = 0; i < 10; ++i)
2769 arr2 [0] = a;
2770 for (int i = 0; i < 10; ++i)
2771 arr3 [0] = a;
2772 for (int i = 0; i < 10; ++i)
2773 arr4 [0] = a;
2774 for (int i = 0; i < 10; ++i)
2775 arr5 [0] = a;
2777 int res = 1;
2778 try {
2779 try_clause_in_finally_clause_regalloc_inner (out res);
2780 } catch (Exception) {
2782 return res;
2785 public static object Throw () {
2786 for (int i = 0; i < 10; ++i)
2788 throw new Exception ();
2791 static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2792 object o = null;
2794 res = 1;
2795 try {
2796 o = Throw ();
2797 } catch (Exception) {
2798 /* Make sure this doesn't branch to the finally */
2799 throw new DivideByZeroException ();
2800 } finally {
2801 try {
2802 /* Make sure o is register allocated */
2803 if (o == null)
2804 res = 0;
2805 else
2806 res = 1;
2807 if (o == null)
2808 res = 0;
2809 else
2810 res = 1;
2811 if (o == null)
2812 res = 0;
2813 else
2814 res = 1;
2815 } catch (DivideByZeroException) {
2820 public static bool t_1835_inner () {
2821 bool a = true;
2822 if (a) throw new Exception();
2823 return true;
2826 [MethodImpl(MethodImplOptions.NoInlining)]
2827 public static bool t_1835_inner_2 () {
2828 bool b = t_1835_inner ();
2829 return b;
2832 public static int test_0_inline_retval_throw_in_branch_1835 () {
2833 try {
2834 t_1835_inner_2 ();
2835 } catch {
2836 return 0;
2838 return 1;
2841 static bool finally_called = false;
2843 static void regress_30472 (int a, int b) {
2844 checked {
2845 try {
2846 int sum = a + b;
2847 } finally {
2848 finally_called = true;
2853 public static int test_0_regress_30472 () {
2854 finally_called = false;
2855 try {
2856 regress_30472 (Int32.MaxValue - 1, 2);
2857 } catch (Exception ex) {
2859 return finally_called ? 0 : 1;
2862 static int array_len_1 = 1;
2864 public static int test_0_bounds_check_negative_constant () {
2865 try {
2866 byte[] arr = new byte [array_len_1];
2867 byte b = arr [-1];
2868 return 1;
2869 } catch {
2871 try {
2872 byte[] arr = new byte [array_len_1];
2873 arr [-1] = 1;
2874 return 2;
2875 } catch {
2877 return 0;
2880 public static int test_0_string_bounds_check_negative_constant () {
2881 try {
2882 string s = "A";
2883 char c = s [-1];
2884 return 1;
2885 } catch {
2887 return 0;
2890 public class MyException : Exception {
2891 public int marker = 0;
2892 public string res = "";
2894 public MyException (String res) {
2895 this.res = res;
2898 public bool FilterWithoutState () {
2899 return this.marker == 0x666;
2902 public bool FilterWithState () {
2903 bool ret = this.marker == 0x566;
2904 this.marker += 0x100;
2905 return ret;
2908 public bool FilterWithStringState () {
2909 bool ret = this.marker == 0x777;
2910 this.res = "fromFilter_" + this.res;
2911 return ret;
2915 [Category ("!BITCODE")]
2916 public static int test_1_basic_filter_catch () {
2917 try {
2918 MyException e = new MyException ("");
2919 e.marker = 0x1337;
2920 throw e;
2921 } catch (MyException ex) when (ex.marker == 0x1337) {
2922 return 1;
2924 return 0;
2927 [Category ("!BITCODE")]
2928 public static int test_1234_complicated_filter_catch () {
2929 string res = "init";
2930 try {
2931 MyException e = new MyException (res);
2932 e.marker = 0x566;
2933 try {
2934 try {
2935 throw e;
2936 } catch (MyException ex) when (ex.FilterWithoutState ()) {
2937 res = "WRONG_" + res;
2938 } finally {
2939 e.marker = 0x777;
2940 res = "innerFinally_" + res;
2942 } catch (MyException ex) when (ex.FilterWithState ()) {
2943 res = "2ndcatch_" + res;
2945 // "2ndcatch_innerFinally_init"
2946 // Console.WriteLine ("res1: " + res);
2947 e.res = res;
2948 throw e;
2949 } catch (MyException ex) when (ex.FilterWithStringState ()) {
2950 res = "fwos_" + ex.res;
2951 } finally {
2952 res = "outerFinally_" + res;
2954 // Console.WriteLine ("res2: " + res);
2955 return "outerFinally_fwos_fromFilter_2ndcatch_innerFinally_init" == res ? 1234 : 0;
2958 public struct FooStruct
2960 public long Part1 { get; }
2961 public long Part2 { get; }
2963 public byte Part3 { get; }
2966 [MethodImpl( MethodImplOptions.NoInlining )]
2967 private static bool ExceptionFilter( byte x, FooStruct item ) => true;
2969 [Category ("!BITCODE")]
2970 public static int test_0_filter_caller_area () {
2971 try {
2972 throw new Exception();
2974 catch (Exception) when (ExceptionFilter (default(byte), default (FooStruct))) {
2976 return 0;
2979 public static int test_0_signed_ct_div () {
2980 int n = 2147483647;
2981 bool divide_by_zero = false;
2982 bool overflow = false;
2984 n = -n;
2985 n--; /* MinValue */
2986 try {
2987 int r = n / (-1);
2988 } catch (OverflowException) {
2989 overflow = true;
2991 if (!overflow)
2992 return 7;
2994 try {
2995 int r = n / 0;
2996 } catch (DivideByZeroException) {
2997 divide_by_zero = true;
2999 if (!divide_by_zero)
3000 return 8;
3002 if ((n / 35) != -61356675)
3003 return 9;
3004 if ((n / -35) != 61356675)
3005 return 10;
3006 n = -(n + 1); /* MaxValue */
3007 if ((n / 35) != 61356675)
3008 return 11;
3009 if ((n / -35) != -61356675)
3010 return 12;
3012 return 0;
3015 public static int test_0_unsigned_ct_div () {
3016 uint n = 4294967295;
3017 bool divide_by_zero = false;
3019 try {
3020 uint a = n / 0;
3021 } catch (DivideByZeroException) {
3022 divide_by_zero = true;
3025 if (!divide_by_zero)
3026 return 5;
3028 if ((n / 35) != 122713351)
3029 return 9;
3031 return 0;
3035 #if !__MOBILE__
3036 class ExceptionTests : Tests
3039 #endif