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