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