* src/SDCC.lex (doPragma): added pragma disable_warning <nnn>
[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 const char *string1 = "\x00\x01";
30 const char string2[] = "\x00\x01";
31
32 #ifndef PORT_HOST
33 #pragma disable_warning 147 //no warning about excess initializers (W_EXCESS_INITIALIZERS)
34 //array will be truncated but warning will be suppressed
35 //if truncation is incorrect, other ASSERTs will fail with high probability
36 char STORAGE trunc[2] = {'a', 'b', 'c'};
37 #endif
38
39 char STORAGE array[5] = {'a', 'b', 'c'};
40
41 #if TEST_G
42   struct w {
43     char a;
44     int  b;
45   } STORAGE g[3] = {
46     {'x', 1},
47     {'y'},
48     {'z', 3}
49   };
50 #endif
51
52 struct x {
53   short a;
54   char  b[10];
55 };
56
57 struct x STORAGE teststruct[5] = {
58   { 10, {  1, 2, 3, 4, 5} },
59   { 20, { 11 } },
60   { 30, {  6, 7, 8} }
61 };
62
63 #if FLEXARRAY
64   struct y {
65     short a;
66     char  b[];
67   };
68
69 struct y STORAGE incompletestruct = {
70   10, {1, 2, 3, 4, 5}
71 };
72 #endif
73
74 void
75 testZeropad(void)
76 {
77   ASSERT(string1[1] == '\x01');
78   ASSERT(string2[1] == '\x01');
79
80   ASSERT(array[2] == 'c');
81   ASSERT(array[4] == 0);
82
83 #if TEST_G
84   ASSERT(g[1].a == 'y');
85   ASSERT(g[1].b == 0);
86   ASSERT(g[2].a == 'z');
87   ASSERT(g[2].b == 3);
88 #endif
89
90   ASSERT(teststruct[0].b[1] ==  2);
91   ASSERT(teststruct[0].b[5] ==  0);
92   ASSERT(teststruct[1].b[0] == 11);
93   ASSERT(teststruct[4].b[9] ==  0);
94
95   ASSERT(sizeof(teststruct[2].a) ==  2);
96   ASSERT(sizeof(teststruct[1].b) == 10);
97   ASSERT(sizeof(teststruct[1])   == 12);
98   ASSERT(sizeof(teststruct)      == 60);
99
100 #if FLEXARRAY
101   ASSERT(incompletestruct.a    == 10);
102   ASSERT(incompletestruct.b[0] ==  1);
103   ASSERT(incompletestruct.b[4] ==  5);
104
105   ASSERT(sizeof(incompletestruct) == sizeof(struct y));
106   ASSERT(sizeof(incompletestruct) == offsetof(struct y, b));
107   ASSERT(sizeof(incompletestruct) == offsetof(struct x, b));
108 #endif
109 }