struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bug3475656.c
blob23ee19ad5cd27de3700bc7ca3742161dcd64c528
1 /*
2 bug3475656.c
4 Digits in output were reversed (e.g. 192 was written as "291").
5 */
7 #include <testfwk.h>
9 #ifdef __SDCC
10 #pragma disable_warning 85
11 #endif
13 #define UCHAR unsigned char
14 #define USHORT unsigned short
15 #define PCHAR unsigned char *
17 UCHAR mod_16x8(USHORT s16, UCHAR i8)
19 return(s16 % i8);
22 USHORT div_16x8(USHORT s16, UCHAR i8)
24 return(s16 / i8);
27 #if 1
28 void itoa(USHORT sVal, PCHAR pBuf, UCHAR iRadix)
30 PCHAR p; // pointer to traverse string
31 PCHAR pFirstDigit; // pointer to first digit
32 UCHAR iTmp; // temp UCHAR
34 p = pBuf;
35 pFirstDigit = p; // save pointer to first digit
37 do
39 iTmp = mod_16x8(sVal, iRadix);
40 sVal = div_16x8(sVal, iRadix);
42 // convert to ascii and store
43 *p++ = (iTmp > 9) ? iTmp - 10 + 'A' : iTmp + '0'; // a letter or a digit
44 } while (sVal > 0);
46 // We now have the digit of the number in the buffer, but in reverse order. Thus we reverse them now.
48 *p-- = '\0'; // terminate string; p points to last digit
52 iTmp = *p;
53 *p = *pFirstDigit;
54 *pFirstDigit = iTmp; // swap *p and *pFirstDigit
55 --p;
56 ++pFirstDigit; // advance to next two digits
57 } while (pFirstDigit < p); // repeat until halfway
59 #endif
61 void testBug(void)
63 unsigned char c[8];
64 unsigned short i;
66 i = 192;
67 #if 1
68 itoa(i, c, 10);
69 ASSERT(c[0] == '1');
70 ASSERT(c[1] == '9');
71 ASSERT(c[2] == '2');
72 #endif