struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / itoa.c.in
blob28ad22e600c94943f1b9bf820cbc8fc7bd8dbc3f
1 /** Simple test for the _itoa and _uitoa.
2 test: uitoa, itoa
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_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
11 /* do not run tests for listed above platforms - out of memory */
13 #define TEST_{test} 1
14 #define ITOA(num,base,res) check_i(num, base, res)
15 #define UITOA(num,base,res) check_ui(num, base, res)
17 static char
18 check_i (int n, int b, const char *r)
20 char buf[34];
21 __itoa(n, buf, b);
22 return !strcmp (buf, r);
25 static char
26 check_ui (int n, int b, const char *r)
28 char buf[34];
29 __uitoa(n, buf, b);
30 return !strcmp (buf, r);
33 #endif
35 void test_itoa (void)
37 #ifdef TEST_itoa
38 #if {part} == 1
39 ASSERT (ITOA (0, 2, "0"));
40 ASSERT (ITOA (0, 8, "0"));
41 ASSERT (ITOA (0, 10, "0"));
42 ASSERT (ITOA (0, 16, "0"));
43 ASSERT (ITOA (1, 2, "1"));
44 ASSERT (ITOA (1, 8, "1"));
45 ASSERT (ITOA (1, 10, "1"));
46 ASSERT (ITOA (1, 16, "1"));
47 ASSERT (ITOA (2, 2, "10"));
48 ASSERT (ITOA (2, 8, "2"));
49 ASSERT (ITOA (2, 10, "2"));
50 ASSERT (ITOA (2, 16, "2"));
51 ASSERT (ITOA (3, 2, "11"));
52 ASSERT (ITOA (3, 8, "3"));
53 ASSERT (ITOA (3, 10, "3"));
54 ASSERT (ITOA (3, 16, "3"));
55 ASSERT (ITOA (4, 2, "100"));
56 ASSERT (ITOA (4, 8, "4"));
57 ASSERT (ITOA (4, 10, "4"));
58 ASSERT (ITOA (4, 16, "4"));
59 ASSERT (ITOA (5, 2, "101"));
60 ASSERT (ITOA (5, 8, "5"));
61 ASSERT (ITOA (5, 10, "5"));
62 ASSERT (ITOA (5, 16, "5"));
63 ASSERT (ITOA (6, 2, "110"));
64 ASSERT (ITOA (6, 8, "6"));
65 ASSERT (ITOA (6, 10, "6"));
66 ASSERT (ITOA (6, 16, "6"));
67 ASSERT (ITOA (7, 2, "111"));
68 ASSERT (ITOA (7, 8, "7"));
69 ASSERT (ITOA (7, 10, "7"));
70 ASSERT (ITOA (7, 16, "7"));
71 ASSERT (ITOA (8, 2, "1000"));
72 ASSERT (ITOA (8, 8, "10"));
73 ASSERT (ITOA (8, 10, "8"));
74 ASSERT (ITOA (8, 16, "8"));
75 ASSERT (ITOA (9, 2, "1001"));
76 ASSERT (ITOA (9, 8, "11"));
77 ASSERT (ITOA (9, 10, "9"));
78 ASSERT (ITOA (9, 16, "9"));
79 ASSERT (ITOA (10, 2, "1010"));
80 ASSERT (ITOA (10, 8, "12"));
81 ASSERT (ITOA (10, 10, "10"));
82 ASSERT (ITOA (10, 16, "A"));
83 ASSERT (ITOA (11, 2, "1011"));
84 ASSERT (ITOA (11, 8, "13"));
85 ASSERT (ITOA (11, 10, "11"));
86 ASSERT (ITOA (11, 16, "B"));
87 ASSERT (ITOA (12, 2, "1100"));
88 ASSERT (ITOA (12, 8, "14"));
89 ASSERT (ITOA (12, 10, "12"));
90 #elif {part} == 2
91 ASSERT (ITOA (12, 16, "C"));
92 ASSERT (ITOA (13, 2, "1101"));
93 ASSERT (ITOA (13, 8, "15"));
94 ASSERT (ITOA (13, 10, "13"));
95 ASSERT (ITOA (13, 16, "D"));
96 ASSERT (ITOA (14, 2, "1110"));
97 ASSERT (ITOA (14, 8, "16"));
98 ASSERT (ITOA (14, 10, "14"));
99 ASSERT (ITOA (14, 16, "E"));
100 ASSERT (ITOA (15, 2, "1111"));
101 ASSERT (ITOA (15, 8, "17"));
102 ASSERT (ITOA (15, 10, "15"));
103 ASSERT (ITOA (15, 16, "F"));
104 ASSERT (ITOA (16, 2, "10000"));
105 ASSERT (ITOA (16, 8, "20"));
106 ASSERT (ITOA (16, 10, "16"));
107 ASSERT (ITOA (16, 16, "10"));
108 ASSERT (ITOA (-1, 2, "1111111111111111"));
109 ASSERT (ITOA (-1, 8, "177777"));
110 ASSERT (ITOA (-1, 10, "-1"));
111 ASSERT (ITOA (-1, 16, "FFFF"));
112 ASSERT (ITOA (-2, 2, "1111111111111110"));
113 ASSERT (ITOA (-2, 8, "177776"));
114 ASSERT (ITOA (-2, 10, "-2"));
115 ASSERT (ITOA (-2, 16, "FFFE"));
116 ASSERT (ITOA (-3, 2, "1111111111111101"));
117 ASSERT (ITOA (-3, 8, "177775"));
118 ASSERT (ITOA (-3, 10, "-3"));
119 ASSERT (ITOA (-3, 16, "FFFD"));
120 ASSERT (ITOA (-4, 2, "1111111111111100"));
121 ASSERT (ITOA (-4, 8, "177774"));
122 ASSERT (ITOA (-4, 10, "-4"));
123 ASSERT (ITOA (-4, 16, "FFFC"));
124 ASSERT (ITOA (-5, 2, "1111111111111011"));
125 ASSERT (ITOA (-5, 8, "177773"));
126 ASSERT (ITOA (-5, 10, "-5"));
127 ASSERT (ITOA (-5, 16, "FFFB"));
128 ASSERT (ITOA (-6, 2, "1111111111111010"));
129 ASSERT (ITOA (-6, 8, "177772"));
130 ASSERT (ITOA (-6, 10, "-6"));
131 ASSERT (ITOA (-6, 16, "FFFA"));
132 ASSERT (ITOA (-7, 2, "1111111111111001"));
133 ASSERT (ITOA (-7, 8, "177771"));
134 ASSERT (ITOA (-7, 10, "-7"));
135 ASSERT (ITOA (-7, 16, "FFF9"));
136 ASSERT (ITOA (-8, 2, "1111111111111000"));
137 ASSERT (ITOA (-8, 8, "177770"));
138 ASSERT (ITOA (-8, 10, "-8"));
139 ASSERT (ITOA (-8, 16, "FFF8"));
140 ASSERT (ITOA (-9, 2, "1111111111110111"));
141 #elif {part} == 3
142 ASSERT (ITOA (-9, 8, "177767"));
143 ASSERT (ITOA (-9, 10, "-9"));
144 ASSERT (ITOA (-9, 16, "FFF7"));
145 ASSERT (ITOA (-10, 2, "1111111111110110"));
146 ASSERT (ITOA (-10, 8, "177766"));
147 ASSERT (ITOA (-10, 10, "-10"));
148 ASSERT (ITOA (-10, 16, "FFF6"));
149 ASSERT (ITOA (-11, 2, "1111111111110101"));
150 ASSERT (ITOA (-11, 8, "177765"));
151 ASSERT (ITOA (-11, 10, "-11"));
152 ASSERT (ITOA (-11, 16, "FFF5"));
153 ASSERT (ITOA (-12, 2, "1111111111110100"));
154 ASSERT (ITOA (-12, 8, "177764"));
155 ASSERT (ITOA (-12, 10, "-12"));
156 ASSERT (ITOA (-12, 16, "FFF4"));
157 ASSERT (ITOA (-13, 2, "1111111111110011"));
158 ASSERT (ITOA (-13, 8, "177763"));
159 ASSERT (ITOA (-13, 10, "-13"));
160 ASSERT (ITOA (-13, 16, "FFF3"));
161 ASSERT (ITOA (-14, 2, "1111111111110010"));
162 ASSERT (ITOA (-14, 8, "177762"));
163 ASSERT (ITOA (-14, 10, "-14"));
164 ASSERT (ITOA (-14, 16, "FFF2"));
165 ASSERT (ITOA (-15, 2, "1111111111110001"));
166 ASSERT (ITOA (-15, 8, "177761"));
167 ASSERT (ITOA (-15, 10, "-15"));
168 ASSERT (ITOA (-15, 16, "FFF1"));
169 ASSERT (ITOA (-16, 2, "1111111111110000"));
170 ASSERT (ITOA (-16, 8, "177760"));
171 ASSERT (ITOA (-16, 10, "-16"));
172 ASSERT (ITOA (-16, 16, "FFF0"));
173 ASSERT (ITOA (127, 2, "1111111"));
174 ASSERT (ITOA (127, 8, "177"));
175 ASSERT (ITOA (127, 10, "127"));
176 ASSERT (ITOA (127, 16, "7F"));
177 ASSERT (ITOA (-127, 2, "1111111110000001"));
178 ASSERT (ITOA (-127, 8, "177601"));
179 ASSERT (ITOA (-127, 10, "-127"));
180 ASSERT (ITOA (-127, 16, "FF81"));
181 ASSERT (ITOA (128, 2, "10000000"));
182 ASSERT (ITOA (128, 8, "200"));
183 ASSERT (ITOA (128, 10, "128"));
184 ASSERT (ITOA (128, 16, "80"));
185 ASSERT (ITOA (-128, 2, "1111111110000000"));
186 ASSERT (ITOA (-128, 8, "177600"));
187 ASSERT (ITOA (-128, 10, "-128"));
188 ASSERT (ITOA (-128, 16, "FF80"));
189 #elif {part} == 4
190 ASSERT (ITOA (255, 2, "11111111"));
191 ASSERT (ITOA (255, 8, "377"));
192 ASSERT (ITOA (255, 10, "255"));
193 ASSERT (ITOA (255, 16, "FF"));
194 ASSERT (ITOA (-255, 2, "1111111100000001"));
195 ASSERT (ITOA (-255, 8, "177401"));
196 ASSERT (ITOA (-255, 10, "-255"));
197 ASSERT (ITOA (-255, 16, "FF01"));
198 ASSERT (ITOA (256, 2, "100000000"));
199 ASSERT (ITOA (256, 8, "400"));
200 ASSERT (ITOA (256, 10, "256"));
201 ASSERT (ITOA (256, 16, "100"));
202 ASSERT (ITOA (-256, 2, "1111111100000000"));
203 ASSERT (ITOA (-256, 8, "177400"));
204 ASSERT (ITOA (-256, 10, "-256"));
205 ASSERT (ITOA (-256, 16, "FF00"));
206 ASSERT (ITOA (0x1234, 2, "1001000110100"));
207 ASSERT (ITOA (0x1234, 8, "11064"));
208 ASSERT (ITOA (0x1234, 10, "4660"));
209 ASSERT (ITOA (0x1234, 16, "1234"));
210 ASSERT (ITOA (-0x1234, 2, "1110110111001100"));
211 ASSERT (ITOA (-0x1234, 8, "166714"));
212 ASSERT (ITOA (-0x1234, 10, "-4660"));
213 ASSERT (ITOA (-0x1234, 16, "EDCC"));
214 ASSERT (ITOA (0x5678, 2, "101011001111000"));
215 ASSERT (ITOA (0x5678, 8, "53170"));
216 ASSERT (ITOA (0x5678, 10, "22136"));
217 ASSERT (ITOA (0x5678, 16, "5678"));
218 ASSERT (ITOA (-0x5678, 2, "1010100110001000"));
219 ASSERT (ITOA (-0x5678, 8, "124610"));
220 ASSERT (ITOA (-0x5678, 10, "-22136"));
221 ASSERT (ITOA (-0x5678, 16, "A988"));
222 ASSERT (ITOA (012345, 2, "1010011100101"));
223 ASSERT (ITOA (012345, 8, "12345"));
224 ASSERT (ITOA (012345, 10, "5349"));
225 ASSERT (ITOA (012345, 16, "14E5"));
226 ASSERT (ITOA (-012345, 2, "1110101100011011"));
227 ASSERT (ITOA (-012345, 8, "165433"));
228 ASSERT (ITOA (-012345, 10, "-5349"));
229 ASSERT (ITOA (-012345, 16, "EB1B"));
230 ASSERT (ITOA (32767, 2, "111111111111111"));
231 ASSERT (ITOA (32767, 8, "77777"));
232 ASSERT (ITOA (32767, 10, "32767"));
233 ASSERT (ITOA (32767, 16, "7FFF"));
234 ASSERT (ITOA (-32767, 2, "1000000000000001"));
235 ASSERT (ITOA (-32767, 8, "100001"));
236 ASSERT (ITOA (-32767, 10, "-32767"));
237 ASSERT (ITOA (-32767, 16, "8001"));
238 ASSERT (ITOA (-32768, 2, "1000000000000000"));
239 ASSERT (ITOA (-32768, 8, "100000"));
240 ASSERT (ITOA (-32768, 10, "-32768"));
241 ASSERT (ITOA (-32768, 16, "8000"));
242 #elif {part} == 5
243 ASSERT (ITOA (1, 6, "1"));
244 ASSERT (ITOA (12, 6, "20"));
245 ASSERT (ITOA (123, 6, "323"));
246 #endif
247 #undef ITOA
248 #endif /* TEST_itoa */
251 void test_uitoa(void)
253 #ifdef TEST_uitoa
254 #if {part} == 1
255 ASSERT (UITOA (0, 2, "0"));
256 ASSERT (UITOA (0, 8, "0"));
257 ASSERT (UITOA (0, 10, "0"));
258 ASSERT (UITOA (0, 16, "0"));
259 ASSERT (UITOA (1, 2, "1"));
260 ASSERT (UITOA (1, 8, "1"));
261 ASSERT (UITOA (1, 10, "1"));
262 ASSERT (UITOA (1, 16, "1"));
263 ASSERT (UITOA (2, 2, "10"));
264 ASSERT (UITOA (2, 8, "2"));
265 ASSERT (UITOA (2, 10, "2"));
266 ASSERT (UITOA (2, 16, "2"));
267 ASSERT (UITOA (3, 2, "11"));
268 ASSERT (UITOA (3, 8, "3"));
269 ASSERT (UITOA (3, 10, "3"));
270 ASSERT (UITOA (3, 16, "3"));
271 ASSERT (UITOA (4, 2, "100"));
272 ASSERT (UITOA (4, 8, "4"));
273 ASSERT (UITOA (4, 10, "4"));
274 ASSERT (UITOA (4, 16, "4"));
275 ASSERT (UITOA (5, 2, "101"));
276 ASSERT (UITOA (5, 8, "5"));
277 ASSERT (UITOA (5, 10, "5"));
278 ASSERT (UITOA (5, 16, "5"));
279 ASSERT (UITOA (6, 2, "110"));
280 ASSERT (UITOA (6, 8, "6"));
281 ASSERT (UITOA (6, 10, "6"));
282 #elif {part} == 2
283 ASSERT (UITOA (6, 16, "6"));
284 ASSERT (UITOA (7, 2, "111"));
285 ASSERT (UITOA (7, 8, "7"));
286 ASSERT (UITOA (7, 10, "7"));
287 ASSERT (UITOA (7, 16, "7"));
288 ASSERT (UITOA (8, 2, "1000"));
289 ASSERT (UITOA (8, 8, "10"));
290 ASSERT (UITOA (8, 10, "8"));
291 ASSERT (UITOA (8, 16, "8"));
292 ASSERT (UITOA (9, 2, "1001"));
293 ASSERT (UITOA (9, 8, "11"));
294 ASSERT (UITOA (9, 10, "9"));
295 ASSERT (UITOA (9, 16, "9"));
296 ASSERT (UITOA (10, 2, "1010"));
297 ASSERT (UITOA (10, 8, "12"));
298 ASSERT (UITOA (10, 10, "10"));
299 ASSERT (UITOA (10, 16, "A"));
300 ASSERT (UITOA (11, 2, "1011"));
301 ASSERT (UITOA (11, 8, "13"));
302 ASSERT (UITOA (11, 10, "11"));
303 ASSERT (UITOA (11, 16, "B"));
304 ASSERT (UITOA (12, 2, "1100"));
305 ASSERT (UITOA (12, 8, "14"));
306 ASSERT (UITOA (12, 10, "12"));
307 ASSERT (UITOA (12, 16, "C"));
308 ASSERT (UITOA (13, 2, "1101"));
309 ASSERT (UITOA (13, 8, "15"));
310 #elif {part} == 3
311 ASSERT (UITOA (13, 10, "13"));
312 ASSERT (UITOA (13, 16, "D"));
313 ASSERT (UITOA (14, 2, "1110"));
314 ASSERT (UITOA (14, 8, "16"));
315 ASSERT (UITOA (14, 10, "14"));
316 ASSERT (UITOA (14, 16, "E"));
317 ASSERT (UITOA (15, 2, "1111"));
318 ASSERT (UITOA (15, 8, "17"));
319 ASSERT (UITOA (15, 10, "15"));
320 ASSERT (UITOA (15, 16, "F"));
321 ASSERT (UITOA (16, 2, "10000"));
322 ASSERT (UITOA (16, 8, "20"));
323 ASSERT (UITOA (16, 10, "16"));
324 ASSERT (UITOA (16, 16, "10"));
325 ASSERT (UITOA (127, 2, "1111111"));
326 ASSERT (UITOA (127, 8, "177"));
327 ASSERT (UITOA (127, 10, "127"));
328 ASSERT (UITOA (127, 16, "7F"));
329 ASSERT (UITOA (255, 2, "11111111"));
330 ASSERT (UITOA (255, 8, "377"));
331 ASSERT (UITOA (255, 10, "255"));
332 ASSERT (UITOA (255, 16, "FF"));
333 ASSERT (UITOA (0x1234, 2, "1001000110100"));
334 ASSERT (UITOA (0x1234, 8, "11064"));
335 ASSERT (UITOA (0x1234, 10, "4660"));
336 ASSERT (UITOA (0x1234, 16, "1234"));
337 ASSERT (UITOA (0x5678, 2, "101011001111000"));
338 #elif {part} == 4
339 ASSERT (UITOA (0x5678, 8, "53170"));
340 ASSERT (UITOA (0x5678, 10, "22136"));
341 ASSERT (UITOA (0x5678, 16, "5678"));
342 ASSERT (UITOA (0x9abc, 2, "1001101010111100"));
343 ASSERT (UITOA (0x9abc, 8, "115274"));
344 ASSERT (UITOA (0x9abc, 10, "39612"));
345 ASSERT (UITOA (0x9abc, 16, "9ABC"));
346 ASSERT (UITOA (0xdef0, 2, "1101111011110000"));
347 ASSERT (UITOA (0xdef0, 8, "157360"));
348 ASSERT (UITOA (0xdef0, 10, "57072"));
349 ASSERT (UITOA (0xdef0, 16, "DEF0"));
350 ASSERT (UITOA (012345, 2, "1010011100101"));
351 ASSERT (UITOA (012345, 8, "12345"));
352 ASSERT (UITOA (012345, 10, "5349"));
353 ASSERT (UITOA (012345, 16, "14E5"));
354 ASSERT (UITOA (32767, 2, "111111111111111"));
355 ASSERT (UITOA (32767, 8, "77777"));
356 ASSERT (UITOA (32767, 10, "32767"));
357 ASSERT (UITOA (32767, 16, "7FFF"));
358 ASSERT (UITOA (32768, 2, "1000000000000000"));
359 ASSERT (UITOA (32768, 8, "100000"));
360 ASSERT (UITOA (32768, 10, "32768"));
361 ASSERT (UITOA (32768, 16, "8000"));
362 ASSERT (UITOA (65535, 2, "1111111111111111"));
363 ASSERT (UITOA (65535, 8, "177777"));
364 ASSERT (UITOA (65535, 10, "65535"));
365 ASSERT (UITOA (65535, 16, "FFFF"));
366 #elif {part} == 5
367 ASSERT (UITOA (1, 6, "1"));
368 ASSERT (UITOA (12, 6, "20"));
369 ASSERT (UITOA (123, 6, "323"));
370 #endif
371 #undef UITOA
372 #endif /* TEST_uitoa */