Rename the tool
[mono.git] / mono / mini / exceptions.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         static int test_0_catch () {
33                 Exception x = new Exception ();
34                 
35                 try {
36                         throw x;
37                 } catch (Exception e) {
38                         if (e == x)
39                                 return 0;
40                 }
41                 return 1;
42         }
43
44         static int test_0_finally_without_exc () {
45                 int x;
46                 
47                 try {
48                         x = 1;
49                 } catch (Exception e) {
50                         x = 2;
51                 } finally {
52                         x = 0;
53                 }
54                 
55                 return x;
56         }
57
58         static int test_0_finally () {
59                 int x = 1;
60                 
61                 try {
62                         throw new Exception ();
63                 } catch (Exception e) {
64                         x = 2;
65                 } finally {
66                         x = 0;
67                 }
68                 return x;
69         }
70
71         static int test_0_nested_finally () {
72                 int a;
73
74                 try {
75                         a = 1;
76                 } finally {
77                         try {
78                                 a = 2;
79                         } finally {
80                                 a = 0;
81                         }
82                 }
83                 return a;
84         }               
85
86         static int test_0_byte_cast () {
87                 int a;
88                 long l;
89                 ulong ul;
90                 byte b = 0;
91                 bool failed;
92
93                 try {
94                         a = 255;
95                         failed = false;
96                         checked {
97                                 b = (byte)a;
98                         }
99                 } catch (OverflowException) {
100                         failed = true;
101                 }
102                 if (failed)
103                         return 1;
104                 if (b != 255)
105                         return -1;
106
107                 try {
108                         a = 0;
109                         failed = false;
110                         checked {
111                                 b = (byte)a;
112                         }
113                 } catch (OverflowException) {
114                         failed = true;
115                 }
116                 if (failed)
117                         return 2;
118                 if (b != 0)
119                         return -2;
120
121
122                 try {
123                         a = 256;
124                         failed = true;
125                         checked {
126                                 b = (byte)a;
127                         }
128                 } catch (OverflowException) {
129                         failed = false;
130                 }
131                 if (failed)
132                         return 3;
133                 if (b != 0)
134                         return -3;
135
136                 try {
137                         a = -1;
138                         failed = true;
139                         checked {
140                                 b = (byte)a;
141                         }
142                 } catch (OverflowException) {
143                         failed = false;
144                 }
145                 if (failed)
146                         return 4;
147                 if (b != 0)
148                         return -4;
149
150                 try {
151                         double d = 0;
152                         failed = false;
153                         checked {
154                                 b = (byte)d;
155                         }
156                 } catch (OverflowException) {
157                         failed = true;
158                 }
159                 if (failed)
160                         return 5;
161                 if (b != 0)
162                         return -5;
163                 
164                 try {
165                         double d = -1;
166                         failed = true;
167                         checked {
168                                 b = (byte)d;
169                         }
170                 } catch (OverflowException) {
171                         failed = false;
172                 }
173                 if (failed)
174                         return 6;
175                 if (b != 0)
176                         return -6;
177
178                 try {
179                         double d = 255;
180                         failed = false;
181                         checked {
182                                 b = (byte)d;
183                         }
184                 } catch (OverflowException) {
185                         failed = true;
186                 }
187                 if (failed)
188                         return 7;
189                 if (b != 255)
190                         return -7;
191
192                 try {
193                         double d = 256;
194                         failed = true;
195                         checked {
196                                 b = (byte)d;
197                         }
198                 } catch (OverflowException) {
199                         failed = false;
200                 }
201                 if (failed)
202                         return 8;
203                 if (b != 255)
204                         return -8;
205
206                 try {
207                         l = 255;
208                         failed = false;
209                         checked {
210                                 b = (byte)l;
211                         }
212                 } catch (OverflowException) {
213                         failed = true;
214                 }
215                 if (failed)
216                         return 9;
217                 if (b != 255)
218                         return -9;
219
220                 try {
221                         l = 0;
222                         failed = false;
223                         checked {
224                                 b = (byte)l;
225                         }
226                 } catch (OverflowException) {
227                         failed = true;
228                 }
229                 if (failed)
230                         return 10;
231                 if (b != 0)
232                         return -10;
233
234                 try {
235                         l = 256;
236                         failed = true;
237                         checked {
238                                 b = (byte)l;
239                         }
240                 } catch (OverflowException) {
241                         failed = false;
242                 }
243                 if (failed)
244                         return 11;
245                 if (b != 0)
246                         return -11;
247
248                 try {
249                         l = -1;
250                         failed = true;
251                         checked {
252                                 b = (byte)l;
253                         }
254                 } catch (OverflowException) {
255                         failed = false;
256                 }
257                 if (failed)
258                         return 12;
259                 if (b != 0)
260                         return -12;
261
262                 try {
263                         ul = 256;
264                         failed = true;
265                         checked {
266                                 b = (byte)ul;
267                         }
268                 }
269                 catch (OverflowException) {
270                         failed = false;
271                 }
272                 if (failed)
273                         return 13;
274                 if (b != 0)
275                         return -13;
276
277                 return 0;
278         }
279         
280         static int test_0_sbyte_cast () {
281                 int a;
282                 long l;
283                 sbyte b = 0;
284                 bool failed;
285
286                 try {
287                         a = 255;
288                         failed = true;
289                         checked {
290                                 b = (sbyte)a;
291                         }
292                 } catch (OverflowException) {
293                         failed = false;
294                 }
295                 if (failed)
296                         return 1;
297                 if (b != 0)
298                         return -1;
299
300                 try {
301                         a = 0;
302                         failed = false;
303                         checked {
304                                 b = (sbyte)a;
305                         }
306                 } catch (OverflowException) {
307                         failed = true;
308                 }
309                 if (failed)
310                         return 2;
311                 if (b != 0)
312                         return -2;
313
314                 try {
315                         a = 256;
316                         failed = true;
317                         checked {
318                                 b = (sbyte)a;
319                         }
320                 } catch (OverflowException) {
321                         failed = false;
322                 }
323                 if (failed)
324                         return 3;
325                 if (b != 0)
326                         return -3;
327
328                 try {
329                         a = -129;
330                         failed = true;
331                         checked {
332                                 b = (sbyte)a;
333                         }
334                 } catch (OverflowException) {
335                         failed = false;
336                 }
337                 if (failed)
338                         return 4;
339                 if (b != 0)
340                         return -4;
341
342                 try {
343                         a = -1;
344                         failed = false;
345                         checked {
346                                 b = (sbyte)a;
347                         }
348                 } catch (OverflowException) {
349                         failed = true;
350                 }
351                 if (failed)
352                         return 5;
353                 if (b != -1)
354                         return -5;
355
356                 try {
357                         a = -128;
358                         failed = false;
359                         checked {
360                                 b = (sbyte)a;
361                         }
362                 } catch (OverflowException) {
363                         failed = true;
364                 }
365                 if (failed)
366                         return 6;
367                 if (b != -128)
368                         return -6;
369
370                 try {
371                         a = 127;
372                         failed = false;
373                         checked {
374                                 b = (sbyte)a;
375                         }
376                 } catch (OverflowException) {
377                         failed = true;
378                 }
379                 if (failed)
380                         return 7;
381                 if (b != 127)
382                         return -7;
383
384                 try {
385                         a = 128;
386                         failed = true;
387                         checked {
388                                 b = (sbyte)a;
389                         }
390                 } catch (OverflowException) {
391                         failed = false;
392                 }
393                 if (failed)
394                         return 8;
395                 if (b != 127)
396                         return -8;
397
398                 try {
399                         double d = 127;
400                         failed = false;
401                         checked {
402                                 b = (sbyte)d;
403                         }
404                 } catch (OverflowException) {
405                         failed = true;
406                 }
407                 if (failed)
408                         return 9;
409                 if (b != 127)
410                         return -9;
411
412                 try {
413                         double d = -128;
414                         failed = false;
415                         checked {
416                                 b = (sbyte)d;
417                         }
418                 } catch (OverflowException) {
419                         failed = true;
420                 }
421                 if (failed)
422                         return 10;
423                 if (b != -128)
424                         return -10;
425
426                 try {
427                         double d = 128;
428                         failed = true;
429                         checked {
430                                 b = (sbyte)d;
431                         }
432                 } catch (OverflowException) {
433                         failed = false;
434                 }
435                 if (failed)
436                         return 11;
437                 if (b != -128)
438                         return -11;
439
440                 try {
441                         double d = -129;
442                         failed = true;
443                         checked {
444                                 b = (sbyte)d;
445                         }
446                 } catch (OverflowException) {
447                         failed = false;
448                 }
449                 if (failed)
450                         return 12;
451                 if (b != -128)
452                         return -12;
453
454                 try {
455                         l = 255;
456                         failed = true;
457                         checked {
458                                 b = (sbyte)l;
459                         }
460                 } catch (OverflowException) {
461                         failed = false;
462                 }
463                 if (failed)
464                         return 13;
465                 if (b != -128)
466                         return -13;
467
468                 try {
469                         l = 0;
470                         failed = false;
471                         checked {
472                                 b = (sbyte)l;
473                         }
474                 } catch (OverflowException) {
475                         failed = true;
476                 }
477                 if (failed)
478                         return 14;
479                 if (b != 0)
480                         return -14;
481
482                 try {
483                         l = 256;
484                         failed = true;
485                         checked {
486                                 b = (sbyte)l;
487                         }
488                 } catch (OverflowException) {
489                         failed = false;
490                 }
491                 if (failed)
492                         return 15;
493                 if (b != 0)
494                         return -15;
495
496                 try {
497                         l = -129;
498                         failed = true;
499                         checked {
500                                 b = (sbyte)l;
501                         }
502                 } catch (OverflowException) {
503                         failed = false;
504                 }
505                 if (failed)
506                         return 16;
507                 if (b != 0)
508                         return -16;
509
510                 try {
511                         l = -1;
512                         failed = false;
513                         checked {
514                                 b = (sbyte)l;
515                         }
516                 } catch (OverflowException) {
517                         failed = true;
518                 }
519                 if (failed)
520                         return 17;
521                 if (b != -1)
522                         return -17;
523
524                 try {
525                         l = -128;
526                         failed = false;
527                         checked {
528                                 b = (sbyte)l;
529                         }
530                 } catch (OverflowException) {
531                         failed = true;
532                 }
533                 if (failed)
534                         return 18;
535                 if (b != -128)
536                         return -18;
537
538                 try {
539                         l = 127;
540                         failed = false;
541                         checked {
542                                 b = (sbyte)l;
543                         }
544                 } catch (OverflowException) {
545                         failed = true;
546                 }
547                 if (failed)
548                         return 19;
549                 if (b != 127)
550                         return -19;
551
552                 try {
553                         l = 128;
554                         failed = true;
555                         checked {
556                                 b = (sbyte)l;
557                         }
558                 } catch (OverflowException) {
559                         failed = false;
560                 }
561                 if (failed)
562                         return 20;
563                 if (b != 127)
564                         return -20;
565
566                 return 0;
567         }
568
569         static int test_0_ushort_cast () {
570                 int a;
571                 long l;
572                 ulong ul;
573                 ushort b;
574                 bool failed;
575
576                 try {
577                         a = System.UInt16.MaxValue;
578                         failed = false;
579                         checked {
580                                 b = (ushort)a;
581                         }
582                 } catch (OverflowException) {
583                         failed = true;
584                 }
585                 if (failed)
586                         return 1;
587
588                 try {
589                         a = 0;
590                         failed = false;
591                         checked {
592                                 b = (ushort)a;
593                         }
594                 } catch (OverflowException) {
595                         failed = true;
596                 }
597                 if (failed)
598                         return 2;
599
600                 try {
601                         a = System.UInt16.MaxValue + 1;
602                         failed = true;
603                         checked {
604                                 b = (ushort)a;
605                         }
606                 } catch (OverflowException) {
607                         failed = false;
608                 }
609                 if (failed)
610                         return 3;
611
612                 try {
613                         a = -1;
614                         failed = true;
615                         checked {
616                                 b = (ushort)a;
617                         }
618                 } catch (OverflowException) {
619                         failed = false;
620                 }
621                 if (failed)
622                         return 4;
623
624                 try {
625                         double d = 0;
626                         failed = false;
627                         checked {
628                                 b = (ushort)d;
629                         }
630                 } catch (OverflowException) {
631                         failed = true;
632                 }
633                 if (failed)
634                         return 5;
635
636                 try {
637                         double d = System.UInt16.MaxValue;
638                         failed = false;
639                         checked {
640                                 b = (ushort)d;
641                         }
642                 } catch (OverflowException) {
643                         failed = true;
644                 }
645                 if (failed)
646                         return 6;
647
648                 try {
649                         double d = -1;
650                         failed = true;
651                         checked {
652                                 b = (ushort)d;
653                         }
654                 } catch (OverflowException) {
655                         failed = false;
656                 }
657                 if (failed)
658                         return 7;
659
660                 try {
661                         double d = System.UInt16.MaxValue + 1.0;
662                         failed = true;
663                         checked {
664                                 b = (ushort)d;
665                         }
666                 } catch (OverflowException) {
667                         failed = false;
668                 }
669                 if (failed)
670                         return 8;
671
672                 try {
673                         l = System.UInt16.MaxValue;
674                         failed = false;
675                         checked {
676                                 b = (ushort)l;
677                         }
678                 } catch (OverflowException) {
679                         failed = true;
680                 }
681                 if (failed)
682                         return 9;
683
684                 try {
685                         l = 0;
686                         failed = false;
687                         checked {
688                                 b = (ushort)l;
689                         }
690                 } catch (OverflowException) {
691                         failed = true;
692                 }
693                 if (failed)
694                         return 10;
695
696                 try {
697                         l = System.UInt16.MaxValue + 1;
698                         failed = true;
699                         checked {
700                                 b = (ushort)l;
701                         }
702                 } catch (OverflowException) {
703                         failed = false;
704                 }
705                 if (failed)
706                         return 11;
707
708                 try {
709                         l = -1;
710                         failed = true;
711                         checked {
712                                 b = (ushort)l;
713                         }
714                 } catch (OverflowException) {
715                         failed = false;
716                 }
717                 if (failed)
718                         return 12;
719
720                 try {
721                         ul = 0xfffff;
722                         failed = true;
723                         checked {
724                                 b = (ushort)ul;
725                         }
726                 } catch (OverflowException) {
727                         failed = false;
728                 }
729                 if (failed)
730                         return 13;
731
732                 return 0;
733         }
734         
735         static int test_0_short_cast () {
736                 int a;
737                 long l;
738                 short b;
739                 bool failed;
740
741                 try {
742                         a = System.UInt16.MaxValue;
743                         failed = true;
744                         checked {
745                                 b = (short)a;
746                         }
747                 } catch (OverflowException) {
748                         failed = false;
749                 }
750                 if (failed)
751                         return 1;
752
753                 try {
754                         a = 0;
755                         failed = false;
756                         checked {
757                                 b = (short)a;
758                         }
759                 } catch (OverflowException) {
760                         failed = true;
761                 }
762                 if (failed)
763                         return 2;
764
765                 try {
766                         a = System.Int16.MaxValue + 1;
767                         failed = true;
768                         checked {
769                                 b = (short)a;
770                         }
771                 } catch (OverflowException) {
772                         failed = false;
773                 }
774                 if (failed)
775                         return 3;
776
777                 try {
778                         a = System.Int16.MinValue - 1;
779                         failed = true;
780                         checked {
781                                 b = (short)a;
782                         }
783                 } catch (OverflowException) {
784                         failed = false;
785                 }
786                 if (failed)
787                         return 4;
788
789                 try {
790                         a = -1;
791                         failed = false;
792                         checked {
793                                 b = (short)a;
794                         }
795                 } catch (OverflowException) {
796                         failed = true;
797                 }
798                 if (failed)
799                         return 5;
800
801                 try {
802                         a = System.Int16.MinValue;
803                         failed = false;
804                         checked {
805                                 b = (short)a;
806                         }
807                 } catch (OverflowException) {
808                         failed = true;
809                 }
810                 if (failed)
811                         return 6;
812
813                 try {
814                         a = System.Int16.MaxValue;
815                         failed = false;
816                         checked {
817                                 b = (short)a;
818                         }
819                 } catch (OverflowException) {
820                         failed = true;
821                 }
822                 if (failed)
823                         return 7;
824
825                 try {
826                         a = System.Int16.MaxValue + 1;
827                         failed = true;
828                         checked {
829                                 b = (short)a;
830                         }
831                 } catch (OverflowException) {
832                         failed = false;
833                 }
834                 if (failed)
835                         return 8;
836
837                 try {
838                         double d = System.Int16.MaxValue;
839                         failed = false;
840                         checked {
841                                 b = (short)d;
842                         }
843                 } catch (OverflowException) {
844                         failed = true;
845                 }
846                 if (failed)
847                         return 9;
848                 
849                 try {
850                         double d = System.Int16.MinValue;
851                         failed = false;
852                         checked {
853                                 b = (short)d;
854                         }
855                 } catch (OverflowException) {
856                         failed = true;
857                 }
858                 if (failed)
859                         return 10;
860                 
861                 try {
862                         double d = System.Int16.MaxValue + 1.0;
863                         failed = true;
864                         checked {
865                                 b = (short)d;
866                         }
867                 } catch (OverflowException) {
868                         failed = false;
869                 }
870                 if (failed)
871                         return 11;
872
873                 try {
874                         double d = System.Int16.MinValue - 1.0;
875                         failed = true;
876                         checked {
877                                 b = (short)d;
878                         }
879                 } catch (OverflowException) {
880                         failed = false;
881                 }
882                 if (failed)
883                         return 12;
884
885                 try {
886                         l = System.Int16.MaxValue + 1;
887                         failed = true;
888                         checked {
889                                 b = (short)l;
890                         }
891                 } catch (OverflowException) {
892                         failed = false;
893                 }
894                 if (failed)
895                         return 13;
896
897                 try {
898                         l = System.Int16.MaxValue;
899                         failed = false;
900                         checked {
901                                 b = (short)l;
902                         }
903                 } catch (OverflowException) {
904                         failed = true;
905                 }
906                 if (failed)
907                         return 14;
908
909                 try {
910                         l = System.Int16.MinValue - 1;
911                         failed = true;
912                         checked {
913                                 b = (short)l;
914                         }
915                 } catch (OverflowException) {
916                         failed = false;
917                 }
918                 if (failed)
919                         return 15;
920
921                 
922                 try {
923                         l = System.Int16.MinValue;
924                         failed = false;
925                         checked {
926                                 b = (short)l;
927                         }
928                 } catch (OverflowException) {
929                         failed = true;
930                 }
931                 if (failed)
932                         return 16;
933
934                 try {
935                         l = 0x00000000ffffffff;
936                         failed = true;
937                         checked {
938                                 b = (short)l;
939                         }
940                 } catch (OverflowException) {
941                         failed = false;
942                 }
943                 if (failed)
944                         return 17;
945
946                 return 0;
947         }
948         
949         static int test_0_int_cast () {
950                 int a;
951                 long l;
952                 bool failed;
953
954                 try {
955                         double d = System.Int32.MaxValue + 1.0;
956                         failed = true;
957                         checked {
958                                 a = (int)d;
959                         }
960                 } catch (OverflowException) {
961                         failed = false;
962                 }
963                 if (failed)
964                         return 1;
965
966                 try {
967                         double d = System.Int32.MaxValue;
968                         failed = false;
969                         checked {
970                                 a = (int)d;
971                         }
972                 } catch (OverflowException) {
973                         failed = true;
974                 }
975                 if (failed)
976                         return 2;
977                 
978
979                 try {
980                         double d = System.Int32.MinValue;
981                         failed = false;                 
982                         checked {
983                                 a = (int)d;
984                         }
985                 } catch (OverflowException) {
986                         failed = true;
987                 }
988                 if (failed)
989                         return 3;
990
991
992                 try {
993                         double d =  System.Int32.MinValue - 1.0;
994                         failed = true;
995                         checked {
996                                 a = (int)d;
997                         }
998                 } catch (OverflowException) {
999                         failed = false;
1000                 }
1001                 if (failed)
1002                         return 4;
1003
1004                 try {
1005                         l = System.Int32.MaxValue + (long)1;
1006                         failed = true;
1007                         checked {
1008                                 a = (int)l;
1009                         }
1010                 } catch (OverflowException) {
1011                         failed = false;
1012                 }
1013                 if (failed)
1014                         return 5;
1015
1016                 try {
1017                         l = System.Int32.MaxValue;
1018                         failed = false;
1019                         checked {
1020                                 a = (int)l;
1021                         }
1022                 } catch (OverflowException) {
1023                         failed = true;
1024                 }
1025                 if (failed)
1026                         return 6;
1027                 
1028
1029                 try {
1030                         l = System.Int32.MinValue;
1031                         failed = false;                 
1032                         checked {
1033                                 a = (int)l;
1034                         }
1035                 } catch (OverflowException) {
1036                         failed = true;
1037                 }
1038                 if (failed)
1039                         return 7;
1040
1041
1042                 try {
1043                         l =  System.Int32.MinValue - (long)1;
1044                         failed = true;
1045                         checked {
1046                                 a = (int)l;
1047                         }
1048                 } catch (OverflowException) {
1049                         failed = false;
1050                 }
1051                 if (failed)
1052                         return 8;
1053
1054                 try {
1055                         uint ui = System.UInt32.MaxValue;
1056                         failed = true;
1057                         checked {
1058                                 a = (int)ui;
1059                         }
1060                 }
1061                 catch (OverflowException) {
1062                         failed = false;
1063                 }
1064                 if (failed)
1065                         return 9;
1066
1067                 {
1068                         int i; 
1069                         float f = 1.1f;
1070                         checked {
1071                                 i = (int) f;
1072                         }
1073                 }
1074
1075                 return 0;
1076         }
1077
1078         static int test_0_uint_cast () {
1079                 uint a;
1080                 long l;
1081                 bool failed;
1082
1083                 try {
1084                         double d =  System.UInt32.MaxValue;
1085                         failed = false;
1086                         checked {
1087                                 a = (uint)d;
1088                         }
1089                 } catch (OverflowException) {
1090                         failed = true;
1091                 }
1092                 if (failed)
1093                         return 1;
1094
1095                 try {
1096                         double d = System.UInt32.MaxValue + 1.0;
1097                         failed = true;
1098                         checked {
1099                                 a = (uint)d;
1100                         }
1101                 } catch (OverflowException) {
1102                         failed = false;
1103                 }
1104                 if (failed)
1105                         return 2;
1106
1107                 try {
1108                         double d = System.UInt32.MinValue;
1109                         failed = false;
1110                         checked {
1111                                 a = (uint)d;
1112                         }
1113                 } catch (OverflowException) {
1114                         failed = true;
1115                 }
1116                 if (failed)
1117                         return 3;
1118
1119                 try {
1120                         double d = System.UInt32.MinValue - 1.0;
1121                         failed = true;
1122                         checked {
1123                                 a = (uint)d;
1124                         }
1125                 } catch (OverflowException) {
1126                         failed = false;
1127                 }
1128                 if (failed)
1129                         return 4;
1130                 
1131                 try {
1132                         l =  System.UInt32.MaxValue;
1133                         failed = false;
1134                         checked {
1135                                 a = (uint)l;
1136                         }
1137                 } catch (OverflowException) {
1138                         failed = true;
1139                 }
1140                 if (failed)
1141                         return 5;
1142
1143                 try {
1144                         l = System.UInt32.MaxValue + (long)1;
1145                         failed = true;
1146                         checked {
1147                                 a = (uint)l;
1148                         }
1149                 } catch (OverflowException) {
1150                         failed = false;
1151                 }
1152                 if (failed)
1153                         return 6;
1154
1155                 try {
1156                         l = System.UInt32.MinValue;
1157                         failed = false;
1158                         checked {
1159                                 a = (uint)l;
1160                         }
1161                 } catch (OverflowException) {
1162                         failed = true;
1163                 }
1164                 if (failed)
1165                         return 7;
1166
1167                 try {
1168                         l = System.UInt32.MinValue - (long)1;
1169                         failed = true;
1170                         checked {
1171                                 a = (uint)l;
1172                         }
1173                 } catch (OverflowException) {
1174                         failed = false;
1175                 }
1176                 if (failed)
1177                         return 8;
1178
1179                 try {
1180                         int i = -1;
1181                         failed = true;
1182                         checked {
1183                                 a = (uint)i;
1184                         }
1185                 }
1186                 catch (OverflowException) {
1187                         failed = false;
1188                 }
1189                 if (failed)
1190                         return 9;
1191
1192                 {
1193                         uint i; 
1194                         float f = 1.1f;
1195                         checked {
1196                                 i = (uint) f;
1197                         }
1198                 }
1199                 
1200                 return 0;
1201         }
1202         
1203         static int test_0_long_cast () {
1204                 long a;
1205                 bool failed;
1206
1207                 try {
1208                         double d = System.Int64.MaxValue - 512.0;
1209                         failed = true;
1210                         checked {
1211                                 a = (long)d;
1212                         }
1213                 } catch (OverflowException) {
1214                         failed = false;
1215                 }
1216                 if (failed)
1217                         return 1;
1218
1219                 try {
1220                         double d = System.Int64.MaxValue - 513.0;
1221                         failed = false;
1222                         checked {
1223                                 a = (long)d;
1224                         }
1225                 } catch (OverflowException) {
1226                         failed = true;
1227                 }
1228                 if (failed)
1229                         return 2;
1230                 
1231                 try {
1232                         double d = System.Int64.MinValue - 1024.0;
1233                         failed = false;                 
1234                         checked {
1235                                 a = (long)d;
1236                         }
1237                 } catch (OverflowException) {
1238                         failed = true;
1239                 }
1240                 if (failed)
1241                         return 3;
1242
1243                 try {
1244                         double d = System.Int64.MinValue - 1025.0;
1245                         failed = true;
1246                         checked {
1247                                 a = (long)d;
1248                         }
1249                 } catch (OverflowException) {
1250                         failed = false;
1251                 }
1252                 if (failed)
1253                         return 4;
1254
1255                 {
1256                         long i; 
1257                         float f = 1.1f;
1258                         checked {
1259                                 i = (long) f;
1260                         }
1261                 }
1262
1263                 return 0;
1264         }
1265
1266         static int test_0_ulong_cast () {
1267                 ulong a;
1268                 bool failed;
1269
1270                 /*
1271                  * These tests depend on properties of x86 fp arithmetic so they won't work
1272                  * on other platforms.
1273                  */
1274
1275                 /*
1276                 try {
1277                         double d = System.UInt64.MaxValue - 1024.0;
1278                         failed = true;
1279                         checked {
1280                                 a = (ulong)d;
1281                         }
1282                 } catch (OverflowException) {
1283                         failed = false;
1284                 }
1285                 if (failed)
1286                         return 1;
1287
1288                 try {
1289                         double d = System.UInt64.MaxValue - 1025.0;
1290                         failed = false;
1291                         checked {
1292                                 a = (ulong)d;
1293                         }
1294                 } catch (OverflowException) {
1295                         failed = true;
1296                 }
1297                 if (failed)
1298                         return 2;
1299                 */      
1300
1301                 try {
1302                         double d = 0;
1303                         failed = false;                 
1304                         checked {
1305                                 a = (ulong)d;
1306                         }
1307                 } catch (OverflowException) {
1308                         failed = true;
1309                 }
1310                 if (failed)
1311                         return 3;
1312
1313                 try {
1314                         double d = -1;
1315                         failed = true;
1316                         checked {
1317                                 a = (ulong)d;
1318                         }
1319                 } catch (OverflowException) {
1320                         failed = false;
1321                 }
1322                 if (failed)
1323                         return 4;
1324
1325                 {
1326                         ulong i; 
1327                         float f = 1.1f;
1328                         checked {
1329                                 i = (ulong) f;
1330                         }
1331                 }
1332
1333                 try {
1334                         int i = -1;
1335                         failed = true;
1336                         checked {
1337                                 a = (ulong)i;
1338                         }
1339                 }
1340                 catch (OverflowException) {
1341                         failed = false;
1342                 }
1343                 if (failed)
1344                         return 5;
1345
1346                 try {
1347                         int i = Int32.MinValue;
1348                         failed = true;
1349                         checked {
1350                                 a = (ulong)i;
1351                         }
1352                 }
1353                 catch (OverflowException) {
1354                         failed = false;
1355                 }
1356                 if (failed)
1357                         return 6;
1358
1359                 return 0;
1360         }
1361
1362         static int test_0_simple_double_casts () {
1363
1364                 double d = 0xffffffff;
1365
1366                 if ((uint)d != 4294967295)
1367                         return 1;
1368
1369                 /*
1370                  * These tests depend on properties of x86 fp arithmetic so they won't work
1371                  * on other platforms.
1372                  */
1373                 /*
1374                 d = 0xffffffffffffffff;
1375
1376                 if ((ulong)d != 0)
1377                         return 2;
1378
1379                 if ((ushort)d != 0)
1380                         return 3;
1381                         
1382                 if ((byte)d != 0)
1383                         return 4;
1384                 */
1385                         
1386                 d = 0xffff;
1387
1388                 if ((ushort)d != 0xffff)
1389                         return 5;
1390                 
1391                 if ((byte)d != 0xff)
1392                         return 6;
1393                         
1394                 return 0;
1395         }
1396         
1397         static int test_0_div_zero () {
1398                 int d = 1;
1399                 int q = 0;
1400                 int val;
1401                 bool failed;
1402
1403                 try {
1404                         failed = true;
1405                         val = d / q;
1406                 } catch (DivideByZeroException) {
1407                         failed = false;
1408                 }
1409                 if (failed)
1410                         return 1;
1411
1412                 try {
1413                         failed = true;
1414                         val = d % q;
1415                 } catch (DivideByZeroException) {
1416                         failed = false;
1417                 }
1418                 if (failed)
1419                         return 2;
1420
1421                 try {
1422                         failed = true;
1423                         q = -1;
1424                         d = Int32.MinValue;
1425                         val = d / q;
1426                 } catch (DivideByZeroException) {
1427                         /* wrong exception */
1428                 } catch (ArithmeticException) {
1429                         failed = false;
1430                 }
1431                 if (failed)
1432                         return 3;
1433
1434                 try {
1435                         failed = true;
1436                         q = -1;
1437                         d = Int32.MinValue;
1438                         val = d % q;
1439                 } catch (DivideByZeroException) {
1440                         /* wrong exception */
1441                 } catch (ArithmeticException) {
1442                         failed = false;
1443                 }
1444                 if (failed)
1445                         return 4;
1446
1447                 return 0;
1448         }
1449
1450         static int return_55 () {
1451                 return 55;
1452         }
1453
1454         static int test_0_cfold_div_zero () {
1455                 // Test that constant folding doesn't cause division by zero exceptions
1456                 if (return_55 () != return_55 ()) {
1457                         int d = 1;
1458                         int q = 0;
1459                         int val;                        
1460
1461                         val = d / q;
1462                         val = d % q;
1463
1464                         q = -1;
1465                         d = Int32.MinValue;
1466                         val = d / q;
1467
1468                         q = -1;
1469                         val = d % q;
1470                 }
1471
1472                 return 0;
1473         }
1474
1475         static int test_0_udiv_zero () {
1476                 uint d = 1;
1477                 uint q = 0;
1478                 uint val;
1479                 bool failed;
1480
1481                 try {
1482                         failed = true;
1483                         val = d / q;
1484                 } catch (DivideByZeroException) {
1485                         failed = false;
1486                 }
1487                 if (failed)
1488                         return 1;
1489
1490                 try {
1491                         failed = true;
1492                         val = d % q;
1493                 } catch (DivideByZeroException) {
1494                         failed = false;
1495                 }
1496                 if (failed)
1497                         return 2;
1498
1499                 return 0;
1500         }
1501
1502         static int test_0_long_div_zero () {
1503                 long d = 1;
1504                 long q = 0;
1505                 long val;
1506                 bool failed;
1507
1508                 try {
1509                         failed = true;
1510                         val = d / q;
1511                 } catch (DivideByZeroException) {
1512                         failed = false;
1513                 }
1514                 if (failed)
1515                         return 1;
1516
1517                 try {
1518                         failed = true;
1519                         val = d % q;
1520                 } catch (DivideByZeroException) {
1521                         failed = false;
1522                 }
1523                 if (failed)
1524                         return 2;
1525
1526                 try {
1527                         failed = true;
1528                         q = -1;
1529                         d = Int64.MinValue;
1530                         val = d / q;
1531                 } catch (DivideByZeroException) {
1532                         /* wrong exception */
1533                 } catch (ArithmeticException) {
1534                         failed = false;
1535                 }
1536                 if (failed)
1537                         return 3;
1538
1539                 try {
1540                         failed = true;
1541                         q = -1;
1542                         d = Int64.MinValue;
1543                         val = d % q;
1544                 } catch (DivideByZeroException) {
1545                         /* wrong exception */
1546                 } catch (ArithmeticException) {
1547                         failed = false;
1548                 }
1549                 if (failed)
1550                         return 4;
1551
1552                 return 0;
1553         }
1554
1555         static int test_0_ulong_div_zero () {
1556                 ulong d = 1;
1557                 ulong q = 0;
1558                 ulong val;
1559                 bool failed;
1560
1561                 try {
1562                         failed = true;
1563                         val = d / q;
1564                 } catch (DivideByZeroException) {
1565                         failed = false;
1566                 }
1567                 if (failed)
1568                         return 1;
1569
1570                 try {
1571                         failed = true;
1572                         val = d % q;
1573                 } catch (DivideByZeroException) {
1574                         failed = false;
1575                 }
1576                 if (failed)
1577                         return 2;
1578
1579                 return 0;
1580         }
1581
1582         static int test_0_float_div_zero () {
1583                 double d = 1;
1584                 double q = 0;
1585                 double val;
1586                 bool failed;
1587
1588                 try {
1589                         failed = false;
1590                         val = d / q;
1591                 } catch (DivideByZeroException) {
1592                         failed = true;
1593                 }
1594                 if (failed)
1595                         return 1;
1596
1597                 try {
1598                         failed = false;
1599                         val = d % q;
1600                 } catch (DivideByZeroException) {
1601                         failed = true;
1602                 }
1603                 if (failed)
1604                         return 2;
1605
1606                 return 0;
1607         }
1608
1609         static int test_0_invalid_unbox () {
1610
1611                 int i = 123;
1612                 object o = "Some string";
1613                 int res = 1;
1614                 
1615                 try {
1616                         // Illegal conversion; o contains a string not an int
1617                         i = (int) o;   
1618                 } catch (Exception e) {
1619                         if (i ==123)
1620                                 res = 0;
1621                 }
1622
1623                 return res;
1624         }
1625
1626         // Test that double[] can't be cast to double (bug #46027)
1627         static int test_0_invalid_unbox_arrays () {
1628                 double[] d1 = { 1.0 };
1629                 double[][] d2 = { d1 };
1630                 Array a = d2;
1631
1632                 try {
1633                         foreach (double d in a) {
1634                         }
1635                         return 1;
1636                 }
1637                 catch (InvalidCastException e) {
1638                         return 0;
1639                 }
1640         }
1641
1642         /* bug# 42190, at least mcs generates a leave for the return that
1643          * jumps out of multiple exception clauses: we used to execute just 
1644          * one enclosing finally block.
1645          */
1646         static int finally_level;
1647         static void do_something () {
1648                 int a = 0;
1649                 try {
1650                         try {
1651                                 return;
1652                         } finally {
1653                                 a = 1;
1654                         }
1655                 } finally {
1656                         finally_level++;
1657                 }
1658         }
1659
1660         static int test_2_multiple_finally_clauses () {
1661                 finally_level = 0;
1662                 do_something ();
1663                 if (finally_level == 1)
1664                         return 2;
1665                 return 0;
1666         }
1667
1668         static int test_3_checked_cast_un () {
1669                 ulong i = 0x8000000034000000;
1670                 long j;
1671
1672                 try {
1673                         checked { j = (long)i; }
1674                 } catch (OverflowException) {
1675                         j = 2;
1676                 }
1677
1678                 if (j != 2)
1679                         return 0;
1680                 return 3;
1681         }
1682         
1683         static int test_4_checked_cast () {
1684                 long i;
1685                 ulong j;
1686
1687                 unchecked { i = (long)0x8000000034000000;};
1688                 try {
1689                         checked { j = (ulong)i; }
1690                 } catch (OverflowException) {
1691                         j = 3;
1692                 }
1693
1694                 if (j != 3)
1695                         return 0;
1696                 return 4;
1697         }
1698
1699         static readonly int[] mul_dim_results = new int[] {
1700                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1701                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1702                 2, 0, 2, 1, 2, 8, 
1703                 3, 0, 3, 1, 3, 8, 
1704                 4, 0, 4, 1, 4, 8, 
1705                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1706                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1707                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1708         };
1709
1710         static int test_0_multi_dim_array_access () {
1711                 int [,] a = System.Array.CreateInstance (typeof (int),
1712                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1713                 int x, y;
1714                 int result_idx = 0;
1715                 for (x = 0; x < 8; ++x) {
1716                         for (y = 0; y < 9; ++y) {
1717                                 bool got_ex = false;
1718                                 try {
1719                                         a [x, y] = 1;
1720                                 } catch {
1721                                         got_ex = true;
1722                                 }
1723                                 if (got_ex) {
1724                                         if (result_idx >= mul_dim_results.Length)
1725                                                 return -1;
1726                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1727                                                 return result_idx + 1;
1728                                         }
1729                                         result_idx += 2;
1730                                 }
1731                         }
1732                 }
1733                 if (result_idx == mul_dim_results.Length)
1734                         return 0;
1735                 return 200;
1736         }
1737
1738         static void helper_out_obj (out object o) {
1739                 o = (object)"buddy";
1740         }
1741
1742         static void helper_out_string (out string o) {
1743                 o = "buddy";
1744         }
1745
1746         static int test_2_array_mismatch () {
1747                 string[] a = { "hello", "world" };
1748                 object[] b = a;
1749                 bool passed = false;
1750
1751                 try {
1752                         helper_out_obj (out b [1]);
1753                 } catch (ArrayTypeMismatchException) {
1754                         passed = true;
1755                 }
1756                 if (!passed)
1757                         return 0;
1758                 helper_out_string (out a [1]);
1759                 if (a [1] != "buddy")
1760                         return 1;
1761                 return 2;
1762         }
1763
1764         static int test_0_ovf () {
1765                 int ocount = 0;
1766                 
1767                 checked {
1768
1769                         ocount = 0;
1770                         try {
1771                                 ulong a =  UInt64.MaxValue - 1;
1772                                 ulong t = a++;
1773                         } catch {
1774                                 ocount++;
1775                         }
1776                         if (ocount != 0)
1777                                 return 1;
1778
1779                         ocount = 0;
1780                         try {
1781                                 ulong a =  UInt64.MaxValue;
1782                                 ulong t = a++;
1783                         } catch {
1784                                 ocount++;
1785                         }
1786                         if (ocount != 1)
1787                                 return 2;
1788
1789                         ocount = 0;
1790                         try {
1791                                 long a = Int64.MaxValue - 1;
1792                                 long t = a++;
1793                         } catch {
1794                                 ocount++;
1795                         }
1796                         if (ocount != 0)
1797                                 return 3;
1798
1799                         try {
1800                                 long a = Int64.MaxValue;
1801                                 long t = a++;
1802                         } catch {
1803                                 ocount++;
1804                         }
1805                         if (ocount != 1)
1806                                 return 4;
1807
1808                         ocount = 0;
1809                         try {
1810                                 ulong a = UInt64.MaxValue - 1;
1811                                 ulong t = a++;
1812                         } catch {
1813                                 ocount++;
1814                         }
1815                         if (ocount != 0)
1816                                 return 5;
1817
1818                         try {
1819                                 ulong a = UInt64.MaxValue;
1820                                 ulong t = a++;
1821                         } catch {
1822                                 ocount++;
1823                         }
1824                         if (ocount != 1)
1825                                 return 6;
1826
1827                         ocount = 0;
1828                         try {
1829                                 long a = Int64.MinValue + 1;
1830                                 long t = a--;
1831                         } catch {
1832                                 ocount++;
1833                         }
1834                         if (ocount != 0)
1835                                 return 7;
1836
1837                         ocount = 0;
1838                         try {
1839                                 long a = Int64.MinValue;
1840                                 long t = a--;
1841                         } catch {
1842                                 ocount++;
1843                         }
1844                         if (ocount != 1)
1845                                 return 8;
1846
1847                         ocount = 0;
1848                         try {
1849                                 ulong a = UInt64.MinValue + 1;
1850                                 ulong t = a--;
1851                         } catch {
1852                                 ocount++;
1853                         }
1854                         if (ocount != 0)
1855                                 return 9;
1856
1857                         ocount = 0;
1858                         try {
1859                                 ulong a = UInt64.MinValue;
1860                                 ulong t = a--;
1861                         } catch {
1862                                 ocount++;
1863                         }
1864                         if (ocount != 1)
1865                                 return 10;
1866
1867                         ocount = 0;
1868                         try {
1869                                 int a = Int32.MinValue + 1;
1870                                 int t = a--;
1871                         } catch {
1872                                 ocount++;
1873                         }
1874                         if (ocount != 0)
1875                                 return 11;
1876
1877                         ocount = 0;
1878                         try {
1879                                 int a = Int32.MinValue;
1880                                 int t = a--;
1881                         } catch {
1882                                 ocount++;
1883                         }
1884                         if (ocount != 1)
1885                                 return 12;
1886
1887                         ocount = 0;
1888                         try {
1889                                 uint a = 1;
1890                                 uint t = a--;
1891                         } catch {
1892                                 ocount++;
1893                         }
1894                         if (ocount != 0)
1895                                 return 13;
1896
1897                         ocount = 0;
1898                         try {
1899                                 uint a = 0;
1900                                 uint t = a--;
1901                         } catch {
1902                                 ocount++;
1903                         }
1904                         if (ocount != 1)
1905                                 return 14;
1906
1907                         ocount = 0;
1908                         try {
1909                                 sbyte a = 126;
1910                                 sbyte t = a++;
1911                         } catch {
1912                                 ocount++;
1913                         }
1914                         if (ocount != 0)
1915                                 return 15;
1916
1917                         ocount = 0;
1918                         try {
1919                                 sbyte a = 127;
1920                                 sbyte t = a++;
1921                         } catch {
1922                                 ocount++;
1923                         }
1924                         if (ocount != 1)
1925                                 return 16;
1926
1927                         ocount = 0;
1928                         try {
1929                         } catch {
1930                                 ocount++;
1931                         }
1932                         if (ocount != 0)
1933                                 return 17;
1934
1935                         ocount = 0;
1936                         try {
1937                                 int a = 1 << 29;
1938                                 int t = a*2;
1939                         } catch {
1940                                 ocount++;
1941                         }
1942                         if (ocount != 0)
1943                                 return 18;
1944
1945                         ocount = 0;
1946                         try {
1947                                 int a = 1 << 30;
1948                                 int t = a*2;
1949                         } catch {
1950                                 ocount++;
1951                         }
1952                         if (ocount != 1)
1953                                 return 19;
1954
1955                         ocount = 0;
1956                         try {
1957                                 ulong a = 0xffffffffff;
1958                                 ulong t = a*0x0ffffff;
1959                         } catch {
1960                                 ocount++;
1961                         }
1962                         if (ocount != 0)
1963                                 return 20;
1964
1965                         ocount = 0;
1966                         try {
1967                                 ulong a = 0xffffffffff;
1968                                 ulong t = a*0x0fffffff;
1969                         } catch {
1970                                 ocount++;
1971                         }
1972                         if (ocount != 1)
1973                                 return 21;
1974
1975                         ocount = 0;
1976                         try {
1977                                 long a = Int64.MinValue;
1978                                 long b = 10;
1979                                 long v = a * b;
1980                         } catch {
1981                                 ocount ++;
1982                         }
1983                         if (ocount != 1)
1984                                 return 22;
1985
1986                         ocount = 0;
1987                         try {
1988                                 long a = 10;
1989                                 long b = Int64.MinValue;
1990                                 long v = a * b;
1991                         } catch {
1992                                 ocount ++;
1993                         }
1994                         if (ocount != 1)
1995                                 return 23;
1996                 }
1997                 
1998                 return 0;
1999         }
2000
2001         class Broken {
2002                 static int i;
2003
2004                 static Broken () {
2005                         throw new Exception ("Ugh!");
2006                 }
2007         
2008                 public static int DoSomething () {
2009                         return i;
2010                 }
2011         }
2012
2013         static int test_0_exception_in_cctor () {
2014                 try {
2015                         Broken.DoSomething ();
2016                 }
2017                 catch (TypeInitializationException) {
2018                         // This will only happen once even if --regression is used
2019                 }
2020                 return 0;
2021         }
2022
2023         static int test_5_regalloc () {
2024                 int i = 0;
2025
2026                 try {
2027                         for (i = 0; i < 10; ++i) {
2028                                 if (i == 5)
2029                                         throw new Exception ();
2030                         }
2031                 }
2032                 catch (Exception) {
2033                         if (i != 5)
2034                                 return i;
2035                 }
2036
2037                 // Check that variables written in catch clauses are volatile
2038                 int j = 0;
2039                 try {
2040                         throw new Exception ();
2041                 }
2042                 catch (Exception) {
2043                         j = 5;
2044                 }
2045                 if (j != 5)
2046                         return 6;
2047
2048                 int k = 0;
2049                 try {
2050                         try {
2051                                 throw new Exception ();
2052                         }
2053                         finally {
2054                                 k = 5;
2055                         }
2056                 }
2057                 catch (Exception) {
2058                 }
2059                 if (k != 5)
2060                         return 7;
2061
2062                 return i;
2063         }
2064
2065         /* MarshalByRefObject prevents the methods from being inlined */
2066         class ThrowClass : MarshalByRefObject {
2067                 public static void rethrow1 () {
2068                         throw new Exception ();
2069                 }
2070
2071                 public static void rethrow2 () {
2072                         rethrow1 ();
2073                 }
2074         }
2075
2076         static int test_0_rethrow_stacktrace () {
2077                 // Check that rethrowing an exception preserves the original stack trace
2078                 try {
2079                         try {
2080                                 ThrowClass.rethrow2 ();
2081                         }
2082                         catch (Exception ex) {
2083                                 throw;
2084                         }
2085                 }
2086                 catch (Exception ex) {
2087                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2088                                 return 0;
2089                 }
2090
2091                 return 1;
2092         }
2093         
2094         interface IFace {}
2095         class Face : IFace {}
2096                 
2097         static int test_1_array_mismatch_2 () {
2098                 try {
2099                         object [] o = new Face [1];
2100                         o [0] = 1;
2101                         return 0;
2102                 } catch (ArrayTypeMismatchException) {
2103                         return 1;
2104                 }
2105         }
2106         
2107         static int test_1_array_mismatch_3 () {
2108                 try {
2109                         object [] o = new IFace [1];
2110                         o [0] = 1;
2111                         return 0;
2112                 } catch (ArrayTypeMismatchException) {
2113                         return 1;
2114                 }
2115         }
2116         
2117         static int test_1_array_mismatch_4 () {
2118                 try {
2119                         object [][] o = new Face [5] [];
2120                         o [0] = new object [5];
2121                         
2122                         return 0;
2123                 } catch (ArrayTypeMismatchException) {
2124                         return 1;
2125                 }
2126         }
2127
2128         static int test_0_array_size () {
2129                 bool failed;
2130
2131                 try {
2132                         failed = true;
2133                         int[] mem1 = new int [Int32.MaxValue];
2134                 }
2135                 catch (OutOfMemoryException e) {
2136                         failed = false;
2137                 }
2138                 if (failed)
2139                         return 1;
2140
2141                 try {
2142                         failed = true;
2143                         int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2144                 }
2145                 catch (OutOfMemoryException e) {
2146                         failed = false;
2147                 }
2148                 if (failed)
2149                         return 2;
2150
2151                 return 0;
2152         }
2153
2154         struct S {
2155                 int i, j, k, l, m, n;
2156         }
2157
2158         static IntPtr[] addr;
2159
2160         static unsafe void throw_func (int i, S s) {
2161                 addr [i] = new IntPtr (&i);
2162                 throw new Exception ();
2163         }
2164
2165         /* Test that arguments are correctly popped off the stack during unwinding */
2166         static int test_0_stack_unwind () {
2167                 addr = new IntPtr [1000];
2168                 S s = new S ();
2169                 for (int j = 0; j < 1000; j++) {
2170                         try {
2171                                 throw_func (j, s);
2172                         }
2173                         catch (Exception) {
2174                         }
2175                 }
2176                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2177         }               
2178 }
2179