struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / const.c
blob90470f1035e1dab8623794b04d6dad09248f7527
1 /* Tests usage of const qualifier.
2 */
3 #include <testfwk.h>
5 char k; /* char */
6 const char const_char = 123; /* constant char */
7 const char * const_char_ptr; /* pointer to constant char */
8 char * const char_ptr_const = &k; /* constant pointer to char */
9 const char * const const_char_ptr_const = &const_char;
10 /* constant pointer to constant char */
11 char char_array[3]; /* array of char */
12 const char const_char_array[] = {1,2,3}; /* array of constant char */
13 const char ** const_char_ptr_ptr; /* pointer to pointer to constant char */
15 char
16 ident(char x)
18 return x;
21 void
22 testConst(void)
24 /* Since const_char_ptr is in itself not constant, we can change it */
25 const_char_ptr = const_char_array;
26 const_char_ptr++;
27 ASSERT(*const_char_ptr == 2);
29 /* Check for bug #621531 */
30 const_char_ptr = const_char_array;
31 ASSERT(const_char_ptr[0] == 1);
32 const_char_ptr++;
34 /* Since char_ptr_const is constant, we cannot change it. However, */
35 /* we can change the object that it points to. */
36 *char_ptr_const = 5;
37 ASSERT(ident(*char_ptr_const)==5);
38 (*char_ptr_const)++;
39 ASSERT(ident(*char_ptr_const)==6);
41 /* We can't modify const_char_ptr_const or the object that it points */
42 /* to. Unfortunately, we can't test that compiler enforces these */
43 /* restriction, so just verify its initialization. */
44 ASSERT(*const_char_ptr_const == const_char);
46 /* We can change const_char_ptr_ptr or the object that it points to. */
47 const_char_ptr = const_char_array;
48 const_char_ptr_ptr = &const_char_ptr;
49 ASSERT(ident(**const_char_ptr_ptr)==1);
50 (*const_char_ptr_ptr)++;
51 ASSERT(ident(**const_char_ptr_ptr)==2);