Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / snprintf.c.in
blob70dde1f04d4e81526f324b47897cee507622eb97
1 /** tests for snprintf
2 type: INT, LONG, STRING, FLOAT, PTR
3 */
4 #include <testfwk.h>
5 #include <string.h>
6 #include <stdio.h>
8 #define {type} 1
10 #if 0
11 # define DEBUG(x) x
12 #else
13 # define DEBUG(x)
14 #endif
16 #define CHECK_B 0
18 #if defined(INT)
20 struct
22 int arg;
23 char *fmt;
24 char *result;
25 } static const cases[] = {
26 // arg, fmt, result
27 {0xab, "%04x", "00ab"},
28 {0xffff, "0x%02X", "0xFFFF"},
29 {0xffffu, "%u", "65535"},
30 {1234, "%+6d", " +1234"},
31 {12345, "% d", " 12345"},
32 {-1234, "%d", "-1234"},
33 {32767, "%8d", " 32767"},
34 {1, "%%%d", "%1"},
35 {1001, "%08i", "00001001"},
36 {101, "%-+8d", "+101 "},
37 {01234, "%o(oct)", "1234(oct)"},
39 // do we want to check these:
40 #if defined(__SDCC) && !defined(__SDCC_z80) && CHECK_B
41 {0x4142, "%bc %bx", "\x41 42"}, /* non-std: print as bytes! */
42 {0xfffe, "0x%02bX%02bx", "0xFFfe"}, /* non-std: print as bytes! */
43 #elif defined(__SDCC) && defined(__SDCC_z80) && CHECK_B
44 {0x4142, "%bc %bx", "\x42 41"}, /* non-std: print as bytes! */
45 {0xfffe, "0x%02bX%02bx", "0xFEff"}, /* non-std: print as bytes! */
46 #endif
49 #elif defined(LONG)
51 struct
53 long arg;
54 char *fmt;
55 char *result;
56 } static const cases[] = {
57 // arg, fmt, result
58 {0x12345678, "0x%lx", "0x12345678"},
59 {0x7fffFFFF, "%10lx", " 7fffffff"},
60 {0x789abcde, "0x%-10lX", "0x789ABCDE "},
61 {0x1000a, "0x%02lX", "0x1000A"},
62 {0x80000000, "%lu", "2147483648"},
63 {-2147483648, "%li", "-2147483648"},
64 {-1234, "%+6ld", " -1234"},
65 {012345670123, "%lo(oct)", "12345670123(oct)"},
66 {0xffffFFFF, "%lo(oct)", "37777777777(oct)"},
68 // do we want to check these:
69 #if defined(__SDCC) && !defined(__SDCC_z80) && CHECK_B
70 {0xfedcba98, "0x%bX%bX%bx%bx", "0xFEDCba98"}, /* non-std: print as bytes! */
71 #elif defined(__SDCC) && defined(__SDCC_z80) && CHECK_B
72 {0xfedcba98, "0x%bX%bX%bx%bx", "0x98BAdcfe"}, /* non-std: print as bytes! */
73 #endif
76 #elif defined(STRING)
78 struct
80 char *arg;
81 char *fmt;
82 char *result;
83 } static const cases[] = {
84 // arg, fmt, result
85 {"abcd", "%s", "abcd"},
86 {"abcd", "%3s", "abcd"},
87 {"abcd", "%5s", " abcd"},
88 {"abcd", "%-5s", "abcd "},
89 {"abcd", "%.2s", "ab"},
90 {"XYZ\\", "%s", "XYZ\x5c"},
91 {"ab\x1b\x7f", "%s", "ab\x1b\x7f"},
92 {"ab\tcd\n", "%s", "ab\tcd\n"},
95 #elif defined(FLOAT)
97 struct
99 float arg;
100 char *fmt;
101 char *result;
102 } static const cases[] = {
103 // arg, fmt, result
104 // ... there should be more ...
105 #if defined(__SDCC) && !defined(__SDCC_ds390) && !(defined(__SDCC_mcs51) && (defined(__SDCC_USE_XSTACK) || defined(__SDCC_MODEL_HUGE)))
106 {1.0, "%f", "<NO FLOAT>"},
107 #else
108 {1.0, "%f", "1.000000"},
109 {1.96, "%3.1f", "2.0"},
110 #endif
113 #elif defined(PTR)
115 #ifndef PORT_HOST
116 #define PTR_NON_STD
117 #endif
119 #if defined(__SDCC) && (defined(__SDCC_mcs51) || defined(__SDCC_ds390))
120 __idata unsigned char __at(0xAA) var_idata;
121 __pdata unsigned char __at(0xAA) var_pdata;
122 __xdata unsigned char __at(0xAABB) var_xdata;
123 __code unsigned char __at(0xAABB) var_code;
124 #endif
126 struct
128 void *arg;
129 char *fmt;
130 char *result;
131 } static const cases[] = {
132 // arg, fmt, result
133 #if defined(__SDCC) && defined(__SDCC_ds390)
134 // Special cases for ds390 covering different address spaces.
135 {var_idata, "%p", "I:0x0000aa"},
136 {var_pdata, "%p", "P:0x0000aa"},
137 {var_xdata, "%p", "X:0x00aabb"},
138 {var_code, "%p", "C:0x00aabb"},
139 #ifdef PTR_NON_STD
140 {var_idata, "%P", "I:0x0000AA"},
141 {var_pdata, "%P", "P:0x0000AA"},
142 {var_xdata, "%P", "X:0x00AABB"},
143 {var_code, "%P", "C:0x00AABB"},
144 #endif
145 #elif defined(__SDCC) && defined(__SDCC_mcs51)
146 // Special cases for mcs51 covering different address spaces.
147 {var_idata, "%p", "I:0xaa"},
148 {var_pdata, "%p", "P:0xaa"},
149 {var_xdata, "%p", "X:0xaabb"},
150 {var_code, "%p", "C:0xaabb"},
151 #ifdef PTR_NON_STD
152 {var_idata, "%P", "I:0xAA"},
153 {var_pdata, "%P", "P:0xAA"},
154 {var_xdata, "%P", "X:0xAABB"},
155 {var_code, "%P", "C:0xAABB"},
156 #endif
157 #else
158 {(void *)0xAABB, "%p", "0xaabb"},
159 #ifdef PTR_NON_STD
160 {(void *)0xAABB, "%P", "0xAABB"},
161 #endif
162 #endif
165 #endif
167 void
168 test_snprintf (void)
170 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
171 #ifndef __SDCC_pic16
172 unsigned char buf[32];
173 unsigned char i;
175 memset (buf, 0xfe, sizeof buf); /* cookies all over */
177 for (i = 0; i < sizeof cases / sizeof cases[0]; i++)
179 sprintf (buf, cases[i].fmt, cases[i].arg);
180 DEBUG (__prints ("Test"));
181 DEBUG (__printd (i));
182 DEBUG (__prints (" should be: '"));
183 DEBUG (__prints (cases[i].result));
184 DEBUG (__prints ("' is: '"));
185 DEBUG (__prints (buf));
186 DEBUG (__prints ("'\n"));
187 ASSERT (!strcmp (buf, cases[i].result));
190 ASSERT (buf[sizeof buf - 10] == 0xfe); /* check for cookie */
191 #endif
192 #endif