2008-12-05 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / tests / libtest.c
index b807504969d51c195d2fd23d447bf74680318798..3fd22de9edde942c1d9d51839c98f71c2d12295d 100644 (file)
@@ -109,6 +109,11 @@ mono_return_int (int a) {
        return a;
 }
 
+LIBTEST_API float STDCALL  
+mono_test_marshal_pass_return_float (float f) {
+       return f + 1.0;
+}
+
 struct ss
 {
        int i;
@@ -675,6 +680,19 @@ mono_test_marshal_delegate (SimpleDelegate delegate)
        return delegate (2);
 }
 
+static int STDCALL inc_cb (int i)
+{
+       return i + 1;
+}
+
+LIBTEST_API int STDCALL 
+mono_test_marshal_out_delegate (SimpleDelegate *delegate)
+{
+       *delegate = inc_cb;
+
+       return 0;
+}
+
 LIBTEST_API SimpleDelegate STDCALL 
 mono_test_marshal_return_delegate (SimpleDelegate delegate)
 {
@@ -1512,6 +1530,17 @@ mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
        return s;
 }
 
+LIBTEST_API amd64_struct1 STDCALL 
+mono_test_marshal_amd64_pass_return_struct1_many_args (amd64_struct1 s, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
+{
+       s.i ++;
+       s.j ++;
+       s.k ++;
+       s.l += 1 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
+
+       return s;
+}
+
 typedef struct amd64_struct2 {
        int i;
        int j;
@@ -1559,10 +1588,10 @@ typedef struct test_struct5 {
 } test_struct5;
 
 LIBTEST_API test_struct5 STDCALL 
-mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, double d3, double d4)
+mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, int i, double d3, double d4)
 {
-       s.d1 += d1 + d2;
-       s.d2 += d3 + d4;
+       s.d1 += d1 + d2 + i;
+       s.d2 += d3 + d4 + i;
 
        return s;
 }
@@ -1572,9 +1601,9 @@ typedef struct test_struct6 {
 } test_struct6;
 
 LIBTEST_API test_struct6 STDCALL 
-mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, double d3, double d4)
+mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, int i, double d3, double d4)
 {
-       s.d1 += d1 + d2;
+       s.d1 += d1 + d2 + i;
        s.d2 += d3 + d4;
 
        return s;
@@ -2022,7 +2051,7 @@ mono_test_marshal_cdecl_delegate (CdeclDelegate del)
        return 0;
 }
 
-typedef char** (*ReturnStringArrayDelegate) (int i);
+typedef char** (STDCALL *ReturnStringArrayDelegate) (int i);
 
 LIBTEST_API int STDCALL 
 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
@@ -2043,6 +2072,28 @@ mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
        return res;
 }
 
