Bring another patch from HEAD from Zoltan
[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 unsigned short*
9 test_lpwstr_marshal (unsigned short* chars, long length)
10 {
11         int i = 0;
12         unsigned short *res;
13
14         res = malloc (2 * (length + 1));
15
16         printf("test_lpwstr_marshal()\n");
17         
18         while ( i < length ) {
19                 printf("X|%u|\n", chars[i]);
20                 res [i] = chars[i];
21                 i++;
22         }
23
24         res [i] = 0;
25
26         return res;
27 }
28
29 typedef struct {
30         int b;
31         int a;
32         int c;
33 } union_test_1_type;
34
35 int mono_union_test_1 (union_test_1_type u1) {
36         printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
37         return u1.a + u1.b + u1.c;
38 }
39
40 int mono_return_int (int a) {
41         printf ("Got value %d\n", a);
42         return a;
43 }
44
45 struct ss
46 {
47         int i;
48 };
49
50 int mono_return_int_ss (struct ss a) {
51         printf ("Got value %d\n", a.i);
52         return a.i;
53 }
54
55 struct ss mono_return_ss (struct ss a) {
56         printf ("Got value %d\n", a.i);
57         a.i++;
58         return a;
59 }
60
61 struct sc1
62 {
63         char c[1];
64 };
65
66 struct sc1 mono_return_sc1 (struct sc1 a) {
67         printf ("Got value %d\n", a.c[0]);
68         a.c[0]++;
69         return a;
70 }
71
72
73 struct sc3
74 {
75         char c[3];
76 };
77
78 struct sc3 mono_return_sc3 (struct sc3 a) {
79         printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
80         a.c[0]++;
81         a.c[1] += 2;
82         a.c[2] += 3;
83         return a;
84 }
85
86 struct sc5
87 {
88         char c[5];
89 };
90
91 struct sc5 mono_return_sc5 (struct sc5 a) {
92         printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
93         a.c[0]++;
94         a.c[1] += 2;
95         a.c[2] += 3;
96         a.c[3] += 4;
97         a.c[4] += 5;
98         return a;
99 }
100
101 union su
102 {
103         int i1;
104         int i2;
105 };
106
107 int mono_return_int_su (union su a) {
108         printf ("Got value %d\n", a.i1);
109         return a.i1;
110 }
111
112 int mono_test_many_int_arguments (int a, int b, int c, int d, int e,
113                                   int f, int g, int h, int i, int j);
114 short mono_test_many_short_arguments (short a, short b, short c, short d, short e,
115                                       short f, short g, short h, short i, short j);
116 char mono_test_many_char_arguments (char a, char b, char c, char d, char e,
117                                     char f, char g, char h, char i, char j);
118
119 int
120 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)
121 {
122         return a + b + c + d + e + f + g + h + i + j;
123 }
124
125 short
126 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)
127 {
128         return a + b + c + d + e + f + g + h + i + j;
129 }
130
131 char
132 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)
133 {
134         return a + b + c + d + e + f + g + h + i + j;
135 }
136
137 float
138 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)
139 {
140         return a + b + c + d + e + f + g + h + i + j;
141 }
142
143 double
144 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)
145 {
146         return a + b + c + d + e + f + g + h + i + j;
147 }
148
149 double
150 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
151 {
152         return a + b + c + d + e;
153 }
154
155 int
156 mono_test_puts_static (char *s)
157 {
158         printf ("TEST %s\n", s);
159         return 1;
160 }
161
162 typedef int (*SimpleDelegate3) (int a, int b);
163
164 int
165 mono_invoke_delegate (SimpleDelegate3 delegate)
166 {
167         int res;
168
169         printf ("start invoke %p\n", delegate);
170
171         res = delegate (2, 3);
172
173         printf ("end invoke\n");
174
175         return res;
176 }
177
178 int 
179 mono_test_marshal_char (short a1)
180 {
181         if (a1 == 'a')
182                 return 0;
183         
184         return 1;
185 }
186
187 void
188 mono_test_marshal_char_array (gunichar2 *s)
189 {
190         const char m[] = "abcdef";
191         gunichar2* s2;
192         glong len;
193
194         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
195         
196         len = (len * 2) + 2;
197         memcpy (s, s2, len);
198
199         g_free (s2);
200 }
201
202 int
203 mono_test_empty_pinvoke (int i)
204 {
205         return i;
206 }
207
208 int 
209 mono_test_marshal_bool_byref (int a, int *b, int c)
210 {
211     int res = *b;
212
213         *b = 1;
214
215         return res;
216 }
217
218 int 
219 mono_test_marshal_array (int *a1)
220 {
221         int i, sum = 0;
222
223         for (i = 0; i < 50; i++)
224                 sum += a1 [i];
225         
226         return sum;
227 }
228
229 int 
230 mono_test_marshal_inout_array (int *a1)
231 {
232         int i, sum = 0;
233
234         for (i = 0; i < 50; i++) {
235                 sum += a1 [i];
236                 a1 [i] = 50 - a1 [i];
237         }
238         
239         return sum;
240 }
241
242 int 
243 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
244 {
245         int i, sum = 0;
246
247         for (i = 0; i < 10; i++) {
248                 a1 [i] = 'F';
249         }
250         
251         return sum;
252 }
253
254 typedef struct {
255         int a;
256         int b;
257         int c;
258         const char *d;
259 } simplestruct;
260
261 simplestruct
262 mono_test_return_vtype (int i)
263 {
264         simplestruct res;
265
266         res.a = 0;
267         res.b = 1;
268         res.c = 0;
269         res.d = "TEST";
270
271         return res;
272 }
273
274 void
275 mono_test_delegate_struct (void)
276 {
277         printf ("TEST\n");
278 }
279
280 typedef char* (*ReturnStringDelegate) (const char *s);
281
282 char *
283 mono_test_return_string (ReturnStringDelegate func)
284 {
285         char *res;
286
287         printf ("mono_test_return_string\n");
288
289         res = func ("TEST");
290
291         printf ("got string: %s\n", res);
292         return res;
293 }
294
295 typedef int (*RefVTypeDelegate) (int a, simplestruct *ss, int b);
296
297 int
298 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
299 {
300         if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
301             !strcmp (ss->d, "TEST1")) {
302                 ss->a = 1;
303                 ss->b = 0;
304                 ss->c = 1;
305                 ss->d = "TEST2";
306         
307                 return func (a, ss, b);
308         }
309
310         return 1;
311 }
312
313 typedef int (*OutVTypeDelegate) (int a, simplestruct *ss, int b);
314
315 int
316 mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
317 {
318         int res;
319
320         /* Check that the input pointer is ignored */
321         ss->d = (gpointer)0x12345678;
322
323         func (a, ss, b);
324
325         if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
326                 return 0;
327         else
328                 return 1;
329 }
330
331 typedef struct {
332         int a;
333         int (*func) (int);
334         int (*func2) (int);
335 } DelegateStruct;
336
337 int 
338 mono_test_marshal_delegate_struct (DelegateStruct ds)
339 {
340         return ds.func (ds.a) + ds.func2 (ds.a);
341 }
342
343 int 
344 mono_test_marshal_struct (simplestruct ss)
345 {
346         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
347             !strcmp (ss.d, "TEST"))
348                 return 0;
349
350         return 1;
351 }
352
353 typedef struct {
354         int a;
355         int b;
356         int c;
357         char *d;
358         unsigned char e;
359         double f;
360         unsigned char g;
361         guint64 h;
362 } simplestruct2;
363
364 int
365 mono_test_marshal_struct2 (simplestruct2 ss)
366 {
367         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
368             !strcmp (ss.d, "TEST") && 
369             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
370                 return 0;
371
372         return 1;
373 }
374
375 /* on HP some of the struct should be on the stack and not in registers */
376 int
377 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
378 {
379         if (i != 10 || j != 11 || k != 12)
380                 return 1;
381         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
382             !strcmp (ss.d, "TEST") && 
383             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
384                 return 0;
385
386         return 1;
387 }
388
389 int
390 mono_test_marshal_struct_array (simplestruct2 *ss)
391 {
392         if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
393                    !strcmp (ss[0].d, "TEST") && 
394                    ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
395                 return 1;
396
397         if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
398                    !strcmp (ss[1].d, "TEST2") && 
399                    ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
400                 return 1;
401
402         return 0;
403 }
404
405 typedef struct long_align_struct {
406         gint32 a;
407         gint64 b;
408         gint64 c;
409 } long_align_struct;
410
411 int
412 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
413 {
414         return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
415 }
416
417 simplestruct2 *
418 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
419 {
420         simplestruct2 *res;
421
422         if (!ss)
423                 return NULL;
424
425         if (i != 10 || j != 11 || k != 12 || l != 14)
426                 return NULL;
427         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
428                    !strcmp (ss->d, "TEST") && 
429                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
430                 return NULL;
431
432         res = g_new0 (simplestruct2, 1);
433         memcpy (res, ss, sizeof (simplestruct2));
434         return res;
435 }
436
437 int
438 mono_test_marshal_byref_class (simplestruct2 **ssp)
439 {
440         simplestruct2 *ss = *ssp;
441         simplestruct2 *res;
442         
443         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
444                    !strcmp (ss->d, "TEST") && 
445                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
446                 return 1;
447
448         res = g_new0 (simplestruct2, 1);
449         memcpy (res, ss, sizeof (simplestruct2));
450         res->d = (char*)"TEST-RES";
451
452         *ssp = res;
453         return 0;
454 }
455
456 #ifdef WIN32
457 typedef int (__stdcall *SimpleDelegate) (int a);
458 #else
459 typedef int (*SimpleDelegate) (int a);
460 #endif
461
462 static void *
463 get_sp (void)
464 {
465         int i;
466         void *p;
467
468         p = &i;
469         return p;
470 }
471
472 int
473 mono_test_marshal_delegate (SimpleDelegate delegate)
474 {
475         void *sp1, *sp2;
476
477         /* Check that the delegate wrapper is stdcall */
478         delegate (2);
479         sp1 = get_sp ();
480         delegate (2);
481         sp2 = get_sp ();
482         g_assert (sp1 == sp2);
483
484         return delegate (2);
485 }
486
487 typedef simplestruct (*SimpleDelegate2) (simplestruct ss);
488
489 int
490 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
491 {
492         simplestruct ss, res;
493
494         ss.a = 0;
495         ss.b = 1;
496         ss.c = 0;
497         ss.d = "TEST";
498
499         res = delegate (ss);
500         if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES")))
501                 return 1;
502
503         return 0;
504 }
505
506 typedef simplestruct* (*SimpleDelegate4) (simplestruct *ss);
507
508 int
509 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
510 {
511         simplestruct ss;
512         simplestruct *res;
513
514         ss.a = 0;
515         ss.b = 1;
516         ss.c = 0;
517         ss.d = "TEST";
518
519         /* Check argument */
520         res = delegate (&ss);
521         if (!res)
522                 return 1;
523
524         /* Check return value */
525         if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
526                 return 2;
527
528         /* Check NULL argument and NULL result */
529         res = delegate (NULL);
530         if (res)
531                 return 3;
532
533         return 0;
534 }
535
536 typedef int (*SimpleDelegate5) (simplestruct **ss);
537
538 int
539 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
540 {
541         simplestruct ss;
542         int res;
543         simplestruct *ptr;
544
545         ss.a = 0;
546         ss.b = 1;
547         ss.c = 0;
548         ss.d = "TEST";
549
550         ptr = &ss;
551
552         res = delegate (&ptr);
553         if (res != 0)
554                 return 1;
555
556         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
557                 return 2;
558
559         return 0;
560 }
561
562 int
563 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
564 {
565         int res;
566
567         res = delegate (NULL);
568
569         return 0;
570 }
571
572 typedef int (*SimpleDelegate7) (simplestruct **ss);
573
574 int
575 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
576 {
577         int res;
578         simplestruct *ptr;
579
580         /* Check that the input pointer is ignored */
581         ptr = (gpointer)0x12345678;
582
583         res = delegate (&ptr);
584         if (res != 0)
585                 return 1;
586
587         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
588                 return 2;
589
590         return 0;
591 }
592
593 typedef int (*SimpleDelegate8) (gunichar2 *s);
594
595 int
596 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
597 {
598         return delegate (s);
599 }
600
601 typedef int (*return_int_fnt) (int i);
602 typedef int (*SimpleDelegate9) (return_int_fnt *d);
603
604 int
605 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
606 {
607         return delegate (ftn);
608 }
609
610 int
611 return_self (int i)
612 {
613         return i;
614 }
615
616 int
617 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
618 {
619         return delegate (return_self);
620 }
621
622 typedef int (*PrimitiveByrefDelegate) (int *i);
623
624 int
625 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
626 {
627         int i = 1;
628
629         int res = delegate (&i);
630         if (res != 0)
631                 return res;
632
633         if (i != 2)
634                 return 2;
635
636         return 0;
637 }
638
639 typedef int (*return_int_delegate) (int i);
640
641 typedef return_int_delegate (*ReturnDelegateDelegate) ();
642
643 int
644 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
645 {
646         return (d ()) (55);
647 }
648
649
650 int 
651 mono_test_marshal_stringbuilder (char *s, int n)
652 {
653         const char m[] = "This is my message.  Isn't it nice?";
654
655         if (strcmp (s, "ABCD") != 0)
656                 return 1;
657         strncpy(s, m, n);
658         return 0;
659 }
660
661 int 
662 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
663 {
664         const char m[] = "This is my message.  Isn't it nice?";
665         gunichar2* s2;
666         glong len;
667
668         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
669         
670         len = (len * 2) + 2;
671         if (len > n)
672                 len = n;
673         memcpy (s, s2, len);
674
675         g_free (s2);
676
677         return 0;
678 }
679
680 typedef struct {
681 #ifndef __GNUC__
682     char a;
683 #endif
684 } EmptyStruct;
685
686 int
687 mono_test_marshal_empty_string_array (char **array)
688 {
689         return (array == NULL) ? 0 : 1;
690 }
691
692 int
693 mono_test_marshal_string_array (char **array)
694 {
695         if (strcmp (array [0], "ABC"))
696                 return 1;
697         if (strcmp (array [1], "DEF"))
698                 return 2;
699
700         return 0;
701 }
702
703 int
704 mono_test_marshal_stringbuilder_array (char **array)
705 {
706         if (strcmp (array [0], "ABC"))
707                 return 1;
708         if (strcmp (array [1], "DEF"))
709                 return 2;
710
711         strcpy (array [0], "DEF");
712         strcpy (array [1], "ABC");
713
714         return 0;
715 }
716
717 int
718 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
719 {
720         GError *error = NULL;
721         char *s;
722
723         s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
724         if (strcmp (s, "ABC"))
725                 return 1;
726
727         s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
728         if (strcmp (s, "DEF"))
729                 return 2;
730
731         if (strcmp (array2 [0], "ABC"))
732                 return 3;
733
734         if (strcmp (array2 [1], "DEF"))
735                 return 4;
736
737         return 0;
738 }
739
740 /* this does not work on Redhat gcc 2.96 */
741 int 
742 mono_test_empty_struct (int a, EmptyStruct es, int b)
743 {
744         printf ("mono_test_empty_struct %d %d\n", a, b);
745
746         if (a == 1 && b == 2)
747                 return 0;
748         return 1;
749 }
750
751 typedef struct {
752        char a[100];
753 } ByValStrStruct;
754
755 ByValStrStruct *
756 mono_test_byvalstr_gen (void)
757 {
758         ByValStrStruct *ret;
759        
760         ret = malloc(sizeof(ByValStrStruct));
761         memset(ret, 'a', sizeof(ByValStrStruct)-1);
762         ret->a[sizeof(ByValStrStruct)-1] = 0;
763
764         return ret;
765 }
766
767 int
768 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
769 {
770         int ret;
771
772         ret = strcmp(data->a, correctString);
773         printf ("T1: %s\n", data->a);
774         printf ("T2: %s\n", correctString);
775
776         g_free(data);
777         return (ret != 0);
778 }
779
780 int 
781 HexDump(char *data)
782 {
783         int i, res = 0;
784         char *p;
785
786         printf ("HEXDUMP DEFAULT VERSION\n");
787
788         p = data;
789         for (i=0; i < 8; ++i)
790         {
791                 res += *p;
792                 printf("%0x ", (int) *(p++));
793         }
794         putchar('\n');
795
796         return res;
797 }
798
799 int 
800 HexDumpA(char *data)
801 {
802         int i, res = 0;
803         char *p;
804
805         printf ("HEXDUMP ANSI VERSION\n");
806
807         p = data;
808         for (i=0; i < 8; ++i)
809         {
810                 res += *p;
811                 printf("%0x ", (int) *(p++));
812         }
813         putchar('\n');
814
815         return res + 100000;
816 }
817
818 int 
819 HexDump1W(char *data)
820 {
821         int i, res = 0;
822         char *p;
823
824         printf ("HEXDUMP UNICODE VERSION\n");
825
826         p = data;
827         for (i=0; i < 8; ++i)
828         {
829                 res += *p;
830                 printf("%0x ", (int) *(p++));
831         }
832         putchar('\n');
833
834         return res + 1000000;
835 }
836
837 typedef int (*intcharFunc)(const char*);
838
839 void 
840 callFunction (intcharFunc f)
841 {
842         f ("ABC");
843 }
844
845 int
846 printInt (int* number)
847 {
848         printf( "<%d>\n", *number );
849         return *number + 1;
850 }
851
852
853 typedef struct {
854         const char* str;
855         int i;
856 } SimpleObj;
857
858 int
859 class_marshal_test0 (SimpleObj *obj1)
860 {
861         printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
862
863         if (strcmp(obj1->str, "T1"))
864                 return -1;
865         if (obj1->i != 4)
866                 return -2;
867
868         return 0;
869 }
870
871 int
872 class_marshal_test4 (SimpleObj *obj1)
873 {
874         if (obj1)
875                 return -1;
876
877         return 0;
878 }
879
880 void
881 class_marshal_test1 (SimpleObj **obj1)
882 {
883         SimpleObj *res = malloc (sizeof (SimpleObj));
884
885         res->str = "ABC";
886         res->i = 5;
887
888         *obj1 = res;
889 }
890
891 int
892 class_marshal_test2 (SimpleObj **obj1)
893 {
894         printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
895
896         if (strcmp((*obj1)->str, "ABC"))
897                 return -1;
898         if ((*obj1)->i != 5)
899                 return -2;
900
901         return 0;
902 }
903
904 int
905 string_marshal_test0 (char *str)
906 {
907         if (strcmp (str, "TEST0"))
908                 return -1;
909
910         return 0;
911 }
912
913 void
914 string_marshal_test1 (const char **str)
915 {
916         *str = "TEST1";
917 }
918
919 int
920 string_marshal_test2 (char **str)
921 {
922         printf ("string_marshal_test2 %s\n", *str);
923
924         if (strcmp (*str, "TEST1"))
925                 return -1;
926
927         return 0;
928 }
929
930 int
931 string_marshal_test3 (char *str)
932 {
933         if (str)
934                 return -1;
935
936         return 0;
937 }
938
939 const char *
940 functionReturningString (void)
941 {
942     return "ABC";
943 }
944
945 typedef struct {
946         int a;
947         int b;
948 } VectorList;
949
950
951 VectorList* TestVectorList (VectorList *vl)
952 {
953         printf ("TestVectorList %d %d\n", vl->a, vl->b);
954
955         vl->a++;
956         vl->b++;
957
958         return vl;
959 }
960
961
962 typedef struct _OSVERSIONINFO
963
964         int a; 
965         int b; 
966 } OSVERSIONINFO; 
967
968 int 
969 GetVersionEx (OSVERSIONINFO *osvi)
970 {
971
972         printf ("GOT %d %d\n", osvi->a, osvi->b);
973
974         osvi->a += 1;
975         osvi->b += 1;
976
977         return osvi->a + osvi->b;
978 }
979
980 int 
981 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO *osvi)
982 {
983
984         printf ("GOT %d %d\n", osvi->a, osvi->b);
985
986         osvi->a += 1;
987         osvi->b += 1;
988
989         return osvi->a + osvi->b;
990 }
991
992 typedef struct {
993         double x;
994         double y;
995 } point;
996
997 int
998 mono_test_marshal_point (point pt)
999 {
1000         printf("point %g %g\n", pt.x, pt.y);
1001         if (pt.x == 1.25 && pt.y == 3.5)
1002                 return 0;
1003
1004         return 1;
1005 }
1006
1007 typedef struct {
1008         int x;
1009         double y;
1010 } mixed_point;
1011
1012 int
1013 mono_test_marshal_mixed_point (mixed_point pt)
1014 {
1015         printf("mixed point %d %g\n", pt.x, pt.y);
1016         if (pt.x == 5 && pt.y == 6.75)
1017                 return 0;
1018
1019         return 1;
1020 }
1021
1022 int
1023 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1024 {
1025         if (pt->x != 5 || pt->y != 6.75)
1026                 return 1;
1027
1028         pt->x = 10;
1029         pt->y = 12.35;
1030
1031         return 0;
1032 }
1033
1034 int
1035 time_t_sizeof (void)
1036 {
1037         return sizeof (time_t);
1038 }
1039
1040 time_t
1041 mono_test_marshal_time_t (time_t *t)
1042 {
1043         /* Skip forward an hour */
1044         /* t can be unaligned on 64bit machines at present owing to the magic 4 bytes currently added
1045            for custom marshaling which may or may not be correct... cope for the moment */
1046         time_t s;
1047         memcpy(&s, t, sizeof *t);
1048         return s + 3600;
1049 }
1050
1051 int 
1052 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1053 {
1054     int res = 1;
1055     if (*b1 != 0 && *b1 != 1)
1056         return 1;
1057     if (*b2 != 0 && *b2 != -1) /* variant_bool */
1058         return 1;
1059     if (*b3 != 0 && *b3 != 1)
1060         return 1;
1061     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1062         res = 0;
1063     *b1 = !*b1;
1064     *b2 = ~*b2;
1065     *b3 = !*b3;
1066     return res;
1067 }
1068
1069 struct BoolStruct
1070 {
1071     int i;
1072     char b1;
1073     short b2; /* variant_bool */
1074     int b3;
1075 };
1076
1077 int 
1078 marshal_test_bool_struct(struct BoolStruct *s)
1079 {
1080     int res = 1;
1081     if (s->b1 != 0 && s->b1 != 1)
1082         return 1;
1083     if (s->b2 != 0 && s->b2 != -1)
1084         return 1;
1085     if (s->b3 != 0 && s->b3 != 1)
1086         return 1;
1087     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1088         res = 0;
1089     s->b1 = !s->b1;
1090     s->b2 = ~s->b2;
1091     s->b3 = !s->b3;
1092     return res;
1093 }
1094
1095 #ifdef WIN32
1096 extern __declspec(dllimport) __stdcall void SetLastError(int x);
1097 #endif
1098
1099 void
1100 mono_test_last_error (int err)
1101 {
1102 #ifdef WIN32
1103         SetLastError (err);
1104 #else
1105         errno = err;
1106 #endif
1107 }
1108
1109 int
1110 mono_test_asany (void *ptr, int what)
1111 {
1112         switch (what) {
1113         case 1:
1114                 return (*(int*)ptr == 5) ? 0 : 1;
1115         case 2:
1116                 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1117         case 3: {
1118                 simplestruct2 ss = *(simplestruct2*)ptr;
1119
1120                 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1121             !strcmp (ss.d, "TEST") && 
1122             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1123                         return 0;
1124                 else
1125                         return 1;
1126         }
1127         case 4: {
1128                 GError *error = NULL;
1129                 char *s;
1130
1131                 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1132                 if (!strcmp (s, "ABC"))
1133                         return 0;
1134                 else
1135                         return 1;
1136         }
1137         default:
1138                 g_assert_not_reached ();
1139         }
1140
1141         return 1;
1142 }
1143
1144
1145