struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bitwise.c.in
blob411dad02f4375c40d3a746c07c617b8296b37c62
1 /** Test the bitwise operators.
3 type: char, short, long
4 attr: volatile,
5 storage: static,
6 */
7 #include <testfwk.h>
9 void
10 testTwoOpBitwise(void)
12 {storage} {attr} {type} left, right;
14 left = ({type})0x3df7;
15 right = ({type})0xc1ec;
17 ASSERT(({type})(left & right) == ({type})0x1E4);
18 ASSERT(({type})(right & left) == ({type})0x1E4);
19 ASSERT(({type})(left & 0xc1ec) == ({type})0x1E4);
20 ASSERT(({type})(0x3df7 & right) == ({type})0x1E4);
22 #ifndef __SDCC_pdk14 // Lack of memory
23 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
24 ASSERT(({type})(left | right) == ({type})0xFDFF);
25 ASSERT(({type})(right | left) == ({type})0xFDFF);
26 ASSERT(({type})(left | 0xc1ec) == ({type})0xFDFF);
27 ASSERT(({type})(0x3df7 | right) == ({type})0xFDFF);
29 ASSERT(({type})(left ^ right) == ({type})0xFC1B);
30 ASSERT(({type})(right ^ left) == ({type})0xFC1B);
31 ASSERT(({type})(left ^ 0xc1ec) == ({type})0xFC1B);
32 ASSERT(({type})(0x3df7 ^ right) == ({type})0xFC1B);
34 #if defined (__alpha__) || defined (__x86_64__) || defined(__sparc64__) || defined(__PPC64__) || defined (__aarch64__)
35 /* long is 64 bits on 64 bit machines */
36 ASSERT(({type})(~left) == ({type})0xFFFFFFFFFFFFC208);
37 #else
38 ASSERT(({type})(~left) == ({type})0xFFFFC208);
39 #endif
40 #endif
41 #endif
44 void
45 testAnd(void)
47 char res;
48 {attr} int a;
50 /* always false if right literal == 0 */
51 #if 0
52 // fails on pic16
53 if (a & 0)
54 res = 1;
55 else
56 res = 0;
57 ASSERT(res = 0);
59 a = 0x1234;
61 if (a & 0)
62 res = 1;
63 else
64 res = 0;
65 ASSERT(res = 0);
66 #endif
69 * result: if, left: var, right: literal
71 a = 0x1234;
73 if (a & 0x4321)
74 res = 1;
75 else
76 res = 0;
77 ASSERT(res == 1);
79 if (a & 0x4321)
80 /* nothing for true */
82 else
83 res = 0;
84 ASSERT(res == 1);
86 if (!(a & 0x4321))
87 res = 1;
88 else
89 res = 0;
90 ASSERT(res == 0);
92 if (!(a & 0x4321))
93 /* nothing for true */
95 else
96 res = 0;
97 ASSERT(res == 0);
99 /* bitmask literal */
100 a = 0xffff;
102 if (a & 0x1004)
103 res = 1;
104 else
105 res = 0;
106 ASSERT(res == 1);
108 if (!(a & 0x1004))
109 res = 1;
110 else
111 res = 0;
112 ASSERT(res == 0);
114 a = 0x0000;
116 if (a & 0x1004)
117 res = 1;
118 else
119 res = 0;
120 ASSERT(res == 0);
122 if (!(a & 0x1004))
123 res = 1;
124 else
125 res = 0;
126 ASSERT(res == 1);
128 a = 0x00ff;
130 if (a & 0x1004)
131 res = 1;
132 else
133 res = 0;
134 ASSERT(res == 1);
136 if (!(a & 0x1004))
137 res = 1;
138 else
139 res = 0;
140 ASSERT(res == 0);
142 a = 0xff00;
144 if (a & 0x1004)
145 res = 1;
146 else
147 res = 0;
148 ASSERT(res == 1);
150 if (!(a & 0x1004))
151 res = 1;
152 else
153 res = 0;
154 ASSERT(res == 0);
156 /* literal with zero bytes */
157 a = 0x1234;
159 if (a & 0x4300)
160 res = 1;
161 else
162 res = 0;
163 ASSERT(res == 1);
164 #ifndef __SDCC_pdk14 // Lack of memory
165 if (a & 0x0012)
166 res = 1;
167 else
168 res = 0;
169 ASSERT(res == 1);
171 if (!(a & 0x4300))
172 res = 1;
173 else
174 res = 0;
175 ASSERT(res == 0);
177 if (!(a & 0x0012))
178 res = 1;
179 else
180 res = 0;
181 ASSERT(res == 0);
184 * result: bit, left: var, right: literal
186 #endif
189 void
190 testOr(void)
192 char res;
193 {attr} int a = 0x1234;
196 * result: if, left: var, right: literal
198 res = 1;
199 if (a | 0x4321)
200 /* nothing for true */
202 else
203 res = 0;
204 ASSERT(res == 1);
206 if (!(a | 0x4321))
207 /* nothing for true */
209 else
210 res = 0;
211 ASSERT(res == 0);
212 #ifndef __SDCC_pdk14 // Lack of memory
213 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
214 if (a | 0x4321)
215 res = 1;
216 else
217 res = 0;
218 ASSERT(res == 1);
220 if (!(a | 0x4321))
221 res = 1;
222 else
223 res = 0;
224 ASSERT(res == 0);
226 /* or with zero: result is left */
227 res = 1;
229 if (a | 0)
230 /* nothing for true */
232 else
233 res = 0;
234 ASSERT(res == 1);
236 res = 1;
238 if (!(a | 0))
239 /* nothing for true */
241 else
242 res = 0;
243 ASSERT(res == 0);
245 if (a | 0)
246 res = 1;
247 else
248 res = 0;
249 ASSERT(res == 1);
251 if (!(a | 0))
252 res = 1;
253 else
254 res = 0;
255 ASSERT(res == 0);
256 #endif
257 #endif
260 void
261 testXor(void)
263 char res;
264 {attr} int a = 0x1234;
267 * result: if, left: var, right: literal
269 if (a ^ 0x4321)
270 res = 1;
271 else
272 res = 0;
273 ASSERT(res == 1);
275 if (a ^ 0x4321)
276 /* nothing for true */
278 else
279 res = 0;
280 ASSERT(res == 1);
281 #ifndef __SDCC_pdk14 // Lack of memory
282 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
283 if (!(a ^ 0x4321))
284 res = 1;
285 else
286 res = 0;
287 ASSERT(res == 0);
289 if (!(a ^ 0x4321))
290 /* nothing for true */
292 else
293 res = 0;
294 ASSERT(res == 0);
296 /* literal with 0xff bytes */
297 if (a ^ 0xff04)
298 res = 1;
299 else
300 res = 0;
301 ASSERT(res == 1);
303 if (!(a ^ 0xff04))
304 res = 1;
305 else
306 res = 0;
307 ASSERT(res == 0);
309 /* literal with zero bytes */
310 if (a ^ 0x0004)
311 res = 1;
312 else
313 res = 0;
314 ASSERT(res == 1);
316 if (!(a ^ 0x0004))
317 res = 1;
318 else
319 res = 0;
320 ASSERT(res == 0);
321 #endif
322 #endif
325 static void
326 testBug_1777758(void)
328 char ep = -1;
330 ep &= ~0x80;
332 ASSERT(ep == 127);