2007-02-08 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mono / tests / libtest.c
index 36cb52fafe9403ed2f8aa459a2082d66d4b5d934..225f2c2c5b62052d46dd59d042cc7d0fa019b12f 100644 (file)
@@ -985,6 +985,35 @@ mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
        return (ret != 0);
 }
 
+typedef struct {
+       guint16 a[4];
+       int  flag;
+} ByValStrStruct_Unicode;
+
+STDCALL int
+mono_test_byvalstr_check_unicode (ByValStrStruct_Unicode *ref, int test)
+{
+       if (ref->flag != 0x1234abcd){
+               printf ("overwritten data");
+               return 1;
+       }
+           
+       if (test == 1 || test == 3){
+               if (ref->a [0] != '1' ||
+                   ref->a [1] != '2'   ||
+                   ref->a [2] != '3')
+                       return 1;
+               return 0;
+       }
+       if (test == 2){
+               if (ref->a [0] != '1' ||
+                   ref->a [1] != '2')
+                       return 1;
+               return 0;
+       }
+       return 10;
+}
+
 STDCALL int
 NameManglingAnsi (char *data)
 {
@@ -1888,6 +1917,84 @@ mono_test_marshal_return_fnptr (void)
        return &add_delegate;
 }
 
+int mono_xr (int code)
+{
+       printf ("codigo %x\n", code);
+       return code + 1234;
+}
+
+typedef struct {
+       int   a;
+       void *handle1;
+       void *handle2;
+       int   b;
+} HandleStructs;
+
+int
+mono_safe_handle_struct_ref (HandleStructs *x)
+{
+       printf ("Dingus Ref! \n");
+       printf ("Values: %d %d %d %d\n", x->a, x->b, x->handle1, x->handle2);
+       if (x->a != 1234)
+               return 1;
+       if (x->b != 8743)
+               return 2;
+
+       if (x->handle1 != (void*) 0x7080feed)
+               return 3;
+
+       if (x->handle2 != (void*) 0x1234abcd)
+               return 4;
+
+       return 0xf00d;
+}
+
+int
+mono_safe_handle_struct (HandleStructs x)
+{
+       printf ("Dingus Standard! \n");
+       printf ("Values: %d %d %d %d\n", x.a, x.b, x.handle1, x.handle2);
+       if (x.a != 1234)
+               return 1;
+       if (x.b != 8743)
+               return 2;
+
+       if (x.handle1 != (void*) 0x7080feed)
+               return 3;
+
+       if (x.handle2 != (void*) 0x1234abcd)
+               return 4;
+       
+       return 0xf00f;
+}
+
+typedef struct {
+       void *a;
+} TrivialHandle;
+
+int
+mono_safe_handle_struct_simple (TrivialHandle x)
+{
+       printf ("The value is %d\n", x.a);
+       return ((int)x.a) * 2;
+}
+
+int
+mono_safe_handle_return ()
+{
+       return 0x1000f00d;
+}
+
+void
+mono_safe_handle_ref (void **handle)
+{
+       if (*handle != 0){
+               *handle = (void *) 0xbad;
+               return;
+       }
+
+       *handle = (void *) 0x800d;
+}
 /*
  * COM INTEROP TESTS
  */
@@ -2012,6 +2119,22 @@ mono_test_marshal_variant_in_bstr(VARIANT variant)
        return 1;
 }
 
+STDCALL int
+mono_test_marshal_variant_in_bool_true (VARIANT variant)
+{
+       if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_TRUE)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_in_bool_false (VARIANT variant)
+{
+       if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_FALSE)
+               return 0;
+       return 1;
+}
+
 STDCALL int
 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
 {
@@ -2111,23 +2234,307 @@ mono_test_marshal_variant_out_bstr(VARIANT* variant)
        return 0;
 }
 
