struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / funptrs.c.in
blob0ff72bf8d50297400716fe5458695b4f088e151a
1 /** Function pointer tests.
3 type: bool, char, int, long
4 */
5 #include <testfwk.h>
7 #ifdef __SDCC
8 #pragma std_sdcc99
9 #endif
11 #include <stdbool.h>
13 #define TYPE_{type}
15 /* Must use a typedef as there is no way of adding the code modifier
16 on the z80.
18 typedef void (*NOARGFUNPTR)(void);
19 typedef void (*ONEARGFUNPTR)({type}) __reentrant;
20 typedef long int (*FOURARGFUNPTR)(char, char, long int, long int) __reentrant;
21 typedef {type} (*TYPEFUNPTR)({type}, {type}) __reentrant;
23 int count;
24 FOURARGFUNPTR fafp;
25 TYPEFUNPTR tfp;
27 void
28 incCount(void)
30 count++;
33 void
34 incBy({type} a) __reentrant
36 count += a;
39 long int f6(char a, char b, long int c, long int d) __reentrant
41 switch (a)
43 case 0: return a;
44 case 1: return b;
45 case 2: return c;
46 case 3: return d;
48 return 0;
52 void
53 callViaPtr(NOARGFUNPTR fptr)
55 (*fptr)();
58 void
59 callViaPtr2(ONEARGFUNPTR fptr, {type} arg)
61 (*fptr)(arg);
64 void
65 callViaPtr3(void (*fptr)(void))
67 (*fptr)();
70 void
71 callViaPtrAnsi(NOARGFUNPTR fptr)
73 fptr();
76 void
77 callViaPtr2Ansi(ONEARGFUNPTR fptr, {type} arg)
79 fptr(arg);
82 void
83 callViaPtr3Ansi(void (*fptr)(void))
85 fptr();
88 {type} f_ret({type} arg1, {type} arg2) __reentrant
90 {type} local;
91 local = !arg1;
92 return (local & arg2);
96 void
97 testFunPtr(void)
99 #if !defined(__SDCC_pdk14) // Lack of memory
100 fafp = f6;
102 ASSERT(count == 0);
103 callViaPtr(incCount);
104 ASSERT(count == 1);
105 callViaPtr2(incBy, 7);
106 ASSERT(count == 8 || count == 2);
108 ASSERT((*fafp)(0, 0x55, 0x12345678, 0x9abcdef0) == 0);
109 ASSERT((*fafp)(1, 0x55, 0x12345678, 0x9abcdef0) == 0x55);
110 ASSERT((*fafp)(2, 0x55, 0x12345678, 0x9abcdef0) == 0x12345678);
111 ASSERT((*fafp)(3, 0x55, 0x12345678, 0x9abcdef0) == 0x9abcdef0);
112 #endif
115 void
116 testFunPtrAnsi(void)
118 #if !defined(__SDCC_pdk14) // Lack of memory
119 fafp = f6;
121 count = 0;
122 callViaPtrAnsi(incCount);
123 ASSERT(count == 1);
124 callViaPtr2Ansi(incBy, 7);
125 ASSERT(count == 8 || count == 2);
127 ASSERT(fafp(0, 0x55, 0x12345678, 0x9abcdef0) == 0);
128 ASSERT(fafp(1, 0x55, 0x12345678, 0x9abcdef0) == 0x55);
129 ASSERT(fafp(2, 0x55, 0x12345678, 0x9abcdef0) == 0x12345678);
130 ASSERT(fafp(3, 0x55, 0x12345678, 0x9abcdef0) == 0x9abcdef0);
131 #endif
134 void
135 testFunPtrReturn(void)
137 #if !defined(__SDCC_pdk14) // Lack of memory
138 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
139 tfp = f_ret;
141 ASSERT(tfp(0, 0) == 0);
142 ASSERT(tfp(0, 1) == 1);
143 ASSERT(tfp(1, 0) == 0);
144 ASSERT(tfp(1, 1) == 0);
145 #endif
146 #endif