1 /** Test results of sizeof operator for unusual cases
3 test : 0, 1, 2, 3, 4, 5
9 #pragma disable_warning 259
10 #pragma disable_warning 88
20 #define VAR_SIZE ({size})
22 #define TEST_VOLATILE ({vol})
24 // Test is split in several parts to be smaller and for debugging purposes
37 #define TEST_LITERAL_BOOL_OP
42 #define TEST_VARIABLE_BOOL_OP
44 #define TEST_VARIABLE_ZERO_RESULT
49 #if TEST_VOLATILE == 1
50 #define VAR_QUALIFIER volatile
55 // Variables for the test.
56 typedef struct structBitFields
59 unsigned int bitField
: 1;
60 unsigned int dummy
: 7;
62 unsigned int bitField
: 7;
63 unsigned int dummy
: 1;
65 unsigned int bitField
: 15;
66 unsigned int dummy
: 1;
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)
87 #define DO_TEST(expression, result, type) ASSERT((result = sizeof (expression), sizeof (type) == result))
90 // Prepare macros depending on variable size to test.
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.
100 #define VAR_TYPE uint8_t
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.
108 #define VAR_TYPE int16_t
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.
116 #error "Unknown test size"
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
134 #elif defined(__SDCC_ds390)
135 #define DEFAULT_ADDR_SPACE __far
137 #define DEFAULT_ADDR_SPACE __data // pdk
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
145 #define TEST_OP_CHAR_PTR_DAS TEST_OP_CHAR_PTR
146 #define TEST_OP_INT_PTR_DAS TEST_OP_INT_PTR
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
*);
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));
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);
247 #ifdef TEST_LITERAL_BOOL_OP
275 #ifdef TEST_VARIABLE_ZERO_RESULT
276 #if VAR_SIZE == 1 // Do test only once
277 TEST_OP ((1>0) - (1>0));
280 TEST_OP (TEST_VAR
- TEST_VAR
);
281 TEST_OP (*TEST_PTR_VAR
- *TEST_PTR_VAR
);
282 TEST_OP (bf
.bitField
- bf
.bitField
);
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);
292 TEST_OP_CHAR_PTR_DIFF(TEST_PTR_VAR
- TEST_PTR_VAR
);
293 TEST_OP_INT_PTR_DIFF (TEST_PTR_VAR
- TEST_PTR_VAR
);
297 TEST_OP_NOPROMO (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
);
355 TEST_OP ((TEST_VAR
>> 1) | (TEST_VAR
<< 7));
359 #ifdef TEST_VARIABLE_BOOL_OP
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);