Merge pull request #3140 from esdrubal/syscall_details
[mono-project.git] / mono / mini / exceptions.cs
blob98c6046e61a5cdea5dab273ee1b289d63c26a7b5
1 using System;
2 using System.Reflection;
3 using System.Runtime.CompilerServices;
5 /*
6 * Regression tests for the mono JIT.
8 * Each test needs to be of the form:
10 * public static int test_<result>_<name> ();
12 * where <result> is an integer (the value that needs to be returned by
13 * the method to make it pass.
14 * <name> is a user-displayed name used to identify the test.
16 * The tests can be driven in two ways:
17 * *) running the program directly: Main() uses reflection to find and invoke
18 * the test methods (this is useful mostly to check that the tests are correct)
19 * *) with the --regression switch of the jit (this is the preferred way since
20 * all the tests will be run with optimizations on and off)
22 * The reflection logic could be moved to a .dll since we need at least another
23 * regression test file written in IL code to have better control on how
24 * the IL code looks.
27 #if __MOBILE__
28 class ExceptionTests
29 #else
30 class Tests
31 #endif
34 #if !__MOBILE__
35 public static int Main (string[] args) {
36 return TestDriver.RunTests (typeof (Tests), args);
38 #endif
40 public static int test_0_catch () {
41 Exception x = new Exception ();
43 try {
44 throw x;
45 } catch (Exception e) {
46 if (e == x)
47 return 0;
49 return 1;
52 public static int test_0_finally_without_exc () {
53 int x;
55 try {
56 x = 1;
57 } catch (Exception e) {
58 x = 2;
59 } finally {
60 x = 0;
63 return x;
66 public static int test_0_finally () {
67 int x = 1;
69 try {
70 throw new Exception ();
71 } catch (Exception e) {
72 x = 2;
73 } finally {
74 x = 0;
76 return x;
79 public static int test_0_nested_finally () {
80 int a;
82 try {
83 a = 1;
84 } finally {
85 try {
86 a = 2;
87 } finally {
88 a = 0;
91 return a;
94 public static int test_0_byte_cast () {
95 int a;
96 long l;
97 ulong ul;
98 byte b = 0;
99 bool failed;
101 try {
102 a = 255;
103 failed = false;
104 checked {
105 b = (byte)a;
107 } catch (OverflowException) {
108 failed = true;
110 if (failed)
111 return 1;
112 if (b != 255)
113 return -1;
115 try {
116 a = 0;
117 failed = false;
118 checked {
119 b = (byte)a;
121 } catch (OverflowException) {
122 failed = true;
124 if (failed)
125 return 2;
126 if (b != 0)
127 return -2;
130 try {
131 a = 256;
132 failed = true;
133 checked {
134 b = (byte)a;
136 } catch (OverflowException) {
137 failed = false;
139 if (failed)
140 return 3;
141 if (b != 0)
142 return -3;
144 try {
145 a = -1;
146 failed = true;
147 checked {
148 b = (byte)a;
150 } catch (OverflowException) {
151 failed = false;
153 if (failed)
154 return 4;
155 if (b != 0)
156 return -4;
158 try {
159 double d = 0;
160 failed = false;
161 checked {
162 b = (byte)d;
164 } catch (OverflowException) {
165 failed = true;
167 if (failed)
168 return 5;
169 if (b != 0)
170 return -5;
172 try {
173 double d = -1;
174 failed = true;
175 checked {
176 b = (byte)d;
178 } catch (OverflowException) {
179 failed = false;
181 if (failed)
182 return 6;
183 if (b != 0)
184 return -6;
186 try {
187 double d = 255;
188 failed = false;
189 checked {
190 b = (byte)d;
192 } catch (OverflowException) {
193 failed = true;
195 if (failed)
196 return 7;
197 if (b != 255)
198 return -7;
200 try {
201 double d = 256;
202 failed = true;
203 checked {
204 b = (byte)d;
206 } catch (OverflowException) {
207 failed = false;
209 if (failed)
210 return 8;
211 if (b != 255)
212 return -8;
214 try {
215 l = 255;
216 failed = false;
217 checked {
218 b = (byte)l;
220 } catch (OverflowException) {
221 failed = true;
223 if (failed)
224 return 9;
225 if (b != 255)
226 return -9;
228 try {
229 l = 0;
230 failed = false;
231 checked {
232 b = (byte)l;
234 } catch (OverflowException) {
235 failed = true;
237 if (failed)
238 return 10;
239 if (b != 0)
240 return -10;
242 try {
243 l = 256;
244 failed = true;
245 checked {
246 b = (byte)l;
248 } catch (OverflowException) {
249 failed = false;
251 if (failed)
252 return 11;
253 if (b != 0)
254 return -11;
256 try {
257 l = -1;
258 failed = true;
259 checked {
260 b = (byte)l;
262 } catch (OverflowException) {
263 failed = false;
265 if (failed)
266 return 12;
267 if (b != 0)
268 return -12;
270 try {
271 ul = 256;
272 failed = true;
273 checked {
274 b = (byte)ul;
277 catch (OverflowException) {
278 failed = false;
280 if (failed)
281 return 13;
282 if (b != 0)
283 return -13;
285 return 0;
288 public static int test_0_sbyte_cast () {
289 int a;
290 long l;
291 sbyte b = 0;
292 bool failed;
294 try {
295 a = 255;
296 failed = true;
297 checked {
298 b = (sbyte)a;
300 } catch (OverflowException) {
301 failed = false;
303 if (failed)
304 return 1;
305 if (b != 0)
306 return -1;
308 try {
309 a = 0;
310 failed = false;
311 checked {
312 b = (sbyte)a;
314 } catch (OverflowException) {
315 failed = true;
317 if (failed)
318 return 2;
319 if (b != 0)
320 return -2;
322 try {
323 a = 256;
324 failed = true;
325 checked {
326 b = (sbyte)a;
328 } catch (OverflowException) {
329 failed = false;
331 if (failed)
332 return 3;
333 if (b != 0)
334 return -3;
336 try {
337 a = -129;
338 failed = true;
339 checked {
340 b = (sbyte)a;
342 } catch (OverflowException) {
343 failed = false;
345 if (failed)
346 return 4;
347 if (b != 0)
348 return -4;
350 try {
351 a = -1;
352 failed = false;
353 checked {
354 b = (sbyte)a;
356 } catch (OverflowException) {
357 failed = true;
359 if (failed)
360 return 5;
361 if (b != -1)
362 return -5;
364 try {
365 a = -128;
366 failed = false;
367 checked {
368 b = (sbyte)a;
370 } catch (OverflowException) {
371 failed = true;
373 if (failed)
374 return 6;
375 if (b != -128)
376 return -6;
378 try {
379 a = 127;
380 failed = false;
381 checked {
382 b = (sbyte)a;
384 } catch (OverflowException) {
385 failed = true;
387 if (failed)
388 return 7;
389 if (b != 127)
390 return -7;
392 try {
393 a = 128;
394 failed = true;
395 checked {
396 b = (sbyte)a;
398 } catch (OverflowException) {
399 failed = false;
401 if (failed)
402 return 8;
403 if (b != 127)
404 return -8;
406 try {
407 double d = 127;
408 failed = false;
409 checked {
410 b = (sbyte)d;
412 } catch (OverflowException) {
413 failed = true;
415 if (failed)
416 return 9;
417 if (b != 127)
418 return -9;
420 try {
421 double d = -128;
422 failed = false;
423 checked {
424 b = (sbyte)d;
426 } catch (OverflowException) {
427 failed = true;
429 if (failed)
430 return 10;
431 if (b != -128)
432 return -10;
434 try {
435 double d = 128;
436 failed = true;
437 checked {
438 b = (sbyte)d;
440 } catch (OverflowException) {
441 failed = false;
443 if (failed)
444 return 11;
445 if (b != -128)
446 return -11;
448 try {
449 double d = -129;
450 failed = true;
451 checked {
452 b = (sbyte)d;
454 } catch (OverflowException) {
455 failed = false;
457 if (failed)
458 return 12;
459 if (b != -128)
460 return -12;
462 try {
463 l = 255;
464 failed = true;
465 checked {
466 b = (sbyte)l;
468 } catch (OverflowException) {
469 failed = false;
471 if (failed)
472 return 13;
473 if (b != -128)
474 return -13;
476 try {
477 l = 0;
478 failed = false;
479 checked {
480 b = (sbyte)l;
482 } catch (OverflowException) {
483 failed = true;
485 if (failed)
486 return 14;
487 if (b != 0)
488 return -14;
490 try {
491 l = 256;
492 failed = true;
493 checked {
494 b = (sbyte)l;
496 } catch (OverflowException) {
497 failed = false;
499 if (failed)
500 return 15;
501 if (b != 0)
502 return -15;
504 try {
505 l = -129;
506 failed = true;
507 checked {
508 b = (sbyte)l;
510 } catch (OverflowException) {
511 failed = false;
513 if (failed)
514 return 16;
515 if (b != 0)
516 return -16;
518 try {
519 l = -1;
520 failed = false;
521 checked {
522 b = (sbyte)l;
524 } catch (OverflowException) {
525 failed = true;
527 if (failed)
528 return 17;
529 if (b != -1)
530 return -17;
532 try {
533 l = -128;
534 failed = false;
535 checked {
536 b = (sbyte)l;
538 } catch (OverflowException) {
539 failed = true;
541 if (failed)
542 return 18;
543 if (b != -128)
544 return -18;
546 try {
547 l = 127;
548 failed = false;
549 checked {
550 b = (sbyte)l;
552 } catch (OverflowException) {
553 failed = true;
555 if (failed)
556 return 19;
557 if (b != 127)
558 return -19;
560 try {
561 l = 128;
562 failed = true;
563 checked {
564 b = (sbyte)l;
566 } catch (OverflowException) {
567 failed = false;
569 if (failed)
570 return 20;
571 if (b != 127)
572 return -20;
574 try {
575 ulong ul = 128;
576 failed = true;
577 checked {
578 b = (sbyte)ul;
581 catch (OverflowException) {
582 failed = false;
584 if (failed)
585 return 21;
586 if (b != 127)
587 return -21;
589 return 0;
592 public static int test_0_ushort_cast () {
593 int a;
594 long l;
595 ulong ul;
596 ushort b;
597 bool failed;
599 try {
600 a = System.UInt16.MaxValue;
601 failed = false;
602 checked {
603 b = (ushort)a;
605 } catch (OverflowException) {
606 failed = true;
608 if (failed)
609 return 1;
611 try {
612 a = 0;
613 failed = false;
614 checked {
615 b = (ushort)a;
617 } catch (OverflowException) {
618 failed = true;
620 if (failed)
621 return 2;
623 try {
624 a = System.UInt16.MaxValue + 1;
625 failed = true;
626 checked {
627 b = (ushort)a;
629 } catch (OverflowException) {
630 failed = false;
632 if (failed)
633 return 3;
635 try {
636 a = -1;
637 failed = true;
638 checked {
639 b = (ushort)a;
641 } catch (OverflowException) {
642 failed = false;
644 if (failed)
645 return 4;
647 try {
648 double d = 0;
649 failed = false;
650 checked {
651 b = (ushort)d;
653 } catch (OverflowException) {
654 failed = true;
656 if (failed)
657 return 5;
659 try {
660 double d = System.UInt16.MaxValue;
661 failed = false;
662 checked {
663 b = (ushort)d;
665 } catch (OverflowException) {
666 failed = true;
668 if (failed)
669 return 6;
671 try {
672 double d = -1;
673 failed = true;
674 checked {
675 b = (ushort)d;
677 } catch (OverflowException) {
678 failed = false;
680 if (failed)
681 return 7;
683 try {
684 double d = System.UInt16.MaxValue + 1.0;
685 failed = true;
686 checked {
687 b = (ushort)d;
689 } catch (OverflowException) {
690 failed = false;
692 if (failed)
693 return 8;
695 try {
696 l = System.UInt16.MaxValue;
697 failed = false;
698 checked {
699 b = (ushort)l;
701 } catch (OverflowException) {
702 failed = true;
704 if (failed)
705 return 9;
707 try {
708 l = 0;
709 failed = false;
710 checked {
711 b = (ushort)l;
713 } catch (OverflowException) {
714 failed = true;
716 if (failed)
717 return 10;
719 try {
720 l = System.UInt16.MaxValue + 1;
721 failed = true;
722 checked {
723 b = (ushort)l;
725 } catch (OverflowException) {
726 failed = false;
728 if (failed)
729 return 11;
731 try {
732 l = -1;
733 failed = true;
734 checked {
735 b = (ushort)l;
737 } catch (OverflowException) {
738 failed = false;
740 if (failed)
741 return 12;
743 try {
744 ul = 0xfffff;
745 failed = true;
746 checked {
747 b = (ushort)ul;
749 } catch (OverflowException) {
750 failed = false;
752 if (failed)
753 return 13;
755 return 0;
758 public static int test_0_short_cast () {
759 int a;
760 long l;
761 short b;
762 bool failed;
764 try {
765 a = System.UInt16.MaxValue;
766 failed = true;
767 checked {
768 b = (short)a;
770 } catch (OverflowException) {
771 failed = false;
773 if (failed)
774 return 1;
776 try {
777 a = 0;
778 failed = false;
779 checked {
780 b = (short)a;
782 } catch (OverflowException) {
783 failed = true;
785 if (failed)
786 return 2;
788 try {
789 a = System.Int16.MaxValue + 1;
790 failed = true;
791 checked {
792 b = (short)a;
794 } catch (OverflowException) {
795 failed = false;
797 if (failed)
798 return 3;
800 try {
801 a = System.Int16.MinValue - 1;
802 failed = true;
803 checked {
804 b = (short)a;
806 } catch (OverflowException) {
807 failed = false;
809 if (failed)
810 return 4;
812 try {
813 a = -1;
814 failed = false;
815 checked {
816 b = (short)a;
818 } catch (OverflowException) {
819 failed = true;
821 if (failed)
822 return 5;
824 try {
825 a = System.Int16.MinValue;
826 failed = false;
827 checked {
828 b = (short)a;
830 } catch (OverflowException) {
831 failed = true;
833 if (failed)
834 return 6;
836 try {
837 a = System.Int16.MaxValue;
838 failed = false;
839 checked {
840 b = (short)a;
842 } catch (OverflowException) {
843 failed = true;
845 if (failed)
846 return 7;
848 try {
849 a = System.Int16.MaxValue + 1;
850 failed = true;
851 checked {
852 b = (short)a;
854 } catch (OverflowException) {
855 failed = false;
857 if (failed)
858 return 8;
860 try {
861 double d = System.Int16.MaxValue;
862 failed = false;
863 checked {
864 b = (short)d;
866 } catch (OverflowException) {
867 failed = true;
869 if (failed)
870 return 9;
872 try {
873 double d = System.Int16.MinValue;
874 failed = false;
875 checked {
876 b = (short)d;
878 } catch (OverflowException) {
879 failed = true;
881 if (failed)
882 return 10;
884 try {
885 double d = System.Int16.MaxValue + 1.0;
886 failed = true;
887 checked {
888 b = (short)d;
890 } catch (OverflowException) {
891 failed = false;
893 if (failed)
894 return 11;
896 try {
897 double d = System.Int16.MinValue - 1.0;
898 failed = true;
899 checked {
900 b = (short)d;
902 } catch (OverflowException) {
903 failed = false;
905 if (failed)
906 return 12;
908 try {
909 l = System.Int16.MaxValue + 1;
910 failed = true;
911 checked {
912 b = (short)l;
914 } catch (OverflowException) {
915 failed = false;
917 if (failed)
918 return 13;
920 try {
921 l = System.Int16.MaxValue;
922 failed = false;
923 checked {
924 b = (short)l;
926 } catch (OverflowException) {
927 failed = true;
929 if (failed)
930 return 14;
932 try {
933 l = System.Int16.MinValue - 1;
934 failed = true;
935 checked {
936 b = (short)l;
938 } catch (OverflowException) {
939 failed = false;
941 if (failed)
942 return 15;
945 try {
946 l = System.Int16.MinValue;
947 failed = false;
948 checked {
949 b = (short)l;
951 } catch (OverflowException) {
952 failed = true;
954 if (failed)
955 return 16;
957 try {
958 l = 0x00000000ffffffff;
959 failed = true;
960 checked {
961 b = (short)l;
963 } catch (OverflowException) {
964 failed = false;
966 if (failed)
967 return 17;
969 try {
970 ulong ul = 32768;
971 failed = true;
972 checked {
973 b = (short)ul;
975 } catch (OverflowException) {
976 failed = false;
978 if (failed)
979 return 18;
981 return 0;
984 public static int test_0_int_cast () {
985 int a;
986 long l;
987 bool failed;
989 try {
990 double d = System.Int32.MaxValue + 1.0;
991 failed = true;
992 checked {
993 a = (int)d;
995 } catch (OverflowException) {
996 failed = false;
998 if (failed)
999 return 1;
1001 try {
1002 double d = System.Int32.MaxValue;
1003 failed = false;
1004 checked {
1005 a = (int)d;
1007 } catch (OverflowException) {
1008 failed = true;
1010 if (failed)
1011 return 2;
1014 try {
1015 double d = System.Int32.MinValue;
1016 failed = false;
1017 checked {
1018 a = (int)d;
1020 } catch (OverflowException) {
1021 failed = true;
1023 if (failed)
1024 return 3;
1027 try {
1028 double d = System.Int32.MinValue - 1.0;
1029 failed = true;
1030 checked {
1031 a = (int)d;
1033 } catch (OverflowException) {
1034 failed = false;
1036 if (failed)
1037 return 4;
1039 try {
1040 l = System.Int32.MaxValue + (long)1;
1041 failed = true;
1042 checked {
1043 a = (int)l;
1045 } catch (OverflowException) {
1046 failed = false;
1048 if (failed)
1049 return 5;
1051 try {
1052 l = System.Int32.MaxValue;
1053 failed = false;
1054 checked {
1055 a = (int)l;
1057 } catch (OverflowException) {
1058 failed = true;
1060 if (failed)
1061 return 6;
1064 try {
1065 l = System.Int32.MinValue;
1066 failed = false;
1067 checked {
1068 a = (int)l;
1070 } catch (OverflowException) {
1071 failed = true;
1073 if (failed)
1074 return 7;
1077 try {
1078 l = System.Int32.MinValue - (long)1;
1079 failed = true;
1080 checked {
1081 a = (int)l;
1083 } catch (OverflowException) {
1084 failed = false;
1086 if (failed)
1087 return 8;
1089 try {
1090 uint ui = System.UInt32.MaxValue;
1091 failed = true;
1092 checked {
1093 a = (int)ui;
1096 catch (OverflowException) {
1097 failed = false;
1099 if (failed)
1100 return 9;
1102 try {
1103 ulong ul = (long)(System.Int32.MaxValue) + 1;
1104 failed = true;
1105 checked {
1106 a = (int)ul;
1109 catch (OverflowException) {
1110 failed = false;
1112 if (failed)
1113 return 10;
1115 try {
1116 ulong ul = UInt64.MaxValue;
1117 failed = true;
1118 checked {
1119 a = (int)ul;
1122 catch (OverflowException) {
1123 failed = false;
1125 if (failed)
1126 return 11;
1129 int i;
1130 float f = 1.1f;
1131 checked {
1132 i = (int) f;
1136 return 0;
1139 public static int test_0_uint_cast () {
1140 uint a;
1141 long l;
1142 bool failed;
1144 try {
1145 double d = System.UInt32.MaxValue;
1146 failed = false;
1147 checked {
1148 a = (uint)d;
1150 } catch (OverflowException) {
1151 failed = true;
1153 if (failed)
1154 return 1;
1156 try {
1157 double d = System.UInt32.MaxValue + 1.0;
1158 failed = true;
1159 checked {
1160 a = (uint)d;
1162 } catch (OverflowException) {
1163 failed = false;
1165 if (failed)
1166 return 2;
1168 try {
1169 double d = System.UInt32.MinValue;
1170 failed = false;
1171 checked {
1172 a = (uint)d;
1174 } catch (OverflowException) {
1175 failed = true;
1177 if (failed)
1178 return 3;
1180 try {
1181 double d = System.UInt32.MinValue - 1.0;
1182 failed = true;
1183 checked {
1184 a = (uint)d;
1186 } catch (OverflowException) {
1187 failed = false;
1189 if (failed)
1190 return 4;
1192 try {
1193 l = System.UInt32.MaxValue;
1194 failed = false;
1195 checked {
1196 a = (uint)l;
1198 } catch (OverflowException) {
1199 failed = true;
1201 if (failed)
1202 return 5;
1204 try {
1205 l = System.UInt32.MaxValue + (long)1;
1206 failed = true;
1207 checked {
1208 a = (uint)l;
1210 } catch (OverflowException) {
1211 failed = false;
1213 if (failed)
1214 return 6;
1216 try {
1217 l = System.UInt32.MinValue;
1218 failed = false;
1219 checked {
1220 a = (uint)l;
1222 } catch (OverflowException) {
1223 failed = true;
1225 if (failed)
1226 return 7;
1228 try {
1229 l = System.UInt32.MinValue - (long)1;
1230 failed = true;
1231 checked {
1232 a = (uint)l;
1234 } catch (OverflowException) {
1235 failed = false;
1237 if (failed)
1238 return 8;
1240 try {
1241 int i = -1;
1242 failed = true;
1243 checked {
1244 a = (uint)i;
1247 catch (OverflowException) {
1248 failed = false;
1250 if (failed)
1251 return 9;
1254 uint i;
1255 float f = 1.1f;
1256 checked {
1257 i = (uint) f;
1261 return 0;
1264 public static int test_0_long_cast () {
1267 * These tests depend on properties of x86 fp arithmetic so they won't work
1268 * on other platforms.
1271 long a;
1272 bool failed;
1274 try {
1275 double d = System.Int64.MaxValue - 512.0;
1276 failed = true;
1277 checked {
1278 a = (long)d;
1280 } catch (OverflowException) {
1281 failed = false;
1283 if (failed)
1284 return 1;
1286 try {
1287 double d = System.Int64.MaxValue - 513.0;
1288 failed = false;
1289 checked {
1290 a = (long)d;
1292 } catch (OverflowException) {
1293 failed = true;
1295 if (failed)
1296 return 2;
1298 try {
1299 double d = System.Int64.MinValue - 1024.0;
1300 failed = false;
1301 checked {
1302 a = (long)d;
1304 } catch (OverflowException) {
1305 failed = true;
1307 if (failed)
1308 return 3;
1310 try {
1311 double d = System.Int64.MinValue - 1025.0;
1312 failed = true;
1313 checked {
1314 a = (long)d;
1316 } catch (OverflowException) {
1317 failed = false;
1319 if (failed)
1320 return 4;
1324 long i;
1325 float f = 1.1f;
1326 checked {
1327 i = (long) f;
1331 return 0;
1334 public static int test_0_ulong_cast () {
1335 ulong a;
1336 bool failed;
1339 * These tests depend on properties of x86 fp arithmetic so they won't work
1340 * on other platforms.
1344 try {
1345 double d = System.UInt64.MaxValue - 1024.0;
1346 failed = true;
1347 checked {
1348 a = (ulong)d;
1350 } catch (OverflowException) {
1351 failed = false;
1353 if (failed)
1354 return 1;
1356 try {
1357 double d = System.UInt64.MaxValue - 1025.0;
1358 failed = false;
1359 checked {
1360 a = (ulong)d;
1362 } catch (OverflowException) {
1363 failed = true;
1365 if (failed)
1366 return 2;
1369 try {
1370 double d = 0;
1371 failed = false;
1372 checked {
1373 a = (ulong)d;
1375 } catch (OverflowException) {
1376 failed = true;
1378 if (failed)
1379 return 3;
1381 try {
1382 double d = -1;
1383 failed = true;
1384 checked {
1385 a = (ulong)d;
1387 } catch (OverflowException) {
1388 failed = false;
1390 if (failed)
1391 return 4;
1394 ulong i;
1395 float f = 1.1f;
1396 checked {
1397 i = (ulong) f;
1401 try {
1402 int i = -1;
1403 failed = true;
1404 checked {
1405 a = (ulong)i;
1408 catch (OverflowException) {
1409 failed = false;
1411 if (failed)
1412 return 5;
1414 try {
1415 int i = Int32.MinValue;
1416 failed = true;
1417 checked {
1418 a = (ulong)i;
1421 catch (OverflowException) {
1422 failed = false;
1424 if (failed)
1425 return 6;
1427 return 0;
1430 public static int test_0_simple_double_casts () {
1432 double d = 0xffffffff;
1434 if ((uint)d != 4294967295)
1435 return 1;
1438 * These tests depend on properties of x86 fp arithmetic so they won't work
1439 * on other platforms.
1442 d = 0xffffffffffffffff;
1444 if ((ulong)d != 0)
1445 return 2;
1447 if ((ushort)d != 0)
1448 return 3;
1450 if ((byte)d != 0)
1451 return 4;
1454 d = 0xffff;
1456 if ((ushort)d != 0xffff)
1457 return 5;
1459 if ((byte)d != 0xff)
1460 return 6;
1462 return 0;
1465 [Category ("NaClDisable")]
1466 public static int test_0_div_zero () {
1467 int d = 1;
1468 int q = 0;
1469 int val;
1470 bool failed;
1472 try {
1473 failed = true;
1474 val = d / q;
1475 } catch (DivideByZeroException) {
1476 failed = false;
1478 if (failed)
1479 return 1;
1481 try {
1482 failed = true;
1483 val = d % q;
1484 } catch (DivideByZeroException) {
1485 failed = false;
1487 if (failed)
1488 return 2;
1490 try {
1491 failed = true;
1492 q = -1;
1493 d = Int32.MinValue;
1494 val = d / q;
1495 } catch (DivideByZeroException) {
1496 /* wrong exception */
1497 } catch (OverflowException) {
1498 failed = false;
1500 if (failed)
1501 return 3;
1503 try {
1504 failed = true;
1505 q = -1;
1506 d = Int32.MinValue;
1507 val = d % q;
1508 } catch (DivideByZeroException) {
1509 /* wrong exception */
1510 } catch (OverflowException) {
1511 failed = false;
1513 if (failed)
1514 return 4;
1516 return 0;
1519 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1520 static void dummy () {
1523 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1524 static int div_zero_llvm_inner (int i) {
1525 try {
1526 // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1527 dummy ();
1528 return 5 / i;
1529 } catch (Exception ex) {
1530 return 0;
1534 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1535 static long div_zero_llvm_inner_long (long l) {
1536 try {
1537 dummy ();
1538 return (long)5 / l;
1539 } catch (Exception ex) {
1540 return 0;
1544 public static int test_0_div_zero_llvm () {
1545 long r = div_zero_llvm_inner (0);
1546 if (r != 0)
1547 return 1;
1548 r = div_zero_llvm_inner_long (0);
1549 if (r != 0)
1550 return 2;
1551 return 0;
1554 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1555 static int div_overflow_llvm_inner (int i) {
1556 try {
1557 dummy ();
1558 return Int32.MinValue / i;
1559 } catch (Exception ex) {
1560 return 0;
1564 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1565 static long div_overflow_llvm_inner_long (long l) {
1566 try {
1567 dummy ();
1568 return Int64.MinValue / l;
1569 } catch (Exception ex) {
1570 return 0;
1574 public static int test_0_div_overflow_llvm () {
1575 long r = div_overflow_llvm_inner (-1);
1576 if (r != 0)
1577 return 1;
1578 r = div_overflow_llvm_inner_long ((long)-1);
1579 if (r != 0)
1580 return 2;
1581 return 0;
1584 public static int return_55 () {
1585 return 55;
1588 public static int test_0_cfold_div_zero () {
1589 // Test that constant folding doesn't cause division by zero exceptions
1590 if (return_55 () != return_55 ()) {
1591 int d = 1;
1592 int q = 0;
1593 int val;
1595 val = d / q;
1596 val = d % q;
1598 q = -1;
1599 d = Int32.MinValue;
1600 val = d / q;
1602 q = -1;
1603 val = d % q;
1606 return 0;
1609 public static int test_0_udiv_zero () {
1610 uint d = 1;
1611 uint q = 0;
1612 uint val;
1613 bool failed;
1615 try {
1616 failed = true;
1617 val = d / q;
1618 } catch (DivideByZeroException) {
1619 failed = false;
1621 if (failed)
1622 return 1;
1624 try {
1625 failed = true;
1626 val = d % q;
1627 } catch (DivideByZeroException) {
1628 failed = false;
1630 if (failed)
1631 return 2;
1633 return 0;
1636 [Category ("NaClDisable")]
1637 public static int test_0_long_div_zero () {
1638 long d = 1;
1639 long q = 0;
1640 long val;
1641 bool failed;
1643 try {
1644 failed = true;
1645 val = d / q;
1646 } catch (DivideByZeroException) {
1647 failed = false;
1649 if (failed)
1650 return 1;
1652 try {
1653 failed = true;
1654 val = d % q;
1655 } catch (DivideByZeroException) {
1656 failed = false;
1658 if (failed)
1659 return 2;
1661 try {
1662 failed = true;
1663 q = -1;
1664 d = Int64.MinValue;
1665 val = d / q;
1666 } catch (DivideByZeroException) {
1667 /* wrong exception */
1668 } catch (ArithmeticException) {
1669 failed = false;
1671 if (failed)
1672 return 3;
1674 try {
1675 failed = true;
1676 q = -1;
1677 d = Int64.MinValue;
1678 val = d % q;
1679 } catch (DivideByZeroException) {
1680 /* wrong exception */
1681 } catch (ArithmeticException) {
1682 failed = false;
1684 if (failed)
1685 return 4;
1687 return 0;
1690 public static int test_0_ulong_div_zero () {
1691 ulong d = 1;
1692 ulong q = 0;
1693 ulong val;
1694 bool failed;
1696 try {
1697 failed = true;
1698 val = d / q;
1699 } catch (DivideByZeroException) {
1700 failed = false;
1702 if (failed)
1703 return 1;
1705 try {
1706 failed = true;
1707 val = d % q;
1708 } catch (DivideByZeroException) {
1709 failed = false;
1711 if (failed)
1712 return 2;
1714 return 0;
1717 public static int test_0_float_div_zero () {
1718 double d = 1;
1719 double q = 0;
1720 double val;
1721 bool failed;
1723 try {
1724 failed = false;
1725 val = d / q;
1726 } catch (DivideByZeroException) {
1727 failed = true;
1729 if (failed)
1730 return 1;
1732 try {
1733 failed = false;
1734 val = d % q;
1735 } catch (DivideByZeroException) {
1736 failed = true;
1738 if (failed)
1739 return 2;
1741 return 0;
1744 public static int test_0_invalid_unbox () {
1746 int i = 123;
1747 object o = "Some string";
1748 int res = 1;
1750 try {
1751 // Illegal conversion; o contains a string not an int
1752 i = (int) o;
1753 } catch (Exception e) {
1754 if (i ==123)
1755 res = 0;
1758 return res;
1761 // Test that double[] can't be cast to double (bug #46027)
1762 public static int test_0_invalid_unbox_arrays () {
1763 double[] d1 = { 1.0 };
1764 double[][] d2 = { d1 };
1765 Array a = d2;
1767 try {
1768 foreach (double d in a) {
1770 return 1;
1772 catch (InvalidCastException e) {
1773 return 0;
1777 /* bug# 42190, at least mcs generates a leave for the return that
1778 * jumps out of multiple exception clauses: we used to execute just
1779 * one enclosing finally block.
1781 public static int finally_level;
1782 static void do_something () {
1783 int a = 0;
1784 try {
1785 try {
1786 return;
1787 } finally {
1788 a = 1;
1790 } finally {
1791 finally_level++;
1795 public static int test_2_multiple_finally_clauses () {
1796 finally_level = 0;
1797 do_something ();
1798 if (finally_level == 1)
1799 return 2;
1800 return 0;
1803 public static int test_3_checked_cast_un () {
1804 ulong i = 0x8000000034000000;
1805 long j;
1807 try {
1808 checked { j = (long)i; }
1809 } catch (OverflowException) {
1810 j = 2;
1813 if (j != 2)
1814 return 0;
1815 return 3;
1818 public static int test_4_checked_cast () {
1819 long i;
1820 ulong j;
1822 unchecked { i = (long)0x8000000034000000;};
1823 try {
1824 checked { j = (ulong)i; }
1825 } catch (OverflowException) {
1826 j = 3;
1829 if (j != 3)
1830 return 0;
1831 return 4;
1834 static readonly int[] mul_dim_results = new int[] {
1835 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1836 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1837 2, 0, 2, 1, 2, 8,
1838 3, 0, 3, 1, 3, 8,
1839 4, 0, 4, 1, 4, 8,
1840 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1841 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1842 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1845 public static int test_0_multi_dim_array_access () {
1846 int [,] a = System.Array.CreateInstance (typeof (int),
1847 new int [] {3,6}, new int [] {2,2 }) as int[,];
1848 int x, y;
1849 int result_idx = 0;
1850 for (x = 0; x < 8; ++x) {
1851 for (y = 0; y < 9; ++y) {
1852 bool got_ex = false;
1853 try {
1854 a [x, y] = 1;
1855 } catch {
1856 got_ex = true;
1858 if (got_ex) {
1859 if (result_idx >= mul_dim_results.Length)
1860 return -1;
1861 if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1862 return result_idx + 1;
1864 result_idx += 2;
1868 if (result_idx == mul_dim_results.Length)
1869 return 0;
1870 return 200;
1873 static void helper_out_obj (out object o) {
1874 o = (object)"buddy";
1877 static void helper_out_string (out string o) {
1878 o = "buddy";
1881 public static int test_2_array_mismatch () {
1882 string[] a = { "hello", "world" };
1883 object[] b = a;
1884 bool passed = false;
1886 try {
1887 helper_out_obj (out b [1]);
1888 } catch (ArrayTypeMismatchException) {
1889 passed = true;
1891 if (!passed)
1892 return 0;
1893 helper_out_string (out a [1]);
1894 if (a [1] != "buddy")
1895 return 1;
1896 return 2;
1899 public static int test_0_ovf1 () {
1900 int exception = 0;
1902 checked {
1903 try {
1904 ulong a = UInt64.MaxValue - 1;
1905 ulong t = a++;
1906 } catch {
1907 exception = 1;
1910 return exception;
1913 public static int test_1_ovf2 () {
1914 int exception = 0;
1916 checked {
1917 try {
1918 ulong a = UInt64.MaxValue;
1919 ulong t = a++;
1920 } catch {
1921 exception = 1;
1924 return exception;
1927 public static int test_0_ovf3 () {
1928 int exception = 0;
1930 long a = Int64.MaxValue - 1;
1931 checked {
1932 try {
1933 long t = a++;
1934 } catch {
1935 exception = 1;
1938 return exception;
1941 public static int test_1_ovf4 () {
1942 int exception = 0;
1944 long a = Int64.MaxValue;
1945 checked {
1946 try {
1947 long t = a++;
1948 } catch {
1949 exception = 1;
1952 return exception;
1955 public static int test_0_ovf5 () {
1956 int exception = 0;
1958 ulong a = UInt64.MaxValue - 1;
1959 checked {
1960 try {
1961 ulong t = a++;
1962 } catch {
1963 exception = 1;
1966 return exception;
1969 public static int test_1_ovf6 () {
1970 int exception = 0;
1972 ulong a = UInt64.MaxValue;
1973 checked {
1974 try {
1975 ulong t = a++;
1976 } catch {
1977 exception = 1;
1980 return exception;
1983 public static int test_0_ovf7 () {
1984 int exception = 0;
1986 long a = Int64.MinValue + 1;
1987 checked {
1988 try {
1989 long t = a--;
1990 } catch {
1991 exception = 1;
1994 return 0;
1997 public static int test_1_ovf8 () {
1998 int exception = 0;
2000 long a = Int64.MinValue;
2001 checked {
2002 try {
2003 long t = a--;
2004 } catch {
2005 exception = 1;
2008 return exception;
2011 public static int test_0_ovf9 () {
2012 int exception = 0;
2014 ulong a = UInt64.MinValue + 1;
2015 checked {
2016 try {
2017 ulong t = a--;
2018 } catch {
2019 exception = 1;
2022 return exception;
2025 public static int test_1_ovf10 () {
2026 int exception = 0;
2028 ulong a = UInt64.MinValue;
2029 checked {
2030 try {
2031 ulong t = a--;
2032 } catch {
2033 exception = 1;
2036 return exception;
2039 public static int test_0_ovf11 () {
2040 int exception = 0;
2042 int a = Int32.MinValue + 1;
2043 checked {
2044 try {
2045 int t = a--;
2046 } catch {
2047 exception = 1;
2050 return exception;
2053 public static int test_1_ovf12 () {
2054 int exception = 0;
2056 int a = Int32.MinValue;
2057 checked {
2058 try {
2059 int t = a--;
2060 } catch {
2061 exception = 1;
2064 return exception;
2067 public static int test_0_ovf13 () {
2068 int exception = 0;
2070 uint a = 1;
2071 checked {
2072 try {
2073 uint t = a--;
2074 } catch {
2075 exception = 1;
2078 return exception;
2081 public static int test_1_ovf14 () {
2082 int exception = 0;
2084 uint a = 0;
2085 checked {
2086 try {
2087 uint t = a--;
2088 } catch {
2089 exception = 1;
2092 return exception;
2095 public static int test_0_ovf15 () {
2096 int exception = 0;
2098 sbyte a = 126;
2099 checked {
2100 try {
2101 sbyte t = a++;
2102 } catch {
2103 exception = 1;
2106 return exception;
2109 public static int test_1_ovf16 () {
2110 int exception = 0;
2112 sbyte a = 127;
2113 checked {
2114 try {
2115 sbyte t = a++;
2116 } catch {
2117 exception = 1;
2120 return exception;
2123 public static int test_0_ovf17 () {
2124 int exception = 0;
2126 checked {
2127 try {
2128 } catch {
2129 exception = 1;
2132 return exception;
2135 public static int test_0_ovf18 () {
2136 int exception = 0;
2138 int a = 1 << 29;
2139 checked {
2140 try {
2141 int t = a*2;
2142 } catch {
2143 exception = 1;
2146 return exception;
2149 public static int test_1_ovf19 () {
2150 int exception = 0;
2152 int a = 1 << 30;
2153 checked {
2154 try {
2155 int t = a*2;
2156 } catch {
2157 exception = 1;
2160 return exception;
2163 public static int test_0_ovf20 () {
2164 int exception = 0;
2166 checked {
2167 try {
2168 ulong a = 0xffffffffff;
2169 ulong t = a*0x0ffffff;
2170 } catch {
2171 exception = 1;
2174 return exception;
2177 public static int test_1_ovf21 () {
2178 int exception = 0;
2180 ulong a = 0xffffffffff;
2181 checked {
2182 try {
2183 ulong t = a*0x0fffffff;
2184 } catch {
2185 exception = 1;
2188 return exception;
2191 public static int test_1_ovf22 () {
2192 int exception = 0;
2194 long a = Int64.MinValue;
2195 long b = 10;
2196 checked {
2197 try {
2198 long v = a * b;
2199 } catch {
2200 exception = 1;
2203 return exception;
2206 public static int test_1_ovf23 () {
2207 int exception = 0;
2209 long a = 10;
2210 long b = Int64.MinValue;
2211 checked {
2212 try {
2213 long v = a * b;
2214 } catch {
2215 exception = 1;
2218 return exception;
2221 class Broken {
2222 public static int i;
2224 static Broken () {
2225 throw new Exception ("Ugh!");
2228 public static int DoSomething () {
2229 return i;
2233 public static int test_0_exception_in_cctor () {
2234 try {
2235 Broken.DoSomething ();
2237 catch (TypeInitializationException) {
2238 // This will only happen once even if --regression is used
2240 return 0;
2243 public static int test_5_regalloc () {
2244 int i = 0;
2246 try {
2247 for (i = 0; i < 10; ++i) {
2248 if (i == 5)
2249 throw new Exception ();
2252 catch (Exception) {
2253 if (i != 5)
2254 return i;
2257 // Check that variables written in catch clauses are volatile
2258 int j = 0;
2259 try {
2260 throw new Exception ();
2262 catch (Exception) {
2263 j = 5;
2265 if (j != 5)
2266 return 6;
2268 int k = 0;
2269 try {
2270 try {
2271 throw new Exception ();
2273 finally {
2274 k = 5;
2277 catch (Exception) {
2279 if (k != 5)
2280 return 7;
2282 return i;
2285 public static void rethrow () {
2286 try {
2287 throw new ApplicationException();
2288 } catch (ApplicationException) {
2289 try {
2290 throw new OverflowException();
2291 } catch (Exception) {
2292 throw;
2297 // Test that a rethrow rethrows the correct exception
2298 public static int test_0_rethrow_nested () {
2299 try {
2300 rethrow ();
2301 } catch (OverflowException) {
2302 return 0;
2303 } catch (Exception) {
2304 return 1;
2306 return 2;
2309 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2310 public static void rethrow1 () {
2311 throw new Exception ();
2314 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2315 public static void rethrow2 () {
2316 rethrow1 ();
2317 /* This disables tailcall opts */
2318 Console.WriteLine ();
2321 [Category ("!BITCODE")]
2322 public static int test_0_rethrow_stacktrace () {
2323 // Check that rethrowing an exception preserves the original stack trace
2324 try {
2325 try {
2326 rethrow2 ();
2328 catch (Exception ex) {
2329 // Check that each catch clause has its own exception variable
2330 // If not, the throw below will overwrite the exception used
2331 // by the rethrow
2332 try {
2333 throw new DivideByZeroException ();
2335 catch (Exception foo) {
2338 throw;
2341 catch (Exception ex) {
2342 if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2343 return 0;
2346 return 1;
2349 interface IFace {}
2350 class Face : IFace {}
2352 public static int test_1_array_mismatch_2 () {
2353 try {
2354 object [] o = new Face [1];
2355 o [0] = 1;
2356 return 0;
2357 } catch (ArrayTypeMismatchException) {
2358 return 1;
2362 public static int test_1_array_mismatch_3 () {
2363 try {
2364 object [] o = new IFace [1];
2365 o [0] = 1;
2366 return 0;
2367 } catch (ArrayTypeMismatchException) {
2368 return 1;
2372 public static int test_1_array_mismatch_4 () {
2373 try {
2374 object [][] o = new Face [5] [];
2375 o [0] = new object [5];
2377 return 0;
2378 } catch (ArrayTypeMismatchException) {
2379 return 1;
2383 public static int test_0_array_size () {
2384 bool failed;
2386 try {
2387 failed = true;
2388 int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2390 catch (OutOfMemoryException e) {
2391 failed = false;
2393 if (failed)
2394 return 2;
2396 return 0;
2399 struct S {
2400 int i, j, k, l, m, n;
2403 static IntPtr[] addr;
2405 static unsafe void throw_func (int i, S s) {
2406 addr [i] = new IntPtr (&i);
2407 throw new Exception ();
2410 /* Test that arguments are correctly popped off the stack during unwinding */
2411 /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2413 public static int test_0_stack_unwind () {
2414 addr = new IntPtr [1000];
2415 S s = new S ();
2416 for (int j = 0; j < 1000; j++) {
2417 try {
2418 throw_func (j, s);
2420 catch (Exception) {
2423 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2427 static unsafe void get_sp (int i) {
2428 addr [i] = new IntPtr (&i);
2431 /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2432 public static int test_0_throw_unwind () {
2433 addr = new IntPtr [1000];
2434 S s = new S ();
2435 for (int j = 0; j < 1000; j++) {
2436 try {
2437 get_sp (j);
2438 throw new Exception ();
2440 catch (Exception) {
2443 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2446 public static int test_0_regress_73242 () {
2447 int [] arr = new int [10];
2448 for (int i = 0; i < 10; ++i)
2449 arr [i] = 0;
2450 try {
2451 throw new Exception ();
2453 catch {
2455 return 0;
2458 public static int test_0_nullref () {
2459 try {
2460 Array foo = null;
2461 foo.Clone();
2462 } catch (NullReferenceException e) {
2463 return 0;
2465 return 1;
2468 public int amethod () {
2469 return 1;
2472 public static int test_0_nonvirt_nullref_at_clause_start () {
2473 ExceptionTests t = null;
2474 try {
2475 t.amethod ();
2476 } catch (NullReferenceException) {
2477 return 0;
2480 return 1;
2483 public static int throw_only () {
2484 throw new Exception ();
2487 [MethodImpl(MethodImplOptions.NoInlining)]
2488 public static int throw_only2 () {
2489 return throw_only ();
2492 public static int test_0_inline_throw_only () {
2493 try {
2494 return throw_only2 ();
2496 catch (Exception ex) {
2497 return 0;
2501 public static string GetText (string s) {
2502 return s;
2505 public static int throw_only_gettext () {
2506 throw new Exception (GetText ("FOO"));
2509 public static int test_0_inline_throw_only_gettext () {
2510 object o = null;
2511 try {
2512 o = throw_only_gettext ();
2514 catch (Exception ex) {
2515 return 0;
2518 return o != null ? 0 : 1;
2521 // bug #78633
2522 public static int test_0_throw_to_branch_opt_outer_clause () {
2523 int i = 0;
2525 try {
2526 try {
2527 string [] files = new string[1];
2529 string s = files[2];
2530 } finally {
2531 i ++;
2533 } catch {
2536 return (i == 1) ? 0 : 1;
2539 // bug #485721
2540 public static int test_0_try_inside_finally_cmov_opt () {
2541 bool Reconect = false;
2543 object o = new object ();
2545 try {
2547 catch (Exception ExCon) {
2548 if (o != null)
2549 Reconect = true;
2551 try {
2553 catch (Exception Last) {
2556 finally {
2557 if (Reconect == true) {
2558 try {
2560 catch (Exception ex) {
2565 return 0;
2568 public static int test_0_inline_throw () {
2569 try {
2570 inline_throw1 (5);
2571 return 1;
2572 } catch {
2573 return 0;
2577 // for llvm, the end bblock is unreachable
2578 public static int inline_throw1 (int i) {
2579 if (i == 0)
2580 throw new Exception ();
2581 else
2582 return inline_throw2 (i);
2585 public static int inline_throw2 (int i) {
2586 throw new Exception ();
2589 // bug #539550
2590 public static int test_0_lmf_filter () {
2591 try {
2592 // The invoke calls a runtime-invoke wrapper which has a filter clause
2593 #if __MOBILE__
2594 typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2595 #else
2596 typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2597 #endif
2598 } catch (TargetInvocationException) {
2600 return 0;
2603 public static void lmf_filter () {
2604 try {
2605 Connect ();
2607 catch {
2608 throw new NotImplementedException ();
2612 public static void Connect () {
2613 Stop ();
2614 throw new Exception();
2617 public static void Stop () {
2618 try {
2619 lock (null) {}
2621 catch {
2625 private static void do_raise () {
2626 throw new System.Exception ();
2629 private static int int_func (int i) {
2630 return i;
2633 // #559876
2634 public static int test_8_local_deadce_causes () {
2635 int myb = 4;
2637 try {
2638 myb = int_func (8);
2639 do_raise();
2640 myb = int_func (2);
2641 } catch (System.Exception) {
2642 return myb;
2644 return 0;
2647 public static int test_0_except_opt_two_clauses () {
2648 int size;
2649 size = -1;
2650 uint ui = (uint)size;
2651 try {
2652 checked {
2653 uint v = ui * (uint)4;
2655 } catch (OverflowException e) {
2656 return 0;
2657 } catch (Exception) {
2658 return 1;
2661 return 2;
2664 class Child
2666 public virtual long Method()
2668 throw new Exception();
2672 /* #612206 */
2673 public static int test_100_long_vars_in_clauses_initlocals_opt () {
2674 Child c = new Child();
2675 long value = 100;
2676 try {
2677 value = c.Method();
2679 catch {}
2680 return (int)value;
2683 class A {
2684 public object AnObj;
2687 public static void DoSomething (ref object o) {
2690 public static int test_0_ldflda_null () {
2691 A a = null;
2693 try {
2694 DoSomething (ref a.AnObj);
2695 } catch (NullReferenceException) {
2696 return 0;
2699 return 1;
2702 unsafe struct Foo
2704 public int i;
2706 public static Foo* pFoo;
2709 /* MS.NET doesn't seem to throw in this case */
2710 public unsafe static int test_0_ldflda_null_pointer () {
2711 int* pi = &Foo.pFoo->i;
2713 return 0;
2716 static int test_0_try_clause_in_finally_clause_regalloc () {
2717 // Fill up registers with values
2718 object a = new object ();
2719 object[] arr1 = new object [1];
2720 object[] arr2 = new object [1];
2721 object[] arr3 = new object [1];
2722 object[] arr4 = new object [1];
2723 object[] arr5 = new object [1];
2725 for (int i = 0; i < 10; ++i)
2726 arr1 [0] = a;
2727 for (int i = 0; i < 10; ++i)
2728 arr2 [0] = a;
2729 for (int i = 0; i < 10; ++i)
2730 arr3 [0] = a;
2731 for (int i = 0; i < 10; ++i)
2732 arr4 [0] = a;
2733 for (int i = 0; i < 10; ++i)
2734 arr5 [0] = a;
2736 int res = 1;
2737 try {
2738 try_clause_in_finally_clause_regalloc_inner (out res);
2739 } catch (Exception) {
2741 return res;
2744 public static object Throw () {
2745 for (int i = 0; i < 10; ++i)
2747 throw new Exception ();
2750 static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2751 object o = null;
2753 res = 1;
2754 try {
2755 o = Throw ();
2756 } catch (Exception) {
2757 /* Make sure this doesn't branch to the finally */
2758 throw new DivideByZeroException ();
2759 } finally {
2760 try {
2761 /* Make sure o is register allocated */
2762 if (o == null)
2763 res = 0;
2764 else
2765 res = 1;
2766 if (o == null)
2767 res = 0;
2768 else
2769 res = 1;
2770 if (o == null)
2771 res = 0;
2772 else
2773 res = 1;
2774 } catch (DivideByZeroException) {
2779 public static bool t_1835_inner () {
2780 bool a = true;
2781 if (a) throw new Exception();
2782 return true;
2785 [MethodImpl(MethodImplOptions.NoInlining)]
2786 public static bool t_1835_inner_2 () {
2787 bool b = t_1835_inner ();
2788 return b;
2791 public static int test_0_inline_retval_throw_in_branch_1835 () {
2792 try {
2793 t_1835_inner_2 ();
2794 } catch {
2795 return 0;
2797 return 1;
2800 static bool finally_called = false;
2802 static void regress_30472 (int a, int b) {
2803 checked {
2804 try {
2805 int sum = a + b;
2806 } finally {
2807 finally_called = true;
2812 public static int test_0_regress_30472 () {
2813 finally_called = false;
2814 try {
2815 regress_30472 (Int32.MaxValue - 1, 2);
2816 } catch (Exception ex) {
2818 return finally_called ? 0 : 1;
2821 static int array_len_1 = 1;
2823 public static int test_0_bounds_check_negative_constant () {
2824 try {
2825 byte[] arr = new byte [array_len_1];
2826 byte b = arr [-1];
2827 return 1;
2828 } catch {
2830 try {
2831 byte[] arr = new byte [array_len_1];
2832 arr [-1] = 1;
2833 return 2;
2834 } catch {
2836 return 0;
2839 public static int test_0_string_bounds_check_negative_constant () {
2840 try {
2841 string s = "A";
2842 char c = s [-1];
2843 return 1;
2844 } catch {
2846 return 0;
2850 #if !__MOBILE__
2851 class ExceptionTests : Tests
2854 #endif