1 /** Test type cast with function pointers in array initialization.
2  */
3 
4 #include <testfwk.h>
5 
6 #ifdef __SDCC
7 /* don't spam the output with "pointer types incompatible" warnings */
8 #pragma disable_warning 244
9 #endif
10 
a(void)11 void a(void) {}
12 
13 typedef void (*fp)(void);
14 
testFptrCastOld(void)15 void testFptrCastOld(void)
16 {
17   /* old functionality */
18   fp tab1[2] = {a, 0};
19   ASSERT(tab1[0] == a);
20   fp tab2[2] = {&a, 0};
21   ASSERT(tab2[0] == a);
22   fp tab3[2] = {(fp)a, 0};
23   ASSERT(tab3[0] == a);
24   fp tab4[2] = {(fp)&a, 0};
25   ASSERT(tab4[0] == a);
26   /* sizeof(void *) < sizeof(fp) implies undefined behavior,
27    * i.e. the result of the comparison does not matter */
28   void * tab5[2] = {a, 0};
29   ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab5[0]) == a);
30   void * tab6[2] = {&a, 0};
31   ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab6[0]) == a);
32 }
33 
testFptrCastNew(void)34 void testFptrCastNew(void)
35 {
36   /* new functionality */
37   void * tab1[2] = {(void*)a, 0};
38   ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab1[0]) == a);
39   void * tab2[2] = {(void*)&a, 0};
40   ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab2[0]) == a);
41   char * tab3[2] = {(char*)a, 0};
42   ASSERT(sizeof(char *) < sizeof(fp) || ((fp)tab3[0]) == a);
43   char * tab4[2] = {(char*)&a, 0};
44   ASSERT(sizeof(char *) < sizeof(fp) || ((fp)tab4[0]) == a);
45 }
46