Commit from head
[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         strncpy(s, m, n);
655         return 0;
656 }
657
658 int 
659 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
660 {
661         const char m[] = "This is my message.  Isn't it nice?";
662         gunichar2* s2;
663         glong len;
664
665         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
666         
667         len = (len * 2) + 2;
668         if (len > n)
669                 len = n;
670         memcpy (s, s2, len);
671
672         g_free (s2);
673
674         return 0;
675 }
676
677 typedef struct {
678 #ifndef __GNUC__
679     char a;
680 #endif
681 } EmptyStruct;
682
683 int
684 mono_test_marshal_empty_string_array (char **array)
685 {
686         return (array == NULL) ? 0 : 1;
687 }
688
689 int
690 mono_test_marshal_string_array (char **array)
691 {
692         if (strcmp (array [0], "ABC"))
693                 return 1;
694         if (strcmp (array [1], "DEF"))
695                 return 2;
696
697         return 0;
698 }
699
700 int
701 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
702 {
703         GError *error = NULL;
704         char *s;
705
706         s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
707         if (strcmp (s, "ABC"))
708                 return 1;
709
710         s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
711         if (strcmp (s, "DEF"))
712                 return 2;
713
714         if (strcmp (array2 [0], "ABC"))
715                 return 3;
716
717         if (strcmp (array2 [1], "DEF"))
718                 return 4;
719
720         return 0;
721 }
722
723 /* this does not work on Redhat gcc 2.96 */
724 int 
725 mono_test_empty_struct (int a, EmptyStruct es, int b)
726 {
727         printf ("mono_test_empty_struct %d %d\n", a, b);
728
729         if (a == 1 && b == 2)
730                 return 0;
731         return 1;
732 }
733
734 typedef struct {
735        char a[100];
736 } ByValStrStruct;
737
738 ByValStrStruct *
739 mono_test_byvalstr_gen (void)
740 {
741         ByValStrStruct *ret;
742        
743         ret = malloc(sizeof(ByValStrStruct));
744         memset(ret, 'a', sizeof(ByValStrStruct)-1);
745         ret->a[sizeof(ByValStrStruct)-1] = 0;
746
747         return ret;
748 }
749
750 int
751 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
752 {
753         int ret;
754
755         ret = strcmp(data->a, correctString);
756         printf ("T1: %s\n", data->a);
757         printf ("T2: %s\n", correctString);
758
759         g_free(data);
760         return (ret != 0);
761 }
762
763 int 
764 HexDump(char *data)
765 {
766         int i, res = 0;
767         char *p;
768
769         printf ("HEXDUMP DEFAULT VERSION\n");
770
771         p = data;
772         for (i=0; i < 8; ++i)
773         {
774                 res += *p;
775                 printf("%0x ", (int) *(p++));
776         }
777         putchar('\n');
778
779         return res;
780 }
781
782 int 
783 HexDumpA(char *data)
784 {
785         int i, res = 0;
786         char *p;
787
788         printf ("HEXDUMP ANSI VERSION\n");
789
790         p = data;
791         for (i=0; i < 8; ++i)
792         {
793                 res += *p;
794                 printf("%0x ", (int) *(p++));
795         }
796         putchar('\n');
797
798         return res + 100000;
799 }
800
801 int 
802 HexDump1W(char *data)
803 {
804         int i, res = 0;
805         char *p;
806
807         printf ("HEXDUMP UNICODE VERSION\n");
808
809         p = data;
810         for (i=0; i < 8; ++i)
811         {
812                 res += *p;
813                 printf("%0x ", (int) *(p++));
814         }
815         putchar('\n');
816
817         return res + 1000000;
818 }
819
820 typedef int (*intcharFunc)(const char*);
821
822 void 
823 callFunction (intcharFunc f)
824 {
825         f ("ABC");
826 }
827
828 int
829 printInt (int* number)
830 {
831         printf( "<%d>\n", *number );
832         return *number + 1;
833 }
834
835
836 typedef struct {
837         const char* str;
838         int i;
839 } SimpleObj;
840
841 int
842 class_marshal_test0 (SimpleObj *obj1)
843 {
844         printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
845
846         if (strcmp(obj1->str, "T1"))
847                 return -1;
848         if (obj1->i != 4)
849                 return -2;
850
851         return 0;
852 }
853
854 int
855 class_marshal_test4 (SimpleObj *obj1)
856 {
857         if (obj1)
858                 return -1;
859
860         return 0;
861 }
862
863 void
864 class_marshal_test1 (SimpleObj **obj1)
865 {
866         SimpleObj *res = malloc (sizeof (SimpleObj));
867
868         res->str = "ABC";
869         res->i = 5;
870
871         *obj1 = res;
872 }
873
874 int
875 class_marshal_test2 (SimpleObj **obj1)
876 {
877         printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
878
879         if (strcmp((*obj1)->str, "ABC"))
880                 return -1;
881         if ((*obj1)->i != 5)
882                 return -2;
883
884         return 0;
885 }
886
887 int
888 string_marshal_test0 (char *str)
889 {
890         if (strcmp (str, "TEST0"))
891                 return -1;
892
893         return 0;
894 }
895
896 void
897 string_marshal_test1 (const char **str)
898 {
899         *str = "TEST1";
900 }
901
902 int
903 string_marshal_test2 (char **str)
904 {
905         printf ("string_marshal_test2 %s\n", *str);
906
907         if (strcmp (*str, "TEST1"))
908                 return -1;
909
910         return 0;
911 }
912
913 int
914 string_marshal_test3 (char *str)
915 {
916         if (str)
917                 return -1;
918
919         return 0;
920 }
921
922 const char *
923 functionReturningString (void)
924 {
925     return "ABC";
926 }
927
928 typedef struct {
929         int a;
930         int b;
931 } VectorList;
932
933
934 VectorList* TestVectorList (VectorList *vl)
935 {
936         printf ("TestVectorList %d %d\n", vl->a, vl->b);
937
938         vl->a++;
939         vl->b++;
940
941         return vl;
942 }
943
944
945 typedef struct _OSVERSIONINFO
946
947         int a; 
948         int b; 
949 } OSVERSIONINFO; 
950
951 int 
952 GetVersionEx (OSVERSIONINFO *osvi)
953 {
954
955         printf ("GOT %d %d\n", osvi->a, osvi->b);
956
957         osvi->a += 1;
958         osvi->b += 1;
959
960         return osvi->a + osvi->b;
961 }
962
963 int 
964 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO *osvi)
965 {
966
967         printf ("GOT %d %d\n", osvi->a, osvi->b);
968
969         osvi->a += 1;
970         osvi->b += 1;
971
972         return osvi->a + osvi->b;
973 }
974
975 typedef struct {
976         double x;
977         double y;
978 } point;
979
980 int
981 mono_test_marshal_point (point pt)
982 {
983         printf("point %g %g\n", pt.x, pt.y);
984         if (pt.x == 1.25 && pt.y == 3.5)
985                 return 0;
986
987         return 1;
988 }
989
990 typedef struct {
991         int x;
992         double y;
993 } mixed_point;
994
995 int
996 mono_test_marshal_mixed_point (mixed_point pt)
997 {
998         printf("mixed point %d %g\n", pt.x, pt.y);
999         if (pt.x == 5 && pt.y == 6.75)
1000                 return 0;
1001
1002         return 1;
1003 }
1004
1005 int
1006 time_t_sizeof (void)
1007 {
1008         return sizeof (time_t);
1009 }
1010
1011 time_t
1012 mono_test_marshal_time_t (time_t *t)
1013 {
1014         /* Skip forward an hour */
1015         /* t can be unaligned on 64bit machines at present owing to the magic 4 bytes currently added
1016            for custom marshaling which may or may not be correct... cope for the moment */
1017         time_t s;
1018         memcpy(&s, t, sizeof *t);
1019         return s + 3600;
1020 }
1021
1022 int 
1023 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1024 {
1025     int res = 1;
1026     if (*b1 != 0 && *b1 != 1)
1027         return 1;
1028     if (*b2 != 0 && *b2 != -1) /* variant_bool */
1029         return 1;
1030     if (*b3 != 0 && *b3 != 1)
1031         return 1;
1032     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1033         res = 0;
1034     *b1 = !*b1;
1035     *b2 = ~*b2;
1036     *b3 = !*b3;
1037     return res;
1038 }
1039
1040 struct BoolStruct
1041 {
1042     int i;
1043     char b1;
1044     short b2; /* variant_bool */
1045     int b3;
1046 };
1047
1048 int 
1049 marshal_test_bool_struct(struct BoolStruct *s)
1050 {
1051     int res = 1;
1052     if (s->b1 != 0 && s->b1 != 1)
1053         return 1;
1054     if (s->b2 != 0 && s->b2 != -1)
1055         return 1;
1056     if (s->b3 != 0 && s->b3 != 1)
1057         return 1;
1058     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1059         res = 0;
1060     s->b1 = !s->b1;
1061     s->b2 = ~s->b2;
1062     s->b3 = !s->b3;
1063     return res;
1064 }
1065
1066 #ifdef WIN32
1067 extern __declspec(dllimport) __stdcall void SetLastError(int x);
1068 #endif
1069
1070 void
1071 mono_test_last_error (int err)
1072 {
1073 #ifdef WIN32
1074         SetLastError (err);
1075 #else
1076         errno = err;
1077 #endif
1078 }
1079
1080 int
1081 mono_test_asany (void *ptr, int what)
1082 {
1083         switch (what) {
1084         case 1:
1085                 return (*(int*)ptr == 5) ? 0 : 1;
1086         case 2:
1087                 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1088         case 3: {
1089                 simplestruct2 ss = *(simplestruct2*)ptr;
1090
1091                 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1092             !strcmp (ss.d, "TEST") && 
1093             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1094                         return 0;
1095                 else
1096                         return 1;
1097         }
1098         case 4: {
1099                 GError *error = NULL;
1100                 char *s;
1101
1102                 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1103                 if (!strcmp (s, "ABC"))
1104                         return 0;
1105                 else
1106                         return 1;
1107         }
1108         default:
1109                 g_assert_not_reached ();
1110         }
1111
1112         return 1;
1113 }
1114
1115
1116