ad5248b8426a070a6f50cbe812696f4007a5f807
[fw/sdcc] / support / regression / tests / funptrs.c
1 /** Function pointer tests.
2
3     type: char, int, long
4  */
5 #include <testfwk.h>
6
7 /* Must use a typedef as there is no way of adding the code modifier
8    on the z80.
9 */
10 typedef void (*NOARGFUNPTR)(void);
11 typedef void (*ONEARGFUNPTR)({type}) REENTRANT;
12 typedef long int (*FOURARGFUNPTR)(char, char, long int, long int) REENTRANT;
13
14 int count;
15 FOURARGFUNPTR fafp;
16
17 void
18 incCount(void)
19 {
20   count++;
21 }
22
23 void
24 incBy({type} a) REENTRANT
25 {
26   count += a;
27 }
28
29 long int f6(char a, char b, long int c, long int d) REENTRANT
30 {
31   switch (a)
32     {
33     case 0: return a;
34     case 1: return b;
35     case 2: return c;
36     case 3: return d;
37     }
38   return 0;
39 }
40
41
42 void
43 callViaPtr(NOARGFUNPTR fptr)
44 {
45   (*fptr)();
46 }
47
48 void
49 callViaPtr2(ONEARGFUNPTR fptr, {type} arg)
50 {
51   (*fptr)(arg);
52 }
53
54 void
55 callViaPtr3(void (*fptr)(void))
56 {
57   (*fptr)();
58 }
59
60 void
61 callViaPtrAnsi(NOARGFUNPTR fptr)
62 {
63   fptr();
64 }
65
66 void
67 callViaPtr2Ansi(ONEARGFUNPTR fptr, {type} arg)
68 {
69   fptr(arg);
70 }
71
72 void
73 callViaPtr3Ansi(void (*fptr)(void))
74 {
75   fptr();
76 }
77
78
79
80 void
81 testFunPtr(void)
82 {
83   fafp = f6;
84
85   ASSERT(count == 0);
86   callViaPtr(incCount);
87   ASSERT(count == 1);
88   callViaPtr2(incBy, 7);
89   ASSERT(count == 8);
90
91   ASSERT((*fafp)(0, 0x55, 0x12345678, 0x9abcdef0) == 0);
92   ASSERT((*fafp)(1, 0x55, 0x12345678, 0x9abcdef0) == 0x55);
93   ASSERT((*fafp)(2, 0x55, 0x12345678, 0x9abcdef0) == 0x12345678);
94   ASSERT((*fafp)(3, 0x55, 0x12345678, 0x9abcdef0) == 0x9abcdef0);
95 }
96
97 void
98 testFunPtrAnsi(void)
99 {
100   fafp = f6;
101
102   count = 0;
103   callViaPtrAnsi(incCount);
104   ASSERT(count == 1);
105   callViaPtr2Ansi(incBy, 7);
106   ASSERT(count == 8);
107
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 }
113