struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / structflexarray.c
blob6cb3826b8cb0ebac91aba6159264a20109f5274a
1 /* Check basic behaviour of "flexible array members"
2 */
3 #include <testfwk.h>
5 #if defined(__SUNPRO_C) || defined(__GNUC__)
6 #pragma pack(1)
7 #endif
9 #if !defined(__SUNPRO_C) && (!defined(__GNUC__) || (defined(__GNUC__) && __GNUC__ >= 3))
10 /* flexible array members not supported by gcc < 3 compiler
11 and seems not to be supported on SunPro C compiler */
12 struct str1
14 char c;
15 short i[];
18 struct str1 s11 = { 1, {2, 3} };
19 struct str1 s12 = { 4, {5, 6, 7} }; /* different size */
20 #endif
22 static void
23 testFlexibleArray1(void)
25 #if !defined(__SUNPRO_C) && (!defined(__GNUC__) || (defined(__GNUC__) && __GNUC__ >= 3))
26 /* flexible array members not supported by gcc < 3 compiler
27 and seems not to be supported on SunPro C compiler */
28 /* test sizeof */
29 ASSERT(sizeof(s11) == 1);
30 /* test allocation size */
31 #if !defined(PORT_HOST) && !defined(__SDCC_pic14) // Linker changes data order
32 ASSERT((char *) &s12 - (char *) &s11 == 1 + 4);
33 #endif
34 #endif
38 /* test initialisation with string */
40 #if !defined(__SUNPRO_C) && (!defined(__GNUC__) || (defined(__GNUC__) && __GNUC__ >= 3))
41 /* flexible array members not supported by gcc < 3 compiler
42 and seems not to be supported on SunPro C compiler */
43 struct str2
45 short s;
46 char str2[];
49 struct str2 s21 = { 1, "sdcc" };
50 struct str2 s22 = { 2, "sdcc is great" }; /* different size */
51 #endif
53 static void
54 testFlexibleArray2(void)
56 #if !defined(__SUNPRO_C) && (!defined(__GNUC__) || (defined(__GNUC__) && __GNUC__ >= 3))
57 /* flexible array members not supported by gcc < 3 compiler
58 and seems not to be supported on SunPro C compiler */
59 /* test sizeof */
60 ASSERT(sizeof(s21) == 2);
61 /* test allocation size */
62 #if !defined(PORT_HOST) && !defined(__SDCC_pic14) // Linker changes data order
63 ASSERT((char *) &s22 - (char *) &s21 == 2 + 5);
64 #endif
65 #endif