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