Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / tests / funptrs.c
1 /** Function pointer tests.
2
3     type: BOOL, char, int, long
4  */
5 #include <testfwk.h>
6 #include <stdbool.h>
7
8 #ifndef BOOL
9 #define BOOL    bool
10 #endif
11
12 #define TYPE_{type}
13
14 /* Must use a typedef as there is no way of adding the code modifier
15    on the z80.
16 */
17 typedef void (*NOARGFUNPTR)(void);
18 typedef void (*ONEARGFUNPTR)({type}) REENTRANT;
19 typedef long int (*FOURARGFUNPTR)(char, char, long int, long int) REENTRANT;
20 typedef {type} (*TYPEFUNPTR)({type}, {type}) REENTRANT;
21
22 int count;
23 FOURARGFUNPTR fafp;
24 TYPEFUNPTR tfp;
25
26 void
27 incCount(void)
28 {
29   count++;
30 }
31
32 void
33 incBy({type} a) REENTRANT
34 {
35   count += a;
36 }
37
38 long int f6(char a, char b, long int c, long int d) REENTRANT
39 {
40   switch (a)
41     {
42     case 0: return a;
43     case 1: return b;
44     case 2: return c;
45     case 3: return d;
46     }
47   return 0;
48 }
49
50
51 void
52 callViaPtr(NOARGFUNPTR fptr)
53 {
54   (*fptr)();
55 }
56
57 void
58 callViaPtr2(ONEARGFUNPTR fptr, {type} arg)
59 {
60   (*fptr)(arg);
61 }
62
63 void
64 callViaPtr3(void (*fptr)(void))
65 {
66   (*fptr)();
67 }
68
69 void
70 callViaPtrAnsi(NOARGFUNPTR fptr)
71 {
72   fptr();
73 }
74
75 void
76 callViaPtr2Ansi(ONEARGFUNPTR fptr, {type} arg)
77 {
78   fptr(arg);
79 }
80
81 void
82 callViaPtr3Ansi(void (*fptr)(void))
83 {
84   fptr();
85 }
86
87 {type} f_ret({type} arg1, {type} arg2) REENTRANT
88 {
89   {type} local;
90   local = !arg1;
91   return (local & arg2);
92 }
93
94
95
96 void
97 testFunPtr(void)
98 {
99   fafp = f6;
100
101   ASSERT(count == 0);
102   callViaPtr(incCount);
103   ASSERT(count == 1);
104   callViaPtr2(incBy, 7);
105   ASSERT(count == 8 || count == 2);
106
107   ASSERT((*fafp)(0, 0x55, 0x12345678, 0x9abcdef0) == 0);
108   ASSERT((*fafp)(1, 0x55, 0x12345678, 0x9abcdef0) == 0x55);
109   ASSERT((*fafp)(2, 0x55, 0x12345678, 0x9abcdef0) == 0x12345678);
110   ASSERT((*fafp)(3, 0x55, 0x12345678, 0x9abcdef0) == 0x9abcdef0);
111 }
112
113 void
114 testFunPtrAnsi(void)
115 {
116   fafp = f6;
117
118   count = 0;
119   callViaPtrAnsi(incCount);
120   ASSERT(count == 1);
121   callViaPtr2Ansi(incBy, 7);
122   ASSERT(count == 8 || count == 2);
123
124   ASSERT(fafp(0, 0x55, 0x12345678, 0x9abcdef0) == 0);
125   ASSERT(fafp(1, 0x55, 0x12345678, 0x9abcdef0) == 0x55);
126   ASSERT(fafp(2, 0x55, 0x12345678, 0x9abcdef0) == 0x12345678);
127   ASSERT(fafp(3, 0x55, 0x12345678, 0x9abcdef0) == 0x9abcdef0);
128 }
129
130 void
131 testFunPtrReturn(void)
132 {
133   tfp = f_ret;
134
135   ASSERT(tfp(0, 0) == 0);
136   ASSERT(tfp(0, 1) == 1);
137   ASSERT(tfp(1, 0) == 0);
138   ASSERT(tfp(1, 1) == 0);
139 }
140