Added a test case for %0f and %.0f.
[wine/multimedia.git] / dlls / msvcrt / tests / printf.c
blob20887e65ee3a6306e42c49e37b824d16a1f9d04e
1 /*
2 * Conformance tests for *printf functions.
4 * Copyright 2002 Uwe Bonnes
5 * Copyright 2004 Aneurin Price
6 * Copyright 2005 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <stdio.h>
25 #include "wine/test.h"
27 static void test_sprintf( void )
29 char buffer[100];
30 const char *format;
31 double pnumber=789456123;
32 int x, r;
33 WCHAR wide[] = { 'w','i','d','e',0};
35 format = "%+#23.15e";
36 r = sprintf(buffer,format,pnumber);
37 todo_wine {
38 ok(!strcmp(buffer,"+7.894561230000000e+008"),"exponent format incorrect\n");
40 ok( r==23, "return count wrong\n");
42 todo_wine {
43 format = "%I64d";
44 r = sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
45 ok(!strcmp(buffer,"-8589934591"),"Problem with long long\n");
46 ok( r==11, "return count wrong\n");
49 format = "%lld";
50 r = sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
51 ok(!strcmp(buffer, "1"), "Problem with \"ll\" interpretation\n");
52 ok( r==1, "return count wrong\n");
54 format = "%S";
55 r = sprintf(buffer,format,wide);
56 ok(!strcmp(buffer,"wide"),"Problem with wide string format\n");
57 ok( r==4, "return count wrong\n");
59 format = "%04c";
60 r = sprintf(buffer,format,'1');
61 ok(!strcmp(buffer,"0001"),"Character not zero-prefixed \"%s\"\n",buffer);
62 ok( r==4, "return count wrong\n");
64 format = "%p";
65 r = sprintf(buffer,format,(void *)57);
66 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
67 ok( r==8, "return count wrong\n");
69 format = "%#012p";
70 r = sprintf(buffer,format,(void *)57);
71 ok(!strcmp(buffer," 0X00000039"),"Pointer formatted incorrectly\n");
72 ok( r==12, "return count wrong\n");
74 format = "%Fp";
75 r = sprintf(buffer,format,(void *)57);
76 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
77 ok( r==8, "return count wrong\n");
79 format = "%04s";
80 r = sprintf(buffer,format,"foo");
81 ok(!strcmp(buffer,"0foo"),"String not zero-prefixed \"%s\"\n",buffer);
82 ok( r==4, "return count wrong\n");
84 format = "%.1s";
85 r = sprintf(buffer,format,"foo");
86 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
87 ok( r==1, "return count wrong\n");
89 format = "%.*s";
90 r = sprintf(buffer,format,1,"foo");
91 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
92 ok( r==1, "return count wrong\n");
94 format = "%#-012p";
95 r = sprintf(buffer,format,(void *)57);
96 ok(!strcmp(buffer,"0X00000039 "),"Pointer formatted incorrectly\n");
97 ok( r==12, "return count wrong\n");
99 format = "hello";
100 r = sprintf(buffer, format);
101 ok(!strcmp(buffer,"hello"), "failed\n");
102 ok( r==5, "return count wrong\n");
104 format = "%ws";
105 r = sprintf(buffer, format, wide);
106 ok(!strcmp(buffer,"wide"), "failed\n");
107 ok( r==4, "return count wrong\n");
109 format = "%-10ws";
110 r = sprintf(buffer, format, wide );
111 ok(!strcmp(buffer,"wide "), "failed\n");
112 ok( r==10, "return count wrong\n");
114 format = "%10ws";
115 r = sprintf(buffer, format, wide );
116 ok(!strcmp(buffer," wide"), "failed\n");
117 ok( r==10, "return count wrong\n");
119 format = "%#+ -03whlls";
120 r = sprintf(buffer, format, wide );
121 ok(!strcmp(buffer,"wide"), "failed\n");
122 ok( r==4, "return count wrong\n");
124 format = "%w0s";
125 r = sprintf(buffer, format, wide );
126 ok(!strcmp(buffer,"0s"), "failed\n");
127 ok( r==2, "return count wrong\n");
129 format = "%w-s";
130 r = sprintf(buffer, format, wide );
131 ok(!strcmp(buffer,"-s"), "failed\n");
132 ok( r==2, "return count wrong\n");
134 format = "%ls";
135 r = sprintf(buffer, format, wide );
136 ok(!strcmp(buffer,"wide"), "failed\n");
137 ok( r==4, "return count wrong\n");
139 format = "%Ls";
140 r = sprintf(buffer, format, "not wide" );
141 ok(!strcmp(buffer,"not wide"), "failed\n");
142 ok( r==8, "return count wrong\n");
144 format = "%b";
145 r = sprintf(buffer, format);
146 ok(!strcmp(buffer,"b"), "failed\n");
147 ok( r==1, "return count wrong\n");
149 format = "%3c";
150 r = sprintf(buffer, format,'a');
151 ok(!strcmp(buffer," a"), "failed\n");
152 ok( r==3, "return count wrong\n");
154 format = "%3d";
155 r = sprintf(buffer, format,1234);
156 ok(!strcmp(buffer,"1234"), "failed\n");
157 ok( r==4, "return count wrong\n");
159 format = "%3h";
160 r = sprintf(buffer, format);
161 ok(!strcmp(buffer,""), "failed\n");
162 ok( r==0, "return count wrong\n");
164 format = "%j%k%m%q%r%t%v%y%z";
165 r = sprintf(buffer, format);
166 ok(!strcmp(buffer,"jkmqrtvyz"), "failed\n");
167 ok( r==9, "return count wrong\n");
169 format = "asdf%n";
170 x = 0;
171 r = sprintf(buffer, format, &x );
172 ok(x == 4, "should write to x\n");
173 ok(!strcmp(buffer,"asdf"), "failed\n");
174 ok( r==4, "return count wrong\n");
176 format = "%-1d";
177 r = sprintf(buffer, format,2);
178 ok(!strcmp(buffer,"2"), "failed\n");
179 ok( r==1, "return count wrong\n");
181 format = "%2.4f";
182 r = sprintf(buffer, format,8.6);
183 ok(!strcmp(buffer,"8.6000"), "failed\n");
184 ok( r==6, "return count wrong\n");
186 format = "%0f";
187 r = sprintf(buffer, format,0.6);
188 ok(!strcmp(buffer,"0.600000"), "failed\n");
189 ok( r==8, "return count wrong\n");
191 format = "%.0f";
192 r = sprintf(buffer, format,0.6);
193 ok(!strcmp(buffer,"1"), "failed\n");
194 ok( r==1, "return count wrong\n");
196 todo_wine {
197 format = "%2.4e";
198 r = sprintf(buffer, format,8.6);
199 ok(!strcmp(buffer,"8.6000e+000"), "failed\n");
200 ok( r==11, "return count wrong\n");
203 format = "%2.4g";
204 r = sprintf(buffer, format,8.6);
205 ok(!strcmp(buffer,"8.6"), "failed\n");
206 ok( r==3, "return count wrong\n");
208 format = "%-i";
209 r = sprintf(buffer, format,-1);
210 ok(!strcmp(buffer,"-1"), "failed\n");
211 ok( r==2, "return count wrong\n");
213 format = "%-i";
214 r = sprintf(buffer, format,1);
215 ok(!strcmp(buffer,"1"), "failed\n");
216 ok( r==1, "return count wrong\n");
218 format = "%+i";
219 r = sprintf(buffer, format,1);
220 ok(!strcmp(buffer,"+1"), "failed\n");
221 ok( r==2, "return count wrong\n");
223 format = "%o";
224 r = sprintf(buffer, format,10);
225 ok(!strcmp(buffer,"12"), "failed\n");
226 ok( r==2, "return count wrong\n");
228 format = "%p";
229 r = sprintf(buffer, format,0);
230 ok(!strcmp(buffer,"00000000"), "failed\n");
231 ok( r==8, "return count wrong\n");
233 format = "%s";
234 r = sprintf(buffer, format,0);
235 ok(!strcmp(buffer,"(null)"), "failed\n");
236 ok( r==6, "return count wrong\n");
238 format = "%s";
239 r = sprintf(buffer, format,"%%%%");
240 ok(!strcmp(buffer,"%%%%"), "failed\n");
241 ok( r==4, "return count wrong\n");
243 format = "%u";
244 r = sprintf(buffer, format,-1);
245 ok(!strcmp(buffer,"4294967295"), "failed\n");
246 ok( r==10, "return count wrong\n");
248 format = "%w";
249 r = sprintf(buffer, format,-1);
250 ok(!strcmp(buffer,""), "failed\n");
251 ok( r==0, "return count wrong\n");
253 format = "%h";
254 r = sprintf(buffer, format,-1);
255 ok(!strcmp(buffer,""), "failed\n");
256 ok( r==0, "return count wrong\n");
258 format = "%z";
259 r = sprintf(buffer, format,-1);
260 ok(!strcmp(buffer,"z"), "failed\n");
261 ok( r==1, "return count wrong\n");
263 format = "%j";
264 r = sprintf(buffer, format,-1);
265 ok(!strcmp(buffer,"j"), "failed\n");
266 ok( r==1, "return count wrong\n");
268 format = "%F";
269 r = sprintf(buffer, format,-1);
270 ok(!strcmp(buffer,""), "failed\n");
271 ok( r==0, "return count wrong\n");
273 format = "%H";
274 r = sprintf(buffer, format,-1);
275 ok(!strcmp(buffer,"H"), "failed\n");
276 ok( r==1, "return count wrong\n");
278 format = "x%cx";
279 r = sprintf(buffer, format, 0x100+'X');
280 ok(!strcmp(buffer,"xXx"), "failed\n");
281 ok( r==3, "return count wrong\n");
283 format = "%%0";
284 r = sprintf(buffer, format);
285 ok(!strcmp(buffer,"%0"), "failed: \"%s\"\n", buffer);
286 ok( r==2, "return count wrong\n");
289 static void test_swprintf( void )
291 wchar_t buffer[100];
292 const wchar_t I64d[] = {'%','I','6','4','d',0};
293 double pnumber=789456123;
294 const wchar_t TwentyThreePoint15e[]= {'%','+','#','2','3','.','1','5','e',0};
295 const wchar_t e008[] = {'e','+','0','0','8',0};
296 const char string[]={'s','t','r','i','n','g',0};
297 const wchar_t S[]={'%','S',0};
298 swprintf(buffer,TwentyThreePoint15e,pnumber);
299 todo_wine
301 ok(wcsstr(buffer,e008) != 0,"Sprintf different\n");
303 swprintf(buffer,I64d,((ULONGLONG)0xffffffff)*0xffffffff);
304 todo_wine
306 ok(wcslen(buffer) == 11,"Problem with long long\n");
308 swprintf(buffer,S,string);
309 ok(wcslen(buffer) == 6,"Problem with \"%%S\" interpretation\n");
312 static void test_fwprintf( void )
314 const char *string="not a wide string";
315 todo_wine
317 ok(fwprintf(fopen("nul","r+"),(wchar_t *)string) == -1,"Non-wide string should not be printed by fwprintf\n");
321 static void test_snprintf (void)
323 struct snprintf_test {
324 const char *format;
325 int expected;
326 struct {
327 int retval;
328 int render;
329 } todo;
331 /* Pre-2.1 libc behaviour, not C99 compliant. */
332 const struct snprintf_test tests[] = {{"short", 5, {0, 0}},
333 {"justfit", 7, {0, 0}},
334 {"justfits", 8, {0, 1}},
335 {"muchlonger", -1, {1, 1}}};
336 char buffer[8];
337 const int bufsiz = sizeof buffer;
338 unsigned int i;
340 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
341 const char *fmt = tests[i].format;
342 const int expect = tests[i].expected;
343 const int n = _snprintf (buffer, bufsiz, fmt);
344 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
346 todo (tests[i].todo.retval ? "wine" : "none")
347 ok (n == expect, "\"%s\": expected %d, returned %d\n",
348 fmt, expect, n);
349 todo (tests[i].todo.render ? "wine" : "none")
350 ok (!memcmp (fmt, buffer, valid),
351 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
355 START_TEST(printf)
357 test_sprintf();
358 test_swprintf();
359 test_fwprintf();
360 test_snprintf();