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