3 /* snprintf( char *, size_t, const char *, ... )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
14 int snprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, ...)
18 va_start( ap, format );
19 rc = vsnprintf( s, n, format, ap );
27 #include <_PDCLIB_test.h>
32 #define testprintf( s, n, format, ... ) snprintf( s, n, format, __VA_ARGS__ )
37 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
38 TESTCASE( strcmp( buffer, "-128" ) == 0 );
39 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
40 TESTCASE( strcmp( buffer, "127" ) == 0 );
41 TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
42 TESTCASE( strcmp( buffer, "0" ) == 0 );
43 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
44 TESTCASE( strcmp( buffer, "-32768" ) == 0 );
45 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
46 TESTCASE( strcmp( buffer, "32767" ) == 0 );
47 TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
48 TESTCASE( strcmp( buffer, "0" ) == 0 );
49 TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
50 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
51 TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
52 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
53 TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
54 TESTCASE( strcmp( buffer, "0" ) == 0 );
55 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
56 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
57 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
58 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
59 TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
60 TESTCASE( strcmp( buffer, "0" ) == 0 );
61 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
62 TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
63 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
64 TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
65 TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
66 TESTCASE( strcmp( buffer, "0" ) == 0 );
67 TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
68 TESTCASE( strcmp( buffer, "255" ) == 0 );
69 TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
70 TESTCASE( strcmp( buffer, "255" ) == 0 );
71 TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
72 TESTCASE( strcmp( buffer, "65535" ) == 0 );
73 TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
74 TESTCASE( strcmp( buffer, "65535" ) == 0 );
75 TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
76 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
77 TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
78 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
79 TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
80 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
81 TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
82 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
83 TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
84 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
85 TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
86 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
87 TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
88 TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
89 TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
90 TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
91 TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
92 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
93 TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
94 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
95 TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
96 TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
97 TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
98 TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
99 /* TODO: This test case is broken, doesn't test what it was intended to. */
100 TESTCASE( testprintf( buffer, 100, "%.0#o", 0 ) == 5 );
101 TESTCASE( strcmp( buffer, "%.0#o" ) == 0 );
102 TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
103 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
104 TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
105 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
106 TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
107 TESTCASE( strcmp( buffer, "+0" ) == 0 );
108 TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
109 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
110 TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
111 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
112 TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
113 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
114 TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
115 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
116 TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
117 TESTCASE( strcmp( buffer, " 0" ) == 0 );
118 TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
119 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
120 TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
121 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
122 TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
123 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
124 TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
125 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
126 TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
127 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
128 TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
129 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
130 TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
131 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
132 TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
133 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
134 TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
135 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
136 TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
137 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
138 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
139 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
140 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
141 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
142 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
143 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
144 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
145 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
146 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
147 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
148 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
149 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
150 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
151 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
152 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
153 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
154 TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
155 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
156 TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
157 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
158 TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
159 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
160 TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
161 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
162 TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
163 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
164 TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
165 TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
166 TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
167 TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
168 TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
169 TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
170 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
171 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
172 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
173 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
174 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
175 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
176 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
177 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
178 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
179 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
180 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
181 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
182 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
183 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
184 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
185 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
186 TESTCASE( testprintf( buffer, 8, "%9d", INT_MAX ) == 10 );
187 TESTCASE( strcmp( buffer, "2147483" ) == 0 );
188 TESTCASE( testprintf( buffer, 8, "%9d", INT_MIN ) == 11 );
189 TESTCASE( strcmp( buffer, "-214748" ) == 0 );
190 TESTCASE( testprintf( buffer, 9, "%9d", INT_MAX ) == 10 );
191 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
192 TESTCASE( testprintf( buffer, 9, "%9d", INT_MIN ) == 11 );
193 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
194 TESTCASE( testprintf( buffer, 10, "%9d", INT_MAX ) == 10 );
195 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
196 TESTCASE( testprintf( buffer, 10, "%9d", INT_MIN ) == 11 );
197 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
198 TESTCASE( testprintf( buffer, 9, "%10d", INT_MAX ) == 10 );
199 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
200 TESTCASE( testprintf( buffer, 9, "%10d", INT_MIN ) == 11 );
201 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
202 TESTCASE( testprintf( buffer, 10, "%10d", INT_MAX ) == 10 );
203 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
204 TESTCASE( testprintf( buffer, 10, "%10d", INT_MIN ) == 11 );
205 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
206 TESTCASE( testprintf( buffer, 11, "%10d", INT_MAX ) == 10 );
207 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
208 TESTCASE( testprintf( buffer, 11, "%10d", INT_MIN ) == 11 );
209 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
210 TESTCASE( testprintf( buffer, 10, "%11d", INT_MAX ) == 11 );
211 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
212 TESTCASE( testprintf( buffer, 10, "%11d", INT_MIN ) == 11 );
213 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
214 TESTCASE( testprintf( buffer, 11, "%11d", INT_MAX ) == 11 );
215 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
216 TESTCASE( testprintf( buffer, 11, "%11d", INT_MIN ) == 11 );
217 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
218 TESTCASE( testprintf( buffer, 12, "%11d", INT_MAX ) == 11 );
219 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
220 TESTCASE( testprintf( buffer, 12, "%11d", INT_MIN ) == 11 );
221 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
222 TESTCASE( testprintf( buffer, 11, "%12d", INT_MAX ) == 12 );
223 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
224 TESTCASE( testprintf( buffer, 11, "%12d", INT_MIN ) == 12 );
225 TESTCASE( strcmp( buffer, " -21474836" ) == 0 );
226 TESTCASE( testprintf( buffer, 12, "%12d", INT_MAX ) == 12 );
227 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
228 TESTCASE( testprintf( buffer, 12, "%12d", INT_MIN ) == 12 );
229 TESTCASE( strcmp( buffer, " -214748364" ) == 0 );
230 TESTCASE( testprintf( buffer, 13, "%12d", INT_MAX ) == 12 );
231 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
232 TESTCASE( testprintf( buffer, 13, "%12d", INT_MIN ) == 12 );
233 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
234 TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
235 TESTCASE( strcmp( buffer, " 00000000002147483647" ) == 0 );
236 TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
237 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
238 TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
239 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
240 TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
241 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
242 TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
243 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
244 TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
245 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
246 TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
247 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
248 TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
249 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
250 TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
251 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
252 TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
253 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
254 TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
255 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
256 TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
257 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
258 TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
259 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
260 TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
261 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
262 TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
263 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
264 TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
265 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
266 TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
267 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
268 TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
269 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
270 TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
271 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
272 TESTCASE( testprintf( buffer, 100, "- %d", INT_MAX ) == 12 );
273 TESTCASE( strcmp( buffer, "- 2147483647" ) == 0 );
274 TESTCASE( testprintf( buffer, 100, "- %d %% %d", INT_MAX, INT_MIN ) == 26 );
275 TESTCASE( strcmp( buffer, "- 2147483647 % -2147483648" ) == 0 );
276 TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
277 TESTCASE( strcmp( buffer, "x" ) == 0 );
278 TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
279 TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
280 TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
281 TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );
284 TESTCASE( testprintf( buffer, 100, "123456%n789%n", &val1, &val2 ) == 9 );
285 TESTCASE( strcmp( buffer, "123456789" ) == 0 );
286 TESTCASE( val1 == 6 );
287 TESTCASE( val2 == 9 );