struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / sizeof.c.in
blobf8da7877294b4fccb7023b4a24f5e18787d1af2f
1 /** Test results of sizeof operator for unusual cases
2 size: 1, 8, 16
3 test : 0, 1, 2, 3, 4, 5
4 vol : 0, 1
5 */
7 #include <testfwk.h>
9 #pragma disable_warning 259
10 #pragma disable_warning 88
12 #ifdef __sun__
13 #include <inttypes.h>
14 #else
15 #include <stdint.h>
16 #endif
17 #include <stddef.h>
18 #include <stdbool.h>
20 #define VAR_SIZE ({size})
21 #define TEST ({test})
22 #define TEST_VOLATILE ({vol})
24 // Test is split in several parts to be smaller and for debugging purposes
25 #if TEST == 0
26 #define TEST_BASIC
27 #define TEST_ASSING
28 #define TEST_POINTERS
29 #elif TEST == 1
30 #define TEST_LITERAL
31 #if VAR_SIZE != 1
32 #define DUMMY_TEST
33 #endif
34 #elif TEST == 2
35 #define TEST_VARIABLE
36 #elif TEST == 3
37 #define TEST_LITERAL_BOOL_OP
38 #if VAR_SIZE != 1
39 #define DUMMY_TEST
40 #endif
41 #elif TEST == 4
42 #define TEST_VARIABLE_BOOL_OP
43 #elif TEST == 5
44 #define TEST_VARIABLE_ZERO_RESULT
45 #else
46 #error "Unknown test"
47 #endif
49 #if TEST_VOLATILE == 1
50 #define VAR_QUALIFIER volatile
51 #else
52 #define VAR_QUALIFIER
53 #endif
55 // Variables for the test.
56 typedef struct structBitFields
58 #if VAR_SIZE == 1
59 unsigned int bitField : 1;
60 unsigned int dummy : 7;
61 #elif VAR_SIZE == 8
62 unsigned int bitField : 7;
63 unsigned int dummy : 1;
64 #else
65 unsigned int bitField : 15;
66 unsigned int dummy : 1;
67 #endif
69 structBitFields;
71 VAR_QUALIFIER structBitFields bf;
73 VAR_QUALIFIER bool b, bt;
74 VAR_QUALIFIER uint8_t c, c2, ct;
75 VAR_QUALIFIER int16_t i, i2, it;
77 VAR_QUALIFIER bool * VAR_QUALIFIER pb;
78 VAR_QUALIFIER uint8_t * VAR_QUALIFIER pc;
79 VAR_QUALIFIER int16_t * VAR_QUALIFIER pi;
82 // Main test macro, assigns the sizeof expression to the result and checks if result is correct.
83 #if defined (__SDCC_pdk13) || defined (__SDCC_pdk14) || defined (__SDCC_pdk15)
84 // Keep the assignment out of the assert, same result, but less memory required by assert strings (worse for debugging).
85 #define DO_TEST(expression, result, type) result = sizeof (expression); ASSERT(sizeof(type)==result)
86 #else
87 #define DO_TEST(expression, result, type) ASSERT((result = sizeof (expression), sizeof (type) == result))
88 #endif
90 // Prepare macros depending on variable size to test.
91 #if VAR_SIZE == 1
92 #define VAR_TYPE bool
93 #define TEST_VAR b
94 #define TEST_VAR2 bt
95 #define TEST_PTR_VAR pb
96 #define TEST_VAR_OTHER i
98 #define TEST_OP(x) DO_TEST(x, c2, int) //Test with promotion, int type expected.
99 #elif VAR_SIZE == 8
100 #define VAR_TYPE uint8_t
101 #define TEST_VAR c
102 #define TEST_VAR2 ct
103 #define TEST_PTR_VAR pc
104 #define TEST_VAR_OTHER i
106 #define TEST_OP(x) DO_TEST(x, c2, int) //Test with promotion, int type expected.
107 #elif VAR_SIZE == 16
108 #define VAR_TYPE int16_t
109 #define TEST_VAR i
110 #define TEST_VAR2 it
111 #define TEST_PTR_VAR pi
112 #define TEST_VAR_OTHER c
114 #define TEST_OP(x) DO_TEST(x, i2, int) //Test with promotion, int type expected.
115 #else
116 #error "Unknown test size"
117 #endif
119 #define TEST_OP_NOPROMO(x) DO_TEST(x, c2, VAR_TYPE) // Test without promotion, same type expected.
120 #define TEST_OP_CHAR_PTR(x) DO_TEST(x, c2, VAR_TYPE *) // Test for pointers, pointer of type expected.
121 #define TEST_OP_INT_PTR(x) DO_TEST(x, i2, VAR_TYPE *) // Same using integer for the result.
123 #define TEST_OP_CHAR_PTR_DIFF(x) DO_TEST(x, c2, ptrdiff_t) // Test for pointers, ptrdiff_t type expected.
124 #define TEST_OP_INT_PTR_DIFF(x) DO_TEST(x, i2, ptrdiff_t) // Same using integer for the result.
126 // Check default address space to check the size of the default pointer size correctly
127 #if defined (__SDCC_pdk13) || defined (__SDCC_pdk14) || defined (__SDCC_pdk15) || defined (__SDCC_mcs51) || defined (__SDCC_ds390)
128 #if defined (__SDCC_mcs51)
129 #if defined (__SDCC_MODEL_SMALL) || (__SDCC_MODEL_MEDIUM)
130 #define DEFAULT_ADDR_SPACE __near
131 #elif defined (__SDCC_MODEL_LARGE) || (__SDCC_MODEL_HUGE)
132 #define DEFAULT_ADDR_SPACE __far
133 #endif
134 #elif defined(__SDCC_ds390)
135 #define DEFAULT_ADDR_SPACE __far
136 #else
137 #define DEFAULT_ADDR_SPACE __data // pdk
138 #endif
139 #endif
141 #ifdef DEFAULT_ADDR_SPACE // Tests for backends that need the default address space qualifier
142 #define TEST_OP_CHAR_PTR_DAS(x) DO_TEST(x, c2, VAR_TYPE DEFAULT_ADDR_SPACE *) // Test for pointers, pointer of type default address space expected.
143 #define TEST_OP_INT_PTR_DAS(x) DO_TEST(x, i2, VAR_TYPE DEFAULT_ADDR_SPACE *) // Same using integer for the result
144 #else
145 #define TEST_OP_CHAR_PTR_DAS TEST_OP_CHAR_PTR
146 #define TEST_OP_INT_PTR_DAS TEST_OP_INT_PTR
147 #endif
150 static void
151 testSizeof(void)
153 #ifndef DUMMY_TEST
155 #ifdef TEST_BASIC // Macro self test
156 TEST_OP_NOPROMO (VAR_TYPE);
157 TEST_OP_CHAR_PTR (VAR_TYPE *);
158 TEST_OP_INT_PTR (VAR_TYPE *);
159 #ifdef DEFAULT_ADDR_SPACE
160 TEST_OP_CHAR_PTR_DAS (VAR_TYPE DEFAULT_ADDR_SPACE *);
161 TEST_OP_INT_PTR_DAS (VAR_TYPE DEFAULT_ADDR_SPACE *);
162 #endif
163 #endif
165 #ifdef TEST_ASSING
166 TEST_OP_NOPROMO (TEST_VAR = 1);
167 TEST_OP_NOPROMO (TEST_VAR = 1 + 1);
169 TEST_OP_NOPROMO (TEST_VAR += 1);
170 TEST_OP_NOPROMO (TEST_VAR -= 1);
171 TEST_OP_NOPROMO (TEST_VAR &= 1);
172 TEST_OP_NOPROMO (TEST_VAR |= 1);
173 TEST_OP_NOPROMO (TEST_VAR ^= 1);
174 TEST_OP_NOPROMO (TEST_VAR *= 5);
175 TEST_OP_NOPROMO (TEST_VAR /= 5);
176 TEST_OP_NOPROMO (TEST_VAR /= 2);
178 TEST_OP_NOPROMO (TEST_VAR <<= 2);
179 TEST_OP_NOPROMO (TEST_VAR >>= 2);
181 TEST_OP_NOPROMO ((TEST_VAR_OTHER++, TEST_VAR += 1));
182 #endif
184 #ifdef TEST_POINTERS
186 TEST_OP_CHAR_PTR(VAR_TYPE *);
187 TEST_OP_INT_PTR (VAR_TYPE *);
189 TEST_OP (+(*TEST_PTR_VAR));
190 TEST_OP (-(*TEST_PTR_VAR));
191 TEST_OP (~(*TEST_PTR_VAR));
193 TEST_OP_CHAR_PTR(TEST_PTR_VAR);
194 TEST_OP_INT_PTR (TEST_PTR_VAR);
196 TEST_OP_CHAR_PTR(TEST_PTR_VAR + 1);
197 TEST_OP_INT_PTR (TEST_PTR_VAR + 1);
199 TEST_OP_CHAR_PTR(TEST_PTR_VAR - 1);
200 TEST_OP_INT_PTR (TEST_PTR_VAR - 1);
202 TEST_OP_CHAR_PTR(TEST_PTR_VAR++);
203 TEST_OP_INT_PTR (TEST_PTR_VAR--);
205 TEST_OP_CHAR_PTR(TEST_PTR_VAR += 1);
206 TEST_OP_INT_PTR (TEST_PTR_VAR -= 1);
208 TEST_OP_CHAR_PTR(TEST_PTR_VAR = (void *)1);
209 TEST_OP_INT_PTR (TEST_PTR_VAR = (void *)1);
211 TEST_OP_CHAR_PTR_DAS(&TEST_VAR);
212 TEST_OP_INT_PTR_DAS (&TEST_VAR);
214 TEST_OP_CHAR_PTR_DAS(&TEST_VAR + 1);
215 TEST_OP_INT_PTR_DAS (&TEST_VAR + 1);
216 TEST_OP_CHAR_PTR_DAS(&TEST_VAR - 1);
217 TEST_OP_INT_PTR_DAS (&TEST_VAR - 1);
218 #endif
220 #ifdef TEST_LITERAL
221 TEST_OP ( 8);
222 TEST_OP (+8);
223 TEST_OP (-8);
224 TEST_OP (~0);
226 TEST_OP (1 ? 0 : 0);
227 TEST_OP (0 ? 0 : 0);
228 TEST_OP (1 ? 0 : 1);
229 TEST_OP (0 ? 0 : 1);
230 TEST_OP (1 ? 1 : 0);
231 TEST_OP (0 ? 1 : 0);
232 TEST_OP (1 ? 1 : 1);
233 TEST_OP (0 ? 1 : 1);
235 TEST_OP (8 << 1);
236 TEST_OP (8 >> 1);
237 TEST_OP (8 & 8);
238 TEST_OP (8 | 8);
239 TEST_OP (8 ^ 8);
240 TEST_OP (8 + 8);
241 TEST_OP (8 - 8);
242 TEST_OP (8 * 8);
243 TEST_OP (8 / 8);
244 TEST_OP (8 % 2);
245 #endif
247 #ifdef TEST_LITERAL_BOOL_OP
248 TEST_OP (!8);
249 TEST_OP (!0);
251 TEST_OP (8 > 1);
252 TEST_OP (8 < 1);
253 TEST_OP (8 >= 1);
254 TEST_OP (8 <= 1);
255 TEST_OP (8 == 1);
256 TEST_OP (8 != 1);
258 TEST_OP (1 > 8);
259 TEST_OP (1 < 8);
260 TEST_OP (1 >= 8);
261 TEST_OP (1 <= 8);
262 TEST_OP (1 == 8);
263 TEST_OP (1 != 8);
266 TEST_OP (1 && 1);
267 TEST_OP (1 && 0);
268 TEST_OP (0 && 0);
270 TEST_OP (1 || 1);
271 TEST_OP (1 || 0);
272 TEST_OP (0 || 0);
273 #endif
275 #ifdef TEST_VARIABLE_ZERO_RESULT
276 #if VAR_SIZE == 1 // Do test only once
277 TEST_OP ((1>0) - (1>0));
278 #endif
280 TEST_OP (TEST_VAR - TEST_VAR);
281 TEST_OP (*TEST_PTR_VAR - *TEST_PTR_VAR);
282 TEST_OP (bf.bitField - bf.bitField);
284 #ifdef __SDCC
285 // Do these tests only for SDCC, behavior is undefined by standard.
286 // SDCC implementation defines the result as 0.
287 // Type of that value should follow standard promotion for shift.
288 TEST_OP (TEST_VAR >> 32);
289 TEST_OP (TEST_VAR >> -1);
290 #endif
292 TEST_OP_CHAR_PTR_DIFF(TEST_PTR_VAR - TEST_PTR_VAR);
293 TEST_OP_INT_PTR_DIFF (TEST_PTR_VAR - TEST_PTR_VAR);
294 #endif
296 #ifdef TEST_VARIABLE
297 TEST_OP_NOPROMO (TEST_VAR);
298 TEST_OP (+TEST_VAR);
299 TEST_OP (-TEST_VAR);
300 TEST_OP (~TEST_VAR);
302 TEST_OP_NOPROMO (TEST_VAR++);
303 TEST_OP_NOPROMO (TEST_VAR--);
305 TEST_OP_NOPROMO (++TEST_VAR);
306 TEST_OP_NOPROMO (--TEST_VAR);
308 TEST_OP (TEST_VAR ? 0 : 0);
309 TEST_OP (TEST_VAR ? 0 : 1);
310 TEST_OP (TEST_VAR ? 1 : 0);
311 TEST_OP (TEST_VAR ? 1 : 1);
312 TEST_OP (1 ? TEST_VAR : 1);
313 TEST_OP (1 ? TEST_VAR : 1);
314 TEST_OP (1 ? 1 : TEST_VAR);
315 TEST_OP (1 ? 1 : TEST_VAR);
316 TEST_OP (0 ? TEST_VAR : 1);
317 TEST_OP (0 ? TEST_VAR : 1);
318 TEST_OP (0 ? 1 : TEST_VAR);
319 TEST_OP (0 ? 1 : TEST_VAR);
321 TEST_OP (TEST_VAR << 1);
322 TEST_OP (TEST_VAR << (2 + 1));
323 TEST_OP (TEST_VAR << (2 << 1));
325 TEST_OP (TEST_VAR >> 1);
326 TEST_OP (TEST_VAR >> (2 + 1));
327 TEST_OP (TEST_VAR >> (2 << 1));
329 TEST_OP (TEST_VAR & 8);
330 TEST_OP (TEST_VAR & (2 + 1));
331 TEST_OP (TEST_VAR & (2 << 1));
333 TEST_OP (TEST_VAR | 8);
334 TEST_OP (TEST_VAR | (2 + 1));
335 TEST_OP (TEST_VAR | (2 << 1));
337 TEST_OP (TEST_VAR ^ 8);
338 TEST_OP (TEST_VAR ^ (2 + 1));
339 TEST_OP (TEST_VAR ^ (2 << 1));
341 TEST_OP (TEST_VAR + 8);
342 TEST_OP (TEST_VAR + (2 + 1));
343 TEST_OP (TEST_VAR + (2 << 1));
345 TEST_OP (TEST_VAR - 8);
346 TEST_OP (TEST_VAR - (2 + 1));
347 TEST_OP (TEST_VAR - (2 << 1));
349 TEST_OP (TEST_VAR * 8);
350 TEST_OP (TEST_VAR * (2 + 1));
351 TEST_OP (TEST_VAR * (2 << 1));
353 TEST_OP (TEST_VAR - TEST_VAR2);
354 #if VAR_SIZE == 8
355 TEST_OP ((TEST_VAR >> 1) | (TEST_VAR << 7));
356 #endif
357 #endif
359 #ifdef TEST_VARIABLE_BOOL_OP
360 TEST_OP (!TEST_VAR);
362 TEST_OP (TEST_VAR > 0);
363 TEST_OP (TEST_VAR < 0);
364 TEST_OP (TEST_VAR >= 1);
365 TEST_OP (TEST_VAR <= 1);
366 TEST_OP (TEST_VAR == 1);
367 TEST_OP (TEST_VAR != 1);
369 TEST_OP (1 > c);
370 TEST_OP (1 < c);
371 TEST_OP (1 >= c);
372 TEST_OP (1 <= c);
373 TEST_OP (1 == c);
374 TEST_OP (1 != c);
376 TEST_OP (c && c);
377 TEST_OP (c && 0);
378 TEST_OP (0 && c);
379 TEST_OP (c || c);
380 TEST_OP (c || 0);
381 TEST_OP (0 || c);
382 #endif
384 #endif //DUMMY_TEST