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