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