f791859d106c7d56ce64ff639cef397779f35584
[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 #if defined __GNUC__
10   #define FLEXARRAY (__GNUC__ >= 3)
11   //since g fails on GCC 2.95.4 on alpha and I don't know how to detect alpha...
12   #define TEST_G    (__GNUC__ >= 3)
13 #else
14   #define FLEXARRAY 1
15   #define TEST_G    1
16 #endif
17
18 #include <testfwk.h>
19
20 typedef unsigned int size_t;
21 #define offsetof(s,m)   (size_t)&(((s *)0)->m)
22
23 #if defined(PORT_HOST) || defined(SDCC_z80) || defined(SDCC_gbz80)
24 # define idata
25 # define xdata
26 # define code
27 #endif
28
29 char STORAGE array[5] = {'a', 'b', 'c'};
30
31 #if TEST_G
32   struct w {
33     char a;
34     int  b;
35   } STORAGE g[3] = {
36     {'x', 1},
37     {'y'},
38     {'z', 3}
39   };
40 #endif
41
42 struct x {
43   short a;
44   char  b[10];
45 };
46
47 struct x STORAGE teststruct[5] = {
48   { 10, {  1, 2, 3, 4, 5} },
49   { 20, { 11 } },
50   { 30, {  6, 7, 8} }
51 };
52
53 #if FLEXARRAY
54   struct y {
55     short a;
56     char  b[];
57   };
58
59 struct y STORAGE incompletestruct = {
60   10, {1, 2, 3, 4, 5}
61 };
62 #endif
63
64 void
65 testZeropad(void)
66 {
67   ASSERT(array[2] == 'c');
68   ASSERT(array[4] == 0);
69
70 #if TEST_G
71   ASSERT(g[1].a == 'y');
72   ASSERT(g[1].b == 0);
73   ASSERT(g[2].a == 'z');
74   ASSERT(g[2].b == 3);
75 #endif
76
77   ASSERT(teststruct[0].b[1] ==  2);
78   ASSERT(teststruct[0].b[5] ==  0);
79   ASSERT(teststruct[1].b[0] == 11);
80   ASSERT(teststruct[4].b[9] ==  0);
81
82   ASSERT(sizeof(teststruct[2].a) ==  2);
83   ASSERT(sizeof(teststruct[1].b) == 10);
84   ASSERT(sizeof(teststruct[1])   == 12);
85   ASSERT(sizeof(teststruct)      == 60);
86
87 #if FLEXARRAY
88   ASSERT(incompletestruct.a    == 10);
89   ASSERT(incompletestruct.b[0] ==  1);
90   ASSERT(incompletestruct.b[4] ==  5);
91
92   ASSERT(sizeof(incompletestruct) == sizeof(struct y));
93   ASSERT(sizeof(incompletestruct) == offsetof(struct y, b));
94   ASSERT(sizeof(incompletestruct) == offsetof(struct x, b));
95 #endif
96 }