struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / ltoa.c.in
bloba611c58fe7cd449bfc6224955b9b9dbf2d660eba
1 /** Simple test for the _ltoa and _ultoa.
2 test: ultoa, ltoa
3 part: 1, 2, 3, 4, 5
4 */
6 #include <testfwk.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #if !defined(PORT_HOST) && !(defined(__SDCC_mcs51) && !defined(__SDCC_STACK_AUTO) && defined(__SDCC_MODEL_SMALL) ) && !defined(__SDCC_pic14) && !defined(__SDCC_pic16) && !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
11 /* do not run tests for listed above platforms - out of memory */
12 #define TEST_{test} 1
13 #define LTOA(num,base,res) check_l(num, base, res)
14 #define ULTOA(num,base,res) check_ul(num, base, res)
16 static char
17 check_l (long n, int b, const char *r)
19 static char buf[66];
20 __ltoa(n, buf, b);
21 return !strcmp (buf, r);
24 static char
25 check_ul (unsigned long n, int b, const char *r)
27 char buf[66];
28 __ultoa(n, buf, b);
29 return !strcmp (buf, r);
32 #endif
34 void test_ltoa (void)
36 #ifdef TEST_ltoa
37 #if {part} == 1
38 ASSERT (LTOA (0, 2, "0"));
39 ASSERT (LTOA (0, 8, "0"));
40 ASSERT (LTOA (0, 10, "0"));
41 ASSERT (LTOA (0, 16, "0"));
42 ASSERT (LTOA (1, 2, "1"));
43 ASSERT (LTOA (1, 8, "1"));
44 ASSERT (LTOA (1, 10, "1"));
45 ASSERT (LTOA (1, 16, "1"));
46 ASSERT (LTOA (2, 2, "10"));
47 ASSERT (LTOA (2, 8, "2"));
48 ASSERT (LTOA (2, 10, "2"));
49 ASSERT (LTOA (2, 16, "2"));
50 ASSERT (LTOA (3, 2, "11"));
51 ASSERT (LTOA (3, 8, "3"));
52 ASSERT (LTOA (3, 10, "3"));
53 ASSERT (LTOA (3, 16, "3"));
54 ASSERT (LTOA (4, 2, "100"));
55 ASSERT (LTOA (4, 8, "4"));
56 ASSERT (LTOA (4, 10, "4"));
57 ASSERT (LTOA (4, 16, "4"));
58 ASSERT (LTOA (5, 2, "101"));
59 ASSERT (LTOA (5, 8, "5"));
60 ASSERT (LTOA (5, 10, "5"));
61 ASSERT (LTOA (5, 16, "5"));
62 ASSERT (LTOA (6, 2, "110"));
63 ASSERT (LTOA (6, 8, "6"));
64 ASSERT (LTOA (6, 10, "6"));
65 ASSERT (LTOA (6, 16, "6"));
66 ASSERT (LTOA (7, 2, "111"));
67 ASSERT (LTOA (7, 8, "7"));
68 ASSERT (LTOA (7, 10, "7"));
69 ASSERT (LTOA (7, 16, "7"));
70 ASSERT (LTOA (8, 2, "1000"));
71 ASSERT (LTOA (8, 8, "10"));
72 ASSERT (LTOA (8, 10, "8"));
73 ASSERT (LTOA (8, 16, "8"));
74 ASSERT (LTOA (9, 2, "1001"));
75 ASSERT (LTOA (9, 8, "11"));
76 ASSERT (LTOA (9, 10, "9"));
77 ASSERT (LTOA (9, 16, "9"));
78 ASSERT (LTOA (10, 2, "1010"));
79 ASSERT (LTOA (10, 8, "12"));
80 ASSERT (LTOA (10, 10, "10"));
81 ASSERT (LTOA (10, 16, "A"));
82 ASSERT (LTOA (11, 2, "1011"));
83 ASSERT (LTOA (11, 8, "13"));
84 ASSERT (LTOA (11, 10, "11"));
85 ASSERT (LTOA (11, 16, "B"));
86 ASSERT (LTOA (12, 2, "1100"));
87 ASSERT (LTOA (12, 8, "14"));
88 ASSERT (LTOA (12, 10, "12"));
89 ASSERT (LTOA (12, 16, "C"));
90 ASSERT (LTOA (13, 2, "1101"));
91 ASSERT (LTOA (13, 8, "15"));
92 #elif {part} == 2
93 ASSERT (LTOA (13, 10, "13"));
94 ASSERT (LTOA (13, 16, "D"));
95 ASSERT (LTOA (14, 2, "1110"));
96 ASSERT (LTOA (14, 8, "16"));
97 ASSERT (LTOA (14, 10, "14"));
98 ASSERT (LTOA (14, 16, "E"));
99 ASSERT (LTOA (15, 2, "1111"));
100 ASSERT (LTOA (15, 8, "17"));
101 ASSERT (LTOA (15, 10, "15"));
102 ASSERT (LTOA (15, 16, "F"));
103 ASSERT (LTOA (16, 2, "10000"));
104 ASSERT (LTOA (16, 8, "20"));
105 ASSERT (LTOA (16, 10, "16"));
106 ASSERT (LTOA (16, 16, "10"));
107 ASSERT (LTOA (-1, 2, "11111111111111111111111111111111"));
108 ASSERT (LTOA (-1, 8, "37777777777"));
109 ASSERT (LTOA (-1, 10, "-1"));
110 ASSERT (LTOA (-1, 16, "FFFFFFFF"));
111 ASSERT (LTOA (-2, 2, "11111111111111111111111111111110"));
112 ASSERT (LTOA (-2, 8, "37777777776"));
113 ASSERT (LTOA (-2, 10, "-2"));
114 ASSERT (LTOA (-2, 16, "FFFFFFFE"));
115 ASSERT (LTOA (-3, 2, "11111111111111111111111111111101"));
116 ASSERT (LTOA (-3, 8, "37777777775"));
117 ASSERT (LTOA (-3, 10, "-3"));
118 ASSERT (LTOA (-3, 16, "FFFFFFFD"));
119 ASSERT (LTOA (-4, 2, "11111111111111111111111111111100"));
120 ASSERT (LTOA (-4, 8, "37777777774"));
121 ASSERT (LTOA (-4, 10, "-4"));
122 ASSERT (LTOA (-4, 16, "FFFFFFFC"));
123 ASSERT (LTOA (-5, 2, "11111111111111111111111111111011"));
124 ASSERT (LTOA (-5, 8, "37777777773"));
125 ASSERT (LTOA (-5, 10, "-5"));
126 ASSERT (LTOA (-5, 16, "FFFFFFFB"));
127 ASSERT (LTOA (-6, 2, "11111111111111111111111111111010"));
128 ASSERT (LTOA (-6, 8, "37777777772"));
129 ASSERT (LTOA (-6, 10, "-6"));
130 ASSERT (LTOA (-6, 16, "FFFFFFFA"));
131 ASSERT (LTOA (-7, 2, "11111111111111111111111111111001"));
132 ASSERT (LTOA (-7, 8, "37777777771"));
133 ASSERT (LTOA (-7, 10, "-7"));
134 ASSERT (LTOA (-7, 16, "FFFFFFF9"));
135 ASSERT (LTOA (-8, 2, "11111111111111111111111111111000"));
136 ASSERT (LTOA (-8, 8, "37777777770"));
137 ASSERT (LTOA (-8, 10, "-8"));
138 ASSERT (LTOA (-8, 16, "FFFFFFF8"));
139 ASSERT (LTOA (-9, 2, "11111111111111111111111111110111"));
140 ASSERT (LTOA (-9, 8, "37777777767"));
141 ASSERT (LTOA (-9, 10, "-9"));
142 ASSERT (LTOA (-9, 16, "FFFFFFF7"));
143 ASSERT (LTOA (-10, 2, "11111111111111111111111111110110"));
144 ASSERT (LTOA (-10, 8, "37777777766"));
145 ASSERT (LTOA (-10, 10, "-10"));
146 ASSERT (LTOA (-10, 16, "FFFFFFF6"));
147 #elif {part} == 3
148 ASSERT (LTOA (-11, 2, "11111111111111111111111111110101"));
149 ASSERT (LTOA (-11, 8, "37777777765"));
150 ASSERT (LTOA (-11, 10, "-11"));
151 ASSERT (LTOA (-11, 16, "FFFFFFF5"));
152 ASSERT (LTOA (-12, 2, "11111111111111111111111111110100"));
153 ASSERT (LTOA (-12, 8, "37777777764"));
154 ASSERT (LTOA (-12, 10, "-12"));
155 ASSERT (LTOA (-12, 16, "FFFFFFF4"));
156 ASSERT (LTOA (-13, 2, "11111111111111111111111111110011"));
157 ASSERT (LTOA (-13, 8, "37777777763"));
158 ASSERT (LTOA (-13, 10, "-13"));
159 ASSERT (LTOA (-13, 16, "FFFFFFF3"));
160 ASSERT (LTOA (-14, 2, "11111111111111111111111111110010"));
161 ASSERT (LTOA (-14, 8, "37777777762"));
162 ASSERT (LTOA (-14, 10, "-14"));
163 ASSERT (LTOA (-14, 16, "FFFFFFF2"));
164 ASSERT (LTOA (-15, 2, "11111111111111111111111111110001"));
165 ASSERT (LTOA (-15, 8, "37777777761"));
166 ASSERT (LTOA (-15, 10, "-15"));
167 ASSERT (LTOA (-15, 16, "FFFFFFF1"));
168 ASSERT (LTOA (-16, 2, "11111111111111111111111111110000"));
169 ASSERT (LTOA (-16, 8, "37777777760"));
170 ASSERT (LTOA (-16, 10, "-16"));
171 ASSERT (LTOA (-16, 16, "FFFFFFF0"));
172 ASSERT (LTOA (127, 2, "1111111"));
173 ASSERT (LTOA (127, 8, "177"));
174 ASSERT (LTOA (127, 10, "127"));
175 ASSERT (LTOA (127, 16, "7F"));
176 ASSERT (LTOA (-127, 2, "11111111111111111111111110000001"));
177 ASSERT (LTOA (-127, 8, "37777777601"));
178 ASSERT (LTOA (-127, 10, "-127"));
179 ASSERT (LTOA (-127, 16, "FFFFFF81"));
180 ASSERT (LTOA (128, 2, "10000000"));
181 ASSERT (LTOA (128, 8, "200"));
182 ASSERT (LTOA (128, 10, "128"));
183 ASSERT (LTOA (128, 16, "80"));
184 ASSERT (LTOA (-128, 2, "11111111111111111111111110000000"));
185 ASSERT (LTOA (-128, 8, "37777777600"));
186 ASSERT (LTOA (-128, 10, "-128"));
187 ASSERT (LTOA (-128, 16, "FFFFFF80"));
188 ASSERT (LTOA (255, 2, "11111111"));
189 ASSERT (LTOA (255, 8, "377"));
190 ASSERT (LTOA (255, 10, "255"));
191 ASSERT (LTOA (255, 16, "FF"));
192 ASSERT (LTOA (-255, 2, "11111111111111111111111100000001"));
193 ASSERT (LTOA (-255, 8, "37777777401"));
194 ASSERT (LTOA (-255, 10, "-255"));
195 ASSERT (LTOA (-255, 16, "FFFFFF01"));
196 ASSERT (LTOA (256, 2, "100000000"));
197 ASSERT (LTOA (256, 8, "400"));
198 ASSERT (LTOA (256, 10, "256"));
199 ASSERT (LTOA (256, 16, "100"));
200 ASSERT (LTOA (-256, 2, "11111111111111111111111100000000"));
201 ASSERT (LTOA (-256, 8, "37777777400"));
202 #elif {part} == 4
203 ASSERT (LTOA (-256, 10, "-256"));
204 ASSERT (LTOA (-256, 16, "FFFFFF00"));
205 ASSERT (LTOA (0x12345678, 2, "10010001101000101011001111000"));
206 ASSERT (LTOA (0x12345678, 8, "2215053170"));
207 ASSERT (LTOA (0x12345678, 10, "305419896"));
208 ASSERT (LTOA (0x12345678, 16, "12345678"));
209 ASSERT (LTOA (-0x12345678, 2, "11101101110010111010100110001000"));
210 ASSERT (LTOA (-0x12345678, 8, "35562724610"));
211 ASSERT (LTOA (-0x12345678, 10, "-305419896"));
212 ASSERT (LTOA (-0x12345678, 16, "EDCBA988"));
213 ASSERT (LTOA (0x70abcdef, 2, "1110000101010111100110111101111"));
214 ASSERT (LTOA (0x70abcdef, 8, "16052746757"));
215 ASSERT (LTOA (0x70abcdef, 10, "1890307567"));
216 ASSERT (LTOA (0x70abcdef, 16, "70ABCDEF"));
217 ASSERT (LTOA (-0x70abcdef, 2, "10001111010101000011001000010001"));
218 ASSERT (LTOA (-0x70abcdef, 8, "21725031021"));
219 ASSERT (LTOA (-0x70abcdef, 10, "-1890307567"));
220 ASSERT (LTOA (-0x70abcdef, 16, "8F543211"));
221 ASSERT (LTOA (01234567, 2, "1010011100101110111"));
222 ASSERT (LTOA (01234567, 8, "1234567"));
223 ASSERT (LTOA (01234567, 10, "342391"));
224 ASSERT (LTOA (01234567, 16, "53977"));
225 ASSERT (LTOA (-01234567, 2, "11111111111110101100011010001001"));
226 ASSERT (LTOA (-01234567, 8, "37776543211"));
227 ASSERT (LTOA (-01234567, 10, "-342391"));
228 ASSERT (LTOA (-01234567, 16, "FFFAC689"));
229 ASSERT (LTOA (32767, 2, "111111111111111"));
230 ASSERT (LTOA (32767, 8, "77777"));
231 ASSERT (LTOA (32767, 10, "32767"));
232 ASSERT (LTOA (32767, 16, "7FFF"));
233 ASSERT (LTOA (-32767, 2, "11111111111111111000000000000001"));
234 ASSERT (LTOA (-32767, 8, "37777700001"));
235 ASSERT (LTOA (-32767, 10, "-32767"));
236 ASSERT (LTOA (-32767, 16, "FFFF8001"));
237 ASSERT (LTOA (32768, 2, "1000000000000000"));
238 ASSERT (LTOA (32768, 8, "100000"));
239 ASSERT (LTOA (32768, 10, "32768"));
240 ASSERT (LTOA (32768, 16, "8000"));
241 ASSERT (LTOA (-32768, 2, "11111111111111111000000000000000"));
242 ASSERT (LTOA (-32768, 8, "37777700000"));
243 ASSERT (LTOA (-32768, 10, "-32768"));
244 ASSERT (LTOA (-32768, 16, "FFFF8000"));
245 ASSERT (LTOA (0x7fffffff, 2, "1111111111111111111111111111111"));
246 ASSERT (LTOA (0x7fffffff, 8, "17777777777"));
247 ASSERT (LTOA (0x7fffffff, 10, "2147483647"));
248 ASSERT (LTOA (0x7fffffff, 16, "7FFFFFFF"));
249 ASSERT (LTOA (-0x7fffffff, 2, "10000000000000000000000000000001"));
250 ASSERT (LTOA (-0x7fffffff, 8, "20000000001"));
251 ASSERT (LTOA (-0x7fffffff, 10, "-2147483647"));
252 ASSERT (LTOA (-0x7fffffff, 16, "80000001"));
253 ASSERT (LTOA (-0x80000000, 2, "10000000000000000000000000000000"));
254 ASSERT (LTOA (-0x80000000, 8, "20000000000"));
255 ASSERT (LTOA (-0x80000000, 10, "-2147483648"));
256 ASSERT (LTOA (-0x80000000, 16, "80000000"));
257 #elif {part} == 5
258 ASSERT (LTOA (1, 6, "1"));
259 ASSERT (LTOA (12, 6, "20"));
260 ASSERT (LTOA (123, 6, "323"));
261 #endif /* {part} */
262 #undef LTOA
263 #endif /* TEST_ltoa */
267 void test_ultoa (void)
269 #ifdef TEST_ultoa
270 #if {part} == 1
271 ASSERT (ULTOA (0, 2, "0"));
272 ASSERT (ULTOA (0, 8, "0"));
273 ASSERT (ULTOA (0, 10, "0"));
274 ASSERT (ULTOA (0, 16, "0"));
275 ASSERT (ULTOA (1, 2, "1"));
276 ASSERT (ULTOA (1, 8, "1"));
277 ASSERT (ULTOA (1, 10, "1"));
278 ASSERT (ULTOA (1, 16, "1"));
279 ASSERT (ULTOA (2, 2, "10"));
280 ASSERT (ULTOA (2, 8, "2"));
281 ASSERT (ULTOA (2, 10, "2"));
282 ASSERT (ULTOA (2, 16, "2"));
283 ASSERT (ULTOA (3, 2, "11"));
284 ASSERT (ULTOA (3, 8, "3"));
285 ASSERT (ULTOA (3, 10, "3"));
286 ASSERT (ULTOA (3, 16, "3"));
287 ASSERT (ULTOA (4, 2, "100"));
288 ASSERT (ULTOA (4, 8, "4"));
289 ASSERT (ULTOA (4, 10, "4"));
290 ASSERT (ULTOA (4, 16, "4"));
291 ASSERT (ULTOA (5, 2, "101"));
292 ASSERT (ULTOA (5, 8, "5"));
293 ASSERT (ULTOA (5, 10, "5"));
294 ASSERT (ULTOA (5, 16, "5"));
295 ASSERT (ULTOA (6, 2, "110"));
296 ASSERT (ULTOA (6, 8, "6"));
297 ASSERT (ULTOA (6, 10, "6"));
298 ASSERT (ULTOA (6, 16, "6"));
299 #elif {part} == 2
300 ASSERT (ULTOA (7, 2, "111"));
301 ASSERT (ULTOA (7, 8, "7"));
302 ASSERT (ULTOA (7, 10, "7"));
303 ASSERT (ULTOA (7, 16, "7"));
304 ASSERT (ULTOA (8, 2, "1000"));
305 ASSERT (ULTOA (8, 8, "10"));
306 ASSERT (ULTOA (8, 10, "8"));
307 ASSERT (ULTOA (8, 16, "8"));
308 ASSERT (ULTOA (9, 2, "1001"));
309 ASSERT (ULTOA (9, 8, "11"));
310 ASSERT (ULTOA (9, 10, "9"));
311 ASSERT (ULTOA (9, 16, "9"));
312 ASSERT (ULTOA (10, 2, "1010"));
313 ASSERT (ULTOA (10, 8, "12"));
314 ASSERT (ULTOA (10, 10, "10"));
315 ASSERT (ULTOA (10, 16, "A"));
316 ASSERT (ULTOA (11, 2, "1011"));
317 ASSERT (ULTOA (11, 8, "13"));
318 ASSERT (ULTOA (11, 10, "11"));
319 ASSERT (ULTOA (11, 16, "B"));
320 ASSERT (ULTOA (12, 2, "1100"));
321 ASSERT (ULTOA (12, 8, "14"));
322 ASSERT (ULTOA (12, 10, "12"));
323 ASSERT (ULTOA (12, 16, "C"));
324 ASSERT (ULTOA (13, 2, "1101"));
325 ASSERT (ULTOA (13, 8, "15"));
326 ASSERT (ULTOA (13, 10, "13"));
327 ASSERT (ULTOA (13, 16, "D"));
328 #elif {part} == 3
329 ASSERT (ULTOA (14, 2, "1110"));
330 ASSERT (ULTOA (14, 8, "16"));
331 ASSERT (ULTOA (14, 10, "14"));
332 ASSERT (ULTOA (14, 16, "E"));
333 ASSERT (ULTOA (15, 2, "1111"));
334 ASSERT (ULTOA (15, 8, "17"));
335 ASSERT (ULTOA (15, 10, "15"));
336 ASSERT (ULTOA (15, 16, "F"));
337 ASSERT (ULTOA (16, 2, "10000"));
338 ASSERT (ULTOA (16, 8, "20"));
339 ASSERT (ULTOA (16, 10, "16"));
340 ASSERT (ULTOA (16, 16, "10"));
341 ASSERT (ULTOA (127, 2, "1111111"));
342 ASSERT (ULTOA (127, 8, "177"));
343 ASSERT (ULTOA (127, 10, "127"));
344 ASSERT (ULTOA (127, 16, "7F"));
345 ASSERT (ULTOA (255, 2, "11111111"));
346 ASSERT (ULTOA (255, 8, "377"));
347 ASSERT (ULTOA (255, 10, "255"));
348 ASSERT (ULTOA (255, 16, "FF"));
349 ASSERT (ULTOA (65535, 2, "1111111111111111"));
350 ASSERT (ULTOA (65535, 8, "177777"));
351 ASSERT (ULTOA (65535, 10, "65535"));
352 ASSERT (ULTOA (65535, 16, "FFFF"));
353 ASSERT (ULTOA (65536, 2, "10000000000000000"));
354 ASSERT (ULTOA (65536, 8, "200000"));
355 ASSERT (ULTOA (65536, 10, "65536"));
356 ASSERT (ULTOA (65536, 16, "10000"));
357 #elif {part} == 4
358 ASSERT (ULTOA (0xffffffff, 2, "11111111111111111111111111111111"));
359 ASSERT (ULTOA (0xffffffff, 8, "37777777777"));
360 ASSERT (ULTOA (0xffffffff, 10, "4294967295"));
361 ASSERT (ULTOA (0xffffffff, 16, "FFFFFFFF"));
362 ASSERT (ULTOA (0x12345678, 2, "10010001101000101011001111000"));
363 ASSERT (ULTOA (0x12345678, 8, "2215053170"));
364 ASSERT (ULTOA (0x12345678, 10, "305419896"));
365 ASSERT (ULTOA (0x12345678, 16, "12345678"));
366 ASSERT (ULTOA (0x56789abc, 2, "1010110011110001001101010111100"));
367 ASSERT (ULTOA (0x56789abc, 8, "12636115274"));
368 ASSERT (ULTOA (0x56789abc, 10, "1450744508"));
369 ASSERT (ULTOA (0x56789abc, 16, "56789ABC"));
370 ASSERT (ULTOA (0xdef01234, 2, "11011110111100000001001000110100"));
371 ASSERT (ULTOA (0xdef01234, 8, "33674011064"));
372 ASSERT (ULTOA (0xdef01234, 10, "3740275252"));
373 ASSERT (ULTOA (0xdef01234, 16, "DEF01234"));
374 ASSERT (ULTOA (01234567, 2, "1010011100101110111"));
375 ASSERT (ULTOA (01234567, 8, "1234567"));
376 ASSERT (ULTOA (01234567, 10, "342391"));
377 ASSERT (ULTOA (01234567, 16, "53977"));
378 ASSERT (ULTOA (0x7fffffff, 2, "1111111111111111111111111111111"));
379 ASSERT (ULTOA (0x7fffffff, 8, "17777777777"));
380 ASSERT (ULTOA (0x7fffffff, 10, "2147483647"));
381 ASSERT (ULTOA (0x7fffffff, 16, "7FFFFFFF"));
382 ASSERT (ULTOA (0x80000000, 2, "10000000000000000000000000000000"));
383 ASSERT (ULTOA (0x80000000, 8, "20000000000"));
384 ASSERT (ULTOA (0x80000000, 10, "2147483648"));
385 ASSERT (ULTOA (0x80000000, 16, "80000000"));
386 ASSERT (ULTOA (0xffffffff, 2, "11111111111111111111111111111111"));
387 ASSERT (ULTOA (0xffffffff, 8, "37777777777"));
388 ASSERT (ULTOA (0xffffffff, 10, "4294967295"));
389 ASSERT (ULTOA (0xffffffff, 16, "FFFFFFFF"));
390 #elif {part} == 5
391 ASSERT (ULTOA (1, 6, "1"));
392 ASSERT (ULTOA (12, 6, "20"));
393 ASSERT (ULTOA (123, 6, "323"));
394 #endif /* {part} */
395 #undef ULTOA
396 #endif /*TEST_ultoa */