-#ifdef _MSC_VER
-#define COM_STDCALL __stdcall
-#else
-#define COM_STDCALL __attribute__((stdcall))
-#endif
+STDCALL int
+mono_test_marshal_variant_out_bool_true (VARIANT* variant)
+{
+       variant->vt = VT_BOOL;
+       variant->boolVal = VARIANT_TRUE;
+
+       return 0;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_bool_false (VARIANT* variant)
+{
+       variant->vt = VT_BOOL;
+       variant->boolVal = VARIANT_FALSE;
+
+       return 0;
+}
+
+typedef int (STDCALL *VarFunc) (int vt, VARIANT variant);
+typedef int (STDCALL *VarRefFunc) (int vt, VARIANT* variant);
+
+STDCALL int
+mono_test_marshal_variant_in_sbyte_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_I1;
+       vt.cVal = -100;
+       return func (VT_I1, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_byte_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_UI1;
+       vt.bVal = 100;
+       return func (VT_UI1, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_short_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_I2;
+       vt.iVal = -100;
+       return func (VT_I2, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_ushort_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_UI2;
+       vt.uiVal = 100;
+       return func (VT_UI2, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_int_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_I4;
+       vt.lVal = -100;
+       return func (VT_I4, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_uint_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_UI4;
+       vt.ulVal = 100;
+       return func (VT_UI4, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_long_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_I8;
+       vt.llVal = -100;
+       return func (VT_I8, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_ulong_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_UI8;
+       vt.ullVal = 100;
+       return func (VT_UI8, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_float_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_R4;
+       vt.fltVal = 3.14;
+       return func (VT_R4, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_double_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_R8;
+       vt.dblVal = 3.14;
+       return func (VT_R8, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_bstr_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_BSTR;
+       vt.bstrVal = SysAllocString(L"PI");
+       return func (VT_BSTR, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_bool_true_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_BOOL;
+       vt.boolVal = VARIANT_TRUE;
+       return func (VT_BOOL, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_in_bool_false_unmanaged(VarFunc func)
+{
+       VARIANT vt;
+       vt.vt = VT_BOOL;
+       vt.boolVal = VARIANT_FALSE;
+       return func (VT_BOOL, vt);
+}
+
+STDCALL int
+mono_test_marshal_variant_out_sbyte_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_I1, &vt);
+       if (vt.vt == VT_I1 && vt.cVal == -100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_byte_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_UI1, &vt);
+       if (vt.vt == VT_UI1 && vt.bVal == 100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_short_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_I2, &vt);
+       if (vt.vt == VT_I2 && vt.iVal == -100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_ushort_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_UI2, &vt);
+       if (vt.vt == VT_UI2 && vt.uiVal == 100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_int_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_I4, &vt);
+       if (vt.vt == VT_I4 && vt.lVal == -100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_uint_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_UI4, &vt);
+       if (vt.vt == VT_UI4 && vt.ulVal == 100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_long_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_I8, &vt);
+       if (vt.vt == VT_I8 && vt.llVal == -100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_ulong_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_UI8, &vt);
+       if (vt.vt == VT_UI8 && vt.ullVal == 100)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_float_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_R4, &vt);
+       if (vt.vt == VT_R4 && fabs (vt.fltVal - 3.14f) < 1e-10)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_double_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_R8, &vt);
+       if (vt.vt == VT_R8 && fabs (vt.dblVal - 3.14) < 1e-10)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_bstr_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_BSTR, &vt);
+       if (vt.vt == VT_BSTR && !wcscmp(vt.bstrVal, L"PI"))
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_bool_true_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_BOOL, &vt);
+       if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
+               return 0;
+       return 1;
+}
+
+STDCALL int
+mono_test_marshal_variant_out_bool_false_unmanaged(VarRefFunc func)
+{
+       VARIANT vt;
+       VariantInit (&vt);
+       func (VT_BOOL, &vt);
+       if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
+               return 0;
+       return 1;
+}
 
 typedef struct MonoComObject MonoComObject;
 
 typedef struct
 {
-       int (COM_STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
-       int (COM_STDCALL *AddRef)(MonoComObject* pUnk);
-       int (COM_STDCALL *Release)(MonoComObject* pUnk);
-       int (COM_STDCALL *Add)(MonoComObject* pUnk, int a, int b, int* c);
-       int (COM_STDCALL *Subtract)(MonoComObject* pUnk, int a, int b, int* c);
-       int (COM_STDCALL *Same)(MonoComObject* pUnk, MonoComObject* *pOut);
-       int (COM_STDCALL *Different)(MonoComObject* pUnk, MonoComObject* *pOut);
+       int (STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
+       int (STDCALL *AddRef)(MonoComObject* pUnk);
+       int (STDCALL *Release)(MonoComObject* pUnk);
+       int (STDCALL *get_ITest)(MonoComObject* pUnk, MonoComObject* *ppUnk);
+       int (STDCALL *SByteIn)(MonoComObject* pUnk, char a);
+       int (STDCALL *ByteIn)(MonoComObject* pUnk, unsigned char a);
+       int (STDCALL *ShortIn)(MonoComObject* pUnk, short a);
+       int (STDCALL *UShortIn)(MonoComObject* pUnk, unsigned short a);
+       int (STDCALL *IntIn)(MonoComObject* pUnk, int a);
+       int (STDCALL *UIntIn)(MonoComObject* pUnk, unsigned int a);
+       int (STDCALL *LongIn)(MonoComObject* pUnk, LONGLONG a);
+       int (STDCALL *ULongIn)(MonoComObject* pUnk, ULONGLONG a);
+       int (STDCALL *FloatIn)(MonoComObject* pUnk, float a);
+       int (STDCALL *DoubleIn)(MonoComObject* pUnk, double a);
+       int (STDCALL *ITestIn)(MonoComObject* pUnk, MonoComObject* pUnk2);
+       int (STDCALL *ITestOut)(MonoComObject* pUnk, MonoComObject* *ppUnk);
 } MonoIUnknown;
 
 struct MonoComObject
@@ -2136,18 +2543,18 @@ struct MonoComObject
        int m_ref;
 };
 
-DEFINE_GUID(IID_IMath, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
+DEFINE_GUID(IID_ITest, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
 DEFINE_GUID(IID_IMonoUnknown, 0, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
 DEFINE_GUID(IID_IMonoDispatch, 0x00020400, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
 
-int COM_STDCALL MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
+int STDCALL MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
 {
        *ppv = NULL;
        if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
                *ppv = pUnk;
                return S_OK;
        }
-       else if (!memcmp(riid, &IID_IMath, sizeof(GUID))) {
+       else if (!memcmp(riid, &IID_ITest, sizeof(GUID))) {
                *ppv = pUnk;
                return S_OK;
        }
@@ -2158,43 +2565,79 @@ int COM_STDCALL MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer*
        return E_NOINTERFACE;
 }
 
-int COM_STDCALL MonoAddRef(MonoComObject* pUnk)
+int STDCALL MonoAddRef(MonoComObject* pUnk)
 {
        return ++(pUnk->m_ref);
 }
 
-int COM_STDCALL MonoRelease(MonoComObject* pUnk)
+int STDCALL MonoRelease(MonoComObject* pUnk)
 {
        return --(pUnk->m_ref);
 }
 
-int COM_STDCALL Add(MonoComObject* pUnk, int a, int b, int* c)
+int STDCALL SByteIn(MonoComObject* pUnk, char a)
 {
-       *c = a + b;
-       return 0;
+       return S_OK;
 }
 
-int COM_STDCALL Subtract(MonoComObject* pUnk, int a, int b, int* c)
+int STDCALL ByteIn(MonoComObject* pUnk, unsigned char a)
 {
-       *c = a - b;
-       return 0;
+       return S_OK;
+}
+
+int STDCALL ShortIn(MonoComObject* pUnk, short a)
+{
+       return S_OK;
 }
 
-static void create_com_object (MonoComObject** pOut);
-static MonoComObject* same_com_object = NULL;
+int STDCALL UShortIn(MonoComObject* pUnk, unsigned short a)
+{
+       return S_OK;
+}
 
-int COM_STDCALL Same(MonoComObject* pUnk, MonoComObject** pOut)
+int STDCALL IntIn(MonoComObject* pUnk, int a)
 {
-       if (!same_com_object)
-               create_com_object (&same_com_object);
-       *pOut = same_com_object;
-       return 0;
+       return S_OK;
 }
 
-int COM_STDCALL Different(MonoComObject* pUnk, MonoComObject** pOut)
+int STDCALL UIntIn(MonoComObject* pUnk, unsigned int a)
 {
-       create_com_object (pOut);
-       return 0;
+       return S_OK;
+}
+
+int STDCALL LongIn(MonoComObject* pUnk, LONGLONG a)
+{
+       return S_OK;
+}
+
+int STDCALL ULongIn(MonoComObject* pUnk, ULONGLONG a)
+{
+       return S_OK;
+}
+
+int STDCALL FloatIn(MonoComObject* pUnk, float a)
+{
+       return S_OK;
+}
+
+int STDCALL DoubleIn(MonoComObject* pUnk, double a)
+{
+       return S_OK;
+}
+
+int STDCALL ITestIn(MonoComObject* pUnk, MonoComObject *pUnk2)
+{
+       return S_OK;
+}
+
+int STDCALL ITestOut(MonoComObject* pUnk, MonoComObject* *ppUnk)
+{
+       return S_OK;
+}
+
+int STDCALL get_ITest(MonoComObject* pUnk, MonoComObject* *ppUnk)
+{
+       return S_OK;
 }
 
 static void create_com_object (MonoComObject** pOut)
@@ -2206,17 +2649,39 @@ static void create_com_object (MonoComObject** pOut)
        (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
        (*pOut)->vtbl->AddRef = MonoAddRef;
        (*pOut)->vtbl->Release = MonoRelease;
-       (*pOut)->vtbl->Add = Add;
-       (*pOut)->vtbl->Subtract = Subtract;
-       (*pOut)->vtbl->Same = Same;
-       (*pOut)->vtbl->Different = Different;
+       (*pOut)->vtbl->SByteIn = SByteIn;
+       (*pOut)->vtbl->ByteIn = ByteIn;
+       (*pOut)->vtbl->ShortIn = ShortIn;
+       (*pOut)->vtbl->UShortIn = UShortIn;
+       (*pOut)->vtbl->IntIn = IntIn;
+       (*pOut)->vtbl->UIntIn = UIntIn;
+       (*pOut)->vtbl->LongIn = LongIn;
+       (*pOut)->vtbl->ULongIn = ULongIn;
+       (*pOut)->vtbl->FloatIn = FloatIn;
+       (*pOut)->vtbl->DoubleIn = DoubleIn;
+       (*pOut)->vtbl->ITestIn = ITestIn;
+       (*pOut)->vtbl->ITestOut = ITestOut;
+       (*pOut)->vtbl->get_ITest = get_ITest;
 }
 
+static MonoComObject* same_object = NULL;
+
 STDCALL int
 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
 {
        create_com_object (pUnk);
 
+       if (!same_object)
+               same_object = *pUnk;
+
+       return 0;
+}
+
+STDCALL int
+mono_test_marshal_com_object_same(MonoComObject* *pUnk)
+{
+       *pUnk = same_object;
+
        return 0;
 }
 
@@ -2236,4 +2701,37 @@ mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)
        return pUnk->m_ref;
 }
 
+STDCALL int
+mono_test_marshal_ccw_itest (MonoComObject *pUnk)
+{
+       int hr = 0;
+       MonoComObject* pTest;
+       MonoComObject* pTest2;
+
+       if (!pUnk)
+               return 1;
+
+       hr = pUnk->vtbl->SByteIn (pUnk, -100);
+       hr = pUnk->vtbl->ByteIn (pUnk, 100);
+       hr = pUnk->vtbl->ShortIn (pUnk, -100);
+       hr = pUnk->vtbl->UShortIn (pUnk, 100);
+       hr = pUnk->vtbl->IntIn (pUnk, -100);
+       hr = pUnk->vtbl->UIntIn (pUnk, 100);
+       hr = pUnk->vtbl->LongIn (pUnk, -100);
+       hr = pUnk->vtbl->ULongIn (pUnk, 100);
+       hr = pUnk->vtbl->FloatIn (pUnk, 3.14f);
+       hr = pUnk->vtbl->DoubleIn (pUnk, 3.14);
+       hr = pUnk->vtbl->ITestIn (pUnk, pUnk);
+
+       hr = pUnk->vtbl->ITestOut (pUnk, &pTest);
+
+       //hr = pUnk->vtbl->get_ITest (pUnk, &pTest2);
+
+       if (hr != 0)
+               return 2;
+
+       return 0;
+}
+
+
 #endif //NOT_YET