merge -r 53370:58178
[mono.git] / mono / tests / libtest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <glib.h>
5 #include <errno.h>
6 #include <time.h>
7
8 #ifdef WIN32
9 #define STDCALL __stdcall
10 #else
11 #define STDCALL
12 #endif
13
14 #ifdef WIN32
15 extern __declspec(dllimport) __stdcall void CoTaskMemFree(void *ptr);
16 #endif
17
18 typedef int (STDCALL *SimpleDelegate) (int a);
19
20 static void marshal_free (void *ptr)
21 {
22 #ifdef WIN32
23         CoTaskMemFree (ptr);
24 #else
25         g_free (ptr);
26 #endif
27 }
28
29 STDCALL unsigned short*
30 test_lpwstr_marshal (unsigned short* chars, long length)
31 {
32         int i = 0;
33         unsigned short *res;
34
35         res = malloc (2 * (length + 1));
36
37         // printf("test_lpwstr_marshal()\n");
38         
39         while ( i < length ) {
40                 // printf("X|%u|\n", chars[i]);
41                 res [i] = chars[i];
42                 i++;
43         }
44
45         res [i] = 0;
46
47         return res;
48 }
49
50 typedef struct {
51         int b;
52         int a;
53         int c;
54 } union_test_1_type;
55
56 STDCALL int 
57 mono_union_test_1 (union_test_1_type u1) {
58         // printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
59         return u1.a + u1.b + u1.c;
60 }
61
62 STDCALL int 
63 mono_return_int (int a) {
64         // printf ("Got value %d\n", a);
65         return a;
66 }
67
68 struct ss
69 {
70         int i;
71 };
72
73 STDCALL int 
74 mono_return_int_ss (struct ss a) {
75         // printf ("Got value %d\n", a.i);
76         return a.i;
77 }
78
79 STDCALL struct ss 
80 mono_return_ss (struct ss a) {
81         // printf ("Got value %d\n", a.i);
82         a.i++;
83         return a;
84 }
85
86 struct sc1
87 {
88         char c[1];
89 };
90
91 STDCALL struct sc1 
92 mono_return_sc1 (struct sc1 a) {
93         // printf ("Got value %d\n", a.c[0]);
94         a.c[0]++;
95         return a;
96 }
97
98
99 struct sc3
100 {
101         char c[3];
102 };
103
104 STDCALL struct sc3 
105 mono_return_sc3 (struct sc3 a) {
106         // printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
107         a.c[0]++;
108         a.c[1] += 2;
109         a.c[2] += 3;
110         return a;
111 }
112
113 struct sc5
114 {
115         char c[5];
116 };
117
118 STDCALL struct sc5 
119 mono_return_sc5 (struct sc5 a) {
120         // printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
121         a.c[0]++;
122         a.c[1] += 2;
123         a.c[2] += 3;
124         a.c[3] += 4;
125         a.c[4] += 5;
126         return a;
127 }
128
129 union su
130 {
131         int i1;
132         int i2;
133 };
134
135 STDCALL int 
136 mono_return_int_su (union su a) {
137         // printf ("Got value %d\n", a.i1);
138         return a.i1;
139 }
140
141 STDCALL int 
142 mono_test_many_int_arguments (int a, int b, int c, int d, int e,
143                                                           int f, int g, int h, int i, int j);
144 STDCALL short 
145 mono_test_many_short_arguments (short a, short b, short c, short d, short e,
146                                                                 short f, short g, short h, short i, short j);
147 STDCALL char 
148 mono_test_many_char_arguments (char a, char b, char c, char d, char e,
149                                                            char f, char g, char h, char i, char j);
150
151 STDCALL int
152 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)
153 {
154         return a + b + c + d + e + f + g + h + i + j;
155 }
156
157 STDCALL short
158 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)
159 {
160         return a + b + c + d + e + f + g + h + i + j;
161 }
162
163 STDCALL char
164 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)
165 {
166         return a + b + c + d + e + f + g + h + i + j;
167 }
168
169 STDCALL float
170 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)
171 {
172         return a + b + c + d + e + f + g + h + i + j;
173 }
174
175 STDCALL double
176 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)
177 {
178         return a + b + c + d + e + f + g + h + i + j;
179 }
180
181 STDCALL double
182 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
183 {
184         return a + b + c + d + e;
185 }
186
187 STDCALL int
188 mono_test_puts_static (char *s)
189 {
190         // printf ("TEST %s\n", s);
191         return 1;
192 }
193
194 typedef int (STDCALL *SimpleDelegate3) (int a, int b);
195
196 STDCALL int
197 mono_invoke_delegate (SimpleDelegate3 delegate)
198 {
199         int res;
200
201         // printf ("start invoke %p\n", delegate);
202
203         res = delegate (2, 3);
204
205         // printf ("end invoke\n");
206
207         return res;
208 }
209
210 STDCALL int 
211 mono_test_marshal_char (short a1)
212 {
213         if (a1 == 'a')
214                 return 0;
215         
216         return 1;
217 }
218
219 STDCALL void
220 mono_test_marshal_char_array (gunichar2 *s)
221 {
222         const char m[] = "abcdef";
223         gunichar2* s2;
224         glong len;
225
226         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
227         
228         len = (len * 2) + 2;
229         memcpy (s, s2, len);
230
231         g_free (s2);
232 }
233
234 STDCALL int
235 mono_test_empty_pinvoke (int i)
236 {
237         return i;
238 }
239
240 STDCALL int 
241 mono_test_marshal_bool_byref (int a, int *b, int c)
242 {
243     int res = *b;
244
245         *b = 1;
246
247         return res;
248 }
249
250 STDCALL int 
251 mono_test_marshal_array (int *a1)
252 {
253         int i, sum = 0;
254
255         for (i = 0; i < 50; i++)
256                 sum += a1 [i];
257         
258         return sum;
259 }
260
261 STDCALL int 
262 mono_test_marshal_inout_array (int *a1)
263 {
264         int i, sum = 0;
265
266         for (i = 0; i < 50; i++) {
267                 sum += a1 [i];
268                 a1 [i] = 50 - a1 [i];
269         }
270         
271         return sum;
272 }
273
274 STDCALL int 
275 mono_test_marshal_out_array (int *a1)
276 {
277         int i;
278
279         for (i = 0; i < 50; i++) {
280                 a1 [i] = i;
281         }
282         
283         return 0;
284 }
285
286 STDCALL int 
287 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
288 {
289         int i, sum = 0;
290
291         for (i = 0; i < 10; i++) {
292                 a1 [i] = 'F';
293         }
294         
295         return sum;
296 }
297
298 typedef struct {
299         int a;
300         int b;
301         int c;
302         const char *d;
303         gunichar2 *d2;
304 } simplestruct;
305
306 STDCALL simplestruct
307 mono_test_return_vtype (int i)
308 {
309         simplestruct res;
310         static gunichar2 test2 [] = { 'T', 'E', 'S', 'T', '2', 0 };
311
312         res.a = 0;
313         res.b = 1;
314         res.c = 0;
315         res.d = "TEST";
316         res.d2 = test2;
317
318         return res;
319 }
320
321 STDCALL void
322 mono_test_delegate_struct (void)
323 {
324         // printf ("TEST\n");
325 }
326
327 typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
328
329 STDCALL char *
330 mono_test_return_string (ReturnStringDelegate func)
331 {
332         char *res;
333
334         // printf ("mono_test_return_string\n");
335
336         res = func ("TEST");
337         marshal_free (res);
338
339         // printf ("got string: %s\n", res);
340         return g_strdup ("12345");
341 }
342
343 typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
344
345 STDCALL int
346 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
347 {
348         if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
349             !strcmp (ss->d, "TEST1")) {
350                 ss->a = 1;
351                 ss->b = 0;
352                 ss->c = 1;
353                 ss->d = "TEST2";
354         
355                 return func (a, ss, b);
356         }
357
358         return 1;
359 }
360
361 typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
362
363 STDCALL int
364 mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
365 {
366         /* Check that the input pointer is ignored */
367         ss->d = (gpointer)0x12345678;
368
369         func (a, ss, b);
370
371         if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
372                 return 0;
373         else
374                 return 1;
375 }
376
377 typedef struct {
378         int a;
379         SimpleDelegate func, func2;
380 } DelegateStruct;
381
382 STDCALL DelegateStruct
383 mono_test_marshal_delegate_struct (DelegateStruct ds)
384 {
385         DelegateStruct res;
386
387         res.a = ds.func (ds.a) + ds.func2 (ds.a);
388         res.func = ds.func;
389         res.func2 = ds.func2;
390
391         return res;
392 }
393
394 STDCALL int 
395 mono_test_marshal_struct (simplestruct ss)
396 {
397         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
398             !strcmp (ss.d, "TEST"))
399                 return 0;
400
401         return 1;
402 }
403
404 STDCALL int
405 mono_test_marshal_byref_struct (simplestruct *ss, int a, int b, int c, char *d)
406 {
407         gboolean res = (ss->a == a && ss->b == b && ss->c == c && strcmp (ss->d, d) == 0);
408
409         ss->a = !ss->a;
410         ss->b = !ss->b;
411         ss->c = !ss->c;
412         ss->d = g_strdup ("DEF");
413
414         return res ? 0 : 1;
415 }
416
417 typedef struct {
418         int a;
419         int b;
420         int c;
421         char *d;
422         unsigned char e;
423         double f;
424         unsigned char g;
425         guint64 h;
426 } simplestruct2;
427
428 STDCALL int
429 mono_test_marshal_struct2 (simplestruct2 ss)
430 {
431         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
432             !strcmp (ss.d, "TEST") && 
433             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
434                 return 0;
435
436         return 1;
437 }
438
439 /* on HP some of the struct should be on the stack and not in registers */
440 STDCALL int
441 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
442 {
443         if (i != 10 || j != 11 || k != 12)
444                 return 1;
445         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
446             !strcmp (ss.d, "TEST") && 
447             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
448                 return 0;
449
450         return 1;
451 }
452
453 STDCALL int
454 mono_test_marshal_struct_array (simplestruct2 *ss)
455 {
456         if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
457                    !strcmp (ss[0].d, "TEST") && 
458                    ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
459                 return 1;
460
461         if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
462                    !strcmp (ss[1].d, "TEST2") && 
463                    ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
464                 return 1;
465
466         return 0;
467 }
468
469 typedef struct long_align_struct {
470         gint32 a;
471         gint64 b;
472         gint64 c;
473 } long_align_struct;
474
475 STDCALL int
476 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
477 {
478         return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
479 }
480
481 STDCALL simplestruct2 *
482 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
483 {
484         simplestruct2 *res;
485
486         if (!ss)
487                 return NULL;
488
489         if (i != 10 || j != 11 || k != 12 || l != 14)
490                 return NULL;
491         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
492                    !strcmp (ss->d, "TEST") && 
493                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
494                 return NULL;
495
496         res = g_new0 (simplestruct2, 1);
497         memcpy (res, ss, sizeof (simplestruct2));
498         res->d = g_strdup ("TEST");
499         return res;
500 }
501
502 STDCALL int
503 mono_test_marshal_byref_class (simplestruct2 **ssp)
504 {
505         simplestruct2 *ss = *ssp;
506         simplestruct2 *res;
507         
508         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
509                    !strcmp (ss->d, "TEST") && 
510                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
511                 return 1;
512
513         res = g_new0 (simplestruct2, 1);
514         memcpy (res, ss, sizeof (simplestruct2));
515         res->d = g_strdup ("TEST-RES");
516
517         *ssp = res;
518         return 0;
519 }
520
521 static void *
522 get_sp (void)
523 {
524         int i;
525         void *p;
526
527         p = &i;
528         return p;
529 }
530
531 STDCALL int
532 reliable_delegate (int a)
533 {
534         return a;
535 }
536
537 /*
538  * Checks whether get_sp() works as expected. It doesn't work with gcc-2.95.3 on linux.
539  */
540 static gboolean
541 is_get_sp_reliable (void)
542 {
543         void *sp1, *sp2;
544
545         reliable_delegate(1);
546         sp1 = get_sp();
547         reliable_delegate(1);
548         sp2 = get_sp();
549         return sp1 == sp2;
550
551
552 STDCALL int
553 mono_test_marshal_delegate (SimpleDelegate delegate)
554 {
555         void *sp1, *sp2;
556
557         /* Check that the delegate wrapper is stdcall */
558         delegate (2);
559         sp1 = get_sp ();
560         delegate (2);
561         sp2 = get_sp ();
562         if (is_get_sp_reliable())
563                 g_assert (sp1 == sp2);
564
565         return delegate (2);
566 }
567
568 STDCALL SimpleDelegate
569 mono_test_marshal_return_delegate (SimpleDelegate delegate)
570 {
571         return delegate;
572 }
573
574 static STDCALL int
575 return_plus_one (int i)
576 {
577         return i + 1;
578 }
579
580 STDCALL SimpleDelegate
581 mono_test_marshal_return_delegate_2 ()
582 {
583         return return_plus_one;
584 }
585
586 typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
587
588 static gboolean
589 is_utf16_equals (gunichar2 *s1, const char *s2)
590 {
591         char *s;
592         int res;
593
594         s = g_utf16_to_utf8 (s1, -1, NULL, NULL, NULL);
595         res = strcmp (s, s2);
596         g_free (s);
597
598         return res == 0;
599 }
600
601 STDCALL int
602 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
603 {
604         simplestruct ss, res;
605
606         ss.a = 0;
607         ss.b = 1;
608         ss.c = 0;
609         ss.d = "TEST";
610         ss.d2 = g_utf8_to_utf16 ("TEST2", -1, NULL, NULL, NULL); 
611
612         res = delegate (ss);
613         if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES") && is_utf16_equals (res.d2, "TEST2-RES")))
614                 return 1;
615
616         return 0;
617 }
618
619 typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
620
621 STDCALL int
622 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
623 {
624         simplestruct ss;
625         simplestruct *res;
626
627         ss.a = 0;
628         ss.b = 1;
629         ss.c = 0;
630         ss.d = "TEST";
631
632         /* Check argument */
633         res = delegate (&ss);
634         if (!res)
635                 return 1;
636
637         /* Check return value */
638         if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
639                 return 2;
640
641         /* Check NULL argument and NULL result */
642         res = delegate (NULL);
643         if (res)
644                 return 3;
645
646         return 0;
647 }
648
649 typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
650
651 STDCALL int
652 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
653 {
654         simplestruct ss;
655         int res;
656         simplestruct *ptr;
657
658         ss.a = 0;
659         ss.b = 1;
660         ss.c = 0;
661         ss.d = "TEST";
662
663         ptr = &ss;
664
665         res = delegate (&ptr);
666         if (res != 0)
667                 return 1;
668
669         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
670                 return 2;
671
672         return 0;
673 }
674
675 STDCALL int
676 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
677 {
678         int res;
679
680         res = delegate (NULL);
681
682         return 0;
683 }
684
685 typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
686
687 STDCALL int
688 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
689 {
690         int res;
691         simplestruct *ptr;
692
693         /* Check that the input pointer is ignored */
694         ptr = (gpointer)0x12345678;
695
696         res = delegate (&ptr);
697         if (res != 0)
698                 return 1;
699
700         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
701                 return 2;
702
703         return 0;
704 }
705
706 typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
707
708 STDCALL int
709 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
710 {
711         return delegate (s);
712 }
713
714 typedef int (STDCALL *return_int_fnt) (int i);
715 typedef int (STDCALL *SimpleDelegate9) (return_int_fnt d);
716
717 STDCALL int
718 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
719 {
720         return delegate (ftn);
721 }
722
723 STDCALL static int
724 return_self (int i)
725 {
726         return i;
727 }
728
729 STDCALL int
730 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
731 {
732         return delegate (return_self);
733 }
734
735 typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
736
737 STDCALL int
738 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
739 {
740         int i = 1;
741
742         int res = delegate (&i);
743         if (res != 0)
744                 return res;
745
746         if (i != 2)
747                 return 2;
748
749         return 0;
750 }
751
752 typedef int (STDCALL *return_int_delegate) (int i);
753
754 typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) (void);
755
756 STDCALL int
757 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
758 {
759         return (d ()) (55);
760 }
761
762 STDCALL int 
763 mono_test_marshal_stringbuilder (char *s, int n)
764 {
765         const char m[] = "This is my message.  Isn't it nice?";
766
767         if (strcmp (s, "ABCD") != 0)
768                 return 1;
769         strncpy(s, m, n);
770         return 0;
771 }
772
773 STDCALL int 
774 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
775 {
776         const char m[] = "This is my message.  Isn't it nice?";
777         gunichar2* s2;
778         glong len;
779
780         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
781         
782         len = (len * 2) + 2;
783         if (len > n)
784                 len = n;
785         memcpy (s, s2, len);
786
787         g_free (s2);
788
789         return 0;
790 }
791
792 typedef struct {
793 #ifndef __GNUC__
794     char a;
795 #endif
796 } EmptyStruct;
797
798 STDCALL int
799 mono_test_marshal_empty_string_array (char **array)
800 {
801         return (array == NULL) ? 0 : 1;
802 }
803
804 STDCALL int
805 mono_test_marshal_string_array (char **array)
806 {
807         if (strcmp (array [0], "ABC"))
808                 return 1;
809         if (strcmp (array [1], "DEF"))
810                 return 2;
811
812         if (array [2] != NULL)
813                 return 3;
814
815         return 0;
816 }
817
818 STDCALL int
819 mono_test_marshal_byref_string_array (char ***array)
820 {
821         if (*array == NULL)
822                 return 0;
823
824         if (strcmp ((*array) [0], "Alpha"))
825                 return 2;
826         if (strcmp ((*array) [1], "Beta"))
827                 return 2;
828         if (strcmp ((*array) [2], "Gamma"))
829                 return 2;
830
831         return 1;
832 }
833
834 STDCALL int
835 mono_test_marshal_stringbuilder_array (char **array)
836 {
837         if (strcmp (array [0], "ABC"))
838                 return 1;
839         if (strcmp (array [1], "DEF"))
840                 return 2;
841
842         strcpy (array [0], "DEF");
843         strcpy (array [1], "ABC");
844
845         return 0;
846 }
847
848 STDCALL int
849 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
850 {
851         GError *error = NULL;
852         char *s;
853         
854         s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
855         if (strcmp (s, "ABC")) {
856                 g_free (s);
857                 return 1;
858         }
859         else
860                 g_free (s);
861
862         s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
863         if (strcmp (s, "DEF")) {
864                 g_free (s);
865                 return 2;
866         }
867         else
868                 g_free (s);
869
870         if (strcmp (array2 [0], "ABC"))
871                 return 3;
872
873         if (strcmp (array2 [1], "DEF")) 
874                 return 4;
875
876         return 0;
877 }
878
879 /* this does not work on Redhat gcc 2.96 */
880 STDCALL int 
881 mono_test_empty_struct (int a, EmptyStruct es, int b)
882 {
883         // printf ("mono_test_empty_struct %d %d\n", a, b);
884
885         // Intel icc on ia64 passes 'es' in 2 registers
886 #if defined(__ia64) && defined(__INTEL_COMPILER)
887         return 0;
888 #else
889         if (a == 1 && b == 2)
890                 return 0;
891         return 1;
892 #endif
893 }
894
895 typedef struct {
896        char a[100];
897 } ByValStrStruct;
898
899 STDCALL ByValStrStruct *
900 mono_test_byvalstr_gen (void)
901 {
902         ByValStrStruct *ret;
903        
904         ret = malloc(sizeof(ByValStrStruct));
905         memset(ret, 'a', sizeof(ByValStrStruct)-1);
906         ret->a[sizeof(ByValStrStruct)-1] = 0;
907
908         return ret;
909 }
910
911 STDCALL int
912 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
913 {
914         int ret;
915
916         ret = strcmp(data->a, correctString);
917         // printf ("T1: %s\n", data->a);
918         // printf ("T2: %s\n", correctString);
919
920         marshal_free (data);
921         return (ret != 0);
922 }
923
924 STDCALL int
925 NameManglingAnsi (char *data)
926 {
927         return data [0] + data [1] + data [2];
928 }
929
930 STDCALL int
931 NameManglingAnsiA (char *data)
932 {
933         g_assert_not_reached ();
934 }
935
936 STDCALL int
937 NameManglingAnsiW (char *data)
938 {
939         g_assert_not_reached ();
940 }
941
942 STDCALL int
943 NameManglingAnsi2A (char *data)
944 {
945         return data [0] + data [1] + data [2];
946 }
947
948 STDCALL int
949 NameManglingAnsi2W (char *data)
950 {
951         g_assert_not_reached ();
952 }
953
954 STDCALL int
955 NameManglingUnicode (char *data)
956 {
957         g_assert_not_reached ();
958 }
959
960 STDCALL int
961 NameManglingUnicodeW (gunichar2 *data)
962 {
963         return data [0] + data [1] + data [2];
964 }
965
966 STDCALL int
967 NameManglingUnicode2 (gunichar2 *data)
968 {
969         return data [0] + data [1] + data [2];
970 }
971
972 STDCALL int
973 NameManglingAutoW (char *data)
974 {
975 #ifdef WIN32
976         return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
977 #else
978         g_assert_not_reached ();
979 #endif
980 }
981
982 STDCALL int
983 NameManglingAuto (char *data)
984 {
985 #ifndef WIN32
986         return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
987 #else
988         g_assert_not_reached ();
989 #endif
990 }
991
992 typedef int (STDCALL *intcharFunc)(const char*);
993
994 STDCALL void 
995 callFunction (intcharFunc f)
996 {
997         f ("ABC");
998 }
999
1000 typedef struct {
1001         const char* str;
1002         int i;
1003 } SimpleObj;
1004
1005 STDCALL int
1006 class_marshal_test0 (SimpleObj *obj1)
1007 {
1008         // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1009
1010         if (strcmp(obj1->str, "T1"))
1011                 return -1;
1012         if (obj1->i != 4)
1013                 return -2;
1014
1015         return 0;
1016 }
1017
1018 STDCALL int
1019 class_marshal_test4 (SimpleObj *obj1)
1020 {
1021         if (obj1)
1022                 return -1;
1023
1024         return 0;
1025 }
1026
1027 STDCALL void
1028 class_marshal_test1 (SimpleObj **obj1)
1029 {
1030         SimpleObj *res = malloc (sizeof (SimpleObj));
1031
1032         res->str = g_strdup ("ABC");
1033         res->i = 5;
1034
1035         *obj1 = res;
1036 }
1037
1038 STDCALL int
1039 class_marshal_test2 (SimpleObj **obj1)
1040 {
1041         // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1042
1043         if (strcmp((*obj1)->str, "ABC"))
1044                 return -1;
1045         if ((*obj1)->i != 5)
1046                 return -2;
1047
1048         return 0;
1049 }
1050
1051 STDCALL int
1052 string_marshal_test0 (char *str)
1053 {
1054         if (strcmp (str, "TEST0"))
1055                 return -1;
1056
1057         return 0;
1058 }
1059
1060 STDCALL void
1061 string_marshal_test1 (const char **str)
1062 {
1063         *str = "TEST1";
1064 }
1065
1066 STDCALL int
1067 string_marshal_test2 (char **str)
1068 {
1069         // printf ("string_marshal_test2 %s\n", *str);
1070
1071         if (strcmp (*str, "TEST1"))
1072                 return -1;
1073
1074         return 0;
1075 }
1076
1077 STDCALL int
1078 string_marshal_test3 (char *str)
1079 {
1080         if (str)
1081                 return -1;
1082
1083         return 0;
1084 }
1085
1086 typedef struct {
1087         int a;
1088         int b;
1089 } VectorList;
1090
1091 STDCALL VectorList* 
1092 TestVectorList (VectorList *vl)
1093 {
1094         VectorList *res;
1095
1096         // printf ("TestVectorList %d %d\n", vl->a, vl->b);
1097
1098         vl->a++;
1099         vl->b++;
1100
1101         res = g_new0 (VectorList, 1);
1102         memcpy (res, vl, sizeof (VectorList));
1103
1104         return res;
1105 }
1106
1107 typedef struct _OSVERSIONINFO
1108
1109         int a; 
1110         int b; 
1111 } OSVERSIONINFO; 
1112
1113 STDCALL int 
1114 GetVersionEx (OSVERSIONINFO *osvi)
1115 {
1116
1117         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1118
1119         osvi->a += 1;
1120         osvi->b += 1;
1121
1122         return osvi->a + osvi->b;
1123 }
1124
1125 STDCALL int 
1126 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO *osvi)
1127 {
1128
1129         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1130
1131         osvi->a += 1;
1132         osvi->b += 1;
1133
1134         return osvi->a + osvi->b;
1135 }
1136
1137 typedef struct {
1138         double x;
1139         double y;
1140 } point;
1141
1142 STDCALL int
1143 mono_test_marshal_point (point pt)
1144 {
1145         // printf("point %g %g\n", pt.x, pt.y);
1146         if (pt.x == 1.25 && pt.y == 3.5)
1147                 return 0;
1148
1149         return 1;
1150 }
1151
1152 typedef struct {
1153         int x;
1154         double y;
1155 } mixed_point;
1156
1157 STDCALL int
1158 mono_test_marshal_mixed_point (mixed_point pt)
1159 {
1160         // printf("mixed point %d %g\n", pt.x, pt.y);
1161         if (pt.x == 5 && pt.y == 6.75)
1162                 return 0;
1163
1164         return 1;
1165 }
1166
1167 STDCALL int
1168 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1169 {
1170         if (pt->x != 5 || pt->y != 6.75)
1171                 return 1;
1172
1173         pt->x = 10;
1174         pt->y = 12.35;
1175
1176         return 0;
1177 }
1178
1179 STDCALL int 
1180 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1181 {
1182     int res = 1;
1183     if (*b1 != 0 && *b1 != 1)
1184         return 1;
1185     if (*b2 != 0 && *b2 != -1) /* variant_bool */
1186         return 1;
1187     if (*b3 != 0 && *b3 != 1)
1188         return 1;
1189     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1190         res = 0;
1191     *b1 = !*b1;
1192     *b2 = ~*b2;
1193     *b3 = !*b3;
1194     return res;
1195 }
1196
1197 struct BoolStruct
1198 {
1199     int i;
1200     char b1;
1201     short b2; /* variant_bool */
1202     int b3;
1203 };
1204
1205 STDCALL int 
1206 marshal_test_bool_struct(struct BoolStruct *s)
1207 {
1208     int res = 1;
1209     if (s->b1 != 0 && s->b1 != 1)
1210         return 1;
1211     if (s->b2 != 0 && s->b2 != -1)
1212         return 1;
1213     if (s->b3 != 0 && s->b3 != 1)
1214         return 1;
1215     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1216         res = 0;
1217     s->b1 = !s->b1;
1218     s->b2 = ~s->b2;
1219     s->b3 = !s->b3;
1220     return res;
1221 }
1222
1223 #ifdef WIN32
1224 extern __declspec(dllimport) __stdcall void SetLastError(int x);
1225 #endif
1226
1227 STDCALL void
1228 mono_test_last_error (int err)
1229 {
1230 #ifdef WIN32
1231         SetLastError (err);
1232 #else
1233         errno = err;
1234 #endif
1235 }
1236
1237 STDCALL int
1238 mono_test_asany (void *ptr, int what)
1239 {
1240         switch (what) {
1241         case 1:
1242                 return (*(int*)ptr == 5) ? 0 : 1;
1243         case 2:
1244                 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1245         case 3: {
1246                 simplestruct2 ss = *(simplestruct2*)ptr;
1247
1248                 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1249             !strcmp (ss.d, "TEST") && 
1250             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1251                         return 0;
1252                 else
1253                         return 1;
1254         }
1255         case 4: {
1256                 GError *error = NULL;
1257                 char *s;
1258
1259                 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1260                 if (!strcmp (s, "ABC")) {
1261                         g_free (s);
1262                         return 0;
1263                 }
1264                 else {
1265                         g_free (s);
1266                         return 1;
1267                 }
1268         }
1269         default:
1270                 g_assert_not_reached ();
1271         }
1272
1273         return 1;
1274 }
1275
1276 /*
1277  * AMD64 marshalling tests.
1278  */
1279
1280 typedef struct amd64_struct1 {
1281         int i;
1282         int j;
1283         int k;
1284         int l;
1285 } amd64_struct1;
1286
1287 STDCALL amd64_struct1
1288 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1289 {
1290         s.i ++;
1291         s.j ++;
1292         s.k ++;
1293         s.l ++;
1294
1295         return s;
1296 }
1297
1298 typedef struct amd64_struct2 {
1299         int i;
1300         int j;
1301 } amd64_struct2;
1302
1303 STDCALL amd64_struct2
1304 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1305 {
1306         s.i ++;
1307         s.j ++;
1308
1309         return s;
1310 }
1311
1312 typedef struct amd64_struct3 {
1313         int i;
1314 } amd64_struct3;
1315
1316 STDCALL amd64_struct3
1317 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1318 {
1319         s.i ++;
1320
1321         return s;
1322 }
1323
1324 typedef struct amd64_struct4 {
1325         double d1, d2;
1326 } amd64_struct4;
1327
1328 STDCALL amd64_struct4
1329 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1330 {
1331         s.d1 ++;
1332         s.d2 ++;
1333
1334         return s;
1335 }
1336
1337 /*
1338  * IA64 marshalling tests.
1339  */
1340 typedef struct test_struct5 {
1341         float d1, d2;
1342 } test_struct5;
1343
1344 STDCALL test_struct5
1345 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, double d3, double d4)
1346 {
1347         s.d1 += d1 + d2;
1348         s.d2 += d3 + d4;
1349
1350         return s;
1351 }
1352
1353 typedef struct test_struct6 {
1354         double d1, d2;
1355 } test_struct6;
1356
1357 STDCALL test_struct6
1358 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, double d3, double d4)
1359 {
1360         s.d1 += d1 + d2;
1361         s.d2 += d3 + d4;
1362
1363         return s;
1364 }
1365
1366 static guint32 custom_res [2];
1367
1368 STDCALL void*
1369 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1370 {
1371         /* ptr will be freed by CleanupNative, so make a copy */
1372         custom_res [0] = 0; /* not allocated by AllocHGlobal */
1373         custom_res [1] = ptr [1];
1374
1375         return &custom_res;
1376 }
1377
1378 STDCALL int
1379 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1380 {
1381         custom_res [0] = 0;
1382         custom_res [1] = i + j + 10;
1383
1384         *ptr = custom_res;
1385
1386         return 0;
1387 }
1388
1389 STDCALL int
1390 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1391 {
1392         (*ptr)[1] += i + j;
1393
1394         return 0;
1395 }
1396
1397 STDCALL void*
1398 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1399 {
1400         g_assert_not_reached ();
1401
1402         return NULL;
1403 }
1404
1405 STDCALL void*
1406 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1407 {
1408         g_assert (ptr == NULL);
1409
1410         return NULL;
1411 }
1412
1413 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1414
1415 STDCALL int
1416 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1417 {
1418         guint32 buf [2];
1419         guint32 res;
1420         guint32 *ptr;
1421
1422         buf [0] = 0;
1423         buf [1] = 10;
1424
1425         ptr = del (&buf);
1426
1427         res = ptr [1];
1428
1429 #ifdef WIN32
1430         /* FIXME: Freed with FreeHGlobal */
1431 #else
1432         g_free (ptr);
1433 #endif
1434
1435         return res;
1436 }
1437
1438 STDCALL int
1439 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1440 {
1441         void *ptr = del (NULL);
1442
1443         return (ptr == NULL) ? 15 : 0;
1444 }
1445
1446 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1447
1448 STDCALL int
1449 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1450 {
1451         return func (1);
1452 }
1453
1454 typedef struct {
1455         int a, b, c;
1456         gint64 d;
1457 } BlittableStruct;
1458         
1459 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
1460
1461 STDCALL int
1462 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
1463 {
1464         BlittableStruct ss, res;
1465
1466         ss.a = 1;
1467         ss.b = 2;
1468         ss.c = 3;
1469         ss.d = 55;
1470
1471         res = delegate (ss);
1472         if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
1473                 return 1;
1474
1475         return 0;
1476 }
1477
1478 STDCALL int
1479 mono_test_stdcall_name_mangling (int a, int b, int c)
1480 {
1481         return a + b + c;
1482 }
1483
1484 /*
1485  * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
1486  */
1487
1488 typedef struct {
1489         int i;
1490 } SmallStruct1;
1491         
1492 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
1493
1494 STDCALL int
1495 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
1496 {
1497         SmallStruct1 ss, res;
1498
1499         ss.i = 1;
1500
1501         res = delegate (ss);
1502         if (! (res.i == -1))
1503                 return 1;
1504
1505         return 0;
1506 }
1507
1508 typedef struct {
1509         gint16 i, j;
1510 } SmallStruct2;
1511         
1512 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
1513
1514 STDCALL int
1515 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
1516 {
1517         SmallStruct2 ss, res;
1518
1519         ss.i = 2;
1520         ss.j = 3;
1521
1522         res = delegate (ss);
1523         if (! ((res.i == -2) && (res.j == -3)))
1524                 return 1;
1525
1526         return 0;
1527 }
1528
1529 typedef struct {
1530         gint16 i;
1531         gint8 j;
1532 } SmallStruct3;
1533         
1534 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
1535
1536 STDCALL int
1537 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
1538 {
1539         SmallStruct3 ss, res;
1540
1541         ss.i = 1;
1542         ss.j = 2;
1543
1544         res = delegate (ss);
1545         if (! ((res.i == -1) && (res.j == -2)))
1546                 return 1;
1547
1548         return 0;
1549 }
1550
1551 typedef struct {
1552         gint16 i;
1553 } SmallStruct4;
1554         
1555 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
1556
1557 STDCALL int
1558 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
1559 {
1560         SmallStruct4 ss, res;
1561
1562         ss.i = 1;
1563
1564         res = delegate (ss);
1565         if (! (res.i == -1))
1566                 return 1;
1567
1568         return 0;
1569 }
1570
1571 typedef struct {
1572         gint64 i;
1573 } SmallStruct5;
1574         
1575 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
1576
1577 STDCALL int
1578 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
1579 {
1580         SmallStruct5 ss, res;
1581
1582         ss.i = 5;
1583
1584         res = delegate (ss);
1585         if (! (res.i == -5))
1586                 return 1;
1587
1588         return 0;
1589 }
1590
1591 typedef struct {
1592         int i, j;
1593 } SmallStruct6;
1594         
1595 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
1596
1597 STDCALL int
1598 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
1599 {
1600         SmallStruct6 ss, res;
1601
1602         ss.i = 1;
1603         ss.j = 2;
1604
1605         res = delegate (ss);
1606         if (! ((res.i == -1) && (res.j == -2)))
1607                 return 1;
1608
1609         return 0;
1610 }
1611
1612 typedef struct {
1613         int i;
1614         gint16 j;
1615 } SmallStruct7;
1616         
1617 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
1618
1619 STDCALL int
1620 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
1621 {
1622         SmallStruct7 ss, res;
1623
1624         ss.i = 1;
1625         ss.j = 2;
1626
1627         res = delegate (ss);
1628         if (! ((res.i == -1) && (res.j == -2)))
1629                 return 1;
1630
1631         return 0;
1632 }
1633
1634 typedef struct {
1635         float i;
1636 } SmallStruct8;
1637         
1638 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
1639
1640 STDCALL int
1641 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
1642 {
1643         SmallStruct8 ss, res;
1644
1645         ss.i = 1.0;
1646
1647         res = delegate (ss);
1648         if (! ((res.i == -1.0)))
1649                 return 1;
1650
1651         return 0;
1652 }
1653
1654 typedef struct {
1655         double i;
1656 } SmallStruct9;
1657         
1658 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
1659
1660 STDCALL int
1661 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
1662 {
1663         SmallStruct9 ss, res;
1664
1665         ss.i = 1.0;
1666
1667         res = delegate (ss);
1668         if (! ((res.i == -1.0)))
1669                 return 1;
1670
1671         return 0;
1672 }
1673
1674 typedef struct {
1675         float i, j;
1676 } SmallStruct10;
1677         
1678 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
1679
1680 STDCALL int
1681 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
1682 {
1683         SmallStruct10 ss, res;
1684
1685         ss.i = 1.0;
1686         ss.j = 2.0;
1687
1688         res = delegate (ss);
1689         if (! ((res.i == -1.0) && (res.j == -2.0)))
1690                 return 1;
1691
1692         return 0;
1693 }
1694
1695 typedef struct {
1696         float i;
1697         int j;
1698 } SmallStruct11;
1699         
1700 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
1701
1702 STDCALL int
1703 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
1704 {
1705         SmallStruct11 ss, res;
1706
1707         ss.i = 1.0;
1708         ss.j = 2;
1709
1710         res = delegate (ss);
1711         if (! ((res.i == -1.0) && (res.j == -2)))
1712                 return 1;
1713
1714         return 0;
1715 }
1716
1717 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
1718
1719 STDCALL int
1720 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
1721 {
1722         return del (len, NULL, arr);
1723 }
1724
1725 STDCALL int
1726 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
1727 {
1728         del (len, NULL, arr);
1729
1730         if ((arr [0] != 1) || (arr [1] != 2))
1731                 return 1;
1732         else
1733                 return 0;
1734 }
1735
1736 STDCALL int
1737 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
1738 {
1739         del (len, NULL, arr);
1740
1741         if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
1742                 return 0;
1743         else
1744                 return 1;
1745 }
1746
1747 typedef int (*CdeclDelegate) (int i, int j);
1748
1749 STDCALL int
1750 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
1751 {
1752         int i;
1753
1754         for (i = 0; i < 1000; ++i)
1755                 del (1, 2);
1756
1757         return 0;
1758 }
1759
1760 typedef char** (*ReturnStringArrayDelegate) (int i);
1761
1762 STDCALL int
1763 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
1764 {
1765         char **arr = d (2);
1766         int res;
1767
1768         if (arr == NULL)
1769                 return 3;
1770
1771         if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
1772                 res = 1;
1773         else
1774                 res = 0;
1775
1776         marshal_free (arr);
1777
1778         return res;
1779 }