2004-02-02 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
7 unsigned short*
8 test_lpwstr_marshal (unsigned short* chars, long length)
9 {
10         int i = 0;
11         unsigned short *res;
12
13         res = malloc (2 * (length + 1));
14
15         printf("test_lpwstr_marshal()\n");
16         
17         while ( i < length ) {
18                 printf("X|%u|\n", chars[i]);
19                 res [i] = chars[i];
20                 i++;
21         }
22
23         res [i] = 0;
24
25         return res;
26 }
27
28 typedef struct {
29         int b;
30         int a;
31         int c;
32 } union_test_1_type;
33
34 int mono_union_test_1 (union_test_1_type u1) {
35         printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
36         return u1.a + u1.b + u1.c;
37 }
38
39 int mono_return_int (int a) {
40         printf ("Got value %d\n", a);
41         return a;
42 }
43
44 struct ss
45 {
46         int i;
47 };
48
49 int mono_return_int_ss (struct ss a) {
50         printf ("Got value %d\n", a.i);
51         return a.i;
52 }
53
54 struct ss mono_return_ss (struct ss a) {
55         printf ("Got value %d\n", a.i);
56         a.i++;
57         return a;
58 }
59
60 struct sc1
61 {
62         char c[1];
63 };
64
65 struct sc1 mono_return_sc1 (struct sc1 a) {
66         printf ("Got value %d\n", a.c[0]);
67         a.c[0]++;
68         return a;
69 }
70
71
72 struct sc3
73 {
74         char c[3];
75 };
76
77 struct sc3 mono_return_sc3 (struct sc3 a) {
78         printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
79         a.c[0]++;
80         a.c[1] += 2;
81         a.c[2] += 3;
82         return a;
83 }
84
85 struct sc5
86 {
87         char c[5];
88 };
89
90 struct sc5 mono_return_sc5 (struct sc5 a) {
91         printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
92         a.c[0]++;
93         a.c[1] += 2;
94         a.c[2] += 3;
95         a.c[3] += 4;
96         a.c[4] += 5;
97         return a;
98 }
99
100 union su
101 {
102         int i1;
103         int i2;
104 };
105
106 int mono_return_int_su (union su a) {
107         printf ("Got value %d\n", a.i1);
108         return a.i1;
109 }
110
111 int mono_test_many_int_arguments (int a, int b, int c, int d, int e,
112                                   int f, int g, int h, int i, int j);
113 short mono_test_many_short_arguments (short a, short b, short c, short d, short e,
114                                       short f, short g, short h, short i, short j);
115 char mono_test_many_char_arguments (char a, char b, char c, char d, char e,
116                                     char f, char g, char h, char i, char j);
117
118 int
119 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)
120 {
121         return a + b + c + d + e + f + g + h + i + j;
122 }
123
124 short
125 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)
126 {
127         return a + b + c + d + e + f + g + h + i + j;
128 }
129
130 char
131 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)
132 {
133         return a + b + c + d + e + f + g + h + i + j;
134 }
135
136 float
137 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)
138 {
139         return a + b + c + d + e + f + g + h + i + j;
140 }
141
142 double
143 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)
144 {
145         return a + b + c + d + e + f + g + h + i + j;
146 }
147
148 double
149 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
150 {
151         return a + b + c + d + e;
152 }
153
154 int
155 mono_test_puts_static (char *s)
156 {
157         printf ("TEST %s\n", s);
158         return 1;
159 }
160
161 typedef int (*SimpleDelegate3) (int a, int b);
162
163 int
164 mono_invoke_delegate (SimpleDelegate3 delegate)
165 {
166         int res;
167
168         printf ("start invoke %p\n", delegate);
169
170         res = delegate (2, 3);
171
172         printf ("end invoke\n");
173
174         return res;
175 }
176
177 int 
178 mono_test_marshal_char (short a1)
179 {
180         if (a1 == 'a')
181                 return 0;
182         
183         return 1;
184 }
185
186 int
187 mono_test_empty_pinvoke (int i)
188 {
189         return i;
190 }
191
192 int 
193 mono_test_marshal_bool_byref (int a, int *b, int c)
194 {
195     int res = *b;
196
197         *b = 1;
198
199         return res;
200 }
201
202 int 
203 mono_test_marshal_array (int *a1)
204 {
205         int i, sum = 0;
206
207         for (i = 0; i < 50; i++)
208                 sum += a1 [i];
209         
210         return sum;
211 }
212
213 int 
214 mono_test_marshal_inout_array (int *a1)
215 {
216         int i, sum = 0;
217
218         for (i = 0; i < 50; i++) {
219                 sum += a1 [i];
220                 a1 [i] = 50 - a1 [i];
221         }
222         
223         return sum;
224 }
225
226 int 
227 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
228 {
229         int i, sum = 0;
230
231         for (i = 0; i < 10; i++) {
232                 a1 [i] = 'F';
233         }
234         
235         return sum;
236 }
237
238 typedef struct {
239         int a;
240         int b;
241         int c;
242         const char *d;
243 } simplestruct;
244
245 simplestruct
246 mono_test_return_vtype (int i)
247 {
248         simplestruct res;
249
250         res.a = 0;
251         res.b = 1;
252         res.c = 0;
253         res.d = "TEST";
254
255         return res;
256 }
257
258 void
259 mono_test_delegate_struct (void)
260 {
261         printf ("TEST\n");
262 }
263
264 typedef char* (*ReturnStringDelegate) (const char *s);
265
266 char *
267 mono_test_return_string (ReturnStringDelegate func)
268 {
269         char *res;
270
271         printf ("mono_test_return_string\n");
272
273         res = func ("TEST");
274
275         printf ("got string: %s\n", res);
276         return res;
277 }
278
279 typedef int (*RefVTypeDelegate) (int a, simplestruct *ss, int b);
280
281 int
282 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
283 {
284         if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
285             !strcmp (ss->d, "TEST1")) {
286                 ss->a = 1;
287                 ss->b = 0;
288                 ss->c = 1;
289                 ss->d = "TEST2";
290         
291                 return func (a, ss, b);
292         }
293
294         return 1;
295 }
296
297 typedef struct {
298         int a;
299         int (*func) (int);
300 } DelegateStruct;
301
302 int 
303 mono_test_marshal_delegate_struct (DelegateStruct ds)
304 {
305         return ds.func (ds.a);
306 }
307
308 int 
309 mono_test_marshal_struct (simplestruct ss)
310 {
311         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
312             !strcmp (ss.d, "TEST"))
313                 return 0;
314
315         return 1;
316 }
317
318 typedef struct {
319         int a;
320         int b;
321         int c;
322         char *d;
323         unsigned char e;
324         double f;
325         unsigned char g;
326         guint64 h;
327 } simplestruct2;
328
329 int
330 mono_test_marshal_struct2 (simplestruct2 ss)
331 {
332         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
333             !strcmp (ss.d, "TEST") && 
334             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
335                 return 0;
336
337         return 1;
338 }
339
340 /* on HP some of the struct should be on the stack and not in registers */
341 int
342 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
343 {
344         if (i != 10 || j != 11 || k != 12)
345                 return 1;
346         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
347             !strcmp (ss.d, "TEST") && 
348             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
349                 return 0;
350
351         return 1;
352 }
353
354 int
355 mono_test_marshal_struct_array (simplestruct2 *ss)
356 {
357         if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
358                    !strcmp (ss[0].d, "TEST") && 
359                    ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
360                 return 1;
361
362         if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
363                    !strcmp (ss[1].d, "TEST2") && 
364                    ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
365                 return 1;
366
367         return 0;
368 }
369
370 simplestruct2 *
371 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
372 {
373         simplestruct2 *res;
374
375         if (!ss)
376                 return NULL;
377
378         if (i != 10 || j != 11 || k != 12 || l != 14)
379                 return NULL;
380         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
381                    !strcmp (ss->d, "TEST") && 
382                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
383                 return NULL;
384
385         res = g_new0 (simplestruct2, 1);
386         memcpy (res, ss, sizeof (simplestruct2));
387         return res;
388 }
389
390 int
391 mono_test_marshal_byref_class (simplestruct2 **ssp)
392 {
393         simplestruct2 *ss = *ssp;
394         simplestruct2 *res;
395         
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 1;
400
401         res = g_new0 (simplestruct2, 1);
402         memcpy (res, ss, sizeof (simplestruct2));
403         res->d = (char*)"TEST-RES";
404
405         *ssp = res;
406         return 0;
407 }
408
409 #ifdef WIN32
410 typedef int (__stdcall *SimpleDelegate) (int a);
411 #else
412 typedef int (*SimpleDelegate) (int a);
413 #endif
414
415 static void *
416 get_sp (void)
417 {
418         int i;
419         void *p;
420
421         p = &i;
422         return p;
423 }
424
425 int
426 mono_test_marshal_delegate (SimpleDelegate delegate)
427 {
428         void *sp1, *sp2;
429
430         /* Check that the delegate wrapper is stdcall */
431         delegate (2);
432         sp1 = get_sp ();
433         delegate (2);
434         sp2 = get_sp ();
435         g_assert (sp1 == sp2);
436
437         return delegate (2);
438 }
439
440 typedef simplestruct (*SimpleDelegate2) (simplestruct ss);
441
442 int
443 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
444 {
445         simplestruct ss, res;
446
447         ss.a = 0;
448         ss.b = 1;
449         ss.c = 0;
450         ss.d = "TEST";
451
452         res = delegate (ss);
453         if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES")))
454                 return 1;
455
456         return 0;
457 }
458
459 typedef simplestruct* (*SimpleDelegate4) (simplestruct *ss);
460
461 int
462 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
463 {
464         simplestruct ss;
465         simplestruct *res;
466
467         ss.a = 0;
468         ss.b = 1;
469         ss.c = 0;
470         ss.d = "TEST";
471
472         /* Check argument */
473         res = delegate (&ss);
474         if (!res)
475                 return 1;
476
477         /* Check return value */
478         if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
479                 return 2;
480
481         /* Check NULL argument and NULL result */
482         res = delegate (NULL);
483         if (res)
484                 return 3;
485
486         return 0;
487 }
488
489 typedef int (*SimpleDelegate5) (simplestruct **ss);
490
491 int
492 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
493 {
494         simplestruct ss;
495         int res;
496         simplestruct *ptr;
497
498         ss.a = 0;
499         ss.b = 1;
500         ss.c = 0;
501         ss.d = "TEST";
502
503         ptr = &ss;
504
505         res = delegate (&ptr);
506         if (res != 0)
507                 return 1;
508
509         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
510                 return 2;
511
512         return 0;
513 }
514
515 int
516 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
517 {
518         int res;
519
520         res = delegate (NULL);
521
522         return 0;
523 }
524
525 typedef int (*SimpleDelegate7) (simplestruct **ss);
526
527 int
528 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
529 {
530         int res;
531         simplestruct *ptr;
532
533         /* Check that the input pointer is ignored */
534         ptr = (gpointer)0x12345678;
535
536         res = delegate (&ptr);
537         if (res != 0)
538                 return 1;
539
540         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
541                 return 2;
542
543         return 0;
544 }
545
546 int 
547 mono_test_marshal_stringbuilder (char *s, int n)
548 {
549         const char m[] = "This is my message.  Isn't it nice?";
550         strncpy(s, m, n);
551         return 0;
552 }
553
554 typedef struct {
555 #ifndef __GNUC__
556     char a;
557 #endif
558 } EmptyStruct;
559
560 int
561 mono_test_marshal_string_array (char **array)
562 {
563         printf ("%p\n", array);
564         return 0;
565 }
566
567 /* this does not work on Redhat gcc 2.96 */
568 int 
569 mono_test_empty_struct (int a, EmptyStruct es, int b)
570 {
571         printf ("mono_test_empty_struct %d %d\n", a, b);
572
573         if (a == 1 && b == 2)
574                 return 0;
575         return 1;
576 }
577
578 typedef struct {
579        char a[100];
580 } ByValStrStruct;
581
582 ByValStrStruct *
583 mono_test_byvalstr_gen (void)
584 {
585         ByValStrStruct *ret;
586        
587         ret = malloc(sizeof(ByValStrStruct));
588         memset(ret, 'a', sizeof(ByValStrStruct)-1);
589         ret->a[sizeof(ByValStrStruct)-1] = 0;
590
591         return ret;
592 }
593
594 int
595 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
596 {
597         int ret;
598
599         ret = strcmp(data->a, correctString);
600         printf ("T1: %s\n", data->a);
601         printf ("T2: %s\n", correctString);
602
603         g_free(data);
604         return (ret != 0);
605 }
606
607 int 
608 HexDump(char *data)
609 {
610         int i, res = 0;
611         char *p;
612
613         printf ("HEXDUMP DEFAULT VERSION\n");
614
615         p = data;
616         for (i=0; i < 8; ++i)
617         {
618                 res += *p;
619                 printf("%0x ", (int) *(p++));
620         }
621         putchar('\n');
622
623         return res;
624 }
625
626 int 
627 HexDumpA(char *data)
628 {
629         int i, res = 0;
630         char *p;
631
632         printf ("HEXDUMP ANSI VERSION\n");
633
634         p = data;
635         for (i=0; i < 8; ++i)
636         {
637                 res += *p;
638                 printf("%0x ", (int) *(p++));
639         }
640         putchar('\n');
641
642         return res + 100000;
643 }
644
645 int 
646 HexDump1W(char *data)
647 {
648         int i, res = 0;
649         char *p;
650
651         printf ("HEXDUMP UNICODE VERSION\n");
652
653         p = data;
654         for (i=0; i < 8; ++i)
655         {
656                 res += *p;
657                 printf("%0x ", (int) *(p++));
658         }
659         putchar('\n');
660
661         return res + 1000000;
662 }
663
664 typedef int (*intcharFunc)(const char*);
665
666 void 
667 callFunction (intcharFunc f)
668 {
669         f ("ABC");
670 }
671
672 int
673 printInt (int* number)
674 {
675         printf( "<%d>\n", *number );
676         return *number + 1;
677 }
678
679
680 typedef struct {
681         const char* str;
682         int i;
683 } SimpleObj;
684
685 int
686 class_marshal_test0 (SimpleObj *obj1)
687 {
688         printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
689
690         if (strcmp(obj1->str, "T1"))
691                 return -1;
692         if (obj1->i != 4)
693                 return -2;
694
695         return 0;
696 }
697
698 int
699 class_marshal_test4 (SimpleObj *obj1)
700 {
701         if (obj1)
702                 return -1;
703
704         return 0;
705 }
706
707 void
708 class_marshal_test1 (SimpleObj **obj1)
709 {
710         SimpleObj *res = malloc (sizeof (SimpleObj));
711
712         res->str = "ABC";
713         res->i = 5;
714
715         *obj1 = res;
716 }
717
718 int
719 class_marshal_test2 (SimpleObj **obj1)
720 {
721         printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
722
723         if (strcmp((*obj1)->str, "ABC"))
724                 return -1;
725         if ((*obj1)->i != 5)
726                 return -2;
727
728         return 0;
729 }
730
731 int
732 string_marshal_test0 (char *str)
733 {
734         if (strcmp (str, "TEST0"))
735                 return -1;
736
737         return 0;
738 }
739
740 void
741 string_marshal_test1 (const char **str)
742 {
743         *str = "TEST1";
744 }
745
746 int
747 string_marshal_test2 (char **str)
748 {
749         printf ("string_marshal_test2 %s\n", *str);
750
751         if (strcmp (*str, "TEST1"))
752                 return -1;
753
754         return 0;
755 }
756
757 int
758 string_marshal_test3 (char *str)
759 {
760         if (str)
761                 return -1;
762
763         return 0;
764 }
765
766 const char *
767 functionReturningString (void)
768 {
769     return "ABC";
770 }
771
772 typedef struct {
773         int a;
774         int b;
775 } VectorList;
776
777
778 VectorList* TestVectorList (VectorList *vl)
779 {
780         printf ("TestVectorList %d %d\n", vl->a, vl->b);
781
782         vl->a++;
783         vl->b++;
784
785         return vl;
786 }
787
788
789 typedef struct _OSVERSIONINFO
790
791         int a; 
792         int b; 
793 } OSVERSIONINFO; 
794
795 int 
796 GetVersionEx (OSVERSIONINFO *osvi)
797 {
798
799         printf ("GOT %d %d\n", osvi->a, osvi->b);
800
801         osvi->a += 1;
802         osvi->b += 1;
803
804         return osvi->a + osvi->b;
805 }
806
807 int 
808 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO *osvi)
809 {
810
811         printf ("GOT %d %d\n", osvi->a, osvi->b);
812
813         osvi->a += 1;
814         osvi->b += 1;
815
816         return osvi->a + osvi->b;
817 }
818
819 typedef struct {
820         double x;
821         double y;
822 } point;
823
824 int
825 mono_test_marshal_point (point pt)
826 {
827         printf("point %g %g\n", pt.x, pt.y);
828         if (pt.x == 1.25 && pt.y == 3.5)
829                 return 0;
830
831         return 1;
832 }
833
834 typedef struct {
835         int x;
836         double y;
837 } mixed_point;
838
839 int
840 mono_test_marshal_mixed_point (mixed_point pt)
841 {
842         printf("mixed point %d %g\n", pt.x, pt.y);
843         if (pt.x == 5 && pt.y == 6.75)
844                 return 0;
845
846         return 1;
847 }
848
849 int 
850 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
851 {
852     int res = 1;
853     if (*b1 != 0 && *b1 != 1)
854         return 1;
855     if (*b2 != 0 && *b2 != -1) /* variant_bool */
856         return 1;
857     if (*b3 != 0 && *b3 != 1)
858         return 1;
859     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
860         res = 0;
861     *b1 = !*b1;
862     *b2 = ~*b2;
863     *b3 = !*b3;
864     return res;
865 }
866
867 struct BoolStruct
868 {
869     int i;
870     char b1;
871     short b2; /* variant_bool */
872     int b3;
873 };
874
875 int 
876 marshal_test_bool_struct(struct BoolStruct *s)
877 {
878     int res = 1;
879     if (s->b1 != 0 && s->b1 != 1)
880         return 1;
881     if (s->b2 != 0 && s->b2 != -1)
882         return 1;
883     if (s->b3 != 0 && s->b3 != 1)
884         return 1;
885     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
886         res = 0;
887     s->b1 = !s->b1;
888     s->b2 = ~s->b2;
889     s->b3 = !s->b3;
890     return res;
891 }
892
893 void
894 mono_test_last_error (int err)
895 {
896 #ifdef WIN32
897         SetLastError (err);
898 #else
899         errno = err;
900 #endif
901 }
902
903
904