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