]> git.sur5r.net Git - cc65/blob - test/val/ptrfunc.c
All regression-tests subdirectories are cleaned before the first test starts, so...
[cc65] / test / val / ptrfunc.c
1 /*
2   !!DESCRIPTION!!
3   !!ORIGIN!!      SDCC regression tests
4   !!LICENCE!!     GPL, read COPYING.GPL
5 */
6
7 #include <stdio.h>
8 #include <limits.h>
9
10 unsigned char success=0;
11 unsigned char failures=0;
12 unsigned char dummy=0;
13
14 #ifdef SUPPORT_BIT_TYPES
15 bit bit0 = 0;
16 #endif
17 unsigned int uint0 = 0;
18 unsigned int uint1 = 0;
19
20 /*
21  * BUG: if these aren't volatile, an overzealous optimizer or somthing
22  * wreaks havoc with the simple tests like "if(uchar != 3)failures++"
23  */
24
25 #if 0
26 unsigned char uchar0 = 0;
27 unsigned char uchar1 = 0;
28 unsigned char uchar2 = 0;
29 #else
30 volatile unsigned char uchar0 = 0;
31 volatile unsigned char uchar1 = 0;
32 volatile unsigned char uchar2 = 0;
33 #endif
34
35 #ifdef NO_IMPLICIT_FUNCPTR_CONV
36 void (*pfunc)(void);
37 void (*p1func)(void);
38 unsigned char (*pcfunc)(void);
39 #else
40 void (*pfunc)();
41 void (*p1func)();
42 unsigned char (*pcfunc)();
43 #endif
44
45 void done()
46 {
47   dummy++;
48 }
49
50 void call0(void)
51 {
52         uchar0++;
53 }
54
55 void call1(void)
56 {
57         uchar1++;
58 }
59
60 unsigned char call2(void)
61 {
62         return uchar0 + 9;
63 }
64
65 void docall0(void)
66 {
67         pfunc = call0;
68         (pfunc)();
69         if(uchar0 != 1)
70                 failures++;
71 }
72
73 void docall1()
74 {
75         unsigned char i;
76         for(i = 0; i < 3; i++) {
77                 (*p1func)();
78         }
79 }
80
81 #ifdef NO_IMPLICIT_FUNCPTR_CONV
82 void docall2( void(*pf)(void) )
83 #else
84 void docall2( void(*pf)() )
85 #endif
86 {
87         unsigned char i;
88         for(i = 0; i < 2; i++) {
89                 pf();
90         }
91 }
92
93 int  main(void)
94 {
95     docall0();
96
97         p1func = call1;
98         docall1();
99         if(uchar1 != 3)
100                 failures++;
101         if(uchar0 != 1)
102                 failures++;
103
104         p1func = call0;
105         docall1();
106         if(uchar1 != 3)
107                 failures++;
108         if(uchar0 != 4)
109                 failures++;
110
111         docall2(call0);
112         if(uchar1 != 3)
113                 failures++;
114         if(uchar0 != 6)
115                 failures++;
116
117         docall2(call1);
118         if(uchar1 != 5)
119                 failures++;
120         if(uchar0 != 6)
121                 failures++;
122
123         pcfunc = call2;
124         uchar2 = (*pcfunc)();
125         if(uchar2 != 15)
126                 failures++;
127
128         uchar2 += (pcfunc)();
129         uchar2 += pcfunc();
130
131         success = failures;
132         done();
133         printf("failures: %d\n",failures);
134
135         return failures;
136 }