2003-10-09 Bernie Solomon <bernard@ugsolutions.com>
[mono.git] / mono / tests / libtest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <glib.h>
5
6 unsigned short*
7 test_lpwstr_marshal (unsigned short* chars, long length)
8 {
9         int i = 0;
10         unsigned short *res;
11
12         res = malloc (2 * (length + 1));
13
14         printf("test_lpwstr_marshal()\n");
15         
16         while ( i < length ) {
17                 printf("X|%u|\n", chars[i]);
18                 res [i] = chars[i];
19                 i++;
20         }
21
22         res [i] = 0;
23
24         return res;
25 }
26
27 typedef struct {
28         int b;
29         int a;
30         int c;
31 } union_test_1_type;
32
33 int mono_union_test_1 (union_test_1_type u1) {
34         printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
35         return u1.a + u1.b + u1.c;
36 }
37
38 int mono_return_int (int a) {
39         printf ("Got value %d\n", a);
40         return a;
41 }
42
43 struct ss
44 {
45         int i;
46 };
47
48 int mono_return_int_ss (struct ss a) {
49         printf ("Got value %d\n", a.i);
50         return a.i;
51 }
52
53 struct ss mono_return_ss (struct ss a) {
54         printf ("Got value %d\n", a.i);
55         a.i++;
56         return a;
57 }
58
59 struct sc1
60 {
61         char c[1];
62 };
63
64 struct sc1 mono_return_sc1 (struct sc1 a) {
65         printf ("Got value %d\n", a.c[0]);
66         a.c[0]++;
67         return a;
68 }
69
70
71 struct sc3
72 {
73         char c[3];
74 };
75
76 struct sc3 mono_return_sc3 (struct sc3 a) {
77         printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
78         a.c[0]++;
79         a.c[1] += 2;
80         a.c[2] += 3;
81         return a;
82 }
83
84 struct sc5
85 {
86         char c[5];
87 };
88
89 struct sc5 mono_return_sc5 (struct sc5 a) {
90         printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
91         a.c[0]++;
92         a.c[1] += 2;
93         a.c[2] += 3;
94         a.c[3] += 4;
95         a.c[4] += 5;
96         return a;
97 }
98
99 union su
100 {
101         int i1;
102         int i2;
103 };
104
105 int mono_return_int_su (union su a) {
106         printf ("Got value %d\n", a.i1);
107         return a.i1;
108 }
109
110 int mono_test_many_int_arguments (int a, int b, int c, int d, int e,
111                                   int f, int g, int h, int i, int j);
112 short mono_test_many_short_arguments (short a, short b, short c, short d, short e,
113                                       short f, short g, short h, short i, short j);
114 char mono_test_many_char_arguments (char a, char b, char c, char d, char e,
115                                     char f, char g, char h, char i, char j);
116
117 int
118 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)
119 {
120         return a + b + c + d + e + f + g + h + i + j;
121 }
122
123 short
124 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)
125 {
126         return a + b + c + d + e + f + g + h + i + j;
127 }
128
129 char
130 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)
131 {
132         return a + b + c + d + e + f + g + h + i + j;
133 }
134
135 float
136 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)
137 {
138         return a + b + c + d + e + f + g + h + i + j;
139 }
140
141 double
142 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)
143 {
144         return a + b + c + d + e + f + g + h + i + j;
145 }
146
147 int
148 mono_test_puts_static (char *s)
149 {
150         printf ("TEST %s\n", s);
151         return 1;
152 }
153
154 typedef int (*SimpleDelegate3) (int a, int b);
155
156 int
157 mono_invoke_delegate (SimpleDelegate3 delegate)
158 {
159         int res;
160
161         printf ("start invoke %p\n", delegate);
162
163         res = delegate (2, 3);
164
165         printf ("end invoke\n");
166
167         return res;
168 }
169
170 int 
171 mono_test_marshal_char (short a1)
172 {
173         if (a1 == 'a')
174                 return 0;
175         
176         return 1;
177 }
178
179 int 
180 mono_test_marshal_array (int *a1)
181 {
182         int i, sum = 0;
183
184         for (i = 0; i < 50; i++)
185                 sum += a1 [i];
186         
187         return sum;
188 }
189
190 typedef struct {
191         int a;
192         int b;
193         int c;
194         const char *d;
195 } simplestruct;
196
197 simplestruct
198 mono_test_return_vtype (void)
199 {
200         simplestruct res;
201
202         res.a = 0;
203         res.b = 1;
204         res.c = 0;
205         res.d = "TEST";
206         printf ("mono_test_return_vtype\n");
207         return res;
208 }
209
210 void
211 mono_test_delegate_struct (void)
212 {
213         printf ("TEST\n");
214 }
215
216 typedef simplestruct (*ReturnVTypeDelegate) (simplestruct ss);
217
218 simplestruct
219 mono_test_return_vtype2 (ReturnVTypeDelegate func)
220 {
221         simplestruct res;
222         simplestruct res1;
223
224         res.a = 1;
225         res.b = 0;
226         res.c = 1;
227         res.d = "TEST";
228         printf ("mono_test_return_vtype2\n");
229
230         res1 = func (res);
231
232         printf ("UA: %d\n", res1.a);
233         printf ("UB: %d\n", res1.b);
234         printf ("UC: %d\n", res1.c);
235         printf ("UD: %s\n", res1.d);
236
237         return res1;
238 }
239
240 typedef char* (*ReturnStringDelegate) (const char *s);
241
242 char *
243 mono_test_return_string (ReturnStringDelegate func)
244 {
245         char *res;
246
247         printf ("mono_test_return_string\n");
248
249         res = func ("TEST");
250
251         printf ("got string: %s\n", res);
252         return res;
253 }
254
255 typedef int (*RefVTypeDelegate) (int a, simplestruct *ss, int b);
256
257 int
258 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
259 {
260         if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
261             !strcmp (ss->d, "TEST1")) {
262                 ss->a = 1;
263                 ss->b = 0;
264                 ss->c = 1;
265                 ss->d = "TEST2";
266         
267                 return func (a, ss, b);
268         }
269
270         return 1;
271 }
272
273 typedef struct {
274         int a;
275         int (*func) (int);
276 } DelegateStruct;
277
278 int 
279 mono_test_marshal_delegate_struct (DelegateStruct ds)
280 {
281         return ds.func (ds.a);
282 }
283
284 int 
285 mono_test_marshal_struct (simplestruct ss)
286 {
287         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
288             !strcmp (ss.d, "TEST"))
289                 return 0;
290
291         return 1;
292 }
293
294 typedef struct {
295         int a;
296         int b;
297         int c;
298         char *d;
299         unsigned char e;
300         double f;
301         unsigned char g;
302         guint64 h;
303 } simplestruct2;
304
305 int
306 mono_test_marshal_struct2 (simplestruct2 ss)
307 {
308         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
309             !strcmp (ss.d, "TEST") && 
310             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
311                 return 0;
312
313         return 1;
314 }
315
316 /* on HP some of the struct should be on the stack and not in registers */
317 int
318 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
319 {
320         if (i != 10 || j != 11 || k != 12)
321                 return 1;
322         if (ss.a == 0 && ss.b == 0 && ss.c == 0 &&
323             !strcmp (ss.d, "TEST2") && 
324             ss.e == 100 && ss.f == 2.5 && ss.g == 43 && ss.h == (guint64)124)
325                 return 0;
326
327         return 1;
328 }
329
330 int
331 mono_test_marshal_struct_array (simplestruct2 *ss)
332 {
333         if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
334                    !strcmp (ss[0].d, "TEST") && 
335                    ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
336                 return 1;
337
338         if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
339                    !strcmp (ss[1].d, "TEST2") && 
340                    ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
341                 return 1;
342
343         return 0;
344 }
345
346 typedef int (*SimpleDelegate) (int a);
347
348 int
349 mono_test_marshal_delegate (SimpleDelegate delegate)
350 {
351         return delegate (2);
352 }
353
354 typedef int (*SimpleDelegate2) (simplestruct ss);
355
356 int
357 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
358 {
359         simplestruct ss;
360         int res;
361
362         ss.a = 0;
363         ss.b = 1;
364         ss.c = 0;
365         ss.d = "TEST";
366
367         printf ("Calling delegate from unmanaged code\n");
368         res = delegate (ss);
369         printf ("GOT %d\n", res);
370
371         return res;
372 }
373
374 int 
375 mono_test_marshal_stringbuilder (char *s, int n)
376 {
377         const char m[] = "This is my message.  Isn't it nice?";
378         strncpy(s, m, n);
379         return 0;
380 }
381
382 #ifdef __GNUC__
383 typedef struct {
384 } EmptyStruct;
385 #endif
386
387 int
388 mono_test_marshal_string_array (char **array)
389 {
390         printf ("%p\n", array);
391         return 0;
392 }
393
394 #ifdef __GNUC__
395 /* this does not work on Redhat gcc 2.96 */
396 int 
397 mono_test_empty_struct (int a, EmptyStruct es, int b)
398 {
399         printf ("mono_test_empty_struct %d %d\n", a, b);
400
401         if (a == 1 && b == 2)
402                 return 0;
403         return 1;
404 }
405 #endif
406
407 typedef struct {
408        char a[100];
409 } ByValStrStruct;
410
411 ByValStrStruct *
412 mono_test_byvalstr_gen (void)
413 {
414         ByValStrStruct *ret;
415        
416         ret = malloc(sizeof(ByValStrStruct));
417         memset(ret, 'a', sizeof(ByValStrStruct)-1);
418         ret->a[sizeof(ByValStrStruct)-1] = 0;
419
420         return ret;
421 }
422
423 int
424 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
425 {
426         int ret;
427
428         ret = strcmp(data->a, correctString);
429         printf ("T1: %s\n", data->a);
430         printf ("T2: %s\n", correctString);
431
432         g_free(data);
433         return (ret != 0);
434 }
435
436 int 
437 HexDump(char *data)
438 {
439         int i, res = 0;
440         char *p;
441
442         printf ("HEXDUMP DEFAULT VERSION\n");
443
444         p = data;
445         for (i=0; i < 8; ++i)
446         {
447                 res += *p;
448                 printf("%0x ", (int) *(p++));
449         }
450         putchar('\n');
451
452         return res;
453 }
454
455 int 
456 HexDumpA(char *data)
457 {
458         int i, res = 0;
459         char *p;
460
461         printf ("HEXDUMP ANSI VERSION\n");
462
463         p = data;
464         for (i=0; i < 8; ++i)
465         {
466                 res += *p;
467                 printf("%0x ", (int) *(p++));
468         }
469         putchar('\n');
470
471         return res + 100000;
472 }
473
474 int 
475 HexDump1W(char *data)
476 {
477         int i, res = 0;
478         char *p;
479
480         printf ("HEXDUMP UNICODE VERSION\n");
481
482         p = data;
483         for (i=0; i < 8; ++i)
484         {
485                 res += *p;
486                 printf("%0x ", (int) *(p++));
487         }
488         putchar('\n');
489
490         return res + 1000000;
491 }
492
493 typedef int (*intcharFunc)(const char*);
494
495 void 
496 callFunction (intcharFunc f)
497 {
498         f ("ABC");
499 }
500
501 int
502 printInt (int* number)
503 {
504         printf( "<%d>\n", *number );
505         return *number + 1;
506 }
507
508
509 typedef struct {
510         const char* str;
511         int i;
512 } SimpleObj;
513
514 int
515 class_marshal_test0 (SimpleObj *obj1)
516 {
517         printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
518
519         if (strcmp(obj1->str, "T1"))
520                 return -1;
521         if (obj1->i != 4)
522                 return -2;
523
524         return 0;
525 }
526
527 int
528 class_marshal_test4 (SimpleObj *obj1)
529 {
530         if (obj1)
531                 return -1;
532
533         return 0;
534 }
535
536 void
537 class_marshal_test1 (SimpleObj **obj1)
538 {
539         SimpleObj *res = malloc (sizeof (SimpleObj));
540
541         res->str = "ABC";
542         res->i = 5;
543
544         *obj1 = res;
545 }
546
547 int
548 class_marshal_test2 (SimpleObj **obj1)
549 {
550         printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
551
552         if (strcmp((*obj1)->str, "ABC"))
553                 return -1;
554         if ((*obj1)->i != 5)
555                 return -2;
556
557         return 0;
558 }
559
560 int
561 string_marshal_test0 (char *str)
562 {
563         if (strcmp (str, "TEST0"))
564                 return -1;
565
566         return 0;
567 }
568
569 void
570 string_marshal_test1 (const char **str)
571 {
572         *str = "TEST1";
573 }
574
575 int
576 string_marshal_test2 (char **str)
577 {
578         printf ("string_marshal_test2 %s\n", *str);
579
580         if (strcmp (*str, "TEST1"))
581                 return -1;
582
583         return 0;
584 }
585
586 int
587 string_marshal_test3 (char *str)
588 {
589         if (str)
590                 return -1;
591
592         return 0;
593 }
594
595 const char *
596 functionReturningString (void)
597 {
598     return "ABC";
599 }
600
601 typedef struct {
602         int a;
603         int b;
604 } VectorList;
605
606
607 VectorList* TestVectorList (VectorList *vl)
608 {
609         printf ("TestVectorList %d %d\n", vl->a, vl->b);
610
611         vl->a++;
612         vl->b++;
613
614         return vl;
615 }
616
617
618 typedef struct _OSVERSIONINFO
619
620         int a; 
621         int b; 
622 } OSVERSIONINFO; 
623
624 int 
625 GetVersionEx (OSVERSIONINFO *osvi)
626 {
627
628         printf ("GOT %d %d\n", osvi->a, osvi->b);
629
630         osvi->a += 1;
631         osvi->b += 1;
632
633         return osvi->a + osvi->b;
634 }
635
636 typedef struct {
637         double x;
638         double y;
639 } point;
640
641 int
642 mono_test_marshal_point (point pt)
643 {
644         printf("point %g %g\n", pt.x, pt.y);
645         if (pt.x == 1.25 && pt.y == 3.5)
646                 return 0;
647
648         return 1;
649 }
650
651 typedef struct {
652         int x;
653         double y;
654 } mixed_point;
655
656 int
657 mono_test_marshal_mixed_point (mixed_point pt)
658 {
659         printf("mixed point %d %g\n", pt.x, pt.y);
660         if (pt.x == 5 && pt.y == 6.75)
661                 return 0;
662
663         return 1;
664 }
665
666 int 
667 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
668 {
669     int res = 1;
670     if (*b1 != 0 && *b1 != 1)
671         return 1;
672     if (*b2 != 0 && *b2 != -1) /* variant_bool */
673         return 1;
674     if (*b3 != 0 && *b3 != 1)
675         return 1;
676     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
677         res = 0;
678     *b1 = !*b1;
679     *b2 = ~*b2;
680     *b3 = !*b3;
681     return res;
682 }
683
684 struct BoolStruct
685 {
686     int i;
687     char b1;
688     short b2; /* variant_bool */
689     int b3;
690 };
691
692 int 
693 marshal_test_bool_struct(struct BoolStruct *s)
694 {
695     int res = 1;
696     if (s->b1 != 0 && s->b1 != 1)
697         return 1;
698     if (s->b2 != 0 && s->b2 != -1)
699         return 1;
700     if (s->b3 != 0 && s->b3 != 1)
701         return 1;
702     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
703         res = 0;
704     s->b1 = !s->b1;
705     s->b2 = ~s->b2;
706     s->b3 = !s->b3;
707     return res;
708 }
709
710