Updates and fixes, mostly from 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         }
20
21         res [i] = 0;
22
23         return res;
24 }
25
26 typedef struct {
27         int b;
28         int a;
29         int c;
30 } union_test_1_type;
31
32 int mono_union_test_1 (union_test_1_type u1) {
33         printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
34         return u1.a + u1.b + u1.c;
35 }
36
37 int mono_return_int (int a) {
38         printf ("Got value %d\n", a);
39         return a;
40 }
41
42 struct ss
43 {
44         int i;
45 };
46
47 int mono_return_int_ss (struct ss a) {
48         printf ("Got value %d\n", a.i);
49         return a.i;
50 }
51
52 union su
53 {
54         int i1;
55         int i2;
56 };
57
58 int mono_return_int_su (union su a) {
59         printf ("Got value %d\n", a.i1);
60         return a.i1;
61 }
62
63 int mono_test_many_int_arguments (int a, int b, int c, int d, int e,
64                                   int f, int g, int h, int i, int j);
65 short mono_test_many_short_arguments (short a, short b, short c, short d, short e,
66                                       short f, short g, short h, short i, short j);
67 char mono_test_many_char_arguments (char a, char b, char c, char d, char e,
68                                     char f, char g, char h, char i, char j);
69
70 int
71 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)
72 {
73         return a + b + c + d + e + f + g + h + i + j;
74 }
75
76 short
77 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)
78 {
79         return a + b + c + d + e + f + g + h + i + j;
80 }
81
82 char
83 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)
84 {
85         return a + b + c + d + e + f + g + h + i + j;
86 }
87
88 float
89 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)
90 {
91         return a + b + c + d + e + f + g + h + i + j;
92 }
93
94 double
95 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)
96 {
97         return a + b + c + d + e + f + g + h + i + j;
98 }
99
100 int
101 mono_test_puts_static (char *s)
102 {
103         printf ("TEST %s\n", s);
104         return 1;
105 }
106
107 typedef int (*SimpleDelegate3) (int a, int b);
108
109 int
110 mono_invoke_delegate (SimpleDelegate3 delegate)
111 {
112         int res;
113
114         printf ("start invoke %p\n", delegate);
115
116         res = delegate (2, 3);
117
118         printf ("end invoke\n");
119
120         return res;
121 }
122
123 int 
124 mono_test_marshal_char (short a1)
125 {
126         if (a1 = 'a')
127                 return 0;
128         
129         return 1;
130 }
131
132 int 
133 mono_test_marshal_array (int *a1)
134 {
135         int i, sum = 0;
136
137         for (i = 0; i < 50; i++)
138                 sum += a1 [i];
139         
140         return sum;
141 }
142
143 typedef struct {
144         int a;
145         int b;
146         int c;
147         char *d;
148 } simplestruct;
149
150 simplestruct
151 mono_test_return_vtype ()
152 {
153         simplestruct res;
154
155         res.a = 0;
156         res.b = 1;
157         res.c = 0;
158         res.d = "TEST";
159         printf ("mono_test_return_vtype\n");
160         return res;
161 }
162
163 void
164 mono_test_delegate_struct ()
165 {
166         printf ("TEST\n");
167 }
168
169 typedef simplestruct (*ReturnVTypeDelegate) (simplestruct ss);
170
171 simplestruct
172 mono_test_return_vtype2 (ReturnVTypeDelegate func)
173 {
174         simplestruct res;
175         simplestruct res1;
176
177         res.a = 1;
178         res.b = 0;
179         res.c = 1;
180         res.d = "TEST";
181         printf ("mono_test_return_vtype2\n");
182
183         res1 = func (res);
184
185         printf ("UA: %d\n", res1.a);
186         printf ("UB: %d\n", res1.b);
187         printf ("UC: %d\n", res1.c);
188         printf ("UD: %s\n", res1.d);
189
190         return res1;
191 }
192
193 typedef char* (*ReturnStringDelegate) (char *s);
194
195 char *
196 mono_test_return_string (ReturnStringDelegate func)
197 {
198         char *res;
199
200         printf ("mono_test_return_string\n");
201
202         res = func ("TEST");
203
204         printf ("got string: %s\n", res);
205         return res;
206 }
207
208 typedef int (*RefVTypeDelegate) (int a, simplestruct *ss, int b);
209
210 int
211 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
212 {
213         if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
214             !strcmp (ss->d, "TEST1")) {
215                 ss->a = 1;
216                 ss->b = 0;
217                 ss->c = 1;
218                 ss->d = "TEST2";
219         
220                 return func (a, ss, b);
221         }
222
223         return 1;
224 }
225
226 typedef struct {
227         int a;
228         int (*func) (int);
229 } DelegateStruct;
230
231 int 
232 mono_test_marshal_delegate_struct (DelegateStruct ds)
233 {
234         return ds.func (ds.a);
235 }
236
237 int 
238 mono_test_marshal_struct (simplestruct ss)
239 {
240         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
241             !strcmp (ss.d, "TEST"))
242                 return 0;
243
244         return 1;
245 }
246
247 typedef struct {
248         int a;
249         int b;
250         int c;
251         char *d;
252         unsigned char e;
253         double f;
254         unsigned char g;
255         guint64 h;
256 } simplestruct2;
257
258 int
259 mono_test_marshal_struct2 (simplestruct2 ss)
260 {
261         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
262             !strcmp (ss.d, "TEST") && 
263             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
264                 return 0;
265
266         return 1;
267 }
268
269
270 typedef int (*SimpleDelegate) (int a);
271
272 int
273 mono_test_marshal_delegate (SimpleDelegate delegate)
274 {
275         return delegate (2);
276 }
277
278 typedef int (*SimpleDelegate2) (simplestruct ss);
279
280 int
281 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
282 {
283         simplestruct ss;
284         int res;
285
286         ss.a = 0;
287         ss.b = 1;
288         ss.c = 0;
289         ss.d = "TEST";
290
291         printf ("Calling delegate from unmanaged code\n");
292         res = delegate (ss);
293         printf ("GOT %d\n", res);
294
295         return res;
296 }
297
298 int 
299 mono_test_marshal_stringbuilder (char *s, int n)
300 {
301         const char m[] = "This is my message.  Isn't it nice?";
302         strncpy(s, m, n);
303         return 0;
304 }
305
306 #ifdef __GNUC__
307 typedef struct {
308 } EmptyStruct;
309 #endif
310
311 int
312 mono_test_marshal_string_array (char **array)
313 {
314         printf ("%p\n", array);
315         return 0;
316 }
317
318 #ifdef __GNUC__
319 /* this does not work on Redhat gcc 2.96 */
320 int 
321 mono_test_empty_struct (int a, EmptyStruct es, int b)
322 {
323         printf ("mono_test_empty_struct %d %d\n", a, b);
324
325         if (a == 1 && b == 2)
326                 return 0;
327         return 1;
328 }
329 #endif
330
331 typedef struct {
332        char a[100];
333 } ByValStrStruct;
334
335 ByValStrStruct *
336 mono_test_byvalstr_gen (void)
337 {
338         ByValStrStruct *ret;
339         int i;
340        
341         ret = malloc(sizeof(ByValStrStruct));
342         memset(ret, 'a', sizeof(ByValStrStruct)-1);
343         ret->a[sizeof(ByValStrStruct)-1] = 0;
344
345         return ret;
346 }
347
348 int
349 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
350 {
351         int ret;
352
353         ret = strcmp(data->a, correctString);
354         printf ("T1: %s\n", data->a);
355         printf ("T2: %s\n", correctString);
356
357         g_free(data);
358         return (ret != 0);
359 }
360
361 int 
362 HexDump(char *data)
363 {
364         int i, res = 0;
365         char *p;
366
367         printf ("HEXDUMP DEFAULT VERSION\n");
368
369         p = data;
370         for (i=0; i < 8; ++i)
371         {
372                 res += *p;
373                 printf("%0x ", (int) *(p++));
374         }
375         putchar('\n');
376
377         return res;
378 }
379
380 int 
381 HexDumpA(char *data)
382 {
383         int i, res = 0;
384         char *p;
385
386         printf ("HEXDUMP ANSI VERSION\n");
387
388         p = data;
389         for (i=0; i < 8; ++i)
390         {
391                 res += *p;
392                 printf("%0x ", (int) *(p++));
393         }
394         putchar('\n');
395
396         return res + 100000;
397 }
398
399 int 
400 HexDump1W(char *data)
401 {
402         int i, res = 0;
403         char *p;
404
405         printf ("HEXDUMP UNICODE VERSION\n");
406
407         p = data;
408         for (i=0; i < 8; ++i)
409         {
410                 res += *p;
411                 printf("%0x ", (int) *(p++));
412         }
413         putchar('\n');
414
415         return res + 1000000;
416 }
417
418 typedef int (*intcharFunc)(char*);
419
420 void 
421 callFunction (intcharFunc f)
422 {
423         f ("ABC");
424 }
425
426 int
427 printInt (int* number)
428 {
429         printf( "<%d>\n", *number );
430         return *number + 1;
431 }
432
433
434 typedef struct {
435         char* str;
436         int i;
437 } SimpleObj;
438
439 int
440 class_marshal_test0 (SimpleObj *obj1)
441 {
442         printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
443
444         if (strcmp(obj1->str, "T1"))
445                 return -1;
446         if (obj1->i != 4)
447                 return -2;
448
449         return 0;
450 }
451
452 int
453 class_marshal_test4 (SimpleObj *obj1)
454 {
455         if (obj1)
456                 return -1;
457
458         return 0;
459 }
460
461 void
462 class_marshal_test1 (SimpleObj **obj1)
463 {
464         SimpleObj *res = malloc (sizeof (SimpleObj));
465
466         res->str = "ABC";
467         res->i = 5;
468
469         *obj1 = res;
470 }
471
472 int
473 class_marshal_test2 (SimpleObj **obj1)
474 {
475         printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
476
477         if (strcmp((*obj1)->str, "ABC"))
478                 return -1;
479         if ((*obj1)->i != 5)
480                 return -2;
481
482         return 0;
483 }
484
485 int
486 string_marshal_test0 (char *str)
487 {
488         if (strcmp (str, "TEST0"))
489                 return -1;
490
491         return 0;
492 }
493
494 void
495 string_marshal_test1 (char **str)
496 {
497         *str = "TEST1";
498 }
499
500 int
501 string_marshal_test2 (char **str)
502 {
503         printf ("string_marshal_test2 %s\n", *str);
504
505         if (strcmp (*str, "TEST1"))
506                 return -1;
507
508         return 0;
509 }
510
511 int
512 string_marshal_test3 (char *str)
513 {
514         if (str)
515                 return -1;
516
517         return 0;
518 }
519
520 char *
521 functionReturningString (void)
522 {
523     return "ABC";
524 }
525
526 typedef struct {
527         int a;
528         int b;
529 } VectorList;
530
531
532 VectorList* TestVectorList (VectorList *vl)
533 {
534         printf ("TestVectorList %d %d\n", vl->a, vl->b);
535
536         vl->a++;
537         vl->b++;
538
539         return vl;
540 }
541
542
543 typedef struct _OSVERSIONINFO
544
545         int a; 
546         int b; 
547 } OSVERSIONINFO; 
548
549 int 
550 GetVersionEx (OSVERSIONINFO *osvi)
551 {
552
553         printf ("GOT %d %d\n", osvi->a, osvi->b);
554
555         osvi->a += 1;
556         osvi->b += 1;
557
558         return osvi->a + osvi->b;
559 }