Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / literalop.c.in
blob79b47b9c2517dfaf59eafedd2790744f9e68dcad
1 /* Test operandOperation() in SDCCicode.c
3 type: char, short, LONG
4 */
5 #include <testfwk.h>
6 #include <limits.h>
8 #if INT_MAX >= 2147483647
9 # define LONG int
10 #else
11 # define LONG long
12 #endif
14 typedef signed {type} stype;
15 typedef unsigned {type} utype;
17 volatile char is8 = 8;
19 #if !defined(__SDCC_pdk14) // Lack of memory
20 signed char sc;
21 signed short ss;
22 signed LONG sl;
23 unsigned char uc;
24 unsigned short us;
25 unsigned LONG ul;
26 volatile signed char vsc;
27 volatile signed short vss;
28 volatile signed LONG vsl;
29 volatile unsigned char vuc;
30 volatile unsigned short vus;
31 volatile unsigned LONG vul;
32 stype s;
33 volatile stype vs;
34 utype u;
35 volatile utype vu;
37 unsigned LONG t1, t2;
38 #endif
40 int
41 mulWrapper (int a, int b)
43 return a * b;
46 void
47 testOpOp (void)
49 #if !defined( __SDCC_pdk14) && !defined( __SDCC_pdk15) // Lack of memory
50 /* mul signedness: usualBinaryConversions() */
51 vsc = 0x7f;
52 vuc = 0xfe;
54 sc = vsc * vsc;
55 ASSERT (sc == 1);
56 sc = vuc * vsc;
57 ASSERT (sc == 2);
58 sc = vuc * vuc;
59 ASSERT (sc == 4);
61 ss = vsc * vsc;
62 ASSERT (ss == 0x3f01);
63 ss = vuc * vsc;
64 ASSERT (ss == 0x7e02);
65 ss = vuc * vuc;
66 ASSERT (ss == (short) 0xfc04);
67 #ifdef __SDCC
68 /* after promotion the result of the multiplication is 'signed int', which overflows! */
69 ASSERT(vuc * vuc < 1);
70 #endif
72 /* mul ast: valMult() */
73 ASSERT ((stype) -3 * (stype) -1 == (stype) 3);
74 ASSERT ((stype) -3 * (stype) 1 == (stype) -3);
75 ASSERT ((stype) 3 * (stype) -1 == (stype) -3);
77 ASSERT ((stype) 1 * (utype) 0xfffffff7 == (utype) 0xfffffff7);
79 ASSERT ((unsigned char ) 0xfffffff8 * (unsigned char ) 0xfffffff7 == 0xef48);
80 ASSERT (mulWrapper ((unsigned short) 0xfffffff8, (unsigned short) 0xfffffff7) == (sizeof(int) == 2 ? 0x0048 : (unsigned int)0xffef0048));
81 ASSERT ((unsigned LONG ) 0xfffffff8 * (unsigned LONG ) 0xfffffff7 == 0x0048);
83 ASSERT ((stype ) 0xfffffff8 * (stype ) 0xfffffff7 == 72);
85 ASSERT ((signed char ) -1 * (unsigned char ) 0xfffffff7 == (sizeof(int) == 2 ? (int)0xff09 : (int)0xffffff09));
86 ASSERT ((signed short) -1 * (unsigned short) 0xfffffff7 == (sizeof(int) == 2 ? 9u : 0xffff0009));
87 ASSERT ((signed LONG ) -1 * (unsigned LONG ) 0xfffffff7 == 9u);
89 ASSERT ((signed char ) -2 * (unsigned char ) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffffff8));
90 ASSERT ((signed short) -2 * (unsigned short) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffefff8));
91 ASSERT ((signed LONG ) -2 * (unsigned LONG ) 0x8004 == 0xfffefff8);
93 ASSERT (-1 * 0xfff7 == (sizeof(int) == 2 ? 9 : 0xffff0009)); // 0xfff7 is stored in 'unsigned int'
94 // but:
95 ASSERT (-1 * 65527 == -65527); // 65527 (== 0xfff7) is stored in 'signed LONG'
96 ASSERT (-1 * 33000 == -33000);
98 ASSERT (1 * 10000 * is8 == (sizeof(int) == 2 ? 14464 : 80000)); /* int */
99 ASSERT (1 * 10000l * is8 == 80000); /* LONG */
100 ASSERT (1 * 40000u * is8 == (sizeof(int) == 2 ? 57856u : 320000)); /* unsigned */
101 ASSERT (1 * 40000 * is8 == 320000); /* LONG */
102 ASSERT (1 * 0x4000 * is8 == (sizeof(int) == 2 ? 0 : 0x20000)); /* unsigned */
104 ASSERT (-2 * 1 < 1); /* comparison with 0 is optimized, so let's use 1 instead */
105 ASSERT (-2 * 1u > 1);
106 ASSERT (0x7fffu * 2 > 1);
107 ASSERT (0x7fffffffu * 2 > 1);
108 if (sizeof (int) == 2)
109 ASSERT (0x7fff * (unsigned char) 2 < 1);
110 else
111 ASSERT (mulWrapper (0x7fffffff, (unsigned char) 2) < 1);
112 ASSERT (mulWrapper (0x7fffffff, (unsigned short) 2) < 1);
114 /* mul icode: operandOperation() */
115 s = -3;
116 ASSERT (s * (stype) -1 == (stype) 3);
117 ASSERT (s * (stype) 1 == (stype) -3);
118 s = 3;
119 ASSERT (s * (stype) -1 == (stype) -3);
121 s = 1;
122 ASSERT (s * (utype) 0xfffffff7 == (utype) 0xfffffff7);
123 uc = (unsigned char ) 0xfffffff8;
124 ASSERT (uc * (unsigned char ) 0xfffffff7 == 0xef48);
125 us = (unsigned short) 0xfffffff8;
126 if (sizeof (int) == 2 && CHAR_BIT == 8)
127 ASSERT (us * (unsigned short) 0xfffffff7 == 0x0048);
128 ul = (unsigned LONG ) 0xfffffff8;
129 ASSERT (ul * (unsigned LONG ) 0xfffffff7 == 0x0048);
130 ul = (unsigned LONG ) 0xfffffff8;
132 ASSERT ((stype ) 0xfffffff8 * (stype ) 0xfffffff7 == 72);
134 ASSERT ((signed char ) -1 * (unsigned char ) 0xfffffff7 == (sizeof(int) == 2 ? (int)0xff09 : (int)0xffffff09));
135 ASSERT ((signed short) -1 * (unsigned short) 0xfffffff7 == (sizeof(int) == 2 ? 9u : 0xffff0009));
136 ASSERT ((signed LONG ) -1 * (unsigned LONG ) 0xfffffff7 == 9u);
138 ASSERT ((signed char ) -2 * (unsigned char ) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffffff8));
139 ASSERT ((signed short) -2 * (unsigned short) 0x8004 == (sizeof(int) == 2 ? (int)0xfff8 : (int)0xfffefff8));
140 ASSERT ((signed LONG ) -2 * (unsigned LONG ) 0x8004 == 0xfffefff8);
142 /* div ast: valDiv() */
143 ASSERT ((stype) -12 / (stype) -3 == (stype) 4);
144 ASSERT ((stype) -12 / (stype) 3 == (stype) -4);
145 ASSERT ((stype) 12 / (stype) -3 == (stype) -4);
147 ASSERT ((unsigned char ) -12 / (signed char ) -3 == (sizeof(int) == 2 ? (int)0xffaf : (int)0xffffffaf));
148 ASSERT ((unsigned short) -12 / (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xffffaaaf));
149 ASSERT ((unsigned LONG ) -12 / (signed LONG ) -3 == 0);
150 ASSERT ((utype) -12 / (stype) 3 == (stype) 0x55555551);
151 ASSERT ((unsigned char ) 12 / (signed char ) -3 == -4);
152 ASSERT ((unsigned short) 12 / (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xfffffffc));
153 ASSERT ((unsigned LONG ) 12 / (signed LONG ) -3 == 0);
155 ASSERT ((stype) -12 / (utype) -3 == 0);
156 ASSERT ((signed char ) -12 / (unsigned char ) 3 == -4);
157 ASSERT ((signed short) -12 / (unsigned short) 3 == (sizeof(int) == 2 ? 0x5551 : -4));
158 ASSERT ((signed LONG ) -12 / (unsigned LONG ) 3 == 0x55555551);
159 ASSERT ((stype) 12 / (utype) -3 == 0);
161 ASSERT (12u / 3 * 10000 == 40000);
163 ASSERT (-1 / 1 < 0);
165 /* div icode: operandOperation() */
166 s = -12;
167 ASSERT (s / (stype) -3 == (stype) 4);
168 s = -12;
169 ASSERT (s / (stype) 3 == (stype) -4);
170 s = 12;
171 ASSERT (s / (stype) -3 == (stype) -4);
173 uc = -12;
174 ASSERT (uc / (signed char ) -3 == (sizeof(int) == 2 ? (int)0xffaf : (int)0xffffffaf));
175 us = -12;
176 ASSERT (us / (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xffffaaaf));
177 ul = -12;
178 ASSERT (ul / (signed LONG ) -3 == 0);
179 u = -12;
180 ASSERT (u / (stype) 3 == (stype) 0x55555551);
181 uc = 12;
182 ASSERT (uc / (signed char ) -3 == -4);
183 us = 12;
184 ASSERT (us / (signed short) -3 == (sizeof(int) == 2 ? 0 : 0xfffffffc));
185 ul = 12;
186 ASSERT (ul / (signed LONG ) -3 == 0);
188 s = -12;
189 ASSERT (s / (utype) -3 == 0);
190 sc = -12;
191 ASSERT (sc / (unsigned char ) 3 == -4);
192 ss = -12;
193 ASSERT (ss / (unsigned short) 3 == (sizeof(int) == 2 ? 0x5551 : -4));
194 sl = -12;
195 ASSERT (sl / (unsigned LONG ) 3 == 0x55555551);
196 s = 12;
197 ASSERT (s / (utype) -3 == 0);
200 /* mod ast: valMod() */
201 /* -11 : 0xfff5 */
202 /* -17 : 0xffef */
203 ASSERT ((stype) -17 % (stype) -11 == (stype) -6);
204 ASSERT ((stype) -17 % (stype) 11 == (stype) -6);
205 ASSERT ((stype) 17 % (stype) -11 == (stype) 6);
206 ASSERT ((unsigned char ) -17 % (signed char ) -11 == 8);
207 ASSERT ((unsigned short) -17 % (signed short) -11 == (sizeof(int) == 2 ? -17 : 3));
208 ASSERT ((unsigned LONG ) -17 % (signed LONG ) -11 == -17);
209 ASSERT ((unsigned char ) -17 % (signed char ) 11 == 8);
210 ASSERT ((unsigned short) -17 % (signed short) 11 == 3);
211 ASSERT ((unsigned LONG ) -17 % (signed LONG ) 11 == 9);
212 ASSERT ((unsigned char ) 17 % (signed char ) -11 == 6);
213 ASSERT ((unsigned short) 17 % (signed short) -11 == (sizeof(int) == 2 ? 17 : 6));
214 ASSERT ((unsigned LONG ) 17 % (signed LONG ) -11 == 17);
216 ASSERT (-3 % 2 < 0);
219 /* add */
220 ASSERT ( 80 + 80 == 160);
221 ASSERT (150 + 150 == 300);
222 ASSERT (160u + 160u == 320);
223 #endif