1 /*
2  * Conformance tests for *printf functions.
3  *
4  * Copyright 2002 Uwe Bonnes
5  * Copyright 2004 Aneurin Price
6  * Copyright 2005 Mike McCormack
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 
23 /* With Visual Studio >= 2005,  swprintf() takes an extra parameter unless
24  * the following macro is defined.
25  */
26 #define _CRT_NON_CONFORMING_SWPRINTFS
27 
28 #include <stdio.h>
29 #include <errno.h>
30 #include <math.h>
31 #include <locale.h>
32 
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winnls.h"
36 
37 #include "wine/test.h"
38 
39 #ifndef __REACTOS__
40 
41 static inline float __port_infinity(void)
42 {
43     static const unsigned __inf_bytes = 0x7f800000;
44     return *(const float *)&__inf_bytes;
45 }
46 #define INFINITY __port_infinity()
47 
48 static inline float __port_nan(void)
49 {
50     static const unsigned __nan_bytes = 0x7fc00000;
51     return *(const float *)&__nan_bytes;
52 }
53 #define NAN __port_nan()
54 
55 #endif
56 
57 static inline float __port_ind(void)
58 {
59     static const unsigned __ind_bytes = 0xffc00000;
60     return *(const float *)&__ind_bytes;
61 }
62 #define IND __port_ind()
63 
64 static int (__cdecl *p__vscprintf)(const char *format, __ms_va_list valist);
65 static int (__cdecl *p__vscwprintf)(const wchar_t *format, __ms_va_list valist);
66 static int (__cdecl *p__vsnwprintf_s)(wchar_t *str, size_t sizeOfBuffer,
67                                       size_t count, const wchar_t *format,
68                                       __ms_va_list valist);
69 static int (__cdecl *p__ecvt_s)(char *buffer, size_t length, double number,
70                                 int ndigits, int *decpt, int *sign);
71 static int (__cdecl *p__fcvt_s)(char *buffer, size_t length, double number,
72                                 int ndigits, int *decpt, int *sign);
73 static unsigned int (__cdecl *p__get_output_format)(void);
74 static unsigned int (__cdecl *p__set_output_format)(unsigned int);
75 static int (WINAPIV *p_sprintf)(char*, ...);
76 static int (__cdecl *p__vsprintf_p)(char*, size_t, const char*, __ms_va_list);
77 static int (__cdecl *p_vswprintf)(wchar_t *str, const wchar_t *format, __ms_va_list valist);
78 static int (__cdecl *p__vswprintf)(wchar_t *str, const wchar_t *format, __ms_va_list valist);
79 static int (__cdecl *p__vswprintf_l)(wchar_t *str, const wchar_t *format,
80                                      void *locale, __ms_va_list valist);
81 static int (__cdecl *p__vswprintf_c)(wchar_t *str, size_t size, const wchar_t *format,
82                                      __ms_va_list valist);
83 static int (__cdecl *p__vswprintf_c_l)(wchar_t *str, size_t size, const wchar_t *format,
84                                        void *locale, __ms_va_list valist);
85 static int (__cdecl *p__vswprintf_p_l)(wchar_t *str, size_t size, const wchar_t *format,
86                                        void *locale, __ms_va_list valist);
87 
88 static void init( void )
89 {
90     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
91 
92     p_sprintf = (void *)GetProcAddress(hmod, "sprintf");
93     p__vscprintf = (void *)GetProcAddress(hmod, "_vscprintf");
94     p__vscwprintf = (void *)GetProcAddress(hmod, "_vscwprintf");
95     p__vsnwprintf_s = (void *)GetProcAddress(hmod, "_vsnwprintf_s");
96     p__ecvt_s = (void *)GetProcAddress(hmod, "_ecvt_s");
97     p__fcvt_s = (void *)GetProcAddress(hmod, "_fcvt_s");
98     p__get_output_format = (void *)GetProcAddress(hmod, "_get_output_format");
99     p__set_output_format = (void *)GetProcAddress(hmod, "_set_output_format");
100     p__vsprintf_p = (void*)GetProcAddress(hmod, "_vsprintf_p");
101     p_vswprintf = (void*)GetProcAddress(hmod, "vswprintf");
102     p__vswprintf = (void*)GetProcAddress(hmod, "_vswprintf");
103     p__vswprintf_l = (void*)GetProcAddress(hmod, "_vswprintf_l");
104     p__vswprintf_c = (void*)GetProcAddress(hmod, "_vswprintf_c");
105     p__vswprintf_c_l = (void*)GetProcAddress(hmod, "_vswprintf_c_l");
106     p__vswprintf_p_l = (void*)GetProcAddress(hmod, "_vswprintf_p_l");
107 }
108 
109 static void test_sprintf( void )
110 {
111     char buffer[100];
112     const char *format;
113     double pnumber=789456123;
114     int x, r;
115     WCHAR wide[] = { 'w','i','d','e',0};
116     WCHAR buf_w[2];
117 
118     format = "%+#23.15e";
119     r = p_sprintf(buffer,format,pnumber);
120     ok(!strcmp(buffer,"+7.894561230000000e+008"),"+#23.15e failed: '%s'\n", buffer);
121     ok( r==23, "return count wrong\n");
122 
123     format = "%-#23.15e";
124     r = p_sprintf(buffer,format,pnumber);
125     ok(!strcmp(buffer,"7.894561230000000e+008 "),"-#23.15e failed: '%s'\n", buffer);
126     ok( r==23, "return count wrong\n");
127 
128     format = "%#23.15e";
129     r = p_sprintf(buffer,format,pnumber);
130     ok(!strcmp(buffer," 7.894561230000000e+008"),"#23.15e failed: '%s'\n", buffer);
131     ok( r==23, "return count wrong\n");
132 
133     format = "%#1.1g";
134     r = p_sprintf(buffer,format,pnumber);
135     ok(!strcmp(buffer,"8.e+008"),"#1.1g failed: '%s'\n", buffer);
136     ok( r==7, "return count wrong\n");
137 
138     format = "%I64d";
139     r = p_sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
140     ok(!strcmp(buffer,"-8589934591"),"Problem with long long\n");
141     ok( r==11, "return count wrong\n");
142 
143     format = "%+8I64d";
144     r = p_sprintf(buffer,format,(LONGLONG)100);
145     ok(!strcmp(buffer,"    +100") && r==8,"+8I64d failed: '%s'\n", buffer);
146 
147     format = "%+.8I64d";
148     r = p_sprintf(buffer,format,(LONGLONG)100);
149     ok(!strcmp(buffer,"+00000100") && r==9,"+.8I64d failed: '%s'\n", buffer);
150 
151     format = "%+10.8I64d";
152     r = p_sprintf(buffer,format,(LONGLONG)100);
153     ok(!strcmp(buffer," +00000100") && r==10,"+10.8I64d failed: '%s'\n", buffer);
154     format = "%_1I64d";
155     r = p_sprintf(buffer,format,(LONGLONG)100);
156     ok(!strcmp(buffer,"_1I64d") && r==6,"_1I64d failed\n");
157 
158     format = "%-1.5I64d";
159     r = p_sprintf(buffer,format,(LONGLONG)-100);
160     ok(!strcmp(buffer,"-00100") && r==6,"-1.5I64d failed: '%s'\n", buffer);
161 
162     format = "%5I64d";
163     r = p_sprintf(buffer,format,(LONGLONG)100);
164     ok(!strcmp(buffer,"  100") && r==5,"5I64d failed: '%s'\n", buffer);
165 
166     format = "%5I64d";
167     r = p_sprintf(buffer,format,(LONGLONG)-100);
168     ok(!strcmp(buffer," -100") && r==5,"5I64d failed: '%s'\n", buffer);
169 
170     format = "%-5I64d";
171     r = p_sprintf(buffer,format,(LONGLONG)100);
172     ok(!strcmp(buffer,"100  ") && r==5,"-5I64d failed: '%s'\n", buffer);
173 
174     format = "%-5I64d";
175     r = p_sprintf(buffer,format,(LONGLONG)-100);
176     ok(!strcmp(buffer,"-100 ") && r==5,"-5I64d failed: '%s'\n", buffer);
177 
178     format = "%-.5I64d";
179     r = p_sprintf(buffer,format,(LONGLONG)100);
180     ok(!strcmp(buffer,"00100") && r==5,"-.5I64d failed: '%s'\n", buffer);
181 
182     format = "%-.5I64d";
183     r = p_sprintf(buffer,format,(LONGLONG)-100);
184     ok(!strcmp(buffer,"-00100") && r==6,"-.5I64d failed: '%s'\n", buffer);
185 
186     format = "%-8.5I64d";
187     r = p_sprintf(buffer,format,(LONGLONG)100);
188     ok(!strcmp(buffer,"00100   ") && r==8,"-8.5I64d failed: '%s'\n", buffer);
189 
190     format = "%-8.5I64d";
191     r = p_sprintf(buffer,format,(LONGLONG)-100);
192     ok(!strcmp(buffer,"-00100  ") && r==8,"-8.5I64d failed: '%s'\n", buffer);
193 
194     format = "%05I64d";
195     r = p_sprintf(buffer,format,(LONGLONG)100);
196     ok(!strcmp(buffer,"00100") && r==5,"05I64d failed: '%s'\n", buffer);
197 
198     format = "%05I64d";
199     r = p_sprintf(buffer,format,(LONGLONG)-100);
200     ok(!strcmp(buffer,"-0100") && r==5,"05I64d failed: '%s'\n", buffer);
201 
202     format = "% I64d";
203     r = p_sprintf(buffer,format,(LONGLONG)100);
204     ok(!strcmp(buffer," 100") && r==4,"' I64d' failed: '%s'\n", buffer);
205 
206     format = "% I64d";
207     r = p_sprintf(buffer,format,(LONGLONG)-100);
208     ok(!strcmp(buffer,"-100") && r==4,"' I64d' failed: '%s'\n", buffer);
209 
210     format = "% 5I64d";
211     r = p_sprintf(buffer,format,(LONGLONG)100);
212     ok(!strcmp(buffer,"  100") && r==5,"' 5I64d' failed: '%s'\n", buffer);
213 
214     format = "% 5I64d";
215     r = p_sprintf(buffer,format,(LONGLONG)-100);
216     ok(!strcmp(buffer," -100") && r==5,"' 5I64d' failed: '%s'\n", buffer);
217 
218     format = "% .5I64d";
219     r = p_sprintf(buffer,format,(LONGLONG)100);
220     ok(!strcmp(buffer," 00100") && r==6,"' .5I64d' failed: '%s'\n", buffer);
221 
222     format = "% .5I64d";
223     r = p_sprintf(buffer,format,(LONGLONG)-100);
224     ok(!strcmp(buffer,"-00100") && r==6,"' .5I64d' failed: '%s'\n", buffer);
225 
226     format = "% 8.5I64d";
227     r = p_sprintf(buffer,format,(LONGLONG)100);
228     ok(!strcmp(buffer,"   00100") && r==8,"' 8.5I64d' failed: '%s'\n", buffer);
229 
230     format = "% 8.5I64d";
231     r = p_sprintf(buffer,format,(LONGLONG)-100);
232     ok(!strcmp(buffer,"  -00100") && r==8,"' 8.5I64d' failed: '%s'\n", buffer);
233 
234     format = "%.0I64d";
235     r = p_sprintf(buffer,format,(LONGLONG)0);
236     ok(r==0,".0I64d failed: '%s'\n", buffer);
237 
238     format = "%#+21.18I64x";
239     r = p_sprintf(buffer,format,(LONGLONG)-100);
240     ok(!strcmp(buffer," 0x00ffffffffffffff9c") && r==21,"#+21.18I64x failed: '%s'\n", buffer);
241 
242     format = "%#.25I64o";
243     r = p_sprintf(buffer,format,(LONGLONG)-100);
244     ok(!strcmp(buffer,"0001777777777777777777634") && r==25,"#.25I64o failed: '%s'\n", buffer);
245 
246     format = "%#+24.20I64o";
247     r = p_sprintf(buffer,format,(LONGLONG)-100);
248     ok(!strcmp(buffer," 01777777777777777777634") && r==24,"#+24.20I64o failed: '%s'\n", buffer);
249 
250     format = "%#+18.21I64X";
251     r = p_sprintf(buffer,format,(LONGLONG)-100);
252     ok(!strcmp(buffer,"0X00000FFFFFFFFFFFFFF9C") && r==23,"#+18.21I64X failed: '%s '\n", buffer);
253 
254     format = "%#+20.24I64o";
255     r = p_sprintf(buffer,format,(LONGLONG)-100);
256     ok(!strcmp(buffer,"001777777777777777777634") && r==24,"#+20.24I64o failed: '%s'\n", buffer);
257 
258     format = "%#+25.22I64u";
259     r = p_sprintf(buffer,format,(LONGLONG)-1);
260     ok(!strcmp(buffer,"   0018446744073709551615") && r==25,"#+25.22I64u conversion failed: '%s'\n", buffer);
261 
262     format = "%#+25.22I64u";
263     r = p_sprintf(buffer,format,(LONGLONG)-1);
264     ok(!strcmp(buffer,"   0018446744073709551615") && r==25,"#+25.22I64u failed: '%s'\n", buffer);
265 
266     format = "%#+30.25I64u";
267     r = p_sprintf(buffer,format,(LONGLONG)-1);
268     ok(!strcmp(buffer,"     0000018446744073709551615") && r==30,"#+30.25I64u failed: '%s'\n", buffer);
269 
270     format = "%+#25.22I64d";
271     r = p_sprintf(buffer,format,(LONGLONG)-1);
272     ok(!strcmp(buffer,"  -0000000000000000000001") && r==25,"+#25.22I64d failed: '%s'\n", buffer);
273 
274     format = "%#-8.5I64o";
275     r = p_sprintf(buffer,format,(LONGLONG)100);
276     ok(!strcmp(buffer,"00144   ") && r==8,"-8.5I64o failed: '%s'\n", buffer);
277 
278     format = "%#-+ 08.5I64d";
279     r = p_sprintf(buffer,format,(LONGLONG)100);
280     ok(!strcmp(buffer,"+00100  ") && r==8,"'#-+ 08.5I64d failed: '%s'\n", buffer);
281 
282     format = "%#-+ 08.5I64d";
283     r = p_sprintf(buffer,format,(LONGLONG)100);
284     ok(!strcmp(buffer,"+00100  ") && r==8,"#-+ 08.5I64d failed: '%s'\n", buffer);
285 
286     format = "%.80I64d";
287     r = p_sprintf(buffer,format,(LONGLONG)1);
288     ok(r==80,"%s format failed\n", format);
289 
290     format = "% .80I64d";
291     r = p_sprintf(buffer,format,(LONGLONG)1);
292     ok(r==81,"%s format failed\n", format);
293 
294     format = "% .80d";
295     r = p_sprintf(buffer,format,1);
296     ok(r==81,"%s format failed\n", format);
297 
298     format = "%lld";
299     r = p_sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
300     ok( r == 1 || r == 11, "return count wrong %d\n", r);
301     if (r == 11)  /* %ll works on Vista */
302         ok(!strcmp(buffer, "-8589934591"), "Problem with \"ll\" interpretation '%s'\n", buffer);
303     else
304         ok(!strcmp(buffer, "1"), "Problem with \"ll\" interpretation '%s'\n", buffer);
305 
306     format = "%I";
307     r = p_sprintf(buffer,format,1);
308     ok(!strcmp(buffer, "I"), "Problem with \"I\" interpretation\n");
309     ok( r==1, "return count wrong\n");
310 
311     format = "%I0d";
312     r = p_sprintf(buffer,format,1);
313     ok(!strcmp(buffer,"I0d"),"I0d failed\n");
314     ok( r==3, "return count wrong\n");
315 
316     format = "%I32d";
317     r = p_sprintf(buffer,format,1);
318     if (r == 1)
319     {
320         ok(!strcmp(buffer,"1"),"I32d failed, got '%s'\n",buffer);
321     }
322     else
323     {
324         /* Older versions don't grok I32 format */
325         ok(r == 4 && !strcmp(buffer,"I32d"),"I32d failed, got '%s',%d\n",buffer,r);
326     }
327 
328     format = "%I64D";
329     r = p_sprintf(buffer,format,(LONGLONG)-1);
330     ok(!strcmp(buffer,"D"),"I64D failed: %s\n",buffer);
331     ok( r==1, "return count wrong\n");
332 
333     format = "%zx";
334     r = p_sprintf(buffer,format,1);
335     ok(!strcmp(buffer, "zx"), "Problem with \"z\" interpretation\n");
336     ok( r==2, "return count wrong\n");
337 
338     format = "% d";
339     r = p_sprintf(buffer,format,1);
340     ok(!strcmp(buffer, " 1"),"Problem with sign place-holder: '%s'\n",buffer);
341     ok( r==2, "return count wrong\n");
342 
343     format = "%+ d";
344     r = p_sprintf(buffer,format,1);
345     ok(!strcmp(buffer, "+1"),"Problem with sign flags: '%s'\n",buffer);
346     ok( r==2, "return count wrong\n");
347 
348     format = "%S";
349     r = p_sprintf(buffer,format,wide);
350     ok(!strcmp(buffer,"wide"),"Problem with wide string format\n");
351     ok( r==4, "return count wrong\n");
352 
353     format = "%04c";
354     r = p_sprintf(buffer,format,'1');
355     ok(!strcmp(buffer,"0001"),"Character not zero-prefixed \"%s\"\n",buffer);
356     ok( r==4, "return count wrong\n");
357 
358     format = "%-04c";
359     r = p_sprintf(buffer,format,'1');
360     ok(!strcmp(buffer,"1   "),"Character zero-padded and/or not left-adjusted \"%s\"\n",buffer);
361     ok( r==4, "return count wrong\n");
362 
363     format = "%#012x";
364     r = p_sprintf(buffer,format,1);
365     ok(!strcmp(buffer,"0x0000000001"),"Hexadecimal zero-padded \"%s\"\n",buffer);
366     ok( r==12, "return count wrong\n");
367 
368     r = p_sprintf(buffer,format,0);
369     ok(!strcmp(buffer,"000000000000"),"Hexadecimal zero-padded \"%s\"\n",buffer);
370     ok( r==12, "return count wrong\n");
371 
372     format = "%#04.8x";
373     r = p_sprintf(buffer,format,1);
374     ok(!strcmp(buffer,"0x00000001"), "Hexadecimal zero-padded precision \"%s\"\n",buffer);
375     ok( r==10, "return count wrong\n");
376 
377     r = p_sprintf(buffer,format,0);
378     ok(!strcmp(buffer,"00000000"), "Hexadecimal zero-padded precision \"%s\"\n",buffer);
379     ok( r==8, "return count wrong\n");
380 
381     format = "%#-08.2x";
382     r = p_sprintf(buffer,format,1);
383     ok(!strcmp(buffer,"0x01    "), "Hexadecimal zero-padded not left-adjusted \"%s\"\n",buffer);
384     ok( r==8, "return count wrong\n");
385 
386     r = p_sprintf(buffer,format,0);
387     ok(!strcmp(buffer,"00      "), "Hexadecimal zero-padded not left-adjusted \"%s\"\n",buffer);
388     ok( r==8, "return count wrong\n");
389 
390     format = "%#.0x";
391     r = p_sprintf(buffer,format,1);
392     ok(!strcmp(buffer,"0x1"), "Hexadecimal zero-padded zero-precision \"%s\"\n",buffer);
393     ok( r==3, "return count wrong\n");
394 
395     r = p_sprintf(buffer,format,0);
396     ok(!strcmp(buffer,""), "Hexadecimal zero-padded zero-precision \"%s\"\n",buffer);
397     ok( r==0, "return count wrong\n");
398 
399     format = "%#08o";
400     r = p_sprintf(buffer,format,1);
401     ok(!strcmp(buffer,"00000001"), "Octal zero-padded \"%s\"\n",buffer);
402     ok( r==8, "return count wrong\n");
403 
404     format = "%#o";
405     r = p_sprintf(buffer,format,1);
406     ok(!strcmp(buffer,"01"), "Octal zero-padded \"%s\"\n",buffer);
407     ok( r==2, "return count wrong\n");
408 
409     r = p_sprintf(buffer,format,0);
410     ok(!strcmp(buffer,"0"), "Octal zero-padded \"%s\"\n",buffer);
411     ok( r==1, "return count wrong\n");
412 
413     if (sizeof(void *) == 8)
414     {
415         format = "%p";
416         r = p_sprintf(buffer,format,(void *)57);
417         ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
418         ok( r==16, "return count wrong\n");
419 
420         format = "%#020p";
421         r = p_sprintf(buffer,format,(void *)57);
422         ok(!strcmp(buffer,"  0X0000000000000039"),"Pointer formatted incorrectly\n");
423         ok( r==20, "return count wrong\n");
424 
425         format = "%Fp";
426         r = p_sprintf(buffer,format,(void *)57);
427         ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
428         ok( r==16, "return count wrong\n");
429 
430         format = "%Np";
431         r = p_sprintf(buffer,format,(void *)57);
432         ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
433         ok( r==16, "return count wrong\n");
434 
435         format = "%#-020p";
436         r = p_sprintf(buffer,format,(void *)57);
437         ok(!strcmp(buffer,"0X0000000000000039  "),"Pointer formatted incorrectly\n");
438         ok( r==20, "return count wrong\n");
439 
440         format = "%Ix %d";
441         r = p_sprintf(buffer,format,(size_t)0x12345678123456,1);
442         ok(!strcmp(buffer,"12345678123456 1"),"buffer = %s\n",buffer);
443         ok( r==16, "return count wrong\n");
444     }
445     else
446     {
447         format = "%p";
448         r = p_sprintf(buffer,format,(void *)57);
449         ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
450         ok( r==8, "return count wrong\n");
451 
452         format = "%#012p";
453         r = p_sprintf(buffer,format,(void *)57);
454         ok(!strcmp(buffer,"  0X00000039"),"Pointer formatted incorrectly\n");
455         ok( r==12, "return count wrong\n");
456 
457         format = "%Fp";
458         r = p_sprintf(buffer,format,(void *)57);
459         ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
460         ok( r==8, "return count wrong\n");
461 
462         format = "%Np";
463         r = p_sprintf(buffer,format,(void *)57);
464         ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
465         ok( r==8, "return count wrong\n");
466 
467         format = "%#-012p";
468         r = p_sprintf(buffer,format,(void *)57);
469         ok(!strcmp(buffer,"0X00000039  "),"Pointer formatted incorrectly\n");
470         ok( r==12, "return count wrong\n");
471 
472         format = "%Ix %d";
473         r = p_sprintf(buffer,format,0x123456,1);
474         ok(!strcmp(buffer,"123456 1"),"buffer = %s\n",buffer);
475         ok( r==8, "return count wrong\n");
476     }
477 
478     format = "%04s";
479     r = p_sprintf(buffer,format,"foo");
480     ok(!strcmp(buffer,"0foo"),"String not zero-prefixed \"%s\"\n",buffer);
481     ok( r==4, "return count wrong\n");
482 
483     format = "%.1s";
484     r = p_sprintf(buffer,format,"foo");
485     ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
486     ok( r==1, "return count wrong\n");
487 
488     format = "%.*s";
489     r = p_sprintf(buffer,format,1,"foo");
490     ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
491     ok( r==1, "return count wrong\n");
492 
493     format = "%*s";
494     r = p_sprintf(buffer,format,-5,"foo");
495     ok(!strcmp(buffer,"foo  "),"Negative field width ignored \"%s\"\n",buffer);
496     ok( r==5, "return count wrong\n");
497 
498     format = "hello";
499     r = p_sprintf(buffer, format);
500     ok(!strcmp(buffer,"hello"), "failed\n");
501     ok( r==5, "return count wrong\n");
502 
503     format = "%ws";
504     r = p_sprintf(buffer, format, wide);
505     ok(!strcmp(buffer,"wide"), "failed\n");
506     ok( r==4, "return count wrong\n");
507 
508     format = "%-10ws";
509     r = p_sprintf(buffer, format, wide );
510     ok(!strcmp(buffer,"wide      "), "failed\n");
511     ok( r==10, "return count wrong\n");
512 
513     format = "%10ws";
514     r = p_sprintf(buffer, format, wide );
515     ok(!strcmp(buffer,"      wide"), "failed\n");
516     ok( r==10, "return count wrong\n");
517 
518     format = "%#+ -03whlls";
519     r = p_sprintf(buffer, format, wide );
520     ok(!strcmp(buffer,"wide"), "failed\n");
521     ok( r==4, "return count wrong\n");
522 
523     format = "%w0s";
524     r = p_sprintf(buffer, format, wide );
525     ok(!strcmp(buffer,"0s"), "failed\n");
526     ok( r==2, "return count wrong\n");
527 
528     format = "%w-s";
529     r = p_sprintf(buffer, format, wide );
530     ok(!strcmp(buffer,"-s"), "failed\n");
531     ok( r==2, "return count wrong\n");
532 
533     format = "%ls";
534     r = p_sprintf(buffer, format, wide );
535     ok(!strcmp(buffer,"wide"), "failed\n");
536     ok( r==4, "return count wrong\n");
537 
538     format = "%Ls";
539     r = p_sprintf(buffer, format, "not wide" );
540     ok(!strcmp(buffer,"not wide"), "failed\n");
541     ok( r==8, "return count wrong\n");
542 
543     format = "%b";
544     r = p_sprintf(buffer, format);
545     ok(!strcmp(buffer,"b"), "failed\n");
546     ok( r==1, "return count wrong\n");
547 
548     format = "%3c";
549     r = p_sprintf(buffer, format,'a');
550     ok(!strcmp(buffer,"  a"), "failed\n");
551     ok( r==3, "return count wrong\n");
552 
553     format = "%3d";
554     r = p_sprintf(buffer, format,1234);
555     ok(!strcmp(buffer,"1234"), "failed\n");
556     ok( r==4, "return count wrong\n");
557 
558     format = "%3h";
559     r = p_sprintf(buffer, format);
560     ok(!strcmp(buffer,""), "failed\n");
561     ok( r==0, "return count wrong\n");
562 
563     format = "%j%k%m%q%r%t%v%y%z";
564     r = p_sprintf(buffer, format);
565     ok(!strcmp(buffer,"jkmqrtvyz"), "failed\n");
566     ok( r==9, "return count wrong\n");
567 
568     format = "asdf%n";
569     x = 0;
570     r = p_sprintf(buffer, format, &x );
571     if (r == -1)
572     {
573         /* %n format is disabled by default on vista */
574         /* FIXME: should test with _set_printf_count_output */
575         ok(x == 0, "should not write to x: %d\n", x);
576     }
577     else
578     {
579         ok(x == 4, "should write to x: %d\n", x);
580         ok(!strcmp(buffer,"asdf"), "failed\n");
581         ok( r==4, "return count wrong: %d\n", r);
582     }
583 
584     format = "%-1d";
585     r = p_sprintf(buffer, format,2);
586     ok(!strcmp(buffer,"2"), "failed\n");
587     ok( r==1, "return count wrong\n");
588 
589     format = "%2.4f";
590     r = p_sprintf(buffer, format,8.6);
591     ok(!strcmp(buffer,"8.6000"), "failed\n");
592     ok( r==6, "return count wrong\n");
593 
594     format = "%0f";
595     r = p_sprintf(buffer, format,0.6);
596     ok(!strcmp(buffer,"0.600000"), "failed\n");
597     ok( r==8, "return count wrong\n");
598 
599     format = "%.0f";
600     r = p_sprintf(buffer, format,0.6);
601     ok(!strcmp(buffer,"1"), "failed\n");
602     ok( r==1, "return count wrong\n");
603 
604     format = "%2.4e";
605     r = p_sprintf(buffer, format,8.6);
606     ok(!strcmp(buffer,"8.6000e+000"), "failed\n");
607     ok( r==11, "return count wrong\n");
608 
609     format = "% 2.4e";
610     r = p_sprintf(buffer, format,8.6);
611     ok(!strcmp(buffer," 8.6000e+000"), "failed: %s\n", buffer);
612     ok( r==12, "return count wrong\n");
613 
614     format = "% 014.4e";
615     r = p_sprintf(buffer, format,8.6);
616     ok(!strcmp(buffer," 008.6000e+000"), "failed: %s\n", buffer);
617     ok( r==14, "return count wrong\n");
618 
619     format = "% 2.4e";
620     r = p_sprintf(buffer, format,-8.6);
621     ok(!strcmp(buffer,"-8.6000e+000"), "failed: %s\n", buffer);
622     ok( r==12, "return count wrong\n");
623 
624     format = "%+2.4e";
625     r = p_sprintf(buffer, format,8.6);
626     ok(!strcmp(buffer,"+8.6000e+000"), "failed: %s\n", buffer);
627     ok( r==12, "return count wrong\n");
628 
629     format = "%2.4g";
630     r = p_sprintf(buffer, format,8.6);
631     ok(!strcmp(buffer,"8.6"), "failed\n");
632     ok( r==3, "return count wrong\n");
633 
634     format = "%-i";
635     r = p_sprintf(buffer, format,-1);
636     ok(!strcmp(buffer,"-1"), "failed\n");
637     ok( r==2, "return count wrong\n");
638 
639     format = "%-i";
640     r = p_sprintf(buffer, format,1);
641     ok(!strcmp(buffer,"1"), "failed\n");
642     ok( r==1, "return count wrong\n");
643 
644     format = "%+i";
645     r = p_sprintf(buffer, format,1);
646     ok(!strcmp(buffer,"+1"), "failed\n");
647     ok( r==2, "return count wrong\n");
648 
649     format = "%o";
650     r = p_sprintf(buffer, format,10);
651     ok(!strcmp(buffer,"12"), "failed\n");
652     ok( r==2, "return count wrong\n");
653 
654     format = "%p";
655     r = p_sprintf(buffer, format,0);
656     if (sizeof(void *) == 8)
657     {
658         ok(!strcmp(buffer,"0000000000000000"), "failed\n");
659         ok( r==16, "return count wrong\n");
660     }
661     else
662     {
663         ok(!strcmp(buffer,"00000000"), "failed\n");
664         ok( r==8, "return count wrong\n");
665     }
666 
667     format = "%s";
668     r = p_sprintf(buffer, format,0);
669     ok(!strcmp(buffer,"(null)"), "failed\n");
670     ok( r==6, "return count wrong\n");
671 
672     format = "%s";
673     r = p_sprintf(buffer, format,"%%%%");
674     ok(!strcmp(buffer,"%%%%"), "failed\n");
675     ok( r==4, "return count wrong\n");
676 
677     format = "%u";
678     r = p_sprintf(buffer, format,-1);
679     ok(!strcmp(buffer,"4294967295"), "failed\n");
680     ok( r==10, "return count wrong\n");
681 
682     format = "%w";
683     r = p_sprintf(buffer, format,-1);
684     ok(!strcmp(buffer,""), "failed\n");
685     ok( r==0, "return count wrong\n");
686 
687     format = "%h";
688     r = p_sprintf(buffer, format,-1);
689     ok(!strcmp(buffer,""), "failed\n");
690     ok( r==0, "return count wrong\n");
691 
692     format = "%z";
693     r = p_sprintf(buffer, format,-1);
694     ok(!strcmp(buffer,"z"), "failed\n");
695     ok( r==1, "return count wrong\n");
696 
697     format = "%j";
698     r = p_sprintf(buffer, format,-1);
699     ok(!strcmp(buffer,"j"), "failed\n");
700     ok( r==1, "return count wrong\n");
701 
702     format = "%F";
703     r = p_sprintf(buffer, format,-1);
704     ok(!strcmp(buffer,""), "failed\n");
705     ok( r==0, "return count wrong\n");
706 
707     format = "%N";
708     r = p_sprintf(buffer, format,-1);
709     ok(!strcmp(buffer,""), "failed\n");
710     ok( r==0, "return count wrong\n");
711 
712     format = "%H";
713     r = p_sprintf(buffer, format,-1);
714     ok(!strcmp(buffer,"H"), "failed\n");
715     ok( r==1, "return count wrong\n");
716 
717     format = "x%cx";
718     r = p_sprintf(buffer, format, 0x100+'X');
719     ok(!strcmp(buffer,"xXx"), "failed\n");
720     ok( r==3, "return count wrong\n");
721 
722     format = "%%0";
723     r = p_sprintf(buffer, format);
724     ok(!strcmp(buffer,"%0"), "failed: \"%s\"\n", buffer);
725     ok( r==2, "return count wrong\n");
726 
727     format = "%hx";
728     r = p_sprintf(buffer, format, 0x12345);
729     ok(!strcmp(buffer,"2345"), "failed \"%s\"\n", buffer);
730 
731     format = "%hhx";
732     r = p_sprintf(buffer, format, 0x123);
733     ok(!strcmp(buffer,"123"), "failed: \"%s\"\n", buffer);
734     r = p_sprintf(buffer, format, 0x12345);
735     ok(!strcmp(buffer,"2345"), "failed \"%s\"\n", buffer);
736 
737     format = "%lf";
738     r = p_sprintf(buffer, format, IND);
739     ok(r==9, "r = %d\n", r);
740     ok(!strcmp(buffer, "-1.#IND00"), "failed: \"%s\"\n", buffer);
741     r = p_sprintf(buffer, format, NAN);
742     ok(r==8, "r = %d\n", r);
743     ok(!strcmp(buffer, "1.#QNAN0"), "failed: \"%s\"\n", buffer);
744     r = p_sprintf(buffer, format, INFINITY);
745     ok(r==8, "r = %d\n", r);
746     ok(!strcmp(buffer, "1.#INF00"), "failed: \"%s\"\n", buffer);
747 
748     format = "%le";
749     r = p_sprintf(buffer, format, IND);
750     ok(r==14, "r = %d\n", r);
751     ok(!strcmp(buffer, "-1.#IND00e+000"), "failed: \"%s\"\n", buffer);
752     r = p_sprintf(buffer, format, NAN);
753     ok(r==13, "r = %d\n", r);
754     ok(!strcmp(buffer, "1.#QNAN0e+000"), "failed: \"%s\"\n", buffer);
755     r = p_sprintf(buffer, format, INFINITY);
756     ok(r==13, "r = %d\n", r);
757     ok(!strcmp(buffer, "1.#INF00e+000"), "failed: \"%s\"\n", buffer);
758 
759     format = "%lg";
760     r = p_sprintf(buffer, format, IND);
761     ok(r==7, "r = %d\n", r);
762     ok(!strcmp(buffer, "-1.#IND"), "failed: \"%s\"\n", buffer);
763     r = p_sprintf(buffer, format, NAN);
764     ok(r==7, "r = %d\n", r);
765     ok(!strcmp(buffer, "1.#QNAN"), "failed: \"%s\"\n", buffer);
766     r = p_sprintf(buffer, format, INFINITY);
767     ok(r==6, "r = %d\n", r);
768     ok(!strcmp(buffer, "1.#INF"), "failed: \"%s\"\n", buffer);
769 
770     format = "%010.2lf";
771     r = p_sprintf(buffer, format, IND);
772     ok(r==10, "r = %d\n", r);
773     ok(!strcmp(buffer, "-000001.#J"), "failed: \"%s\"\n", buffer);
774     r = p_sprintf(buffer, format, NAN);
775     ok(r==10, "r = %d\n", r);
776     ok(!strcmp(buffer, "0000001.#R"), "failed: \"%s\"\n", buffer);
777     r = p_sprintf(buffer, format, INFINITY);
778     ok(r==10, "r = %d\n", r);
779     ok(!strcmp(buffer, "0000001.#J"), "failed: \"%s\"\n", buffer);
780 
781     format = "%c";
782     r = p_sprintf(buffer, format, 'a');
783     ok(r==1, "r = %d\n", r);
784     ok(!strcmp(buffer, "a"), "failed: \"%s\"\n", buffer);
785     r = p_sprintf(buffer, format, 0xa082);
786     ok(r==1, "r = %d\n", r);
787     ok(!strcmp(buffer, "\x82"), "failed: \"%s\"\n", buffer);
788 
789     format = "%C";
790     r = p_sprintf(buffer, format, 'a');
791     ok(r==1, "r = %d\n", r);
792     ok(!strcmp(buffer, "a"), "failed: \"%s\"\n", buffer);
793     r = p_sprintf(buffer, format, 0x3042);
794     ok(r==0, "r = %d\n", r);
795     ok(!strcmp(buffer, ""), "failed: \"%s\"\n", buffer);
796 
797     format = "a%Cb";
798     r = p_sprintf(buffer, format, 0x3042);
799     ok(r==2, "r = %d\n", r);
800     ok(!strcmp(buffer, "ab"), "failed: \"%s\"\n", buffer);
801 
802     format = "%S";
803     buf_w[0] = 0x3042;
804     buf_w[1] = 0;
805     r = p_sprintf(buffer, format, buf_w);
806     ok(r==-1 || broken(!r), "r = %d\n", r);
807 
808     if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
809         win_skip("Japanese_Japan.932 locale not available\n");
810         return;
811     }
812 
813     format = "%c";
814     r = p_sprintf(buffer, format, 0xa082);
815     ok(r==1, "r = %d\n", r);
816     ok(!strcmp(buffer, "\x82"), "failed: \"%s\"\n", buffer);
817 
818     format = "%C";
819     r = p_sprintf(buffer, format, 0x3042);
820     ok(r==2, "r = %d\n", r);
821     ok(!strcmp(buffer, "\x82\xa0"), "failed: \"%s\"\n", buffer);
822 
823     strcpy(buffer, " string to copy");
824     r = p_sprintf(buffer, buffer+1);
825     ok(r==14, "r = %d\n", r);
826     ok(!strcmp(buffer, "string to copy"), "failed: \"%s\"\n", buffer);
827 
828     setlocale(LC_ALL, "C");
829 }
830 
831 static void test_swprintf( void )
832 {
833     wchar_t buffer[100];
834     const wchar_t I64d[] = {'%','I','6','4','d',0};
835     double pnumber=789456123;
836     const wchar_t TwentyThreePoint15e[]= {'%','+','#','2','3','.','1','5','e',0};
837     const wchar_t e008[] = {'e','+','0','0','8',0};
838     const wchar_t string_w[] = {'s','t','r','i','n','g',0};
839     const char string[] = "string";
840     const wchar_t S[]={'%','S',0};
841     const wchar_t hs[] = {'%', 'h', 's', 0};
842 
843     swprintf(buffer,TwentyThreePoint15e,pnumber);
844     ok(wcsstr(buffer,e008) != 0,"Sprintf different\n");
845     swprintf(buffer,I64d,((ULONGLONG)0xffffffff)*0xffffffff);
846       ok(wcslen(buffer) == 11,"Problem with long long\n");
847     swprintf(buffer,S,string);
848       ok(wcslen(buffer) == 6,"Problem with \"%%S\" interpretation\n");
849    swprintf(buffer, hs, string);
850    ok( wcscmp(string_w,buffer) == 0, "swprintf failed with %%hs\n");
851 }
852 
853 static void test_snprintf (void)
854 {
855     struct snprintf_test {
856         const char *format;
857         int expected;
858     };
859     /* Pre-2.1 libc behaviour, not C99 compliant. */
860     const struct snprintf_test tests[] = {{"short", 5},
861                                           {"justfit", 7},
862                                           {"justfits", 8},
863                                           {"muchlonger", -1}};
864     char buffer[8];
865     const int bufsiz = sizeof buffer;
866     unsigned int i;
867 
868     for (i = 0; i < ARRAY_SIZE(tests); i++) {
869         const char *fmt  = tests[i].format;
870         const int expect = tests[i].expected;
871         const int n      = _snprintf (buffer, bufsiz, fmt);
872         const int valid  = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
873 
874         ok (n == expect, "\"%s\": expected %d, returned %d\n",
875             fmt, expect, n);
876         ok (!memcmp (fmt, buffer, valid),
877             "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
878     }
879 }
880 
881 static void test_fprintf(void)
882 {
883     static const char file_name[] = "fprintf.tst";
884     static const WCHAR utf16_test[] = {'u','n','i','c','o','d','e','\n',0};
885 
886     FILE *fp = fopen(file_name, "wb");
887     char buf[1024];
888     int ret;
889 
890     ret = fprintf(fp, "simple test\n");
891     ok(ret == 12, "ret = %d\n", ret);
892     ret = ftell(fp);
893     ok(ret == 12, "ftell returned %d\n", ret);
894 
895     ret = fprintf(fp, "contains%cnull\n", '\0');
896     ok(ret == 14, "ret = %d\n", ret);
897     ret = ftell(fp);
898     ok(ret == 26, "ftell returned %d\n", ret);
899 
900     ret = fwprintf(fp, utf16_test);
901     ok(ret == 8, "ret = %d\n", ret);
902     ret = ftell(fp);
903     ok(ret == 42, "ftell returned %d\n", ret);
904 
905     fclose(fp);
906 
907     fp = fopen(file_name, "rb");
908     ret = fscanf(fp, "%[^\n] ", buf);
909     ok(ret == 1, "ret = %d\n", ret);
910     ret = ftell(fp);
911     ok(ret == 12, "ftell returned %d\n", ret);
912     ok(!strcmp(buf, "simple test"), "buf = %s\n", buf);
913 
914     fgets(buf, sizeof(buf), fp);
915     ret = ftell(fp);
916     ok(ret == 26, "ret = %d\n", ret);
917     ok(!memcmp(buf, "contains\0null\n", 14), "buf = %s\n", buf);
918 
919     memset(buf, 0, sizeof(buf));
920     fgets(buf, sizeof(buf), fp);
921     ret = ftell(fp);
922     ok(ret == 41, "ret =  %d\n", ret);
923     ok(!memcmp(buf, utf16_test, sizeof(utf16_test)),
924             "buf = %s\n", wine_dbgstr_w((WCHAR*)buf));
925 
926     fclose(fp);
927 
928     fp = fopen(file_name, "wt");
929 
930     ret = fprintf(fp, "simple test\n");
931     ok(ret == 12, "ret = %d\n", ret);
932     ret = ftell(fp);
933     ok(ret == 13, "ftell returned %d\n", ret);
934 
935     ret = fprintf(fp, "contains%cnull\n", '\0');
936     ok(ret == 14, "ret = %d\n", ret);
937     ret = ftell(fp);
938     ok(ret == 28, "ftell returned %d\n", ret);
939 
940     ret = fwprintf(fp, utf16_test);
941     ok(ret == 8, "ret = %d\n", ret);
942     ret = ftell(fp);
943     ok(ret == 37, "ftell returned %d\n", ret);
944 
945     fclose(fp);
946 
947     fp = fopen(file_name, "rb");
948     ret = fscanf(fp, "%[^\n] ", buf);
949     ok(ret == 1, "ret = %d\n", ret);
950     ret = ftell(fp);
951     ok(ret == 13, "ftell returned %d\n", ret);
952     ok(!strcmp(buf, "simple test\r"), "buf = %s\n", buf);
953 
954     fgets(buf, sizeof(buf), fp);
955     ret = ftell(fp);
956     ok(ret == 28, "ret = %d\n", ret);
957     ok(!memcmp(buf, "contains\0null\r\n", 15), "buf = %s\n", buf);
958 
959     fgets(buf, sizeof(buf), fp);
960     ret = ftell(fp);
961     ok(ret == 37, "ret =  %d\n", ret);
962     ok(!strcmp(buf, "unicode\r\n"), "buf = %s\n", buf);
963 
964     fclose(fp);
965     unlink(file_name);
966 }
967 
968 static void test_fcvt(void)
969 {
970     char *str;
971     int dec=100, sign=100;
972 
973     /* Numbers less than 1.0 with different precisions */
974     str = _fcvt(0.0001, 1, &dec, &sign );
975     ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
976     ok( -3 == dec, "dec wrong %d\n", dec);
977     ok( 0 == sign, "sign wrong\n");
978 
979     str = _fcvt(0.0001, -10, &dec, &sign );
980     ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
981     ok( -3 == dec, "dec wrong %d\n", dec);
982     ok( 0 == sign, "sign wrong\n");
983 
984     str = _fcvt(0.0001, 10, &dec, &sign );
985     ok( 0 == strcmp(str,"1000000"), "bad return '%s'\n", str);
986     ok( -3 == dec, "dec wrong %d\n", dec);
987     ok( 0 == sign, "sign wrong\n");
988 
989     /* Basic sign test */
990     str = _fcvt(-111.0001, 5, &dec, &sign );
991     ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
992     ok( 3 == dec, "dec wrong %d\n", dec);
993     ok( 1 == sign, "sign wrong\n");
994 
995     str = _fcvt(111.0001, 5, &dec, &sign );
996     ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
997     ok( 3 == dec, "dec wrong\n");
998     ok( 0 == sign, "sign wrong\n");
999 
1000     /* 0.0 with different precisions */
1001     str = _fcvt(0.0, 5, &dec, &sign );
1002     ok( 0 == strcmp(str,"00000"), "bad return '%s'\n", str);
1003     ok( 0 == dec, "dec wrong %d\n", dec);
1004     ok( 0 == sign, "sign wrong\n");
1005 
1006     str = _fcvt(0.0, 0, &dec, &sign );
1007     ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
1008     ok( 0 == dec, "dec wrong %d\n", dec);
1009     ok( 0 == sign, "sign wrong\n");
1010 
1011     str = _fcvt(0.0, -1, &dec, &sign );
1012     ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
1013     ok( 0 == dec, "dec wrong %d\n", dec);
1014     ok( 0 == sign, "sign wrong\n");
1015 
1016     /* Numbers > 1.0 with 0 or -ve precision */
1017     str = _fcvt(-123.0001, 0, &dec, &sign );
1018     ok( 0 == strcmp(str,"123"), "bad return '%s'\n", str);
1019     ok( 3 == dec, "dec wrong %d\n", dec);
1020     ok( 1 == sign, "sign wrong\n");
1021 
1022     str = _fcvt(-123.0001, -1, &dec, &sign );
1023     ok( 0 == strcmp(str,"12"), "bad return '%s'\n", str);
1024     ok( 3 == dec, "dec wrong %d\n", dec);
1025     ok( 1 == sign, "sign wrong\n");
1026 
1027     str = _fcvt(-123.0001, -2, &dec, &sign );
1028     ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
1029     ok( 3 == dec, "dec wrong %d\n", dec);
1030     ok( 1 == sign, "sign wrong\n");
1031 
1032     str = _fcvt(-123.0001, -3, &dec, &sign );
1033     ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
1034     ok( 3 == dec, "dec wrong %d\n", dec);
1035     ok( 1 == sign, "sign wrong\n");
1036 
1037     /* Numbers > 1.0, but with rounding at the point of precision */
1038     str = _fcvt(99.99, 1, &dec, &sign );
1039     ok( 0 == strcmp(str,"1000"), "bad return '%s'\n", str);
1040     ok( 3 == dec, "dec wrong %d\n", dec);
1041     ok( 0 == sign, "sign wrong\n");
1042 
1043     /* Numbers < 1.0 where rounding occurs at the point of precision */
1044     str = _fcvt(0.00636, 2, &dec, &sign );
1045     ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
1046     ok( -1 == dec, "dec wrong %d\n", dec);
1047     ok( 0 == sign, "sign wrong\n");
1048 
1049     str = _fcvt(0.00636, 3, &dec, &sign );
1050     ok( 0 == strcmp(str,"6"), "bad return '%s'\n", str);
1051     ok( -2 == dec, "dec wrong %d\n", dec);
1052     ok( 0 == sign, "sign wrong\n");
1053 
1054     str = _fcvt(0.09999999996, 2, &dec, &sign );
1055     ok( 0 == strcmp(str,"10"), "bad return '%s'\n", str);
1056     ok( 0 == dec, "dec wrong %d\n", dec);
1057     ok( 0 == sign, "sign wrong\n");
1058 
1059     str = _fcvt(0.6, 0, &dec, &sign );
1060     ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
1061     ok( 1 == dec, "dec wrong %d\n", dec);
1062     ok( 0 == sign, "sign wrong\n");
1063 }
1064 
1065 /* Don't test nrdigits < 0, msvcrt on Win9x and NT4 will corrupt memory by
1066  * writing outside allocated memory */
1067 static struct {
1068     double value;
1069     int nrdigits;
1070     const char *expstr_e;
1071     const char *expstr_f;
1072     int expdecpt_e;
1073     int expdecpt_f;
1074     int expsign;
1075 } test_cvt_testcases[] = {
1076     {          45.0,   2,        "45",           "4500",          2,      2,      0 },
1077     /* Numbers less than 1.0 with different precisions */
1078     {        0.0001,   1,         "1",               "",         -3,     -3,     0 },
1079     {        0.0001,  10,"1000000000",        "1000000",         -3,     -3,     0 },
1080     /* Basic sign test */
1081     {     -111.0001,   5,     "11100",       "11100010",          3,      3,     1 },
1082     {      111.0001,   5,     "11100",       "11100010",          3,      3,     0 },
1083     /* big numbers with low precision */
1084     {        3333.3,   2,        "33",         "333330",          4,      4,     0 },
1085     {999999999999.9,   3,       "100","999999999999900",         13,     12,     0 },
1086     /* 0.0 with different precisions */
1087     {           0.0,   5,     "00000",          "00000",          0,      0,     0 },
1088     {           0.0,   0,          "",               "",          0,      0,     0 },
1089     {           0.0,  -1,          "",               "",          0,      0,     0 },
1090     /* Numbers > 1.0 with 0 or -ve precision */
1091     {     -123.0001,   0,          "",            "123",          3,      3,     1 },
1092     {     -123.0001,  -1,          "",             "12",          3,      3,     1 },
1093     {     -123.0001,  -2,          "",              "1",          3,      3,     1 },
1094     {     -123.0001,  -3,          "",               "",          3,      3,     1 },
1095     /* Numbers > 1.0, but with rounding at the point of precision */
1096     {         99.99,   1,         "1",           "1000",          3,      3,     0 },
1097     /* Numbers < 1.0 where rounding occurs at the point of precision */
1098     {        0.0063,   2,        "63",              "1",         -2,     -1,     0 },
1099     {        0.0063,   3,        "630",             "6",         -2,     -2,     0 },
1100     { 0.09999999996,   2,        "10",             "10",          0,      0,     0 },
1101     {           0.6,   1,         "6",              "6",          0,      0,     0 },
1102     {           0.6,   0,          "",              "1",          1,      1,     0 },
1103     {           0.4,   0,          "",               "",          0,      0,     0 },
1104     {          0.49,   0,          "",               "",          0,      0,     0 },
1105     {          0.51,   0,          "",              "1",          1,      1,     0 },
1106     /* ask for ridiculous precision, ruin formatting this table */
1107     {           1.0,  30, "100000000000000000000000000000",
1108                       "1000000000000000000000000000000",          1,      1,      0},
1109     {           123456789012345678901.0,  30, "123456789012345680000000000000",
1110                       "123456789012345680000000000000000000000000000000000",         21,    21,      0},
1111     /* end marker */
1112     { 0, 0, "END"}
1113 };
1114 
1115 static void test_xcvt(void)
1116 {
1117     char *str;
1118     int i, decpt, sign, err;
1119 
1120     for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1121         decpt = sign = 100;
1122         str = _ecvt( test_cvt_testcases[i].value,
1123                 test_cvt_testcases[i].nrdigits,
1124                 &decpt,
1125                 &sign);
1126         ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15),
1127                "_ecvt() bad return, got \n'%s' expected \n'%s'\n", str,
1128               test_cvt_testcases[i].expstr_e);
1129         ok( decpt == test_cvt_testcases[i].expdecpt_e,
1130                 "_ecvt() decimal point wrong, got %d expected %d\n", decpt,
1131                 test_cvt_testcases[i].expdecpt_e);
1132         ok( sign == test_cvt_testcases[i].expsign,
1133                 "_ecvt() sign wrong, got %d expected %d\n", sign,
1134                 test_cvt_testcases[i].expsign);
1135     }
1136     for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1137         decpt = sign = 100;
1138         str = _fcvt( test_cvt_testcases[i].value,
1139                 test_cvt_testcases[i].nrdigits,
1140                 &decpt,
1141                 &sign);
1142         ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15),
1143                "_fcvt() bad return, got \n'%s' expected \n'%s'\n", str,
1144               test_cvt_testcases[i].expstr_f);
1145         ok( decpt == test_cvt_testcases[i].expdecpt_f,
1146                 "_fcvt() decimal point wrong, got %d expected %d\n", decpt,
1147                 test_cvt_testcases[i].expdecpt_f);
1148         ok( sign == test_cvt_testcases[i].expsign,
1149                 "_fcvt() sign wrong, got %d expected %d\n", sign,
1150                 test_cvt_testcases[i].expsign);
1151     }
1152 
1153     if (p__ecvt_s)
1154     {
1155         str = malloc(1024);
1156         for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1157             decpt = sign = 100;
1158             err = p__ecvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign);
1159             ok(err == 0, "_ecvt_s() failed with error code %d\n", err);
1160             ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15),
1161                    "_ecvt_s() bad return, got \n'%s' expected \n'%s'\n", str,
1162                   test_cvt_testcases[i].expstr_e);
1163             ok( decpt == test_cvt_testcases[i].expdecpt_e,
1164                     "_ecvt_s() decimal point wrong, got %d expected %d\n", decpt,
1165                     test_cvt_testcases[i].expdecpt_e);
1166             ok( sign == test_cvt_testcases[i].expsign,
1167                     "_ecvt_s() sign wrong, got %d expected %d\n", sign,
1168                     test_cvt_testcases[i].expsign);
1169         }
1170         free(str);
1171     }
1172     else
1173         win_skip("_ecvt_s not available\n");
1174 
1175     if (p__fcvt_s)
1176     {
1177         int i;
1178 
1179         str = malloc(1024);
1180 
1181         /* invalid arguments */
1182         err = p__fcvt_s(NULL, 0, 0.0, 0, &i, &i);
1183         ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1184 
1185         err = p__fcvt_s(str, 0, 0.0, 0, &i, &i);
1186         ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1187 
1188         str[0] = ' ';
1189         str[1] = 0;
1190         err = p__fcvt_s(str, -1, 0.0, 0, &i, &i);
1191         ok(err == 0, "got %d, expected 0\n", err);
1192         ok(str[0] == 0, "got %c, expected 0\n", str[0]);
1193         ok(str[1] == 0, "got %c, expected 0\n", str[1]);
1194 
1195         err = p__fcvt_s(str, 1, 0.0, 0, NULL, &i);
1196         ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1197 
1198         err = p__fcvt_s(str, 1, 0.0, 0, &i, NULL);
1199         ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1200 
1201         for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1202             decpt = sign = 100;
1203             err = p__fcvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign);
1204             ok(err == 0, "_fcvt_s() failed with error code %d\n", err);
1205             ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15),
1206                    "_fcvt_s() bad return, got '%s' expected '%s'. test %d\n", str,
1207                   test_cvt_testcases[i].expstr_f, i);
1208             ok( decpt == test_cvt_testcases[i].expdecpt_f,
1209                     "_fcvt_s() decimal point wrong, got %d expected %d\n", decpt,
1210                     test_cvt_testcases[i].expdecpt_f);
1211             ok( sign == test_cvt_testcases[i].expsign,
1212                     "_fcvt_s() sign wrong, got %d expected %d\n", sign,
1213                     test_cvt_testcases[i].expsign);
1214         }
1215         free(str);
1216     }
1217     else
1218         win_skip("_fcvt_s not available\n");
1219 }
1220 
1221 static int WINAPIV _vsnwprintf_wrapper(wchar_t *str, size_t len, const wchar_t *format, ...)
1222 {
1223     int ret;
1224     __ms_va_list valist;
1225     __ms_va_start(valist, format);
1226     ret = _vsnwprintf(str, len, format, valist);
1227     __ms_va_end(valist);
1228     return ret;
1229 }
1230 
1231 static void test_vsnwprintf(void)
1232 {
1233     const wchar_t format[] = {'%','w','s','%','w','s','%','w','s',0};
1234     const wchar_t one[]    = {'o','n','e',0};
1235     const wchar_t two[]    = {'t','w','o',0};
1236     const wchar_t three[]  = {'t','h','r','e','e',0};
1237 
1238     int ret;
1239     wchar_t str[32];
1240     char buf[32];
1241 
1242     ret = _vsnwprintf_wrapper( str, ARRAY_SIZE(str), format, one, two, three );
1243 
1244     ok( ret == 11, "got %d expected 11\n", ret );
1245     WideCharToMultiByte( CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL );
1246     ok( !strcmp(buf, "onetwothree"), "got %s expected 'onetwothree'\n", buf );
1247 
1248     ret = _vsnwprintf_wrapper( str, 0, format, one, two, three );
1249     ok( ret == -1, "got %d, expected -1\n", ret );
1250 
1251     ret = _vsnwprintf_wrapper( NULL, 0, format, one, two, three );
1252     ok( ret == 11 || broken(ret == -1 /* Win2k */), "got %d, expected 11\n", ret );
1253 }
1254 
1255 static int WINAPIV vswprintf_wrapper(wchar_t *str, const wchar_t *format, ...)
1256 {
1257     int ret;
1258     __ms_va_list valist;
1259     __ms_va_start(valist, format);
1260     ret = p_vswprintf(str, format, valist);
1261     __ms_va_end(valist);
1262     return ret;
1263 }
1264 
1265 static int WINAPIV _vswprintf_wrapper(wchar_t *str, const wchar_t *format, ...)
1266 {
1267     int ret;
1268     __ms_va_list valist;
1269     __ms_va_start(valist, format);
1270     ret = p__vswprintf(str, format, valist);
1271     __ms_va_end(valist);
1272     return ret;
1273 }
1274 
1275 static int WINAPIV _vswprintf_l_wrapper(wchar_t *str, const wchar_t *format, void *locale, ...)
1276 {
1277     int ret;
1278     __ms_va_list valist;
1279     __ms_va_start(valist, locale);
1280     ret = p__vswprintf_l(str, format, locale, valist);
1281     __ms_va_end(valist);
1282     return ret;
1283 }
1284 
1285 static int WINAPIV _vswprintf_c_wrapper(wchar_t *str, size_t size, const wchar_t *format, ...)
1286 {
1287     int ret;
1288     __ms_va_list valist;
1289     __ms_va_start(valist, format);
1290     ret = p__vswprintf_c(str, size, format, valist);
1291     __ms_va_end(valist);
1292     return ret;
1293 }
1294 
1295 static int WINAPIV _vswprintf_c_l_wrapper(wchar_t *str, size_t size, const wchar_t *format, void *locale, ...)
1296 {
1297     int ret;
1298     __ms_va_list valist;
1299     __ms_va_start(valist, locale);
1300     ret = p__vswprintf_c_l(str, size, format, locale, valist);
1301     __ms_va_end(valist);
1302     return ret;
1303 }
1304 
1305 static int WINAPIV _vswprintf_p_l_wrapper(wchar_t *str, size_t size, const wchar_t *format, void *locale, ...)
1306 {
1307     int ret;
1308     __ms_va_list valist;
1309     __ms_va_start(valist, locale);
1310     ret = p__vswprintf_p_l(str, size, format, locale, valist);
1311     __ms_va_end(valist);
1312     return ret;
1313 }
1314 
1315 static void test_vswprintf(void)
1316 {
1317     const wchar_t format[] = {'%','s',' ','%','d',0};
1318     const wchar_t number[] = {'n','u','m','b','e','r',0};
1319     const wchar_t out[] = {'n','u','m','b','e','r',' ','1','2','3',0};
1320     wchar_t buf[20];
1321 
1322     int ret;
1323 
1324     if (!p_vswprintf || !p__vswprintf || !p__vswprintf_l ||!p__vswprintf_c
1325             || !p__vswprintf_c_l || !p__vswprintf_p_l)
1326     {
1327         win_skip("_vswprintf or vswprintf not available\n");
1328         return;
1329     }
1330 
1331     ret = vswprintf_wrapper(buf, format, number, 123);
1332     ok(ret == 10, "got %d, expected 10\n", ret);
1333     ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1334 
1335     memset(buf, 0, sizeof(buf));
1336     ret = _vswprintf_wrapper(buf, format, number, 123);
1337     ok(ret == 10, "got %d, expected 10\n", ret);
1338     ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1339 
1340     memset(buf, 0, sizeof(buf));
1341     ret = _vswprintf_l_wrapper(buf, format, NULL, number, 123);
1342     ok(ret == 10, "got %d, expected 10\n", ret);
1343     ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1344 
1345     memset(buf, 0, sizeof(buf));
1346     ret = _vswprintf_c_wrapper(buf, 20, format, number, 123);
1347     ok(ret == 10, "got %d, expected 10\n", ret);
1348     ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1349 
1350     memset(buf, 0, sizeof(buf));
1351     ret = _vswprintf_c_l_wrapper(buf, 20, format, NULL, number, 123);
1352     ok(ret == 10, "got %d, expected 10\n", ret);
1353     ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1354 
1355     memset(buf, 0, sizeof(buf));
1356     ret = _vswprintf_p_l_wrapper(buf, 20, format, NULL, number, 123);
1357     ok(ret == 10, "got %d, expected 10\n", ret);
1358     ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1359 }
1360 
1361 static int WINAPIV _vscprintf_wrapper(const char *format, ...)
1362 {
1363     int ret;
1364     __ms_va_list valist;
1365     __ms_va_start(valist, format);
1366     ret = p__vscprintf(format, valist);
1367     __ms_va_end(valist);
1368     return ret;
1369 }
1370 
1371 static void test_vscprintf(void)
1372 {
1373     int ret;
1374 
1375     if (!p__vscprintf)
1376     {
1377        win_skip("_vscprintf not available\n");
1378        return;
1379     }
1380 
1381     ret = _vscprintf_wrapper( "%s %d", "number", 1 );
1382     ok( ret == 8, "got %d expected 8\n", ret );
1383 }
1384 
1385 static int WINAPIV _vscwprintf_wrapper(const wchar_t *format, ...)
1386 {
1387     int ret;
1388     __ms_va_list valist;
1389     __ms_va_start(valist, format);
1390     ret = p__vscwprintf(format, valist);
1391     __ms_va_end(valist);
1392     return ret;
1393 }
1394 
1395 static void test_vscwprintf(void)
1396 {
1397     const wchar_t format[] = {'%','s',' ','%','d',0};
1398     const wchar_t number[] = {'n','u','m','b','e','r',0};
1399 
1400     int ret;
1401 
1402     if (!p__vscwprintf)
1403     {
1404         win_skip("_vscwprintf not available\n");
1405         return;
1406     }
1407 
1408     ret = _vscwprintf_wrapper( format, number, 1 );
1409     ok( ret == 8, "got %d expected 8\n", ret );
1410 }
1411 
1412 static int WINAPIV _vsnwprintf_s_wrapper(wchar_t *str, size_t sizeOfBuffer,
1413                                  size_t count, const wchar_t *format, ...)
1414 {
1415     int ret;
1416     __ms_va_list valist;
1417     __ms_va_start(valist, format);
1418     ret = p__vsnwprintf_s(str, sizeOfBuffer, count, format, valist);
1419     __ms_va_end(valist);
1420     return ret;
1421 }
1422 
1423 static void test_vsnwprintf_s(void)
1424 {
1425     const wchar_t format[] = { 'A','B','%','u','C',0 };
1426     const wchar_t out7[] = { 'A','B','1','2','3','C',0 };
1427     const wchar_t out6[] = { 'A','B','1','2','3',0 };
1428     const wchar_t out2[] = { 'A',0 };
1429     const wchar_t out1[] = { 0 };
1430     wchar_t buffer[14] = { 0 };
1431     int exp, got;
1432 
1433     if (!p__vsnwprintf_s)
1434     {
1435         win_skip("_vsnwprintf_s not available\n");
1436         return;
1437     }
1438 
1439     /* Enough room. */
1440     exp = wcslen(out7);
1441 
1442     got = _vsnwprintf_s_wrapper(buffer, 14, _TRUNCATE, format, 123);
1443     ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1444     ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1445 
1446     got = _vsnwprintf_s_wrapper(buffer, 12, _TRUNCATE, format, 123);
1447     ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1448     ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1449 
1450     got = _vsnwprintf_s_wrapper(buffer, 7, _TRUNCATE, format, 123);
1451     ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1452     ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1453 
1454     /* Not enough room. */
1455     exp = -1;
1456 
1457     got = _vsnwprintf_s_wrapper(buffer, 6, _TRUNCATE, format, 123);
1458     ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1459     ok( !wcscmp(out6, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1460 
1461     got = _vsnwprintf_s_wrapper(buffer, 2, _TRUNCATE, format, 123);
1462     ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1463     ok( !wcscmp(out2, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1464 
1465     got = _vsnwprintf_s_wrapper(buffer, 1, _TRUNCATE, format, 123);
1466     ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1467     ok( !wcscmp(out1, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1468 }
1469 
1470 static int WINAPIV _vsprintf_p_wrapper(char *str, size_t sizeOfBuffer,
1471                                  const char *format, ...)
1472 {
1473     int ret;
1474     __ms_va_list valist;
1475     __ms_va_start(valist, format);
1476     ret = p__vsprintf_p(str, sizeOfBuffer, format, valist);
1477     __ms_va_end(valist);
1478     return ret;
1479 }
1480 
1481 static void test_vsprintf_p(void)
1482 {
1483     char buf[1024];
1484     int ret;
1485 
1486     if(!p__vsprintf_p) {
1487         win_skip("vsprintf_p not available\n");
1488         return;
1489     }
1490 
1491     ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%s %d", "test", 1234);
1492     ok(ret == 9, "ret = %d\n", ret);
1493     ok(!memcmp(buf, "test 1234", 10), "buf = %s\n", buf);
1494 
1495     ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%1$d", 1234, "additional param");
1496     ok(ret == 4, "ret = %d\n", ret);
1497     ok(!memcmp(buf, "1234", 5), "buf = %s\n", buf);
1498 
1499     ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%2$s %1$d", 1234, "test");
1500     ok(ret == 9, "ret = %d\n", ret);
1501     ok(!memcmp(buf, "test 1234", 10), "buf = %s\n", buf);
1502 
1503     ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%2$*3$s %2$.*1$s", 2, "test", 3);
1504     ok(ret == 7, "ret = %d\n", ret);
1505     ok(!memcmp(buf, "test te", 8), "buf = %s\n", buf);
1506 
1507     /* Following test invokes invalid parameter handler */
1508     /* ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%d %1$d", 1234); */
1509 }
1510 
1511 static void test__get_output_format(void)
1512 {
1513     unsigned int ret;
1514     char buf[64];
1515     int c;
1516 
1517     if (!p__get_output_format || !p__set_output_format)
1518     {
1519         win_skip("_get_output_format or _set_output_format is not available\n");
1520         return;
1521     }
1522 
1523     ret = p__get_output_format();
1524     ok(ret == 0, "got %d\n", ret);
1525 
1526     c = p_sprintf(buf, "%E", 1.23);
1527     ok(c == 13, "c = %d\n", c);
1528     ok(!strcmp(buf, "1.230000E+000"), "buf = %s\n", buf);
1529 
1530     ret = p__set_output_format(_TWO_DIGIT_EXPONENT);
1531     ok(ret == 0, "got %d\n", ret);
1532 
1533     c = p_sprintf(buf, "%E", 1.23);
1534     ok(c == 12, "c = %d\n", c);
1535     ok(!strcmp(buf, "1.230000E+00"), "buf = %s\n", buf);
1536 
1537     ret = p__get_output_format();
1538     ok(ret == _TWO_DIGIT_EXPONENT, "got %d\n", ret);
1539 
1540     ret = p__set_output_format(_TWO_DIGIT_EXPONENT);
1541     ok(ret == _TWO_DIGIT_EXPONENT, "got %d\n", ret);
1542 }
1543 
1544 START_TEST(printf)
1545 {
1546     init();
1547 
1548     test_sprintf();
1549     test_swprintf();
1550     test_snprintf();
1551     test_fprintf();
1552     test_fcvt();
1553     test_xcvt();
1554     test_vsnwprintf();
1555     test_vscprintf();
1556     test_vscwprintf();
1557     test_vswprintf();
1558     test_vsnwprintf_s();
1559     test_vsprintf_p();
1560     test__get_output_format();
1561 }
1562