summaryrefslogtreecommitdiff
path: root/support/regression/tests/fptr_cast_array_init.c
blob: b5e973674c66f20dd8afb0d1a75771fb565b66c4 (plain)
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);
}