Thu May 27 15:50:52 CEST 2004 Paolo Molaro <lupus@ximian.com>
[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 struct {
314         int a;
315         int (*func) (int);
316         int (*func2) (int);
317 } DelegateStruct;
318
319 int 
320 mono_test_marshal_delegate_struct (DelegateStruct ds)
321 {
322         return ds.func (ds.a) + ds.func2 (ds.a);
323 }
324
325 int 
326 mono_test_marshal_struct (simplestruct ss)
327 {
328         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
329             !strcmp (ss.d, "TEST"))
330                 return 0;
331
332         return 1;
333 }
334
335 typedef struct {
336         int a;
337         int b;
338         int c;
339         char *d;
340         unsigned char e;
341         double f;
342         unsigned char g;
343         guint64 h;
344 } simplestruct2;
345
346 int
347 mono_test_marshal_struct2 (simplestruct2 ss)
348 {
349         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
350             !strcmp (ss.d, "TEST") && 
351             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
352                 return 0;
353
354         return 1;
355 }
356
357 /* on HP some of the struct should be on the stack and not in registers */
358 int
359 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
360 {
361         if (i != 10 || j != 11 || k != 12)
362                 return 1;
363         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
364             !strcmp (ss.d, "TEST") && 
365             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
366                 return 0;
367
368         return 1;
369 }
370
371 int
372 mono_test_marshal_struct_array (simplestruct2 *ss)
373 {
374         if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
375                    !strcmp (ss[0].d, "TEST") && 
376                    ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
377                 return 1;
378
379         if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
380                    !strcmp (ss[1].d, "TEST2") && 
381                    ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
382                 return 1;
383
384         return 0;
385 }
386
387 typedef struct long_align_struct {
388         gint32 a;
389         gint64 b;
390         gint64 c;
391 } long_align_struct;
392
393 int
394 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
395 {
396         return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
397 }
398
399 simplestruct2 *
400 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
401 {
402         simplestruct2 *res;
403
404         if (!ss)
405                 return NULL;
406
407         if (i != 10 || j != 11 || k != 12 || l != 14)
408                 return NULL;
409         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
410                    !strcmp (ss->d, "TEST") && 
411                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
412                 return NULL;
413
414         res = g_new0 (simplestruct2, 1);
415         memcpy (res, ss, sizeof (simplestruct2));
416         return res;
417 }
418
419 int
420 mono_test_marshal_byref_class (simplestruct2 **ssp)
421 {
422         simplestruct2 *ss = *ssp;
423         simplestruct2 *res;
424         
425         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
426                    !strcmp (ss->d, "TEST") && 
427                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
428                 return 1;
429
430         res = g_new0 (simplestruct2, 1);
431         memcpy (res, ss, sizeof (simplestruct2));
432         res->d = (char*)"TEST-RES";
433
434         *ssp = res;
435         return 0;
436 }
437
438 #ifdef WIN32
439 typedef int (__stdcall *SimpleDelegate) (int a);
440 #else
441 typedef int (*SimpleDelegate) (int a);
442 #endif
443
444 static void *
445 get_sp (void)
446 {
447         int i;
448         void *p;
449
450         p = &i;
451         return p;
452 }
453
454 int
455 mono_test_marshal_delegate (SimpleDelegate delegate)
456 {
457         void *sp1, *sp2;
458
459         /* Check that the delegate wrapper is stdcall */
460         delegate (2);
461         sp1 = get_sp ();
462         delegate (2);
463         sp2 = get_sp ();
464         g_assert (sp1 == sp2);
465
466         return delegate (2);
467 }
468
469 typedef simplestruct (*SimpleDelegate2) (simplestruct ss);
470
471 int
472 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
473 {
474         simplestruct ss, res;
475
476         ss.a = 0;
477         ss.b = 1;
478         ss.c = 0;
479         ss.d = "TEST";
480
481         res = delegate (ss);
482         if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES")))
483                 return 1;
484
485         return 0;
486 }
487
488 typedef simplestruct* (*SimpleDelegate4) (simplestruct *ss);
489
490 int
491 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
492 {
493         simplestruct ss;
494         simplestruct *res;
495
496         ss.a = 0;
497         ss.b = 1;
498         ss.c = 0;
499         ss.d = "TEST";
500
501         /* Check argument */
502         res = delegate (&ss);
503         if (!res)
504                 return 1;
505
506         /* Check return value */
507         if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
508                 return 2;
509
510         /* Check NULL argument and NULL result */
511         res = delegate (NULL);
512         if (res)
513                 return 3;
514
515         return 0;
516 }
517
518 typedef int (*SimpleDelegate5) (simplestruct **ss);
519
520 int
521 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
522 {
523         simplestruct ss;
524         int res;
525         simplestruct *ptr;
526
527         ss.a = 0;
528         ss.b = 1;
529         ss.c = 0;
530         ss.d = "TEST";
531
532         ptr = &ss;
533
534         res = delegate (&ptr);
535         if (res != 0)
536                 return 1;
537
538         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
539                 return 2;
540
541         return 0;
542 }
543
544 int
545 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
546 {
547         int res;
548
549         res = delegate (NULL);
550
551         return 0;
552 }
553
554 typedef int (*SimpleDelegate7) (simplestruct **ss);
555
556 int
557 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
558 {
559         int res;
560         simplestruct *ptr;
561
562         /* Check that the input pointer is ignored */
563         ptr = (gpointer)0x12345678;
564
565         res = delegate (&ptr);
566         if (res != 0)
567                 return 1;
568
569         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
570                 return 2;
571
572         return 0;
573 }
574
575 typedef int (*SimpleDelegate8) (gunichar2 *s);
576
577 int
578 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
579 {
580         return delegate (s);
581 }
582
583 typedef int (*return_int_fnt) (int i);
584 typedef int (*SimpleDelegate9) (return_int_fnt *d);
585
586 int
587 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
588 {
589         return delegate (ftn);
590 }
591
592 int
593 return_self (int i)
594 {
595         return i;
596 }
597
598 int
599 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
600 {
601         return delegate (return_self);
602 }
603
604 int 
605 mono_test_marshal_stringbuilder (char *s, int n)
606 {
607         const char m[] = "This is my message.  Isn't it nice?";
608         strncpy(s, m, n);
609         return 0;
610 }
611
612 int 
613 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
614 {
615         const char m[] = "This is my message.  Isn't it nice?";
616         gunichar2* s2;
617         glong len;
618
619         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
620         
621         len = (len * 2) + 2;
622         if (len > n)
623                 len = n;
624         memcpy (s, s2, len);
625
626         g_free (s2);
627
628         return 0;
629 }
630
631 typedef struct {
632 #ifndef __GNUC__
633     char a;
634 #endif
635 } EmptyStruct;
636
637 int
638 mono_test_marshal_empty_string_array (char **array)
639 {
640         return (array == NULL) ? 0 : 1;
641 }
642
643 int
644 mono_test_marshal_string_array (char **array)
645 {
646         if (strcmp (array [0], "ABC"))
647                 return 1;
648         if (strcmp (array [1], "DEF"))
649                 return 2;
650
651         return 0;
652 }
653
654 int
655 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
656 {
657         GError *error = NULL;
658         char *s;
659
660         s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
661         if (strcmp (s, "ABC"))
662                 return 1;
663
664         s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
665         if (strcmp (s, "DEF"))
666                 return 2;
667
668         if (strcmp (array2 [0], "ABC"))
669                 return 3;
670
671         if (strcmp (array2 [1], "DEF"))
672                 return 4;
673
674         return 0;
675 }
676
677 /* this does not work on Redhat gcc 2.96 */
678 int 
679 mono_test_empty_struct (int a, EmptyStruct es, int b)
680 {
681         printf ("mono_test_empty_struct %d %d\n", a, b);
682
683         if (a == 1 && b == 2)
684                 return 0;
685         return 1;
686 }
687
688 typedef struct {
689        char a[100];
690 } ByValStrStruct;
691
692 ByValStrStruct *
693 mono_test_byvalstr_gen (void)
694 {
695         ByValStrStruct *ret;
696        
697         ret = malloc(sizeof(ByValStrStruct));
698         memset(ret, 'a', sizeof(ByValStrStruct)-1);
699         ret->a[sizeof(ByValStrStruct)-1] = 0;
700
701         return ret;
702 }
703
704 int
705 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
706 {
707         int ret;
708
709         ret = strcmp(data->a, correctString);
710         printf ("T1: %s\n", data->a);
711         printf ("T2: %s\n", correctString);
712
713         g_free(data);
714         return (ret != 0);
715 }
716
717 int 
718 HexDump(char *data)
719 {
720         int i, res = 0;
721         char *p;
722
723         printf ("HEXDUMP DEFAULT VERSION\n");
724
725         p = data;
726         for (i=0; i < 8; ++i)
727         {
728                 res += *p;
729                 printf("%0x ", (int) *(p++));
730         }
731         putchar('\n');
732
733         return res;
734 }
735
736 int 
737 HexDumpA(char *data)
738 {
739         int i, res = 0;
740         char *p;
741
742         printf ("HEXDUMP ANSI VERSION\n");
743
744         p = data;
745         for (i=0; i < 8; ++i)
746         {
747                 res += *p;
748                 printf("%0x ", (int) *(p++));
749         }
750         putchar('\n');
751
752         return res + 100000;
753 }
754
755 int 
756 HexDump1W(char *data)
757 {
758         int i, res = 0;
759         char *p;
760
761         printf ("HEXDUMP UNICODE VERSION\n");
762
763         p = data;
764         for (i=0; i < 8; ++i)
765         {
766                 res += *p;
767                 printf("%0x ", (int) *(p++));
768         }
769         putchar('\n');
770
771         return res + 1000000;
772 }
773
774 typedef int (*intcharFunc)(const char*);
775
776 void 
777 callFunction (intcharFunc f)
778 {
779         f ("ABC");
780 }
781
782 int
783 printInt (int* number)
784 {
785         printf( "<%d>\n", *number );
786         return *number + 1;
787 }
788
789
790 typedef struct {
791         const char* str;
792         int i;
793 } SimpleObj;
794
795 int
796 class_marshal_test0 (SimpleObj *obj1)
797 {
798         printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
799
800         if (strcmp(obj1->str, "T1"))
801                 return -1;
802         if (obj1->i != 4)
803                 return -2;
804
805         return 0;
806 }
807
808 int
809 class_marshal_test4 (SimpleObj *obj1)
810 {
811         if (obj1)
812                 return -1;
813
814         return 0;
815 }
816
817 void
818 class_marshal_test1 (SimpleObj **obj1)
819 {
820         SimpleObj *res = malloc (sizeof (SimpleObj));
821
822         res->str = "ABC";
823         res->i = 5;
824
825         *obj1 = res;
826 }
827
828 int
829 class_marshal_test2 (SimpleObj **obj1)
830 {
831         printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
832
833         if (strcmp((*obj1)->str, "ABC"))
834                 return -1;
835         if ((*obj1)->i != 5)
836                 return -2;
837
838         return 0;
839 }
840
841 int
842 string_marshal_test0 (char *str)
843 {
844         if (strcmp (str, "TEST0"))
845                 return -1;
846
847         return 0;
848 }
849
850 void
851 string_marshal_test1 (const char **str)
852 {
853         *str = "TEST1";
854 }
855
856 int
857 string_marshal_test2 (char **str)
858 {
859         printf ("string_marshal_test2 %s\n", *str);
860
861         if (strcmp (*str, "TEST1"))
862                 return -1;
863
864         return 0;
865 }
866
867 int
868 string_marshal_test3 (char *str)
869 {
870         if (str)
871                 return -1;
872
873         return 0;
874 }
875
876 const char *
877 functionReturningString (void)
878 {
879     return "ABC";
880 }
881
882 typedef struct {
883         int a;
884         int b;
885 } VectorList;
886
887
888 VectorList* TestVectorList (VectorList *vl)
889 {
890         printf ("TestVectorList %d %d\n", vl->a, vl->b);
891
892         vl->a++;
893         vl->b++;
894
895         return vl;
896 }
897
898
899 typedef struct _OSVERSIONINFO
900
901         int a; 
902         int b; 
903 } OSVERSIONINFO; 
904
905 int 
906 GetVersionEx (OSVERSIONINFO *osvi)
907 {
908
909         printf ("GOT %d %d\n", osvi->a, osvi->b);
910
911         osvi->a += 1;
912         osvi->b += 1;
913
914         return osvi->a + osvi->b;
915 }
916
917 int 
918 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO *osvi)
919 {
920
921         printf ("GOT %d %d\n", osvi->a, osvi->b);
922
923         osvi->a += 1;
924         osvi->b += 1;
925
926         return osvi->a + osvi->b;
927 }
928
929 typedef struct {
930         double x;
931         double y;
932 } point;
933
934 int
935 mono_test_marshal_point (point pt)
936 {
937         printf("point %g %g\n", pt.x, pt.y);
938         if (pt.x == 1.25 && pt.y == 3.5)
939                 return 0;
940
941         return 1;
942 }
943
944 typedef struct {
945         int x;
946         double y;
947 } mixed_point;
948
949 int
950 mono_test_marshal_mixed_point (mixed_point pt)
951 {
952         printf("mixed point %d %g\n", pt.x, pt.y);
953         if (pt.x == 5 && pt.y == 6.75)
954                 return 0;
955
956         return 1;
957 }
958
959 int
960 time_t_sizeof (void)
961 {
962         return sizeof (time_t);
963 }
964
965 time_t
966 mono_test_marshal_time_t (time_t *t)
967 {
968         /* Skip forward an hour */
969         /* t can be unaligned on 64bit machines at present owing to the magic 4 bytes currently added
970            for custom marshaling which may or may not be correct... cope for the moment */
971         time_t s;
972         memcpy(&s, t, sizeof *t);
973         return s + 3600;
974 }
975
976 int 
977 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
978 {
979     int res = 1;
980     if (*b1 != 0 && *b1 != 1)
981         return 1;
982     if (*b2 != 0 && *b2 != -1) /* variant_bool */
983         return 1;
984     if (*b3 != 0 && *b3 != 1)
985         return 1;
986     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
987         res = 0;
988     *b1 = !*b1;
989     *b2 = ~*b2;
990     *b3 = !*b3;
991     return res;
992 }
993
994 struct BoolStruct
995 {
996     int i;
997     char b1;
998     short b2; /* variant_bool */
999     int b3;
1000 };
1001
1002 int 
1003 marshal_test_bool_struct(struct BoolStruct *s)
1004 {
1005     int res = 1;
1006     if (s->b1 != 0 && s->b1 != 1)
1007         return 1;
1008     if (s->b2 != 0 && s->b2 != -1)
1009         return 1;
1010     if (s->b3 != 0 && s->b3 != 1)
1011         return 1;
1012     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1013         res = 0;
1014     s->b1 = !s->b1;
1015     s->b2 = ~s->b2;
1016     s->b3 = !s->b3;
1017     return res;
1018 }
1019
1020 #ifdef WIN32
1021 extern __declspec(dllimport) __stdcall void SetLastError(int x);
1022 #endif
1023
1024 void
1025 mono_test_last_error (int err)
1026 {
1027 #ifdef WIN32
1028         SetLastError (err);
1029 #else
1030         errno = err;
1031 #endif
1032 }
1033
1034 int
1035 mono_test_asany (void *ptr, int what)
1036 {
1037         switch (what) {
1038         case 1:
1039                 return (*(int*)ptr == 5) ? 0 : 1;
1040         case 2:
1041                 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1042         case 3: {
1043                 simplestruct2 ss = *(simplestruct2*)ptr;
1044
1045                 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1046             !strcmp (ss.d, "TEST") && 
1047             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1048                         return 0;
1049                 else
1050                         return 1;
1051         }
1052         case 4: {
1053                 GError *error = NULL;
1054                 char *s;
1055
1056                 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1057                 if (!strcmp (s, "ABC"))
1058                         return 0;
1059                 else
1060                         return 1;
1061         }
1062         default:
1063                 g_assert_not_reached ();
1064         }
1065
1066         return 1;
1067 }
1068
1069
1070