3 /* sprintf( char *, 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.
15 int sprintf( char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, ...)
19 va_start( ap, format );
20 rc = vsnprintf( s, SIZE_MAX, format, ap ); /* TODO: replace with non-checking call */
28 #include <_PDCLIB_test.h>
33 #define testprintf( s, n, format, ... ) sprintf( s, format, __VA_ARGS__ )
38 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
39 TESTCASE( strcmp( buffer, "-128" ) == 0 );
40 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
41 TESTCASE( strcmp( buffer, "127" ) == 0 );
42 TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
43 TESTCASE( strcmp( buffer, "0" ) == 0 );
44 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
45 TESTCASE( strcmp( buffer, "-32768" ) == 0 );
46 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
47 TESTCASE( strcmp( buffer, "32767" ) == 0 );
48 TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
49 TESTCASE( strcmp( buffer, "0" ) == 0 );
50 TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
51 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
52 TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
53 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
54 TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
55 TESTCASE( strcmp( buffer, "0" ) == 0 );
56 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
57 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
58 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
59 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
60 TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
61 TESTCASE( strcmp( buffer, "0" ) == 0 );
62 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
63 TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
64 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
65 TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
66 TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
67 TESTCASE( strcmp( buffer, "0" ) == 0 );
68 TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
69 TESTCASE( strcmp( buffer, "255" ) == 0 );
70 TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
71 TESTCASE( strcmp( buffer, "255" ) == 0 );
72 TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
73 TESTCASE( strcmp( buffer, "65535" ) == 0 );
74 TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
75 TESTCASE( strcmp( buffer, "65535" ) == 0 );
76 TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
77 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
78 TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
79 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
80 TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
81 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
82 TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
83 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
84 TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
85 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
86 TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
87 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
88 TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
89 TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
90 TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
91 TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
92 TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
93 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
94 TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
95 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
96 TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
97 TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
98 TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
99 TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
100 /* TODO: This test case is broken, doesn't test what it was intended to. */
101 TESTCASE( testprintf( buffer, 100, "%.0#o", 0 ) == 5 );
102 TESTCASE( strcmp( buffer, "%.0#o" ) == 0 );
103 TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
104 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
105 TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
106 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
107 TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
108 TESTCASE( strcmp( buffer, "+0" ) == 0 );
109 TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
110 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
111 TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
112 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
113 TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
114 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
115 TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
116 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
117 TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
118 TESTCASE( strcmp( buffer, " 0" ) == 0 );
119 TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
120 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
121 TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
122 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
123 TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
124 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
125 TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
126 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
127 TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
128 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
129 TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
130 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
131 TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
132 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
133 TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
134 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
135 TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
136 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
137 TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
138 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
139 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
140 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
141 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
142 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
143 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
144 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
145 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
146 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
147 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
148 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
149 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
150 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
151 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
152 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
153 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
154 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
155 TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
156 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
157 TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
158 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
159 TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
160 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
161 TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
162 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
163 TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
164 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
165 TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
166 TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
167 TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
168 TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
169 TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
170 TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
171 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
172 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
173 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
174 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
175 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
176 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
177 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
178 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
179 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
180 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
181 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
182 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
183 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
184 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
185 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
186 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
187 TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
188 TESTCASE( strcmp( buffer, " 00000000002147483647" ) == 0 );
189 TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
190 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
191 TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
192 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
193 TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
194 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
195 TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
196 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
197 TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
198 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
199 TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
200 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
201 TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
202 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
203 TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
204 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
205 TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
206 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
207 TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
208 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
209 TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
210 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
211 TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
212 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
213 TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
214 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
215 TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
216 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
217 TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
218 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
219 TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
220 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
221 TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
222 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
223 TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
224 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
225 TESTCASE( testprintf( buffer, 100, "- %d", INT_MAX ) == 12 );
226 TESTCASE( strcmp( buffer, "- 2147483647" ) == 0 );
227 TESTCASE( testprintf( buffer, 100, "- %d %% %d", INT_MAX, INT_MIN ) == 26 );
228 TESTCASE( strcmp( buffer, "- 2147483647 % -2147483648" ) == 0 );
229 TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
230 TESTCASE( strcmp( buffer, "x" ) == 0 );
231 TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
232 TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
233 TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
234 TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );
237 TESTCASE( testprintf( buffer, 100, "123456%n789%n", &val1, &val2 ) == 9 );
238 TESTCASE( strcmp( buffer, "123456789" ) == 0 );
239 TESTCASE( val1 == 6 );
240 TESTCASE( val2 == 9 );