File: fptr_cast_array_init.c

package info (click to toggle)
sdcc 4.5.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,980 kB
  • sloc: ansic: 622,683; cpp: 259,454; makefile: 81,253; sh: 40,203; asm: 19,222; perl: 12,139; yacc: 7,761; awk: 3,378; lisp: 1,677; python: 1,097; lex: 1,028; sed: 76
file content (45 lines) | stat: -rw-r--r-- 1,250 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/** Test type cast with function pointers in array initialization.
 */

#include <testfwk.h>

#ifdef __SDCC
/* don't spam the output with "pointer types incompatible" warnings */
#pragma disable_warning 244
#endif

void a(void) {}

typedef void (*fp)(void);

void testFptrCastOld(void)
{
  /* old functionality */
  fp tab1[2] = {a, 0};
  ASSERT(tab1[0] == a);
  fp tab2[2] = {&a, 0};
  ASSERT(tab2[0] == a);
  fp tab3[2] = {(fp)a, 0};
  ASSERT(tab3[0] == a);
  fp tab4[2] = {(fp)&a, 0};
  ASSERT(tab4[0] == a);
  /* sizeof(void *) < sizeof(fp) implies undefined behavior,
   * i.e. the result of the comparison does not matter */
  void * tab5[2] = {a, 0};
  ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab5[0]) == a);
  void * tab6[2] = {&a, 0};
  ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab6[0]) == a);
}

void testFptrCastNew(void)
{
  /* new functionality */
  void * tab1[2] = {(void*)a, 0};
  ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab1[0]) == a);
  void * tab2[2] = {(void*)&a, 0};
  ASSERT(sizeof(void *) < sizeof(fp) || ((fp)tab2[0]) == a);
  char * tab3[2] = {(char*)a, 0};
  ASSERT(sizeof(char *) < sizeof(fp) || ((fp)tab3[0]) == a);
  char * tab4[2] = {(char*)&a, 0};
  ASSERT(sizeof(char *) < sizeof(fp) || ((fp)tab4[0]) == a);
}