Fix / Improve with-foreign-pointer-as-string in manual
[cffi.git] / tests / libtest.c
blob2aba85848a27cdd958aaf1363b2b7964c2bc3ca4
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*-
3 * libtest.c --- auxiliary C lib for testing purposes
5 * Copyright (C) 2005-2007, Luis Oliveira <loliveira(@)common-lisp.net>
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #ifdef WIN32
29 #define DLLEXPORT __declspec(dllexport)
30 #else
31 #define DLLEXPORT
32 #endif
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <math.h>
40 #include <float.h>
41 #include <stdbool.h>
42 #include <stdarg.h>
43 #include <stdint.h>
44 #include <stddef.h>
46 #ifdef WIN32
47 #ifdef _MSC_VER
48 #define STDCALL __stdcall
49 #else
50 #define STDCALL __attribute__((stdcall))
51 #endif
52 #else
53 #define STDCALL
54 #endif
57 * Some functions that aren't available on WIN32
60 DLLEXPORT
61 float my_sqrtf(float n)
63 return (float) sqrt((double) n);
66 DLLEXPORT
67 char *my_strdup(const char *str)
69 char *p = malloc(strlen(str) + 1);
70 strcpy(p, str);
71 return p;
74 DLLEXPORT
75 void my_strfree(char *str)
77 free(str);
80 DLLEXPORT
81 long long my_llabs(long long n)
83 return n < 0 ? -n : n;
87 DLLEXPORT
88 unsigned long long ullong(unsigned long long n)
90 return n == ULLONG_MAX ? n : 42;
94 * Foreign Globals
96 * (var_int is used in MISC-TYPES.EXPAND.3 as well)
99 DLLEXPORT char * dll_version = "20120107";
101 /* TODO: look into signed char vs. unsigned char issue */
102 DLLEXPORT char var_char = -127;
103 DLLEXPORT unsigned char var_unsigned_char = 255;
104 DLLEXPORT short var_short = -32767;
105 DLLEXPORT unsigned short var_unsigned_short = 65535;
106 DLLEXPORT int var_int = -32767;
107 DLLEXPORT unsigned int var_unsigned_int = 65535;
108 DLLEXPORT long var_long = -2147483647L;
109 DLLEXPORT unsigned long var_unsigned_long = 4294967295UL;
110 DLLEXPORT float var_float = 42.0f;
111 DLLEXPORT double var_double = 42.0;
112 DLLEXPORT void * var_pointer = NULL;
113 DLLEXPORT char * var_string = "Hello, foreign world!";
115 DLLEXPORT long long var_long_long = -9223372036854775807LL;
116 DLLEXPORT unsigned long long var_unsigned_long_long = 18446744073709551615ULL;
118 DLLEXPORT float float_max = FLT_MAX;
119 DLLEXPORT float float_min = FLT_MIN;
120 DLLEXPORT double double_max = DBL_MAX;
121 DLLEXPORT double double_min = DBL_MIN;
124 * Callbacks
127 DLLEXPORT
128 int expect_char_sum(char (*f)(char, char))
130 return f('a', 3) == 'd';
133 DLLEXPORT
134 int expect_unsigned_char_sum(unsigned char (*f)(unsigned char, unsigned char))
136 return f(UCHAR_MAX-1, 1) == UCHAR_MAX;
139 DLLEXPORT
140 int expect_short_sum(short (*f)(short a, short b))
142 return f(SHRT_MIN+1, -1) == SHRT_MIN;
145 DLLEXPORT
146 int expect_unsigned_short_sum(unsigned short (*f)(unsigned short,
147 unsigned short))
149 return f(USHRT_MAX-1, 1) == USHRT_MAX;
152 /* used in MISC-TYPES.EXPAND.4 as well */
153 DLLEXPORT
154 int expect_int_sum(int (*f)(int, int))
156 return f(INT_MIN+1, -1) == INT_MIN;
159 DLLEXPORT
160 int expect_unsigned_int_sum(unsigned int (*f)(unsigned int, unsigned int))
162 return f(UINT_MAX-1, 1) == UINT_MAX;
165 DLLEXPORT
166 int expect_long_sum(long (*f)(long, long))
168 return f(LONG_MIN+1, -1) == LONG_MIN;
171 DLLEXPORT
172 int expect_unsigned_long_sum(unsigned long (*f)(unsigned long, unsigned long))
174 return f(ULONG_MAX-1, 1) == ULONG_MAX;
177 DLLEXPORT
178 int expect_long_long_sum(long long (*f)(long long, long long))
180 return f(LLONG_MIN+1, -1) == LLONG_MIN;
183 DLLEXPORT
184 int expect_unsigned_long_long_sum (unsigned long long
185 (*f)(unsigned long long, unsigned long long))
187 return f(ULLONG_MAX-1, 1) == ULLONG_MAX;
190 DLLEXPORT
191 int expect_float_sum(float (*f)(float, float))
193 /*printf("\n>>> FLOAT: %f <<<\n", f(20.0f, 22.0f));*/
194 return f(20.0f, 22.0f) == 42.0f;
197 DLLEXPORT
198 int expect_double_sum(double (*f)(double, double))
200 /*printf("\n>>> DOUBLE: %f<<<\n", f(-20.0, -22.0));*/
201 return f(-20.0, -22.0) == -42.0;
204 DLLEXPORT
205 int expect_long_double_sum(long double (*f)(long double, long double))
207 /*printf("\n>>> DOUBLE: %f<<<\n", f(-20.0, -22.0));*/
208 return f(-20.0, -22.0) == -42.0;
211 DLLEXPORT
212 int expect_pointer_sum(void* (*f)(void*, int))
214 return f(NULL, 0xDEAD) == (void *) 0xDEAD;
217 DLLEXPORT
218 int expect_strcat(char* (*f)(char*, char*))
220 char *ret = f("Hello, ", "C world!");
221 int res = strcmp(ret, "Hello, C world!") == 0;
222 /* commented out as a quick fix on platforms that don't
223 foreign allocate in C malloc space. */
224 /*free(ret);*/ /* is this allowed? */
225 return res;
228 DLLEXPORT
229 void pass_int_ref(void (*f)(int*))
231 int x = 1984;
232 f(&x);
236 * Enums
239 typedef enum {
240 ONE = 1,
241 TWO,
242 THREE,
243 FOUR,
244 FORTY_ONE = 41,
245 FORTY_TWO
246 } numeros;
248 DLLEXPORT
249 int check_enums(numeros one, numeros two, numeros three, numeros four,
250 numeros forty_one, numeros forty_two)
252 if (one == ONE && two == TWO && three == THREE && four == FOUR &&
253 forty_one == FORTY_ONE && forty_two == FORTY_TWO)
254 return 1;
256 return 0;
259 typedef enum { FALSE, TRUE } another_boolean;
261 DLLEXPORT
262 another_boolean return_enum(int x)
264 if (x == 0)
265 return FALSE;
266 else
267 return TRUE;
271 * Booleans
274 DLLEXPORT
275 int equalequal(int a, unsigned int b)
277 return ((unsigned int) a) == b;
280 DLLEXPORT
281 char bool_and(unsigned char a, char b)
283 return a && b;
286 DLLEXPORT
287 unsigned long bool_xor(long a, unsigned long b)
289 return (a && !b) || (!a && b);
292 DLLEXPORT
293 unsigned sizeof_bool(void)
295 return (unsigned) sizeof(_Bool);
298 DLLEXPORT
299 unsigned bool_to_unsigned(_Bool b)
301 return (unsigned) b;
304 DLLEXPORT
305 _Bool unsigned_to_bool(unsigned u)
307 return (_Bool) u;
311 * Test struct alignment issues. These comments assume the x86 gABI.
312 * Hopefully these tests will spot alignment issues in others archs
313 * too.
317 * STRUCT.ALIGNMENT.1
320 struct s_ch {
321 char a_char;
324 /* This struct's size should be 2 bytes */
325 struct s_s_ch {
326 char another_char;
327 struct s_ch a_s_ch;
330 DLLEXPORT
331 struct s_s_ch the_s_s_ch = { 2, { 1 } };
334 * STRUCT.ALIGNMENT.2
337 /* This one should be alignment should be the same as short's alignment. */
338 struct s_short {
339 char a_char;
340 char another_char;
341 short a_short;
344 struct s_s_short {
345 char yet_another_char;
346 struct s_short a_s_short; /* so this should be 2-byte aligned */
347 }; /* size: 6 bytes */
349 DLLEXPORT
350 struct s_s_short the_s_s_short = { 4, { 1, 2, 3 } };
353 * STRUCT.ALIGNMENT.3
356 /* This test will, among other things, check for the existence tail padding. */
358 struct s_double {
359 char a_char; /* 1 byte */
360 /* padding: 3 bytes */
361 double a_double; /* 8 bytes */
362 char another_char; /* 1 byte */
363 /* padding: 3 bytes */
364 }; /* total size: 16 bytes */
366 struct s_s_double {
367 char yet_another_char; /* 1 byte */
368 /* 3 bytes padding */
369 struct s_double a_s_double; /* 16 bytes */
370 short a_short; /* 2 byte */
371 /* 2 bytes padding */
372 }; /* total size: 24 bytes */
374 DLLEXPORT
375 struct s_s_double the_s_s_double = { 4, { 1, 2.0, 3 }, 5 };
378 * STRUCT.ALIGNMENT.4
380 struct s_s_s_double {
381 short another_short; /* 2 bytes */
382 /* 2 bytes padding */
383 struct s_s_double a_s_s_double; /* 24 bytes */
384 char last_char; /* 1 byte */
385 /* 3 bytes padding */
386 }; /* total size: 32 */
388 DLLEXPORT
389 struct s_s_s_double the_s_s_s_double = { 6, { 4, { 1, 2.0, 3 }, 5 }, 7 };
392 * STRUCT.ALIGNMENT.5
395 /* MacOSX ABI says: "The embedding alignment of the first element in a data
396 structure is equal to the element's natural alignment." and "For subsequent
397 elements that have a natural alignment greater than 4 bytes, the embedding
398 alignment is 4, unless the element is a vector." */
400 /* note: these rules will apply to the structure itself. So, unless it is
401 the first element of another structure, its alignment will be 4. */
403 /* the following offsets and sizes are specific to darwin/ppc32 */
405 struct s_double2 {
406 double a_double; /* 8 bytes (alignment 8) */
407 short a_short; /* 2 bytes */
408 /* 6 bytes padding */
409 }; /* total size: 16 */
411 struct s_s_double2 {
412 char a_char; /* 1 byte */
413 /* 3 bytes padding */
414 struct s_double2 a_s_double2; /* 16 bytes, alignment 4 */
415 short another_short; /* 2 bytes */
416 /* 2 bytes padding */
417 }; /* total size: 24 bytes */
418 /* alignment: 4 */
420 DLLEXPORT
421 struct s_s_double2 the_s_s_double2 = { 3, { 1.0, 2 }, 4 };
424 * STRUCT.ALIGNMENT.6
427 /* Same as STRUCT.ALIGNMENT.5 but with long long. */
429 struct s_long_long {
430 long long a_long_long; /* 8 bytes (alignment 8) */
431 short a_short; /* 2 bytes */
432 /* 6 bytes padding */
433 }; /* total size: 16 */
435 struct s_s_long_long {
436 char a_char; /* 1 byte */
437 /* 3 bytes padding */
438 struct s_long_long a_s_long_long; /* 16 bytes, alignment 4 */
439 short a_short; /* 2 bytes */
440 /* 2 bytes padding */
441 }; /* total size: 24 bytes */
442 /* alignment: 4 */
444 DLLEXPORT
445 struct s_s_long_long the_s_s_long_long = { 3, { 1, 2 }, 4 };
448 * STRUCT.ALIGNMENT.7
451 /* Another test for Darwin's PPC32 ABI. */
453 struct s_s_double3 {
454 struct s_double2 a_s_double2; /* 16 bytes, alignment 8*/
455 short another_short; /* 2 bytes */
456 /* 6 bytes padding */
457 }; /* total size: 24 */
459 struct s_s_s_double3 {
460 struct s_s_double3 a_s_s_double3; /* 24 bytes */
461 char a_char; /* 1 byte */
462 /* 7 bytes padding */
463 }; /* total size: 32 */
465 DLLEXPORT
466 struct s_s_s_double3 the_s_s_s_double3 = { { { 1.0, 2 }, 3 }, 4 };
469 * STRUCT.ALIGNMENT.8
472 /* Same as STRUCT.ALIGNMENT.[56] but with unsigned long long. */
474 struct s_unsigned_long_long {
475 unsigned long long an_unsigned_long_long; /* 8 bytes (alignment 8) */
476 short a_short; /* 2 bytes */
477 /* 6 bytes padding */
478 }; /* total size: 16 */
480 struct s_s_unsigned_long_long {
481 char a_char; /* 1 byte */
482 /* 3 bytes padding */
483 struct s_unsigned_long_long a_s_unsigned_long_long; /* 16 bytes, align 4 */
484 short a_short; /* 2 bytes */
485 /* 2 bytes padding */
486 }; /* total size: 24 bytes */
487 /* alignment: 4 */
489 DLLEXPORT
490 struct s_s_unsigned_long_long the_s_s_unsigned_long_long = { 3, { 1, 2 }, 4 };
492 /* STRUCT.ALIGNMENT.x */
494 /* commented this test out because this is not standard C
495 and MSVC++ (or some versions of it at least) won't compile it. */
498 struct empty_struct {};
500 struct with_empty_struct {
501 struct empty_struct foo;
502 int an_int;
505 DLLEXPORT
506 struct with_empty_struct the_with_empty_struct = { {}, 42 };
510 * STRUCT-VALUES.*
513 struct pair { int a, b; };
515 DLLEXPORT
516 int pair_sum(struct pair p)
518 return p.a + p.b;
521 DLLEXPORT
522 int pair_pointer_sum(struct pair *p)
524 return p->a + p->b;
527 DLLEXPORT
528 struct pair make_pair(int a, int b)
530 return (struct pair) { a, b };
533 DLLEXPORT
534 struct pair *alloc_pair(int a, int b)
536 struct pair *p = malloc(sizeof(struct pair));
537 p->a = a;
538 p->b = b;
539 return p;
542 struct pair_plus_one {
543 struct pair p;
544 int c;
547 DLLEXPORT
548 int pair_plus_one_sum(struct pair_plus_one p)
550 return p.p.a + p.p.b + p.c;
553 DLLEXPORT
554 int pair_plus_one_pointer_sum(struct pair_plus_one *p)
556 return p->p.a + p->p.b + p->c;
559 DLLEXPORT
560 struct pair_plus_one make_pair_plus_one(int a, int b, int c)
562 return (struct pair_plus_one) { { a, b }, c };
565 DLLEXPORT
566 struct pair_plus_one *alloc_pair_plus_one(int a, int b, int c)
568 struct pair_plus_one *p = malloc(sizeof(struct pair_plus_one));
569 p->p.a = a;
570 p->p.b = b;
571 p->c = c;
572 return p;
576 * DEFCFUN.NOARGS and DEFCFUN.NOOP
579 DLLEXPORT
580 int noargs()
582 return 42;
585 DLLEXPORT
586 void noop()
588 return;
592 * DEFCFUN.BFF.1
594 * (let ((rettype (find-type :long))
595 * (arg-types (n-random-types-no-ll 127)))
596 * (c-function rettype arg-types)
597 * (gen-function-test rettype arg-types))
600 DLLEXPORT long sum_127_no_ll
601 (long a1, unsigned long a2, short a3, unsigned short a4, float a5,
602 double a6, unsigned long a7, float a8, unsigned char a9, unsigned
603 short a10, short a11, unsigned long a12, double a13, long a14,
604 unsigned int a15, void* a16, unsigned int a17, unsigned short a18,
605 long a19, float a20, void* a21, float a22, int a23, int a24, unsigned
606 short a25, long a26, long a27, double a28, unsigned char a29, unsigned
607 int a30, unsigned int a31, int a32, unsigned short a33, unsigned int
608 a34, void* a35, double a36, double a37, long a38, short a39, unsigned
609 short a40, long a41, char a42, long a43, unsigned short a44, void*
610 a45, int a46, unsigned int a47, double a48, unsigned char a49,
611 unsigned char a50, float a51, int a52, unsigned short a53, double a54,
612 short a55, unsigned char a56, unsigned long a57, float a58, float a59,
613 float a60, void* a61, void* a62, unsigned int a63, unsigned long a64,
614 char a65, short a66, unsigned short a67, unsigned long a68, void* a69,
615 float a70, double a71, long a72, unsigned long a73, short a74,
616 unsigned int a75, unsigned short a76, int a77, unsigned short a78,
617 char a79, double a80, short a81, unsigned char a82, float a83, char
618 a84, int a85, double a86, unsigned char a87, int a88, unsigned long
619 a89, double a90, short a91, short a92, unsigned int a93, unsigned char
620 a94, float a95, long a96, float a97, long a98, long a99, int a100, int
621 a101, unsigned int a102, char a103, char a104, unsigned short a105,
622 unsigned int a106, unsigned short a107, unsigned short a108, int a109,
623 long a110, char a111, double a112, unsigned int a113, char a114, short
624 a115, unsigned long a116, unsigned int a117, short a118, unsigned char
625 a119, float a120, void* a121, double a122, int a123, long a124, char
626 a125, unsigned short a126, float a127)
628 return (long) a1 + a2 + a3 + a4 + ((long) a5) + ((long) a6) + a7 +
629 ((long) a8) + a9 + a10 + a11 + a12 + ((long) a13) + a14 + a15 +
630 ((intptr_t) a16) + a17 + a18 + a19 + ((long) a20) +
631 ((intptr_t) a21) + ((long) a22) + a23 + a24 + a25 + a26 + a27 +
632 ((long) a28) + a29 + a30 + a31 + a32 + a33 + a34 + ((intptr_t) a35) +
633 ((long) a36) + ((long) a37) + a38 + a39 + a40 + a41 + a42 + a43 + a44 +
634 ((intptr_t) a45) + a46 + a47 + ((long) a48) + a49 + a50 +
635 ((long) a51) + a52 + a53 + ((long) a54) + a55 + a56 + a57 + ((long) a58) +
636 ((long) a59) + ((long) a60) + ((intptr_t) a61) +
637 ((intptr_t) a62) + a63 + a64 + a65 + a66 + a67 + a68 +
638 ((intptr_t) a69) + ((long) a70) + ((long) a71) + a72 + a73 + a74 +
639 a75 + a76 + a77 + a78 + a79 + ((long) a80) + a81 + a82 + ((long) a83) +
640 a84 + a85 + ((long) a86) + a87 + a88 + a89 + ((long) a90) + a91 + a92 +
641 a93 + a94 + ((long) a95) + a96 + ((long) a97) + a98 + a99 + a100 + a101 +
642 a102 + a103 + a104 + a105 + a106 + a107 + a108 + a109 + a110 + a111 +
643 ((long) a112) + a113 + a114 + a115 + a116 + a117 + a118 + a119 +
644 ((long) a120) + ((intptr_t) a121) + ((long) a122) + a123 + a124 +
645 a125 + a126 + ((long) a127);
649 * DEFCFUN.BFF.2
651 * (let ((rettype (find-type :long-long))
652 * (arg-types (n-random-types 127)))
653 * (c-function rettype arg-types)
654 * (gen-function-test rettype arg-types))
657 DLLEXPORT long long sum_127
658 (void* a1, void* a2, float a3, unsigned long a4, void* a5, long long
659 a6, double a7, double a8, unsigned short a9, int a10, long long a11,
660 long a12, short a13, unsigned int a14, long a15, unsigned char a16,
661 int a17, double a18, short a19, short a20, long long a21, unsigned
662 int a22, unsigned short a23, short a24, void* a25, short a26,
663 unsigned short a27, unsigned short a28, int a29, long long a30,
664 void* a31, int a32, unsigned long a33, unsigned long a34, void* a35,
665 unsigned long long a36, float a37, int a38, short a39, void* a40,
666 unsigned long long a41, long long a42, unsigned long a43, unsigned
667 long a44, unsigned long long a45, unsigned long a46, char a47,
668 double a48, long a49, unsigned int a50, int a51, short a52, void*
669 a53, long a54, unsigned long long a55, int a56, unsigned short a57,
670 unsigned long long a58, float a59, void* a60, float a61, unsigned
671 short a62, unsigned long a63, float a64, unsigned int a65, unsigned
672 long long a66, void* a67, double a68, unsigned long long a69, double
673 a70, double a71, long long a72, void* a73, unsigned short a74, long
674 a75, void* a76, short a77, double a78, long a79, unsigned char a80,
675 void* a81, unsigned char a82, long a83, double a84, void* a85, int
676 a86, double a87, unsigned char a88, double a89, short a90, long a91,
677 int a92, long a93, double a94, unsigned short a95, unsigned int a96,
678 int a97, char a98, long long a99, double a100, float a101, unsigned
679 long a102, short a103, void* a104, float a105, long long a106, int
680 a107, long long a108, long long a109, double a110, unsigned long
681 long a111, double a112, unsigned long a113, char a114, char a115,
682 unsigned long a116, short a117, unsigned char a118, unsigned char
683 a119, int a120, int a121, float a122, unsigned char a123, unsigned
684 char a124, double a125, unsigned long long a126, char a127)
686 return (long long) ((intptr_t) a1) + ((intptr_t) a2) + ((long) a3) +
687 a4 + ((intptr_t) a5) + a6 + ((long) a7) + ((long) a8) + a9 + a10 +
688 a11 + a12 + a13 + a14 + a15 + a16 + a17 + ((long) a18) + a19 + a20 +
689 a21 + a22 + a23 + a24 + ((intptr_t) a25) + a26 + a27 + a28 + a29 +
690 a30 + ((intptr_t) a31) + a32 + a33 + a34 + ((intptr_t) a35) +
691 a36 + ((long) a37) + a38 + a39 + ((intptr_t) a40) + a41 + a42 + a43 +
692 a44 + a45 + a46 + a47 + ((long) a48) + a49 + a50 + a51 + a52 +
693 ((intptr_t) a53) + a54 + a55 + a56 + a57 + a58 + ((long) a59) +
694 ((intptr_t) a60) + ((long) a61) + a62 + a63 + ((long) a64) + a65 + a66
695 + ((intptr_t) a67) + ((long) a68) + a69 + ((long) a70) + ((long) a71) +
696 a72 + ((intptr_t) a73) + a74 + a75 + ((intptr_t) a76) + a77 +
697 ((long) a78) + a79 + a80 + ((intptr_t) a81) + a82 + a83 + ((long) a84)
698 + ((intptr_t) a85) + a86 + ((long) a87) + a88 + ((long) a89) + a90 +
699 a91 + a92 + a93 + ((long) a94) + a95 + a96 + a97 + a98 + a99 +
700 ((long) a100) + ((long) a101) + a102 + a103 + ((intptr_t) a104) +
701 ((long) a105) + a106 + a107 + a108 + a109 + ((long) a110) + a111 +
702 ((long) a112) + a113 + a114 + a115 + a116 + a117 + a118 + a119 + a120 +
703 a121 + ((long) a122) + a123 + a124 + ((long) a125) + a126 + a127;
707 * CALLBACKS.BFF.1 (cb-test :no-long-long t)
710 DLLEXPORT long call_sum_127_no_ll
711 (long (*func)
712 (unsigned long, void*, long, double, unsigned long, float, float,
713 int, unsigned int, double, double, double, void*, unsigned short,
714 unsigned short, void*, long, long, int, short, unsigned short,
715 unsigned short, char, long, void*, void*, char, unsigned char,
716 unsigned long, short, int, int, unsigned char, short, long, long,
717 void*, unsigned short, char, double, unsigned short, void*, short,
718 unsigned long, unsigned short, float, unsigned char, short, float,
719 short, char, unsigned long, unsigned long, char, float, long, void*,
720 short, float, unsigned int, float, unsigned int, double, unsigned int,
721 unsigned char, int, long, char, short, double, int, void*, char,
722 unsigned short, void*, unsigned short, void*, unsigned long, double,
723 void*, long, float, unsigned short, unsigned short, void*, float, int,
724 unsigned int, double, float, long, void*, unsigned short, float,
725 unsigned char, unsigned char, float, unsigned int, float, unsigned
726 short, double, unsigned short, unsigned long, unsigned int, unsigned
727 long, void*, unsigned char, char, char, unsigned short, unsigned long,
728 float, short, void*, long, unsigned short, short, double, short, int,
729 char, unsigned long, long, int, void*, double, unsigned char))
731 return
732 func(948223085, (void *) 803308438, -465723152, 20385,
733 219679466, -10035, 13915, -1193455756, 1265303699, 27935, -18478,
734 -10508, (void *) 215389089, 55561, 55472, (void *) 146070433,
735 -1040819989, -17851453, -1622662247, -19473, 20837, 30216, 79,
736 986800400, (void *) 390281604, (void *) 1178532858, 19, 117,
737 78337699, -5718, -991300738, 872160910, 184, 926, -1487245383,
738 1633973783, (void *) 33738609, 53985, -116, 31645, 27196, (void *)
739 145569903, -6960, 17252220, 47404, -10491, 88, -30438, -21212,
740 -1982, -16, 1175270, 7949380, -121, 8559, -432968526, (void *)
741 293455312, 11894, -8394, 142421516, -25758, 3422998, 4004,
742 15758212, 198, -1071899743, -1284904617, -11, -17219, -30039,
743 311589092, (void *) 541468577, 123, 63517, (void *) 1252504506,
744 39368, (void *) 10057868, 134781408, -7143, (void *) 72825877,
745 -1190798667, -30862, 63757, 14965, (void *) 802391252, 22008,
746 -517289619, 806091099, 1125, 451, -498145176, (void *) 55960931,
747 15379, 4629, 184, 254, 22532, 465856451, -1669, 49416, -16546,
748 2983, 4337541, 65292495, 39253529, (void *) 669025, 211, 85, -19,
749 24298, 65358, 16776, -29957, (void *) 124311, -163231228, 2610,
750 -7806, 26434, -21913, -753615541, 120, 358697932, -1198889034,
751 -2131350926, (void *) 3749492036, -13413, 17);
755 * CALLBACKS.BFF.2 (cb-test)
758 DLLEXPORT long long call_sum_127
759 (long long (*func)
760 (short, char, void*, float, long, double, unsigned long long,
761 unsigned short, unsigned char, char, char, unsigned short, unsigned
762 long long, unsigned short, long long, unsigned short, unsigned long
763 long, unsigned char, unsigned char, unsigned long long, long long,
764 char, float, unsigned int, float, float, unsigned int, float, char,
765 unsigned char, long, long long, unsigned char, double, long,
766 double, unsigned int, unsigned short, long long, unsigned int, int,
767 unsigned long long, long, short, unsigned int, unsigned int,
768 unsigned long long, unsigned int, long, void*, unsigned char, char,
769 long long, unsigned short, unsigned int, float, unsigned char,
770 unsigned long, long long, float, long, float, int, float, unsigned
771 short, unsigned long long, short, unsigned long, long, char,
772 unsigned short, long long, short, double, void*, unsigned int,
773 char, unsigned int, void*, void*, unsigned char, void*, unsigned
774 short, unsigned char, long, void*, char, long, unsigned short,
775 unsigned char, double, unsigned long long, unsigned short, unsigned
776 short, unsigned int, long, char, long, char, short, unsigned short,
777 unsigned long, unsigned long, short, long long, long long, long
778 long, double, unsigned short, unsigned char, short, unsigned char,
779 long, long long, unsigned long long, unsigned int, unsigned long,
780 unsigned char, long long, unsigned char, unsigned long long,
781 double, unsigned char, long long, unsigned char, char, long long))
783 return
784 func(-8573, 14, (void *) 832601021, -32334, -1532040888,
785 -18478, 2793023182591311826, 2740, 230, 103, 97, 13121,
786 5112369026351511084, 7763, -8134147951003417418, 34348,
787 5776613699556468853, 19, 122, 1431603726926527625,
788 439503521880490337, -112, -21557, 1578969190, -22008, -4953,
789 2127745975, -7262, -6, 180, 226352974, -3928775366167459219, 134,
790 -17730, -1175042526, 23868, 3494181009, 57364,
791 3134876875147518682, 104531655, -1286882727, 803577887579693487,
792 1349268803, 24912, 3313099419, 3907347884, 1738833249233805034,
793 2794230885, 1008818752, (void *) 1820044575, 189, 61,
794 -931654560961745071, 57531, 3096859985, 10405, 220, 3631311224,
795 -8531370353478907668, 31258, 678896693, -32150, -1869057813,
796 -19877, 62841, 4161660185772906873, -23869, 4016251006, 610353435,
797 105, 47315, -1051054492535331660, 6846, -15163, (void *)
798 736672359, 2123928476, -122, 3859258652, (void *) 3923394833,
799 (void *) 1265031970, 161, (void *) 1993867800, 55056, 122,
800 1562112760, (void *) 866615125, -79, -1261399547, 31737, 254,
801 -31279, 5462649659172897980, 5202, 7644, 174224940, -337854382,
802 -45, -583502442, -37, -13266, 24520, 2198606699, 2890453969,
803 -8282, -2295716637858246075, -1905178488651598878,
804 -6384652209316714643, 14841, 35443, 132, 15524, 187, 2138878229,
805 -5153032566879951000, 9056545530140684207, 4124632010, 276167701,
806 56, -2307310370663738730, 66, 9113015627153789746, -9618, 167,
807 755753399701306200, 119, -28, -990561962725435433);
811 * CALLBACKS.DOUBLE26
814 DLLEXPORT double call_double26
815 (double (*f)(double, double, double, double, double, double, double, double,
816 double, double, double, double, double, double, double, double,
817 double, double, double, double, double, double, double, double,
818 double, double))
820 return f(3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14,
821 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14, 3.14,
822 3.14, 3.14, 3.14, 3.14);
826 * DEFCFUN.DOUBLE26 and FUNCALL.DOUBLE26
829 DLLEXPORT
830 double sum_double26(double a1, double a2, double a3, double a4, double a5,
831 double a6, double a7, double a8, double a9, double a10,
832 double a11, double a12, double a13, double a14, double a15,
833 double a16, double a17, double a18, double a19, double a20,
834 double a21, double a22, double a23, double a24, double a25,
835 double a26)
837 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 +
838 a14 + a15 + a16 + a17 + a18 + a19 + a20 + a21 + a22 + a23 + a24 + a25 +
839 a26;
843 * DEFCFUN.VARARGS.NOSTDLIB and FUNCALL.VARARGS.NOSTDLIB
845 DLLEXPORT
846 double sum_double_arbitrary(int n, ...)
848 va_list ap;
849 double sum = 0;
850 va_start(ap, n);
851 for(int j=0; j<n; j++)
852 sum += va_arg(ap, double);
853 va_end(ap);
854 return sum;
858 * CALLBACKS.FLOAT26
861 DLLEXPORT float call_float26
862 (float (*f)(float, float, float, float, float, float, float, float,
863 float, float, float, float, float, float, float, float,
864 float, float, float, float, float, float, float, float,
865 float, float))
867 return f(5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0,
868 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0,
869 5.0, 5.0, 5.0, 5.0);
873 * DEFCFUN.FLOAT26 and FUNCALL.FLOAT26
876 DLLEXPORT
877 float sum_float26(float a1, float a2, float a3, float a4, float a5,
878 float a6, float a7, float a8, float a9, float a10,
879 float a11, float a12, float a13, float a14, float a15,
880 float a16, float a17, float a18, float a19, float a20,
881 float a21, float a22, float a23, float a24, float a25,
882 float a26)
884 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 +
885 a14 + a15 + a16 + a17 + a18 + a19 + a20 + a21 + a22 + a23 + a24 + a25 +
886 a26;
890 * Symbol case.
893 DLLEXPORT int UPPERCASEINT1 = 12345;
894 DLLEXPORT int UPPER_CASE_INT1 = 23456;
895 DLLEXPORT int MiXeDCaSeInT1 = 34567;
896 DLLEXPORT int MiXeD_CaSe_InT1 = 45678;
898 DLLEXPORT int UPPERCASEINT2 = 12345;
899 DLLEXPORT int UPPER_CASE_INT2 = 23456;
900 DLLEXPORT int MiXeDCaSeInT2 = 34567;
901 DLLEXPORT int MiXeD_CaSe_InT2 = 45678;
903 DLLEXPORT int UPPERCASEINT3 = 12345;
904 DLLEXPORT int UPPER_CASE_INT3 = 23456;
905 DLLEXPORT int MiXeDCaSeInT3 = 34567;
906 DLLEXPORT int MiXeD_CaSe_InT3 = 45678;
909 * FOREIGN-SYMBOL-POINTER.1
912 DLLEXPORT int compare_against_abs(intptr_t p)
914 return p == (intptr_t) abs;
918 * FOREIGN-SYMBOL-POINTER.2
921 DLLEXPORT void xpto_fun() {}
923 DLLEXPORT
924 int compare_against_xpto_fun(intptr_t p)
926 return p == (intptr_t) xpto_fun;
930 * [DEFCFUN|FUNCALL].NAMESPACE.1
933 DLLEXPORT
934 int ns_function()
936 return 1;
940 * FOREIGN-GLOBALS.NAMESPACE.*
943 DLLEXPORT int ns_var = 1;
946 * DEFCFUN.STDCALL.1
949 DLLEXPORT
950 int STDCALL stdcall_fun(int a, int b, int c)
952 return a + b + c;
956 * CALLBACKS.STDCALL.1
959 DLLEXPORT
960 int call_stdcall_fun(int (STDCALL *f)(int, int, int))
962 int a = 42;
963 f(1, 2, 3);
964 return a;
967 /* Unlike the one above, this commented test below actually
968 * works. But, alas, it doesn't compile with -std=c99. */
971 DLLEXPORT
972 int call_stdcall_fun(int __attribute__((stdcall)) (*f)(int, int, int))
974 asm("pushl $42");
975 register int ebx asm("%ebx");
976 f(1, 2, 3);
977 asm("popl %ebx");
978 return ebx;
982 /* vim: ts=4 et
986 * STDINT.TYPES.1
989 DLLEXPORT
990 unsigned sizeof_ptrdiff(void)
992 return (unsigned) sizeof(ptrdiff_t);
995 DLLEXPORT
996 unsigned sizeof_size(void)
998 return (unsigned) sizeof(size_t);
1001 DLLEXPORT
1002 unsigned sizeof_offset(void)
1004 return (unsigned) sizeof(off_t);
1007 DLLEXPORT
1008 unsigned sizeof_uintptr(void)
1010 return (unsigned) sizeof(uintptr_t);
1013 DLLEXPORT
1014 unsigned sizeof_intptr(void)
1016 return (unsigned) sizeof(intptr_t);