a3049bcf3b00809e7624ce706636c8d11908fea2
[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 struct z {
42   char     c;
43   struct y s;
44 };
45
46 struct x STORAGE teststruct[5] = {
47   { 10, {  1, 2, 3, 4, 5} },
48   { 20, { 11 } },
49   { 30, {  6, 7, 8} }
50 };
51
52 struct y STORAGE incompletestruct = {
53   10, {1, 2, 3, 4, 5}
54 };
55
56 struct z STORAGE nestedstruct = {
57   16,
58   {20, {6, 7, 8} }
59 };
60
61 void
62 testZeropad(void)
63 {
64   ASSERT(array[2] == 'c');
65   ASSERT(array[4] == 0);
66
67   ASSERT(g[1].a == 'y');
68   ASSERT(g[1].b == 0);
69   ASSERT(g[2].a == 'z');
70   ASSERT(g[2].b == 3);
71
72   ASSERT(teststruct[0].b[1] ==  2);
73   ASSERT(teststruct[0].b[5] ==  0);
74   ASSERT(teststruct[1].b[0] == 11);
75   ASSERT(teststruct[4].b[9] ==  0);
76
77   ASSERT(sizeof(teststruct[2].a) ==  2);
78   ASSERT(sizeof(teststruct[1].b) == 10);
79   ASSERT(sizeof(teststruct[1])   == 12);
80   ASSERT(sizeof(teststruct)      == 60);
81
82   ASSERT(incompletestruct.a    == 10);
83   ASSERT(incompletestruct.b[0] ==  1);
84   ASSERT(incompletestruct.b[4] ==  5);
85
86   ASSERT(sizeof(incompletestruct) == sizeof(struct y));
87   ASSERT(sizeof(incompletestruct) == offsetof(struct y, b));
88   ASSERT(sizeof(incompletestruct) == offsetof(struct x, b));
89
90   ASSERT(nestedstruct.c      == 16);
91   ASSERT(nestedstruct.s.a    == 20);
92   ASSERT(nestedstruct.s.b[2] ==  8);
93 }