[wasm] Improve virtualenv installation script (#18470)
[mono-project.git] / mono / mini / exceptions.cs
blobfe5bc32817096bcf0f0460b270ae429cdbf1c23e
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 public static int test_0_simple_double_casts () {
1474 double d = 0xffffffff;
1476 if ((uint)d != 4294967295)
1477 return 1;
1480 * These tests depend on properties of x86 fp arithmetic so they won't work
1481 * on other platforms.
1484 d = 0xffffffffffffffff;
1486 if ((ulong)d != 0)
1487 return 2;
1489 if ((ushort)d != 0)
1490 return 3;
1492 if ((byte)d != 0)
1493 return 4;
1496 d = 0xffff;
1498 if ((ushort)d != 0xffff)
1499 return 5;
1501 if ((byte)d != 0xff)
1502 return 6;
1504 return 0;
1507 public static int test_0_div_zero () {
1508 int d = 1;
1509 int q = 0;
1510 int val;
1511 bool failed;
1513 try {
1514 failed = true;
1515 val = d / q;
1516 } catch (DivideByZeroException) {
1517 failed = false;
1519 if (failed)
1520 return 1;
1522 try {
1523 failed = true;
1524 val = d % q;
1525 } catch (DivideByZeroException) {
1526 failed = false;
1528 if (failed)
1529 return 2;
1531 try {
1532 failed = true;
1533 q = -1;
1534 d = Int32.MinValue;
1535 val = d / q;
1536 } catch (DivideByZeroException) {
1537 /* wrong exception */
1538 } catch (OverflowException) {
1539 failed = false;
1541 if (failed)
1542 return 3;
1544 try {
1545 failed = true;
1546 q = -1;
1547 d = Int32.MinValue;
1548 val = d % q;
1549 } catch (DivideByZeroException) {
1550 /* wrong exception */
1551 } catch (OverflowException) {
1552 failed = false;
1554 if (failed)
1555 return 4;
1557 return 0;
1560 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1561 static void dummy () {
1564 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1565 static int div_zero_llvm_inner (int i) {
1566 try {
1567 // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1568 dummy ();
1569 return 5 / i;
1570 } catch (Exception ex) {
1571 return 0;
1575 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1576 static long div_zero_llvm_inner_long (long l) {
1577 try {
1578 dummy ();
1579 return (long)5 / l;
1580 } catch (Exception ex) {
1581 return 0;
1585 public static int test_0_div_zero_llvm () {
1586 long r = div_zero_llvm_inner (0);
1587 if (r != 0)
1588 return 1;
1589 r = div_zero_llvm_inner_long (0);
1590 if (r != 0)
1591 return 2;
1592 return 0;
1595 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1596 static int div_overflow_llvm_inner (int i) {
1597 try {
1598 dummy ();
1599 return Int32.MinValue / i;
1600 } catch (Exception ex) {
1601 return 0;
1605 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1606 static long div_overflow_llvm_inner_long (long l) {
1607 try {
1608 dummy ();
1609 return Int64.MinValue / l;
1610 } catch (Exception ex) {
1611 return 0;
1615 public static int test_0_div_overflow_llvm () {
1616 long r = div_overflow_llvm_inner (-1);
1617 if (r != 0)
1618 return 1;
1619 r = div_overflow_llvm_inner_long ((long)-1);
1620 if (r != 0)
1621 return 2;
1622 return 0;
1625 public static int return_55 () {
1626 return 55;
1629 public static int test_0_cfold_div_zero () {
1630 // Test that constant folding doesn't cause division by zero exceptions
1631 if (return_55 () != return_55 ()) {
1632 int d = 1;
1633 int q = 0;
1634 int val;
1636 val = d / q;
1637 val = d % q;
1639 q = -1;
1640 d = Int32.MinValue;
1641 val = d / q;
1643 q = -1;
1644 val = d % q;
1647 return 0;
1650 public static int test_0_udiv_zero () {
1651 uint d = 1;
1652 uint q = 0;
1653 uint val;
1654 bool failed;
1656 try {
1657 failed = true;
1658 val = d / q;
1659 } catch (DivideByZeroException) {
1660 failed = false;
1662 if (failed)
1663 return 1;
1665 try {
1666 failed = true;
1667 val = d % q;
1668 } catch (DivideByZeroException) {
1669 failed = false;
1671 if (failed)
1672 return 2;
1674 return 0;
1677 public static int test_0_long_div_zero () {
1678 long d = 1;
1679 long q = 0;
1680 long val;
1681 bool failed;
1683 try {
1684 failed = true;
1685 val = d / q;
1686 } catch (DivideByZeroException) {
1687 failed = false;
1689 if (failed)
1690 return 1;
1692 try {
1693 failed = true;
1694 val = d % q;
1695 } catch (DivideByZeroException) {
1696 failed = false;
1698 if (failed)
1699 return 2;
1701 try {
1702 failed = true;
1703 q = -1;
1704 d = Int64.MinValue;
1705 val = d / q;
1706 } catch (DivideByZeroException) {
1707 /* wrong exception */
1708 } catch (OverflowException) {
1709 failed = false;
1711 if (failed)
1712 return 3;
1714 try {
1715 failed = true;
1716 q = -1;
1717 d = Int64.MinValue;
1718 val = d % q;
1719 } catch (DivideByZeroException) {
1720 /* wrong exception */
1721 } catch (OverflowException) {
1722 failed = false;
1724 if (failed)
1725 return 4;
1727 return 0;
1730 public static int test_0_ulong_div_zero () {
1731 ulong d = 1;
1732 ulong q = 0;
1733 ulong val;
1734 bool failed;
1736 try {
1737 failed = true;
1738 val = d / q;
1739 } catch (DivideByZeroException) {
1740 failed = false;
1742 if (failed)
1743 return 1;
1745 try {
1746 failed = true;
1747 val = d % q;
1748 } catch (DivideByZeroException) {
1749 failed = false;
1751 if (failed)
1752 return 2;
1754 return 0;
1757 public static int test_0_float_div_zero () {
1758 double d = 1;
1759 double q = 0;
1760 double val;
1761 bool failed;
1763 try {
1764 failed = false;
1765 val = d / q;
1766 } catch (DivideByZeroException) {
1767 failed = true;
1769 if (failed)
1770 return 1;
1772 try {
1773 failed = false;
1774 val = d % q;
1775 } catch (DivideByZeroException) {
1776 failed = true;
1778 if (failed)
1779 return 2;
1781 return 0;
1784 public static int test_0_invalid_unbox () {
1786 int i = 123;
1787 object o = "Some string";
1788 int res = 1;
1790 try {
1791 // Illegal conversion; o contains a string not an int
1792 i = (int) o;
1793 } catch (Exception e) {
1794 if (i ==123)
1795 res = 0;
1798 return res;
1801 // Test that double[] can't be cast to double (bug #46027)
1802 public static int test_0_invalid_unbox_arrays () {
1803 double[] d1 = { 1.0 };
1804 double[][] d2 = { d1 };
1805 Array a = d2;
1807 try {
1808 foreach (double d in a) {
1810 return 1;
1812 catch (InvalidCastException e) {
1813 return 0;
1817 /* bug# 42190, at least mcs generates a leave for the return that
1818 * jumps out of multiple exception clauses: we used to execute just
1819 * one enclosing finally block.
1821 public static int finally_level;
1822 static void do_something () {
1823 int a = 0;
1824 try {
1825 try {
1826 return;
1827 } finally {
1828 a = 1;
1830 } finally {
1831 finally_level++;
1835 public static int test_2_multiple_finally_clauses () {
1836 finally_level = 0;
1837 do_something ();
1838 if (finally_level == 1)
1839 return 2;
1840 return 0;
1843 public static int test_3_checked_cast_un () {
1844 ulong i = 0x8000000034000000;
1845 long j;
1847 try {
1848 checked { j = (long)i; }
1849 } catch (OverflowException) {
1850 j = 2;
1853 if (j != 2)
1854 return 0;
1855 return 3;
1858 public static int test_4_checked_cast () {
1859 long i;
1860 ulong j;
1862 unchecked { i = (long)0x8000000034000000;};
1863 try {
1864 checked { j = (ulong)i; }
1865 } catch (OverflowException) {
1866 j = 3;
1869 if (j != 3)
1870 return 0;
1871 return 4;
1874 static readonly int[] mul_dim_results = new int[] {
1875 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1876 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1877 2, 0, 2, 1, 2, 8,
1878 3, 0, 3, 1, 3, 8,
1879 4, 0, 4, 1, 4, 8,
1880 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1881 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1882 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1885 public static int test_0_multi_dim_array_access () {
1886 int [,] a = System.Array.CreateInstance (typeof (int),
1887 new int [] {3,6}, new int [] {2,2 }) as int[,];
1888 int x, y;
1889 int result_idx = 0;
1890 for (x = 0; x < 8; ++x) {
1891 for (y = 0; y < 9; ++y) {
1892 bool got_ex = false;
1893 try {
1894 a [x, y] = 1;
1895 } catch {
1896 got_ex = true;
1898 if (got_ex) {
1899 if (result_idx >= mul_dim_results.Length)
1900 return -1;
1901 if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1902 return result_idx + 1;
1904 result_idx += 2;
1908 if (result_idx == mul_dim_results.Length)
1909 return 0;
1910 return 200;
1913 static void helper_out_obj (out object o) {
1914 o = (object)"buddy";
1917 static void helper_out_string (out string o) {
1918 o = "buddy";
1921 public static int test_2_array_mismatch () {
1922 string[] a = { "hello", "world" };
1923 object[] b = a;
1924 bool passed = false;
1926 try {
1927 helper_out_obj (out b [1]);
1928 } catch (ArrayTypeMismatchException) {
1929 passed = true;
1931 if (!passed)
1932 return 0;
1933 helper_out_string (out a [1]);
1934 if (a [1] != "buddy")
1935 return 1;
1936 return 2;
1939 public static int test_0_ovf1 () {
1940 int exception = 0;
1942 checked {
1943 try {
1944 ulong a = UInt64.MaxValue - 1;
1945 ulong t = a++;
1946 } catch {
1947 exception = 1;
1950 return exception;
1953 public static int test_1_ovf2 () {
1954 int exception = 0;
1956 checked {
1957 try {
1958 ulong a = UInt64.MaxValue;
1959 ulong t = a++;
1960 } catch {
1961 exception = 1;
1964 return exception;
1967 public static int test_0_ovf3 () {
1968 int exception = 0;
1970 long a = Int64.MaxValue - 1;
1971 checked {
1972 try {
1973 long t = a++;
1974 } catch {
1975 exception = 1;
1978 return exception;
1981 public static int test_1_ovf4 () {
1982 int exception = 0;
1984 long a = Int64.MaxValue;
1985 checked {
1986 try {
1987 long t = a++;
1988 } catch {
1989 exception = 1;
1992 return exception;
1995 public static int test_0_ovf5 () {
1996 int exception = 0;
1998 ulong a = UInt64.MaxValue - 1;
1999 checked {
2000 try {
2001 ulong t = a++;
2002 } catch {
2003 exception = 1;
2006 return exception;
2009 public static int test_1_ovf6 () {
2010 int exception = 0;
2012 ulong a = UInt64.MaxValue;
2013 checked {
2014 try {
2015 ulong t = a++;
2016 } catch {
2017 exception = 1;
2020 return exception;
2023 public static int test_0_ovf7 () {
2024 int exception = 0;
2026 long a = Int64.MinValue + 1;
2027 checked {
2028 try {
2029 long t = a--;
2030 } catch {
2031 exception = 1;
2034 return 0;
2037 public static int test_1_ovf8 () {
2038 int exception = 0;
2040 long a = Int64.MinValue;
2041 checked {
2042 try {
2043 long t = a--;
2044 } catch {
2045 exception = 1;
2048 return exception;
2051 public static int test_0_ovf9 () {
2052 int exception = 0;
2054 ulong a = UInt64.MinValue + 1;
2055 checked {
2056 try {
2057 ulong t = a--;
2058 } catch {
2059 exception = 1;
2062 return exception;
2065 public static int test_1_ovf10 () {
2066 int exception = 0;
2068 ulong a = UInt64.MinValue;
2069 checked {
2070 try {
2071 ulong t = a--;
2072 } catch {
2073 exception = 1;
2076 return exception;
2079 public static int test_0_ovf11 () {
2080 int exception = 0;
2082 int a = Int32.MinValue + 1;
2083 checked {
2084 try {
2085 int t = a--;
2086 } catch {
2087 exception = 1;
2090 return exception;
2093 public static int test_1_ovf12 () {
2094 int exception = 0;
2096 int a = Int32.MinValue;
2097 checked {
2098 try {
2099 int t = a--;
2100 } catch {
2101 exception = 1;
2104 return exception;
2107 public static int test_0_ovf13 () {
2108 int exception = 0;
2110 uint a = 1;
2111 checked {
2112 try {
2113 uint t = a--;
2114 } catch {
2115 exception = 1;
2118 return exception;
2121 public static int test_1_ovf14 () {
2122 int exception = 0;
2124 uint a = 0;
2125 checked {
2126 try {
2127 uint t = a--;
2128 } catch {
2129 exception = 1;
2132 return exception;
2135 public static int test_0_ovf15 () {
2136 int exception = 0;
2138 sbyte a = 126;
2139 checked {
2140 try {
2141 sbyte t = a++;
2142 } catch {
2143 exception = 1;
2146 return exception;
2149 public static int test_1_ovf16 () {
2150 int exception = 0;
2152 sbyte a = 127;
2153 checked {
2154 try {
2155 sbyte t = a++;
2156 } catch {
2157 exception = 1;
2160 return exception;
2163 public static int test_0_ovf17 () {
2164 int exception = 0;
2166 checked {
2167 try {
2168 } catch {
2169 exception = 1;
2172 return exception;
2175 public static int test_0_ovf18 () {
2176 int exception = 0;
2178 int a = 1 << 29;
2179 checked {
2180 try {
2181 int t = a*2;
2182 } catch {
2183 exception = 1;
2186 return exception;
2189 public static int test_1_ovf19 () {
2190 int exception = 0;
2192 int a = 1 << 30;
2193 checked {
2194 try {
2195 int t = a*2;
2196 } catch {
2197 exception = 1;
2200 return exception;
2203 public static int test_0_ovf20 () {
2204 int exception = 0;
2206 checked {
2207 try {
2208 ulong a = 0xffffffffff;
2209 ulong t = a*0x0ffffff;
2210 } catch {
2211 exception = 1;
2214 return exception;
2217 public static int test_1_ovf21 () {
2218 int exception = 0;
2220 ulong a = 0xffffffffff;
2221 checked {
2222 try {
2223 ulong t = a*0x0fffffff;
2224 } catch {
2225 exception = 1;
2228 return exception;
2231 public static int test_1_ovf22 () {
2232 int exception = 0;
2234 long a = Int64.MinValue;
2235 long b = 10;
2236 checked {
2237 try {
2238 long v = a * b;
2239 } catch {
2240 exception = 1;
2243 return exception;
2246 public static int test_1_ovf23 () {
2247 int exception = 0;
2249 long a = 10;
2250 long b = Int64.MinValue;
2251 checked {
2252 try {
2253 long v = a * b;
2254 } catch {
2255 exception = 1;
2258 return exception;
2261 class Broken {
2262 public static int i;
2264 static Broken () {
2265 throw new Exception ("Ugh!");
2268 public static int DoSomething () {
2269 return i;
2273 public static int test_0_exception_in_cctor () {
2274 try {
2275 Broken.DoSomething ();
2277 catch (TypeInitializationException) {
2278 // This will only happen once even if --regression is used
2280 return 0;
2283 public static int test_5_regalloc () {
2284 int i = 0;
2286 try {
2287 for (i = 0; i < 10; ++i) {
2288 if (i == 5)
2289 throw new Exception ();
2292 catch (Exception) {
2293 if (i != 5)
2294 return i;
2297 // Check that variables written in catch clauses are volatile
2298 int j = 0;
2299 try {
2300 throw new Exception ();
2302 catch (Exception) {
2303 j = 5;
2305 if (j != 5)
2306 return 6;
2308 int k = 0;
2309 try {
2310 try {
2311 throw new Exception ();
2313 finally {
2314 k = 5;
2317 catch (Exception) {
2319 if (k != 5)
2320 return 7;
2322 return i;
2325 public static void rethrow () {
2326 try {
2327 throw new ApplicationException();
2328 } catch (ApplicationException) {
2329 try {
2330 throw new OverflowException();
2331 } catch (Exception) {
2332 throw;
2337 // Test that a rethrow rethrows the correct exception
2338 public static int test_0_rethrow_nested () {
2339 try {
2340 rethrow ();
2341 } catch (OverflowException) {
2342 return 0;
2343 } catch (Exception) {
2344 return 1;
2346 return 2;
2349 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2350 public static void rethrow1 () {
2351 throw new Exception ();
2354 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2355 public static void rethrow2 () {
2356 rethrow1 ();
2357 /* This disables tailcall opts */
2358 Console.WriteLine ();
2361 [Category ("!BITCODE")]
2362 public static int test_0_rethrow_stacktrace () {
2363 // Check that rethrowing an exception preserves the original stack trace
2364 try {
2365 try {
2366 rethrow2 ();
2368 catch (Exception ex) {
2369 // Check that each catch clause has its own exception variable
2370 // If not, the throw below will overwrite the exception used
2371 // by the rethrow
2372 try {
2373 throw new DivideByZeroException ();
2375 catch (Exception foo) {
2378 throw;
2381 catch (Exception ex) {
2382 if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2383 return 0;
2386 return 1;
2389 interface IFace {}
2390 class Face : IFace {}
2392 public static int test_1_array_mismatch_2 () {
2393 try {
2394 object [] o = new Face [1];
2395 o [0] = 1;
2396 return 0;
2397 } catch (ArrayTypeMismatchException) {
2398 return 1;
2402 public static int test_1_array_mismatch_3 () {
2403 try {
2404 object [] o = new IFace [1];
2405 o [0] = 1;
2406 return 0;
2407 } catch (ArrayTypeMismatchException) {
2408 return 1;
2412 public static int test_1_array_mismatch_4 () {
2413 try {
2414 object [][] o = new Face [5] [];
2415 o [0] = new object [5];
2417 return 0;
2418 } catch (ArrayTypeMismatchException) {
2419 return 1;
2423 public static int test_0_array_size () {
2424 bool failed;
2426 try {
2427 failed = true;
2428 int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2430 catch (OutOfMemoryException e) {
2431 failed = false;
2433 if (failed)
2434 return 2;
2436 return 0;
2439 struct S {
2440 int i, j, k, l, m, n;
2443 static IntPtr[] addr;
2445 static unsafe void throw_func (int i, S s) {
2446 addr [i] = new IntPtr (&i);
2447 throw new Exception ();
2450 /* Test that arguments are correctly popped off the stack during unwinding */
2451 /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2453 public static int test_0_stack_unwind () {
2454 addr = new IntPtr [1000];
2455 S s = new S ();
2456 for (int j = 0; j < 1000; j++) {
2457 try {
2458 throw_func (j, s);
2460 catch (Exception) {
2463 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2467 static unsafe void get_sp (int i) {
2468 addr [i] = new IntPtr (&i);
2471 /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2472 public static int test_0_throw_unwind () {
2473 addr = new IntPtr [1000];
2474 S s = new S ();
2475 for (int j = 0; j < 1000; j++) {
2476 try {
2477 get_sp (j);
2478 throw new Exception ();
2480 catch (Exception) {
2483 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2486 public static int test_0_regress_73242 () {
2487 int [] arr = new int [10];
2488 for (int i = 0; i < 10; ++i)
2489 arr [i] = 0;
2490 try {
2491 throw new Exception ();
2493 catch {
2495 return 0;
2498 public static int test_0_nullref () {
2499 try {
2500 Array foo = null;
2501 foo.Clone();
2502 } catch (NullReferenceException e) {
2503 return 0;
2505 return 1;
2508 public int amethod () {
2509 return 1;
2512 public static int test_0_nonvirt_nullref_at_clause_start () {
2513 ExceptionTests t = null;
2514 try {
2515 t.amethod ();
2516 } catch (NullReferenceException) {
2517 return 0;
2520 return 1;
2523 public static int throw_only () {
2524 throw new Exception ();
2527 [MethodImpl(MethodImplOptions.NoInlining)]
2528 public static int throw_only2 () {
2529 return throw_only ();
2532 public static int test_0_inline_throw_only () {
2533 try {
2534 return throw_only2 ();
2536 catch (Exception ex) {
2537 return 0;
2541 public static string GetText (string s) {
2542 return s;
2545 public static int throw_only_gettext () {
2546 throw new Exception (GetText ("FOO"));
2549 public static int test_0_inline_throw_only_gettext () {
2550 object o = null;
2551 try {
2552 o = throw_only_gettext ();
2554 catch (Exception ex) {
2555 return 0;
2558 return o != null ? 0 : 1;
2561 // bug #78633
2562 public static int test_0_throw_to_branch_opt_outer_clause () {
2563 int i = 0;
2565 try {
2566 try {
2567 string [] files = new string[1];
2569 string s = files[2];
2570 } finally {
2571 i ++;
2573 } catch {
2576 return (i == 1) ? 0 : 1;
2579 // bug #485721
2580 public static int test_0_try_inside_finally_cmov_opt () {
2581 bool Reconect = false;
2583 object o = new object ();
2585 try {
2587 catch (Exception ExCon) {
2588 if (o != null)
2589 Reconect = true;
2591 try {
2593 catch (Exception Last) {
2596 finally {
2597 if (Reconect == true) {
2598 try {
2600 catch (Exception ex) {
2605 return 0;
2608 public static int test_0_inline_throw () {
2609 try {
2610 inline_throw1 (5);
2611 return 1;
2612 } catch {
2613 return 0;
2617 // for llvm, the end bblock is unreachable
2618 public static int inline_throw1 (int i) {
2619 if (i == 0)
2620 throw new Exception ();
2621 else
2622 return inline_throw2 (i);
2625 public static int inline_throw2 (int i) {
2626 throw new Exception ();
2629 // bug #539550
2630 public static int test_0_lmf_filter () {
2631 try {
2632 // The invoke calls a runtime-invoke wrapper which has a filter clause
2633 #if __MOBILE__
2634 typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2635 #else
2636 typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2637 #endif
2638 } catch (TargetInvocationException) {
2640 return 0;
2643 public static void lmf_filter () {
2644 try {
2645 Connect ();
2647 catch {
2648 throw new NotImplementedException ();
2652 public static void Connect () {
2653 Stop ();
2654 throw new Exception();
2657 public static void Stop () {
2658 try {
2659 lock (null) {}
2661 catch {
2665 private static void do_raise () {
2666 throw new System.Exception ();
2669 private static int int_func (int i) {
2670 return i;
2673 // #559876
2674 public static int test_8_local_deadce_causes () {
2675 int myb = 4;
2677 try {
2678 myb = int_func (8);
2679 do_raise();
2680 myb = int_func (2);
2681 } catch (System.Exception) {
2682 return myb;
2684 return 0;
2687 public static int test_0_except_opt_two_clauses () {
2688 int size;
2689 size = -1;
2690 uint ui = (uint)size;
2691 try {
2692 checked {
2693 uint v = ui * (uint)4;
2695 } catch (OverflowException e) {
2696 return 0;
2697 } catch (Exception) {
2698 return 1;
2701 return 2;
2704 class Child
2706 public virtual long Method()
2708 throw new Exception();
2712 /* #612206 */
2713 public static int test_100_long_vars_in_clauses_initlocals_opt () {
2714 Child c = new Child();
2715 long value = 100;
2716 try {
2717 value = c.Method();
2719 catch {}
2720 return (int)value;
2723 class A {
2724 public object AnObj;
2727 public static void DoSomething (ref object o) {
2730 public static int test_0_ldflda_null () {
2731 A a = null;
2733 try {
2734 DoSomething (ref a.AnObj);
2735 } catch (NullReferenceException) {
2736 return 0;
2739 return 1;
2742 unsafe struct Foo
2744 public int i;
2746 public static Foo* pFoo;
2749 /* MS.NET doesn't seem to throw in this case */
2750 public unsafe static int test_0_ldflda_null_pointer () {
2751 int* pi = &Foo.pFoo->i;
2753 return 0;
2756 static int test_0_try_clause_in_finally_clause_regalloc () {
2757 // Fill up registers with values
2758 object a = new object ();
2759 object[] arr1 = new object [1];
2760 object[] arr2 = new object [1];
2761 object[] arr3 = new object [1];
2762 object[] arr4 = new object [1];
2763 object[] arr5 = new object [1];
2765 for (int i = 0; i < 10; ++i)
2766 arr1 [0] = a;
2767 for (int i = 0; i < 10; ++i)
2768 arr2 [0] = a;
2769 for (int i = 0; i < 10; ++i)
2770 arr3 [0] = a;
2771 for (int i = 0; i < 10; ++i)
2772 arr4 [0] = a;
2773 for (int i = 0; i < 10; ++i)
2774 arr5 [0] = a;
2776 int res = 1;
2777 try {
2778 try_clause_in_finally_clause_regalloc_inner (out res);
2779 } catch (Exception) {
2781 return res;
2784 public static object Throw () {
2785 for (int i = 0; i < 10; ++i)
2787 throw new Exception ();
2790 static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2791 object o = null;
2793 res = 1;
2794 try {
2795 o = Throw ();
2796 } catch (Exception) {
2797 /* Make sure this doesn't branch to the finally */
2798 throw new DivideByZeroException ();
2799 } finally {
2800 try {
2801 /* Make sure o is register allocated */
2802 if (o == null)
2803 res = 0;
2804 else
2805 res = 1;
2806 if (o == null)
2807 res = 0;
2808 else
2809 res = 1;
2810 if (o == null)
2811 res = 0;
2812 else
2813 res = 1;
2814 } catch (DivideByZeroException) {
2819 public static bool t_1835_inner () {
2820 bool a = true;
2821 if (a) throw new Exception();
2822 return true;
2825 [MethodImpl(MethodImplOptions.NoInlining)]
2826 public static bool t_1835_inner_2 () {
2827 bool b = t_1835_inner ();
2828 return b;
2831 public static int test_0_inline_retval_throw_in_branch_1835 () {
2832 try {
2833 t_1835_inner_2 ();
2834 } catch {
2835 return 0;
2837 return 1;
2840 static bool finally_called = false;
2842 static void regress_30472 (int a, int b) {
2843 checked {
2844 try {
2845 int sum = a + b;
2846 } finally {
2847 finally_called = true;
2852 public static int test_0_regress_30472 () {
2853 finally_called = false;
2854 try {
2855 regress_30472 (Int32.MaxValue - 1, 2);
2856 } catch (Exception ex) {
2858 return finally_called ? 0 : 1;
2861 static int array_len_1 = 1;
2863 public static int test_0_bounds_check_negative_constant () {
2864 try {
2865 byte[] arr = new byte [array_len_1];
2866 byte b = arr [-1];
2867 return 1;
2868 } catch {
2870 try {
2871 byte[] arr = new byte [array_len_1];
2872 arr [-1] = 1;
2873 return 2;
2874 } catch {
2876 return 0;
2879 public static int test_0_string_bounds_check_negative_constant () {
2880 try {
2881 string s = "A";
2882 char c = s [-1];
2883 return 1;
2884 } catch {
2886 return 0;
2889 public class MyException : Exception {
2890 public int marker = 0;
2891 public string res = "";
2893 public MyException (String res) {
2894 this.res = res;
2897 public bool FilterWithoutState () {
2898 return this.marker == 0x666;
2901 public bool FilterWithState () {
2902 bool ret = this.marker == 0x566;
2903 this.marker += 0x100;
2904 return ret;
2907 public bool FilterWithStringState () {
2908 bool ret = this.marker == 0x777;
2909 this.res = "fromFilter_" + this.res;
2910 return ret;
2914 [Category ("!BITCODE")]
2915 public static int test_1_basic_filter_catch () {
2916 try {
2917 MyException e = new MyException ("");
2918 e.marker = 0x1337;
2919 throw e;
2920 } catch (MyException ex) when (ex.marker == 0x1337) {
2921 return 1;
2923 return 0;
2926 [Category ("!BITCODE")]
2927 public static int test_1234_complicated_filter_catch () {
2928 string res = "init";
2929 try {
2930 MyException e = new MyException (res);
2931 e.marker = 0x566;
2932 try {
2933 try {
2934 throw e;
2935 } catch (MyException ex) when (ex.FilterWithoutState ()) {
2936 res = "WRONG_" + res;
2937 } finally {
2938 e.marker = 0x777;
2939 res = "innerFinally_" + res;
2941 } catch (MyException ex) when (ex.FilterWithState ()) {
2942 res = "2ndcatch_" + res;
2944 // "2ndcatch_innerFinally_init"
2945 // Console.WriteLine ("res1: " + res);
2946 e.res = res;
2947 throw e;
2948 } catch (MyException ex) when (ex.FilterWithStringState ()) {
2949 res = "fwos_" + ex.res;
2950 } finally {
2951 res = "outerFinally_" + res;
2953 // Console.WriteLine ("res2: " + res);
2954 return "outerFinally_fwos_fromFilter_2ndcatch_innerFinally_init" == res ? 1234 : 0;
2957 public struct FooStruct
2959 public long Part1 { get; }
2960 public long Part2 { get; }
2962 public byte Part3 { get; }
2965 [MethodImpl( MethodImplOptions.NoInlining )]
2966 private static bool ExceptionFilter( byte x, FooStruct item ) => true;
2968 [Category ("!BITCODE")]
2969 public static int test_0_filter_caller_area () {
2970 try {
2971 throw new Exception();
2973 catch (Exception) when (ExceptionFilter (default(byte), default (FooStruct))) {
2975 return 0;
2978 public static int test_0_signed_ct_div () {
2979 int n = 2147483647;
2980 bool divide_by_zero = false;
2981 bool overflow = false;
2983 n = -n;
2984 n--; /* MinValue */
2985 try {
2986 int r = n / (-1);
2987 } catch (OverflowException) {
2988 overflow = true;
2990 if (!overflow)
2991 return 7;
2993 try {
2994 int r = n / 0;
2995 } catch (DivideByZeroException) {
2996 divide_by_zero = true;
2998 if (!divide_by_zero)
2999 return 8;
3001 if ((n / 35) != -61356675)
3002 return 9;
3003 if ((n / -35) != 61356675)
3004 return 10;
3005 n = -(n + 1); /* MaxValue */
3006 if ((n / 35) != 61356675)
3007 return 11;
3008 if ((n / -35) != -61356675)
3009 return 12;
3011 return 0;
3014 public static int test_0_unsigned_ct_div () {
3015 uint n = 4294967295;
3016 bool divide_by_zero = false;
3018 try {
3019 uint a = n / 0;
3020 } catch (DivideByZeroException) {
3021 divide_by_zero = true;
3024 if (!divide_by_zero)
3025 return 5;
3027 if ((n / 35) != 122713351)
3028 return 9;
3030 return 0;
3034 #if !__MOBILE__
3035 class ExceptionTests : Tests
3038 #endif