Changed link from GUID to URL
[mono.git] / mono / tests / libtest.c
1 #include <config.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <glib.h>
6 #include <gmodule.h>
7 #include <errno.h>
8 #include <time.h>
9 #include <math.h>
10
11 #ifdef WIN32
12 #include <windows.h>
13 #include "initguid.h"
14 #else
15 #include <pthread.h>
16 #endif
17
18 #ifdef WIN32
19 #define STDCALL __stdcall
20 #else
21 #define STDCALL
22 #endif
23
24 #ifdef __GNUC__
25 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
26 #endif
27
28 #ifdef WIN32
29 extern __declspec(dllimport) void __stdcall CoTaskMemFree(void *ptr);
30 #endif
31
32 typedef int (STDCALL *SimpleDelegate) (int a);
33
34 #if defined(WIN32) && defined (_MSC_VER)
35 #define LIBTEST_API __declspec(dllexport)
36 #elif defined(__GNUC__)
37 #define LIBTEST_API  __attribute__ ((visibility ("default")))
38 #else
39 #define LIBTEST_API
40 #endif
41
42 static void marshal_free (void *ptr)
43 {
44 #ifdef WIN32
45         CoTaskMemFree (ptr);
46 #else
47         g_free (ptr);
48 #endif
49 }
50
51 static void* marshal_alloc (gsize size)
52 {
53 #ifdef WIN32
54         return CoTaskMemAlloc (size);
55 #else
56         return g_malloc (size);
57 #endif
58 }
59
60 static void* marshal_alloc0 (gsize size)
61 {
62 #ifdef WIN32
63         void* ptr = CoTaskMemAlloc (size);
64         memset(ptr, 0, size);
65         return ptr;
66 #else
67         return g_malloc0 (size);
68 #endif
69 }
70
71 static char* marshal_strdup (const char *str)
72 {
73 #ifdef WIN32
74         int len;
75         char *buf;
76
77         if (!str)
78                 return NULL;
79
80         len = strlen (str);
81         buf = (char *) CoTaskMemAlloc (len + 1);
82         return strcpy (buf, str);
83 #else
84         return g_strdup (str);
85 #endif
86 }
87
88 static gunichar2* marshal_bstr_alloc(const gchar* str)
89 {
90 #ifdef WIN32
91         gunichar2* ret = NULL;
92         gunichar2* temp = NULL;
93         temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
94         ret = SysAllocString (temp);
95         g_free (temp);
96         return ret;
97 #else
98         gchar* ret = NULL;
99         int slen = strlen (str);
100         gunichar2* temp;
101         /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
102         ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
103         if (ret == NULL)
104                 return NULL;
105         temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
106         memcpy (ret + sizeof(guint32), temp, slen * sizeof(gunichar2));
107         * ((guint32 *) ret) = slen * sizeof(gunichar2);
108         ret [4 + slen * sizeof(gunichar2)] = 0;
109         ret [5 + slen * sizeof(gunichar2)] = 0;
110
111         return (gunichar2*)(ret + 4);
112 #endif
113 }
114
115 #define marshal_new0(type,size)       ((type *) marshal_alloc0 (sizeof (type)* (size)))
116
117 LIBTEST_API int STDCALL
118 mono_cominterop_is_supported (void)
119 {
120 #if defined(TARGET_X86) || defined(TARGET_AMD64)
121         return 1;
122 #endif
123         return 0;
124 }
125
126 LIBTEST_API unsigned short* STDCALL
127 test_lpwstr_marshal (unsigned short* chars, long length)
128 {
129         int i = 0;
130         unsigned short *res;
131
132         res = marshal_alloc (2 * (length + 1));
133
134         // printf("test_lpwstr_marshal()\n");
135         
136         while ( i < length ) {
137                 // printf("X|%u|\n", chars[i]);
138                 res [i] = chars[i];
139                 i++;
140         }
141
142         res [i] = 0;
143
144         return res;
145 }
146
147
148 LIBTEST_API void STDCALL
149 test_lpwstr_marshal_out (unsigned short** chars)
150 {
151         int i = 0;
152         const char abc[] = "ABC";
153         glong len = strlen(abc);
154
155         *chars = marshal_alloc (2 * (len + 1));
156         
157         while ( i < len ) {
158                 (*chars) [i] = abc[i];
159                 i++;
160         }
161
162         (*chars) [i] = 0;
163 }
164
165 typedef struct {
166         int b;
167         int a;
168         int c;
169 } union_test_1_type;
170
171 LIBTEST_API int STDCALL  
172 mono_union_test_1 (union_test_1_type u1) {
173         // printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
174         return u1.a + u1.b + u1.c;
175 }
176
177 LIBTEST_API int STDCALL  
178 mono_return_int (int a) {
179         // printf ("Got value %d\n", a);
180         return a;
181 }
182
183 LIBTEST_API float STDCALL  
184 mono_test_marshal_pass_return_float (float f) {
185         return f + 1.0;
186 }
187
188 struct ss
189 {
190         int i;
191 };
192
193 LIBTEST_API int STDCALL 
194 mono_return_int_ss (struct ss a) {
195         // printf ("Got value %d\n", a.i);
196         return a.i;
197 }
198
199 LIBTEST_API struct ss STDCALL
200 mono_return_ss (struct ss a) {
201         // printf ("Got value %d\n", a.i);
202         a.i++;
203         return a;
204 }
205
206 struct sc1
207 {
208         char c[1];
209 };
210
211 LIBTEST_API struct sc1 STDCALL
212 mono_return_sc1 (struct sc1 a) {
213         // printf ("Got value %d\n", a.c[0]);
214         a.c[0]++;
215         return a;
216 }
217
218
219 struct sc3
220 {
221         char c[3];
222 };
223
224 LIBTEST_API struct sc3 STDCALL 
225 mono_return_sc3 (struct sc3 a) {
226         // printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
227         a.c[0]++;
228         a.c[1] += 2;
229         a.c[2] += 3;
230         return a;
231 }
232
233 struct sc5
234 {
235         char c[5];
236 };
237
238 LIBTEST_API struct sc5 STDCALL 
239 mono_return_sc5 (struct sc5 a) {
240         // printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
241         a.c[0]++;
242         a.c[1] += 2;
243         a.c[2] += 3;
244         a.c[3] += 4;
245         a.c[4] += 5;
246         return a;
247 }
248
249 union su
250 {
251         int i1;
252         int i2;
253 };
254
255 LIBTEST_API int STDCALL  
256 mono_return_int_su (union su a) {
257         // printf ("Got value %d\n", a.i1);
258         return a.i1;
259 }
260
261 struct FI {
262         float f1;
263         float f2;
264         float f3;
265 };
266
267 struct NestedFloat {
268         struct FI fi;
269         float f4;
270 };
271
272 LIBTEST_API struct NestedFloat STDCALL
273 mono_return_nested_float (void)
274 {
275         struct NestedFloat f;
276         f.fi.f1 = 1.0;
277         f.fi.f2 = 2.0;
278         f.fi.f3 = 3.0;
279         f.f4 = 4.0;
280         return f;
281 }
282
283 LIBTEST_API int STDCALL  
284 mono_test_many_int_arguments (int a, int b, int c, int d, int e,
285                                                           int f, int g, int h, int i, int j);
286 LIBTEST_API short STDCALL 
287 mono_test_many_short_arguments (short a, short b, short c, short d, short e,
288                                                                 short f, short g, short h, short i, short j);
289 LIBTEST_API char STDCALL 
290 mono_test_many_char_arguments (char a, char b, char c, char d, char e,
291                                                            char f, char g, char h, char i, char j);
292
293 LIBTEST_API int STDCALL 
294 mono_test_many_int_arguments (int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
295 {
296         return a + b + c + d + e + f + g + h + i + j;
297 }
298
299 LIBTEST_API short STDCALL 
300 mono_test_many_short_arguments (short a, short b, short c, short d, short e, short f, short g, short h, short i, short j)
301 {
302         return a + b + c + d + e + f + g + h + i + j;
303 }
304
305 LIBTEST_API char STDCALL 
306 mono_test_many_byte_arguments (char a, char b, char c, char d, char e, char f, char g, char h, char i, char j)
307 {
308         return a + b + c + d + e + f + g + h + i + j;
309 }
310
311 LIBTEST_API float STDCALL 
312 mono_test_many_float_arguments (float a, float b, float c, float d, float e, float f, float g, float h, float i, float j)
313 {
314         return a + b + c + d + e + f + g + h + i + j;
315 }
316
317 LIBTEST_API double STDCALL 
318 mono_test_many_double_arguments (double a, double b, double c, double d, double e, double f, double g, double h, double i, double j)
319 {
320         return a + b + c + d + e + f + g + h + i + j;
321 }
322
323 LIBTEST_API double STDCALL 
324 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
325 {
326         return a + b + c + d + e;
327 }
328
329 LIBTEST_API int STDCALL 
330 mono_test_puts_static (char *s)
331 {
332         // printf ("TEST %s\n", s);
333         return 1;
334 }
335
336 typedef int (STDCALL *SimpleDelegate3) (int a, int b);
337
338 LIBTEST_API int STDCALL 
339 mono_invoke_delegate (SimpleDelegate3 delegate)
340 {
341         int res;
342
343         // printf ("start invoke %p\n", delegate);
344
345         res = delegate (2, 3);
346
347         // printf ("end invoke\n");
348
349         return res;
350 }
351
352 LIBTEST_API int STDCALL
353 mono_invoke_simple_delegate (SimpleDelegate d)
354 {
355         return d (4);
356 }
357
358 LIBTEST_API int STDCALL  
359 mono_test_marshal_char (short a1)
360 {
361         if (a1 == 'a')
362                 return 0;
363         
364         return 1;
365 }
366
367 LIBTEST_API void STDCALL
368 mono_test_marshal_char_array (gunichar2 *s)
369 {
370         const char m[] = "abcdef";
371         gunichar2* s2;
372         glong len;
373
374         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
375         
376         len = (len * 2) + 2;
377         memcpy (s, s2, len);
378
379         g_free (s2);
380 }
381
382 LIBTEST_API int STDCALL
383 mono_test_marshal_ansi_char_array (char *s)
384 {
385         const char m[] = "abcdef";
386
387         if (strncmp ("qwer", s, 4))
388                 return 1;
389
390         memcpy (s, m, sizeof (m));
391         return 0;
392 }
393
394 LIBTEST_API int STDCALL
395 mono_test_marshal_unicode_char_array (gunichar2 *s)
396 {
397         const char m[] = "abcdef";
398         const char expected[] = "qwer";
399         gunichar2 *s1, *s2;
400         glong len1, len2;
401
402         s1 = g_utf8_to_utf16 (m, -1, NULL, &len1, NULL);
403         s2 = g_utf8_to_utf16 (expected, -1, NULL, &len2, NULL);
404         len1 = (len1 * 2);
405         len2 = (len2 * 2);
406
407         if (memcmp (s, s2, len2))
408                 return 1;
409
410         memcpy (s, s1, len1);
411         return 0;
412 }
413
414 LIBTEST_API int STDCALL 
415 mono_test_empty_pinvoke (int i)
416 {
417         return i;
418 }
419
420 LIBTEST_API int STDCALL  
421 mono_test_marshal_bool_byref (int a, int *b, int c)
422 {
423     int res = *b;
424
425         *b = 1;
426
427         return res;
428 }
429
430 LIBTEST_API int STDCALL 
431 mono_test_marshal_bool_in_as_I1_U1 (char bTrue, char bFalse)
432 {
433         if (!bTrue)
434                 return 1;
435         if (bFalse)
436                 return 2;
437         return 0;
438 }
439
440 LIBTEST_API int STDCALL 
441 mono_test_marshal_bool_out_as_I1_U1 (char* bTrue, char* bFalse)
442 {
443         if (!bTrue || !bFalse)
444                 return 3;
445
446         *bTrue = 1;
447         *bFalse = 0;
448
449         return 0;
450 }
451
452 LIBTEST_API int STDCALL 
453 mono_test_marshal_bool_ref_as_I1_U1 (char* bTrue, char* bFalse)
454 {
455         if (!bTrue || !bFalse)
456                 return 4;
457
458         if (!(*bTrue))
459                 return 5;
460         if (*bFalse)
461                 return 6;
462
463         *bFalse = 1;
464         *bTrue = 0;
465
466         return 0;
467 }
468
469 LIBTEST_API int STDCALL  
470 mono_test_marshal_array (int *a1)
471 {
472         int i, sum = 0;
473
474         for (i = 0; i < 50; i++)
475                 sum += a1 [i];
476         
477         return sum;
478 }
479
480 LIBTEST_API int STDCALL  
481 mono_test_marshal_inout_array (int *a1)
482 {
483         int i, sum = 0;
484
485         for (i = 0; i < 50; i++) {
486                 sum += a1 [i];
487                 a1 [i] = 50 - a1 [i];
488         }
489         
490         return sum;
491 }
492
493 LIBTEST_API int /* cdecl */
494 mono_test_marshal_inout_array_cdecl (int *a1)
495 {
496         return mono_test_marshal_inout_array (a1);
497 }
498
499 LIBTEST_API int STDCALL  
500 mono_test_marshal_out_array (int *a1)
501 {
502         int i;
503
504         for (i = 0; i < 50; i++) {
505                 a1 [i] = i;
506         }
507         
508         return 0;
509 }
510
511 LIBTEST_API int STDCALL
512 mono_test_marshal_out_byref_array_out_size_param (int **out_arr, int *out_len)
513 {
514         int *arr;
515         int i, len;
516
517         len = 4;
518         arr = marshal_alloc (sizeof (gint32) * len);
519         for (i = 0; i < len; ++i)
520                 arr [i] = i;
521         *out_arr = arr;
522         *out_len = len;
523
524         return 0;
525 }
526
527 LIBTEST_API int STDCALL  
528 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
529 {
530         int i, sum = 0;
531
532         for (i = 0; i < 10; i++) {
533                 a1 [i] = 'F';
534         }
535         
536         return sum;
537 }
538
539 typedef struct {
540         int a;
541         int b;
542         int c;
543         const char *d;
544         gunichar2 *d2;
545 } simplestruct;
546
547 typedef struct {
548         double x;
549         double y;
550 } point;
551
552 LIBTEST_API simplestruct STDCALL 
553 mono_test_return_vtype (int i)
554 {
555         simplestruct res;
556         static gunichar2 test2 [] = { 'T', 'E', 'S', 'T', '2', 0 };
557
558         res.a = 0;
559         res.b = 1;
560         res.c = 0;
561         res.d = "TEST";
562         res.d2 = test2;
563
564         return res;
565 }
566
567 LIBTEST_API void STDCALL
568 mono_test_delegate_struct (void)
569 {
570         // printf ("TEST\n");
571 }
572
573 typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
574
575 LIBTEST_API char * STDCALL 
576 mono_test_return_string (ReturnStringDelegate func)
577 {
578         char *res;
579
580         // printf ("mono_test_return_string\n");
581
582         res = func ("TEST");
583         marshal_free (res);
584
585         // printf ("got string: %s\n", res);
586         return marshal_strdup ("12345");
587 }
588
589 typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
590
591 LIBTEST_API int STDCALL 
592 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
593 {
594         if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
595             !strcmp (ss->d, "TEST1")) {
596                 ss->a = 1;
597                 ss->b = 0;
598                 ss->c = 1;
599                 ss->d = "TEST2";
600
601                 return func (a, ss, b);
602         }
603
604         return 1;
605 }
606
607 typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
608
609 LIBTEST_API int STDCALL 
610 mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
611 {
612         /* Check that the input pointer is ignored */
613         ss->d = (gpointer)0x12345678;
614
615         func (a, ss, b);
616
617         if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
618                 return 0;
619         else
620                 return 1;
621 }
622
623 typedef int (STDCALL *InVTypeDelegate) (int a, simplestruct *ss, int b);
624
625 LIBTEST_API int STDCALL 
626 mono_test_marshal_in_struct (int a, simplestruct *ss, int b, InVTypeDelegate func)
627 {
628         simplestruct ss2;
629         int res;
630
631         memcpy (&ss2, ss, sizeof (simplestruct));
632
633         res = func (a, ss, b);
634         if (res) {
635                 printf ("mono_test_marshal_in_struct () failed: %d\n", res);
636                 return 1;
637         }
638
639         /* Check that no modifications is made to the struct */
640         if (ss2.a == ss->a && ss2.b == ss->b && ss2.c == ss->c && ss2.d == ss->d)
641                 return 0;
642         else
643                 return 1;
644 }
645
646 typedef struct {
647         int a;
648         SimpleDelegate func, func2, func3;
649 } DelegateStruct;
650
651 LIBTEST_API DelegateStruct STDCALL 
652 mono_test_marshal_delegate_struct (DelegateStruct ds)
653 {
654         DelegateStruct res;
655
656         res.a = ds.func (ds.a) + ds.func2 (ds.a) + (ds.func3 == NULL ? 0 : 1);
657         res.func = ds.func;
658         res.func2 = ds.func2;
659         res.func3 = NULL;
660
661         return res;
662 }
663
664 LIBTEST_API int STDCALL  
665 mono_test_marshal_struct (simplestruct ss)
666 {
667         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
668             !strcmp (ss.d, "TEST"))
669                 return 0;
670
671         return 1;
672 }
673
674 LIBTEST_API int STDCALL 
675 mono_test_marshal_byref_struct (simplestruct *ss, int a, int b, int c, char *d)
676 {
677         gboolean res = (ss->a == a && ss->b == b && ss->c == c && strcmp (ss->d, d) == 0);
678
679         marshal_free ((char*)ss->d);
680
681         ss->a = !ss->a;
682         ss->b = !ss->b;
683         ss->c = !ss->c;
684         ss->d = marshal_strdup ("DEF");
685
686         return res ? 0 : 1;
687 }
688
689 typedef struct {
690         int a;
691         int b;
692         int c;
693         char *d;
694         unsigned char e;
695         double f;
696         unsigned char g;
697         guint64 h;
698 } simplestruct2;
699
700 LIBTEST_API int STDCALL 
701 mono_test_marshal_struct2 (simplestruct2 ss)
702 {
703         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
704             !strcmp (ss.d, "TEST") && 
705             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
706                 return 0;
707
708         return 1;
709 }
710
711 /* on HP some of the struct should be on the stack and not in registers */
712 LIBTEST_API int STDCALL 
713 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
714 {
715         if (i != 10 || j != 11 || k != 12)
716                 return 1;
717         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
718             !strcmp (ss.d, "TEST") && 
719             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
720                 return 0;
721
722         return 1;
723 }
724
725 LIBTEST_API int STDCALL  
726 mono_test_marshal_lpstruct (simplestruct *ss)
727 {
728         if (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
729             !strcmp (ss->d, "TEST"))
730                 return 0;
731
732         return 1;
733 }
734
735 LIBTEST_API int STDCALL  
736 mono_test_marshal_lpstruct_blittable (point *p)
737 {
738         if (p->x == 1.0 && p->y == 2.0)
739                 return 0;
740         else
741                 return 1;
742 }
743
744 LIBTEST_API int STDCALL 
745 mono_test_marshal_struct_array (simplestruct2 *ss)
746 {
747         if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
748                    !strcmp (ss[0].d, "TEST") && 
749                    ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
750                 return 1;
751
752         if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
753                    !strcmp (ss[1].d, "TEST2") && 
754                    ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
755                 return 1;
756
757         return 0;
758 }
759
760 typedef struct long_align_struct {
761         gint32 a;
762         gint64 b;
763         gint64 c;
764 } long_align_struct;
765
766 LIBTEST_API int STDCALL 
767 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
768 {
769         return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
770 }
771
772 LIBTEST_API simplestruct2 * STDCALL 
773 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
774 {
775         simplestruct2 *res;
776
777         if (!ss)
778                 return NULL;
779
780         if (i != 10 || j != 11 || k != 12 || l != 14)
781                 return NULL;
782         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
783                    !strcmp (ss->d, "TEST") && 
784                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
785                 return NULL;
786
787         res = marshal_new0 (simplestruct2, 1);
788         memcpy (res, ss, sizeof (simplestruct2));
789         res->d = marshal_strdup ("TEST");
790         return res;
791 }
792
793 LIBTEST_API int STDCALL 
794 mono_test_marshal_byref_class (simplestruct2 **ssp)
795 {
796         simplestruct2 *ss = *ssp;
797         simplestruct2 *res;
798         
799         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
800                    !strcmp (ss->d, "TEST") && 
801                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
802                 return 1;
803
804         res = marshal_new0 (simplestruct2, 1);
805         memcpy (res, ss, sizeof (simplestruct2));
806         res->d = marshal_strdup ("TEST-RES");
807
808         *ssp = res;
809         return 0;
810 }
811
812 static void *
813 get_sp (void)
814 {
815         int i;
816         void *p;
817
818         /* Yes, this is correct, we are only trying to determine the value of the stack here */
819         p = &i;
820         return p;
821 }
822
823 LIBTEST_API int STDCALL 
824 reliable_delegate (int a)
825 {
826         return a;
827 }
828
829 /*
830  * Checks whether get_sp() works as expected. It doesn't work with gcc-2.95.3 on linux.
831  */
832 static gboolean
833 is_get_sp_reliable (void)
834 {
835         void *sp1, *sp2;
836
837         reliable_delegate(1);
838         sp1 = get_sp();
839         reliable_delegate(1);
840         sp2 = get_sp();
841         return sp1 == sp2;
842
843
844 LIBTEST_API int STDCALL 
845 mono_test_marshal_delegate (SimpleDelegate delegate)
846 {
847         void *sp1, *sp2;
848
849         /* Check that the delegate wrapper is stdcall */
850         delegate (2);
851         sp1 = get_sp ();
852         delegate (2);
853         sp2 = get_sp ();
854         if (is_get_sp_reliable())
855                 g_assert (sp1 == sp2);
856
857         return delegate (2);
858 }
859
860 static int STDCALL inc_cb (int i)
861 {
862         return i + 1;
863 }
864
865 LIBTEST_API int STDCALL 
866 mono_test_marshal_out_delegate (SimpleDelegate *delegate)
867 {
868         *delegate = inc_cb;
869
870         return 0;
871 }
872
873 LIBTEST_API SimpleDelegate STDCALL 
874 mono_test_marshal_return_delegate (SimpleDelegate delegate)
875 {
876         return delegate;
877 }
878
879 typedef int DelegateByrefDelegate (void *);
880
881 LIBTEST_API int STDCALL
882 mono_test_marshal_delegate_ref_delegate (DelegateByrefDelegate del)
883 {
884         int (*ptr) (int i);
885
886         del (&ptr);
887
888         return ptr (54);
889 }
890
891 static int STDCALL
892 return_plus_one (int i)
893 {
894         return i + 1;
895 }
896
897 LIBTEST_API SimpleDelegate STDCALL 
898 mono_test_marshal_return_delegate_2 (void)
899 {
900         return return_plus_one;
901 }
902
903 typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
904
905 static gboolean
906 is_utf16_equals (gunichar2 *s1, const char *s2)
907 {
908         char *s;
909         int res;
910
911         s = g_utf16_to_utf8 (s1, -1, NULL, NULL, NULL);
912         res = strcmp (s, s2);
913         g_free (s);
914
915         return res == 0;
916 }
917
918 LIBTEST_API int STDCALL 
919 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
920 {
921         simplestruct ss, res;
922
923         ss.a = 0;
924         ss.b = 1;
925         ss.c = 0;
926         ss.d = "TEST";
927         ss.d2 = g_utf8_to_utf16 ("TEST2", -1, NULL, NULL, NULL); 
928
929         res = delegate (ss);
930         if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES") && is_utf16_equals (res.d2, "TEST2-RES")))
931                 return 1;
932
933         return 0;
934 }
935
936 typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
937
938 LIBTEST_API int STDCALL 
939 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
940 {
941         simplestruct ss;
942         simplestruct *res;
943
944         ss.a = 0;
945         ss.b = 1;
946         ss.c = 0;
947         ss.d = "TEST";
948
949         /* Check argument */
950         res = delegate (&ss);
951         if (!res)
952                 return 1;
953
954         /* Check return value */
955         if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
956                 return 2;
957
958         /* Check NULL argument and NULL result */
959         res = delegate (NULL);
960         if (res)
961                 return 3;
962
963         return 0;
964 }
965
966 typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
967
968 LIBTEST_API int STDCALL 
969 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
970 {
971         simplestruct ss;
972         int res;
973         simplestruct *ptr;
974
975         ss.a = 0;
976         ss.b = 1;
977         ss.c = 0;
978         ss.d = "TEST";
979
980         ptr = &ss;
981
982         res = delegate (&ptr);
983         if (res != 0)
984                 return 1;
985
986         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
987                 return 2;
988
989         return 0;
990 }
991
992 LIBTEST_API int STDCALL 
993 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
994 {
995         delegate (NULL);
996         return 0;
997 }
998
999 typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
1000
1001 LIBTEST_API int STDCALL 
1002 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
1003 {
1004         int res;
1005         simplestruct *ptr;
1006
1007         /* Check that the input pointer is ignored */
1008         ptr = (gpointer)0x12345678;
1009
1010         res = delegate (&ptr);
1011         if (res != 0)
1012                 return 1;
1013
1014         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
1015                 return 2;
1016
1017         return 0;
1018 }
1019
1020 typedef int (STDCALL *InOutByvalClassDelegate) (simplestruct *ss);
1021
1022 LIBTEST_API int STDCALL 
1023 mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate delegate)
1024 {
1025         int res;
1026         simplestruct ss;
1027
1028         ss.a = FALSE;
1029         ss.b = TRUE;
1030         ss.c = FALSE;
1031         ss.d = g_strdup_printf ("%s", "FOO");
1032
1033         res = delegate (&ss);
1034         if (res != 0)
1035                 return 1;
1036
1037         if (!(ss.a && !ss.b && ss.c && !strcmp (ss.d, "RES")))
1038                 return 2;
1039
1040         return 0;
1041 }
1042
1043 typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
1044
1045 LIBTEST_API int STDCALL 
1046 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
1047 {
1048         return delegate (s);
1049 }
1050
1051 typedef int (STDCALL *return_int_fnt) (int i);
1052 typedef int (STDCALL *SimpleDelegate9) (return_int_fnt d);
1053
1054 LIBTEST_API int STDCALL 
1055 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
1056 {
1057         return delegate (ftn);
1058 }
1059
1060 static int STDCALL 
1061 return_self (int i)
1062 {
1063         return i;
1064 }
1065
1066 LIBTEST_API int STDCALL 
1067 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
1068 {
1069         return delegate (return_self);
1070 }
1071
1072 typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
1073
1074 LIBTEST_API int STDCALL 
1075 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
1076 {
1077         int i = 1;
1078
1079         int res = delegate (&i);
1080         if (res != 0)
1081                 return res;
1082
1083         if (i != 2)
1084                 return 2;
1085
1086         return 0;
1087 }
1088
1089 typedef int (STDCALL *return_int_delegate) (int i);
1090
1091 typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) (void);
1092
1093 LIBTEST_API int STDCALL 
1094 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
1095 {
1096         return (d ()) (55);
1097 }
1098
1099 LIBTEST_API int STDCALL  
1100 mono_test_marshal_stringbuilder (char *s, int n)
1101 {
1102         const char m[] = "This is my message.  Isn't it nice?";
1103
1104         if (strcmp (s, "ABCD") != 0)
1105                 return 1;
1106         strncpy(s, m, n);
1107         s [n] = '\0';
1108         return 0;
1109 }
1110
1111 LIBTEST_API int STDCALL  
1112 mono_test_marshal_stringbuilder_append (char *s, int length)
1113 {
1114         const char out_sentinel[] = "CSHARP_";
1115         const char out_len = strlen (out_sentinel);
1116
1117         for (int i=0; i < length; i++) {
1118                 s [i] = out_sentinel [i % out_len];
1119         }
1120
1121         s [length] = '\0';
1122
1123
1124         return 0;
1125 }
1126
1127 LIBTEST_API int STDCALL  
1128 mono_test_marshal_stringbuilder_default (char *s, int n)
1129 {
1130         const char m[] = "This is my message.  Isn't it nice?";
1131
1132         strncpy(s, m, n);
1133         s [n] = '\0';
1134         return 0;
1135 }
1136
1137 LIBTEST_API int STDCALL  
1138 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
1139 {
1140         const char m[] = "This is my message.  Isn't it nice?";
1141         gunichar2* s2;
1142         glong len;
1143
1144         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
1145         
1146         len = (len * 2) + 2;
1147         if (len > (n * 2))
1148                 len = n * 2;
1149         memcpy (s, s2, len);
1150
1151         g_free (s2);
1152
1153         return 0;
1154 }
1155
1156 LIBTEST_API void STDCALL
1157 mono_test_marshal_stringbuilder_out (char **s)
1158 {
1159         const char m[] = "This is my message.  Isn't it nice?";
1160         char *str;
1161
1162         str = marshal_alloc (strlen (m) + 1);
1163         memcpy (str, m, strlen (m) + 1);
1164         
1165         *s = str;
1166 }
1167
1168 LIBTEST_API int STDCALL  
1169 mono_test_marshal_stringbuilder_out_unicode (gunichar2 **s)
1170 {
1171         const char m[] = "This is my message.  Isn't it nice?";
1172         gunichar2 *s2;
1173         glong len;
1174
1175         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
1176         
1177         len = (len * 2) + 2;
1178         *s = marshal_alloc (len);
1179         memcpy (*s, s2, len);
1180
1181         g_free (s2);
1182
1183         return 0;
1184 }
1185
1186 LIBTEST_API int STDCALL
1187 mono_test_marshal_stringbuilder_ref (char **s)
1188 {
1189         const char m[] = "This is my message.  Isn't it nice?";
1190         char *str;
1191
1192         if (strcmp (*s, "ABC"))
1193                 return 1;
1194
1195         str = marshal_alloc (strlen (m) + 1);
1196         memcpy (str, m, strlen (m) + 1);
1197         
1198         *s = str;
1199         return 0;
1200 }
1201
1202 typedef struct {
1203 #ifndef __GNUC__
1204     char a;
1205 #endif
1206 } EmptyStruct;
1207
1208 LIBTEST_API int STDCALL 
1209 mono_test_marshal_empty_string_array (char **array)
1210 {
1211         return (array == NULL) ? 0 : 1;
1212 }
1213
1214 LIBTEST_API int STDCALL 
1215 mono_test_marshal_string_array (char **array)
1216 {
1217         if (strcmp (array [0], "ABC"))
1218                 return 1;
1219         if (strcmp (array [1], "DEF"))
1220                 return 2;
1221
1222         if (array [2] != NULL)
1223                 return 3;
1224
1225         return 0;
1226 }
1227
1228 LIBTEST_API int STDCALL 
1229 mono_test_marshal_byref_string_array (char ***array)
1230 {
1231         if (*array == NULL)
1232                 return 0;
1233
1234         if (strcmp ((*array) [0], "Alpha"))
1235                 return 2;
1236         if (strcmp ((*array) [1], "Beta"))
1237                 return 2;
1238         if (strcmp ((*array) [2], "Gamma"))
1239                 return 2;
1240
1241         return 1;
1242 }
1243
1244 LIBTEST_API int STDCALL 
1245 mono_test_marshal_stringbuilder_array (char **array)
1246 {
1247         if (strcmp (array [0], "ABC"))
1248                 return 1;
1249         if (strcmp (array [1], "DEF"))
1250                 return 2;
1251
1252         strcpy (array [0], "DEF");
1253         strcpy (array [1], "ABC");
1254
1255         return 0;
1256 }
1257
1258 LIBTEST_API int STDCALL 
1259 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
1260 {
1261         GError *error = NULL;
1262         char *s;
1263         
1264         s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
1265         if (strcmp (s, "ABC")) {
1266                 g_free (s);
1267                 return 1;
1268         }
1269         else
1270                 g_free (s);
1271
1272         s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
1273         if (strcmp (s, "DEF")) {
1274                 g_free (s);
1275                 return 2;
1276         }
1277         else
1278                 g_free (s);
1279
1280         if (strcmp (array2 [0], "ABC"))
1281                 return 3;
1282
1283         if (strcmp (array2 [1], "DEF")) 
1284                 return 4;
1285
1286         return 0;
1287 }
1288
1289 /* this does not work on Redhat gcc 2.96 */
1290 LIBTEST_API int STDCALL  
1291 mono_test_empty_struct (int a, EmptyStruct es, int b)
1292 {
1293         // printf ("mono_test_empty_struct %d %d\n", a, b);
1294
1295         // Intel icc on ia64 passes 'es' in 2 registers
1296 #if defined(__ia64) && defined(__INTEL_COMPILER)
1297         return 0;
1298 #else
1299         if (a == 1 && b == 2)
1300                 return 0;
1301         return 1;
1302 #endif
1303 }
1304
1305 LIBTEST_API EmptyStruct STDCALL
1306 mono_test_return_empty_struct (int a)
1307 {
1308         EmptyStruct s;
1309
1310         g_assert (a == 42);
1311
1312         return s;
1313 }
1314
1315 typedef struct {
1316        char a[100];
1317 } ByValStrStruct;
1318
1319 LIBTEST_API ByValStrStruct * STDCALL 
1320 mono_test_byvalstr_gen (void)
1321 {
1322         ByValStrStruct *ret;
1323        
1324         ret = malloc(sizeof(ByValStrStruct));
1325         memset(ret, 'a', sizeof(ByValStrStruct)-1);
1326         ret->a[sizeof(ByValStrStruct)-1] = 0;
1327
1328         return ret;
1329 }
1330
1331 LIBTEST_API int STDCALL 
1332 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
1333 {
1334         int ret;
1335
1336         ret = strcmp(data->a, correctString);
1337         // printf ("T1: %s\n", data->a);
1338         // printf ("T2: %s\n", correctString);
1339
1340         /* we need g_free because the allocation was performed by mono_test_byvalstr_gen */
1341         g_free (data);
1342         return (ret != 0);
1343 }
1344
1345 typedef struct {
1346         guint16 a[4];
1347         int  flag;
1348 } ByValStrStruct_Unicode;
1349
1350 LIBTEST_API int STDCALL 
1351 mono_test_byvalstr_check_unicode (ByValStrStruct_Unicode *ref, int test)
1352 {
1353         if (ref->flag != 0x1234abcd){
1354                 printf ("overwritten data");
1355                 return 1;
1356         }
1357             
1358         if (test == 1 || test == 3){
1359                 if (ref->a [0] != '1' ||
1360                     ref->a [1] != '2'   ||
1361                     ref->a [2] != '3')
1362                         return 1;
1363                 return 0;
1364         }
1365         if (test == 2){
1366                 if (ref->a [0] != '1' ||
1367                     ref->a [1] != '2')
1368                         return 1;
1369                 return 0;
1370         }
1371         return 10;
1372 }
1373
1374 LIBTEST_API int STDCALL 
1375 NameManglingAnsi (char *data)
1376 {
1377         return data [0] + data [1] + data [2];
1378 }
1379
1380 LIBTEST_API int STDCALL 
1381 NameManglingAnsiA (char *data)
1382 {
1383         g_assert_not_reached ();
1384 }
1385
1386 LIBTEST_API int STDCALL 
1387 NameManglingAnsiW (char *data)
1388 {
1389         g_assert_not_reached ();
1390 }
1391
1392 LIBTEST_API int STDCALL 
1393 NameManglingAnsi2A (char *data)
1394 {
1395         return data [0] + data [1] + data [2];
1396 }
1397
1398 LIBTEST_API int STDCALL 
1399 NameManglingAnsi2W (char *data)
1400 {
1401         g_assert_not_reached ();
1402 }
1403
1404 LIBTEST_API int STDCALL 
1405 NameManglingUnicode (char *data)
1406 {
1407         g_assert_not_reached ();
1408 }
1409
1410 LIBTEST_API int STDCALL 
1411 NameManglingUnicodeW (gunichar2 *data)
1412 {
1413         return data [0] + data [1] + data [2];
1414 }
1415
1416 LIBTEST_API int STDCALL 
1417 NameManglingUnicode2 (gunichar2 *data)
1418 {
1419         return data [0] + data [1] + data [2];
1420 }
1421
1422 LIBTEST_API int STDCALL 
1423 NameManglingAutoW (char *data)
1424 {
1425 #ifdef WIN32
1426         return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
1427 #else
1428         g_assert_not_reached ();
1429 #endif
1430 }
1431
1432 LIBTEST_API int STDCALL 
1433 NameManglingAuto (char *data)
1434 {
1435 #ifndef WIN32
1436         return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
1437 #else
1438         g_assert_not_reached ();
1439 #endif
1440 }
1441
1442 typedef int (STDCALL *intcharFunc)(const char*);
1443
1444 LIBTEST_API void STDCALL 
1445 callFunction (intcharFunc f)
1446 {
1447         f ("ABC");
1448 }
1449
1450 typedef struct {
1451         const char* str;
1452         int i;
1453 } SimpleObj;
1454
1455 LIBTEST_API int STDCALL 
1456 class_marshal_test0 (SimpleObj *obj1)
1457 {
1458         // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1459
1460         if (strcmp(obj1->str, "T1"))
1461                 return -1;
1462         if (obj1->i != 4)
1463                 return -2;
1464
1465         return 0;
1466 }
1467
1468 LIBTEST_API int STDCALL 
1469 class_marshal_test4 (SimpleObj *obj1)
1470 {
1471         if (obj1)
1472                 return -1;
1473
1474         return 0;
1475 }
1476
1477 LIBTEST_API void STDCALL
1478 class_marshal_test1 (SimpleObj **obj1)
1479 {
1480         SimpleObj *res = malloc (sizeof (SimpleObj));
1481
1482         res->str = marshal_strdup ("ABC");
1483         res->i = 5;
1484
1485         *obj1 = res;
1486 }
1487
1488 LIBTEST_API int STDCALL 
1489 class_marshal_test2 (SimpleObj **obj1)
1490 {
1491         // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1492
1493         if (strcmp((*obj1)->str, "ABC"))
1494                 return -1;
1495         if ((*obj1)->i != 5)
1496                 return -2;
1497
1498         return 0;
1499 }
1500
1501 LIBTEST_API int STDCALL 
1502 string_marshal_test0 (char *str)
1503 {
1504         if (strcmp (str, "TEST0"))
1505                 return -1;
1506
1507         return 0;
1508 }
1509
1510 LIBTEST_API void STDCALL
1511 string_marshal_test1 (const char **str)
1512 {
1513         *str = marshal_strdup ("TEST1");
1514 }
1515
1516 LIBTEST_API int STDCALL 
1517 string_marshal_test2 (char **str)
1518 {
1519         // printf ("string_marshal_test2 %s\n", *str);
1520
1521         if (strcmp (*str, "TEST1"))
1522                 return -1;
1523
1524         *str = marshal_strdup ("TEST2");
1525
1526         return 0;
1527 }
1528
1529 LIBTEST_API int STDCALL 
1530 string_marshal_test3 (char *str)
1531 {
1532         if (str)
1533                 return -1;
1534
1535         return 0;
1536 }
1537
1538 typedef struct {
1539         int a;
1540         int b;
1541 } BlittableClass;
1542
1543 LIBTEST_API BlittableClass* STDCALL 
1544 TestBlittableClass (BlittableClass *vl)
1545 {
1546         BlittableClass *res;
1547
1548         // printf ("TestBlittableClass %d %d\n", vl->a, vl->b);
1549
1550         if (vl) {
1551                 vl->a++;
1552                 vl->b++;
1553
1554                 res = marshal_new0 (BlittableClass, 1);
1555                 memcpy (res, vl, sizeof (BlittableClass));
1556         } else {
1557                 res = marshal_new0 (BlittableClass, 1);
1558                 res->a = 42;
1559                 res->b = 43;
1560         }
1561
1562         return res;
1563 }
1564
1565 typedef struct OSVERSIONINFO_STRUCT
1566
1567         int a; 
1568         int b; 
1569 } OSVERSIONINFO_STRUCT;
1570
1571 LIBTEST_API int STDCALL  
1572 MyGetVersionEx (OSVERSIONINFO_STRUCT *osvi)
1573 {
1574
1575         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1576
1577         osvi->a += 1;
1578         osvi->b += 1;
1579
1580         return osvi->a + osvi->b;
1581 }
1582
1583 LIBTEST_API int STDCALL  
1584 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO_STRUCT *osvi)
1585 {
1586
1587         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1588
1589         osvi->a += 1;
1590         osvi->b += 1;
1591
1592         return osvi->a + osvi->b;
1593 }
1594
1595 LIBTEST_API int STDCALL 
1596 mono_test_marshal_point (point pt)
1597 {
1598         // printf("point %g %g\n", pt.x, pt.y);
1599         if (pt.x == 1.25 && pt.y == 3.5)
1600                 return 0;
1601
1602         return 1;
1603 }
1604
1605 typedef struct {
1606         int x;
1607         double y;
1608 } mixed_point;
1609
1610 LIBTEST_API int STDCALL 
1611 mono_test_marshal_mixed_point (mixed_point pt)
1612 {
1613         // printf("mixed point %d %g\n", pt.x, pt.y);
1614         if (pt.x == 5 && pt.y == 6.75)
1615                 return 0;
1616
1617         return 1;
1618 }
1619
1620 LIBTEST_API int STDCALL 
1621 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1622 {
1623         if (pt->x != 5 || pt->y != 6.75)
1624                 return 1;
1625
1626         pt->x = 10;
1627         pt->y = 12.35;
1628
1629         return 0;
1630 }
1631
1632 LIBTEST_API int STDCALL  
1633 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1634 {
1635     int res = 1;
1636     if (*b1 != 0 && *b1 != 1)
1637         return 1;
1638     if (*b2 != 0 && *b2 != -1) /* variant_bool */
1639         return 1;
1640     if (*b3 != 0 && *b3 != 1)
1641         return 1;
1642     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1643         res = 0;
1644     *b1 = !*b1;
1645     *b2 = ~*b2;
1646     *b3 = !*b3;
1647     return res;
1648 }
1649
1650 struct BoolStruct
1651 {
1652     int i;
1653     char b1;
1654     short b2; /* variant_bool */
1655     int b3;
1656 };
1657
1658 LIBTEST_API int STDCALL  
1659 marshal_test_bool_struct(struct BoolStruct *s)
1660 {
1661     int res = 1;
1662     if (s->b1 != 0 && s->b1 != 1)
1663         return 1;
1664     if (s->b2 != 0 && s->b2 != -1)
1665         return 1;
1666     if (s->b3 != 0 && s->b3 != 1)
1667         return 1;
1668     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1669         res = 0;
1670     s->b1 = !s->b1;
1671     s->b2 = ~s->b2;
1672     s->b3 = !s->b3;
1673     return res;
1674 }
1675
1676 typedef struct {
1677         gint64 l;
1678 } LongStruct2;
1679
1680 typedef struct {
1681         int i;
1682         LongStruct2 l;
1683 } LongStruct;
1684
1685 LIBTEST_API int STDCALL
1686 mono_test_marshal_long_struct (LongStruct *s)
1687 {
1688         return s->i + s->l.l;
1689 }
1690
1691 LIBTEST_API void STDCALL
1692 mono_test_last_error (int err)
1693 {
1694 #ifdef WIN32
1695         SetLastError (err);
1696 #else
1697         errno = err;
1698 #endif
1699 }
1700
1701 LIBTEST_API int STDCALL 
1702 mono_test_asany (void *ptr, int what)
1703 {
1704         switch (what) {
1705         case 1:
1706                 return (*(int*)ptr == 5) ? 0 : 1;
1707         case 2:
1708                 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1709         case 3: {
1710                 simplestruct2 ss = *(simplestruct2*)ptr;
1711
1712                 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1713             !strcmp (ss.d, "TEST") && 
1714             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1715                         return 0;
1716                 else
1717                         return 1;
1718         }
1719         case 4: {
1720                 GError *error = NULL;
1721                 char *s;
1722
1723                 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1724
1725                 if (!s)
1726                         return 1;
1727
1728                 if (!strcmp (s, "ABC")) {
1729                         g_free (s);
1730                         return 0;
1731                 }
1732                 else {
1733                         g_free (s);
1734                         return 1;
1735                 }
1736         }
1737         default:
1738                 g_assert_not_reached ();
1739         }
1740
1741         return 1;
1742 }
1743
1744 typedef struct
1745 {
1746         int i;
1747         int j;
1748         int k;
1749         char *s;
1750 } AsAnyStruct;
1751
1752 LIBTEST_API int STDCALL 
1753 mono_test_marshal_asany_in (void* ptr)
1754 {
1755         AsAnyStruct* asAny = ptr;
1756         int res = asAny->i + asAny->j + asAny->k;
1757
1758         return res;
1759 }
1760
1761 LIBTEST_API int STDCALL 
1762 mono_test_marshal_asany_inout (void* ptr)
1763 {
1764         AsAnyStruct* asAny = ptr;
1765         int res = asAny->i + asAny->j + asAny->k;
1766
1767         marshal_free (asAny->s);
1768
1769         asAny->i = 10;
1770         asAny->j = 20;
1771         asAny->k = 30;
1772         asAny->s = 0;
1773
1774         return res;
1775 }
1776
1777 LIBTEST_API int STDCALL 
1778 mono_test_marshal_asany_out (void* ptr)
1779 {
1780         AsAnyStruct* asAny = ptr;
1781         int res = asAny->i + asAny->j + asAny->k;
1782
1783         asAny->i = 10;
1784         asAny->j = 20;
1785         asAny->k = 30;
1786         asAny->s = 0;
1787
1788         return res;
1789 }
1790
1791 /*
1792  * AMD64 marshalling tests.
1793  */
1794
1795 typedef struct amd64_struct1 {
1796         int i;
1797         int j;
1798         int k;
1799         int l;
1800 } amd64_struct1;
1801
1802 LIBTEST_API amd64_struct1 STDCALL 
1803 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1804 {
1805         s.i ++;
1806         s.j ++;
1807         s.k ++;
1808         s.l ++;
1809
1810         return s;
1811 }
1812
1813 LIBTEST_API amd64_struct1 STDCALL 
1814 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)
1815 {
1816         s.i ++;
1817         s.j ++;
1818         s.k ++;
1819         s.l += 1 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
1820
1821         return s;
1822 }
1823
1824 typedef struct amd64_struct2 {
1825         int i;
1826         int j;
1827 } amd64_struct2;
1828
1829 LIBTEST_API amd64_struct2 STDCALL 
1830 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1831 {
1832         s.i ++;
1833         s.j ++;
1834
1835         return s;
1836 }
1837
1838 typedef struct amd64_struct3 {
1839         int i;
1840 } amd64_struct3;
1841
1842 LIBTEST_API amd64_struct3 STDCALL 
1843 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1844 {
1845         s.i ++;
1846
1847         return s;
1848 }
1849
1850 typedef struct amd64_struct4 {
1851         double d1, d2;
1852 } amd64_struct4;
1853
1854 LIBTEST_API amd64_struct4 STDCALL 
1855 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1856 {
1857         s.d1 ++;
1858         s.d2 ++;
1859
1860         return s;
1861 }
1862
1863 /*
1864  * IA64 marshalling tests.
1865  */
1866 typedef struct test_struct5 {
1867         float d1, d2;
1868 } test_struct5;
1869
1870 LIBTEST_API test_struct5 STDCALL 
1871 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, int i, double d3, double d4)
1872 {
1873         s.d1 += d1 + d2 + i;
1874         s.d2 += d3 + d4 + i;
1875
1876         return s;
1877 }
1878
1879 typedef struct test_struct6 {
1880         double d1, d2;
1881 } test_struct6;
1882
1883 LIBTEST_API test_struct6 STDCALL 
1884 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, int i, double d3, double d4)
1885 {
1886         s.d1 += d1 + d2 + i;
1887         s.d2 += d3 + d4;
1888
1889         return s;
1890 }
1891
1892 static guint32 custom_res [2];
1893
1894 LIBTEST_API void* STDCALL
1895 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1896 {
1897         /* ptr will be freed by CleanupNative, so make a copy */
1898         custom_res [0] = 0; /* not allocated by AllocHGlobal */
1899         custom_res [1] = ptr [1];
1900
1901         return &custom_res;
1902 }
1903
1904 LIBTEST_API int STDCALL 
1905 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1906 {
1907         custom_res [0] = 0;
1908         custom_res [1] = i + j + 10;
1909
1910         *ptr = custom_res;
1911
1912         return 0;
1913 }
1914
1915 LIBTEST_API int STDCALL 
1916 mono_test_marshal_pass_inout_custom (int i, guint32 *ptr, int j)
1917 {
1918         ptr [0] = 0;
1919         ptr [1] = i + ptr [1] + j;
1920
1921         return 0;
1922 }
1923
1924 LIBTEST_API int STDCALL 
1925 mono_test_marshal_pass_out_byval_custom (int i, guint32 *ptr, int j)
1926 {
1927         return ptr == NULL ? 0 : 1;
1928 }
1929
1930 LIBTEST_API int STDCALL 
1931 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1932 {
1933         (*ptr)[1] += i + j;
1934
1935         return 0;
1936 }
1937
1938 LIBTEST_API void* STDCALL
1939 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1940 {
1941         g_assert_not_reached ();
1942
1943         return NULL;
1944 }
1945
1946 LIBTEST_API void* STDCALL
1947 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1948 {
1949         g_assert (ptr == NULL);
1950
1951         return NULL;
1952 }
1953
1954 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1955
1956 LIBTEST_API int STDCALL 
1957 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1958 {
1959         guint32 buf [2];
1960         guint32 res;
1961         guint32 *ptr;
1962
1963         buf [0] = 0;
1964         buf [1] = 10;
1965
1966         ptr = del (&buf);
1967
1968         res = ptr [1];
1969
1970 #ifdef WIN32
1971         /* FIXME: Freed with FreeHGlobal */
1972 #else
1973         g_free (ptr);
1974 #endif
1975
1976         return res;
1977 }
1978
1979 LIBTEST_API int STDCALL 
1980 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1981 {
1982         void *ptr = del (NULL);
1983
1984         return (ptr == NULL) ? 15 : 0;
1985 }
1986
1987 typedef void (STDCALL *CustomOutParamDelegate) (void **pptr);
1988
1989 LIBTEST_API int STDCALL 
1990 mono_test_marshal_custom_out_param_delegate (CustomOutParamDelegate del)
1991 {
1992         void* pptr = del;
1993
1994         del (&pptr);
1995
1996         if(pptr != NULL)
1997                 return 1;
1998
1999         return 0;
2000 }
2001
2002 typedef int (STDCALL *ReturnEnumDelegate) (int e);
2003
2004 LIBTEST_API int STDCALL 
2005 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
2006 {
2007         return func (1);
2008 }
2009
2010 typedef struct {
2011         int a, b, c;
2012         gint64 d;
2013 } BlittableStruct;
2014         
2015 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
2016
2017 LIBTEST_API int STDCALL 
2018 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
2019 {
2020         BlittableStruct ss, res;
2021
2022         ss.a = 1;
2023         ss.b = 2;
2024         ss.c = 3;
2025         ss.d = 55;
2026
2027         res = delegate (ss);
2028         if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
2029                 return 1;
2030
2031         return 0;
2032 }
2033
2034 LIBTEST_API int STDCALL 
2035 mono_test_stdcall_name_mangling (int a, int b, int c)
2036 {
2037         return a + b + c;
2038 }
2039
2040 LIBTEST_API int
2041 mono_test_stdcall_mismatch_1 (int a, int b, int c)
2042 {
2043         return a + b + c;
2044 }
2045
2046 LIBTEST_API int STDCALL
2047 mono_test_stdcall_mismatch_2 (int a, int b, int c)
2048 {
2049         return a + b + c;
2050 }
2051
2052 /*
2053  * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
2054  */
2055
2056 typedef struct {
2057         int i;
2058 } SmallStruct1;
2059         
2060 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
2061
2062 LIBTEST_API int STDCALL 
2063 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
2064 {
2065         SmallStruct1 ss, res;
2066
2067         ss.i = 1;
2068
2069         res = delegate (ss);
2070         if (! (res.i == -1))
2071                 return 1;
2072
2073         return 0;
2074 }
2075
2076 typedef struct {
2077         gint16 i, j;
2078 } SmallStruct2;
2079         
2080 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
2081
2082 LIBTEST_API int STDCALL 
2083 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
2084 {
2085         SmallStruct2 ss, res;
2086
2087         ss.i = 2;
2088         ss.j = 3;
2089
2090         res = delegate (ss);
2091         if (! ((res.i == -2) && (res.j == -3)))
2092                 return 1;
2093
2094         return 0;
2095 }
2096
2097 typedef struct {
2098         gint16 i;
2099         gint8 j;
2100 } SmallStruct3;
2101         
2102 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
2103
2104 LIBTEST_API int STDCALL 
2105 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
2106 {
2107         SmallStruct3 ss, res;
2108
2109         ss.i = 1;
2110         ss.j = 2;
2111
2112         res = delegate (ss);
2113         if (! ((res.i == -1) && (res.j == -2)))
2114                 return 1;
2115
2116         return 0;
2117 }
2118
2119 typedef struct {
2120         gint16 i;
2121 } SmallStruct4;
2122         
2123 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
2124
2125 LIBTEST_API int STDCALL 
2126 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
2127 {
2128         SmallStruct4 ss, res;
2129
2130         ss.i = 1;
2131
2132         res = delegate (ss);
2133         if (! (res.i == -1))
2134                 return 1;
2135
2136         return 0;
2137 }
2138
2139 typedef struct {
2140         gint64 i;
2141 } SmallStruct5;
2142         
2143 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
2144
2145 LIBTEST_API int STDCALL 
2146 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
2147 {
2148         SmallStruct5 ss, res;
2149
2150         ss.i = 5;
2151
2152         res = delegate (ss);
2153         if (! (res.i == -5))
2154                 return 1;
2155
2156         return 0;
2157 }
2158
2159 typedef struct {
2160         int i, j;
2161 } SmallStruct6;
2162         
2163 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
2164
2165 LIBTEST_API int STDCALL 
2166 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
2167 {
2168         SmallStruct6 ss, res;
2169
2170         ss.i = 1;
2171         ss.j = 2;
2172
2173         res = delegate (ss);
2174         if (! ((res.i == -1) && (res.j == -2)))
2175                 return 1;
2176
2177         return 0;
2178 }
2179
2180 typedef struct {
2181         int i;
2182         gint16 j;
2183 } SmallStruct7;
2184         
2185 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
2186
2187 LIBTEST_API int STDCALL 
2188 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
2189 {
2190         SmallStruct7 ss, res;
2191
2192         ss.i = 1;
2193         ss.j = 2;
2194
2195         res = delegate (ss);
2196         if (! ((res.i == -1) && (res.j == -2)))
2197                 return 1;
2198
2199         return 0;
2200 }
2201
2202 typedef struct {
2203         float i;
2204 } SmallStruct8;
2205         
2206 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
2207
2208 LIBTEST_API int STDCALL 
2209 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
2210 {
2211         SmallStruct8 ss, res;
2212
2213         ss.i = 1.0;
2214
2215         res = delegate (ss);
2216         if (! ((res.i == -1.0)))
2217                 return 1;
2218
2219         return 0;
2220 }
2221
2222 typedef struct {
2223         double i;
2224 } SmallStruct9;
2225         
2226 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
2227
2228 LIBTEST_API int STDCALL 
2229 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
2230 {
2231         SmallStruct9 ss, res;
2232
2233         ss.i = 1.0;
2234
2235         res = delegate (ss);
2236         if (! ((res.i == -1.0)))
2237                 return 1;
2238
2239         return 0;
2240 }
2241
2242 typedef struct {
2243         float i, j;
2244 } SmallStruct10;
2245         
2246 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
2247
2248 LIBTEST_API int STDCALL 
2249 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
2250 {
2251         SmallStruct10 ss, res;
2252
2253         ss.i = 1.0;
2254         ss.j = 2.0;
2255
2256         res = delegate (ss);
2257         if (! ((res.i == -1.0) && (res.j == -2.0)))
2258                 return 1;
2259
2260         return 0;
2261 }
2262
2263 typedef struct {
2264         float i;
2265         int j;
2266 } SmallStruct11;
2267         
2268 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
2269
2270 LIBTEST_API int STDCALL 
2271 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
2272 {
2273         SmallStruct11 ss, res;
2274
2275         ss.i = 1.0;
2276         ss.j = 2;
2277
2278         res = delegate (ss);
2279         if (! ((res.i == -1.0) && (res.j == -2)))
2280                 return 1;
2281
2282         return 0;
2283 }
2284
2285 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
2286
2287 LIBTEST_API int STDCALL 
2288 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
2289 {
2290         return del (len, NULL, arr);
2291 }
2292
2293 typedef int (STDCALL *ArrayDelegateLong) (gint64 i, char *j, void *arr);
2294
2295 LIBTEST_API int STDCALL 
2296 mono_test_marshal_array_delegate_long (void *arr, gint64 len, ArrayDelegateLong del)
2297 {
2298         return del (len, NULL, arr);
2299 }
2300
2301 LIBTEST_API int STDCALL 
2302 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
2303 {
2304         del (len, NULL, arr);
2305
2306         if ((arr [0] != 1) || (arr [1] != 2))
2307                 return 1;
2308         else
2309                 return 0;
2310 }
2311
2312 typedef gunichar2* (STDCALL *UnicodeStringDelegate) (gunichar2 *message);
2313
2314 LIBTEST_API int STDCALL 
2315 mono_test_marshal_return_unicode_string_delegate (UnicodeStringDelegate del)
2316 {
2317         const char m[] = "abcdef";
2318         gunichar2 *s2, *res;
2319         glong len;
2320
2321         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
2322
2323         res = del (s2);
2324
2325         marshal_free (res);
2326
2327         return 0;
2328 }
2329
2330 LIBTEST_API int STDCALL 
2331 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
2332 {
2333         del (len, NULL, arr);
2334
2335         if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
2336                 return 0;
2337         else
2338                 return 1;
2339 }
2340
2341 typedef int (*CdeclDelegate) (int i, int j);
2342
2343 LIBTEST_API int STDCALL 
2344 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
2345 {
2346         int i;
2347
2348         for (i = 0; i < 1000; ++i)
2349                 del (1, 2);
2350
2351         return 0;
2352 }
2353
2354 typedef char** (STDCALL *ReturnStringArrayDelegate) (int i);
2355
2356 LIBTEST_API int STDCALL 
2357 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
2358 {
2359         char **arr = d (2);
2360         int res;
2361
2362         if (arr == NULL)
2363                 return 3;
2364
2365         if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
2366                 res = 1;
2367         else
2368                 res = 0;
2369
2370         marshal_free (arr);
2371
2372         return res;
2373 }
2374
2375 typedef int (STDCALL *ByrefStringDelegate) (char **s);
2376
2377 LIBTEST_API int STDCALL 
2378 mono_test_marshal_byref_string_delegate (ByrefStringDelegate d)
2379 {
2380         char *s = (char*)"ABC";
2381         int res;
2382
2383         res = d (&s);
2384         if (res != 0)
2385                 return res;
2386
2387         if (!strcmp (s, "DEF"))
2388                 res = 0;
2389         else
2390                 res = 2;
2391
2392         marshal_free (s);
2393
2394         return res;
2395 }
2396
2397 LIBTEST_API int STDCALL 
2398 add_delegate (int i, int j)
2399 {
2400         return i + j;
2401 }
2402
2403 LIBTEST_API gpointer STDCALL 
2404 mono_test_marshal_return_fnptr (void)
2405 {
2406         return &add_delegate;
2407 }
2408
2409 LIBTEST_API int STDCALL 
2410 mono_xr (int code)
2411 {
2412         printf ("codigo %x\n", code);
2413         return code + 1234;
2414 }
2415
2416 typedef struct {
2417         int handle;
2418 } HandleRef;
2419
2420 LIBTEST_API HandleRef STDCALL 
2421 mono_xr_as_handle (int code)
2422 {
2423         HandleRef ref;
2424
2425         memset (&ref, 0, sizeof (ref));
2426
2427         return ref;
2428 }
2429  
2430 typedef struct {
2431         int   a;
2432         void *handle1;
2433         void *handle2;
2434         int   b;
2435 } HandleStructs;
2436
2437 LIBTEST_API int STDCALL 
2438 mono_safe_handle_struct_ref (HandleStructs *x)
2439 {
2440         printf ("Dingus Ref! \n");
2441         printf ("Values: %d %d %p %p\n", x->a, x->b, x->handle1, x->handle2);
2442         if (x->a != 1234)
2443                 return 1;
2444         if (x->b != 8743)
2445                 return 2;
2446
2447         if (x->handle1 != (void*) 0x7080feed)
2448                 return 3;
2449
2450         if (x->handle2 != (void*) 0x1234abcd)
2451                 return 4;
2452
2453         return 0xf00d;
2454 }
2455
2456 LIBTEST_API int STDCALL 
2457 mono_safe_handle_struct (HandleStructs x)
2458 {
2459         printf ("Dingus Standard! \n");
2460         printf ("Values: %d %d %p %p\n", x.a, x.b, x.handle1, x.handle2);
2461         if (x.a != 1234)
2462                 return 1;
2463         if (x.b != 8743)
2464                 return 2;
2465
2466         if (x.handle1 != (void*) 0x7080feed)
2467                 return 3;
2468
2469         if (x.handle2 != (void*) 0x1234abcd)
2470                 return 4;
2471         
2472         return 0xf00f;
2473 }
2474
2475 typedef struct {
2476         void *a;
2477 } TrivialHandle;
2478
2479 LIBTEST_API int STDCALL 
2480 mono_safe_handle_struct_simple (TrivialHandle x)
2481 {
2482         printf ("The value is %p\n", x.a);
2483         return ((int)(gsize)x.a) * 2;
2484 }
2485
2486 LIBTEST_API int STDCALL 
2487 mono_safe_handle_return (void)
2488 {
2489         return 0x1000f00d;
2490 }
2491
2492 LIBTEST_API void STDCALL
2493 mono_safe_handle_ref (void **handle)
2494 {
2495         if (*handle != 0){
2496                 *handle = (void *) 0xbad;
2497                 return;
2498         }
2499
2500         *handle = (void *) 0x800d;
2501 }
2502
2503 LIBTEST_API double STDCALL
2504 mono_test_marshal_date_time (double d, double *d2)
2505 {
2506         *d2 = d;
2507         return d;
2508 }
2509
2510 /*
2511  * COM INTEROP TESTS
2512  */
2513
2514 #ifndef WIN32
2515
2516 typedef struct {
2517         guint16 vt;
2518         guint16 wReserved1;
2519         guint16 wReserved2;
2520         guint16 wReserved3;
2521         union {
2522                 gint64 llVal;
2523                 gint32 lVal;
2524                 guint8  bVal;
2525                 gint16 iVal;
2526                 float  fltVal;
2527                 double dblVal;
2528                 gint16 boolVal;
2529                 gunichar2* bstrVal;
2530                 gint8 cVal;
2531                 guint16 uiVal;
2532                 guint32 ulVal;
2533                 guint64 ullVal;
2534                 gpointer byref;
2535                 struct {
2536                         gpointer pvRecord;
2537                         gpointer pRecInfo;
2538                 };
2539         };
2540 } VARIANT;
2541
2542 typedef enum {
2543         VARIANT_TRUE = -1,
2544         VARIANT_FALSE = 0
2545 } VariantBool;
2546
2547 typedef enum {
2548         VT_EMPTY = 0,
2549         VT_NULL = 1,
2550         VT_I2 = 2,
2551         VT_I4 = 3,
2552         VT_R4 = 4,
2553         VT_R8 = 5,
2554         VT_CY = 6,
2555         VT_DATE = 7,
2556         VT_BSTR = 8,
2557         VT_DISPATCH = 9,
2558         VT_ERROR = 10,
2559         VT_BOOL = 11,
2560         VT_VARIANT = 12,
2561         VT_UNKNOWN = 13,
2562         VT_DECIMAL = 14,
2563         VT_I1 = 16,
2564         VT_UI1 = 17,
2565         VT_UI2 = 18,
2566         VT_UI4 = 19,
2567         VT_I8 = 20,
2568         VT_UI8 = 21,
2569         VT_INT = 22,
2570         VT_UINT = 23,
2571         VT_VOID = 24,
2572         VT_HRESULT = 25,
2573         VT_PTR = 26,
2574         VT_SAFEARRAY = 27,
2575         VT_CARRAY = 28,
2576         VT_USERDEFINED = 29,
2577         VT_LPSTR = 30,
2578         VT_LPWSTR = 31,
2579         VT_RECORD = 36,
2580         VT_FILETIME = 64,
2581         VT_BLOB = 65,
2582         VT_STREAM = 66,
2583         VT_STORAGE = 67,
2584         VT_STREAMED_OBJECT = 68,
2585         VT_STORED_OBJECT = 69,
2586         VT_BLOB_OBJECT = 70,
2587         VT_CF = 71,
2588         VT_CLSID = 72,
2589         VT_VECTOR = 4096,
2590         VT_ARRAY = 8192,
2591         VT_BYREF = 16384
2592 } VarEnum;
2593
2594 void VariantInit(VARIANT* vt)
2595 {
2596         vt->vt = VT_EMPTY;
2597 }
2598
2599 typedef struct
2600 {
2601         guint32 a;
2602         guint16 b;
2603         guint16 c;
2604         guint8 d[8];
2605 } GUID;
2606
2607 #define S_OK 0
2608
2609 #endif
2610
2611 LIBTEST_API int STDCALL 
2612 mono_test_marshal_bstr_in(gunichar2* bstr)
2613 {
2614         gint32 result = 0;
2615         gchar* bstr_utf8 = g_utf16_to_utf8 (bstr, -1, NULL, NULL, NULL);
2616         result = strcmp("mono_test_marshal_bstr_in", bstr_utf8);
2617         g_free(bstr_utf8);
2618         if (result == 0)
2619                 return 0;
2620         return 1;
2621 }
2622
2623 LIBTEST_API int STDCALL 
2624 mono_test_marshal_bstr_out(gunichar2** bstr)
2625 {
2626         *bstr = marshal_bstr_alloc ("mono_test_marshal_bstr_out");
2627         return 0;
2628 }
2629
2630 LIBTEST_API int STDCALL 
2631 mono_test_marshal_bstr_in_null(gunichar2* bstr)
2632 {
2633         if (!bstr)
2634                 return 0;
2635         return 1;
2636 }
2637
2638 LIBTEST_API int STDCALL 
2639 mono_test_marshal_bstr_out_null(gunichar2** bstr)
2640 {
2641         *bstr = NULL;
2642         return 0;
2643 }
2644
2645 LIBTEST_API int STDCALL 
2646 mono_test_marshal_variant_in_sbyte(VARIANT variant)
2647 {
2648         if (variant.vt == VT_I1 && variant.cVal == 100)
2649                 return 0;
2650         return 1;
2651 }
2652
2653 LIBTEST_API int STDCALL 
2654 mono_test_marshal_variant_in_byte(VARIANT variant)
2655 {
2656         if (variant.vt == VT_UI1 && variant.bVal == 100)
2657                 return 0;
2658         return 1;
2659 }
2660
2661 LIBTEST_API int STDCALL 
2662 mono_test_marshal_variant_in_short(VARIANT variant)
2663 {
2664         if (variant.vt == VT_I2 && variant.iVal == 314)
2665                 return 0;
2666         return 1;
2667 }
2668
2669 LIBTEST_API int STDCALL 
2670 mono_test_marshal_variant_in_ushort(VARIANT variant)
2671 {
2672         if (variant.vt == VT_UI2 && variant.uiVal == 314)
2673                 return 0;
2674         return 1;
2675 }
2676
2677 LIBTEST_API int STDCALL 
2678 mono_test_marshal_variant_in_int(VARIANT variant)
2679 {
2680         if (variant.vt == VT_I4 && variant.lVal == 314)
2681                 return 0;
2682         return 1;
2683 }
2684
2685 LIBTEST_API int STDCALL 
2686 mono_test_marshal_variant_in_uint(VARIANT variant)
2687 {
2688         if (variant.vt == VT_UI4 && variant.ulVal == 314)
2689                 return 0;
2690         return 1;
2691 }
2692
2693 LIBTEST_API int STDCALL 
2694 mono_test_marshal_variant_in_long(VARIANT variant)
2695 {
2696         if (variant.vt == VT_I8 && variant.llVal == 314)
2697                 return 0;
2698         return 1;
2699 }
2700
2701 LIBTEST_API int STDCALL 
2702 mono_test_marshal_variant_in_ulong(VARIANT variant)
2703 {
2704         if (variant.vt == VT_UI8 && variant.ullVal == 314)
2705                 return 0;
2706         return 1;
2707 }
2708
2709 LIBTEST_API int STDCALL 
2710 mono_test_marshal_variant_in_float(VARIANT variant)
2711 {
2712         if (variant.vt == VT_R4 && (variant.fltVal - 3.14)/3.14 < .001)
2713                 return 0;
2714         return 1;
2715 }
2716
2717 LIBTEST_API int STDCALL 
2718 mono_test_marshal_variant_in_double(VARIANT variant)
2719 {
2720         if (variant.vt == VT_R8 && (variant.dblVal - 3.14)/3.14 < .001)
2721                 return 0;
2722         return 1;
2723 }
2724
2725 LIBTEST_API int STDCALL 
2726 mono_test_marshal_variant_in_bstr(VARIANT variant)
2727 {
2728         gint32 result = 0;
2729         gchar* bstr_utf8 = g_utf16_to_utf8 (variant.bstrVal, -1, NULL, NULL, NULL);
2730         result = strcmp("PI", bstr_utf8);
2731         g_free(bstr_utf8);
2732
2733         if (variant.vt == VT_BSTR && !result)
2734                 return 0;
2735         return 1;
2736 }
2737
2738 LIBTEST_API int STDCALL 
2739 mono_test_marshal_variant_in_bool_true (VARIANT variant)
2740 {
2741         if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_TRUE)
2742                 return 0;
2743         return 1;
2744 }
2745
2746 LIBTEST_API int STDCALL 
2747 mono_test_marshal_variant_in_bool_false (VARIANT variant)
2748 {
2749         if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_FALSE)
2750                 return 0;
2751         return 1;
2752 }
2753
2754 LIBTEST_API int STDCALL 
2755 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
2756 {
2757         variant->vt = VT_I1;
2758         variant->cVal = 100;
2759
2760         return 0;
2761 }
2762
2763 LIBTEST_API int STDCALL 
2764 mono_test_marshal_variant_out_sbyte_byref(VARIANT* variant)
2765 {
2766         variant->vt = VT_I1|VT_BYREF;
2767         variant->byref = marshal_alloc(1);
2768         *((gint8*)variant->byref) = 100;
2769
2770         return 0;
2771 }
2772
2773 LIBTEST_API int STDCALL 
2774 mono_test_marshal_variant_out_byte(VARIANT* variant)
2775 {       
2776         variant->vt = VT_UI1;
2777         variant->bVal = 100;
2778
2779         return 0;
2780 }
2781
2782 LIBTEST_API int STDCALL 
2783 mono_test_marshal_variant_out_byte_byref(VARIANT* variant)
2784 {       
2785         variant->vt = VT_UI1|VT_BYREF;
2786         variant->byref = marshal_alloc(1);
2787         *((gint8*)variant->byref) = 100;
2788
2789         return 0;
2790 }
2791
2792 LIBTEST_API int STDCALL 
2793 mono_test_marshal_variant_out_short(VARIANT* variant)
2794 {
2795         variant->vt = VT_I2;
2796         variant->iVal = 314;
2797
2798         return 0;
2799 }
2800
2801 LIBTEST_API int STDCALL 
2802 mono_test_marshal_variant_out_short_byref(VARIANT* variant)
2803 {
2804         variant->vt = VT_I2|VT_BYREF;
2805         variant->byref = marshal_alloc(2);
2806         *((gint16*)variant->byref) = 314;
2807
2808         return 0;
2809 }
2810
2811 LIBTEST_API int STDCALL 
2812 mono_test_marshal_variant_out_ushort(VARIANT* variant)
2813 {
2814         variant->vt = VT_UI2;
2815         variant->uiVal = 314;
2816
2817         return 0;
2818 }
2819
2820 LIBTEST_API int STDCALL 
2821 mono_test_marshal_variant_out_ushort_byref(VARIANT* variant)
2822 {
2823         variant->vt = VT_UI2|VT_BYREF;
2824         variant->byref = marshal_alloc(2);
2825         *((guint16*)variant->byref) = 314;
2826
2827         return 0;
2828 }
2829
2830 LIBTEST_API int STDCALL 
2831 mono_test_marshal_variant_out_int(VARIANT* variant)
2832 {
2833         variant->vt = VT_I4;
2834         variant->lVal = 314;
2835
2836         return 0;
2837 }
2838
2839 LIBTEST_API int STDCALL 
2840 mono_test_marshal_variant_out_int_byref(VARIANT* variant)
2841 {
2842         variant->vt = VT_I4|VT_BYREF;
2843         variant->byref = marshal_alloc(4);
2844         *((gint32*)variant->byref) = 314;
2845
2846         return 0;
2847 }
2848
2849 LIBTEST_API int STDCALL 
2850 mono_test_marshal_variant_out_uint(VARIANT* variant)
2851 {
2852         variant->vt = VT_UI4;
2853         variant->ulVal = 314;
2854
2855         return 0;
2856 }
2857
2858 LIBTEST_API int STDCALL 
2859 mono_test_marshal_variant_out_uint_byref(VARIANT* variant)
2860 {
2861         variant->vt = VT_UI4|VT_BYREF;
2862         variant->byref = marshal_alloc(4);
2863         *((guint32*)variant->byref) = 314;
2864
2865         return 0;
2866 }
2867
2868 LIBTEST_API int STDCALL 
2869 mono_test_marshal_variant_out_long(VARIANT* variant)
2870 {
2871         variant->vt = VT_I8;
2872         variant->llVal = 314;
2873
2874         return 0;
2875 }
2876
2877 LIBTEST_API int STDCALL 
2878 mono_test_marshal_variant_out_long_byref(VARIANT* variant)
2879 {
2880         variant->vt = VT_I8|VT_BYREF;
2881         variant->byref = marshal_alloc(8);
2882         *((gint64*)variant->byref) = 314;
2883
2884         return 0;
2885 }
2886
2887 LIBTEST_API int STDCALL 
2888 mono_test_marshal_variant_out_ulong(VARIANT* variant)
2889 {
2890         variant->vt = VT_UI8;
2891         variant->ullVal = 314;
2892
2893         return 0;
2894 }
2895
2896 LIBTEST_API int STDCALL 
2897 mono_test_marshal_variant_out_ulong_byref(VARIANT* variant)
2898 {
2899         variant->vt = VT_UI8|VT_BYREF;
2900         variant->byref = marshal_alloc(8);
2901         *((guint64*)variant->byref) = 314;
2902
2903         return 0;
2904 }
2905
2906 LIBTEST_API int STDCALL 
2907 mono_test_marshal_variant_out_float(VARIANT* variant)
2908 {
2909         variant->vt = VT_R4;
2910         variant->fltVal = 3.14;
2911
2912         return 0;
2913 }
2914
2915 LIBTEST_API int STDCALL 
2916 mono_test_marshal_variant_out_float_byref(VARIANT* variant)
2917 {
2918         variant->vt = VT_R4|VT_BYREF;
2919         variant->byref = marshal_alloc(4);
2920         *((float*)variant->byref) = 3.14;
2921
2922         return 0;
2923 }
2924
2925 LIBTEST_API int STDCALL 
2926 mono_test_marshal_variant_out_double(VARIANT* variant)
2927 {
2928         variant->vt = VT_R8;
2929         variant->dblVal = 3.14;
2930
2931         return 0;
2932 }
2933
2934 LIBTEST_API int STDCALL 
2935 mono_test_marshal_variant_out_double_byref(VARIANT* variant)
2936 {
2937         variant->vt = VT_R8|VT_BYREF;
2938         variant->byref = marshal_alloc(8);
2939         *((double*)variant->byref) = 3.14;
2940
2941         return 0;
2942 }
2943
2944 LIBTEST_API int STDCALL 
2945 mono_test_marshal_variant_out_bstr(VARIANT* variant)
2946 {
2947         variant->vt = VT_BSTR;
2948         variant->bstrVal = marshal_bstr_alloc("PI");
2949
2950         return 0;
2951 }
2952
2953 LIBTEST_API int STDCALL 
2954 mono_test_marshal_variant_out_bstr_byref(VARIANT* variant)
2955 {
2956         variant->vt = VT_BSTR|VT_BYREF;
2957         variant->byref = marshal_alloc(sizeof(gpointer));
2958         *((gunichar**)variant->byref) = (gunichar*)marshal_bstr_alloc("PI");
2959
2960         return 0;
2961 }
2962
2963 LIBTEST_API int STDCALL 
2964 mono_test_marshal_variant_out_bool_true (VARIANT* variant)
2965 {
2966         variant->vt = VT_BOOL;
2967         variant->boolVal = VARIANT_TRUE;
2968
2969         return 0;
2970 }
2971
2972 LIBTEST_API int STDCALL 
2973 mono_test_marshal_variant_out_bool_true_byref (VARIANT* variant)
2974 {
2975         variant->vt = VT_BOOL|VT_BYREF;
2976         variant->byref = marshal_alloc(2);
2977         *((gint16*)variant->byref) = VARIANT_TRUE;
2978
2979         return 0;
2980 }
2981
2982 LIBTEST_API int STDCALL 
2983 mono_test_marshal_variant_out_bool_false (VARIANT* variant)
2984 {
2985         variant->vt = VT_BOOL;
2986         variant->boolVal = VARIANT_FALSE;
2987
2988         return 0;
2989 }
2990
2991 LIBTEST_API int STDCALL 
2992 mono_test_marshal_variant_out_bool_false_byref (VARIANT* variant)
2993 {
2994         variant->vt = VT_BOOL|VT_BYREF;
2995         variant->byref = marshal_alloc(2);
2996         *((gint16*)variant->byref) = VARIANT_FALSE;
2997
2998         return 0;
2999 }
3000
3001 typedef int (STDCALL *VarFunc) (int vt, VARIANT variant);
3002 typedef int (STDCALL *VarRefFunc) (int vt, VARIANT* variant);
3003
3004 LIBTEST_API int STDCALL 
3005 mono_test_marshal_variant_in_sbyte_unmanaged(VarFunc func)
3006 {
3007         VARIANT vt;
3008         vt.vt = VT_I1;
3009         vt.cVal = -100;
3010         return func (VT_I1, vt);
3011 }
3012
3013 LIBTEST_API int STDCALL 
3014 mono_test_marshal_variant_in_byte_unmanaged(VarFunc func)
3015 {
3016         VARIANT vt;
3017         vt.vt = VT_UI1;
3018         vt.bVal = 100;
3019         return func (VT_UI1, vt);
3020 }
3021
3022 LIBTEST_API int STDCALL 
3023 mono_test_marshal_variant_in_short_unmanaged(VarFunc func)
3024 {
3025         VARIANT vt;
3026         vt.vt = VT_I2;
3027         vt.iVal = -100;
3028         return func (VT_I2, vt);
3029 }
3030
3031 LIBTEST_API int STDCALL 
3032 mono_test_marshal_variant_in_ushort_unmanaged(VarFunc func)
3033 {
3034         VARIANT vt;
3035         vt.vt = VT_UI2;
3036         vt.uiVal = 100;
3037         return func (VT_UI2, vt);
3038 }
3039
3040 LIBTEST_API int STDCALL 
3041 mono_test_marshal_variant_in_int_unmanaged(VarFunc func)
3042 {
3043         VARIANT vt;
3044         vt.vt = VT_I4;
3045         vt.lVal = -100;
3046         return func (VT_I4, vt);
3047 }
3048
3049 LIBTEST_API int STDCALL 
3050 mono_test_marshal_variant_in_uint_unmanaged(VarFunc func)
3051 {
3052         VARIANT vt;
3053         vt.vt = VT_UI4;
3054         vt.ulVal = 100;
3055         return func (VT_UI4, vt);
3056 }
3057
3058 LIBTEST_API int STDCALL 
3059 mono_test_marshal_variant_in_long_unmanaged(VarFunc func)
3060 {
3061         VARIANT vt;
3062         vt.vt = VT_I8;
3063         vt.llVal = -100;
3064         return func (VT_I8, vt);
3065 }
3066
3067 LIBTEST_API int STDCALL 
3068 mono_test_marshal_variant_in_ulong_unmanaged(VarFunc func)
3069 {
3070         VARIANT vt;
3071         vt.vt = VT_UI8;
3072         vt.ullVal = 100;
3073         return func (VT_UI8, vt);
3074 }
3075
3076 LIBTEST_API int STDCALL 
3077 mono_test_marshal_variant_in_float_unmanaged(VarFunc func)
3078 {
3079         VARIANT vt;
3080         vt.vt = VT_R4;
3081         vt.fltVal = 3.14;
3082         return func (VT_R4, vt);
3083 }
3084
3085 LIBTEST_API int STDCALL 
3086 mono_test_marshal_variant_in_double_unmanaged(VarFunc func)
3087 {
3088         VARIANT vt;
3089         vt.vt = VT_R8;
3090         vt.dblVal = 3.14;
3091         return func (VT_R8, vt);
3092 }
3093
3094 LIBTEST_API int STDCALL 
3095 mono_test_marshal_variant_in_bstr_unmanaged(VarFunc func)
3096 {
3097         VARIANT vt;
3098         vt.vt = VT_BSTR;
3099         vt.bstrVal = marshal_bstr_alloc("PI");
3100         return func (VT_BSTR, vt);
3101 }
3102
3103 LIBTEST_API int STDCALL 
3104 mono_test_marshal_variant_in_bool_true_unmanaged(VarFunc func)
3105 {
3106         VARIANT vt;
3107         vt.vt = VT_BOOL;
3108         vt.boolVal = VARIANT_TRUE;
3109         return func (VT_BOOL, vt);
3110 }
3111
3112 LIBTEST_API int STDCALL 
3113 mono_test_marshal_variant_in_bool_false_unmanaged(VarFunc func)
3114 {
3115         VARIANT vt;
3116         vt.vt = VT_BOOL;
3117         vt.boolVal = VARIANT_FALSE;
3118         return func (VT_BOOL, vt);
3119 }
3120
3121 LIBTEST_API int STDCALL 
3122 mono_test_marshal_variant_out_sbyte_unmanaged(VarRefFunc func)
3123 {
3124         VARIANT vt;
3125         VariantInit (&vt);
3126         func (VT_I1, &vt);
3127         if (vt.vt == VT_I1 && vt.cVal == -100)
3128                 return 0;
3129         return 1;
3130 }
3131
3132 LIBTEST_API int STDCALL 
3133 mono_test_marshal_variant_out_byte_unmanaged(VarRefFunc func)
3134 {
3135         VARIANT vt;
3136         VariantInit (&vt);
3137         func (VT_UI1, &vt);
3138         if (vt.vt == VT_UI1 && vt.bVal == 100)
3139                 return 0;
3140         return 1;
3141 }
3142
3143 LIBTEST_API int STDCALL 
3144 mono_test_marshal_variant_out_short_unmanaged(VarRefFunc func)
3145 {
3146         VARIANT vt;
3147         VariantInit (&vt);
3148         func (VT_I2, &vt);
3149         if (vt.vt == VT_I2 && vt.iVal == -100)
3150                 return 0;
3151         return 1;
3152 }
3153
3154 LIBTEST_API int STDCALL 
3155 mono_test_marshal_variant_out_ushort_unmanaged(VarRefFunc func)
3156 {
3157         VARIANT vt;
3158         VariantInit (&vt);
3159         func (VT_UI2, &vt);
3160         if (vt.vt == VT_UI2 && vt.uiVal == 100)
3161                 return 0;
3162         return 1;
3163 }
3164
3165 LIBTEST_API int STDCALL 
3166 mono_test_marshal_variant_out_int_unmanaged(VarRefFunc func)
3167 {
3168         VARIANT vt;
3169         VariantInit (&vt);
3170         func (VT_I4, &vt);
3171         if (vt.vt == VT_I4 && vt.lVal == -100)
3172                 return 0;
3173         return 1;
3174 }
3175
3176 LIBTEST_API int STDCALL 
3177 mono_test_marshal_variant_out_uint_unmanaged(VarRefFunc func)
3178 {
3179         VARIANT vt;
3180         VariantInit (&vt);
3181         func (VT_UI4, &vt);
3182         if (vt.vt == VT_UI4 && vt.ulVal == 100)
3183                 return 0;
3184         return 1;
3185 }
3186
3187 LIBTEST_API int STDCALL 
3188 mono_test_marshal_variant_out_long_unmanaged(VarRefFunc func)
3189 {
3190         VARIANT vt;
3191         VariantInit (&vt);
3192         func (VT_I8, &vt);
3193         if (vt.vt == VT_I8 && vt.llVal == -100)
3194                 return 0;
3195         return 1;
3196 }
3197
3198 LIBTEST_API int STDCALL 
3199 mono_test_marshal_variant_out_ulong_unmanaged(VarRefFunc func)
3200 {
3201         VARIANT vt;
3202         VariantInit (&vt);
3203         func (VT_UI8, &vt);
3204         if (vt.vt == VT_UI8 && vt.ullVal == 100)
3205                 return 0;
3206         return 1;
3207 }
3208
3209 LIBTEST_API int STDCALL 
3210 mono_test_marshal_variant_out_float_unmanaged(VarRefFunc func)
3211 {
3212         VARIANT vt;
3213         VariantInit (&vt);
3214         func (VT_R4, &vt);
3215         if (vt.vt == VT_R4 && fabs (vt.fltVal - 3.14f) < 1e-10)
3216                 return 0;
3217         return 1;
3218 }
3219
3220 LIBTEST_API int STDCALL 
3221 mono_test_marshal_variant_out_double_unmanaged(VarRefFunc func)
3222 {
3223         VARIANT vt;
3224         VariantInit (&vt);
3225         func (VT_R8, &vt);
3226         if (vt.vt == VT_R8 && fabs (vt.dblVal - 3.14) < 1e-10)
3227                 return 0;
3228         return 1;
3229 }
3230
3231 LIBTEST_API int STDCALL 
3232 mono_test_marshal_variant_out_bstr_unmanaged(VarRefFunc func)
3233 {
3234         VARIANT vt;
3235         gchar* bstr_utf8;
3236         gint32 result = 0;
3237
3238
3239         VariantInit (&vt);
3240         func (VT_BSTR, &vt);
3241         bstr_utf8 = g_utf16_to_utf8 (vt.bstrVal, -1, NULL, NULL, NULL);
3242         result = strcmp("PI", bstr_utf8);
3243         g_free(bstr_utf8);
3244         if (vt.vt == VT_BSTR && !result)
3245                 return 0;
3246         return 1;
3247 }
3248
3249 LIBTEST_API int STDCALL 
3250 mono_test_marshal_variant_out_bool_true_unmanaged(VarRefFunc func)
3251 {
3252         VARIANT vt;
3253         VariantInit (&vt);
3254         func (VT_BOOL, &vt);
3255         if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
3256                 return 0;
3257         return 1;
3258 }
3259
3260 LIBTEST_API int STDCALL 
3261 mono_test_marshal_variant_out_bool_false_unmanaged(VarRefFunc func)
3262 {
3263         VARIANT vt;
3264         VariantInit (&vt);
3265         func (VT_BOOL, &vt);
3266         if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
3267                 return 0;
3268         return 1;
3269 }
3270
3271 typedef struct MonoComObject MonoComObject;
3272
3273 typedef struct
3274 {
3275         int (STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
3276         int (STDCALL *AddRef)(MonoComObject* pUnk);
3277         int (STDCALL *Release)(MonoComObject* pUnk);
3278         int (STDCALL *get_ITest)(MonoComObject* pUnk, MonoComObject* *ppUnk);
3279         int (STDCALL *SByteIn)(MonoComObject* pUnk, char a);
3280         int (STDCALL *ByteIn)(MonoComObject* pUnk, unsigned char a);
3281         int (STDCALL *ShortIn)(MonoComObject* pUnk, short a);
3282         int (STDCALL *UShortIn)(MonoComObject* pUnk, unsigned short a);
3283         int (STDCALL *IntIn)(MonoComObject* pUnk, int a);
3284         int (STDCALL *UIntIn)(MonoComObject* pUnk, unsigned int a);
3285         int (STDCALL *LongIn)(MonoComObject* pUnk, gint64 a);
3286         int (STDCALL *ULongIn)(MonoComObject* pUnk, guint64 a);
3287         int (STDCALL *FloatIn)(MonoComObject* pUnk, float a);
3288         int (STDCALL *DoubleIn)(MonoComObject* pUnk, double a);
3289         int (STDCALL *ITestIn)(MonoComObject* pUnk, MonoComObject* pUnk2);
3290         int (STDCALL *ITestOut)(MonoComObject* pUnk, MonoComObject* *ppUnk);
3291 } MonoIUnknown;
3292
3293 struct MonoComObject
3294 {
3295         MonoIUnknown* vtbl;
3296         int m_ref;
3297 };
3298
3299 static GUID IID_ITest = {0, 0, 0, {0,0,0,0,0,0,0,1}};
3300 static GUID IID_IMonoUnknown = {0, 0, 0, {0xc0,0,0,0,0,0,0,0x46}};
3301 static GUID IID_IMonoDispatch = {0x00020400, 0, 0, {0xc0,0,0,0,0,0,0,0x46}};
3302
3303 LIBTEST_API int STDCALL
3304 MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
3305 {
3306
3307         *ppv = NULL;
3308         if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
3309                 *ppv = pUnk;
3310                 return S_OK;
3311         }
3312         else if (!memcmp(riid, &IID_ITest, sizeof(GUID))) {
3313                 *ppv = pUnk;
3314                 return S_OK;
3315         }
3316         else if (!memcmp(riid, &IID_IMonoDispatch, sizeof(GUID))) {
3317                 *ppv = pUnk;
3318                 return S_OK;
3319         }
3320         return 0x80004002; //E_NOINTERFACE;
3321 }
3322
3323 LIBTEST_API int STDCALL 
3324 MonoAddRef(MonoComObject* pUnk)
3325 {
3326         return ++(pUnk->m_ref);
3327 }
3328
3329 LIBTEST_API int STDCALL 
3330 MonoRelease(MonoComObject* pUnk)
3331 {
3332         return --(pUnk->m_ref);
3333 }
3334
3335 LIBTEST_API int STDCALL 
3336 SByteIn(MonoComObject* pUnk, char a)
3337 {
3338         return S_OK;
3339 }
3340
3341 LIBTEST_API int STDCALL 
3342 ByteIn(MonoComObject* pUnk, unsigned char a)
3343 {
3344         return S_OK;
3345 }
3346
3347 LIBTEST_API int STDCALL 
3348 ShortIn(MonoComObject* pUnk, short a)
3349 {
3350         return S_OK;
3351 }
3352
3353 LIBTEST_API int STDCALL 
3354 UShortIn(MonoComObject* pUnk, unsigned short a)
3355 {
3356         return S_OK;
3357 }
3358
3359 LIBTEST_API int STDCALL 
3360 IntIn(MonoComObject* pUnk, int a)
3361 {
3362         return S_OK;
3363 }
3364
3365 LIBTEST_API int STDCALL 
3366 UIntIn(MonoComObject* pUnk, unsigned int a)
3367 {
3368         return S_OK;
3369 }
3370
3371 LIBTEST_API int STDCALL 
3372 LongIn(MonoComObject* pUnk, gint64 a)
3373 {
3374         return S_OK;
3375 }
3376
3377 LIBTEST_API int STDCALL 
3378 ULongIn(MonoComObject* pUnk, guint64 a)
3379 {
3380         return S_OK;
3381 }
3382
3383 LIBTEST_API int STDCALL 
3384 FloatIn(MonoComObject* pUnk, float a)
3385 {
3386         return S_OK;
3387 }
3388
3389 LIBTEST_API int STDCALL 
3390 DoubleIn(MonoComObject* pUnk, double a)
3391 {
3392         return S_OK;
3393 }
3394
3395 LIBTEST_API int STDCALL 
3396 ITestIn(MonoComObject* pUnk, MonoComObject *pUnk2)
3397 {
3398         return S_OK;
3399 }
3400
3401 LIBTEST_API int STDCALL 
3402 ITestOut(MonoComObject* pUnk, MonoComObject* *ppUnk)
3403 {
3404         return S_OK;
3405 }
3406
3407 static void create_com_object (MonoComObject** pOut);
3408
3409 LIBTEST_API int STDCALL 
3410 get_ITest(MonoComObject* pUnk, MonoComObject* *ppUnk)
3411 {
3412         create_com_object (ppUnk);
3413         return S_OK;
3414 }
3415
3416 static void create_com_object (MonoComObject** pOut)
3417 {
3418         *pOut = marshal_new0 (MonoComObject, 1);
3419         (*pOut)->vtbl = marshal_new0 (MonoIUnknown, 1);
3420
3421         (*pOut)->m_ref = 1;
3422         (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
3423         (*pOut)->vtbl->AddRef = MonoAddRef;
3424         (*pOut)->vtbl->Release = MonoRelease;
3425         (*pOut)->vtbl->SByteIn = SByteIn;
3426         (*pOut)->vtbl->ByteIn = ByteIn;
3427         (*pOut)->vtbl->ShortIn = ShortIn;
3428         (*pOut)->vtbl->UShortIn = UShortIn;
3429         (*pOut)->vtbl->IntIn = IntIn;
3430         (*pOut)->vtbl->UIntIn = UIntIn;
3431         (*pOut)->vtbl->LongIn = LongIn;
3432         (*pOut)->vtbl->ULongIn = ULongIn;
3433         (*pOut)->vtbl->FloatIn = FloatIn;
3434         (*pOut)->vtbl->DoubleIn = DoubleIn;
3435         (*pOut)->vtbl->ITestIn = ITestIn;
3436         (*pOut)->vtbl->ITestOut = ITestOut;
3437         (*pOut)->vtbl->get_ITest = get_ITest;
3438 }
3439
3440 static MonoComObject* same_object = NULL;
3441
3442 LIBTEST_API int STDCALL 
3443 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
3444 {
3445         create_com_object (pUnk);
3446
3447         if (!same_object)
3448                 same_object = *pUnk;
3449
3450         return 0;
3451 }
3452
3453 LIBTEST_API int STDCALL 
3454 mono_test_marshal_com_object_same(MonoComObject* *pUnk)
3455 {
3456         *pUnk = same_object;
3457
3458         return 0;
3459 }
3460
3461 LIBTEST_API int STDCALL 
3462 mono_test_marshal_com_object_destroy(MonoComObject *pUnk)
3463 {
3464         int ref = --(pUnk->m_ref);
3465         g_free(pUnk->vtbl);
3466         g_free(pUnk);
3467
3468         return ref;
3469 }
3470
3471 LIBTEST_API int STDCALL 
3472 mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)
3473 {
3474         return pUnk->m_ref;
3475 }
3476
3477 LIBTEST_API int STDCALL 
3478 mono_test_marshal_ccw_itest (MonoComObject *pUnk)
3479 {
3480         int hr = 0;
3481         MonoComObject* pTest;
3482
3483         if (!pUnk)
3484                 return 1;
3485
3486         hr = pUnk->vtbl->SByteIn (pUnk, -100);
3487         if (hr != 0)
3488                 return 2;
3489         hr = pUnk->vtbl->ByteIn (pUnk, 100);
3490         if (hr != 0)
3491                 return 3;
3492         hr = pUnk->vtbl->ShortIn (pUnk, -100);
3493         if (hr != 0)
3494                 return 4;
3495         hr = pUnk->vtbl->UShortIn (pUnk, 100);
3496         if (hr != 0)
3497                 return 5;
3498         hr = pUnk->vtbl->IntIn (pUnk, -100);
3499         if (hr != 0)
3500                 return 6;
3501         hr = pUnk->vtbl->UIntIn (pUnk, 100);
3502         if (hr != 0)
3503                 return 7;
3504         hr = pUnk->vtbl->LongIn (pUnk, -100);
3505         if (hr != 0)
3506                 return 8;
3507         hr = pUnk->vtbl->ULongIn (pUnk, 100);
3508         if (hr != 0)
3509                 return 9;
3510         hr = pUnk->vtbl->FloatIn (pUnk, 3.14f);
3511         if (hr != 0)
3512                 return 10;
3513         hr = pUnk->vtbl->DoubleIn (pUnk, 3.14);
3514         if (hr != 0)
3515                 return 11;
3516         hr = pUnk->vtbl->ITestIn (pUnk, pUnk);
3517         if (hr != 0)
3518                 return 12;
3519         hr = pUnk->vtbl->ITestOut (pUnk, &pTest);
3520         if (hr != 0)
3521                 return 13;
3522
3523         return 0;
3524 }
3525
3526 /*
3527  * mono_method_get_unmanaged_thunk tests
3528  */
3529
3530 #if defined(__GNUC__) && ((defined(__i386__) && (defined(__linux__) || defined (__APPLE__)) || defined (__FreeBSD__) || defined(__OpenBSD__)) || (defined(__ppc__) && defined(__APPLE__)))
3531 #define ALIGN(size) __attribute__ ((aligned(size)))
3532 #else
3533 #define ALIGN(size)
3534 #endif
3535
3536
3537 /* thunks.cs:TestStruct */
3538 typedef struct _TestStruct {
3539         int A;
3540         double B;
3541 } TestStruct;
3542
3543 /* Searches for mono symbols in all loaded modules */
3544 static gpointer
3545 lookup_mono_symbol (const char *symbol_name)
3546 {
3547         gpointer symbol;
3548         if (g_module_symbol (g_module_open (NULL, G_MODULE_BIND_LAZY), symbol_name, &symbol))
3549                 return symbol;
3550         else
3551                 return NULL;
3552 }
3553
3554 LIBTEST_API gpointer STDCALL
3555 mono_test_marshal_lookup_symbol (const char *symbol_name)
3556 {
3557         return lookup_mono_symbol (symbol_name);
3558 }
3559
3560 #define MONO_BEGIN_EFRAME { void *__dummy; void *__region_cookie = mono_threads_enter_gc_unsafe_region ? mono_threads_enter_gc_unsafe_region (&__dummy) : NULL;
3561 #define MONO_END_EFRAME if (mono_threads_exit_gc_unsafe_region) mono_threads_exit_gc_unsafe_region (__region_cookie, &__dummy); }
3562
3563 /**
3564  * test_method_thunk:
3565  *
3566  * @test_id: the test number
3567  * @test_method_handle: MonoMethod* of the C# test method
3568  * @create_object_method_handle: MonoMethod* of thunks.cs:Test.CreateObject
3569  */
3570 LIBTEST_API int STDCALL  
3571 test_method_thunk (int test_id, gpointer test_method_handle, gpointer create_object_method_handle)
3572 {
3573         int ret = 0;
3574
3575         gpointer (*mono_method_get_unmanaged_thunk)(gpointer)
3576                 = lookup_mono_symbol ("mono_method_get_unmanaged_thunk");
3577
3578         gpointer (*mono_string_new_wrapper)(const char *)
3579                 = lookup_mono_symbol ("mono_string_new_wrapper");
3580
3581         char* (*mono_string_to_utf8)(gpointer)
3582                 = lookup_mono_symbol ("mono_string_to_utf8");
3583
3584         gpointer (*mono_object_unbox)(gpointer)
3585                 = lookup_mono_symbol ("mono_object_unbox");
3586
3587         gpointer (*mono_threads_enter_gc_unsafe_region) (gpointer)
3588                 = lookup_mono_symbol ("mono_threads_enter_gc_unsafe_region");
3589
3590         void (*mono_threads_exit_gc_unsafe_region) (gpointer, gpointer)
3591                 = lookup_mono_symbol ("mono_threads_exit_gc_unsafe_region");
3592
3593         
3594
3595         gpointer test_method, ex = NULL;
3596         gpointer (STDCALL *CreateObject)(gpointer*);
3597
3598         MONO_BEGIN_EFRAME;
3599
3600         if (!mono_method_get_unmanaged_thunk) {
3601                 ret = 1;
3602                 goto done;
3603         }
3604
3605         test_method =  mono_method_get_unmanaged_thunk (test_method_handle);
3606         if (!test_method) {
3607                 ret = 2;
3608                 goto done;
3609         }
3610
3611         CreateObject = mono_method_get_unmanaged_thunk (create_object_method_handle);
3612         if (!CreateObject) {
3613                 ret = 3;
3614                 goto done;
3615         }
3616         
3617
3618         switch (test_id) {
3619
3620         case 0: {
3621                 /* thunks.cs:Test.Test0 */
3622                 void (STDCALL *F)(gpointer*) = test_method;
3623                 F (&ex);
3624                 break;
3625         }
3626
3627         case 1: {
3628                 /* thunks.cs:Test.Test1 */
3629                 int (STDCALL *F)(gpointer*) = test_method;
3630                 if (F (&ex) != 42) {
3631                         ret = 4;
3632                         goto done;
3633                 }
3634                 break;
3635         }
3636
3637         case 2: {
3638                 /* thunks.cs:Test.Test2 */
3639                 gpointer (STDCALL *F)(gpointer, gpointer*) = test_method;
3640                 gpointer str = mono_string_new_wrapper ("foo");
3641                 if (str != F (str, &ex)) {
3642                         ret = 4;
3643                         goto done;
3644                 }
3645                 break;
3646         }
3647
3648         case 3: {
3649                 /* thunks.cs:Test.Test3 */
3650                 gpointer (STDCALL *F)(gpointer, gpointer, gpointer*);
3651                 gpointer obj;
3652                 gpointer str;
3653
3654                 F = test_method;
3655                 obj = CreateObject (&ex);
3656                 str = mono_string_new_wrapper ("bar");
3657
3658                 if (str != F (obj, str, &ex)) {
3659                         ret = 4;
3660                         goto done;
3661                 }
3662                 break;
3663         }
3664
3665         case 4: {
3666                 /* thunks.cs:Test.Test4 */
3667                 int (STDCALL *F)(gpointer, gpointer, int, gpointer*);
3668                 gpointer obj;
3669                 gpointer str;
3670
3671                 F = test_method;
3672                 obj = CreateObject (&ex);
3673                 str = mono_string_new_wrapper ("bar");
3674
3675                 if (42 != F (obj, str, 42, &ex)) {
3676                         ret = 4;
3677                         goto done;
3678                 }
3679
3680                 break;
3681         }
3682
3683         case 5: {
3684                 /* thunks.cs:Test.Test5 */
3685                 int (STDCALL *F)(gpointer, gpointer, int, gpointer*);
3686                 gpointer obj;
3687                 gpointer str;
3688
3689                 F = test_method;
3690                 obj = CreateObject (&ex);
3691                 str = mono_string_new_wrapper ("bar");
3692
3693                 F (obj, str, 42, &ex);
3694                 if (!ex) {
3695                         ret = 4;
3696                         goto done;
3697                 }
3698
3699                 break;
3700         }
3701
3702         case 6: {
3703                 /* thunks.cs:Test.Test6 */
3704                 int (STDCALL *F)(gpointer, guint8, gint16, gint32, gint64, float, double,
3705                                  gpointer, gpointer*);
3706                 gpointer obj;
3707                 gpointer str = mono_string_new_wrapper ("Test6");
3708                 int res;
3709
3710                 F = test_method;
3711                 obj = CreateObject (&ex);
3712
3713                 res = F (obj, 254, 32700, -245378, 6789600, 3.1415, 3.1415, str, &ex);
3714                 if (ex) {
3715                         ret = 4;
3716                         goto done;
3717                 }
3718
3719                 if (!res) {
3720                         ret = 5;
3721                         goto done;
3722                 }
3723
3724                 break;
3725         }
3726
3727         case 7: {
3728                 /* thunks.cs:Test.Test7 */
3729                 gint64 (STDCALL *F)(gpointer*) = test_method;
3730                 if (F (&ex) != G_MAXINT64) {
3731                         ret = 4;
3732                         goto done;
3733                 }
3734                 break;
3735         }
3736
3737         case 8: {
3738                 /* thunks.cs:Test.Test8 */
3739                 void (STDCALL *F)(guint8*, gint16*, gint32*, gint64*, float*, double*,
3740                                  gpointer*, gpointer*);
3741
3742                 guint8 a1;
3743                 gint16 a2;
3744                 gint32 a3;
3745                 gint64 a4;
3746                 float a5;
3747                 double a6;
3748                 gpointer a7;
3749
3750                 F = test_method;
3751
3752                 F (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &ex);
3753                 if (ex) {
3754                         ret = 4;
3755                         goto done;
3756                 }
3757
3758                 if (!(a1 == 254 &&
3759                       a2 == 32700 &&
3760                       a3 == -245378 &&
3761                       a4 == 6789600 &&
3762                       (fabs (a5 - 3.1415) < 0.001) &&
3763                       (fabs (a6 - 3.1415) < 0.001) &&
3764                       strcmp (mono_string_to_utf8 (a7), "Test8") == 0)){
3765                                 ret = 5;
3766                                 goto done;
3767                         }
3768
3769                 break;
3770         }
3771
3772         case 9: {
3773                 /* thunks.cs:Test.Test9 */
3774                 void (STDCALL *F)(guint8*, gint16*, gint32*, gint64*, float*, double*,
3775                                  gpointer*, gpointer*);
3776
3777                 guint8 a1;
3778                 gint16 a2;
3779                 gint32 a3;
3780                 gint64 a4;
3781                 float a5;
3782                 double a6;
3783                 gpointer a7;
3784
3785                 F = test_method;
3786
3787                 F (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &ex);
3788                 if (!ex) {
3789                         ret = 4;
3790                         goto done;
3791                 }
3792
3793                 break;
3794         }
3795
3796         case 10: {
3797                 /* thunks.cs:Test.Test10 */
3798                 void (STDCALL *F)(gpointer*, gpointer*);
3799
3800                 gpointer obj1, obj2;
3801
3802                 obj1 = obj2 = CreateObject (&ex);
3803                 if (ex) {
3804                         ret = 4;
3805                         goto done;
3806                 }
3807
3808                 F = test_method;
3809
3810                 F (&obj1, &ex);
3811                 if (ex) {
3812                         ret = 5;
3813                         goto done;
3814                 }
3815
3816                 if (obj1 == obj2) {
3817                         ret = 6;
3818                         goto done;
3819                 }
3820
3821                 break;
3822         }
3823
3824         case 100: {
3825                 /* thunks.cs:TestStruct.Test0 */
3826                 int (STDCALL *F)(gpointer*, gpointer*);
3827
3828                 gpointer obj;
3829                 TestStruct *a1;
3830                 int res;
3831
3832                 obj = CreateObject (&ex);
3833                 if (ex) {
3834                         ret = 4;
3835                         goto done;
3836                 }
3837
3838                 if (!obj) {
3839                         ret = 5;
3840                         goto done;
3841                 }
3842
3843                 a1 = mono_object_unbox (obj);
3844                 if (!a1) {
3845                         ret = 6;
3846                         goto done;
3847                 }
3848
3849                 a1->A = 42;
3850                 a1->B = 3.1415;
3851
3852                 F = test_method;
3853
3854                 res = F (obj, &ex);
3855                 if (ex) {
3856                         ret = 7;
3857                         goto done;
3858                 }
3859
3860                 if (!res) {
3861                         ret = 8;
3862                         goto done;
3863                 }
3864
3865                 /* check whether the call was really by value */
3866                 if (a1->A != 42 || a1->B != 3.1415) {
3867                         ret = 9;
3868                         goto done;
3869                 }
3870
3871                 break;
3872         }
3873
3874         case 101: {
3875                 /* thunks.cs:TestStruct.Test1 */
3876                 void (STDCALL *F)(gpointer, gpointer*);
3877
3878                 TestStruct *a1;
3879                 gpointer obj;
3880
3881                 obj = CreateObject (&ex);
3882                 if (ex) {
3883                         ret = 4;
3884                         goto done;
3885                 }
3886
3887                 if (!obj) {
3888                         ret = 5;
3889                         goto done;
3890                 }
3891
3892                 a1 = mono_object_unbox (obj);
3893                 if (!a1) {
3894                         ret = 6;
3895                         goto done;
3896                 }
3897
3898                 F = test_method;
3899
3900                 F (obj, &ex);
3901                 if (ex) {
3902                         ret = 7;
3903                         goto done;
3904                 }
3905
3906                 if (a1->A != 42) {
3907                         ret = 8;
3908                         goto done;
3909                 }
3910
3911                 if (!(fabs (a1->B - 3.1415) < 0.001)) {
3912                         ret = 9;
3913                         goto done;
3914                 }
3915
3916                 break;
3917         }
3918
3919         case 102: {
3920                 /* thunks.cs:TestStruct.Test2 */
3921                 gpointer (STDCALL *F)(gpointer*);
3922
3923                 TestStruct *a1;
3924                 gpointer obj;
3925
3926                 F = test_method;
3927
3928                 obj = F (&ex);
3929                 if (ex) {
3930                         ret = 4;
3931                         goto done;
3932                 }
3933
3934                 if (!obj) {
3935                         ret = 5;
3936                         goto done;
3937                 }
3938
3939                 a1 = mono_object_unbox (obj);
3940
3941                 if (a1->A != 42) {
3942                         ret = 5;
3943                         goto done;
3944                 }
3945
3946                 if (!(fabs (a1->B - 3.1415) < 0.001)) {
3947                         ret = 6;
3948                         goto done;
3949                 }
3950
3951                 break;
3952         }
3953
3954         case 103: {
3955                 /* thunks.cs:TestStruct.Test3 */
3956                 void (STDCALL *F)(gpointer, gpointer*);
3957
3958                 TestStruct *a1;
3959                 gpointer obj;
3960
3961                 obj = CreateObject (&ex);
3962                 if (ex) {
3963                         ret = 4;
3964                         goto done;
3965                 }
3966
3967                 if (!obj) {
3968                         ret = 5;
3969                         goto done;
3970                 }
3971                 
3972                 a1 = mono_object_unbox (obj);
3973
3974                 if (!a1) {
3975                         ret = 6;
3976                         goto done;
3977                 }
3978
3979                 a1->A = 42;
3980                 a1->B = 3.1415;
3981
3982                 F = test_method;
3983
3984                 F (obj, &ex);
3985                 if (ex) {
3986                         ret = 4;
3987                         goto done;
3988                 }
3989
3990                 if (a1->A != 1) {
3991                         ret = 5;
3992                         goto done;
3993                 }
3994
3995                 if (a1->B != 17) {
3996                         ret = 6;
3997                         goto done;
3998                 }
3999
4000                 break;
4001         }
4002
4003         default:
4004                 ret = 9;
4005
4006         }
4007 done:
4008         MONO_END_EFRAME;
4009
4010         return ret;
4011 }
4012
4013 typedef struct 
4014 {
4015         char a;
4016 } winx64_struct1;
4017
4018 LIBTEST_API int STDCALL  
4019 mono_test_Winx64_struct1_in (winx64_struct1 var)
4020 {
4021         if (var.a != 123)
4022                 return 1;
4023         return 0;
4024 }
4025
4026 typedef struct
4027 {
4028         char a;
4029         char b;
4030 } winx64_struct2;
4031
4032 LIBTEST_API int STDCALL  
4033 mono_test_Winx64_struct2_in (winx64_struct2 var)
4034 {
4035         if (var.a != 4)
4036                 return 1;
4037         if (var.b != 5)
4038                 return 2;
4039         return 0;
4040 }
4041
4042
4043 typedef struct
4044 {
4045         char a;
4046         char b;
4047         short c;
4048 } winx64_struct3;
4049
4050 LIBTEST_API int STDCALL  
4051 mono_test_Winx64_struct3_in (winx64_struct3 var)
4052 {
4053         if (var.a != 4)
4054                 return 1;
4055         if (var.b != 5)
4056                 return 2;
4057         if (var.c != 0x1234)
4058                 return 3;
4059         return 0;
4060 }
4061
4062 typedef struct
4063 {
4064         char a;
4065         char b;
4066         short c;
4067         unsigned int d;
4068 } winx64_struct4;
4069
4070 LIBTEST_API int STDCALL  
4071 mono_test_Winx64_struct4_in (winx64_struct4 var)
4072 {
4073         if (var.a != 4)
4074                 return 1;
4075         if (var.b != 5)
4076                 return 2;
4077         if (var.c != 0x1234)
4078                 return 3;
4079         if (var.d != 0x87654321)
4080                 return 4;
4081         return 0;
4082 }
4083
4084 typedef struct
4085 {
4086         char a;
4087         char b;
4088         char c;
4089 } winx64_struct5;
4090
4091 LIBTEST_API int STDCALL  
4092 mono_test_Winx64_struct5_in (winx64_struct5 var)
4093 {
4094         if (var.a != 4)
4095                 return 1;
4096         if (var.b != 5)
4097                 return 2;
4098         if (var.c != 6)
4099                 return 3;
4100         return 0;
4101 }
4102
4103 typedef struct
4104 {
4105         winx64_struct1 a;
4106         short b;
4107         char c;
4108 } winx64_struct6;
4109
4110 LIBTEST_API int STDCALL  
4111 mono_test_Winx64_struct6_in (winx64_struct6 var)
4112 {
4113         if (var.a.a != 4)
4114                 return 1;
4115         if (var.b != 5)
4116                 return 2;
4117         if (var.c != 6)
4118                 return 3;
4119         return 0;
4120 }
4121
4122 LIBTEST_API int STDCALL  
4123 mono_test_Winx64_structs_in1 (winx64_struct1 var1,
4124                          winx64_struct2 var2,
4125                          winx64_struct3 var3,
4126                          winx64_struct4 var4)
4127 {
4128         if (var1.a != 123)
4129                 return 1;
4130         
4131         if (var2.a != 4)
4132                 return 2;
4133         if (var2.b != 5)
4134                 return 3;
4135         
4136         if (var3.a != 4)
4137                 return 4;
4138         if (var3.b != 5)
4139                 return 2;
4140         if (var3.c != 0x1234)
4141                 return 5;
4142         
4143         if (var4.a != 4)
4144                 return 6;
4145         if (var4.b != 5)
4146                 return 7;
4147         if (var4.c != 0x1234)
4148                 return 8;
4149         if (var4.d != 0x87654321)
4150                 return 9;
4151         return 0;
4152 }
4153
4154 LIBTEST_API int STDCALL  
4155 mono_test_Winx64_structs_in2 (winx64_struct1 var1,
4156                          winx64_struct1 var2,
4157                          winx64_struct1 var3,
4158                          winx64_struct1 var4,
4159                          winx64_struct1 var5)
4160 {
4161         if (var1.a != 1)
4162                 return 1;
4163         if (var2.a != 2)
4164                 return 2;
4165         if (var3.a != 3)
4166                 return 3;
4167         if (var4.a != 4)
4168                 return 4;
4169         if (var5.a != 5)
4170                 return 5;
4171         
4172         return 0;
4173 }
4174
4175 LIBTEST_API int STDCALL  
4176 mono_test_Winx64_structs_in3 (winx64_struct1 var1,
4177                          winx64_struct5 var2,
4178                          winx64_struct1 var3,
4179                          winx64_struct5 var4,
4180                          winx64_struct1 var5,
4181                          winx64_struct5 var6)
4182 {
4183         if (var1.a != 1)
4184                 return 1;
4185         
4186         if (var2.a != 2)
4187                 return 2;
4188         if (var2.b != 3)
4189                 return 2;
4190         if (var2.c != 4)
4191                 return 4;
4192         
4193         if (var3.a != 5)
4194                 return 5;
4195         
4196         if (var4.a != 6)
4197                 return 6;
4198         if (var4.b != 7)
4199                 return 7;
4200         if (var4.c != 8)
4201                 return 8;
4202         
4203         if (var5.a != 9)
4204                 return 9;
4205
4206         if (var6.a != 10)
4207                 return 10;
4208         if (var6.b != 11)
4209                 return 11;
4210         if (var6.c != 12)
4211                 return 12;
4212         
4213         return 0;
4214 }
4215
4216 LIBTEST_API winx64_struct1 STDCALL  
4217 mono_test_Winx64_struct1_ret (void)
4218 {
4219         winx64_struct1 ret;
4220         ret.a = 123;
4221         return ret;
4222 }
4223
4224 LIBTEST_API winx64_struct2 STDCALL  
4225 mono_test_Winx64_struct2_ret (void)
4226 {
4227         winx64_struct2 ret;
4228         ret.a = 4;
4229         ret.b = 5;
4230         return ret;
4231 }
4232
4233 LIBTEST_API winx64_struct3 STDCALL  
4234 mono_test_Winx64_struct3_ret (void)
4235 {
4236         winx64_struct3 ret;
4237         ret.a = 4;
4238         ret.b = 5;
4239         ret.c = 0x1234;
4240         return ret;
4241 }
4242
4243 LIBTEST_API winx64_struct4 STDCALL  
4244 mono_test_Winx64_struct4_ret (void)
4245 {
4246         winx64_struct4 ret;
4247         ret.a = 4;
4248         ret.b = 5;
4249         ret.c = 0x1234;
4250         ret.d = 0x87654321;
4251         return ret;
4252 }
4253
4254 LIBTEST_API winx64_struct5 STDCALL  
4255 mono_test_Winx64_struct5_ret (void)
4256 {
4257         winx64_struct5 ret;
4258         ret.a = 4;
4259         ret.b = 5;
4260         ret.c = 6;
4261         return ret;
4262 }
4263
4264 LIBTEST_API winx64_struct1 STDCALL  
4265 mono_test_Winx64_struct1_ret_5_args (char a, char b, char c, char d, char e)
4266 {
4267         winx64_struct1 ret;
4268         ret.a = a + b + c + d + e;
4269         return ret;
4270 }
4271
4272 LIBTEST_API winx64_struct5 STDCALL
4273 mono_test_Winx64_struct5_ret6_args (char a, char b, char c, char d, char e)
4274 {
4275         winx64_struct5 ret;
4276         ret.a = a + b;
4277         ret.b = c + d;
4278         ret.c = e;
4279         return ret;
4280 }
4281
4282 typedef struct
4283 {
4284         float a;
4285         float b;
4286 } winx64_floatStruct;
4287
4288 LIBTEST_API int STDCALL  
4289 mono_test_Winx64_floatStruct (winx64_floatStruct a)
4290 {
4291         if (a.a > 5.6 || a.a < 5.4)
4292                 return 1;
4293
4294         if (a.b > 9.6 || a.b < 9.4)
4295                 return 2;
4296         
4297         return 0;
4298 }
4299
4300 typedef struct
4301 {
4302         double a;
4303 } winx64_doubleStruct;
4304
4305 LIBTEST_API int STDCALL  
4306 mono_test_Winx64_doubleStruct (winx64_doubleStruct a)
4307 {
4308         if (a.a > 5.6 || a.a < 5.4)
4309                 return 1;
4310         
4311         return 0;
4312 }
4313
4314 typedef int (STDCALL *managed_struct1_delegate) (winx64_struct1 a);
4315
4316 LIBTEST_API int STDCALL 
4317 mono_test_managed_Winx64_struct1_in(managed_struct1_delegate func)
4318 {
4319         winx64_struct1 val;
4320         val.a = 5;
4321         return func (val);
4322 }
4323
4324 typedef int (STDCALL *managed_struct5_delegate) (winx64_struct5 a);
4325
4326 LIBTEST_API int STDCALL 
4327 mono_test_managed_Winx64_struct5_in(managed_struct5_delegate func)
4328 {
4329         winx64_struct5 val;
4330         val.a = 5;
4331         val.b = 0x10;
4332         val.c = 0x99;
4333         return func (val);
4334 }
4335
4336 typedef int (STDCALL *managed_struct1_struct5_delegate) (winx64_struct1 a, winx64_struct5 b,
4337                                                          winx64_struct1 c, winx64_struct5 d,
4338                                                          winx64_struct1 e, winx64_struct5 f);
4339
4340 LIBTEST_API int STDCALL 
4341 mono_test_managed_Winx64_struct1_struct5_in(managed_struct1_struct5_delegate func)
4342 {
4343         winx64_struct1 a, c, e;
4344         winx64_struct5 b, d, f;
4345         a.a = 1;
4346         b.a = 2; b.b = 3; b.c = 4;
4347         c.a = 5;
4348         d.a = 6; d.b = 7; d.c = 8;
4349         e.a = 9;
4350         f.a = 10; f.b = 11; f.c = 12;
4351
4352         return func (a, b, c, d, e, f);
4353 }
4354
4355 typedef winx64_struct1 (STDCALL *managed_struct1_ret_delegate) (void);
4356
4357 LIBTEST_API int STDCALL 
4358 mono_test_Winx64_struct1_ret_managed (managed_struct1_ret_delegate func)
4359 {
4360         winx64_struct1 ret;
4361
4362         ret = func ();
4363
4364         if (ret.a != 0x45)
4365                 return 1;
4366         
4367         return 0;
4368 }
4369
4370 typedef winx64_struct5 (STDCALL *managed_struct5_ret_delegate) (void);
4371
4372 LIBTEST_API int STDCALL 
4373 mono_test_Winx64_struct5_ret_managed (managed_struct5_ret_delegate func)
4374 {
4375         winx64_struct5 ret;
4376
4377         ret = func ();
4378
4379         if (ret.a != 0x12)
4380                 return 1;
4381         if (ret.b != 0x34)
4382                 return 2;
4383         if (ret.c != 0x56)
4384                 return 3;
4385         
4386         return 0;
4387 }
4388
4389 LIBTEST_API int STDCALL 
4390 mono_test_marshal_bool_in (int arg, unsigned int expected, unsigned int bDefaultMarsh, unsigned int bBoolCustMarsh,
4391                            char bI1CustMarsh, unsigned char bU1CustMarsh, short bVBCustMarsh)
4392 {
4393         switch (arg) {
4394         case 1: 
4395                 if (bDefaultMarsh != expected)
4396                         return 1;
4397                 break;
4398         case 2: 
4399                 if (bBoolCustMarsh != expected)
4400                         return 2;
4401                 break;
4402         case 3: 
4403                 if (bI1CustMarsh != expected)
4404                         return 3;
4405                 break;
4406         case 4: 
4407                 if (bU1CustMarsh != expected)
4408                         return 4;
4409                 break;
4410         case 5: 
4411                 if (bVBCustMarsh != expected)
4412                         return 5;
4413                 break;
4414         default:
4415                 return 999;             
4416         }
4417         return 0;
4418 }
4419
4420 LIBTEST_API int STDCALL 
4421 mono_test_marshal_bool_out (int arg, unsigned int testVal, unsigned int* bDefaultMarsh, unsigned int* bBoolCustMarsh,
4422                            char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh)
4423 {
4424         switch (arg) {
4425         case 1: 
4426                 if (!bDefaultMarsh)
4427                         return 1;
4428                 *bDefaultMarsh = testVal;
4429                 break;  
4430         case 2: 
4431                 if (!bBoolCustMarsh)
4432                         return 2;
4433                 *bBoolCustMarsh = testVal;
4434                 break;  
4435         case 3: 
4436                 if (!bI1CustMarsh)
4437                         return 3;
4438                 *bI1CustMarsh = (char)testVal;
4439                 break;  
4440         case 4: 
4441                 if (!bU1CustMarsh)
4442                         return 4;
4443                 *bU1CustMarsh = (unsigned char)testVal;
4444                 break;  
4445         case 5: 
4446                 if (!bVBCustMarsh)
4447                         return 5;
4448                 *bVBCustMarsh = (unsigned short)testVal;
4449                 break;  
4450         default:
4451                 return 999;
4452         }
4453         return 0;
4454 }
4455
4456 LIBTEST_API int STDCALL 
4457 mono_test_marshal_bool_ref (int arg, unsigned int expected, unsigned int testVal, unsigned int* bDefaultMarsh,
4458                             unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, 
4459                             unsigned short* bVBCustMarsh)
4460 {
4461         switch (arg) {
4462         case 1: 
4463                 if (!bDefaultMarsh)
4464                         return 1;
4465                 if (*bDefaultMarsh != expected)
4466                         return 2;
4467                 *bDefaultMarsh = testVal;
4468                 break;
4469         case 2: 
4470                 if (!bBoolCustMarsh)
4471                         return 3;
4472                 if (*bBoolCustMarsh != expected)
4473                         return 4;
4474                 *bBoolCustMarsh = testVal;
4475                 break;
4476         case 3: 
4477                 if (!bI1CustMarsh)
4478                         return 5;
4479                 if (*bI1CustMarsh != expected)
4480                         return 6;
4481                 *bI1CustMarsh = (char)testVal;
4482                 break;
4483         case 4: 
4484                 if (!bU1CustMarsh)
4485                         return 7;
4486                 if (*bU1CustMarsh != expected)
4487                         return 8;
4488                 *bU1CustMarsh = (unsigned char)testVal;
4489                 break;
4490         case 5: 
4491                 if (!bVBCustMarsh)
4492                         return 9;
4493                 if (*bVBCustMarsh != expected)
4494                         return 10;
4495                 *bVBCustMarsh = (unsigned short)testVal;
4496                 break;
4497         default:
4498                 return 999;             
4499         }
4500         return 0;
4501 }
4502
4503
4504 typedef int (STDCALL *MarshalBoolInDelegate) (int arg, unsigned int expected, unsigned int bDefaultMarsh,
4505         unsigned int bBoolCustMarsh, char bI1CustMarsh, unsigned char bU1CustMarsh, unsigned short bVBCustMarsh);
4506
4507 LIBTEST_API int STDCALL 
4508 mono_test_managed_marshal_bool_in (int arg, unsigned int expected, unsigned int testVal, MarshalBoolInDelegate pfcn)
4509 {
4510         if (!pfcn)
4511                 return 0x9900;
4512
4513         switch (arg) {
4514         case 1:
4515                 return pfcn (arg, expected, testVal, 0, 0, 0, 0);
4516         case 2:
4517                 return pfcn (arg, expected, 0, testVal,  0, 0, 0);
4518         case 3:
4519                 return pfcn (arg, expected, 0, 0, testVal, 0, 0);
4520         case 4:
4521                 return pfcn (arg, expected, 0, 0, 0, testVal, 0);
4522         case 5:
4523                 return pfcn (arg, expected, 0, 0, 0, 0, testVal);
4524         default:
4525                 return 0x9800;
4526         }
4527
4528         return 0;
4529 }
4530
4531 typedef int (STDCALL *MarshalBoolOutDelegate) (int arg, unsigned int expected, unsigned int* bDefaultMarsh,
4532         unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh);
4533
4534 LIBTEST_API int STDCALL 
4535 mono_test_managed_marshal_bool_out (int arg, unsigned int expected, unsigned int testVal, MarshalBoolOutDelegate pfcn)
4536 {
4537         int ret;
4538         unsigned int lDefaultMarsh, lBoolCustMarsh;
4539         char lI1CustMarsh = 0;
4540         unsigned char lU1CustMarsh = 0;
4541         unsigned short lVBCustMarsh = 0;
4542         lDefaultMarsh = lBoolCustMarsh = 0;
4543
4544         if (!pfcn)
4545                 return 0x9900;
4546
4547         switch (arg) {
4548         case 1: {
4549                 unsigned int ltVal = 0;
4550                 ret = pfcn (arg, testVal, &ltVal, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4551                 if (ret)
4552                         return 0x0100 + ret;
4553                 if (expected != ltVal)
4554                         return 0x0200;
4555                 break;
4556         }
4557         case 2: {
4558                 unsigned int ltVal = 0;
4559                 ret = pfcn (arg, testVal, &lDefaultMarsh, &ltVal, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4560                 if (ret)
4561                         return 0x0300 + ret;
4562                 if (expected != ltVal)
4563                         return 0x0400;
4564                 break;
4565         }
4566         case 3: {
4567                 char ltVal = 0;
4568                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &ltVal, &lU1CustMarsh, &lVBCustMarsh);
4569                 if (ret)
4570                         return 0x0500 + ret;
4571                 if (expected != ltVal)
4572                         return 0x0600;
4573                 break;
4574         }
4575         case 4: {
4576                 unsigned char ltVal = 0;
4577                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &ltVal, &lVBCustMarsh);
4578                 if (ret)
4579                         return 0x0700 + ret;
4580                 if (expected != ltVal)
4581                         return 0x0800;
4582                 break;
4583         }
4584         case 5: {
4585                 unsigned short ltVal = 0;
4586                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &ltVal);
4587                 if (ret)
4588                         return 0x0900 + ret;
4589                 if (expected != ltVal)
4590                         return 0x1000;
4591                 break;
4592         }
4593         default:
4594                 return 0x9800;
4595         }
4596
4597         return 0;
4598 }
4599
4600 typedef int (STDCALL *MarshalBoolRefDelegate) (int arg, unsigned int expected, unsigned int testVal, unsigned int* bDefaultMarsh,
4601         unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh);
4602
4603 LIBTEST_API int STDCALL 
4604 mono_test_managed_marshal_bool_ref (int arg, unsigned int expected, unsigned int testVal, unsigned int outExpected,
4605                                     unsigned int outTestVal, MarshalBoolRefDelegate pfcn)
4606 {
4607         int ret;
4608         unsigned int lDefaultMarsh, lBoolCustMarsh;
4609         char lI1CustMarsh = 0;
4610         unsigned char lU1CustMarsh = 0;
4611         unsigned short lVBCustMarsh = 0;
4612         lDefaultMarsh = lBoolCustMarsh = 0;
4613
4614         if (!pfcn)
4615                 return 0x9900;
4616
4617         switch (arg) {
4618         case 1:
4619         {
4620                 unsigned int ltestVal = testVal;
4621                 ret = pfcn (arg, expected, outTestVal, &ltestVal, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4622                 if (ret)
4623                         return 0x0100 + ret;
4624                 if (outExpected != ltestVal)
4625                         return 0x0200;
4626                 break;
4627         }
4628         case 2:
4629         {
4630                 unsigned int ltestVal = testVal;
4631                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &ltestVal, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4632                 if (ret)
4633                         return 0x0300 + ret;
4634                 if (outExpected != ltestVal)
4635                         return 0x0400;
4636                 break;
4637         }
4638         case 3:
4639         {
4640                 char ltestVal = testVal;
4641                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &ltestVal, &lU1CustMarsh, &lVBCustMarsh);
4642                 if (ret)
4643                         return 0x0500 + ret;
4644                 if (outExpected != ltestVal)
4645                         return 0x0600;
4646                 break;
4647         }
4648         case 4:
4649         {
4650                 unsigned char ltestVal = testVal;
4651                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &ltestVal, &lVBCustMarsh);
4652                 if (ret)
4653                         return 0x0700 + ret;
4654                 if (outExpected != ltestVal)
4655                         return 0x0800;
4656                 break;
4657         }
4658         case 5:
4659         {
4660                 unsigned short ltestVal = testVal;
4661                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &ltestVal);
4662                 if (ret)
4663                         return 0x0900 + ret;
4664                 if (outExpected != ltestVal)
4665                         return 0x1000;
4666                 break;
4667         }
4668         default:
4669                 return 0x9800;
4670         }
4671
4672         return 0;
4673 }
4674
4675 #ifdef WIN32
4676
4677 LIBTEST_API int STDCALL 
4678 mono_test_marshal_safearray_out_1dim_vt_bstr_empty (SAFEARRAY** safearray)
4679 {
4680         /* Create an empty one-dimensional array of variants */
4681         SAFEARRAY *pSA;
4682         SAFEARRAYBOUND dimensions [1];
4683
4684         dimensions [0].lLbound = 0;
4685         dimensions [0].cElements = 0;
4686
4687         pSA= SafeArrayCreate (VT_VARIANT, 1, dimensions);
4688         *safearray = pSA;
4689         return S_OK;
4690 }
4691
4692 LIBTEST_API int STDCALL 
4693 mono_test_marshal_safearray_out_1dim_vt_bstr (SAFEARRAY** safearray)
4694 {
4695         /* Create a one-dimensional array of 10 variants filled with "0" to "9" */
4696         SAFEARRAY *pSA;
4697         SAFEARRAYBOUND dimensions [1];
4698         long i;
4699         gchar buffer [20];
4700         HRESULT hr = S_OK;
4701         long indices [1];
4702
4703         dimensions [0].lLbound = 0;
4704         dimensions [0].cElements = 10;
4705
4706         pSA= SafeArrayCreate (VT_VARIANT, 1, dimensions);
4707         for (i= dimensions [0].lLbound; i< (dimensions [0].cElements + dimensions [0].lLbound); i++) {
4708                 VARIANT vOut;
4709                 VariantInit (&vOut);
4710                 vOut.vt = VT_BSTR;
4711                 _ltoa (i,buffer,10);
4712                 vOut.bstrVal= marshal_bstr_alloc (buffer);
4713                 indices [0] = i;
4714                 if ((hr = SafeArrayPutElement (pSA, indices, &vOut)) != S_OK) {
4715                         VariantClear (&vOut);
4716                         SafeArrayDestroy (pSA);
4717                         return hr;
4718                 }
4719                 VariantClear (&vOut);
4720         }
4721         *safearray = pSA;
4722         return hr;
4723 }
4724
4725 LIBTEST_API int STDCALL 
4726 mono_test_marshal_safearray_out_2dim_vt_i4 (SAFEARRAY** safearray)
4727 {
4728         /* Create a two-dimensional array of 4x3 variants filled with 11, 12, 13, etc. */
4729         SAFEARRAY *pSA;
4730         SAFEARRAYBOUND dimensions [2];
4731         long i, j;
4732         HRESULT hr = S_OK;
4733         long indices [2];
4734
4735         dimensions [0].lLbound = 0;
4736         dimensions [0].cElements = 4;
4737         dimensions [1].lLbound = 0;
4738         dimensions [1].cElements = 3;
4739
4740         pSA= SafeArrayCreate(VT_VARIANT, 2, dimensions);
4741         for (i= dimensions [0].lLbound; i< (dimensions [0].cElements + dimensions [0].lLbound); i++) {
4742                 for (j= dimensions [1].lLbound; j< (dimensions [1].cElements + dimensions [1].lLbound); j++) {
4743                         VARIANT vOut;
4744                         VariantInit (&vOut);
4745                         vOut.vt = VT_I4;
4746                         vOut.lVal = (i+1)*10+(j+1);
4747                         indices [0] = i;
4748                         indices [1] = j;
4749                         if ((hr = SafeArrayPutElement (pSA, indices, &vOut)) != S_OK) {
4750                                 VariantClear (&vOut);
4751                                 SafeArrayDestroy (pSA);
4752                                 return hr;
4753                         }
4754                         VariantClear (&vOut);  // does a deep destroy of source VARIANT 
4755                 }
4756         }
4757         *safearray = pSA;
4758         return hr;
4759 }
4760
4761 LIBTEST_API int STDCALL 
4762 mono_test_marshal_safearray_out_4dim_vt_i4 (SAFEARRAY** safearray)
4763 {
4764         /* Create a four-dimensional array of 10x3x6x7 variants filled with their indices */
4765         /* Also use non zero lower bounds                                                 */
4766         SAFEARRAY *pSA;
4767         SAFEARRAYBOUND dimensions [4];
4768         long i;
4769         HRESULT hr = S_OK;
4770         VARIANT *pData;
4771
4772         dimensions [0].lLbound = 15;
4773         dimensions [0].cElements = 10;
4774         dimensions [1].lLbound = 20;
4775         dimensions [1].cElements = 3;
4776         dimensions [2].lLbound = 5;
4777         dimensions [2].cElements = 6;
4778         dimensions [3].lLbound = 12;
4779         dimensions [3].cElements = 7;
4780
4781         pSA= SafeArrayCreate (VT_VARIANT, 4, dimensions);
4782
4783         SafeArrayAccessData (pSA, (void **)&pData);
4784
4785         for (i= 0; i< 10*3*6*7; i++) {
4786                 VariantInit(&pData [i]);
4787                 pData [i].vt = VT_I4;
4788                 pData [i].lVal = i;
4789         }
4790         SafeArrayUnaccessData (pSA);
4791         *safearray = pSA;
4792         return hr;
4793 }
4794
4795 LIBTEST_API int STDCALL 
4796 mono_test_marshal_safearray_in_byval_1dim_empty (SAFEARRAY* safearray)
4797 {
4798         /* Check that array is one dimensional and empty */
4799
4800         UINT dim;
4801         long lbound, ubound;
4802         
4803         dim = SafeArrayGetDim (safearray);
4804         if (dim != 1)
4805                 return 1;
4806
4807         SafeArrayGetLBound (safearray, 1, &lbound);
4808         SafeArrayGetUBound (safearray, 1, &ubound);
4809
4810         if ((lbound > 0) || (ubound > 0))
4811                 return 1;
4812
4813         return 0;
4814 }
4815
4816 LIBTEST_API int STDCALL 
4817 mono_test_marshal_safearray_in_byval_1dim_vt_i4 (SAFEARRAY* safearray)
4818 {
4819         /* Check that array is one dimensional containing integers from 1 to 10 */
4820
4821         UINT dim;
4822         long lbound, ubound;
4823         VARIANT *pData; 
4824         long i;
4825         int result=0;
4826
4827         dim = SafeArrayGetDim (safearray);
4828         if (dim != 1)
4829                 return 1;
4830
4831         SafeArrayGetLBound (safearray, 1, &lbound);
4832         SafeArrayGetUBound (safearray, 1, &ubound);
4833
4834         if ((lbound != 0) || (ubound != 9))
4835                 return 1;
4836
4837         SafeArrayAccessData (safearray, (void **)&pData);
4838         for (i= lbound; i <= ubound; i++) {
4839                 if ((VariantChangeType (&pData [i], &pData [i], VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) || (pData [i].lVal != i + 1))
4840                         result = 1;
4841         }
4842         SafeArrayUnaccessData (safearray);
4843
4844         return result;
4845 }
4846
4847 LIBTEST_API int STDCALL 
4848 mono_test_marshal_safearray_in_byval_1dim_vt_mixed (SAFEARRAY* safearray)
4849 {
4850         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4851
4852         UINT dim;
4853         long lbound, ubound;
4854         VARIANT *pData; 
4855         long i;
4856         long indices [1];
4857         VARIANT element;
4858         int result=0;
4859
4860         VariantInit (&element);
4861
4862         dim = SafeArrayGetDim (safearray);
4863         if (dim != 1)
4864                 return 1;
4865
4866         SafeArrayGetLBound (safearray, 1, &lbound);
4867         SafeArrayGetUBound (safearray, 1, &ubound);
4868                 
4869         if ((lbound != 0) || (ubound != 12))
4870                 return 1;
4871
4872         SafeArrayAccessData (safearray, (void **)&pData);
4873         for (i= lbound; i <= ubound; i++) {
4874                 if ((i%2 == 0) && (pData [i].vt != VT_I4))
4875                         result = 1;
4876                 if ((i%2 == 1) && (pData [i].vt != VT_BSTR))
4877                         result = 1;
4878                 if ((VariantChangeType (&pData [i], &pData [i], VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) || (pData [i].lVal != i))
4879                         result = 1;
4880         }
4881         SafeArrayUnaccessData (safearray);
4882
4883         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4884
4885         indices [0] = 0;
4886         element.vt = VT_I4;
4887         element.lVal = 333;
4888         SafeArrayPutElement (safearray, indices, &element);
4889         VariantClear (&element);
4890
4891         return result;
4892 }
4893
4894 LIBTEST_API int STDCALL 
4895 mono_test_marshal_safearray_in_byval_2dim_vt_i4 (SAFEARRAY* safearray)
4896 {
4897         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4898
4899         UINT dim;
4900         long lbound1, ubound1, lbound2, ubound2;
4901         long i, j, failed;
4902         long indices [2];
4903         VARIANT element;
4904
4905         VariantInit (&element);
4906
4907         dim = SafeArrayGetDim (safearray);
4908         if (dim != 2)
4909                 return 1;
4910
4911         SafeArrayGetLBound (safearray, 1, &lbound1);
4912         SafeArrayGetUBound (safearray, 1, &ubound1);
4913
4914         if ((lbound1 != 0) || (ubound1 != 1))
4915                 return 1;
4916
4917         SafeArrayGetLBound (safearray, 2, &lbound2);
4918         SafeArrayGetUBound (safearray, 2, &ubound2);
4919
4920         if ((lbound2 != 0) || (ubound2 != 3)) {
4921                 return 1;
4922         }
4923
4924         for (i= lbound1; i <= ubound1; i++) {
4925                 indices [0] = i;
4926                 for (j= lbound2; j <= ubound2; j++) {
4927                         indices [1] = j;
4928                         if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
4929                                 return 1;
4930                         failed = ((element.vt != VT_I4) || (element.lVal != 10*(i+1)+(j+1)));
4931                         VariantClear (&element);
4932                         if (failed)
4933                                 return 1;
4934                 }
4935         }
4936
4937         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4938
4939         indices [0] = 0;
4940         indices [1] = 0;
4941         element.vt = VT_I4;
4942         element.lVal = 333;
4943         SafeArrayPutElement (safearray, indices, &element);
4944         VariantClear (&element);
4945
4946         return 0;
4947 }
4948
4949 LIBTEST_API int STDCALL 
4950 mono_test_marshal_safearray_in_byval_3dim_vt_bstr (SAFEARRAY* safearray)
4951 {
4952         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4953
4954         UINT dim;
4955         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
4956         long i, j, k, failed;
4957         long indices [3];
4958         VARIANT element;
4959
4960         VariantInit (&element);
4961
4962         dim = SafeArrayGetDim (safearray);
4963         if (dim != 3)
4964                 return 1;
4965
4966         SafeArrayGetLBound (safearray, 1, &lbound1);
4967         SafeArrayGetUBound (safearray, 1, &ubound1);
4968
4969         if ((lbound1 != 0) || (ubound1 != 1))
4970                 return 1;
4971
4972         SafeArrayGetLBound (safearray, 2, &lbound2);
4973         SafeArrayGetUBound (safearray, 2, &ubound2);
4974
4975         if ((lbound2 != 0) || (ubound2 != 1))
4976                 return 1;
4977
4978         SafeArrayGetLBound (safearray, 3, &lbound3);
4979         SafeArrayGetUBound (safearray, 3, &ubound3);
4980
4981         if ((lbound3 != 0) || (ubound3 != 2))
4982                 return 1;
4983
4984         for (i= lbound1; i <= ubound1; i++) {
4985                 indices [0] = i;
4986                 for (j= lbound2; j <= ubound2; j++) {
4987                         indices [1] = j;
4988                 for (k= lbound3; k <= ubound3; k++) {
4989                                 indices [2] = k;
4990                                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
4991                                         return 1;
4992                                 failed = ((element.vt != VT_BSTR) 
4993                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
4994                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
4995                                 VariantClear (&element);
4996                                 if (failed)
4997                                         return 1;
4998                         }
4999                 }
5000         }
5001
5002         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
5003
5004         indices [0] = 0;
5005         indices [1] = 0;
5006         indices [2] = 0;
5007         element.vt = VT_BSTR;
5008         element.bstrVal = SysAllocString(L"Should not be copied");
5009         SafeArrayPutElement (safearray, indices, &element);
5010         VariantClear (&element);
5011
5012         return 0;
5013 }
5014
5015 LIBTEST_API int STDCALL 
5016 mono_test_marshal_safearray_in_byref_3dim_vt_bstr (SAFEARRAY** safearray)
5017 {
5018         return mono_test_marshal_safearray_in_byval_3dim_vt_bstr (*safearray);
5019 }
5020
5021 LIBTEST_API int STDCALL 
5022 mono_test_marshal_safearray_in_out_byref_1dim_empty (SAFEARRAY** safearray)
5023 {
5024         /* Check that the input array is what is expected and change it so the caller can check */
5025         /* correct marshalling back to managed code                                             */
5026
5027         UINT dim;
5028         long lbound, ubound;
5029         SAFEARRAYBOUND dimensions [1];
5030         long i;
5031         wchar_t buffer [20];
5032         HRESULT hr = S_OK;
5033         long indices [1];
5034
5035         /* Check that in array is one dimensional and empty */
5036
5037         dim = SafeArrayGetDim (*safearray);
5038         if (dim != 1) {
5039                 return 1;
5040         }
5041
5042         SafeArrayGetLBound (*safearray, 1, &lbound);
5043         SafeArrayGetUBound (*safearray, 1, &ubound);
5044                 
5045         if ((lbound > 0) || (ubound > 0)) {
5046                 return 1;
5047         }
5048
5049         /* Re-dimension the array and return a one-dimensional array of 8 variants filled with "0" to "7" */
5050
5051         dimensions [0].lLbound = 0;
5052         dimensions [0].cElements = 8;
5053
5054         hr = SafeArrayRedim (*safearray, dimensions);
5055         if (hr != S_OK)
5056                 return 1;
5057
5058         for (i= dimensions [0].lLbound; i< (dimensions [0].lLbound + dimensions [0].cElements); i++) {
5059                 VARIANT vOut;
5060                 VariantInit (&vOut);
5061                 vOut.vt = VT_BSTR;
5062                 _ltow (i,buffer,10);
5063                 vOut.bstrVal = SysAllocString (buffer);
5064                 indices [0] = i;
5065                 if ((hr = SafeArrayPutElement (*safearray, indices, &vOut)) != S_OK) {
5066                         VariantClear (&vOut);
5067                         SafeArrayDestroy (*safearray);
5068                         return hr;
5069                 }
5070                 VariantClear (&vOut);
5071         }
5072         return hr;
5073 }
5074
5075 LIBTEST_API int STDCALL 
5076 mono_test_marshal_safearray_in_out_byref_3dim_vt_bstr (SAFEARRAY** safearray)
5077 {
5078         /* Check that the input array is what is expected and change it so the caller can check */
5079         /* correct marshalling back to managed code                                             */
5080
5081         UINT dim;
5082         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
5083         SAFEARRAYBOUND dimensions [1];
5084         long i, j, k, failed;
5085         wchar_t buffer [20];
5086         HRESULT hr = S_OK;
5087         long indices [3];
5088         VARIANT element;
5089
5090         VariantInit (&element);
5091
5092         /* Check that in array is three dimensional and contains the expected values */
5093
5094         dim = SafeArrayGetDim (*safearray);
5095         if (dim != 3)
5096                 return 1;
5097
5098         SafeArrayGetLBound (*safearray, 1, &lbound1);
5099         SafeArrayGetUBound (*safearray, 1, &ubound1);
5100
5101         if ((lbound1 != 0) || (ubound1 != 1))
5102                 return 1;
5103
5104         SafeArrayGetLBound (*safearray, 2, &lbound2);
5105         SafeArrayGetUBound (*safearray, 2, &ubound2);
5106
5107         if ((lbound2 != 0) || (ubound2 != 1))
5108                 return 1;
5109
5110         SafeArrayGetLBound (*safearray, 3, &lbound3);
5111         SafeArrayGetUBound (*safearray, 3, &ubound3);
5112
5113         if ((lbound3 != 0) || (ubound3 != 2))
5114                 return 1;
5115
5116         for (i= lbound1; i <= ubound1; i++) {
5117                 indices [0] = i;
5118                 for (j= lbound2; j <= ubound2; j++) {
5119                         indices [1] = j;
5120                         for (k= lbound3; k <= ubound3; k++) {
5121                                 indices [2] = k;
5122                                 if (SafeArrayGetElement (*safearray, indices, &element) != S_OK)
5123                                         return 1;
5124                                 failed = ((element.vt != VT_BSTR) 
5125                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
5126                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
5127                                 VariantClear (&element);
5128                                 if (failed)
5129                                         return 1;
5130                         }
5131                 }
5132         }
5133
5134         hr = SafeArrayDestroy (*safearray);
5135         if (hr != S_OK)
5136                 return 1;
5137
5138         /* Return a new one-dimensional array of 8 variants filled with "0" to "7" */
5139
5140         dimensions [0].lLbound = 0;
5141         dimensions [0].cElements = 8;
5142
5143         *safearray = SafeArrayCreate (VT_VARIANT, 1, dimensions);
5144
5145         for (i= dimensions [0].lLbound; i< (dimensions [0].lLbound + dimensions [0].cElements); i++) {
5146                 VARIANT vOut;
5147                 VariantInit (&vOut);
5148                 vOut.vt = VT_BSTR;
5149                 _ltow (i,buffer,10);
5150                 vOut.bstrVal = SysAllocString (buffer);
5151                 indices [0] = i;
5152                 if ((hr = SafeArrayPutElement (*safearray, indices, &vOut)) != S_OK) {
5153                         VariantClear (&vOut);
5154                         SafeArrayDestroy (*safearray);
5155                         return hr;
5156                 }
5157                 VariantClear (&vOut);
5158         }
5159         return hr;
5160 }
5161
5162 LIBTEST_API int STDCALL 
5163 mono_test_marshal_safearray_in_out_byref_1dim_vt_i4 (SAFEARRAY** safearray)
5164 {
5165         /* Check that the input array is what is expected and change it so the caller can check */
5166         /* correct marshalling back to managed code                                             */
5167
5168         UINT dim;
5169         long lbound1, ubound1;
5170         long i, failed;
5171         HRESULT hr = S_OK;
5172         long indices [1];
5173         VARIANT element;
5174         
5175         VariantInit (&element);
5176
5177         /* Check that in array is one dimensional and contains the expected value */
5178
5179         dim = SafeArrayGetDim (*safearray);
5180         if (dim != 1)
5181                 return 1;
5182
5183         SafeArrayGetLBound (*safearray, 1, &lbound1);
5184         SafeArrayGetUBound (*safearray, 1, &ubound1);
5185
5186         ubound1 = 1;
5187         if ((lbound1 != 0) || (ubound1 != 1))
5188                 return 1;
5189         ubound1 = 0;
5190
5191         for (i= lbound1; i <= ubound1; i++) {
5192                 indices [0] = i;
5193                 if (SafeArrayGetElement (*safearray, indices, &element) != S_OK)
5194                         return 1;
5195                 failed = (element.vt != VT_I4) || (element.lVal != i+1);
5196                 VariantClear (&element);
5197                 if (failed)
5198                         return 1;
5199         }
5200
5201         /* Change one of the elements of the array to verify that [out] parameter is marshalled back to the managed side */
5202
5203         indices [0] = 0;
5204         element.vt = VT_I4;
5205         element.lVal = -1;
5206         SafeArrayPutElement (*safearray, indices, &element);
5207         VariantClear (&element);
5208
5209         return hr;
5210 }
5211
5212 LIBTEST_API int STDCALL 
5213 mono_test_marshal_safearray_in_out_byval_1dim_vt_i4 (SAFEARRAY* safearray)
5214 {
5215         /* Check that the input array is what is expected and change it so the caller can check */
5216         /* correct marshalling back to managed code                                             */
5217
5218         UINT dim;
5219         long lbound1, ubound1;
5220         SAFEARRAYBOUND dimensions [1];
5221         long i, failed;
5222         HRESULT hr = S_OK;
5223         long indices [1];
5224         VARIANT element;
5225
5226         VariantInit (&element);
5227
5228         /* Check that in array is one dimensional and contains the expected value */
5229
5230         dim = SafeArrayGetDim (safearray);
5231         if (dim != 1)
5232                 return 1;
5233
5234         SafeArrayGetLBound (safearray, 1, &lbound1);
5235         SafeArrayGetUBound (safearray, 1, &ubound1);
5236                 
5237         if ((lbound1 != 0) || (ubound1 != 0))
5238                 return 1;
5239
5240         for (i= lbound1; i <= ubound1; i++) {
5241                 indices [0] = i;
5242                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
5243                         return 1;
5244                 failed = (element.vt != VT_I4) || (element.lVal != i+1);
5245                 VariantClear (&element);
5246                 if (failed)
5247                         return 1;
5248         }
5249
5250         /* Change the array to verify how [out] parameter is marshalled back to the managed side */
5251
5252         /* Redimension the array */
5253         dimensions [0].lLbound = lbound1;
5254         dimensions [0].cElements = 2;
5255         hr = SafeArrayRedim(safearray, dimensions);
5256
5257         indices [0] = 0;
5258         element.vt = VT_I4;
5259         element.lVal = 12345;
5260         SafeArrayPutElement (safearray, indices, &element);
5261         VariantClear (&element);
5262
5263         indices [0] = 1;
5264         element.vt = VT_I4;
5265         element.lVal = -12345;
5266         SafeArrayPutElement (safearray, indices, &element);
5267         VariantClear (&element);
5268
5269         return hr;
5270 }
5271
5272 LIBTEST_API int STDCALL 
5273 mono_test_marshal_safearray_in_out_byval_3dim_vt_bstr (SAFEARRAY* safearray)
5274 {
5275         /* Check that the input array is what is expected and change it so the caller can check */
5276         /* correct marshalling back to managed code                                             */
5277
5278         UINT dim;
5279         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
5280         long i, j, k, failed;
5281         HRESULT hr = S_OK;
5282         long indices [3];
5283         VARIANT element;
5284
5285         VariantInit (&element);
5286
5287         /* Check that in array is three dimensional and contains the expected values */
5288
5289         dim = SafeArrayGetDim (safearray);
5290         if (dim != 3)
5291                 return 1;
5292
5293         SafeArrayGetLBound (safearray, 1, &lbound1);
5294         SafeArrayGetUBound (safearray, 1, &ubound1);
5295
5296         if ((lbound1 != 0) || (ubound1 != 1))
5297                 return 1;
5298
5299         SafeArrayGetLBound (safearray, 2, &lbound2);
5300         SafeArrayGetUBound (safearray, 2, &ubound2);
5301
5302         if ((lbound2 != 0) || (ubound2 != 1))
5303                 return 1;
5304
5305         SafeArrayGetLBound (safearray, 3, &lbound3);
5306         SafeArrayGetUBound (safearray, 3, &ubound3);
5307
5308         if ((lbound3 != 0) || (ubound3 != 2))
5309                 return 1;
5310
5311         for (i= lbound1; i <= ubound1; i++) {
5312                 indices [0] = i;
5313                 for (j= lbound2; j <= ubound2; j++) {
5314                         indices [1] = j;
5315                         for (k= lbound3; k <= ubound3; k++) {
5316                                 indices [2] = k;
5317                                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
5318                                         return 1;
5319                                 failed = ((element.vt != VT_BSTR) 
5320                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
5321                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
5322                                 VariantClear (&element);
5323                                 if (failed)
5324                                         return 1;
5325                         }
5326                 }
5327         }
5328
5329         /* Change the elements of the array to verify that [out] parameter is marshalled back to the managed side */
5330
5331         indices [0] = 1;
5332         indices [1] = 1;
5333         indices [2] = 2;
5334         element.vt = VT_I4;
5335         element.lVal = 333;
5336         SafeArrayPutElement (safearray, indices, &element);
5337         VariantClear (&element);
5338
5339         indices [0] = 1;
5340         indices [1] = 1;
5341         indices [2] = 1;
5342         element.vt = VT_I4;
5343         element.lVal = 111;
5344         SafeArrayPutElement (safearray, indices, &element);
5345         VariantClear (&element);
5346
5347         indices [0] = 0;
5348         indices [1] = 1;
5349         indices [2] = 0;
5350         element.vt = VT_BSTR;
5351         element.bstrVal = marshal_bstr_alloc("ABCDEFG");
5352         SafeArrayPutElement (safearray, indices, &element);
5353         VariantClear (&element);
5354
5355         return hr;
5356 }
5357
5358 LIBTEST_API int STDCALL 
5359 mono_test_marshal_safearray_mixed(
5360                 SAFEARRAY  *safearray1,
5361                 SAFEARRAY **safearray2,
5362                 SAFEARRAY  *safearray3,
5363                 SAFEARRAY **safearray4
5364                 )
5365 {
5366         HRESULT hr = S_OK;
5367
5368         /* Initialize out parameters */
5369         *safearray2 = NULL;
5370
5371         /* array1: Check that in array is one dimensional and contains the expected value */
5372         hr = mono_test_marshal_safearray_in_out_byval_1dim_vt_i4 (safearray1);
5373
5374         /* array2: Fill in with some values to check on the managed side */
5375         if (hr == S_OK)
5376                 hr = mono_test_marshal_safearray_out_1dim_vt_bstr (safearray2);
5377
5378         /* array3: Check that in array is one dimensional and contains the expected value */
5379         if (hr == S_OK)
5380                 hr = mono_test_marshal_safearray_in_byval_1dim_vt_mixed(safearray3);
5381
5382         /* array4: Check input values and fill in with some values to check on the managed side */
5383         if (hr == S_OK)
5384                 hr = mono_test_marshal_safearray_in_out_byref_3dim_vt_bstr(safearray4);
5385
5386         return hr;
5387 }
5388
5389 #endif
5390
5391 static int call_managed_res;
5392
5393 static void
5394 call_managed (gpointer arg)
5395 {
5396         SimpleDelegate del = arg;
5397
5398         call_managed_res = del (42);
5399 }
5400
5401 LIBTEST_API int STDCALL 
5402 mono_test_marshal_thread_attach (SimpleDelegate del)
5403 {
5404 #ifdef WIN32
5405         return 43;
5406 #else
5407         int res;
5408         pthread_t t;
5409
5410         res = pthread_create (&t, NULL, (gpointer)call_managed, del);
5411         g_assert (res == 0);
5412         pthread_join (t, NULL);
5413
5414         return call_managed_res;
5415 #endif
5416 }
5417
5418 typedef int (STDCALL *Callback) (void);
5419
5420 static Callback callback;
5421
5422 LIBTEST_API void STDCALL 
5423 mono_test_marshal_set_callback (Callback cb)
5424 {
5425         callback = cb;
5426 }
5427
5428 LIBTEST_API int STDCALL 
5429 mono_test_marshal_call_callback (void)
5430 {
5431         return callback ();
5432 }
5433
5434 LIBTEST_API int STDCALL
5435 mono_test_marshal_lpstr (char *str)
5436 {
5437         return strcmp ("ABC", str);
5438 }
5439
5440 LIBTEST_API int STDCALL
5441 mono_test_marshal_lpwstr (gunichar2 *str)
5442 {
5443         char *s;
5444         int res;
5445
5446         s = g_utf16_to_utf8 (str, -1, NULL, NULL, NULL);
5447         res = strcmp ("ABC", s);
5448         g_free (s);
5449
5450         return res;
5451 }
5452
5453 LIBTEST_API char* STDCALL
5454 mono_test_marshal_return_lpstr (void)
5455 {
5456         char *res = marshal_alloc (4);
5457         strcpy (res, "XYZ");
5458         return res;
5459 }
5460
5461
5462 LIBTEST_API gunichar2* STDCALL
5463 mono_test_marshal_return_lpwstr (void)
5464 {
5465         gunichar2 *res = marshal_alloc (8);
5466         gunichar2* tmp = g_utf8_to_utf16 ("XYZ", -1, NULL, NULL, NULL);
5467
5468         memcpy (res, tmp, 8);
5469         g_free (tmp);
5470
5471         return res;
5472 }
5473
5474 typedef struct {
5475         double d;
5476 } SingleDoubleStruct;
5477
5478 LIBTEST_API SingleDoubleStruct STDCALL
5479 mono_test_marshal_return_single_double_struct (void)
5480 {
5481         SingleDoubleStruct res;
5482
5483         res.d = 3.0;
5484
5485         return res;
5486 }
5487
5488
5489 #ifndef TARGET_X86
5490
5491 LIBTEST_API int STDCALL
5492 mono_test_has_thiscall (void)
5493 {
5494         return 1;
5495 }
5496
5497 LIBTEST_API int
5498 _mono_test_native_thiscall1 (int arg)
5499 {
5500         return arg;
5501 }
5502
5503 LIBTEST_API int
5504 _mono_test_native_thiscall2 (int arg, int arg2)
5505 {
5506         return arg + (arg2^1);
5507 }
5508
5509 LIBTEST_API int
5510 _mono_test_native_thiscall3 (int arg, int arg2, int arg3)
5511 {
5512         return arg + (arg2^1) + (arg3^2);
5513 }
5514
5515 #elif defined(__GNUC__)
5516
5517 LIBTEST_API int STDCALL
5518 mono_test_has_thiscall (void)
5519 {
5520         return 1;
5521 }
5522
5523 #define def_asm_fn(name) \
5524         "\t.align 4\n" \
5525         "\t.globl _" #name "\n" \
5526         "_" #name ":\n" \
5527         "\t.globl __" #name "\n" \
5528         "__" #name ":\n"
5529
5530 asm(".text\n"
5531
5532 def_asm_fn(mono_test_native_thiscall1)
5533 "\tmovl %ecx,%eax\n"
5534 "\tret\n"
5535
5536 def_asm_fn(mono_test_native_thiscall2)
5537 "\tmovl %ecx,%eax\n"
5538 "\tmovl 4(%esp),%ecx\n"
5539 "\txorl $1,%ecx\n"
5540 "\taddl %ecx,%eax\n"
5541 "\tret $4\n"
5542
5543 def_asm_fn(mono_test_native_thiscall3)
5544 "\tmovl %ecx,%eax\n"
5545 "\tmovl 4(%esp),%ecx\n"
5546 "\txorl $1,%ecx\n"
5547 "\taddl %ecx,%eax\n"
5548 "\tmovl 8(%esp),%ecx\n"
5549 "\txorl $2,%ecx\n"
5550 "\taddl %ecx,%eax\n"
5551 "\tret $8\n"
5552
5553 );
5554
5555 #else
5556
5557 LIBTEST_API int STDCALL
5558 mono_test_has_thiscall (void)
5559 {
5560         return 0;
5561 }
5562
5563 #endif
5564
5565
5566 typedef struct {
5567         char f1;
5568 } sbyte1;
5569
5570 LIBTEST_API sbyte1 STDCALL
5571 mono_return_sbyte1 (sbyte1 s1, int addend) {
5572         if (s1.f1 != 1) {
5573                 fprintf(stderr, "mono_return_sbyte1 s1.f1: got %d but expected %d\n", s1.f1, 1);
5574         }
5575         s1.f1+=addend; 
5576         return s1;
5577 }
5578
5579 typedef struct {
5580         char f1,f2;
5581 } sbyte2;
5582
5583 LIBTEST_API sbyte2 STDCALL
5584 mono_return_sbyte2 (sbyte2 s2, int addend) {
5585         if (s2.f1 != 1) {
5586                 fprintf(stderr, "mono_return_sbyte2 s2.f1: got %d but expected %d\n", s2.f1, 1);
5587         }
5588         if (s2.f2 != 2) {
5589                 fprintf(stderr, "mono_return_sbyte2 s2.f2: got %d but expected %d\n", s2.f2, 2);
5590         }
5591         s2.f1+=addend; s2.f2+=addend; 
5592         return s2;
5593 }
5594
5595 typedef struct {
5596         char f1,f2,f3;
5597 } sbyte3;
5598
5599 LIBTEST_API sbyte3 STDCALL
5600 mono_return_sbyte3 (sbyte3 s3, int addend) {
5601         if (s3.f1 != 1) {
5602                 fprintf(stderr, "mono_return_sbyte3 s3.f1: got %d but expected %d\n", s3.f1, 1);
5603         }
5604         if (s3.f2 != 2) {
5605                 fprintf(stderr, "mono_return_sbyte3 s3.f2: got %d but expected %d\n", s3.f2, 2);
5606         }
5607         if (s3.f3 != 3) {
5608                 fprintf(stderr, "mono_return_sbyte3 s3.f3: got %d but expected %d\n", s3.f3, 3);
5609         }
5610         s3.f1+=addend; s3.f2+=addend; s3.f3+=addend; 
5611         return s3;
5612 }
5613
5614 typedef struct {
5615         char f1,f2,f3,f4;
5616 } sbyte4;
5617
5618 LIBTEST_API sbyte4 STDCALL
5619 mono_return_sbyte4 (sbyte4 s4, int addend) {
5620         if (s4.f1 != 1) {
5621                 fprintf(stderr, "mono_return_sbyte4 s4.f1: got %d but expected %d\n", s4.f1, 1);
5622         }
5623         if (s4.f2 != 2) {
5624                 fprintf(stderr, "mono_return_sbyte4 s4.f2: got %d but expected %d\n", s4.f2, 2);
5625         }
5626         if (s4.f3 != 3) {
5627                 fprintf(stderr, "mono_return_sbyte4 s4.f3: got %d but expected %d\n", s4.f3, 3);
5628         }
5629         if (s4.f4 != 4) {
5630                 fprintf(stderr, "mono_return_sbyte4 s4.f4: got %d but expected %d\n", s4.f4, 4);
5631         }
5632         s4.f1+=addend; s4.f2+=addend; s4.f3+=addend; s4.f4+=addend; 
5633         return s4;
5634 }
5635
5636 typedef struct {
5637         char f1,f2,f3,f4,f5;
5638 } sbyte5;
5639
5640 LIBTEST_API sbyte5 STDCALL
5641 mono_return_sbyte5 (sbyte5 s5, int addend) {
5642         if (s5.f1 != 1) {
5643                 fprintf(stderr, "mono_return_sbyte5 s5.f1: got %d but expected %d\n", s5.f1, 1);
5644         }
5645         if (s5.f2 != 2) {
5646                 fprintf(stderr, "mono_return_sbyte5 s5.f2: got %d but expected %d\n", s5.f2, 2);
5647         }
5648         if (s5.f3 != 3) {
5649                 fprintf(stderr, "mono_return_sbyte5 s5.f3: got %d but expected %d\n", s5.f3, 3);
5650         }
5651         if (s5.f4 != 4) {
5652                 fprintf(stderr, "mono_return_sbyte5 s5.f4: got %d but expected %d\n", s5.f4, 4);
5653         }
5654         if (s5.f5 != 5) {
5655                 fprintf(stderr, "mono_return_sbyte5 s5.f5: got %d but expected %d\n", s5.f5, 5);
5656         }
5657         s5.f1+=addend; s5.f2+=addend; s5.f3+=addend; s5.f4+=addend; s5.f5+=addend; 
5658         return s5;
5659 }
5660
5661 typedef struct {
5662         char f1,f2,f3,f4,f5,f6;
5663 } sbyte6;
5664
5665 LIBTEST_API sbyte6 STDCALL
5666 mono_return_sbyte6 (sbyte6 s6, int addend) {
5667         if (s6.f1 != 1) {
5668                 fprintf(stderr, "mono_return_sbyte6 s6.f1: got %d but expected %d\n", s6.f1, 1);
5669         }
5670         if (s6.f2 != 2) {
5671                 fprintf(stderr, "mono_return_sbyte6 s6.f2: got %d but expected %d\n", s6.f2, 2);
5672         }
5673         if (s6.f3 != 3) {
5674                 fprintf(stderr, "mono_return_sbyte6 s6.f3: got %d but expected %d\n", s6.f3, 3);
5675         }
5676         if (s6.f4 != 4) {
5677                 fprintf(stderr, "mono_return_sbyte6 s6.f4: got %d but expected %d\n", s6.f4, 4);
5678         }
5679         if (s6.f5 != 5) {
5680                 fprintf(stderr, "mono_return_sbyte6 s6.f5: got %d but expected %d\n", s6.f5, 5);
5681         }
5682         if (s6.f6 != 6) {
5683                 fprintf(stderr, "mono_return_sbyte6 s6.f6: got %d but expected %d\n", s6.f6, 6);
5684         }
5685         s6.f1+=addend; s6.f2+=addend; s6.f3+=addend; s6.f4+=addend; s6.f5+=addend; s6.f6+=addend; 
5686         return s6;
5687 }
5688
5689 typedef struct {
5690         char f1,f2,f3,f4,f5,f6,f7;
5691 } sbyte7;
5692
5693 LIBTEST_API sbyte7 STDCALL
5694 mono_return_sbyte7 (sbyte7 s7, int addend) {
5695         if (s7.f1 != 1) {
5696                 fprintf(stderr, "mono_return_sbyte7 s7.f1: got %d but expected %d\n", s7.f1, 1);
5697         }
5698         if (s7.f2 != 2) {
5699                 fprintf(stderr, "mono_return_sbyte7 s7.f2: got %d but expected %d\n", s7.f2, 2);
5700         }
5701         if (s7.f3 != 3) {
5702                 fprintf(stderr, "mono_return_sbyte7 s7.f3: got %d but expected %d\n", s7.f3, 3);
5703         }
5704         if (s7.f4 != 4) {
5705                 fprintf(stderr, "mono_return_sbyte7 s7.f4: got %d but expected %d\n", s7.f4, 4);
5706         }
5707         if (s7.f5 != 5) {
5708                 fprintf(stderr, "mono_return_sbyte7 s7.f5: got %d but expected %d\n", s7.f5, 5);
5709         }
5710         if (s7.f6 != 6) {
5711                 fprintf(stderr, "mono_return_sbyte7 s7.f6: got %d but expected %d\n", s7.f6, 6);
5712         }
5713         if (s7.f7 != 7) {
5714                 fprintf(stderr, "mono_return_sbyte7 s7.f7: got %d but expected %d\n", s7.f7, 7);
5715         }
5716         s7.f1+=addend; s7.f2+=addend; s7.f3+=addend; s7.f4+=addend; s7.f5+=addend; s7.f6+=addend; s7.f7+=addend; 
5717         return s7;
5718 }
5719
5720 typedef struct {
5721         char f1,f2,f3,f4,f5,f6,f7,f8;
5722 } sbyte8;
5723
5724 LIBTEST_API sbyte8 STDCALL
5725 mono_return_sbyte8 (sbyte8 s8, int addend) {
5726         if (s8.f1 != 1) {
5727                 fprintf(stderr, "mono_return_sbyte8 s8.f1: got %d but expected %d\n", s8.f1, 1);
5728         }
5729         if (s8.f2 != 2) {
5730                 fprintf(stderr, "mono_return_sbyte8 s8.f2: got %d but expected %d\n", s8.f2, 2);
5731         }
5732         if (s8.f3 != 3) {
5733                 fprintf(stderr, "mono_return_sbyte8 s8.f3: got %d but expected %d\n", s8.f3, 3);
5734         }
5735         if (s8.f4 != 4) {
5736                 fprintf(stderr, "mono_return_sbyte8 s8.f4: got %d but expected %d\n", s8.f4, 4);
5737         }
5738         if (s8.f5 != 5) {
5739                 fprintf(stderr, "mono_return_sbyte8 s8.f5: got %d but expected %d\n", s8.f5, 5);
5740         }
5741         if (s8.f6 != 6) {
5742                 fprintf(stderr, "mono_return_sbyte8 s8.f6: got %d but expected %d\n", s8.f6, 6);
5743         }
5744         if (s8.f7 != 7) {
5745                 fprintf(stderr, "mono_return_sbyte8 s8.f7: got %d but expected %d\n", s8.f7, 7);
5746         }
5747         if (s8.f8 != 8) {
5748                 fprintf(stderr, "mono_return_sbyte8 s8.f8: got %d but expected %d\n", s8.f8, 8);
5749         }
5750         s8.f1+=addend; s8.f2+=addend; s8.f3+=addend; s8.f4+=addend; s8.f5+=addend; s8.f6+=addend; s8.f7+=addend; s8.f8+=addend; 
5751         return s8;
5752 }
5753
5754 typedef struct {
5755         char f1,f2,f3,f4,f5,f6,f7,f8,f9;
5756 } sbyte9;
5757
5758 LIBTEST_API sbyte9 STDCALL
5759 mono_return_sbyte9 (sbyte9 s9, int addend) {
5760         if (s9.f1 != 1) {
5761                 fprintf(stderr, "mono_return_sbyte9 s9.f1: got %d but expected %d\n", s9.f1, 1);
5762         }
5763         if (s9.f2 != 2) {
5764                 fprintf(stderr, "mono_return_sbyte9 s9.f2: got %d but expected %d\n", s9.f2, 2);
5765         }
5766         if (s9.f3 != 3) {
5767                 fprintf(stderr, "mono_return_sbyte9 s9.f3: got %d but expected %d\n", s9.f3, 3);
5768         }
5769         if (s9.f4 != 4) {
5770                 fprintf(stderr, "mono_return_sbyte9 s9.f4: got %d but expected %d\n", s9.f4, 4);
5771         }
5772         if (s9.f5 != 5) {
5773                 fprintf(stderr, "mono_return_sbyte9 s9.f5: got %d but expected %d\n", s9.f5, 5);
5774         }
5775         if (s9.f6 != 6) {
5776                 fprintf(stderr, "mono_return_sbyte9 s9.f6: got %d but expected %d\n", s9.f6, 6);
5777         }
5778         if (s9.f7 != 7) {
5779                 fprintf(stderr, "mono_return_sbyte9 s9.f7: got %d but expected %d\n", s9.f7, 7);
5780         }
5781         if (s9.f8 != 8) {
5782                 fprintf(stderr, "mono_return_sbyte9 s9.f8: got %d but expected %d\n", s9.f8, 8);
5783         }
5784         if (s9.f9 != 9) {
5785                 fprintf(stderr, "mono_return_sbyte9 s9.f9: got %d but expected %d\n", s9.f9, 9);
5786         }
5787         s9.f1+=addend; s9.f2+=addend; s9.f3+=addend; s9.f4+=addend; s9.f5+=addend; s9.f6+=addend; s9.f7+=addend; s9.f8+=addend; s9.f9+=addend; 
5788         return s9;
5789 }
5790
5791 typedef struct {
5792         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10;
5793 } sbyte10;
5794
5795 LIBTEST_API sbyte10 STDCALL
5796 mono_return_sbyte10 (sbyte10 s10, int addend) {
5797         if (s10.f1 != 1) {
5798                 fprintf(stderr, "mono_return_sbyte10 s10.f1: got %d but expected %d\n", s10.f1, 1);
5799         }
5800         if (s10.f2 != 2) {
5801                 fprintf(stderr, "mono_return_sbyte10 s10.f2: got %d but expected %d\n", s10.f2, 2);
5802         }
5803         if (s10.f3 != 3) {
5804                 fprintf(stderr, "mono_return_sbyte10 s10.f3: got %d but expected %d\n", s10.f3, 3);
5805         }
5806         if (s10.f4 != 4) {
5807                 fprintf(stderr, "mono_return_sbyte10 s10.f4: got %d but expected %d\n", s10.f4, 4);
5808         }
5809         if (s10.f5 != 5) {
5810                 fprintf(stderr, "mono_return_sbyte10 s10.f5: got %d but expected %d\n", s10.f5, 5);
5811         }
5812         if (s10.f6 != 6) {
5813                 fprintf(stderr, "mono_return_sbyte10 s10.f6: got %d but expected %d\n", s10.f6, 6);
5814         }
5815         if (s10.f7 != 7) {
5816                 fprintf(stderr, "mono_return_sbyte10 s10.f7: got %d but expected %d\n", s10.f7, 7);
5817         }
5818         if (s10.f8 != 8) {
5819                 fprintf(stderr, "mono_return_sbyte10 s10.f8: got %d but expected %d\n", s10.f8, 8);
5820         }
5821         if (s10.f9 != 9) {
5822                 fprintf(stderr, "mono_return_sbyte10 s10.f9: got %d but expected %d\n", s10.f9, 9);
5823         }
5824         if (s10.f10 != 10) {
5825                 fprintf(stderr, "mono_return_sbyte10 s10.f10: got %d but expected %d\n", s10.f10, 10);
5826         }
5827         s10.f1+=addend; s10.f2+=addend; s10.f3+=addend; s10.f4+=addend; s10.f5+=addend; s10.f6+=addend; s10.f7+=addend; s10.f8+=addend; s10.f9+=addend; s10.f10+=addend; 
5828         return s10;
5829 }
5830
5831 typedef struct {
5832         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11;
5833 } sbyte11;
5834
5835 LIBTEST_API sbyte11 STDCALL
5836 mono_return_sbyte11 (sbyte11 s11, int addend) {
5837         if (s11.f1 != 1) {
5838                 fprintf(stderr, "mono_return_sbyte11 s11.f1: got %d but expected %d\n", s11.f1, 1);
5839         }
5840         if (s11.f2 != 2) {
5841                 fprintf(stderr, "mono_return_sbyte11 s11.f2: got %d but expected %d\n", s11.f2, 2);
5842         }
5843         if (s11.f3 != 3) {
5844                 fprintf(stderr, "mono_return_sbyte11 s11.f3: got %d but expected %d\n", s11.f3, 3);
5845         }
5846         if (s11.f4 != 4) {
5847                 fprintf(stderr, "mono_return_sbyte11 s11.f4: got %d but expected %d\n", s11.f4, 4);
5848         }
5849         if (s11.f5 != 5) {
5850                 fprintf(stderr, "mono_return_sbyte11 s11.f5: got %d but expected %d\n", s11.f5, 5);
5851         }
5852         if (s11.f6 != 6) {
5853                 fprintf(stderr, "mono_return_sbyte11 s11.f6: got %d but expected %d\n", s11.f6, 6);
5854         }
5855         if (s11.f7 != 7) {
5856                 fprintf(stderr, "mono_return_sbyte11 s11.f7: got %d but expected %d\n", s11.f7, 7);
5857         }
5858         if (s11.f8 != 8) {
5859                 fprintf(stderr, "mono_return_sbyte11 s11.f8: got %d but expected %d\n", s11.f8, 8);
5860         }
5861         if (s11.f9 != 9) {
5862                 fprintf(stderr, "mono_return_sbyte11 s11.f9: got %d but expected %d\n", s11.f9, 9);
5863         }
5864         if (s11.f10 != 10) {
5865                 fprintf(stderr, "mono_return_sbyte11 s11.f10: got %d but expected %d\n", s11.f10, 10);
5866         }
5867         if (s11.f11 != 11) {
5868                 fprintf(stderr, "mono_return_sbyte11 s11.f11: got %d but expected %d\n", s11.f11, 11);
5869         }
5870         s11.f1+=addend; s11.f2+=addend; s11.f3+=addend; s11.f4+=addend; s11.f5+=addend; s11.f6+=addend; s11.f7+=addend; s11.f8+=addend; s11.f9+=addend; s11.f10+=addend; s11.f11+=addend; 
5871         return s11;
5872 }
5873
5874 typedef struct {
5875         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12;
5876 } sbyte12;
5877
5878 LIBTEST_API sbyte12 STDCALL
5879 mono_return_sbyte12 (sbyte12 s12, int addend) {
5880         if (s12.f1 != 1) {
5881                 fprintf(stderr, "mono_return_sbyte12 s12.f1: got %d but expected %d\n", s12.f1, 1);
5882         }
5883         if (s12.f2 != 2) {
5884                 fprintf(stderr, "mono_return_sbyte12 s12.f2: got %d but expected %d\n", s12.f2, 2);
5885         }
5886         if (s12.f3 != 3) {
5887                 fprintf(stderr, "mono_return_sbyte12 s12.f3: got %d but expected %d\n", s12.f3, 3);
5888         }
5889         if (s12.f4 != 4) {
5890                 fprintf(stderr, "mono_return_sbyte12 s12.f4: got %d but expected %d\n", s12.f4, 4);
5891         }
5892         if (s12.f5 != 5) {
5893                 fprintf(stderr, "mono_return_sbyte12 s12.f5: got %d but expected %d\n", s12.f5, 5);
5894         }
5895         if (s12.f6 != 6) {
5896                 fprintf(stderr, "mono_return_sbyte12 s12.f6: got %d but expected %d\n", s12.f6, 6);
5897         }
5898         if (s12.f7 != 7) {
5899                 fprintf(stderr, "mono_return_sbyte12 s12.f7: got %d but expected %d\n", s12.f7, 7);
5900         }
5901         if (s12.f8 != 8) {
5902                 fprintf(stderr, "mono_return_sbyte12 s12.f8: got %d but expected %d\n", s12.f8, 8);
5903         }
5904         if (s12.f9 != 9) {
5905                 fprintf(stderr, "mono_return_sbyte12 s12.f9: got %d but expected %d\n", s12.f9, 9);
5906         }
5907         if (s12.f10 != 10) {
5908                 fprintf(stderr, "mono_return_sbyte12 s12.f10: got %d but expected %d\n", s12.f10, 10);
5909         }
5910         if (s12.f11 != 11) {
5911                 fprintf(stderr, "mono_return_sbyte12 s12.f11: got %d but expected %d\n", s12.f11, 11);
5912         }
5913         if (s12.f12 != 12) {
5914                 fprintf(stderr, "mono_return_sbyte12 s12.f12: got %d but expected %d\n", s12.f12, 12);
5915         }
5916         s12.f1+=addend; s12.f2+=addend; s12.f3+=addend; s12.f4+=addend; s12.f5+=addend; s12.f6+=addend; s12.f7+=addend; s12.f8+=addend; s12.f9+=addend; s12.f10+=addend; s12.f11+=addend; s12.f12+=addend; 
5917         return s12;
5918 }
5919
5920 typedef struct {
5921         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13;
5922 } sbyte13;
5923
5924 LIBTEST_API sbyte13 STDCALL
5925 mono_return_sbyte13 (sbyte13 s13, int addend) {
5926         if (s13.f1 != 1) {
5927                 fprintf(stderr, "mono_return_sbyte13 s13.f1: got %d but expected %d\n", s13.f1, 1);
5928         }
5929         if (s13.f2 != 2) {
5930                 fprintf(stderr, "mono_return_sbyte13 s13.f2: got %d but expected %d\n", s13.f2, 2);
5931         }
5932         if (s13.f3 != 3) {
5933                 fprintf(stderr, "mono_return_sbyte13 s13.f3: got %d but expected %d\n", s13.f3, 3);
5934         }
5935         if (s13.f4 != 4) {
5936                 fprintf(stderr, "mono_return_sbyte13 s13.f4: got %d but expected %d\n", s13.f4, 4);
5937         }
5938         if (s13.f5 != 5) {
5939                 fprintf(stderr, "mono_return_sbyte13 s13.f5: got %d but expected %d\n", s13.f5, 5);
5940         }
5941         if (s13.f6 != 6) {
5942                 fprintf(stderr, "mono_return_sbyte13 s13.f6: got %d but expected %d\n", s13.f6, 6);
5943         }
5944         if (s13.f7 != 7) {
5945                 fprintf(stderr, "mono_return_sbyte13 s13.f7: got %d but expected %d\n", s13.f7, 7);
5946         }
5947         if (s13.f8 != 8) {
5948                 fprintf(stderr, "mono_return_sbyte13 s13.f8: got %d but expected %d\n", s13.f8, 8);
5949         }
5950         if (s13.f9 != 9) {
5951                 fprintf(stderr, "mono_return_sbyte13 s13.f9: got %d but expected %d\n", s13.f9, 9);
5952         }
5953         if (s13.f10 != 10) {
5954                 fprintf(stderr, "mono_return_sbyte13 s13.f10: got %d but expected %d\n", s13.f10, 10);
5955         }
5956         if (s13.f11 != 11) {
5957                 fprintf(stderr, "mono_return_sbyte13 s13.f11: got %d but expected %d\n", s13.f11, 11);
5958         }
5959         if (s13.f12 != 12) {
5960                 fprintf(stderr, "mono_return_sbyte13 s13.f12: got %d but expected %d\n", s13.f12, 12);
5961         }
5962         if (s13.f13 != 13) {
5963                 fprintf(stderr, "mono_return_sbyte13 s13.f13: got %d but expected %d\n", s13.f13, 13);
5964         }
5965         s13.f1+=addend; s13.f2+=addend; s13.f3+=addend; s13.f4+=addend; s13.f5+=addend; s13.f6+=addend; s13.f7+=addend; s13.f8+=addend; s13.f9+=addend; s13.f10+=addend; s13.f11+=addend; s13.f12+=addend; s13.f13+=addend; 
5966         return s13;
5967 }
5968
5969 typedef struct {
5970         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14;
5971 } sbyte14;
5972
5973 LIBTEST_API sbyte14 STDCALL
5974 mono_return_sbyte14 (sbyte14 s14, int addend) {
5975         if (s14.f1 != 1) {
5976                 fprintf(stderr, "mono_return_sbyte14 s14.f1: got %d but expected %d\n", s14.f1, 1);
5977         }
5978         if (s14.f2 != 2) {
5979                 fprintf(stderr, "mono_return_sbyte14 s14.f2: got %d but expected %d\n", s14.f2, 2);
5980         }
5981         if (s14.f3 != 3) {
5982                 fprintf(stderr, "mono_return_sbyte14 s14.f3: got %d but expected %d\n", s14.f3, 3);
5983         }
5984         if (s14.f4 != 4) {
5985                 fprintf(stderr, "mono_return_sbyte14 s14.f4: got %d but expected %d\n", s14.f4, 4);
5986         }
5987         if (s14.f5 != 5) {
5988                 fprintf(stderr, "mono_return_sbyte14 s14.f5: got %d but expected %d\n", s14.f5, 5);
5989         }
5990         if (s14.f6 != 6) {
5991                 fprintf(stderr, "mono_return_sbyte14 s14.f6: got %d but expected %d\n", s14.f6, 6);
5992         }
5993         if (s14.f7 != 7) {
5994                 fprintf(stderr, "mono_return_sbyte14 s14.f7: got %d but expected %d\n", s14.f7, 7);
5995         }
5996         if (s14.f8 != 8) {
5997                 fprintf(stderr, "mono_return_sbyte14 s14.f8: got %d but expected %d\n", s14.f8, 8);
5998         }
5999         if (s14.f9 != 9) {
6000                 fprintf(stderr, "mono_return_sbyte14 s14.f9: got %d but expected %d\n", s14.f9, 9);
6001         }
6002         if (s14.f10 != 10) {
6003                 fprintf(stderr, "mono_return_sbyte14 s14.f10: got %d but expected %d\n", s14.f10, 10);
6004         }
6005         if (s14.f11 != 11) {
6006                 fprintf(stderr, "mono_return_sbyte14 s14.f11: got %d but expected %d\n", s14.f11, 11);
6007         }
6008         if (s14.f12 != 12) {
6009                 fprintf(stderr, "mono_return_sbyte14 s14.f12: got %d but expected %d\n", s14.f12, 12);
6010         }
6011         if (s14.f13 != 13) {
6012                 fprintf(stderr, "mono_return_sbyte14 s14.f13: got %d but expected %d\n", s14.f13, 13);
6013         }
6014         if (s14.f14 != 14) {
6015                 fprintf(stderr, "mono_return_sbyte14 s14.f14: got %d but expected %d\n", s14.f14, 14);
6016         }
6017         s14.f1+=addend; s14.f2+=addend; s14.f3+=addend; s14.f4+=addend; s14.f5+=addend; s14.f6+=addend; s14.f7+=addend; s14.f8+=addend; s14.f9+=addend; s14.f10+=addend; s14.f11+=addend; s14.f12+=addend; s14.f13+=addend; s14.f14+=addend; 
6018         return s14;
6019 }
6020
6021 typedef struct {
6022         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15;
6023 } sbyte15;
6024
6025 LIBTEST_API sbyte15 STDCALL
6026 mono_return_sbyte15 (sbyte15 s15, int addend) {
6027         if (s15.f1 != 1) {
6028                 fprintf(stderr, "mono_return_sbyte15 s15.f1: got %d but expected %d\n", s15.f1, 1);
6029         }
6030         if (s15.f2 != 2) {
6031                 fprintf(stderr, "mono_return_sbyte15 s15.f2: got %d but expected %d\n", s15.f2, 2);
6032         }
6033         if (s15.f3 != 3) {
6034                 fprintf(stderr, "mono_return_sbyte15 s15.f3: got %d but expected %d\n", s15.f3, 3);
6035         }
6036         if (s15.f4 != 4) {
6037                 fprintf(stderr, "mono_return_sbyte15 s15.f4: got %d but expected %d\n", s15.f4, 4);
6038         }
6039         if (s15.f5 != 5) {
6040                 fprintf(stderr, "mono_return_sbyte15 s15.f5: got %d but expected %d\n", s15.f5, 5);
6041         }
6042         if (s15.f6 != 6) {
6043                 fprintf(stderr, "mono_return_sbyte15 s15.f6: got %d but expected %d\n", s15.f6, 6);
6044         }
6045         if (s15.f7 != 7) {
6046                 fprintf(stderr, "mono_return_sbyte15 s15.f7: got %d but expected %d\n", s15.f7, 7);
6047         }
6048         if (s15.f8 != 8) {
6049                 fprintf(stderr, "mono_return_sbyte15 s15.f8: got %d but expected %d\n", s15.f8, 8);
6050         }
6051         if (s15.f9 != 9) {
6052                 fprintf(stderr, "mono_return_sbyte15 s15.f9: got %d but expected %d\n", s15.f9, 9);
6053         }
6054         if (s15.f10 != 10) {
6055                 fprintf(stderr, "mono_return_sbyte15 s15.f10: got %d but expected %d\n", s15.f10, 10);
6056         }
6057         if (s15.f11 != 11) {
6058                 fprintf(stderr, "mono_return_sbyte15 s15.f11: got %d but expected %d\n", s15.f11, 11);
6059         }
6060         if (s15.f12 != 12) {
6061                 fprintf(stderr, "mono_return_sbyte15 s15.f12: got %d but expected %d\n", s15.f12, 12);
6062         }
6063         if (s15.f13 != 13) {
6064                 fprintf(stderr, "mono_return_sbyte15 s15.f13: got %d but expected %d\n", s15.f13, 13);
6065         }
6066         if (s15.f14 != 14) {
6067                 fprintf(stderr, "mono_return_sbyte15 s15.f14: got %d but expected %d\n", s15.f14, 14);
6068         }
6069         if (s15.f15 != 15) {
6070                 fprintf(stderr, "mono_return_sbyte15 s15.f15: got %d but expected %d\n", s15.f15, 15);
6071         }
6072         s15.f1+=addend; s15.f2+=addend; s15.f3+=addend; s15.f4+=addend; s15.f5+=addend; s15.f6+=addend; s15.f7+=addend; s15.f8+=addend; s15.f9+=addend; s15.f10+=addend; s15.f11+=addend; s15.f12+=addend; s15.f13+=addend; s15.f14+=addend; s15.f15+=addend; 
6073         return s15;
6074 }
6075
6076 typedef struct {
6077         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16;
6078 } sbyte16;
6079
6080 LIBTEST_API sbyte16 STDCALL
6081 mono_return_sbyte16 (sbyte16 s16, int addend) {
6082         if (s16.f1 != 1) {
6083                 fprintf(stderr, "mono_return_sbyte16 s16.f1: got %d but expected %d\n", s16.f1, 1);
6084         }
6085         if (s16.f2 != 2) {
6086                 fprintf(stderr, "mono_return_sbyte16 s16.f2: got %d but expected %d\n", s16.f2, 2);
6087         }
6088         if (s16.f3 != 3) {
6089                 fprintf(stderr, "mono_return_sbyte16 s16.f3: got %d but expected %d\n", s16.f3, 3);
6090         }
6091         if (s16.f4 != 4) {
6092                 fprintf(stderr, "mono_return_sbyte16 s16.f4: got %d but expected %d\n", s16.f4, 4);
6093         }
6094         if (s16.f5 != 5) {
6095                 fprintf(stderr, "mono_return_sbyte16 s16.f5: got %d but expected %d\n", s16.f5, 5);
6096         }
6097         if (s16.f6 != 6) {
6098                 fprintf(stderr, "mono_return_sbyte16 s16.f6: got %d but expected %d\n", s16.f6, 6);
6099         }
6100         if (s16.f7 != 7) {
6101                 fprintf(stderr, "mono_return_sbyte16 s16.f7: got %d but expected %d\n", s16.f7, 7);
6102         }
6103         if (s16.f8 != 8) {
6104                 fprintf(stderr, "mono_return_sbyte16 s16.f8: got %d but expected %d\n", s16.f8, 8);
6105         }
6106         if (s16.f9 != 9) {
6107                 fprintf(stderr, "mono_return_sbyte16 s16.f9: got %d but expected %d\n", s16.f9, 9);
6108         }
6109         if (s16.f10 != 10) {
6110                 fprintf(stderr, "mono_return_sbyte16 s16.f10: got %d but expected %d\n", s16.f10, 10);
6111         }
6112         if (s16.f11 != 11) {
6113                 fprintf(stderr, "mono_return_sbyte16 s16.f11: got %d but expected %d\n", s16.f11, 11);
6114         }
6115         if (s16.f12 != 12) {
6116                 fprintf(stderr, "mono_return_sbyte16 s16.f12: got %d but expected %d\n", s16.f12, 12);
6117         }
6118         if (s16.f13 != 13) {
6119                 fprintf(stderr, "mono_return_sbyte16 s16.f13: got %d but expected %d\n", s16.f13, 13);
6120         }
6121         if (s16.f14 != 14) {
6122                 fprintf(stderr, "mono_return_sbyte16 s16.f14: got %d but expected %d\n", s16.f14, 14);
6123         }
6124         if (s16.f15 != 15) {
6125                 fprintf(stderr, "mono_return_sbyte16 s16.f15: got %d but expected %d\n", s16.f15, 15);
6126         }
6127         if (s16.f16 != 16) {
6128                 fprintf(stderr, "mono_return_sbyte16 s16.f16: got %d but expected %d\n", s16.f16, 16);
6129         }
6130         s16.f1+=addend; s16.f2+=addend; s16.f3+=addend; s16.f4+=addend; s16.f5+=addend; s16.f6+=addend; s16.f7+=addend; s16.f8+=addend; s16.f9+=addend; s16.f10+=addend; s16.f11+=addend; s16.f12+=addend; s16.f13+=addend; s16.f14+=addend; s16.f15+=addend; s16.f16+=addend; 
6131         return s16;
6132 }
6133
6134 typedef struct {
6135         char f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17;
6136 } sbyte17;
6137
6138 LIBTEST_API sbyte17 STDCALL
6139 mono_return_sbyte17 (sbyte17 s17, int addend) {
6140         if (s17.f1 != 1) {
6141                 fprintf(stderr, "mono_return_sbyte17 s17.f1: got %d but expected %d\n", s17.f1, 1);
6142         }
6143         if (s17.f2 != 2) {
6144                 fprintf(stderr, "mono_return_sbyte17 s17.f2: got %d but expected %d\n", s17.f2, 2);
6145         }
6146         if (s17.f3 != 3) {
6147                 fprintf(stderr, "mono_return_sbyte17 s17.f3: got %d but expected %d\n", s17.f3, 3);
6148         }
6149         if (s17.f4 != 4) {
6150                 fprintf(stderr, "mono_return_sbyte17 s17.f4: got %d but expected %d\n", s17.f4, 4);
6151         }
6152         if (s17.f5 != 5) {
6153                 fprintf(stderr, "mono_return_sbyte17 s17.f5: got %d but expected %d\n", s17.f5, 5);
6154         }
6155         if (s17.f6 != 6) {
6156                 fprintf(stderr, "mono_return_sbyte17 s17.f6: got %d but expected %d\n", s17.f6, 6);
6157         }
6158         if (s17.f7 != 7) {
6159                 fprintf(stderr, "mono_return_sbyte17 s17.f7: got %d but expected %d\n", s17.f7, 7);
6160         }
6161         if (s17.f8 != 8) {
6162                 fprintf(stderr, "mono_return_sbyte17 s17.f8: got %d but expected %d\n", s17.f8, 8);
6163         }
6164         if (s17.f9 != 9) {
6165                 fprintf(stderr, "mono_return_sbyte17 s17.f9: got %d but expected %d\n", s17.f9, 9);
6166         }
6167         if (s17.f10 != 10) {
6168                 fprintf(stderr, "mono_return_sbyte17 s17.f10: got %d but expected %d\n", s17.f10, 10);
6169         }
6170         if (s17.f11 != 11) {
6171                 fprintf(stderr, "mono_return_sbyte17 s17.f11: got %d but expected %d\n", s17.f11, 11);
6172         }
6173         if (s17.f12 != 12) {
6174                 fprintf(stderr, "mono_return_sbyte17 s17.f12: got %d but expected %d\n", s17.f12, 12);
6175         }
6176         if (s17.f13 != 13) {
6177                 fprintf(stderr, "mono_return_sbyte17 s17.f13: got %d but expected %d\n", s17.f13, 13);
6178         }
6179         if (s17.f14 != 14) {
6180                 fprintf(stderr, "mono_return_sbyte17 s17.f14: got %d but expected %d\n", s17.f14, 14);
6181         }
6182         if (s17.f15 != 15) {
6183                 fprintf(stderr, "mono_return_sbyte17 s17.f15: got %d but expected %d\n", s17.f15, 15);
6184         }
6185         if (s17.f16 != 16) {
6186                 fprintf(stderr, "mono_return_sbyte17 s17.f16: got %d but expected %d\n", s17.f16, 16);
6187         }
6188         if (s17.f17 != 17) {
6189                 fprintf(stderr, "mono_return_sbyte17 s17.f17: got %d but expected %d\n", s17.f17, 17);
6190         }
6191         s17.f1+=addend; s17.f2+=addend; s17.f3+=addend; s17.f4+=addend; s17.f5+=addend; s17.f6+=addend; s17.f7+=addend; s17.f8+=addend; s17.f9+=addend; s17.f10+=addend; s17.f11+=addend; s17.f12+=addend; s17.f13+=addend; s17.f14+=addend; s17.f15+=addend; s17.f16+=addend; s17.f17+=addend; 
6192         return s17;
6193 }
6194
6195 typedef struct {
6196         struct {
6197                 char f1;
6198         } nested1;
6199         char f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15;
6200         struct {
6201                 char f16;
6202         } nested2;
6203 } sbyte16_nested;
6204
6205 LIBTEST_API sbyte16_nested STDCALL
6206 mono_return_sbyte16_nested (sbyte16_nested sn16, int addend) {
6207         if (sn16.nested1.f1 != 1) {
6208                 fprintf(stderr, "mono_return_sbyte16_nested sn16.nested1.f1: got %d but expected %d\n", sn16.nested1.f1, 1);
6209         }
6210         if (sn16.f2 != 2) {
6211                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f2: got %d but expected %d\n", sn16.f2, 2);
6212         }
6213         if (sn16.f3 != 3) {
6214                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f3: got %d but expected %d\n", sn16.f3, 3);
6215         }
6216         if (sn16.f4 != 4) {
6217                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f4: got %d but expected %d\n", sn16.f4, 4);
6218         }
6219         if (sn16.f5 != 5) {
6220                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f5: got %d but expected %d\n", sn16.f5, 5);
6221         }
6222         if (sn16.f6 != 6) {
6223                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f6: got %d but expected %d\n", sn16.f6, 6);
6224         }
6225         if (sn16.f7 != 7) {
6226                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f7: got %d but expected %d\n", sn16.f7, 7);
6227         }
6228         if (sn16.f8 != 8) {
6229                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f8: got %d but expected %d\n", sn16.f8, 8);
6230         }
6231         if (sn16.f9 != 9) {
6232                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f9: got %d but expected %d\n", sn16.f9, 9);
6233         }
6234         if (sn16.f10 != 10) {
6235                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f10: got %d but expected %d\n", sn16.f10, 10);
6236         }
6237         if (sn16.f11 != 11) {
6238                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f11: got %d but expected %d\n", sn16.f11, 11);
6239         }
6240         if (sn16.f12 != 12) {
6241                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f12: got %d but expected %d\n", sn16.f12, 12);
6242         }
6243         if (sn16.f13 != 13) {
6244                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f13: got %d but expected %d\n", sn16.f13, 13);
6245         }
6246         if (sn16.f14 != 14) {
6247                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f14: got %d but expected %d\n", sn16.f14, 14);
6248         }
6249         if (sn16.f15 != 15) {
6250                 fprintf(stderr, "mono_return_sbyte16_nested sn16.f15: got %d but expected %d\n", sn16.f15, 15);
6251         }
6252         if (sn16.nested2.f16 != 16) {
6253                 fprintf(stderr, "mono_return_sbyte16_nested sn16.nested2.f16: got %d but expected %d\n", sn16.nested2.f16, 16);
6254         }
6255         sn16.nested1.f1+=addend; sn16.f2+=addend; sn16.f3+=addend; sn16.f4+=addend; sn16.f5+=addend; sn16.f6+=addend; sn16.f7+=addend; sn16.f8+=addend; sn16.f9+=addend; sn16.f10+=addend; sn16.f11+=addend; sn16.f12+=addend; sn16.f13+=addend; sn16.f14+=addend; sn16.f15+=addend; sn16.nested2.f16+=addend; 
6256         return sn16;
6257 }
6258
6259
6260 typedef struct {
6261         short f1;
6262 } short1;
6263
6264 LIBTEST_API short1 STDCALL
6265 mono_return_short1 (short1 s1, int addend) {
6266         if (s1.f1 != 1) {
6267                 fprintf(stderr, "mono_return_short1 s1.f1: got %d but expected %d\n", s1.f1, 1);
6268         }
6269         s1.f1+=addend; 
6270         return s1;
6271 }
6272
6273 typedef struct {
6274         short f1,f2;
6275 } short2;
6276
6277 LIBTEST_API short2 STDCALL
6278 mono_return_short2 (short2 s2, int addend) {
6279         if (s2.f1 != 1) {
6280                 fprintf(stderr, "mono_return_short2 s2.f1: got %d but expected %d\n", s2.f1, 1);
6281         }
6282         if (s2.f2 != 2) {
6283                 fprintf(stderr, "mono_return_short2 s2.f2: got %d but expected %d\n", s2.f2, 2);
6284         }
6285         s2.f1+=addend; s2.f2+=addend; 
6286         return s2;
6287 }
6288
6289 typedef struct {
6290         short f1,f2,f3;
6291 } short3;
6292
6293 LIBTEST_API short3 STDCALL
6294 mono_return_short3 (short3 s3, int addend) {
6295         if (s3.f1 != 1) {
6296                 fprintf(stderr, "mono_return_short3 s3.f1: got %d but expected %d\n", s3.f1, 1);
6297         }
6298         if (s3.f2 != 2) {
6299                 fprintf(stderr, "mono_return_short3 s3.f2: got %d but expected %d\n", s3.f2, 2);
6300         }
6301         if (s3.f3 != 3) {
6302                 fprintf(stderr, "mono_return_short3 s3.f3: got %d but expected %d\n", s3.f3, 3);
6303         }
6304         s3.f1+=addend; s3.f2+=addend; s3.f3+=addend; 
6305         return s3;
6306 }
6307
6308 typedef struct {
6309         short f1,f2,f3,f4;
6310 } short4;
6311
6312 LIBTEST_API short4 STDCALL
6313 mono_return_short4 (short4 s4, int addend) {
6314         if (s4.f1 != 1) {
6315                 fprintf(stderr, "mono_return_short4 s4.f1: got %d but expected %d\n", s4.f1, 1);
6316         }
6317         if (s4.f2 != 2) {
6318                 fprintf(stderr, "mono_return_short4 s4.f2: got %d but expected %d\n", s4.f2, 2);
6319         }
6320         if (s4.f3 != 3) {
6321                 fprintf(stderr, "mono_return_short4 s4.f3: got %d but expected %d\n", s4.f3, 3);
6322         }
6323         if (s4.f4 != 4) {
6324                 fprintf(stderr, "mono_return_short4 s4.f4: got %d but expected %d\n", s4.f4, 4);
6325         }
6326         s4.f1+=addend; s4.f2+=addend; s4.f3+=addend; s4.f4+=addend; 
6327         return s4;
6328 }
6329
6330 typedef struct {
6331         short f1,f2,f3,f4,f5;
6332 } short5;
6333
6334 LIBTEST_API short5 STDCALL
6335 mono_return_short5 (short5 s5, int addend) {
6336         if (s5.f1 != 1) {
6337                 fprintf(stderr, "mono_return_short5 s5.f1: got %d but expected %d\n", s5.f1, 1);
6338         }
6339         if (s5.f2 != 2) {
6340                 fprintf(stderr, "mono_return_short5 s5.f2: got %d but expected %d\n", s5.f2, 2);
6341         }
6342         if (s5.f3 != 3) {
6343                 fprintf(stderr, "mono_return_short5 s5.f3: got %d but expected %d\n", s5.f3, 3);
6344         }
6345         if (s5.f4 != 4) {
6346                 fprintf(stderr, "mono_return_short5 s5.f4: got %d but expected %d\n", s5.f4, 4);
6347         }
6348         if (s5.f5 != 5) {
6349                 fprintf(stderr, "mono_return_short5 s5.f5: got %d but expected %d\n", s5.f5, 5);
6350         }
6351         s5.f1+=addend; s5.f2+=addend; s5.f3+=addend; s5.f4+=addend; s5.f5+=addend; 
6352         return s5;
6353 }
6354
6355 typedef struct {
6356         short f1,f2,f3,f4,f5,f6;
6357 } short6;
6358
6359 LIBTEST_API short6 STDCALL
6360 mono_return_short6 (short6 s6, int addend) {
6361         if (s6.f1 != 1) {
6362                 fprintf(stderr, "mono_return_short6 s6.f1: got %d but expected %d\n", s6.f1, 1);
6363         }
6364         if (s6.f2 != 2) {
6365                 fprintf(stderr, "mono_return_short6 s6.f2: got %d but expected %d\n", s6.f2, 2);
6366         }
6367         if (s6.f3 != 3) {
6368                 fprintf(stderr, "mono_return_short6 s6.f3: got %d but expected %d\n", s6.f3, 3);
6369         }
6370         if (s6.f4 != 4) {
6371                 fprintf(stderr, "mono_return_short6 s6.f4: got %d but expected %d\n", s6.f4, 4);
6372         }
6373         if (s6.f5 != 5) {
6374                 fprintf(stderr, "mono_return_short6 s6.f5: got %d but expected %d\n", s6.f5, 5);
6375         }
6376         if (s6.f6 != 6) {
6377                 fprintf(stderr, "mono_return_short6 s6.f6: got %d but expected %d\n", s6.f6, 6);
6378         }
6379         s6.f1+=addend; s6.f2+=addend; s6.f3+=addend; s6.f4+=addend; s6.f5+=addend; s6.f6+=addend; 
6380         return s6;
6381 }
6382
6383 typedef struct {
6384         short f1,f2,f3,f4,f5,f6,f7;
6385 } short7;
6386
6387 LIBTEST_API short7 STDCALL
6388 mono_return_short7 (short7 s7, int addend) {
6389         if (s7.f1 != 1) {
6390                 fprintf(stderr, "mono_return_short7 s7.f1: got %d but expected %d\n", s7.f1, 1);
6391         }
6392         if (s7.f2 != 2) {
6393                 fprintf(stderr, "mono_return_short7 s7.f2: got %d but expected %d\n", s7.f2, 2);
6394         }
6395         if (s7.f3 != 3) {
6396                 fprintf(stderr, "mono_return_short7 s7.f3: got %d but expected %d\n", s7.f3, 3);
6397         }
6398         if (s7.f4 != 4) {
6399                 fprintf(stderr, "mono_return_short7 s7.f4: got %d but expected %d\n", s7.f4, 4);
6400         }
6401         if (s7.f5 != 5) {
6402                 fprintf(stderr, "mono_return_short7 s7.f5: got %d but expected %d\n", s7.f5, 5);
6403         }
6404         if (s7.f6 != 6) {
6405                 fprintf(stderr, "mono_return_short7 s7.f6: got %d but expected %d\n", s7.f6, 6);
6406         }
6407         if (s7.f7 != 7) {
6408                 fprintf(stderr, "mono_return_short7 s7.f7: got %d but expected %d\n", s7.f7, 7);
6409         }
6410         s7.f1+=addend; s7.f2+=addend; s7.f3+=addend; s7.f4+=addend; s7.f5+=addend; s7.f6+=addend; s7.f7+=addend; 
6411         return s7;
6412 }
6413
6414 typedef struct {
6415         short f1,f2,f3,f4,f5,f6,f7,f8;
6416 } short8;
6417
6418 LIBTEST_API short8 STDCALL
6419 mono_return_short8 (short8 s8, int addend) {
6420         if (s8.f1 != 1) {
6421                 fprintf(stderr, "mono_return_short8 s8.f1: got %d but expected %d\n", s8.f1, 1);
6422         }
6423         if (s8.f2 != 2) {
6424                 fprintf(stderr, "mono_return_short8 s8.f2: got %d but expected %d\n", s8.f2, 2);
6425         }
6426         if (s8.f3 != 3) {
6427                 fprintf(stderr, "mono_return_short8 s8.f3: got %d but expected %d\n", s8.f3, 3);
6428         }
6429         if (s8.f4 != 4) {
6430                 fprintf(stderr, "mono_return_short8 s8.f4: got %d but expected %d\n", s8.f4, 4);
6431         }
6432         if (s8.f5 != 5) {
6433                 fprintf(stderr, "mono_return_short8 s8.f5: got %d but expected %d\n", s8.f5, 5);
6434         }
6435         if (s8.f6 != 6) {
6436                 fprintf(stderr, "mono_return_short8 s8.f6: got %d but expected %d\n", s8.f6, 6);
6437         }
6438         if (s8.f7 != 7) {
6439                 fprintf(stderr, "mono_return_short8 s8.f7: got %d but expected %d\n", s8.f7, 7);
6440         }
6441         if (s8.f8 != 8) {
6442                 fprintf(stderr, "mono_return_short8 s8.f8: got %d but expected %d\n", s8.f8, 8);
6443         }
6444         s8.f1+=addend; s8.f2+=addend; s8.f3+=addend; s8.f4+=addend; s8.f5+=addend; s8.f6+=addend; s8.f7+=addend; s8.f8+=addend; 
6445         return s8;
6446 }
6447
6448 typedef struct {
6449         short f1,f2,f3,f4,f5,f6,f7,f8,f9;
6450 } short9;
6451
6452 LIBTEST_API short9 STDCALL
6453 mono_return_short9 (short9 s9, int addend) {
6454         if (s9.f1 != 1) {
6455                 fprintf(stderr, "mono_return_short9 s9.f1: got %d but expected %d\n", s9.f1, 1);
6456         }
6457         if (s9.f2 != 2) {
6458                 fprintf(stderr, "mono_return_short9 s9.f2: got %d but expected %d\n", s9.f2, 2);
6459         }
6460         if (s9.f3 != 3) {
6461                 fprintf(stderr, "mono_return_short9 s9.f3: got %d but expected %d\n", s9.f3, 3);
6462         }
6463         if (s9.f4 != 4) {
6464                 fprintf(stderr, "mono_return_short9 s9.f4: got %d but expected %d\n", s9.f4, 4);
6465         }
6466         if (s9.f5 != 5) {
6467                 fprintf(stderr, "mono_return_short9 s9.f5: got %d but expected %d\n", s9.f5, 5);
6468         }
6469         if (s9.f6 != 6) {
6470                 fprintf(stderr, "mono_return_short9 s9.f6: got %d but expected %d\n", s9.f6, 6);
6471         }
6472         if (s9.f7 != 7) {
6473                 fprintf(stderr, "mono_return_short9 s9.f7: got %d but expected %d\n", s9.f7, 7);
6474         }
6475         if (s9.f8 != 8) {
6476                 fprintf(stderr, "mono_return_short9 s9.f8: got %d but expected %d\n", s9.f8, 8);
6477         }
6478         if (s9.f9 != 9) {
6479                 fprintf(stderr, "mono_return_short9 s9.f9: got %d but expected %d\n", s9.f9, 9);
6480         }
6481         s9.f1+=addend; s9.f2+=addend; s9.f3+=addend; s9.f4+=addend; s9.f5+=addend; s9.f6+=addend; s9.f7+=addend; s9.f8+=addend; s9.f9+=addend; 
6482         return s9;
6483 }
6484
6485 typedef struct {
6486         struct {
6487                 short f1;
6488         } nested1;
6489         short f2,f3,f4,f5,f6,f7;
6490         struct {
6491                 short f8;
6492         } nested2;
6493 } short8_nested;
6494
6495 LIBTEST_API short8_nested STDCALL
6496 mono_return_short8_nested (short8_nested sn8, int addend) {
6497         if (sn8.nested1.f1 != 1) {
6498                 fprintf(stderr, "mono_return_short8_nested sn8.nested1.f1: got %d but expected %d\n", sn8.nested1.f1, 1);
6499         }
6500         if (sn8.f2 != 2) {
6501                 fprintf(stderr, "mono_return_short8_nested sn8.f2: got %d but expected %d\n", sn8.f2, 2);
6502         }
6503         if (sn8.f3 != 3) {
6504                 fprintf(stderr, "mono_return_short8_nested sn8.f3: got %d but expected %d\n", sn8.f3, 3);
6505         }
6506         if (sn8.f4 != 4) {
6507                 fprintf(stderr, "mono_return_short8_nested sn8.f4: got %d but expected %d\n", sn8.f4, 4);
6508         }
6509         if (sn8.f5 != 5) {
6510                 fprintf(stderr, "mono_return_short8_nested sn8.f5: got %d but expected %d\n", sn8.f5, 5);
6511         }
6512         if (sn8.f6 != 6) {
6513                 fprintf(stderr, "mono_return_short8_nested sn8.f6: got %d but expected %d\n", sn8.f6, 6);
6514         }
6515         if (sn8.f7 != 7) {
6516                 fprintf(stderr, "mono_return_short8_nested sn8.f7: got %d but expected %d\n", sn8.f7, 7);
6517         }
6518         if (sn8.nested2.f8 != 8) {
6519                 fprintf(stderr, "mono_return_short8_nested sn8.nested2.f8: got %d but expected %d\n", sn8.nested2.f8, 8);
6520         }
6521         sn8.nested1.f1+=addend; sn8.f2+=addend; sn8.f3+=addend; sn8.f4+=addend; sn8.f5+=addend; sn8.f6+=addend; sn8.f7+=addend; sn8.nested2.f8+=addend; 
6522         return sn8;
6523 }
6524
6525
6526 typedef struct {
6527         int f1;
6528 } int1;
6529
6530 LIBTEST_API int1 STDCALL
6531 mono_return_int1 (int1 s1, int addend) {
6532         if (s1.f1 != 1) {
6533                 fprintf(stderr, "mono_return_int1 s1.f1: got %d but expected %d\n", s1.f1, 1);
6534         }
6535         s1.f1+=addend; 
6536         return s1;
6537 }
6538
6539 typedef struct {
6540         int f1,f2;
6541 } int2;
6542
6543 LIBTEST_API int2 STDCALL
6544 mono_return_int2 (int2 s2, int addend) {
6545         if (s2.f1 != 1) {
6546                 fprintf(stderr, "mono_return_int2 s2.f1: got %d but expected %d\n", s2.f1, 1);
6547         }
6548         if (s2.f2 != 2) {
6549                 fprintf(stderr, "mono_return_int2 s2.f2: got %d but expected %d\n", s2.f2, 2);
6550         }
6551         s2.f1+=addend; s2.f2+=addend; 
6552         return s2;
6553 }
6554
6555 typedef struct {
6556         int f1,f2,f3;
6557 } int3;
6558
6559 LIBTEST_API int3 STDCALL
6560 mono_return_int3 (int3 s3, int addend) {
6561         if (s3.f1 != 1) {
6562                 fprintf(stderr, "mono_return_int3 s3.f1: got %d but expected %d\n", s3.f1, 1);
6563         }
6564         if (s3.f2 != 2) {
6565                 fprintf(stderr, "mono_return_int3 s3.f2: got %d but expected %d\n", s3.f2, 2);
6566         }
6567         if (s3.f3 != 3) {
6568                 fprintf(stderr, "mono_return_int3 s3.f3: got %d but expected %d\n", s3.f3, 3);
6569         }
6570         s3.f1+=addend; s3.f2+=addend; s3.f3+=addend; 
6571         return s3;
6572 }
6573
6574 typedef struct {
6575         int f1,f2,f3,f4;
6576 } int4;
6577
6578 LIBTEST_API int4 STDCALL
6579 mono_return_int4 (int4 s4, int addend) {
6580         if (s4.f1 != 1) {
6581                 fprintf(stderr, "mono_return_int4 s4.f1: got %d but expected %d\n", s4.f1, 1);
6582         }
6583         if (s4.f2 != 2) {
6584                 fprintf(stderr, "mono_return_int4 s4.f2: got %d but expected %d\n", s4.f2, 2);
6585         }
6586         if (s4.f3 != 3) {
6587                 fprintf(stderr, "mono_return_int4 s4.f3: got %d but expected %d\n", s4.f3, 3);
6588         }
6589         if (s4.f4 != 4) {
6590                 fprintf(stderr, "mono_return_int4 s4.f4: got %d but expected %d\n", s4.f4, 4);
6591         }
6592         s4.f1+=addend; s4.f2+=addend; s4.f3+=addend; s4.f4+=addend; 
6593         return s4;
6594 }
6595
6596 typedef struct {
6597         int f1,f2,f3,f4,f5;
6598 } int5;
6599
6600 LIBTEST_API int5 STDCALL
6601 mono_return_int5 (int5 s5, int addend) {
6602         if (s5.f1 != 1) {
6603                 fprintf(stderr, "mono_return_int5 s5.f1: got %d but expected %d\n", s5.f1, 1);
6604         }
6605         if (s5.f2 != 2) {
6606                 fprintf(stderr, "mono_return_int5 s5.f2: got %d but expected %d\n", s5.f2, 2);
6607         }
6608         if (s5.f3 != 3) {
6609                 fprintf(stderr, "mono_return_int5 s5.f3: got %d but expected %d\n", s5.f3, 3);
6610         }
6611         if (s5.f4 != 4) {
6612                 fprintf(stderr, "mono_return_int5 s5.f4: got %d but expected %d\n", s5.f4, 4);
6613         }
6614         if (s5.f5 != 5) {
6615                 fprintf(stderr, "mono_return_int5 s5.f5: got %d but expected %d\n", s5.f5, 5);
6616         }
6617         s5.f1+=addend; s5.f2+=addend; s5.f3+=addend; s5.f4+=addend; s5.f5+=addend; 
6618         return s5;
6619 }
6620
6621 typedef struct {
6622         struct {
6623                 int f1;
6624         } nested1;
6625         int f2,f3;
6626         struct {
6627                 int f4;
6628         } nested2;
6629 } int4_nested;
6630
6631 LIBTEST_API int4_nested STDCALL
6632 mono_return_int4_nested (int4_nested sn4, int addend) {
6633         if (sn4.nested1.f1 != 1) {
6634                 fprintf(stderr, "mono_return_int4_nested sn4.nested1.f1: got %d but expected %d\n", sn4.nested1.f1, 1);
6635         }
6636         if (sn4.f2 != 2) {
6637                 fprintf(stderr, "mono_return_int4_nested sn4.f2: got %d but expected %d\n", sn4.f2, 2);
6638         }
6639         if (sn4.f3 != 3) {
6640                 fprintf(stderr, "mono_return_int4_nested sn4.f3: got %d but expected %d\n", sn4.f3, 3);
6641         }
6642         if (sn4.nested2.f4 != 4) {
6643                 fprintf(stderr, "mono_return_int4_nested sn4.nested2.f4: got %d but expected %d\n", sn4.nested2.f4, 4);
6644         }
6645         sn4.nested1.f1+=addend; sn4.f2+=addend; sn4.f3+=addend; sn4.nested2.f4+=addend; 
6646         return sn4;
6647 }
6648
6649 typedef struct {
6650         float f1;
6651 } float1;
6652
6653 LIBTEST_API float1 STDCALL
6654 mono_return_float1 (float1 s1, int addend) {
6655         if (s1.f1 != 1) {
6656                 fprintf(stderr, "mono_return_float1 s1.f1: got %f but expected %d\n", s1.f1, 1);
6657         }
6658         s1.f1+=addend; 
6659         return s1;
6660 }
6661
6662 typedef struct {
6663         float f1,f2;
6664 } float2;
6665
6666 LIBTEST_API float2 STDCALL
6667 mono_return_float2 (float2 s2, int addend) {
6668         if (s2.f1 != 1) {
6669                 fprintf(stderr, "mono_return_float2 s2.f1: got %f but expected %d\n", s2.f1, 1);
6670         }
6671         if (s2.f2 != 2) {
6672                 fprintf(stderr, "mono_return_float2 s2.f2: got %f but expected %d\n", s2.f2, 2);
6673         }
6674         s2.f1+=addend; s2.f2+=addend; 
6675         return s2;
6676 }
6677
6678 typedef struct {
6679         float f1,f2,f3;
6680 } float3;
6681
6682 LIBTEST_API float3 STDCALL
6683 mono_return_float3 (float3 s3, int addend) {
6684         if (s3.f1 != 1) {
6685                 fprintf(stderr, "mono_return_float3 s3.f1: got %f but expected %d\n", s3.f1, 1);
6686         }
6687         if (s3.f2 != 2) {
6688                 fprintf(stderr, "mono_return_float3 s3.f2: got %f but expected %d\n", s3.f2, 2);
6689         }
6690         if (s3.f3 != 3) {
6691                 fprintf(stderr, "mono_return_float3 s3.f3: got %f but expected %d\n", s3.f3, 3);
6692         }
6693         s3.f1+=addend; s3.f2+=addend; s3.f3+=addend; 
6694         return s3;
6695 }
6696
6697 typedef struct {
6698         float f1,f2,f3,f4;
6699 } float4;
6700
6701 LIBTEST_API float4 STDCALL
6702 mono_return_float4 (float4 s4, int addend) {
6703         if (s4.f1 != 1) {
6704                 fprintf(stderr, "mono_return_float4 s4.f1: got %f but expected %d\n", s4.f1, 1);
6705         }
6706         if (s4.f2 != 2) {
6707                 fprintf(stderr, "mono_return_float4 s4.f2: got %f but expected %d\n", s4.f2, 2);
6708         }
6709         if (s4.f3 != 3) {
6710                 fprintf(stderr, "mono_return_float4 s4.f3: got %f but expected %d\n", s4.f3, 3);
6711         }
6712         if (s4.f4 != 4) {
6713                 fprintf(stderr, "mono_return_float4 s4.f4: got %f but expected %d\n", s4.f4, 4);
6714         }
6715         s4.f1+=addend; s4.f2+=addend; s4.f3+=addend; s4.f4+=addend; 
6716         return s4;
6717 }
6718
6719 typedef struct {
6720         float f1,f2,f3,f4,f5;
6721 } float5;
6722
6723 LIBTEST_API float5 STDCALL
6724 mono_return_float5 (float5 s5, int addend) {
6725         if (s5.f1 != 1) {
6726                 fprintf(stderr, "mono_return_float5 s5.f1: got %f but expected %d\n", s5.f1, 1);
6727         }
6728         if (s5.f2 != 2) {
6729                 fprintf(stderr, "mono_return_float5 s5.f2: got %f but expected %d\n", s5.f2, 2);
6730         }
6731         if (s5.f3 != 3) {
6732                 fprintf(stderr, "mono_return_float5 s5.f3: got %f but expected %d\n", s5.f3, 3);
6733         }
6734         if (s5.f4 != 4) {
6735                 fprintf(stderr, "mono_return_float5 s5.f4: got %f but expected %d\n", s5.f4, 4);
6736         }
6737         if (s5.f5 != 5) {
6738                 fprintf(stderr, "mono_return_float5 s5.f5: got %f but expected %d\n", s5.f5, 5);
6739         }
6740         s5.f1+=addend; s5.f2+=addend; s5.f3+=addend; s5.f4+=addend; s5.f5+=addend; 
6741         return s5;
6742 }
6743
6744 typedef struct {
6745         float f1,f2,f3,f4,f5,f6;
6746 } float6;
6747
6748 LIBTEST_API float6 STDCALL
6749 mono_return_float6 (float6 s6, int addend) {
6750         if (s6.f1 != 1) {
6751                 fprintf(stderr, "mono_return_float6 s6.f1: got %f but expected %d\n", s6.f1, 1);
6752         }
6753         if (s6.f2 != 2) {
6754                 fprintf(stderr, "mono_return_float6 s6.f2: got %f but expected %d\n", s6.f2, 2);
6755         }
6756         if (s6.f3 != 3) {
6757                 fprintf(stderr, "mono_return_float6 s6.f3: got %f but expected %d\n", s6.f3, 3);
6758         }
6759         if (s6.f4 != 4) {
6760                 fprintf(stderr, "mono_return_float6 s6.f4: got %f but expected %d\n", s6.f4, 4);
6761         }
6762         if (s6.f5 != 5) {
6763                 fprintf(stderr, "mono_return_float6 s6.f5: got %f but expected %d\n", s6.f5, 5);
6764         }
6765         if (s6.f6 != 6) {
6766                 fprintf(stderr, "mono_return_float6 s6.f6: got %f but expected %d\n", s6.f6, 6);
6767         }
6768         s6.f1+=addend; s6.f2+=addend; s6.f3+=addend; s6.f4+=addend; s6.f5+=addend; s6.f6+=addend; 
6769         return s6;
6770 }
6771
6772 typedef struct {
6773         float f1,f2,f3,f4,f5,f6,f7;
6774 } float7;
6775
6776 LIBTEST_API float7 STDCALL
6777 mono_return_float7 (float7 s7, int addend) {
6778         if (s7.f1 != 1) {
6779                 fprintf(stderr, "mono_return_float7 s7.f1: got %f but expected %d\n", s7.f1, 1);
6780         }
6781         if (s7.f2 != 2) {
6782                 fprintf(stderr, "mono_return_float7 s7.f2: got %f but expected %d\n", s7.f2, 2);
6783         }
6784         if (s7.f3 != 3) {
6785                 fprintf(stderr, "mono_return_float7 s7.f3: got %f but expected %d\n", s7.f3, 3);
6786         }
6787         if (s7.f4 != 4) {
6788                 fprintf(stderr, "mono_return_float7 s7.f4: got %f but expected %d\n", s7.f4, 4);
6789         }
6790         if (s7.f5 != 5) {
6791                 fprintf(stderr, "mono_return_float7 s7.f5: got %f but expected %d\n", s7.f5, 5);
6792         }
6793         if (s7.f6 != 6) {
6794                 fprintf(stderr, "mono_return_float7 s7.f6: got %f but expected %d\n", s7.f6, 6);
6795         }
6796         if (s7.f7 != 7) {
6797                 fprintf(stderr, "mono_return_float7 s7.f7: got %f but expected %d\n", s7.f7, 7);
6798         }
6799         s7.f1+=addend; s7.f2+=addend; s7.f3+=addend; s7.f4+=addend; s7.f5+=addend; s7.f6+=addend; s7.f7+=addend; 
6800         return s7;
6801 }
6802
6803 typedef struct {
6804         float f1,f2,f3,f4,f5,f6,f7,f8;
6805 } float8;
6806
6807 LIBTEST_API float8 STDCALL
6808 mono_return_float8 (float8 s8, int addend) {
6809         if (s8.f1 != 1) {
6810                 fprintf(stderr, "mono_return_float8 s8.f1: got %f but expected %d\n", s8.f1, 1);
6811         }
6812         if (s8.f2 != 2) {
6813                 fprintf(stderr, "mono_return_float8 s8.f2: got %f but expected %d\n", s8.f2, 2);
6814         }
6815         if (s8.f3 != 3) {
6816                 fprintf(stderr, "mono_return_float8 s8.f3: got %f but expected %d\n", s8.f3, 3);
6817         }
6818         if (s8.f4 != 4) {
6819                 fprintf(stderr, "mono_return_float8 s8.f4: got %f but expected %d\n", s8.f4, 4);
6820         }
6821         if (s8.f5 != 5) {
6822                 fprintf(stderr, "mono_return_float8 s8.f5: got %f but expected %d\n", s8.f5, 5);
6823         }
6824         if (s8.f6 != 6) {
6825                 fprintf(stderr, "mono_return_float8 s8.f6: got %f but expected %d\n", s8.f6, 6);
6826         }
6827         if (s8.f7 != 7) {
6828                 fprintf(stderr, "mono_return_float8 s8.f7: got %f but expected %d\n", s8.f7, 7);
6829         }
6830         if (s8.f8 != 8) {
6831                 fprintf(stderr, "mono_return_float8 s8.f8: got %f but expected %d\n", s8.f8, 8);
6832         }
6833         s8.f1+=addend; s8.f2+=addend; s8.f3+=addend; s8.f4+=addend; s8.f5+=addend; s8.f6+=addend; s8.f7+=addend; s8.f8+=addend; 
6834         return s8;
6835 }
6836
6837 typedef struct {
6838         float f1,f2,f3,f4,f5,f6,f7,f8,f9;
6839 } float9;
6840
6841 LIBTEST_API float9 STDCALL
6842 mono_return_float9 (float9 s9, int addend) {
6843         if (s9.f1 != 1) {
6844                 fprintf(stderr, "mono_return_float9 s9.f1: got %f but expected %d\n", s9.f1, 1);
6845         }
6846         if (s9.f2 != 2) {
6847                 fprintf(stderr, "mono_return_float9 s9.f2: got %f but expected %d\n", s9.f2, 2);
6848         }
6849         if (s9.f3 != 3) {
6850                 fprintf(stderr, "mono_return_float9 s9.f3: got %f but expected %d\n", s9.f3, 3);
6851         }
6852         if (s9.f4 != 4) {
6853                 fprintf(stderr, "mono_return_float9 s9.f4: got %f but expected %d\n", s9.f4, 4);
6854         }
6855         if (s9.f5 != 5) {
6856                 fprintf(stderr, "mono_return_float9 s9.f5: got %f but expected %d\n", s9.f5, 5);
6857         }
6858         if (s9.f6 != 6) {
6859                 fprintf(stderr, "mono_return_float9 s9.f6: got %f but expected %d\n", s9.f6, 6);
6860         }
6861         if (s9.f7 != 7) {
6862                 fprintf(stderr, "mono_return_float9 s9.f7: got %f but expected %d\n", s9.f7, 7);
6863         }
6864         if (s9.f8 != 8) {
6865                 fprintf(stderr, "mono_return_float9 s9.f8: got %f but expected %d\n", s9.f8, 8);
6866         }
6867         if (s9.f9 != 9) {
6868                 fprintf(stderr, "mono_return_float9 s9.f9: got %f but expected %d\n", s9.f9, 9);
6869         }
6870         s9.f1+=addend; s9.f2+=addend; s9.f3+=addend; s9.f4+=addend; s9.f5+=addend; s9.f6+=addend; s9.f7+=addend; s9.f8+=addend; s9.f9+=addend; 
6871         return s9;
6872 }
6873
6874 typedef struct {
6875         struct {
6876                 float f1;
6877         } nested1;
6878         float f2,f3;
6879         struct {
6880                 float f4;
6881         } nested2;
6882 } float4_nested;
6883
6884 LIBTEST_API float4_nested STDCALL
6885 mono_return_float4_nested (float4_nested sn4, int addend) {
6886         if (sn4.nested1.f1 != 1) {
6887                 fprintf(stderr, "mono_return_float4_nested sn4.nested1.f1: got %f but expected %d\n", sn4.nested1.f1, 1);
6888         }
6889         if (sn4.f2 != 2) {
6890                 fprintf(stderr, "mono_return_float4_nested sn4.f2: got %f but expected %d\n", sn4.f2, 2);
6891         }
6892         if (sn4.f3 != 3) {
6893                 fprintf(stderr, "mono_return_float4_nested sn4.f3: got %f but expected %d\n", sn4.f3, 3);
6894         }
6895         if (sn4.nested2.f4 != 4) {
6896                 fprintf(stderr, "mono_return_float4_nested sn4.nested2.f4: got %f but expected %d\n", sn4.nested2.f4, 4);
6897         }
6898         sn4.nested1.f1+=addend; sn4.f2+=addend; sn4.f3+=addend; sn4.nested2.f4+=addend; 
6899         return sn4;
6900 }
6901
6902 typedef struct {
6903         double f1;
6904 } double1;
6905
6906 LIBTEST_API double1 STDCALL
6907 mono_return_double1 (double1 s1, int addend) {
6908         if (s1.f1 != 1) {
6909                 fprintf(stderr, "mono_return_double1 s1.f1: got %f but expected %d\n", s1.f1, 1);
6910         }
6911         s1.f1+=addend; 
6912         return s1;
6913 }
6914
6915 typedef struct {
6916         double f1,f2;
6917 } double2;
6918
6919 LIBTEST_API double2 STDCALL
6920 mono_return_double2 (double2 s2, int addend) {
6921         if (s2.f1 != 1) {
6922                 fprintf(stderr, "mono_return_double2 s2.f1: got %f but expected %d\n", s2.f1, 1);
6923         }
6924         if (s2.f2 != 2) {
6925                 fprintf(stderr, "mono_return_double2 s2.f2: got %f but expected %d\n", s2.f2, 2);
6926         }
6927         s2.f1+=addend; s2.f2+=addend; 
6928         return s2;
6929 }
6930
6931 typedef struct {
6932         double f1,f2,f3;
6933 } double3;
6934
6935 LIBTEST_API double3 STDCALL
6936 mono_return_double3 (double3 s3, int addend) {
6937         if (s3.f1 != 1) {
6938                 fprintf(stderr, "mono_return_double3 s3.f1: got %f but expected %d\n", s3.f1, 1);
6939         }
6940         if (s3.f2 != 2) {
6941                 fprintf(stderr, "mono_return_double3 s3.f2: got %f but expected %d\n", s3.f2, 2);
6942         }
6943         if (s3.f3 != 3) {
6944                 fprintf(stderr, "mono_return_double3 s3.f3: got %f but expected %d\n", s3.f3, 3);
6945         }
6946         s3.f1+=addend; s3.f2+=addend; s3.f3+=addend; 
6947         return s3;
6948 }
6949
6950 typedef struct {
6951         double f1,f2,f3,f4;
6952 } double4;
6953
6954 LIBTEST_API double4 STDCALL
6955 mono_return_double4 (double4 s4, int addend) {
6956         if (s4.f1 != 1) {
6957                 fprintf(stderr, "mono_return_double4 s4.f1: got %f but expected %d\n", s4.f1, 1);
6958         }
6959         if (s4.f2 != 2) {
6960                 fprintf(stderr, "mono_return_double4 s4.f2: got %f but expected %d\n", s4.f2, 2);
6961         }
6962         if (s4.f3 != 3) {
6963                 fprintf(stderr, "mono_return_double4 s4.f3: got %f but expected %d\n", s4.f3, 3);
6964         }
6965         if (s4.f4 != 4) {
6966                 fprintf(stderr, "mono_return_double4 s4.f4: got %f but expected %d\n", s4.f4, 4);
6967         }
6968         s4.f1+=addend; s4.f2+=addend; s4.f3+=addend; s4.f4+=addend; 
6969         return s4;
6970 }
6971
6972 typedef struct {
6973         double f1,f2,f3,f4,f5;
6974 } double5;
6975
6976 LIBTEST_API double5 STDCALL
6977 mono_return_double5 (double5 s5, int addend) {
6978         if (s5.f1 != 1) {
6979                 fprintf(stderr, "mono_return_double5 s5.f1: got %f but expected %d\n", s5.f1, 1);
6980         }
6981         if (s5.f2 != 2) {
6982                 fprintf(stderr, "mono_return_double5 s5.f2: got %f but expected %d\n", s5.f2, 2);
6983         }
6984         if (s5.f3 != 3) {
6985                 fprintf(stderr, "mono_return_double5 s5.f3: got %f but expected %d\n", s5.f3, 3);
6986         }
6987         if (s5.f4 != 4) {
6988                 fprintf(stderr, "mono_return_double5 s5.f4: got %f but expected %d\n", s5.f4, 4);
6989         }
6990         if (s5.f5 != 5) {
6991                 fprintf(stderr, "mono_return_double5 s5.f5: got %f but expected %d\n", s5.f5, 5);
6992         }
6993         s5.f1+=addend; s5.f2+=addend; s5.f3+=addend; s5.f4+=addend; s5.f5+=addend; 
6994         return s5;
6995 }
6996
6997 typedef struct {
6998         double f1,f2,f3,f4,f5,f6;
6999 } double6;
7000
7001 LIBTEST_API double6 STDCALL
7002 mono_return_double6 (double6 s6, int addend) {
7003         if (s6.f1 != 1) {
7004                 fprintf(stderr, "mono_return_double6 s6.f1: got %f but expected %d\n", s6.f1, 1);
7005         }
7006         if (s6.f2 != 2) {
7007                 fprintf(stderr, "mono_return_double6 s6.f2: got %f but expected %d\n", s6.f2, 2);
7008         }
7009         if (s6.f3 != 3) {
7010                 fprintf(stderr, "mono_return_double6 s6.f3: got %f but expected %d\n", s6.f3, 3);
7011         }
7012         if (s6.f4 != 4) {
7013                 fprintf(stderr, "mono_return_double6 s6.f4: got %f but expected %d\n", s6.f4, 4);
7014         }
7015         if (s6.f5 != 5) {
7016                 fprintf(stderr, "mono_return_double6 s6.f5: got %f but expected %d\n", s6.f5, 5);
7017         }
7018         if (s6.f6 != 6) {
7019                 fprintf(stderr, "mono_return_double6 s6.f6: got %f but expected %d\n", s6.f6, 6);
7020         }
7021         s6.f1+=addend; s6.f2+=addend; s6.f3+=addend; s6.f4+=addend; s6.f5+=addend; s6.f6+=addend; 
7022         return s6;
7023 }
7024
7025 typedef struct {
7026         double f1,f2,f3,f4,f5,f6,f7;
7027 } double7;
7028
7029 LIBTEST_API double7 STDCALL
7030 mono_return_double7 (double7 s7, int addend) {
7031         if (s7.f1 != 1) {
7032                 fprintf(stderr, "mono_return_double7 s7.f1: got %f but expected %d\n", s7.f1, 1);
7033         }
7034         if (s7.f2 != 2) {
7035                 fprintf(stderr, "mono_return_double7 s7.f2: got %f but expected %d\n", s7.f2, 2);
7036         }
7037         if (s7.f3 != 3) {
7038                 fprintf(stderr, "mono_return_double7 s7.f3: got %f but expected %d\n", s7.f3, 3);
7039         }
7040         if (s7.f4 != 4) {
7041                 fprintf(stderr, "mono_return_double7 s7.f4: got %f but expected %d\n", s7.f4, 4);
7042         }
7043         if (s7.f5 != 5) {
7044                 fprintf(stderr, "mono_return_double7 s7.f5: got %f but expected %d\n", s7.f5, 5);
7045         }
7046         if (s7.f6 != 6) {
7047                 fprintf(stderr, "mono_return_double7 s7.f6: got %f but expected %d\n", s7.f6, 6);
7048         }
7049         if (s7.f7 != 7) {
7050                 fprintf(stderr, "mono_return_double7 s7.f7: got %f but expected %d\n", s7.f7, 7);
7051         }
7052         s7.f1+=addend; s7.f2+=addend; s7.f3+=addend; s7.f4+=addend; s7.f5+=addend; s7.f6+=addend; s7.f7+=addend; 
7053         return s7;
7054 }
7055
7056 typedef struct {
7057         double f1,f2,f3,f4,f5,f6,f7,f8;
7058 } double8;
7059
7060 LIBTEST_API double8 STDCALL
7061 mono_return_double8 (double8 s8, int addend) {
7062         if (s8.f1 != 1) {
7063                 fprintf(stderr, "mono_return_double8 s8.f1: got %f but expected %d\n", s8.f1, 1);
7064         }
7065         if (s8.f2 != 2) {
7066                 fprintf(stderr, "mono_return_double8 s8.f2: got %f but expected %d\n", s8.f2, 2);
7067         }
7068         if (s8.f3 != 3) {
7069                 fprintf(stderr, "mono_return_double8 s8.f3: got %f but expected %d\n", s8.f3, 3);
7070         }
7071         if (s8.f4 != 4) {
7072                 fprintf(stderr, "mono_return_double8 s8.f4: got %f but expected %d\n", s8.f4, 4);
7073         }
7074         if (s8.f5 != 5) {
7075                 fprintf(stderr, "mono_return_double8 s8.f5: got %f but expected %d\n", s8.f5, 5);
7076         }
7077         if (s8.f6 != 6) {
7078                 fprintf(stderr, "mono_return_double8 s8.f6: got %f but expected %d\n", s8.f6, 6);
7079         }
7080         if (s8.f7 != 7) {
7081                 fprintf(stderr, "mono_return_double8 s8.f7: got %f but expected %d\n", s8.f7, 7);
7082         }
7083         if (s8.f8 != 8) {
7084                 fprintf(stderr, "mono_return_double8 s8.f8: got %f but expected %d\n", s8.f8, 8);
7085         }
7086         s8.f1+=addend; s8.f2+=addend; s8.f3+=addend; s8.f4+=addend; s8.f5+=addend; s8.f6+=addend; s8.f7+=addend; s8.f8+=addend; 
7087         return s8;
7088 }
7089
7090 typedef struct {
7091         double f1,f2,f3,f4,f5,f6,f7,f8,f9;
7092 } double9;
7093
7094 LIBTEST_API double9 STDCALL
7095 mono_return_double9 (double9 s9, int addend) {
7096         if (s9.f1 != 1) {
7097                 fprintf(stderr, "mono_return_double9 s9.f1: got %f but expected %d\n", s9.f1, 1);
7098         }
7099         if (s9.f2 != 2) {
7100                 fprintf(stderr, "mono_return_double9 s9.f2: got %f but expected %d\n", s9.f2, 2);
7101         }
7102         if (s9.f3 != 3) {
7103                 fprintf(stderr, "mono_return_double9 s9.f3: got %f but expected %d\n", s9.f3, 3);
7104         }
7105         if (s9.f4 != 4) {
7106                 fprintf(stderr, "mono_return_double9 s9.f4: got %f but expected %d\n", s9.f4, 4);
7107         }
7108         if (s9.f5 != 5) {
7109                 fprintf(stderr, "mono_return_double9 s9.f5: got %f but expected %d\n", s9.f5, 5);
7110         }
7111         if (s9.f6 != 6) {
7112                 fprintf(stderr, "mono_return_double9 s9.f6: got %f but expected %d\n", s9.f6, 6);
7113         }
7114         if (s9.f7 != 7) {
7115                 fprintf(stderr, "mono_return_double9 s9.f7: got %f but expected %d\n", s9.f7, 7);
7116         }
7117         if (s9.f8 != 8) {
7118                 fprintf(stderr, "mono_return_double9 s9.f8: got %f but expected %d\n", s9.f8, 8);
7119         }
7120         if (s9.f9 != 9) {
7121                 fprintf(stderr, "mono_return_double9 s9.f9: got %f but expected %d\n", s9.f9, 9);
7122         }
7123         s9.f1+=addend; s9.f2+=addend; s9.f3+=addend; s9.f4+=addend; s9.f5+=addend; s9.f6+=addend; s9.f7+=addend; s9.f8+=addend; s9.f9+=addend; 
7124         return s9;
7125 }
7126
7127 typedef struct {
7128         struct {
7129                 double f1;
7130         } nested1;
7131         struct {
7132                 double f2;
7133         } nested2;
7134 } double2_nested;
7135
7136 LIBTEST_API double2_nested STDCALL
7137 mono_return_double2_nested (double2_nested sn2, int addend) {
7138         if (sn2.nested1.f1 != 1) {
7139                 fprintf(stderr, "mono_return_double2_nested sn2.nested1.f1: got %f but expected %d\n", sn2.nested1.f1, 1);
7140         }
7141         if (sn2.nested2.f2 != 2) {
7142                 fprintf(stderr, "mono_return_double2_nested sn2.nested2.f2: got %f but expected %d\n", sn2.nested2.f2, 2);
7143         }
7144         sn2.nested1.f1+=addend; sn2.nested2.f2+=addend; 
7145         return sn2;
7146 }
7147
7148
7149
7150 typedef struct {
7151         double f1[4];
7152 } double_array4;
7153
7154 LIBTEST_API double_array4 STDCALL
7155 mono_return_double_array4 (double_array4 sa4, int addend) {
7156         if (sa4.f1[0] != 1) {
7157                 fprintf(stderr, "mono_return_double_array4 sa4.f1[0]: got %f but expected %d\n", sa4.f1[0], 1);
7158         }
7159         if (sa4.f1[1] != 2) {
7160                 fprintf(stderr, "mono_return_double_array4 sa4.f1[1]: got %f but expected %d\n", sa4.f1[1], 2);
7161         }
7162         if (sa4.f1[2] != 3) {
7163                 fprintf(stderr, "mono_return_double_array4 sa4.f1[2]: got %f but expected %d\n", sa4.f1[2], 3);
7164         }
7165         if (sa4.f1[3] != 4) {
7166                 fprintf(stderr, "mono_return_double_array4 sa4.f1[3]: got %f but expected %d\n", sa4.f1[3], 4);
7167         }
7168         sa4.f1[0]+=addend; sa4.f1[1]+=addend; sa4.f1[2]+=addend; sa4.f1[3]+=addend; 
7169         return sa4;
7170 }
7171