+typedef int (STDCALL *ByrefStringDelegate) (char **s);
+
+LIBTEST_API int STDCALL 
+mono_test_marshal_byref_string_delegate (ByrefStringDelegate d)
+{
+       char *s = (char*)"ABC";
+       int res;
+
+       res = d (&s);
+       if (res != 0)
+               return res;
+
+       if (!strcmp (s, "DEF"))
+               res = 0;
+       else
+               res = 2;
+
+       marshal_free (s);
+
+       return res;
+}
+
 LIBTEST_API int STDCALL 
 add_delegate (int i, int j)
 {
@@ -2071,6 +2122,8 @@ mono_xr_as_handle (int code)
 {
        HandleRef ref;
 
+       memset (&ref, 0, sizeof (ref));
+
        return ref;
 }
  
@@ -2925,7 +2978,7 @@ mono_test_marshal_ccw_itest (MonoComObject *pUnk)
  * mono_method_get_unmanaged_thunk tests
  */
 
-#if defined(__GNUC__) && defined(__i386__) && (defined(__linux__) || defined (__APPLE__))
+#if defined(__GNUC__) && ((defined(__i386__) && (defined(__linux__) || defined (__APPLE__))) || (defined(__ppc__) && defined(__APPLE__)))
 #define ALIGN(size) __attribute__ ((aligned(size)))
 #else
 #define ALIGN(size)
@@ -2940,7 +2993,7 @@ typedef struct _TestStruct {
 
 /* Searches for mono symbols in all loaded modules */
 static gpointer
-lookup_mono_symbol (char *symbol_name)
+lookup_mono_symbol (const char *symbol_name)
 {
        gpointer symbol;
        if (g_module_symbol (g_module_open (NULL, G_MODULE_BIND_LAZY), symbol_name, &symbol))
@@ -2962,7 +3015,7 @@ test_method_thunk (int test_id, gpointer test_method_handle, gpointer create_obj
        gpointer (*mono_method_get_unmanaged_thunk)(gpointer)
                = lookup_mono_symbol ("mono_method_get_unmanaged_thunk");
 
-       gpointer (*mono_string_new_wrapper)(char *)
+       gpointer (*mono_string_new_wrapper)(const char *)
                = lookup_mono_symbol ("mono_string_new_wrapper");
 
        char* (*mono_string_to_utf8)(gpointer)
@@ -3307,3 +3360,382 @@ test_method_thunk (int test_id, gpointer test_method_handle, gpointer create_obj
 
        return 0;
 }
+
+typedef struct 
+{
+       char a;
+} winx64_struct1;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_struct1_in (winx64_struct1 var)
+{
+       if (var.a != 123)
+               return 1;
+       return 0;
+}
+
+typedef struct
+{
+       char a;
+       char b;
+} winx64_struct2;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_struct2_in (winx64_struct2 var)
+{
+       if (var.a != 4)
+               return 1;
+       if (var.b != 5)
+               return 2;
+       return 0;
+}
+
+
+typedef struct
+{
+       char a;
+       char b;
+       short c;
+} winx64_struct3;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_struct3_in (winx64_struct3 var)
+{
+       if (var.a != 4)
+               return 1;
+       if (var.b != 5)
+               return 2;
+       if (var.c != 0x1234)
+               return 3;
+       return 0;
+}
+
+typedef struct
+{
+       char a;
+       char b;
+       short c;
+       unsigned int d;
+} winx64_struct4;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_struct4_in (winx64_struct4 var)
+{
+       if (var.a != 4)
+               return 1;
+       if (var.b != 5)
+               return 2;
+       if (var.c != 0x1234)
+               return 3;
+       if (var.d != 0x87654321)
+               return 4;
+       return 0;
+}
+
+typedef struct
+{
+       char a;
+       char b;
+       char c;
+} winx64_struct5;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_struct5_in (winx64_struct5 var)
+{
+       if (var.a != 4)
+               return 1;
+       if (var.b != 5)
+               return 2;
+       if (var.c != 6)
+               return 3;
+       return 0;
+}
+
+typedef struct
+{
+       winx64_struct1 a;
+       short b;
+       char c;
+} winx64_struct6;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_struct6_in (winx64_struct6 var)
+{
+       if (var.a.a != 4)
+               return 1;
+       if (var.b != 5)
+               return 2;
+       if (var.c != 6)
+               return 3;
+       return 0;
+}
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_structs_in1 (winx64_struct1 var1,
+                        winx64_struct2 var2,
+                        winx64_struct3 var3,
+                        winx64_struct4 var4)
+{
+       if (var1.a != 123)
+               return 1;
+       
+       if (var2.a != 4)
+               return 2;
+       if (var2.b != 5)
+               return 3;
+       
+       if (var3.a != 4)
+               return 4;
+       if (var3.b != 5)
+               return 2;
+       if (var3.c != 0x1234)
+               return 5;
+       
+       if (var4.a != 4)
+               return 6;
+       if (var4.b != 5)
+               return 7;
+       if (var4.c != 0x1234)
+               return 8;
+       if (var4.d != 0x87654321)
+               return 9;
+       return 0;
+}
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_structs_in2 (winx64_struct1 var1,
+                        winx64_struct1 var2,
+                        winx64_struct1 var3,
+                        winx64_struct1 var4,
+                        winx64_struct1 var5)
+{
+       if (var1.a != 1)
+               return 1;
+       if (var2.a != 2)
+               return 2;
+       if (var3.a != 3)
+               return 3;
+       if (var4.a != 4)
+               return 4;
+       if (var5.a != 5)
+               return 5;
+       
+       return 0;
+}
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_structs_in3 (winx64_struct1 var1,
+                        winx64_struct5 var2,
+                        winx64_struct1 var3,
+                        winx64_struct5 var4,
+                        winx64_struct1 var5,
+                        winx64_struct5 var6)
+{
+       if (var1.a != 1)
+               return 1;
+       
+       if (var2.a != 2)
+               return 2;
+       if (var2.b != 3)
+               return 2;
+       if (var2.c != 4)
+               return 4;
+       
+       if (var3.a != 5)
+               return 5;
+       
+       if (var4.a != 6)
+               return 6;
+       if (var4.b != 7)
+               return 7;
+       if (var4.c != 8)
+               return 8;
+       
+       if (var5.a != 9)
+               return 9;
+
+       if (var6.a != 10)
+               return 10;
+       if (var6.b != 11)
+               return 11;
+       if (var6.c != 12)
+               return 12;
+       
+       return 0;
+}
+
+LIBTEST_API winx64_struct1 STDCALL  
+mono_test_Winx64_struct1_ret (void)
+{
+       winx64_struct1 ret;
+       ret.a = 123;
+       return ret;
+}
+
+LIBTEST_API winx64_struct2 STDCALL  
+mono_test_Winx64_struct2_ret (void)
+{
+       winx64_struct2 ret;
+       ret.a = 4;
+       ret.b = 5;
+       return ret;
+}
+
+LIBTEST_API winx64_struct3 STDCALL  
+mono_test_Winx64_struct3_ret (void)
+{
+       winx64_struct3 ret;
+       ret.a = 4;
+       ret.b = 5;
+       ret.c = 0x1234;
+       return ret;
+}
+
+LIBTEST_API winx64_struct4 STDCALL  
+mono_test_Winx64_struct4_ret (void)
+{
+       winx64_struct4 ret;
+       ret.a = 4;
+       ret.b = 5;
+       ret.c = 0x1234;
+       ret.d = 0x87654321;
+       return ret;
+}
+
+LIBTEST_API winx64_struct5 STDCALL  
+mono_test_Winx64_struct5_ret (void)
+{
+       winx64_struct5 ret;
+       ret.a = 4;
+       ret.b = 5;
+       ret.c = 6;
+       return ret;
+}
+
+LIBTEST_API winx64_struct1 STDCALL  
+mono_test_Winx64_struct1_ret_5_args (char a, char b, char c, char d, char e)
+{
+       winx64_struct1 ret;
+       ret.a = a + b + c + d + e;
+       return ret;
+}
+
+LIBTEST_API winx64_struct5 STDCALL
+mono_test_Winx64_struct5_ret6_args (char a, char b, char c, char d, char e)
+{
+       winx64_struct5 ret;
+       ret.a = a + b;
+       ret.b = c + d;
+       ret.c = e;
+       return ret;
+}
+
+typedef struct
+{
+       float a;
+       float b;
+} winx64_floatStruct;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_floatStruct (winx64_floatStruct a)
+{
+       if (a.a > 5.6 || a.a < 5.4)
+               return 1;
+
+       if (a.b > 9.6 || a.b < 9.4)
+               return 2;
+       
+       return 0;
+}
+
+typedef struct
+{
+       double a;
+} winx64_doubleStruct;
+
+LIBTEST_API int STDCALL  
+mono_test_Winx64_doubleStruct (winx64_doubleStruct a)
+{
+       if (a.a > 5.6 || a.a < 5.4)
+               return 1;
+       
+       return 0;
+}
+
+typedef int (STDCALL *managed_struct1_delegate) (winx64_struct1 a);
+
+LIBTEST_API int STDCALL 
+mono_test_managed_Winx64_struct1_in(managed_struct1_delegate func)
+{
+       winx64_struct1 val;
+       val.a = 5;
+       return func (val);
+}
+
+typedef int (STDCALL *managed_struct5_delegate) (winx64_struct5 a);
+
+LIBTEST_API int STDCALL 
+mono_test_managed_Winx64_struct5_in(managed_struct5_delegate func)
+{
+       winx64_struct5 val;
+       val.a = 5;
+       val.b = 0x10;
+       val.c = 0x99;
+       return func (val);
+}
+
+typedef int (STDCALL *managed_struct1_struct5_delegate) (winx64_struct1 a, winx64_struct5 b,
+                                                        winx64_struct1 c, winx64_struct5 d,
+                                                        winx64_struct1 e, winx64_struct5 f);
+
+LIBTEST_API int STDCALL 
+mono_test_managed_Winx64_struct1_struct5_in(managed_struct1_struct5_delegate func)
+{
+       winx64_struct1 a, c, e;
+       winx64_struct5 b, d, f;
+       a.a = 1;
+       b.a = 2; b.b = 3; b.c = 4;
+       c.a = 5;
+       d.a = 6; d.b = 7; d.c = 8;
+       e.a = 9;
+       f.a = 10; f.b = 11; f.c = 12;
+
+       return func (a, b, c, d, e, f);
+}
+
+typedef winx64_struct1 (STDCALL *managed_struct1_ret_delegate) (void);
+
+LIBTEST_API int STDCALL 
+mono_test_Winx64_struct1_ret_managed (managed_struct1_ret_delegate func)
+{
+       winx64_struct1 ret;
+
+       ret = func ();
+
+       if (ret.a != 0x45)
+               return 1;
+       
+       return 0;
+}
+
+typedef winx64_struct5 (STDCALL *managed_struct5_ret_delegate) (void);
+
+LIBTEST_API int STDCALL 
+mono_test_Winx64_struct5_ret_managed (managed_struct5_ret_delegate func)
+{
+       winx64_struct5 ret;
+
+       ret = func ();
+
+       if (ret.a != 0x12)
+               return 1;
+       if (ret.b != 0x34)
+               return 2;
+       if (ret.c != 0x56)
+               return 3;
+       
+       return 0;
+}
+
+
+