2007-11-14 Yoichi NAKAYAMA <nakayama@pixela.co.jp>
[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         static Simple s_v;
230         static int test_5_pass_static_struct () {
231                 s_v = get_simple (1);
232                 if (receive_simple (7, s_v, 9) != 0)
233                         return 0;
234                 return 5;
235         }
236
237         // Test alignment of small structs
238
239         static Small get_small (byte v) {
240                 Small r = new Small ();
241         
242                 r.b1 = v;
243                 r.b2 = (byte)(v + 1);
244
245                 return r;
246         }
247
248         static Small return_small (Small s) {
249                 return s;
250         }
251
252         static int receive_small (int a, 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 receive_small_sparc_many_args (int a, int a2, int a3, int a4, int a5, int a6, Small v, int b) {
261                 if (v.b1 != 1)
262                         return 1;
263                 if (v.b2 != 2)
264                         return 2;
265                 return 0;
266         }
267
268         static int test_5_pass_small_struct () {
269                 Small v = get_small (1);
270                 if (receive_small (7, v, 9) != 0)
271                         return 0;
272                 if (receive_small (7, get_small (1), 9) != 0)
273                         return 1;
274                 if (receive_small_sparc_many_args (1, 2, 3, 4, 5, 6, v, 9) != 0)
275                         return 2;
276                 v = return_small (v);
277                 if (v.b1 != 1)
278                         return 3;
279                 if (v.b2 != 2)
280                         return 4;
281                 return 5;
282         }
283
284         // 64-bits, 32-bit aligned
285         struct struct1 {
286                 public int      a;
287                 public int      b;
288         };
289
290         static int check_struct1(struct1 x) {
291                 if (x.a != 1)
292                         return 1;
293                 if (x.b != 2)
294                         return 2;
295                 return 0;
296         }
297
298         static int pass_struct1(int a, int b, struct1 x) {
299                 if (a != 3)
300                         return 3;
301                 if (b != 4)
302                         return 4;
303                 return check_struct1(x);
304         }
305
306         static int pass_struct1(int a, struct1 x) {
307                 if (a != 3)
308                         return 3;
309                 return check_struct1(x);
310         }
311
312         static int pass_struct1(struct1 x) {
313                 return check_struct1(x);
314         }
315
316         static int test_0_struct1_args () {
317                 int r;
318                 struct1 x;
319
320                 x.a = 1;
321                 x.b = 2;
322                 if ((r = check_struct1(x)) != 0)
323                         return r;
324                 if ((r = pass_struct1(x)) != 0)
325                         return r + 10;
326                 if ((r = pass_struct1(3, x)) != 0)
327                         return r + 20;
328                 if ((r = pass_struct1(3, 4, x)) != 0)
329                         return r + 30;
330                 return 0;
331         }
332
333         // 64-bits, 64-bit aligned
334         struct struct2 {
335                 public long     a;
336         };
337
338         static int check_struct2(struct2 x) {
339                 if (x.a != 1)
340                         return 1;
341                 return 0;
342         }
343
344         static int pass_struct2(int a, int b, int c, struct2 x) {
345                 if (a != 3)
346                         return 3;
347                 if (b != 4)
348                         return 4;
349                 if (c != 5)
350                         return 5;
351                 return check_struct2(x);
352         }
353
354         static int pass_struct2(int a, int b, struct2 x) {
355                 if (a != 3)
356                         return 3;
357                 if (b != 4)
358                         return 4;
359                 return check_struct2(x);
360         }
361
362         static int pass_struct2(int a, struct2 x) {
363                 if (a != 3)
364                         return 3;
365                 return check_struct2(x);
366         }
367
368         static int pass_struct2(struct2 x) {
369                 return check_struct2(x);
370         }
371
372         static int test_0_struct2_args () {
373                 int r;
374                 struct2 x;
375
376                 x.a = 1;
377                 if ((r = check_struct2(x)) != 0)
378                         return r;
379                 if ((r = pass_struct2(x)) != 0)
380                         return r + 10;
381                 if ((r = pass_struct2(3, x)) != 0)
382                         return r + 20;
383                 if ((r = pass_struct2(3, 4, x)) != 0)
384                         return r + 30;
385                 if ((r = pass_struct2(3, 4, 5, x)) != 0)
386                         return r + 40;
387                 return 0;
388         }
389
390         struct AStruct {
391                 public int i;
392
393                 public AStruct (int i) {
394                         this.i = i;
395                 }
396
397                 public override int GetHashCode () {
398                         return i;
399                 }
400         }
401
402         // Test that vtypes are unboxed during a virtual call
403         static int test_44_unbox_trampoline () {
404                 AStruct s = new AStruct (44);
405                 object o = s;
406                 return o.GetHashCode ();
407         }
408
409         static int test_0_unbox_trampoline2 () {
410                 int i = 12;
411                 object o = i;
412                         
413                 if (i.ToString () != "12")
414                         return 1;
415                 if (((Int32)o).ToString () != "12")
416                         return 2;
417                 if (o.ToString () != "12")
418                         return 3;
419                 return 0;
420         }
421
422         // Test fields with big offsets
423         static int test_0_fields_with_big_offsets () {
424                 StructWithBigOffsets s = new StructWithBigOffsets ();
425                 StructWithBigOffsets s2 = new StructWithBigOffsets ();
426
427                 s.b = 0xde;
428                 s.sb = 0xe;
429                 s.s = 0x12de;
430                 s.us = 0x12da;
431                 s.i = 0xdeadbeef;
432                 s.si = 0xcafe;
433                 s.l = 0xcafebabe;
434                 s.f = 3.14F;
435                 s.d = 3.14;
436
437                 s2.b = s.b;
438                 s2.sb = s.sb;
439                 s2.s = s.s;
440                 s2.us = s.us;
441                 s2.i = s.i;
442                 s2.si = s.si;
443                 s2.l = s.l;
444                 s2.f = s.f;
445                 s2.d = s.d;
446
447                 if (s2.b != 0xde)
448                         return 1;
449                 if (s2.s != 0x12de)
450                         return 2;
451                 if (s2.i != 0xdeadbeef)
452                         return 3;
453                 if (s2.l != 0xcafebabe)
454                         return 4;
455                 if (s2.f != 3.14F)
456                         return 5;
457                 if (s2.d != 3.14)
458                         return 6;
459                 if (s2.sb != 0xe)
460                         return 7;
461                 if (s2.us != 0x12da)
462                         return 9;
463                 if (s2.si != 0xcafe)
464                         return 10;
465
466                 return 0;
467         }
468
469         class TestRegA {
470
471                 long buf_start;
472                 int buf_length, buf_offset;
473
474                 public TestRegA () {
475                         buf_start = 0;
476                         buf_length = 0;
477                         buf_offset = 0;
478                 }
479         
480                 public long Seek (long position) {
481                         long pos = position;
482                         /* interaction between the register allocator and
483                          * allocating arguments to registers */
484                         if (pos >= buf_start && pos <= buf_start + buf_length) {
485                                 buf_offset = (int) (pos - buf_start);
486                                 return pos;
487                         }
488                         return buf_start;
489                 }
490
491         }
492
493         static int test_0_seektest () {
494                 TestRegA t = new TestRegA ();
495                 return (int)t.Seek (0);
496         }
497
498         class Super : ICloneable {
499                 public virtual object Clone () {
500                         return null;
501                 }
502         }
503         class Duper: Super {
504         }
505
506         static int test_0_null_cast () {
507                 object o = null;
508
509                 Super s = (Super)o;
510
511                 return 0;
512         }
513         
514         static int test_0_super_cast () {
515                 Duper d = new Duper ();
516                 Super sup = d;
517                 Object o = d;
518
519                 if (!(o is Super))
520                         return 1;
521                 try {
522                         d = (Duper)sup;
523                 } catch {
524                         return 2;
525                 }
526                 if (!(d is Object))
527                         return 3;
528                 try {
529                         d = (Duper)(object)sup;
530                 } catch {
531                         return 4;
532                 }
533                 return 0;
534         }
535
536         static int test_0_super_cast_array () {
537                 Duper[] d = new Duper [0];
538                 Super[] sup = d;
539                 Object[] o = d;
540
541                 if (!(o is Super[]))
542                         return 1;
543                 try {
544                         d = (Duper[])sup;
545                 } catch {
546                         return 2;
547                 }
548                 if (!(d is Object[]))
549                         return 3;
550                 try {
551                         d = (Duper[])(object[])sup;
552                 } catch {
553                         return 4;
554                 }
555                 return 0;
556         }
557
558         static int test_0_multi_array_cast () {
559                 Duper[,] d = new Duper [1, 1];
560                 object[,] o = d;
561
562                 try {
563                         o [0, 0] = new Super ();
564                         return 1;
565                 }
566                 catch (ArrayTypeMismatchException) {
567                 }
568
569                 return 0;
570         }
571
572         static int test_0_vector_array_cast () {
573                 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
574                 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
575
576                 if (arr1.GetType () != typeof (int[]))
577                         return 1;
578
579                 if (arr2.GetType () == typeof (int[]))
580                         return 2;
581
582                 int[] b;
583
584                 b = (int[])arr1;
585
586                 try {
587                         b = (int[])arr2;
588                         return 3;
589                 }
590                 catch (InvalidCastException) {
591                 }
592
593                 if (arr2 is int[])
594                         return 4;
595
596                 return 0;
597         }
598
599         static int test_0_enum_array_cast () {
600                 TypeCode[] tc = new TypeCode [0];
601                 object[] oa;
602                 ValueType[] vta;
603                 int[] inta;
604                 Array a = tc;
605                 bool ok;
606
607                 if (a is object[])
608                         return 1;
609                 if (a is ValueType[])
610                         return 2;
611                 if (a is Enum[])
612                         return 3;
613                 try {
614                         ok = false;
615                         oa = (object[])a;
616                 } catch {
617                         ok = true;
618                 }
619                 if (!ok)
620                         return 4;
621                 try {
622                         ok = false;
623                         vta = (ValueType[])a;
624                 } catch {
625                         ok = true;
626                 }
627                 if (!ok)
628                         return 5;
629                 try {
630                         ok = true;
631                         inta = (int[])a;
632                 } catch {
633                         ok = false;
634                 }
635                 if (!ok)
636                         return 6;
637                 return 0;
638         }
639
640         static int test_0_more_cast_corner_cases () {
641                 ValueType[] vta = new ValueType [0];
642                 Enum[] ea = new Enum [0];
643                 Array a = vta;
644                 object[] oa;
645                 bool ok;
646
647                 if (!(a is object[]))
648                         return 1;
649                 if (!(a is ValueType[]))
650                         return 2;
651                 if (a is Enum[])
652                         return 3;
653                 a = ea;
654                 if (!(a is object[]))
655                         return 4;
656                 if (!(a is ValueType[]))
657                         return 5;
658                 if (!(a is Enum[]))
659                         return 6;
660
661                 try {
662                         ok = true;
663                         oa = (object[])a;
664                 } catch {
665                         ok = false;
666                 }
667                 if (!ok)
668                         return 7;
669         
670                 try {
671                         ok = true;
672                         oa = (Enum[])a;
673                 } catch {
674                         ok = false;
675                 }
676                 if (!ok)
677                         return 8;
678         
679                 try {
680                         ok = true;
681                         oa = (ValueType[])a;
682                 } catch {
683                         ok = false;
684                 }
685                 if (!ok)
686                         return 9;
687
688                 a = vta;
689                 try {
690                         ok = true;
691                         oa = (object[])a;
692                 } catch {
693                         ok = false;
694                 }
695                 if (!ok)
696                         return 10;
697         
698                 try {
699                         ok = true;
700                         oa = (ValueType[])a;
701                 } catch {
702                         ok = false;
703                 }
704                 if (!ok)
705                         return 11;
706         
707                 try {
708                         ok = false;
709                         vta = (Enum[])a;
710                 } catch {
711                         ok = true;
712                 }
713                 if (!ok)
714                         return 12;
715                 return 0;
716         }
717
718         static int test_0_cast_iface_array () {
719                 object o = new ICloneable [0];
720                 object o2 = new Duper [0];
721                 object t;
722                 bool ok;
723
724                 if (!(o is object[]))
725                         return 1;
726                 if (!(o2 is ICloneable[]))
727                         return 2;
728
729                 try {
730                         ok = true;
731                         t = (object[])o;
732                 } catch {
733                         ok = false;
734                 }
735                 if (!ok)
736                         return 3;
737         
738                 try {
739                         ok = true;
740                         t = (ICloneable[])o2;
741                 } catch {
742                         ok = false;
743                 }
744                 if (!ok)
745                         return 4;
746
747                 try {
748                         ok = true;
749                         t = (ICloneable[])o;
750                 } catch {
751                         ok = false;
752                 }
753                 if (!ok)
754                         return 5;
755
756                 if (!(o is ICloneable[]))
757                         return 6;
758
759                 /* add tests for interfaces that 'inherit' interfaces */
760                 return 0;
761         }
762
763         private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
764
765         private static int AbsoluteDays (int year, int month, int day)
766         {
767                 int temp = 0, m = 1;
768                 int[] days = daysmonthleap;
769                 while (m < month)
770                         temp += days[m++];
771                 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
772         }
773
774         static int test_719162_complex_div () {
775                 int adays = AbsoluteDays (1970, 1, 1);
776                 return adays;
777         }
778
779         delegate int GetIntDel ();
780
781         static int return4 () {
782                 return 4;
783         }
784
785         int return5 () {
786                 return 5;
787         }
788
789         static int test_2_static_delegate () {
790                 GetIntDel del = new GetIntDel (return4);
791                 int v = del ();
792                 if (v != 4)
793                         return 0;
794                 return 2;
795         }
796
797         static int test_2_instance_delegate () {
798                 Tests t = new Tests ();
799                 GetIntDel del = new GetIntDel (t.return5);
800                 int v = del ();
801                 if (v != 5)
802                         return 0;
803                 return 2;
804         }
805
806         static int test_1_store_decimal () {
807                 decimal[,] a = {{1}};
808
809                 if (a[0,0] != 1m)
810                         return 0;
811                 return 1;
812         }
813
814         static int test_2_intptr_stobj () {
815                 System.IntPtr [] arr = { new System.IntPtr () };
816
817                 if (arr [0] != (System.IntPtr)0)
818                         return 1;
819                 return 2;
820         }
821
822         static int llmult (int a, int b, int c, int d) {
823                 return a + b + c + d;
824         }
825
826         /* 
827          * Test that evaluation of complex arguments does not overwrite the
828          * arguments already in outgoing registers.
829          */
830         static int test_155_regalloc () {
831                 int a = 10;
832                 int b = 10;
833
834                 int c = 0;
835                 int d = 0;
836                 int[] arr = new int [5];
837
838                 return llmult (arr [c + d], 150, 5, 0);
839         }
840
841         static bool large_struct_test (Large a, Large b, Large c, Large d)
842         {
843                 if (!a.check ()) return false;
844                 if (!b.check ()) return false;
845                 if (!c.check ()) return false;
846                 if (!d.check ()) return false;
847                 return true;
848         }
849
850         static int test_2_large_struct_pass ()
851         {
852                 Large a, b, c, d;
853                 a = new Large ();
854                 b = new Large ();
855                 c = new Large ();
856                 d = new Large ();
857                 a.populate ();
858                 b.populate ();
859                 c.populate ();
860                 d.populate ();
861                 if (large_struct_test (a, b, c, d))
862                         return 2;
863                 return 0;
864         }
865
866         public static unsafe int test_0_pin_string () {
867                 string x = "xxx";
868                 fixed (char *c = x) {
869                         if (*c != 'x')
870                                 return 1;
871                 }
872                 return 0;
873         }
874         
875         public static int my_flags;
876         public static int test_0_and_cmp_static ()
877         {
878                 
879                 /* various forms of test [mem], imm */
880                 
881                 my_flags = 0x01020304;
882                 
883                 if ((my_flags & 0x01020304) == 0)
884                         return 1;
885                 
886                 if ((my_flags & 0x00000304) == 0)
887                         return 2;
888                 
889                 if ((my_flags & 0x00000004) == 0)
890                         return 3;
891                 
892                 if ((my_flags & 0x00000300) == 0)
893                         return 4;
894                 
895                 if ((my_flags & 0x00020000) == 0)
896                         return 5;
897                 
898                 if ((my_flags & 0x01000000) == 0)
899                         return 6;
900                 
901                 return 0;
902         }
903         
904         static byte b;
905         public static int test_0_byte_compares ()
906         {
907                 b = 0xff;
908                 if (b == -1)
909                         return 1;
910                 b = 0;
911                 if (!(b < System.Byte.MaxValue))
912                         return 2;
913                 
914                 if (!(b <= System.Byte.MaxValue))
915                         return 3;
916                 
917                 return 0;
918         }
919
920         public static int test_71_long_shift_right () {
921                 ulong value = 38654838087;
922                 int x = 0;
923                 byte [] buffer = new byte [1];
924                 buffer [x] = ((byte)(value >> x));
925                 return buffer [x];
926         }
927         
928         static long x;
929         public static int test_0_addsub_mem ()
930         {
931                 x = 0;
932                 x += 5;
933                 
934                 if (x != 5)
935                         return 1;
936                 
937                 x -= 10;
938                 
939                 if (x != -5)
940                         return 2;
941                 
942                 return 0;
943         }
944         
945         static ulong y;
946         public static int test_0_sh32_mem ()
947         {
948                 y = 0x0102130405060708;
949                 y >>= 32;
950                 
951                 if (y != 0x01021304)
952                         return 1;
953                 
954                 y = 0x0102130405060708;
955                 y <<= 32;
956                 
957                 if (y != 0x0506070800000000)
958                         return 2;
959                 
960                 x = 0x0102130405060708;
961                 x <<= 32;
962                 
963                 if (x != 0x0506070800000000)
964                         return 2;
965                 
966                 return 0;
967         }
968
969
970         static uint dum_de_dum = 1;
971         static int test_0_long_arg_opt ()
972         {
973                 return Foo (0x1234567887654321, dum_de_dum);
974         }
975         
976         static int Foo (ulong x, ulong y)
977         {
978                 if (x != 0x1234567887654321)
979                         return 1;
980                 
981                 if (y != 1)
982                         return 2;
983                 
984                 return 0;
985         }
986         
987         static int test_0_long_ret_opt ()
988         {
989                 ulong x = X ();
990                 if (x != 0x1234567887654321)
991                         return 1;
992                 ulong y = Y ();
993                 if (y != 1)
994                         return 2;
995                 
996                 return 0;
997         }
998         
999         static ulong X ()
1000         {
1001                 return 0x1234567887654321;
1002         }
1003         
1004         static ulong Y ()
1005         {
1006                 return dum_de_dum;
1007         }
1008
1009         /* from bug# 71515 */
1010         static int counter = 0;
1011         static bool WriteStuff () {
1012                 counter = 10;
1013                 return true;
1014         }
1015         static int test_0_cond_branch_side_effects () {
1016                 counter = 5;
1017                 if (WriteStuff());
1018                 if (counter == 10)
1019                         return 0;
1020                 return 1;
1021         }
1022
1023         // bug #74992
1024         public static int arg_only_written (string file_name, int[]
1025 ncells ) {
1026                 if (file_name == null)
1027                         return 1;
1028
1029                 ncells = foo ();
1030                 bar (ncells [0]);
1031
1032                 return 0;
1033         }
1034
1035         public static int[] foo () {
1036                 return new int [3];
1037         }
1038
1039         public static void bar (int i) {
1040         }
1041         
1042
1043         public static int test_0_arg_only_written ()
1044         {
1045                 return arg_only_written ("md.in", null);
1046         }               
1047
1048         static long position = 0;
1049
1050         public static int test_4_static_inc_long () {
1051
1052                 int count = 4;
1053
1054                 position = 0;
1055
1056                 position += count;
1057
1058                 return (int)position;
1059         }
1060
1061         struct FooStruct {
1062
1063                 public FooStruct (long l) {
1064                 }
1065         }
1066
1067         static int test_0_calls_opcode_emulation () {
1068                 // Test that emulated opcodes do not clobber arguments already in
1069                 // out registers
1070                 checked {
1071                         long val = 10000;
1072                         new FooStruct (val * 10000);
1073                 }
1074                 return 0;
1075         }
1076
1077         static int test_0_intrins_string_length () {
1078                 string s = "ABC";
1079
1080                 return (s.Length == 3) ? 0 : 1;
1081         }
1082
1083         static int test_0_intrins_string_chars () {
1084                 string s = "ABC";
1085
1086                 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1087         }
1088
1089         static int test_0_intrins_object_gettype () {
1090                 object o = 1;
1091
1092                 return (o.GetType () == typeof (int)) ? 0 : 1;
1093         }
1094
1095         static int test_0_intrins_object_gethashcode () {
1096                 object o = new Object ();
1097
1098                 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1099         }
1100
1101         class FooClass {
1102         }
1103
1104         static int test_0_intrins_object_ctor () {
1105                 object o = new FooClass ();
1106
1107                 return (o != null) ? 0 : 1;
1108         }
1109
1110         static int test_0_intrins_array_rank () {
1111                 int[,] a = new int [10, 10];
1112
1113                 return (a.Rank == 2) ? 0 : 1;
1114         }
1115
1116         static int test_0_intrins_array_length () {
1117                 int[,] a = new int [10, 10];
1118                 Array a2 = a;
1119
1120                 return (a2.Length == 100) ? 0 : 1;
1121         }
1122
1123         static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1124                 int i = RuntimeHelpers.OffsetToStringData;
1125                 
1126                 return i - i;
1127         }
1128
1129         static int test_0_intrins_string_setchar () {
1130                 StringBuilder sb = new StringBuilder ("ABC");
1131
1132                 sb [1] = 'D';
1133
1134                 return sb.ToString () == "ADC" ? 0 : 1;
1135         }
1136
1137         public class Bar {
1138                 bool allowLocation = true;
1139         Foo f = new Foo ();     
1140         }
1141
1142         static int test_0_regress_78990_unaligned_structs () {
1143                 new Bar ();
1144
1145                 return 0;
1146         }
1147
1148         static unsafe int test_97_negative_index () {
1149                 char[] arr = new char[] {'a', 'b'};
1150                 fixed (char *p = arr) {
1151                         char *i = p + 2;
1152                         char a = i[-2];
1153                         return a;
1154                 }
1155         }
1156
1157         /* bug #82281 */
1158         static int test_0_unsigned_right_shift_imm0 () {
1159                 uint temp = 0;
1160                 byte[] data = new byte[256];
1161                 for (int i = 0; i < 1; i ++)
1162                         temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1163                 return 0;
1164         }
1165
1166         class Foo2 {
1167                 public virtual int foo () {
1168                         return 0;
1169                 }
1170         }
1171
1172         sealed class Bar2 : Foo2 {
1173                 public override int foo () {
1174                         return 0;
1175                 }
1176         }
1177
1178         static int test_0_abcrem_check_this_removal () {
1179                 Bar2 b = new Bar2 ();
1180
1181                 // The check_this generated here by the JIT should be removed
1182                 b.foo ();
1183
1184                 return 0;
1185         }
1186
1187         static int invoke_twice (Bar2 b) {
1188                 b.foo ();
1189                 // The check_this generated here by the JIT should be removed
1190                 b.foo ();
1191
1192                 return 0;
1193         }
1194
1195         static int test_0_abcrem_check_this_removal2 () {
1196                 Bar2 b = new Bar2 ();
1197
1198                 invoke_twice (b);
1199
1200                 return 0;
1201         }
1202
1203         /* #346563 */
1204         static int test_0_array_access_64_bit () {
1205                 int[] arr2 = new int [10];
1206                 for (int i = 0; i < 10; ++i)
1207                         arr2 [i] = i;
1208                 string s = "ABCDEFGH";
1209
1210                 byte[] arr = new byte [4];
1211                 arr [0] = 252;
1212                 arr [1] = 255;
1213                 arr [2] = 255;
1214                 arr [3] = 255;
1215
1216                 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1217                 int len2 = - (len + 2);
1218
1219                 // Test array and string access with a 32 bit value whose upper 32 bits are
1220                 // undefined
1221                 // len2 = 3
1222                 if (arr2 [len2] != 2)
1223                         return 1;
1224                 if (s [len2] != 'C')
1225                         return 2;
1226                 return 0;
1227         }
1228
1229         class R4Holder {
1230                 public static float pi = 3.14f;
1231
1232                 public float float_field;
1233         }
1234
1235         static int test_0_ldsfld_soft_float () {
1236                 if (R4Holder.pi == 3.14f)
1237                         return 0;
1238                 else
1239                         return 1;
1240         }
1241
1242         static int test_0_ldfld_stfld_soft_float () {
1243                 R4Holder h = new R4Holder ();
1244                 h.float_field = 3.14f;
1245
1246                 if (h.float_field == 3.14f)
1247                         return 0;
1248                 else
1249                         return 1;
1250         }
1251
1252         class R4HolderRemote : MarshalByRefObject {
1253                 public static float pi = 3.14f;
1254
1255                 public float float_field;
1256         }
1257
1258         static int test_0_ldfld_stfld_soft_float_remote () {
1259                 R4HolderRemote h = new R4HolderRemote ();
1260                 h.float_field = 3.14f;
1261
1262                 if (h.float_field == 3.14f)
1263                         return 0;
1264                 else
1265                         return 1;
1266         }
1267
1268         static int test_0_locals_soft_float () {
1269                 float f = 0.0f;
1270                 
1271                 f = 3.14f;
1272
1273                 if (f == 3.14f)
1274                         return 0;
1275                 else
1276                         return 1;
1277         }
1278 }
1279