2008-01-24 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / objects.cs
1 using System;
2 using System.Text;
3 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Runtime.CompilerServices;
6
7 /*
8  * Regression tests for the mono JIT.
9  *
10  * Each test needs to be of the form:
11  *
12  * static int test_<result>_<name> ();
13  *
14  * where <result> is an integer (the value that needs to be returned by
15  * the method to make it pass.
16  * <name> is a user-displayed name used to identify the test.
17  *
18  * The tests can be driven in two ways:
19  * *) running the program directly: Main() uses reflection to find and invoke
20  *      the test methods (this is useful mostly to check that the tests are correct)
21  * *) with the --regression switch of the jit (this is the preferred way since
22  *      all the tests will be run with optimizations on and off)
23  *
24  * The reflection logic could be moved to a .dll since we need at least another
25  * regression test file written in IL code to have better control on how
26  * the IL code looks.
27  */
28
29 struct Simple {
30         public int a;
31         public byte b;
32         public short c;
33         public long d;
34 }
35
36 struct Small {
37         public byte b1;
38         public byte b2;
39 }
40
41 // Size=2, Align=1
42 struct Foo {
43         bool b1;
44         bool b2;
45 }
46
47 struct Large {
48         int one;
49         int two;
50         long three;
51         long four;
52         int five;
53         long six;
54         int seven;
55         long eight;
56         long nine;
57         long ten;
58
59         public void populate ()
60         {
61                 one = 1; two = 2;
62                 three = 3; four = 4;
63                 five = 5; six = 6;
64                 seven = 7; eight = 8;
65                 nine = 9; ten = 10;
66         }
67         public bool check ()
68         {
69                 return one == 1  && two == 2  &&
70                         three == 3  && four == 4  &&
71                         five == 5  && six == 6  &&
72                         seven == 7  && eight == 8  &&
73                         nine == 9  && ten == 10;
74         }
75 }
76
77 class Sample {
78         public int a;
79         public Sample (int v) {
80                 a = v;
81         }
82 }
83
84 [StructLayout ( LayoutKind.Explicit )]
85 struct StructWithBigOffsets {
86                 [ FieldOffset(10000) ] public byte b;
87                 [ FieldOffset(10001) ] public sbyte sb;
88                 [ FieldOffset(11000) ] public short s;
89                 [ FieldOffset(11002) ] public ushort us;
90                 [ FieldOffset(12000) ] public uint i;
91                 [ FieldOffset(12004) ] public int si;
92                 [ FieldOffset(13000) ] public long l;
93                 [ FieldOffset(14000) ] public float f;
94                 [ FieldOffset(15000) ] public double d;
95 }
96
97 enum SampleEnum {
98         A,
99         B,
100         C
101 }
102
103 class Tests {
104
105         static int Main () {
106                 return TestDriver.RunTests (typeof (Tests));
107         }
108         
109         static int test_0_return () {
110                 Simple s;
111                 s.a = 1;
112                 s.b = 2;
113                 s.c = (short)(s.a + s.b);
114                 s.d = 4;
115                 return s.a - 1;
116         }
117
118         static int test_0_string_access () {
119                 string s = "Hello";
120                 if (s [1] != 'e')
121                         return 1;
122                 return 0;
123         }
124
125         static int test_0_string_virtual_call () {
126                 string s = "Hello";
127                 string s2 = s.ToString ();
128                 if (s2 [1] != 'e')
129                         return 1;
130                 return 0;
131         }
132
133         static int test_0_iface_call () {
134                 string s = "Hello";
135                 object o = ((ICloneable)s).Clone ();
136                 return 0;
137         }
138
139         static int test_5_newobj () {
140                 Sample s = new Sample (5);
141                 return s.a;
142         }
143
144         static int test_4_box () {
145                 object obj = 4;
146                 return (int)obj;
147         }
148
149         static int test_0_enum_unbox () {
150                 SampleEnum x = SampleEnum.A;
151                 object o = x;
152                 
153                 int res = 1;
154
155                 res = (int)o;
156                 
157                 return res;
158         }
159         
160         static Simple get_simple (int v) {
161                 Simple r = new Simple ();
162                 r.a = v;
163                 r.b = (byte)(v + 1);
164                 r.c = (short)(v + 2);
165                 r.d = v + 3;
166
167                 return r;
168         }
169
170         static int test_3_return_struct () {
171                 Simple v = get_simple (1);
172
173                 if (v.a != 1)
174                         return 0;
175                 if (v.b != 2)
176                         return 0;
177                 if (v.c != 3)
178                         return 0;
179                 if (v.d != 4)
180                         return 0;
181                 return 3;
182         }
183
184         public virtual Simple v_get_simple (int v)
185         {
186                 return get_simple (v);
187         }
188         
189         static int test_2_return_struct_virtual () {
190                 Tests t = new Tests ();
191                 Simple v = t.v_get_simple (2);
192
193                 if (v.a != 2)
194                         return 0;
195                 if (v.b != 3)
196                         return 0;
197                 if (v.c != 4)
198                         return 0;
199                 if (v.d != 5)
200                         return 0;
201                 return 2;
202         }
203
204         static int receive_simple (int a, Simple v, int b) {
205                 if (v.a != 1)
206                         return 1;
207                 if (v.b != 2)
208                         return 2;
209                 if (v.c != 3)
210                         return 3;
211                 if (v.d != 4)
212                         return 4;
213                 if (a != 7)
214                         return 5;
215                 if (b != 9)
216                         return 6;
217                 return 0;
218         }
219         
220         static int test_5_pass_struct () {
221                 Simple v = get_simple (1);
222                 if (receive_simple (7, v, 9) != 0)
223                         return 0;
224                 if (receive_simple (7, get_simple (1), 9) != 0)
225                         return 1;
226                 return 5;
227         }
228
229         // Test alignment of small structs
230
231         static Small get_small (byte v) {
232                 Small r = new Small ();
233         
234                 r.b1 = v;
235                 r.b2 = (byte)(v + 1);
236
237                 return r;
238         }
239
240         static Small return_small (Small s) {
241                 return s;
242         }
243
244         static int receive_small (int a, Small v, int b) {
245                 if (v.b1 != 1)
246                         return 1;
247                 if (v.b2 != 2)
248                         return 2;
249                 return 0;
250         }
251
252         static int receive_small_sparc_many_args (int a, int a2, int a3, int a4, int a5, int a6, Small v, int b) {
253                 if (v.b1 != 1)
254                         return 1;
255                 if (v.b2 != 2)
256                         return 2;
257                 return 0;
258         }
259
260         static int test_5_pass_small_struct () {
261                 Small v = get_small (1);
262                 if (receive_small (7, v, 9) != 0)
263                         return 0;
264                 if (receive_small (7, get_small (1), 9) != 0)
265                         return 1;
266                 if (receive_small_sparc_many_args (1, 2, 3, 4, 5, 6, v, 9) != 0)
267                         return 2;
268                 v = return_small (v);
269                 if (v.b1 != 1)
270                         return 3;
271                 if (v.b2 != 2)
272                         return 4;
273                 return 5;
274         }
275
276         // 64-bits, 32-bit aligned
277         struct struct1 {
278                 public int      a;
279                 public int      b;
280         };
281
282         static int check_struct1(struct1 x) {
283                 if (x.a != 1)
284                         return 1;
285                 if (x.b != 2)
286                         return 2;
287                 return 0;
288         }
289
290         static int pass_struct1(int a, int b, struct1 x) {
291                 if (a != 3)
292                         return 3;
293                 if (b != 4)
294                         return 4;
295                 return check_struct1(x);
296         }
297
298         static int pass_struct1(int a, struct1 x) {
299                 if (a != 3)
300                         return 3;
301                 return check_struct1(x);
302         }
303
304         static int pass_struct1(struct1 x) {
305                 return check_struct1(x);
306         }
307
308         static int test_0_struct1_args () {
309                 int r;
310                 struct1 x;
311
312                 x.a = 1;
313                 x.b = 2;
314                 if ((r = check_struct1(x)) != 0)
315                         return r;
316                 if ((r = pass_struct1(x)) != 0)
317                         return r + 10;
318                 if ((r = pass_struct1(3, x)) != 0)
319                         return r + 20;
320                 if ((r = pass_struct1(3, 4, x)) != 0)
321                         return r + 30;
322                 return 0;
323         }
324
325         // 64-bits, 64-bit aligned
326         struct struct2 {
327                 public long     a;
328         };
329
330         static int check_struct2(struct2 x) {
331                 if (x.a != 1)
332                         return 1;
333                 return 0;
334         }
335
336         static int pass_struct2(int a, int b, int c, struct2 x) {
337                 if (a != 3)
338                         return 3;
339                 if (b != 4)
340                         return 4;
341                 if (c != 5)
342                         return 5;
343                 return check_struct2(x);
344         }
345
346         static int pass_struct2(int a, int b, struct2 x) {
347                 if (a != 3)
348                         return 3;
349                 if (b != 4)
350                         return 4;
351                 return check_struct2(x);
352         }
353
354         static int pass_struct2(int a, struct2 x) {
355                 if (a != 3)
356                         return 3;
357                 return check_struct2(x);
358         }
359
360         static int pass_struct2(struct2 x) {
361                 return check_struct2(x);
362         }
363
364         static int test_0_struct2_args () {
365                 int r;
366                 struct2 x;
367
368                 x.a = 1;
369                 if ((r = check_struct2(x)) != 0)
370                         return r;
371                 if ((r = pass_struct2(x)) != 0)
372                         return r + 10;
373                 if ((r = pass_struct2(3, x)) != 0)
374                         return r + 20;
375                 if ((r = pass_struct2(3, 4, x)) != 0)
376                         return r + 30;
377                 if ((r = pass_struct2(3, 4, 5, x)) != 0)
378                         return r + 40;
379                 return 0;
380         }
381
382         struct AStruct {
383                 public int i;
384
385                 public AStruct (int i) {
386                         this.i = i;
387                 }
388
389                 public override int GetHashCode () {
390                         return i;
391                 }
392         }
393
394         // Test that vtypes are unboxed during a virtual call
395         static int test_44_unbox_trampoline () {
396                 AStruct s = new AStruct (44);
397                 object o = s;
398                 return o.GetHashCode ();
399         }
400
401         static int test_0_unbox_trampoline2 () {
402                 int i = 12;
403                 object o = i;
404                         
405                 if (i.ToString () != "12")
406                         return 1;
407                 if (((Int32)o).ToString () != "12")
408                         return 2;
409                 if (o.ToString () != "12")
410                         return 3;
411                 return 0;
412         }
413
414         // Test fields with big offsets
415         static int test_0_fields_with_big_offsets () {
416                 StructWithBigOffsets s = new StructWithBigOffsets ();
417                 StructWithBigOffsets s2 = new StructWithBigOffsets ();
418
419                 s.b = 0xde;
420                 s.sb = 0xe;
421                 s.s = 0x12de;
422                 s.us = 0x12da;
423                 s.i = 0xdeadbeef;
424                 s.si = 0xcafe;
425                 s.l = 0xcafebabe;
426                 s.f = 3.14F;
427                 s.d = 3.14;
428
429                 s2.b = s.b;
430                 s2.sb = s.sb;
431                 s2.s = s.s;
432                 s2.us = s.us;
433                 s2.i = s.i;
434                 s2.si = s.si;
435                 s2.l = s.l;
436                 s2.f = s.f;
437                 s2.d = s.d;
438
439                 if (s2.b != 0xde)
440                         return 1;
441                 if (s2.s != 0x12de)
442                         return 2;
443                 if (s2.i != 0xdeadbeef)
444                         return 3;
445                 if (s2.l != 0xcafebabe)
446                         return 4;
447                 if (s2.f != 3.14F)
448                         return 5;
449                 if (s2.d != 3.14)
450                         return 6;
451                 if (s2.sb != 0xe)
452                         return 7;
453                 if (s2.us != 0x12da)
454                         return 9;
455                 if (s2.si != 0xcafe)
456                         return 10;
457
458                 return 0;
459         }
460
461         class TestRegA {
462
463                 long buf_start;
464                 int buf_length, buf_offset;
465
466                 public TestRegA () {
467                         buf_start = 0;
468                         buf_length = 0;
469                         buf_offset = 0;
470                 }
471         
472                 public long Seek (long position) {
473                         long pos = position;
474                         /* interaction between the register allocator and
475                          * allocating arguments to registers */
476                         if (pos >= buf_start && pos <= buf_start + buf_length) {
477                                 buf_offset = (int) (pos - buf_start);
478                                 return pos;
479                         }
480                         return buf_start;
481                 }
482
483         }
484
485         static int test_0_seektest () {
486                 TestRegA t = new TestRegA ();
487                 return (int)t.Seek (0);
488         }
489
490         class Super : ICloneable {
491                 public virtual object Clone () {
492                         return null;
493                 }
494         }
495         class Duper: Super {
496         }
497
498         static int test_0_null_cast () {
499                 object o = null;
500
501                 Super s = (Super)o;
502
503                 return 0;
504         }
505         
506         static int test_0_super_cast () {
507                 Duper d = new Duper ();
508                 Super sup = d;
509                 Object o = d;
510
511                 if (!(o is Super))
512                         return 1;
513                 try {
514                         d = (Duper)sup;
515                 } catch {
516                         return 2;
517                 }
518                 if (!(d is Object))
519                         return 3;
520                 try {
521                         d = (Duper)(object)sup;
522                 } catch {
523                         return 4;
524                 }
525                 return 0;
526         }
527
528         static int test_0_super_cast_array () {
529                 Duper[] d = new Duper [0];
530                 Super[] sup = d;
531                 Object[] o = d;
532
533                 if (!(o is Super[]))
534                         return 1;
535                 try {
536                         d = (Duper[])sup;
537                 } catch {
538                         return 2;
539                 }
540                 if (!(d is Object[]))
541                         return 3;
542                 try {
543                         d = (Duper[])(object[])sup;
544                 } catch {
545                         return 4;
546                 }
547                 return 0;
548         }
549
550         static int test_0_multi_array_cast () {
551                 Duper[,] d = new Duper [1, 1];
552                 object[,] o = d;
553
554                 try {
555                         o [0, 0] = new Super ();
556                         return 1;
557                 }
558                 catch (ArrayTypeMismatchException) {
559                 }
560
561                 return 0;
562         }
563
564         static int test_0_vector_array_cast () {
565                 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
566                 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
567
568                 if (arr1.GetType () != typeof (int[]))
569                         return 1;
570
571                 if (arr2.GetType () == typeof (int[]))
572                         return 2;
573
574                 int[] b;
575
576                 b = (int[])arr1;
577
578                 try {
579                         b = (int[])arr2;
580                         return 3;
581                 }
582                 catch (InvalidCastException) {
583                 }
584
585                 if (arr2 is int[])
586                         return 4;
587
588                 return 0;
589         }
590
591         static int test_0_enum_array_cast () {
592                 TypeCode[] tc = new TypeCode [0];
593                 object[] oa;
594                 ValueType[] vta;
595                 int[] inta;
596                 Array a = tc;
597                 bool ok;
598
599                 if (a is object[])
600                         return 1;
601                 if (a is ValueType[])
602                         return 2;
603                 if (a is Enum[])
604                         return 3;
605                 try {
606                         ok = false;
607                         oa = (object[])a;
608                 } catch {
609                         ok = true;
610                 }
611                 if (!ok)
612                         return 4;
613                 try {
614                         ok = false;
615                         vta = (ValueType[])a;
616                 } catch {
617                         ok = true;
618                 }
619                 if (!ok)
620                         return 5;
621                 try {
622                         ok = true;
623                         inta = (int[])a;
624                 } catch {
625                         ok = false;
626                 }
627                 if (!ok)
628                         return 6;
629                 return 0;
630         }
631
632         static int test_0_more_cast_corner_cases () {
633                 ValueType[] vta = new ValueType [0];
634                 Enum[] ea = new Enum [0];
635                 Array a = vta;
636                 object[] oa;
637                 bool ok;
638
639                 if (!(a is object[]))
640                         return 1;
641                 if (!(a is ValueType[]))
642                         return 2;
643                 if (a is Enum[])
644                         return 3;
645                 a = ea;
646                 if (!(a is object[]))
647                         return 4;
648                 if (!(a is ValueType[]))
649                         return 5;
650                 if (!(a is Enum[]))
651                         return 6;
652
653                 try {
654                         ok = true;
655                         oa = (object[])a;
656                 } catch {
657                         ok = false;
658                 }
659                 if (!ok)
660                         return 7;
661         
662                 try {
663                         ok = true;
664                         oa = (Enum[])a;
665                 } catch {
666                         ok = false;
667                 }
668                 if (!ok)
669                         return 8;
670         
671                 try {
672                         ok = true;
673                         oa = (ValueType[])a;
674                 } catch {
675                         ok = false;
676                 }
677                 if (!ok)
678                         return 9;
679
680                 a = vta;
681                 try {
682                         ok = true;
683                         oa = (object[])a;
684                 } catch {
685                         ok = false;
686                 }
687                 if (!ok)
688                         return 10;
689         
690                 try {
691                         ok = true;
692                         oa = (ValueType[])a;
693                 } catch {
694                         ok = false;
695                 }
696                 if (!ok)
697                         return 11;
698         
699                 try {
700                         ok = false;
701                         vta = (Enum[])a;
702                 } catch {
703                         ok = true;
704                 }
705                 if (!ok)
706                         return 12;
707                 return 0;
708         }
709
710         static int test_0_cast_iface_array () {
711                 object o = new ICloneable [0];
712                 object o2 = new Duper [0];
713                 object t;
714                 bool ok;
715
716                 if (!(o is object[]))
717                         return 1;
718                 if (!(o2 is ICloneable[]))
719                         return 2;
720
721                 try {
722                         ok = true;
723                         t = (object[])o;
724                 } catch {
725                         ok = false;
726                 }
727                 if (!ok)
728                         return 3;
729         
730                 try {
731                         ok = true;
732                         t = (ICloneable[])o2;
733                 } catch {
734                         ok = false;
735                 }
736                 if (!ok)
737                         return 4;
738
739                 try {
740                         ok = true;
741                         t = (ICloneable[])o;
742                 } catch {
743                         ok = false;
744                 }
745                 if (!ok)
746                         return 5;
747
748                 if (!(o is ICloneable[]))
749                         return 6;
750
751                 /* add tests for interfaces that 'inherit' interfaces */
752                 return 0;
753         }
754
755         private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
756
757         private static int AbsoluteDays (int year, int month, int day)
758         {
759                 int temp = 0, m = 1;
760                 int[] days = daysmonthleap;
761                 while (m < month)
762                         temp += days[m++];
763                 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
764         }
765
766         static int test_719162_complex_div () {
767                 int adays = AbsoluteDays (1970, 1, 1);
768                 return adays;
769         }
770
771         delegate int GetIntDel ();
772
773         static int return4 () {
774                 return 4;
775         }
776
777         int return5 () {
778                 return 5;
779         }
780
781         static int test_2_static_delegate () {
782                 GetIntDel del = new GetIntDel (return4);
783                 int v = del ();
784                 if (v != 4)
785                         return 0;
786                 return 2;
787         }
788
789         static int test_2_instance_delegate () {
790                 Tests t = new Tests ();
791                 GetIntDel del = new GetIntDel (t.return5);
792                 int v = del ();
793                 if (v != 5)
794                         return 0;
795                 return 2;
796         }
797
798         static int test_1_store_decimal () {
799                 decimal[,] a = {{1}};
800
801                 if (a[0,0] != 1m)
802                         return 0;
803                 return 1;
804         }
805
806         static int test_2_intptr_stobj () {
807                 System.IntPtr [] arr = { new System.IntPtr () };
808
809                 if (arr [0] != (System.IntPtr)0)
810                         return 1;
811                 return 2;
812         }
813
814         static int llmult (int a, int b, int c, int d) {
815                 return a + b + c + d;
816         }
817
818         /* 
819          * Test that evaluation of complex arguments does not overwrite the
820          * arguments already in outgoing registers.
821          */
822         static int test_155_regalloc () {
823                 int a = 10;
824                 int b = 10;
825
826                 int c = 0;
827                 int d = 0;
828                 int[] arr = new int [5];
829
830                 return llmult (arr [c + d], 150, 5, 0);
831         }
832
833         static bool large_struct_test (Large a, Large b, Large c, Large d)
834         {
835                 if (!a.check ()) return false;
836                 if (!b.check ()) return false;
837                 if (!c.check ()) return false;
838                 if (!d.check ()) return false;
839                 return true;
840         }
841
842         static int test_2_large_struct_pass ()
843         {
844                 Large a, b, c, d;
845                 a = new Large ();
846                 b = new Large ();
847                 c = new Large ();
848                 d = new Large ();
849                 a.populate ();
850                 b.populate ();
851                 c.populate ();
852                 d.populate ();
853                 if (large_struct_test (a, b, c, d))
854                         return 2;
855                 return 0;
856         }
857
858         public static unsafe int test_0_pin_string () {
859                 string x = "xxx";
860                 fixed (char *c = x) {
861                         if (*c != 'x')
862                                 return 1;
863                 }
864                 return 0;
865         }
866         
867         public static int my_flags;
868         public static int test_0_and_cmp_static ()
869         {
870                 
871                 /* various forms of test [mem], imm */
872                 
873                 my_flags = 0x01020304;
874                 
875                 if ((my_flags & 0x01020304) == 0)
876                         return 1;
877                 
878                 if ((my_flags & 0x00000304) == 0)
879                         return 2;
880                 
881                 if ((my_flags & 0x00000004) == 0)
882                         return 3;
883                 
884                 if ((my_flags & 0x00000300) == 0)
885                         return 4;
886                 
887                 if ((my_flags & 0x00020000) == 0)
888                         return 5;
889                 
890                 if ((my_flags & 0x01000000) == 0)
891                         return 6;
892                 
893                 return 0;
894         }
895         
896         static byte b;
897         public static int test_0_byte_compares ()
898         {
899                 b = 0xff;
900                 if (b == -1)
901                         return 1;
902                 b = 0;
903                 if (!(b < System.Byte.MaxValue))
904                         return 2;
905                 
906                 if (!(b <= System.Byte.MaxValue))
907                         return 3;
908                 
909                 return 0;
910         }
911
912         public static int test_71_long_shift_right () {
913                 ulong value = 38654838087;
914                 int x = 0;
915                 byte [] buffer = new byte [1];
916                 buffer [x] = ((byte)(value >> x));
917                 return buffer [x];
918         }
919         
920         static long x;
921         public static int test_0_addsub_mem ()
922         {
923                 x = 0;
924                 x += 5;
925                 
926                 if (x != 5)
927                         return 1;
928                 
929                 x -= 10;
930                 
931                 if (x != -5)
932                         return 2;
933                 
934                 return 0;
935         }
936         
937         static ulong y;
938         public static int test_0_sh32_mem ()
939         {
940                 y = 0x0102130405060708;
941                 y >>= 32;
942                 
943                 if (y != 0x01021304)
944                         return 1;
945                 
946                 y = 0x0102130405060708;
947                 y <<= 32;
948                 
949                 if (y != 0x0506070800000000)
950                         return 2;
951                 
952                 x = 0x0102130405060708;
953                 x <<= 32;
954                 
955                 if (x != 0x0506070800000000)
956                         return 2;
957                 
958                 return 0;
959         }
960
961
962         static uint dum_de_dum = 1;
963         static int test_0_long_arg_opt ()
964         {
965                 return Foo (0x1234567887654321, dum_de_dum);
966         }
967         
968         static int Foo (ulong x, ulong y)
969         {
970                 if (x != 0x1234567887654321)
971                         return 1;
972                 
973                 if (y != 1)
974                         return 2;
975                 
976                 return 0;
977         }
978         
979         static int test_0_long_ret_opt ()
980         {
981                 ulong x = X ();
982                 if (x != 0x1234567887654321)
983                         return 1;
984                 ulong y = Y ();
985                 if (y != 1)
986                         return 2;
987                 
988                 return 0;
989         }
990         
991         static ulong X ()
992         {
993                 return 0x1234567887654321;
994         }
995         
996         static ulong Y ()
997         {
998                 return dum_de_dum;
999         }
1000
1001         /* from bug# 71515 */
1002         static int counter = 0;
1003         static bool WriteStuff () {
1004                 counter = 10;
1005                 return true;
1006         }
1007         static int test_0_cond_branch_side_effects () {
1008                 counter = 5;
1009                 if (WriteStuff());
1010                 if (counter == 10)
1011                         return 0;
1012                 return 1;
1013         }
1014
1015         // bug #74992
1016         public static int arg_only_written (string file_name, int[]
1017 ncells ) {
1018                 if (file_name == null)
1019                         return 1;
1020
1021                 ncells = foo ();
1022                 bar (ncells [0]);
1023
1024                 return 0;
1025         }
1026
1027         public static int[] foo () {
1028                 return new int [3];
1029         }
1030
1031         public static void bar (int i) {
1032         }
1033         
1034
1035         public static int test_0_arg_only_written ()
1036         {
1037                 return arg_only_written ("md.in", null);
1038         }               
1039
1040         static long position = 0;
1041
1042         public static int test_4_static_inc_long () {
1043
1044                 int count = 4;
1045
1046                 position = 0;
1047
1048                 position += count;
1049
1050                 return (int)position;
1051         }
1052
1053         struct FooStruct {
1054
1055                 public FooStruct (long l) {
1056                 }
1057         }
1058
1059         static int test_0_calls_opcode_emulation () {
1060                 // Test that emulated opcodes do not clobber arguments already in
1061                 // out registers
1062                 checked {
1063                         long val = 10000;
1064                         new FooStruct (val * 10000);
1065                 }
1066                 return 0;
1067         }
1068
1069         static int test_0_intrins_string_length () {
1070                 string s = "ABC";
1071
1072                 return (s.Length == 3) ? 0 : 1;
1073         }
1074
1075         static int test_0_intrins_string_chars () {
1076                 string s = "ABC";
1077
1078                 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1079         }
1080
1081         static int test_0_intrins_object_gettype () {
1082                 object o = 1;
1083
1084                 return (o.GetType () == typeof (int)) ? 0 : 1;
1085         }
1086
1087         static int test_0_intrins_object_gethashcode () {
1088                 object o = new Object ();
1089
1090                 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1091         }
1092
1093         class FooClass {
1094         }
1095
1096         static int test_0_intrins_object_ctor () {
1097                 object o = new FooClass ();
1098
1099                 return (o != null) ? 0 : 1;
1100         }
1101
1102         static int test_0_intrins_array_rank () {
1103                 int[,] a = new int [10, 10];
1104
1105                 return (a.Rank == 2) ? 0 : 1;
1106         }
1107
1108         static int test_0_intrins_array_length () {
1109                 int[,] a = new int [10, 10];
1110                 Array a2 = a;
1111
1112                 return (a2.Length == 100) ? 0 : 1;
1113         }
1114
1115         static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1116                 int i = RuntimeHelpers.OffsetToStringData;
1117                 
1118                 return i - i;
1119         }
1120
1121         static int test_0_intrins_string_setchar () {
1122                 StringBuilder sb = new StringBuilder ("ABC");
1123
1124                 sb [1] = 'D';
1125
1126                 return sb.ToString () == "ADC" ? 0 : 1;
1127         }
1128
1129         public class Bar {
1130                 bool allowLocation = true;
1131         Foo f = new Foo ();     
1132         }
1133
1134         static int test_0_regress_78990_unaligned_structs () {
1135                 new Bar ();
1136
1137                 return 0;
1138         }
1139
1140         static unsafe int test_97_negative_index () {
1141                 char[] arr = new char[] {'a', 'b'};
1142                 fixed (char *p = arr) {
1143                         char *i = p + 2;
1144                         char a = i[-2];
1145                         return a;
1146                 }
1147         }
1148
1149         /* bug #82281 */
1150         static int test_0_unsigned_right_shift_imm0 () {
1151                 uint temp = 0;
1152                 byte[] data = new byte[256];
1153                 for (int i = 0; i < 1; i ++)
1154                         temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1155                 return 0;
1156         }
1157
1158         class Foo2 {
1159                 public virtual int foo () {
1160                         return 0;
1161                 }
1162         }
1163
1164         sealed class Bar2 : Foo2 {
1165                 public override int foo () {
1166                         return 0;
1167                 }
1168         }
1169
1170         static int test_0_abcrem_check_this_removal () {
1171                 Bar2 b = new Bar2 ();
1172
1173                 // The check_this generated here by the JIT should be removed
1174                 b.foo ();
1175
1176                 return 0;
1177         }
1178
1179         static int invoke_twice (Bar2 b) {
1180                 b.foo ();
1181                 // The check_this generated here by the JIT should be removed
1182                 b.foo ();
1183
1184                 return 0;
1185         }
1186
1187         static int test_0_abcrem_check_this_removal2 () {
1188                 Bar2 b = new Bar2 ();
1189
1190                 invoke_twice (b);
1191
1192                 return 0;
1193         }
1194
1195         /* #346563 */
1196         static int test_0_array_access_64_bit () {
1197                 int[] arr2 = new int [10];
1198                 for (int i = 0; i < 10; ++i)
1199                         arr2 [i] = i;
1200                 string s = "ABCDEFGH";
1201
1202                 byte[] arr = new byte [4];
1203                 arr [0] = 252;
1204                 arr [1] = 255;
1205                 arr [2] = 255;
1206                 arr [3] = 255;
1207
1208                 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1209                 int len2 = - (len + 2);
1210
1211                 // Test array and string access with a 32 bit value whose upper 32 bits are
1212                 // undefined
1213                 // len2 = 3
1214                 if (arr2 [len2] != 2)
1215                         return 1;
1216                 if (s [len2] != 'C')
1217                         return 2;
1218                 return 0;
1219         }
1220 }
1221