2004-11-09 Zoltan Varga <vargaz@freemail.hu>
[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
1232                 try {
1233                         double d = System.Int64.MinValue - 1024.0;
1234                         failed = false;                 
1235                         checked {
1236                                 a = (long)d;
1237                         }
1238                 } catch (OverflowException) {
1239                         failed = true;
1240                 }
1241                 if (failed)
1242                         return 3;
1243
1244                 try {
1245                         double d = System.Int64.MinValue - 1025.0;
1246                         failed = true;
1247                         checked {
1248                                 a = (long)d;
1249                         }
1250                 } catch (OverflowException) {
1251                         failed = false;
1252                 }
1253                 if (failed)
1254                         return 4;
1255
1256                 {
1257                         long i; 
1258                         float f = 1.1f;
1259                         checked {
1260                                 i = (long) f;
1261                         }
1262                 }
1263
1264                 return 0;
1265         }
1266
1267         static int test_0_ulong_cast () {
1268                 ulong a;
1269                 bool failed;
1270
1271                 try {
1272                         double d = System.UInt64.MaxValue - 1024.0;
1273                         failed = true;
1274                         checked {
1275                                 a = (ulong)d;
1276                         }
1277                 } catch (OverflowException) {
1278                         failed = false;
1279                 }
1280                 if (failed)
1281                         return 1;
1282
1283                 try {
1284                         double d = System.UInt64.MaxValue - 1025.0;
1285                         failed = false;
1286                         checked {
1287                                 a = (ulong)d;
1288                         }
1289                 } catch (OverflowException) {
1290                         failed = true;
1291                 }
1292                 if (failed)
1293                         return 2;
1294                 
1295
1296                 try {
1297                         double d = 0;
1298                         failed = false;                 
1299                         checked {
1300                                 a = (ulong)d;
1301                         }
1302                 } catch (OverflowException) {
1303                         failed = true;
1304                 }
1305                 if (failed)
1306                         return 3;
1307
1308                 try {
1309                         double d = -1;
1310                         failed = true;
1311                         checked {
1312                                 a = (ulong)d;
1313                         }
1314                 } catch (OverflowException) {
1315                         failed = false;
1316                 }
1317                 if (failed)
1318                         return 4;
1319
1320                 {
1321                         ulong i; 
1322                         float f = 1.1f;
1323                         checked {
1324                                 i = (ulong) f;
1325                         }
1326                 }
1327
1328                 try {
1329                         int i = -1;
1330                         failed = true;
1331                         checked {
1332                                 a = (ulong)i;
1333                         }
1334                 }
1335                 catch (OverflowException) {
1336                         failed = false;
1337                 }
1338                 if (failed)
1339                         return 5;
1340
1341                 try {
1342                         int i = Int32.MinValue;
1343                         failed = true;
1344                         checked {
1345                                 a = (ulong)i;
1346                         }
1347                 }
1348                 catch (OverflowException) {
1349                         failed = false;
1350                 }
1351                 if (failed)
1352                         return 6;
1353
1354                 return 0;
1355         }
1356
1357         static int test_0_simple_double_casts () {
1358
1359                 double d = 0xffffffff;
1360
1361                 if ((uint)d != 4294967295)
1362                         return 1;
1363
1364                 d = 0xffffffffffffffff;
1365
1366                 if ((ulong)d != 0)
1367                         return 2;
1368
1369                 if ((ushort)d != 0)
1370                         return 3;
1371                         
1372                 if ((byte)d != 0)
1373                         return 4;
1374                         
1375                 d = 0xffff;
1376
1377                 if ((ushort)d != 0xffff)
1378                         return 5;
1379                 
1380                 if ((byte)d != 0xff)
1381                         return 6;
1382                         
1383                 return 0;
1384         }
1385         
1386         static int test_0_div_zero () {
1387                 int d = 1;
1388                 int q = 0;
1389                 int val;
1390                 bool failed;
1391
1392                 try {
1393                         failed = true;
1394                         val = d / q;
1395                 } catch (DivideByZeroException) {
1396                         failed = false;
1397                 }
1398                 if (failed)
1399                         return 1;
1400
1401                 try {
1402                         failed = true;
1403                         val = d % q;
1404                 } catch (DivideByZeroException) {
1405                         failed = false;
1406                 }
1407                 if (failed)
1408                         return 2;
1409
1410                 try {
1411                         failed = true;
1412                         q = -1;
1413                         d = Int32.MinValue;
1414                         val = d / q;
1415                 } catch (DivideByZeroException) {
1416                         /* wrong exception */
1417                 } catch (ArithmeticException) {
1418                         failed = false;
1419                 }
1420                 if (failed)
1421                         return 3;
1422
1423                 try {
1424                         failed = true;
1425                         q = -1;
1426                         d = Int32.MinValue;
1427                         val = d % q;
1428                 } catch (DivideByZeroException) {
1429                         /* wrong exception */
1430                 } catch (ArithmeticException) {
1431                         failed = false;
1432                 }
1433                 if (failed)
1434                         return 4;
1435
1436                 return 0;
1437         }
1438
1439         static int return_55 () {
1440                 return 55;
1441         }
1442
1443         static int test_0_cfold_div_zero () {
1444                 // Test that constant folding doesn't cause division by zero exceptions
1445                 if (return_55 () != return_55 ()) {
1446                         int d = 1;
1447                         int q = 0;
1448                         int val;                        
1449
1450                         val = d / q;
1451                         val = d % q;
1452
1453                         q = -1;
1454                         d = Int32.MinValue;
1455                         val = d / q;
1456
1457                         q = -1;
1458                         val = d % q;
1459                 }
1460
1461                 return 0;
1462         }
1463
1464         static int test_0_udiv_zero () {
1465                 uint d = 1;
1466                 uint q = 0;
1467                 uint val;
1468                 bool failed;
1469
1470                 try {
1471                         failed = true;
1472                         val = d / q;
1473                 } catch (DivideByZeroException) {
1474                         failed = false;
1475                 }
1476                 if (failed)
1477                         return 1;
1478
1479                 try {
1480                         failed = true;
1481                         val = d % q;
1482                 } catch (DivideByZeroException) {
1483                         failed = false;
1484                 }
1485                 if (failed)
1486                         return 2;
1487
1488                 return 0;
1489         }
1490
1491         static int test_0_long_div_zero () {
1492                 long d = 1;
1493                 long q = 0;
1494                 long val;
1495                 bool failed;
1496
1497                 try {
1498                         failed = true;
1499                         val = d / q;
1500                 } catch (DivideByZeroException) {
1501                         failed = false;
1502                 }
1503                 if (failed)
1504                         return 1;
1505
1506                 try {
1507                         failed = true;
1508                         val = d % q;
1509                 } catch (DivideByZeroException) {
1510                         failed = false;
1511                 }
1512                 if (failed)
1513                         return 2;
1514
1515                 try {
1516                         failed = true;
1517                         q = -1;
1518                         d = Int64.MinValue;
1519                         val = d / q;
1520                 } catch (DivideByZeroException) {
1521                         /* wrong exception */
1522                 } catch (ArithmeticException) {
1523                         failed = false;
1524                 }
1525                 if (failed)
1526                         return 3;
1527
1528                 try {
1529                         failed = true;
1530                         q = -1;
1531                         d = Int64.MinValue;
1532                         val = d % q;
1533                 } catch (DivideByZeroException) {
1534                         /* wrong exception */
1535                 } catch (ArithmeticException) {
1536                         failed = false;
1537                 }
1538                 if (failed)
1539                         return 4;
1540
1541                 return 0;
1542         }
1543
1544         static int test_0_ulong_div_zero () {
1545                 ulong d = 1;
1546                 ulong q = 0;
1547                 ulong val;
1548                 bool failed;
1549
1550                 try {
1551                         failed = true;
1552                         val = d / q;
1553                 } catch (DivideByZeroException) {
1554                         failed = false;
1555                 }
1556                 if (failed)
1557                         return 1;
1558
1559                 try {
1560                         failed = true;
1561                         val = d % q;
1562                 } catch (DivideByZeroException) {
1563                         failed = false;
1564                 }
1565                 if (failed)
1566                         return 2;
1567
1568                 return 0;
1569         }
1570
1571         static int test_0_float_div_zero () {
1572                 double d = 1;
1573                 double q = 0;
1574                 double val;
1575                 bool failed;
1576
1577                 try {
1578                         failed = false;
1579                         val = d / q;
1580                 } catch (DivideByZeroException) {
1581                         failed = true;
1582                 }
1583                 if (failed)
1584                         return 1;
1585
1586                 try {
1587                         failed = false;
1588                         val = d % q;
1589                 } catch (DivideByZeroException) {
1590                         failed = true;
1591                 }
1592                 if (failed)
1593                         return 2;
1594
1595                 return 0;
1596         }
1597
1598         static int test_0_invalid_unbox () {
1599
1600                 int i = 123;
1601                 object o = "Some string";
1602                 int res = 1;
1603                 
1604                 try {
1605                         // Illegal conversion; o contains a string not an int
1606                         i = (int) o;   
1607                 } catch (Exception e) {
1608                         if (i ==123)
1609                                 res = 0;
1610                 }
1611
1612                 return res;
1613         }
1614
1615         // Test that double[] can't be cast to double (bug #46027)
1616         static int test_0_invalid_unbox_arrays () {
1617                 double[] d1 = { 1.0 };
1618                 double[][] d2 = { d1 };
1619                 Array a = d2;
1620
1621                 try {
1622                         foreach (double d in a) {
1623                         }
1624                         return 1;
1625                 }
1626                 catch (InvalidCastException e) {
1627                         return 0;
1628                 }
1629         }
1630
1631         /* bug# 42190, at least mcs generates a leave for the return that
1632          * jumps out of multiple exception clauses: we used to execute just 
1633          * one enclosing finally block.
1634          */
1635         static int finally_level;
1636         static void do_something () {
1637                 int a = 0;
1638                 try {
1639                         try {
1640                                 return;
1641                         } finally {
1642                                 a = 1;
1643                         }
1644                 } finally {
1645                         finally_level++;
1646                 }
1647         }
1648
1649         static int test_2_multiple_finally_clauses () {
1650                 finally_level = 0;
1651                 do_something ();
1652                 if (finally_level == 1)
1653                         return 2;
1654                 return 0;
1655         }
1656
1657         static int test_3_checked_cast_un () {
1658                 ulong i = 0x8000000034000000;
1659                 long j;
1660
1661                 try {
1662                         checked { j = (long)i; }
1663                 } catch (OverflowException) {
1664                         j = 2;
1665                 }
1666
1667                 if (j != 2)
1668                         return 0;
1669                 return 3;
1670         }
1671         
1672         static int test_4_checked_cast () {
1673                 long i;
1674                 ulong j;
1675
1676                 unchecked { i = (long)0x8000000034000000;};
1677                 try {
1678                         checked { j = (ulong)i; }
1679                 } catch (OverflowException) {
1680                         j = 3;
1681                 }
1682
1683                 if (j != 3)
1684                         return 0;
1685                 return 4;
1686         }
1687
1688         static readonly int[] mul_dim_results = new int[] {
1689                 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1690                 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1691                 2, 0, 2, 1, 2, 8, 
1692                 3, 0, 3, 1, 3, 8, 
1693                 4, 0, 4, 1, 4, 8, 
1694                 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1695                 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1696                 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1697         };
1698
1699         static int test_0_multi_dim_array_access () {
1700                 int [,] a = System.Array.CreateInstance (typeof (int),
1701                         new int [] {3,6}, new int [] {2,2 }) as int[,];
1702                 int x, y;
1703                 int result_idx = 0;
1704                 for (x = 0; x < 8; ++x) {
1705                         for (y = 0; y < 9; ++y) {
1706                                 bool got_ex = false;
1707                                 try {
1708                                         a [x, y] = 1;
1709                                 } catch {
1710                                         got_ex = true;
1711                                 }
1712                                 if (got_ex) {
1713                                         if (result_idx >= mul_dim_results.Length)
1714                                                 return -1;
1715                                         if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1716                                                 return result_idx + 1;
1717                                         }
1718                                         result_idx += 2;
1719                                 }
1720                         }
1721                 }
1722                 if (result_idx == mul_dim_results.Length)
1723                         return 0;
1724                 return 200;
1725         }
1726
1727         static void helper_out_obj (out object o) {
1728                 o = (object)"buddy";
1729         }
1730
1731         static void helper_out_string (out string o) {
1732                 o = "buddy";
1733         }
1734
1735         static int test_2_array_mismatch () {
1736                 string[] a = { "hello", "world" };
1737                 object[] b = a;
1738                 bool passed = false;
1739
1740                 try {
1741                         helper_out_obj (out b [1]);
1742                 } catch (ArrayTypeMismatchException) {
1743                         passed = true;
1744                 }
1745                 if (!passed)
1746                         return 0;
1747                 helper_out_string (out a [1]);
1748                 if (a [1] != "buddy")
1749                         return 1;
1750                 return 2;
1751         }
1752
1753         static int test_0_ovf () {
1754                 int ocount = 0;
1755                 
1756                 checked {
1757
1758                         ocount = 0;
1759                         try {
1760                                 ulong a =  UInt64.MaxValue - 1;
1761                                 ulong t = a++;
1762                         } catch {
1763                                 ocount++;
1764                         }
1765                         if (ocount != 0)
1766                                 return 1;
1767
1768                         ocount = 0;
1769                         try {
1770                                 ulong a =  UInt64.MaxValue;
1771                                 ulong t = a++;
1772                         } catch {
1773                                 ocount++;
1774                         }
1775                         if (ocount != 1)
1776                                 return 2;
1777
1778                         ocount = 0;
1779                         try {
1780                                 long a = Int64.MaxValue - 1;
1781                                 long t = a++;
1782                         } catch {
1783                                 ocount++;
1784                         }
1785                         if (ocount != 0)
1786                                 return 3;
1787
1788                         try {
1789                                 long a = Int64.MaxValue;
1790                                 long t = a++;
1791                         } catch {
1792                                 ocount++;
1793                         }
1794                         if (ocount != 1)
1795                                 return 4;
1796
1797                         ocount = 0;
1798                         try {
1799                                 ulong a = UInt64.MaxValue - 1;
1800                                 ulong t = a++;
1801                         } catch {
1802                                 ocount++;
1803                         }
1804                         if (ocount != 0)
1805                                 return 5;
1806
1807                         try {
1808                                 ulong a = UInt64.MaxValue;
1809                                 ulong t = a++;
1810                         } catch {
1811                                 ocount++;
1812                         }
1813                         if (ocount != 1)
1814                                 return 6;
1815
1816                         ocount = 0;
1817                         try {
1818                                 long a = Int64.MinValue + 1;
1819                                 long t = a--;
1820                         } catch {
1821                                 ocount++;
1822                         }
1823                         if (ocount != 0)
1824                                 return 7;
1825
1826                         ocount = 0;
1827                         try {
1828                                 long a = Int64.MinValue;
1829                                 long t = a--;
1830                         } catch {
1831                                 ocount++;
1832                         }
1833                         if (ocount != 1)
1834                                 return 8;
1835
1836                         ocount = 0;
1837                         try {
1838                                 ulong a = UInt64.MinValue + 1;
1839                                 ulong t = a--;
1840                         } catch {
1841                                 ocount++;
1842                         }
1843                         if (ocount != 0)
1844                                 return 9;
1845
1846                         ocount = 0;
1847                         try {
1848                                 ulong a = UInt64.MinValue;
1849                                 ulong t = a--;
1850                         } catch {
1851                                 ocount++;
1852                         }
1853                         if (ocount != 1)
1854                                 return 10;
1855
1856                         ocount = 0;
1857                         try {
1858                                 int a = Int32.MinValue + 1;
1859                                 int t = a--;
1860                         } catch {
1861                                 ocount++;
1862                         }
1863                         if (ocount != 0)
1864                                 return 11;
1865
1866                         ocount = 0;
1867                         try {
1868                                 int a = Int32.MinValue;
1869                                 int t = a--;
1870                         } catch {
1871                                 ocount++;
1872                         }
1873                         if (ocount != 1)
1874                                 return 12;
1875
1876                         ocount = 0;
1877                         try {
1878                                 uint a = 1;
1879                                 uint t = a--;
1880                         } catch {
1881                                 ocount++;
1882                         }
1883                         if (ocount != 0)
1884                                 return 13;
1885
1886                         ocount = 0;
1887                         try {
1888                                 uint a = 0;
1889                                 uint t = a--;
1890                         } catch {
1891                                 ocount++;
1892                         }
1893                         if (ocount != 1)
1894                                 return 14;
1895
1896                         ocount = 0;
1897                         try {
1898                                 sbyte a = 126;
1899                                 sbyte t = a++;
1900                         } catch {
1901                                 ocount++;
1902                         }
1903                         if (ocount != 0)
1904                                 return 15;
1905
1906                         ocount = 0;
1907                         try {
1908                                 sbyte a = 127;
1909                                 sbyte t = a++;
1910                         } catch {
1911                                 ocount++;
1912                         }
1913                         if (ocount != 1)
1914                                 return 16;
1915
1916                         ocount = 0;
1917                         try {
1918                         } catch {
1919                                 ocount++;
1920                         }
1921                         if (ocount != 0)
1922                                 return 17;
1923
1924                         ocount = 0;
1925                         try {
1926                                 int a = 1 << 29;
1927                                 int t = a*2;
1928                         } catch {
1929                                 ocount++;
1930                         }
1931                         if (ocount != 0)
1932                                 return 18;
1933
1934                         ocount = 0;
1935                         try {
1936                                 int a = 1 << 30;
1937                                 int t = a*2;
1938                         } catch {
1939                                 ocount++;
1940                         }
1941                         if (ocount != 1)
1942                                 return 19;
1943
1944                         ocount = 0;
1945                         try {
1946                                 ulong a = 0xffffffffff;
1947                                 ulong t = a*0x0ffffff;
1948                         } catch {
1949                                 ocount++;
1950                         }
1951                         if (ocount != 0)
1952                                 return 20;
1953
1954                         ocount = 0;
1955                         try {
1956                                 ulong a = 0xffffffffff;
1957                                 ulong t = a*0x0fffffff;
1958                         } catch {
1959                                 ocount++;
1960                         }
1961                         if (ocount != 1)
1962                                 return 21;
1963
1964                         ocount = 0;
1965                         try {
1966                                 long a = Int64.MinValue;
1967                                 long b = 10;
1968                                 long v = a * b;
1969                         } catch {
1970                                 ocount ++;
1971                         }
1972                         if (ocount != 1)
1973                                 return 22;
1974
1975                         ocount = 0;
1976                         try {
1977                                 long a = 10;
1978                                 long b = Int64.MinValue;
1979                                 long v = a * b;
1980                         } catch {
1981                                 ocount ++;
1982                         }
1983                         if (ocount != 1)
1984                                 return 23;
1985                 }
1986                 
1987                 return 0;
1988         }
1989
1990         class Broken {
1991                 static int i;
1992
1993                 static Broken () {
1994                         throw new Exception ("Ugh!");
1995                 }
1996         
1997                 public static int DoSomething () {
1998                         return i;
1999                 }
2000         }
2001
2002         static int test_0_exception_in_cctor () {
2003                 try {
2004                         Broken.DoSomething ();
2005                 }
2006                 catch (TypeInitializationException) {
2007                         // This will only happen once even if --regression is used
2008                 }
2009                 return 0;
2010         }
2011
2012         static int test_5_regalloc () {
2013                 int i = 0;
2014
2015                 try {
2016                         for (i = 0; i < 10; ++i) {
2017                                 if (i == 5)
2018                                         throw new Exception ();
2019                         }
2020                 }
2021                 catch (Exception) {
2022                         if (i != 5)
2023                                 return i;
2024                 }
2025
2026                 // Check that variables written in catch clauses are volatile
2027                 int j = 0;
2028                 try {
2029                         throw new Exception ();
2030                 }
2031                 catch (Exception) {
2032                         j = 5;
2033                 }
2034                 if (j != 5)
2035                         return 6;
2036
2037                 int k = 0;
2038                 try {
2039                         try {
2040                                 throw new Exception ();
2041                         }
2042                         finally {
2043                                 k = 5;
2044                         }
2045                 }
2046                 catch (Exception) {
2047                 }
2048                 if (k != 5)
2049                         return 7;
2050
2051                 return i;
2052         }
2053
2054         /* MarshalByRefObject prevents the methods from being inlined */
2055         class ThrowClass : MarshalByRefObject {
2056                 public static void rethrow1 () {
2057                         throw new Exception ();
2058                 }
2059
2060                 public static void rethrow2 () {
2061                         rethrow1 ();
2062                 }
2063         }
2064
2065         static int test_0_rethrow_stacktrace () {
2066                 // Check that rethrowing an exception preserves the original stack trace
2067                 try {
2068                         try {
2069                                 ThrowClass.rethrow2 ();
2070                         }
2071                         catch (Exception ex) {
2072                                 throw;
2073                         }
2074                 }
2075                 catch (Exception ex) {
2076                         if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2077                                 return 0;
2078                 }
2079
2080                 return 1;
2081         }
2082         
2083         interface IFace {}
2084         class Face : IFace {}
2085                 
2086         static int test_1_array_mismatch_2 () {
2087                 try {
2088                         object [] o = new Face [1];
2089                         o [0] = 1;
2090                         return 0;
2091                 } catch (ArrayTypeMismatchException) {
2092                         return 1;
2093                 }
2094         }
2095         
2096         static int test_1_array_mismatch_3 () {
2097                 try {
2098                         object [] o = new IFace [1];
2099                         o [0] = 1;
2100                         return 0;
2101                 } catch (ArrayTypeMismatchException) {
2102                         return 1;
2103                 }
2104         }
2105         
2106         static int test_1_array_mismatch_4 () {
2107                 try {
2108                         object [][] o = new Face [5] [];
2109                         o [0] = new object [5];
2110                         
2111                         return 0;
2112                 } catch (ArrayTypeMismatchException) {
2113                         return 1;
2114                 }
2115         }
2116
2117         static int test_0_array_size () {
2118                 bool failed;
2119
2120                 try {
2121                         failed = true;
2122                         int[] mem1 = new int [Int32.MaxValue];
2123                 }
2124                 catch (OutOfMemoryException e) {
2125                         failed = false;
2126                 }
2127                 if (failed)
2128                         return 1;
2129
2130                 try {
2131                         failed = true;
2132                         int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2133                 }
2134                 catch (OutOfMemoryException e) {
2135                         failed = false;
2136                 }
2137                 if (failed)
2138                         return 2;
2139
2140                 return 0;
2141         }
2142
2143         struct S {
2144                 int i, j, k, l, m, n;
2145         }
2146
2147         static IntPtr[] addr;
2148
2149         static unsafe void throw_func (int i, S s) {
2150                 addr [i] = new IntPtr (&i);
2151                 throw new Exception ();
2152         }
2153
2154         /* Test that arguments are correctly popped off the stack during unwinding */
2155         static int test_0_stack_unwind () {
2156                 addr = new IntPtr [1000];
2157                 S s = new S ();
2158                 for (int j = 0; j < 1000; j++) {
2159                         try {
2160                                 throw_func (j, s);
2161                         }
2162                         catch (Exception) {
2163                         }
2164                 }
2165                 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2166         }               
2167 }
2168