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