2005-05-22 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / objects.cs
index f57a50c4e76c8e9c270aaac2617152a8a606e4a6..8df96ca0552d4b0b0865bd694cdc5953d7a06775 100644 (file)
@@ -1,5 +1,6 @@
 using System;
 using System.Reflection;
+using System.Runtime.InteropServices;
 
 /*
  * Regression tests for the mono JIT.
@@ -30,6 +31,41 @@ struct Simple {
        public long d;
 }
 
+struct Small {
+       public byte b1;
+       public byte b2;
+}
+
+struct Large {
+       int one;
+       int two;
+       long three;
+       long four;
+       int five;
+       long six;
+       int seven;
+       long eight;
+       long nine;
+       long ten;
+
+       public void populate ()
+       {
+               one = 1; two = 2;
+               three = 3; four = 4;
+               five = 5; six = 6;
+               seven = 7; eight = 8;
+               nine = 9; ten = 10;
+       }
+       public bool check ()
+       {
+               return one == 1  && two == 2  &&
+                       three == 3  && four == 4  &&
+                       five == 5  && six == 6  &&
+                       seven == 7  && eight == 8  &&
+                       nine == 9  && ten == 10;
+       }
+}
+
 class Sample {
        public int a;
        public Sample (int v) {
@@ -37,6 +73,16 @@ class Sample {
        }
 }
 
+[StructLayout ( LayoutKind.Explicit )]
+struct StructWithBigOffsets {
+               [ FieldOffset(10000) ] public byte b;
+               [ FieldOffset(11000) ] public short s;
+               [ FieldOffset(12000) ] public uint i;
+               [ FieldOffset(13000) ] public long l;
+               [ FieldOffset(14000) ] public float f;
+               [ FieldOffset(15000) ] public double d;
+}
+
 enum SampleEnum {
        A,
        B,
@@ -146,33 +192,143 @@ class Tests {
 
        static int receive_simple (int a, Simple v, int b) {
                if (v.a != 1)
-                       return 0;
+                       return 1;
                if (v.b != 2)
-                       return 0;
+                       return 2;
                if (v.c != 3)
-                       return 0;
+                       return 3;
                if (v.d != 4)
-                       return 0;
+                       return 4;
                if (a != 7)
-                       return 0;
+                       return 5;
                if (b != 9)
-                       return 0;
-               return 1;
+                       return 6;
+               return 0;
        }
        
        static int test_5_pass_struct () {
                Simple v = get_simple (1);
-               if (receive_simple (7, v, 9) != 1)
+               if (receive_simple (7, v, 9) != 0)
                        return 0;
-               if (receive_simple (7, get_simple (1), 9) != 1)
+               if (receive_simple (7, get_simple (1), 9) != 0)
                        return 1;
                return 5;
        }
 
+       // Test alignment of small structs
+
+       static Small get_small (byte v) {
+               Small r = new Small ();
+       
+               r.b1 = v;
+               r.b2 = (byte)(v + 1);
+
+               return r;
+       }
+
+       static Small return_small (Small s) {
+               return s;
+       }
+
+       static int receive_small (int a, Small v, int b) {
+               if (v.b1 != 1)
+                       return 1;
+               if (v.b2 != 2)
+                       return 2;
+               return 0;
+       }
+
+       static int test_5_pass_small_struct () {
+               Small v = get_small (1);
+               if (receive_small (7, v, 9) != 0)
+                       return 0;
+               if (receive_small (7, get_small (1), 9) != 0)
+                       return 1;
+               v = return_small (v);
+               if (v.b1 != 1)
+                       return 2;
+               if (v.b2 != 2)
+                       return 3;
+               return 5;
+       }
+
+       struct AStruct {
+               public int i;
+
+               public AStruct (int i) {
+                       this.i = i;
+               }
+
+               public override int GetHashCode () {
+                       return i;
+               }
+       }
+
+       // Test that vtypes are unboxed during a virtual call
+       static int test_44_unbox_trampoline () {
+               AStruct s = new AStruct (44);
+               object o = s;
+               return o.GetHashCode ();
+       }
+
+       static int test_0_unbox_trampoline2 () {
+               int i = 12;
+               object o = i;
+                       
+               if (i.ToString () != "12")
+                       return 1;
+               if (((Int32)o).ToString () != "12")
+                       return 2;
+               if (o.ToString () != "12")
+                       return 3;
+               return 0;
+       }
+
+       // Test fields with big offsets
+       static int test_0_fields_with_big_offsets () {
+               StructWithBigOffsets s = new StructWithBigOffsets ();
+               StructWithBigOffsets s2 = new StructWithBigOffsets ();
+
+               s.b = 0xde;
+               s.s = 0x12de;
+               s.i = 0xdeadbeef;
+               s.l = 0xcafebabe;
+               s.f = 3.14F;
+               s.d = 3.14;
+
+               s2.b = s.b;
+               s2.s = s.s;
+               s2.i = s.i;
+               s2.l = s.l;
+               s2.f = s.f;
+               s2.d = s.d;
+
+               if (s2.b != 0xde)
+                       return 1;
+               if (s2.s != 0x12de)
+                       return 2;
+               if (s2.i != 0xdeadbeef)
+                       return 3;
+               if (s2.l != 0xcafebabe)
+                       return 4;
+               if (s2.f != 3.14F)
+                       return 5;
+               if (s2.d != 3.14)
+                       return 6;
+
+               return 0;
+       }
+
        class TestRegA {
 
                long buf_start;
                int buf_length, buf_offset;
+
+               public TestRegA () {
+                       buf_start = 0;
+                       buf_length = 0;
+                       buf_offset = 0;
+               }
        
                public long Seek (long position) {
                        long pos = position;
@@ -199,6 +355,14 @@ class Tests {
        }
        class Duper: Super {
        }
+
+       static int test_0_null_cast () {
+               object o = null;
+
+               Super s = (Super)o;
+
+               return 0;
+       }
        
        static int test_0_super_cast () {
                Duper d = new Duper ();
@@ -244,6 +408,47 @@ class Tests {
                return 0;
        }
 
+       static int test_0_multi_array_cast () {
+               Duper[,] d = new Duper [1, 1];
+               object[,] o = d;
+
+               try {
+                       o [0, 0] = new Super ();
+                       return 1;
+               }
+               catch (ArrayTypeMismatchException) {
+               }
+
+               return 0;
+       }
+
+       static int test_0_vector_array_cast () {
+               Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
+               Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
+
+               if (arr1.GetType () != typeof (int[]))
+                       return 1;
+
+               if (arr2.GetType () == typeof (int[]))
+                       return 2;
+
+               int[] b;
+
+               b = (int[])arr1;
+
+               try {
+                       b = (int[])arr2;
+                       return 3;
+               }
+               catch (InvalidCastException) {
+               }
+
+               if (arr2 is int[])
+                       return 4;
+
+               return 0;
+       }
+
        static int test_0_enum_array_cast () {
                TypeCode[] tc = new TypeCode [0];
                object[] oa;
@@ -424,6 +629,33 @@ class Tests {
                return adays;
        }
 
+       delegate int GetIntDel ();
+
+       static int return4 () {
+               return 4;
+       }
+
+       int return5 () {
+               return 5;
+       }
+
+       static int test_2_static_delegate () {
+               GetIntDel del = new GetIntDel (return4);
+               int v = del ();
+               if (v != 4)
+                       return 0;
+               return 2;
+       }
+
+       static int test_2_instance_delegate () {
+               Tests t = new Tests ();
+               GetIntDel del = new GetIntDel (t.return5);
+               int v = del ();
+               if (v != 5)
+                       return 0;
+               return 2;
+       }
+
        static int test_1_store_decimal () {
                decimal[,] a = {{1}};
 
@@ -439,5 +671,58 @@ class Tests {
                        return 1;
                return 2;
        }
+
+       static int llmult (int a, int b, int c, int d) {
+               return a + b + c + d;
+       }
+
+       /* 
+        * Test that evaluation of complex arguments does not overwrite the
+        * arguments already in outgoing registers.
+        */
+       static int test_155_regalloc () {
+               int a = 10;
+               int b = 10;
+
+               int c = 0;
+               int d = 0;
+               int[] arr = new int [5];
+
+               return llmult (arr [c + d], 150, 5, 0);
+       }
+
+       static bool large_struct_test (Large a, Large b, Large c, Large d)
+       {
+               if (!a.check ()) return false;
+               if (!b.check ()) return false;
+               if (!c.check ()) return false;
+               if (!d.check ()) return false;
+               return true;
+       }
+
+       static int test_2_large_struct_pass ()
+       {
+               Large a, b, c, d;
+               a = new Large ();
+               b = new Large ();
+               c = new Large ();
+               d = new Large ();
+               a.populate ();
+               b.populate ();
+               c.populate ();
+               d.populate ();
+               if (large_struct_test (a, b, c, d))
+                       return 2;
+               return 0;
+       }
+
+       public static unsafe int test_0_pin_string () {
+               string x = "xxx";
+               fixed (char *c = x) {
+                       if (*c != 'x')
+                               return 1;
+               }
+               return 0;
+       }
 }