* src/hc08/gen.c (genPointerGetSetOfs): disabled optimization if
[fw/sdcc] / support / regression / tests / zeropad.c
1 /** Zeropad tests.
2
3     storage: idata, xdata, code,
4 */
5 #ifndef STORAGE
6 #define STORAGE {storage}
7 #endif
8
9 #include <testfwk.h>
10
11 typedef unsigned int size_t;
12 #define offsetof(s,m)   (size_t)&(((s *)0)->m)
13
14 #if defined(PORT_HOST) || defined(SDCC_z80) || defined(SDCC_gbz80)
15 # define idata
16 # define xdata
17 # define code
18 #endif
19
20 char array[5] = {'a', 'b', 'c'};
21
22 struct w {
23   char a;
24   int  b;
25 } STORAGE g[3] = {
26   {'x', 1},
27   {'y'},
28   {'z', 3}
29 };
30
31 struct x {
32   short a;
33   char  b[10];
34 };
35
36 struct y {
37   short a;
38   char  b[];
39 };
40
41 /* I think section 6.7.2.1 paragraph 2 of ISO/IEC 9899:1999 prohibits */
42 /* nesting a structure ending in a flexible array inside another      */
43 /* struct/union. In any case, my gcc (3.2.2) chokes on this. -- EEP   */
44 #ifdef NESTED_FLEX_ARRAY
45 struct z {
46   char     c;
47   struct y s;
48 };
49 #endif
50
51 struct x STORAGE teststruct[5] = {
52   { 10, {  1, 2, 3, 4, 5} },
53   { 20, { 11 } },
54   { 30, {  6, 7, 8} }
55 };
56
57 struct y STORAGE incompletestruct = {
58   10, {1, 2, 3, 4, 5}
59 };
60
61 #ifdef NESTED_FLEX_ARRAY
62 struct z STORAGE nestedstruct = {
63   16,
64   {20, {6, 7, 8} }
65 };
66 #endif
67
68 void
69 testZeropad(void)
70 {
71   ASSERT(array[2] == 'c');
72   ASSERT(array[4] == 0);
73
74   ASSERT(g[1].a == 'y');
75   ASSERT(g[1].b == 0);
76   ASSERT(g[2].a == 'z');
77   ASSERT(g[2].b == 3);
78
79   ASSERT(teststruct[0].b[1] ==  2);
80   ASSERT(teststruct[0].b[5] ==  0);
81   ASSERT(teststruct[1].b[0] == 11);
82   ASSERT(teststruct[4].b[9] ==  0);
83
84   ASSERT(sizeof(teststruct[2].a) ==  2);
85   ASSERT(sizeof(teststruct[1].b) == 10);
86   ASSERT(sizeof(teststruct[1])   == 12);
87   ASSERT(sizeof(teststruct)      == 60);
88
89   ASSERT(incompletestruct.a    == 10);
90   ASSERT(incompletestruct.b[0] ==  1);
91   ASSERT(incompletestruct.b[4] ==  5);
92
93   ASSERT(sizeof(incompletestruct) == sizeof(struct y));
94   ASSERT(sizeof(incompletestruct) == offsetof(struct y, b));
95   ASSERT(sizeof(incompletestruct) == offsetof(struct x, b));
96
97 #ifdef NESTED_FLEX_ARRAY
98   ASSERT(nestedstruct.c      == 16);
99   ASSERT(nestedstruct.s.a    == 20);
100   ASSERT(nestedstruct.s.b[2] ==  8);
101 #endif
102 }