wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / msvcrt / tests / printf.c
blobf05d124a005c582be76fdc4a13ad1194b3d2c07a
1 /*
2 * Conformance tests for *printf functions.
4 * Copyright 2002 Uwe Bonnes
5 * Copyright 2004 Aneurin Price
6 * Copyright 2005 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* With Visual Studio >= 2005, swprintf() takes an extra parameter unless
24 * the following macro is defined.
26 #define _CRT_NON_CONFORMING_SWPRINTFS
28 #include <stdio.h>
29 #include <errno.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnls.h"
35 #include "wine/test.h"
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);
47 static void init( void )
49 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
51 p__vscprintf = (void *)GetProcAddress(hmod, "_vscprintf");
52 p__vscwprintf = (void *)GetProcAddress(hmod, "_vscwprintf");
53 p__vsnwprintf_s = (void *)GetProcAddress(hmod, "_vsnwprintf_s");
54 p__ecvt_s = (void *)GetProcAddress(hmod, "_ecvt_s");
55 p__fcvt_s = (void *)GetProcAddress(hmod, "_fcvt_s");
58 static void test_sprintf( void )
60 char buffer[100];
61 const char *format;
62 double pnumber=789456123;
63 int x, r;
64 WCHAR wide[] = { 'w','i','d','e',0};
66 format = "%+#23.15e";
67 r = sprintf(buffer,format,pnumber);
68 ok(!strcmp(buffer,"+7.894561230000000e+008"),"+#23.15e failed: '%s'\n", buffer);
69 ok( r==23, "return count wrong\n");
71 format = "%-#23.15e";
72 r = sprintf(buffer,format,pnumber);
73 ok(!strcmp(buffer,"7.894561230000000e+008 "),"-#23.15e failed: '%s'\n", buffer);
74 ok( r==23, "return count wrong\n");
76 format = "%#23.15e";
77 r = sprintf(buffer,format,pnumber);
78 ok(!strcmp(buffer," 7.894561230000000e+008"),"#23.15e failed: '%s'\n", buffer);
79 ok( r==23, "return count wrong\n");
81 format = "%#1.1g";
82 r = sprintf(buffer,format,pnumber);
83 ok(!strcmp(buffer,"8.e+008"),"#1.1g failed: '%s'\n", buffer);
84 ok( r==7, "return count wrong\n");
86 format = "%I64d";
87 r = sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
88 ok(!strcmp(buffer,"-8589934591"),"Problem with long long\n");
89 ok( r==11, "return count wrong\n");
91 format = "%+8I64d";
92 r = sprintf(buffer,format,(LONGLONG)100);
93 ok(!strcmp(buffer," +100") && r==8,"+8I64d failed: '%s'\n", buffer);
95 format = "%+.8I64d";
96 r = sprintf(buffer,format,(LONGLONG)100);
97 ok(!strcmp(buffer,"+00000100") && r==9,"+.8I64d failed: '%s'\n", buffer);
99 format = "%+10.8I64d";
100 r = sprintf(buffer,format,(LONGLONG)100);
101 ok(!strcmp(buffer," +00000100") && r==10,"+10.8I64d failed: '%s'\n", buffer);
102 format = "%_1I64d";
103 r = sprintf(buffer,format,(LONGLONG)100);
104 ok(!strcmp(buffer,"_1I64d") && r==6,"_1I64d failed\n");
106 format = "%-1.5I64d";
107 r = sprintf(buffer,format,(LONGLONG)-100);
108 ok(!strcmp(buffer,"-00100") && r==6,"-1.5I64d failed: '%s'\n", buffer);
110 format = "%5I64d";
111 r = sprintf(buffer,format,(LONGLONG)100);
112 ok(!strcmp(buffer," 100") && r==5,"5I64d failed: '%s'\n", buffer);
114 format = "%5I64d";
115 r = sprintf(buffer,format,(LONGLONG)-100);
116 ok(!strcmp(buffer," -100") && r==5,"5I64d failed: '%s'\n", buffer);
118 format = "%-5I64d";
119 r = sprintf(buffer,format,(LONGLONG)100);
120 ok(!strcmp(buffer,"100 ") && r==5,"-5I64d failed: '%s'\n", buffer);
122 format = "%-5I64d";
123 r = sprintf(buffer,format,(LONGLONG)-100);
124 ok(!strcmp(buffer,"-100 ") && r==5,"-5I64d failed: '%s'\n", buffer);
126 format = "%-.5I64d";
127 r = sprintf(buffer,format,(LONGLONG)100);
128 ok(!strcmp(buffer,"00100") && r==5,"-.5I64d failed: '%s'\n", buffer);
130 format = "%-.5I64d";
131 r = sprintf(buffer,format,(LONGLONG)-100);
132 ok(!strcmp(buffer,"-00100") && r==6,"-.5I64d failed: '%s'\n", buffer);
134 format = "%-8.5I64d";
135 r = sprintf(buffer,format,(LONGLONG)100);
136 ok(!strcmp(buffer,"00100 ") && r==8,"-8.5I64d failed: '%s'\n", buffer);
138 format = "%-8.5I64d";
139 r = sprintf(buffer,format,(LONGLONG)-100);
140 ok(!strcmp(buffer,"-00100 ") && r==8,"-8.5I64d failed: '%s'\n", buffer);
142 format = "%05I64d";
143 r = sprintf(buffer,format,(LONGLONG)100);
144 ok(!strcmp(buffer,"00100") && r==5,"05I64d failed: '%s'\n", buffer);
146 format = "%05I64d";
147 r = sprintf(buffer,format,(LONGLONG)-100);
148 ok(!strcmp(buffer,"-0100") && r==5,"05I64d failed: '%s'\n", buffer);
150 format = "% I64d";
151 r = sprintf(buffer,format,(LONGLONG)100);
152 ok(!strcmp(buffer," 100") && r==4,"' I64d' failed: '%s'\n", buffer);
154 format = "% I64d";
155 r = sprintf(buffer,format,(LONGLONG)-100);
156 ok(!strcmp(buffer,"-100") && r==4,"' I64d' failed: '%s'\n", buffer);
158 format = "% 5I64d";
159 r = sprintf(buffer,format,(LONGLONG)100);
160 ok(!strcmp(buffer," 100") && r==5,"' 5I64d' failed: '%s'\n", buffer);
162 format = "% 5I64d";
163 r = sprintf(buffer,format,(LONGLONG)-100);
164 ok(!strcmp(buffer," -100") && r==5,"' 5I64d' failed: '%s'\n", buffer);
166 format = "% .5I64d";
167 r = sprintf(buffer,format,(LONGLONG)100);
168 ok(!strcmp(buffer," 00100") && r==6,"' .5I64d' failed: '%s'\n", buffer);
170 format = "% .5I64d";
171 r = sprintf(buffer,format,(LONGLONG)-100);
172 ok(!strcmp(buffer,"-00100") && r==6,"' .5I64d' failed: '%s'\n", buffer);
174 format = "% 8.5I64d";
175 r = sprintf(buffer,format,(LONGLONG)100);
176 ok(!strcmp(buffer," 00100") && r==8,"' 8.5I64d' failed: '%s'\n", buffer);
178 format = "% 8.5I64d";
179 r = sprintf(buffer,format,(LONGLONG)-100);
180 ok(!strcmp(buffer," -00100") && r==8,"' 8.5I64d' failed: '%s'\n", buffer);
182 format = "%.0I64d";
183 r = sprintf(buffer,format,(LONGLONG)0);
184 ok(r==0,".0I64d failed: '%s'\n", buffer);
186 format = "%#+21.18I64x";
187 r = sprintf(buffer,format,(LONGLONG)-100);
188 ok(!strcmp(buffer," 0x00ffffffffffffff9c") && r==21,"#+21.18I64x failed: '%s'\n", buffer);
190 format = "%#.25I64o";
191 r = sprintf(buffer,format,(LONGLONG)-100);
192 ok(!strcmp(buffer,"0001777777777777777777634") && r==25,"#.25I64o failed: '%s'\n", buffer);
194 format = "%#+24.20I64o";
195 r = sprintf(buffer,format,(LONGLONG)-100);
196 ok(!strcmp(buffer," 01777777777777777777634") && r==24,"#+24.20I64o failed: '%s'\n", buffer);
198 format = "%#+18.21I64X";
199 r = sprintf(buffer,format,(LONGLONG)-100);
200 ok(!strcmp(buffer,"0X00000FFFFFFFFFFFFFF9C") && r==23,"#+18.21I64X failed: '%s '\n", buffer);
202 format = "%#+20.24I64o";
203 r = sprintf(buffer,format,(LONGLONG)-100);
204 ok(!strcmp(buffer,"001777777777777777777634") && r==24,"#+20.24I64o failed: '%s'\n", buffer);
206 format = "%#+25.22I64u";
207 r = sprintf(buffer,format,(LONGLONG)-1);
208 ok(!strcmp(buffer," 0018446744073709551615") && r==25,"#+25.22I64u conversion failed: '%s'\n", buffer);
210 format = "%#+25.22I64u";
211 r = sprintf(buffer,format,(LONGLONG)-1);
212 ok(!strcmp(buffer," 0018446744073709551615") && r==25,"#+25.22I64u failed: '%s'\n", buffer);
214 format = "%#+30.25I64u";
215 r = sprintf(buffer,format,(LONGLONG)-1);
216 ok(!strcmp(buffer," 0000018446744073709551615") && r==30,"#+30.25I64u failed: '%s'\n", buffer);
218 format = "%+#25.22I64d";
219 r = sprintf(buffer,format,(LONGLONG)-1);
220 ok(!strcmp(buffer," -0000000000000000000001") && r==25,"+#25.22I64d failed: '%s'\n", buffer);
222 format = "%#-8.5I64o";
223 r = sprintf(buffer,format,(LONGLONG)100);
224 ok(!strcmp(buffer,"00144 ") && r==8,"-8.5I64o failed: '%s'\n", buffer);
226 format = "%#-+ 08.5I64d";
227 r = sprintf(buffer,format,(LONGLONG)100);
228 ok(!strcmp(buffer,"+00100 ") && r==8,"'#-+ 08.5I64d failed: '%s'\n", buffer);
230 format = "%#-+ 08.5I64d";
231 r = sprintf(buffer,format,(LONGLONG)100);
232 ok(!strcmp(buffer,"+00100 ") && r==8,"#-+ 08.5I64d failed: '%s'\n", buffer);
234 format = "%.80I64d";
235 r = sprintf(buffer,format,(LONGLONG)1);
236 ok(r==80,"%s format failed\n", format);
238 format = "% .80I64d";
239 r = sprintf(buffer,format,(LONGLONG)1);
240 ok(r==81,"%s format failed\n", format);
242 format = "% .80d";
243 r = sprintf(buffer,format,1);
244 ok(r==81,"%s format failed\n", format);
246 format = "%lld";
247 r = sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
248 ok( r == 1 || r == 11, "return count wrong %d\n", r);
249 if (r == 11) /* %ll works on Vista */
250 ok(!strcmp(buffer, "-8589934591"), "Problem with \"ll\" interpretation '%s'\n", buffer);
251 else
252 ok(!strcmp(buffer, "1"), "Problem with \"ll\" interpretation '%s'\n", buffer);
254 format = "%I";
255 r = sprintf(buffer,format,1);
256 ok(!strcmp(buffer, "I"), "Problem with \"I\" interpretation\n");
257 ok( r==1, "return count wrong\n");
259 format = "%I0d";
260 r = sprintf(buffer,format,1);
261 ok(!strcmp(buffer,"I0d"),"I0d failed\n");
262 ok( r==3, "return count wrong\n");
264 format = "%I32d";
265 r = sprintf(buffer,format,1);
266 if (r == 1)
268 ok(!strcmp(buffer,"1"),"I32d failed, got '%s'\n",buffer);
270 else
272 /* Older versions don't grok I32 format */
273 ok(r == 4 && !strcmp(buffer,"I32d"),"I32d failed, got '%s',%d\n",buffer,r);
276 format = "%I64D";
277 r = sprintf(buffer,format,(LONGLONG)-1);
278 ok(!strcmp(buffer,"D"),"I64D failed: %s\n",buffer);
279 ok( r==1, "return count wrong\n");
281 format = "% d";
282 r = sprintf(buffer,format,1);
283 ok(!strcmp(buffer, " 1"),"Problem with sign place-holder: '%s'\n",buffer);
284 ok( r==2, "return count wrong\n");
286 format = "%+ d";
287 r = sprintf(buffer,format,1);
288 ok(!strcmp(buffer, "+1"),"Problem with sign flags: '%s'\n",buffer);
289 ok( r==2, "return count wrong\n");
291 format = "%S";
292 r = sprintf(buffer,format,wide);
293 ok(!strcmp(buffer,"wide"),"Problem with wide string format\n");
294 ok( r==4, "return count wrong\n");
296 format = "%04c";
297 r = sprintf(buffer,format,'1');
298 ok(!strcmp(buffer,"0001"),"Character not zero-prefixed \"%s\"\n",buffer);
299 ok( r==4, "return count wrong\n");
301 format = "%-04c";
302 r = sprintf(buffer,format,'1');
303 ok(!strcmp(buffer,"1 "),"Character zero-padded and/or not left-adjusted \"%s\"\n",buffer);
304 ok( r==4, "return count wrong\n");
306 if (sizeof(void *) == 8)
308 format = "%p";
309 r = sprintf(buffer,format,(void *)57);
310 ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
311 ok( r==16, "return count wrong\n");
313 format = "%#020p";
314 r = sprintf(buffer,format,(void *)57);
315 ok(!strcmp(buffer," 0X0000000000000039"),"Pointer formatted incorrectly\n");
316 ok( r==20, "return count wrong\n");
318 format = "%Fp";
319 r = sprintf(buffer,format,(void *)57);
320 ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
321 ok( r==16, "return count wrong\n");
323 format = "%#-020p";
324 r = sprintf(buffer,format,(void *)57);
325 ok(!strcmp(buffer,"0X0000000000000039 "),"Pointer formatted incorrectly\n");
326 ok( r==20, "return count wrong\n");
328 else
330 format = "%p";
331 r = sprintf(buffer,format,(void *)57);
332 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
333 ok( r==8, "return count wrong\n");
335 format = "%#012p";
336 r = sprintf(buffer,format,(void *)57);
337 ok(!strcmp(buffer," 0X00000039"),"Pointer formatted incorrectly\n");
338 ok( r==12, "return count wrong\n");
340 format = "%Fp";
341 r = sprintf(buffer,format,(void *)57);
342 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
343 ok( r==8, "return count wrong\n");
345 format = "%#-012p";
346 r = sprintf(buffer,format,(void *)57);
347 ok(!strcmp(buffer,"0X00000039 "),"Pointer formatted incorrectly\n");
348 ok( r==12, "return count wrong\n");
351 format = "%04s";
352 r = sprintf(buffer,format,"foo");
353 ok(!strcmp(buffer,"0foo"),"String not zero-prefixed \"%s\"\n",buffer);
354 ok( r==4, "return count wrong\n");
356 format = "%.1s";
357 r = sprintf(buffer,format,"foo");
358 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
359 ok( r==1, "return count wrong\n");
361 format = "%.*s";
362 r = sprintf(buffer,format,1,"foo");
363 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
364 ok( r==1, "return count wrong\n");
366 format = "%*s";
367 r = sprintf(buffer,format,-5,"foo");
368 ok(!strcmp(buffer,"foo "),"Negative field width ignored \"%s\"\n",buffer);
369 ok( r==5, "return count wrong\n");
371 format = "hello";
372 r = sprintf(buffer, format);
373 ok(!strcmp(buffer,"hello"), "failed\n");
374 ok( r==5, "return count wrong\n");
376 format = "%ws";
377 r = sprintf(buffer, format, wide);
378 ok(!strcmp(buffer,"wide"), "failed\n");
379 ok( r==4, "return count wrong\n");
381 format = "%-10ws";
382 r = sprintf(buffer, format, wide );
383 ok(!strcmp(buffer,"wide "), "failed\n");
384 ok( r==10, "return count wrong\n");
386 format = "%10ws";
387 r = sprintf(buffer, format, wide );
388 ok(!strcmp(buffer," wide"), "failed\n");
389 ok( r==10, "return count wrong\n");
391 format = "%#+ -03whlls";
392 r = sprintf(buffer, format, wide );
393 ok(!strcmp(buffer,"wide"), "failed\n");
394 ok( r==4, "return count wrong\n");
396 format = "%w0s";
397 r = sprintf(buffer, format, wide );
398 ok(!strcmp(buffer,"0s"), "failed\n");
399 ok( r==2, "return count wrong\n");
401 format = "%w-s";
402 r = sprintf(buffer, format, wide );
403 ok(!strcmp(buffer,"-s"), "failed\n");
404 ok( r==2, "return count wrong\n");
406 format = "%ls";
407 r = sprintf(buffer, format, wide );
408 ok(!strcmp(buffer,"wide"), "failed\n");
409 ok( r==4, "return count wrong\n");
411 format = "%Ls";
412 r = sprintf(buffer, format, "not wide" );
413 ok(!strcmp(buffer,"not wide"), "failed\n");
414 ok( r==8, "return count wrong\n");
416 format = "%b";
417 r = sprintf(buffer, format);
418 ok(!strcmp(buffer,"b"), "failed\n");
419 ok( r==1, "return count wrong\n");
421 format = "%3c";
422 r = sprintf(buffer, format,'a');
423 ok(!strcmp(buffer," a"), "failed\n");
424 ok( r==3, "return count wrong\n");
426 format = "%3d";
427 r = sprintf(buffer, format,1234);
428 ok(!strcmp(buffer,"1234"), "failed\n");
429 ok( r==4, "return count wrong\n");
431 format = "%3h";
432 r = sprintf(buffer, format);
433 ok(!strcmp(buffer,""), "failed\n");
434 ok( r==0, "return count wrong\n");
436 format = "%j%k%m%q%r%t%v%y%z";
437 r = sprintf(buffer, format);
438 ok(!strcmp(buffer,"jkmqrtvyz"), "failed\n");
439 ok( r==9, "return count wrong\n");
441 format = "asdf%n";
442 x = 0;
443 r = sprintf(buffer, format, &x );
444 if (r == -1)
446 /* %n format is disabled by default on vista */
447 /* FIXME: should test with _set_printf_count_output */
448 ok(x == 0, "should not write to x: %d\n", x);
450 else
452 ok(x == 4, "should write to x: %d\n", x);
453 ok(!strcmp(buffer,"asdf"), "failed\n");
454 ok( r==4, "return count wrong: %d\n", r);
457 format = "%-1d";
458 r = sprintf(buffer, format,2);
459 ok(!strcmp(buffer,"2"), "failed\n");
460 ok( r==1, "return count wrong\n");
462 format = "%2.4f";
463 r = sprintf(buffer, format,8.6);
464 ok(!strcmp(buffer,"8.6000"), "failed\n");
465 ok( r==6, "return count wrong\n");
467 format = "%0f";
468 r = sprintf(buffer, format,0.6);
469 ok(!strcmp(buffer,"0.600000"), "failed\n");
470 ok( r==8, "return count wrong\n");
472 format = "%.0f";
473 r = sprintf(buffer, format,0.6);
474 ok(!strcmp(buffer,"1"), "failed\n");
475 ok( r==1, "return count wrong\n");
477 format = "%2.4e";
478 r = sprintf(buffer, format,8.6);
479 ok(!strcmp(buffer,"8.6000e+000"), "failed\n");
480 ok( r==11, "return count wrong\n");
482 format = "%2.4g";
483 r = sprintf(buffer, format,8.6);
484 ok(!strcmp(buffer,"8.6"), "failed\n");
485 ok( r==3, "return count wrong\n");
487 format = "%-i";
488 r = sprintf(buffer, format,-1);
489 ok(!strcmp(buffer,"-1"), "failed\n");
490 ok( r==2, "return count wrong\n");
492 format = "%-i";
493 r = sprintf(buffer, format,1);
494 ok(!strcmp(buffer,"1"), "failed\n");
495 ok( r==1, "return count wrong\n");
497 format = "%+i";
498 r = sprintf(buffer, format,1);
499 ok(!strcmp(buffer,"+1"), "failed\n");
500 ok( r==2, "return count wrong\n");
502 format = "%o";
503 r = sprintf(buffer, format,10);
504 ok(!strcmp(buffer,"12"), "failed\n");
505 ok( r==2, "return count wrong\n");
507 format = "%p";
508 r = sprintf(buffer, format,0);
509 if (sizeof(void *) == 8)
511 ok(!strcmp(buffer,"0000000000000000"), "failed\n");
512 ok( r==16, "return count wrong\n");
514 else
516 ok(!strcmp(buffer,"00000000"), "failed\n");
517 ok( r==8, "return count wrong\n");
520 format = "%s";
521 r = sprintf(buffer, format,0);
522 ok(!strcmp(buffer,"(null)"), "failed\n");
523 ok( r==6, "return count wrong\n");
525 format = "%s";
526 r = sprintf(buffer, format,"%%%%");
527 ok(!strcmp(buffer,"%%%%"), "failed\n");
528 ok( r==4, "return count wrong\n");
530 format = "%u";
531 r = sprintf(buffer, format,-1);
532 ok(!strcmp(buffer,"4294967295"), "failed\n");
533 ok( r==10, "return count wrong\n");
535 format = "%w";
536 r = sprintf(buffer, format,-1);
537 ok(!strcmp(buffer,""), "failed\n");
538 ok( r==0, "return count wrong\n");
540 format = "%h";
541 r = sprintf(buffer, format,-1);
542 ok(!strcmp(buffer,""), "failed\n");
543 ok( r==0, "return count wrong\n");
545 format = "%z";
546 r = sprintf(buffer, format,-1);
547 ok(!strcmp(buffer,"z"), "failed\n");
548 ok( r==1, "return count wrong\n");
550 format = "%j";
551 r = sprintf(buffer, format,-1);
552 ok(!strcmp(buffer,"j"), "failed\n");
553 ok( r==1, "return count wrong\n");
555 format = "%F";
556 r = sprintf(buffer, format,-1);
557 ok(!strcmp(buffer,""), "failed\n");
558 ok( r==0, "return count wrong\n");
560 format = "%H";
561 r = sprintf(buffer, format,-1);
562 ok(!strcmp(buffer,"H"), "failed\n");
563 ok( r==1, "return count wrong\n");
565 format = "x%cx";
566 r = sprintf(buffer, format, 0x100+'X');
567 ok(!strcmp(buffer,"xXx"), "failed\n");
568 ok( r==3, "return count wrong\n");
570 format = "%%0";
571 r = sprintf(buffer, format);
572 ok(!strcmp(buffer,"%0"), "failed: \"%s\"\n", buffer);
573 ok( r==2, "return count wrong\n");
576 static void test_swprintf( void )
578 wchar_t buffer[100];
579 const wchar_t I64d[] = {'%','I','6','4','d',0};
580 double pnumber=789456123;
581 const wchar_t TwentyThreePoint15e[]= {'%','+','#','2','3','.','1','5','e',0};
582 const wchar_t e008[] = {'e','+','0','0','8',0};
583 const wchar_t string_w[] = {'s','t','r','i','n','g',0};
584 const char string[] = "string";
585 const wchar_t S[]={'%','S',0};
586 const wchar_t hs[] = {'%', 'h', 's', 0};
588 swprintf(buffer,TwentyThreePoint15e,pnumber);
589 ok(wcsstr(buffer,e008) != 0,"Sprintf different\n");
590 swprintf(buffer,I64d,((ULONGLONG)0xffffffff)*0xffffffff);
591 ok(wcslen(buffer) == 11,"Problem with long long\n");
592 swprintf(buffer,S,string);
593 ok(wcslen(buffer) == 6,"Problem with \"%%S\" interpretation\n");
594 swprintf(buffer, hs, string);
595 ok( wcscmp(string_w,buffer) == 0, "swprintf failed with %%hs\n");
598 static void test_snprintf (void)
600 struct snprintf_test {
601 const char *format;
602 int expected;
604 /* Pre-2.1 libc behaviour, not C99 compliant. */
605 const struct snprintf_test tests[] = {{"short", 5},
606 {"justfit", 7},
607 {"justfits", 8},
608 {"muchlonger", -1}};
609 char buffer[8];
610 const int bufsiz = sizeof buffer;
611 unsigned int i;
613 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
614 const char *fmt = tests[i].format;
615 const int expect = tests[i].expected;
616 const int n = _snprintf (buffer, bufsiz, fmt);
617 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
619 ok (n == expect, "\"%s\": expected %d, returned %d\n",
620 fmt, expect, n);
621 ok (!memcmp (fmt, buffer, valid),
622 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
626 static void test_fcvt(void)
628 char *str;
629 int dec=100, sign=100;
631 /* Numbers less than 1.0 with different precisions */
632 str = _fcvt(0.0001, 1, &dec, &sign );
633 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
634 ok( -3 == dec, "dec wrong %d\n", dec);
635 ok( 0 == sign, "sign wrong\n");
637 str = _fcvt(0.0001, -10, &dec, &sign );
638 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
639 ok( -3 == dec, "dec wrong %d\n", dec);
640 ok( 0 == sign, "sign wrong\n");
642 str = _fcvt(0.0001, 10, &dec, &sign );
643 ok( 0 == strcmp(str,"1000000"), "bad return '%s'\n", str);
644 ok( -3 == dec, "dec wrong %d\n", dec);
645 ok( 0 == sign, "sign wrong\n");
647 /* Basic sign test */
648 str = _fcvt(-111.0001, 5, &dec, &sign );
649 ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
650 ok( 3 == dec, "dec wrong %d\n", dec);
651 ok( 1 == sign, "sign wrong\n");
653 str = _fcvt(111.0001, 5, &dec, &sign );
654 ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
655 ok( 3 == dec, "dec wrong\n");
656 ok( 0 == sign, "sign wrong\n");
658 /* 0.0 with different precisions */
659 str = _fcvt(0.0, 5, &dec, &sign );
660 ok( 0 == strcmp(str,"00000"), "bad return '%s'\n", str);
661 ok( 0 == dec, "dec wrong %d\n", dec);
662 ok( 0 == sign, "sign wrong\n");
664 str = _fcvt(0.0, 0, &dec, &sign );
665 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
666 ok( 0 == dec, "dec wrong %d\n", dec);
667 ok( 0 == sign, "sign wrong\n");
669 str = _fcvt(0.0, -1, &dec, &sign );
670 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
671 ok( 0 == dec, "dec wrong %d\n", dec);
672 ok( 0 == sign, "sign wrong\n");
674 /* Numbers > 1.0 with 0 or -ve precision */
675 str = _fcvt(-123.0001, 0, &dec, &sign );
676 ok( 0 == strcmp(str,"123"), "bad return '%s'\n", str);
677 ok( 3 == dec, "dec wrong %d\n", dec);
678 ok( 1 == sign, "sign wrong\n");
680 str = _fcvt(-123.0001, -1, &dec, &sign );
681 ok( 0 == strcmp(str,"12"), "bad return '%s'\n", str);
682 ok( 3 == dec, "dec wrong %d\n", dec);
683 ok( 1 == sign, "sign wrong\n");
685 str = _fcvt(-123.0001, -2, &dec, &sign );
686 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
687 ok( 3 == dec, "dec wrong %d\n", dec);
688 ok( 1 == sign, "sign wrong\n");
690 str = _fcvt(-123.0001, -3, &dec, &sign );
691 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
692 ok( 3 == dec, "dec wrong %d\n", dec);
693 ok( 1 == sign, "sign wrong\n");
695 /* Numbers > 1.0, but with rounding at the point of precision */
696 str = _fcvt(99.99, 1, &dec, &sign );
697 ok( 0 == strcmp(str,"1000"), "bad return '%s'\n", str);
698 ok( 3 == dec, "dec wrong %d\n", dec);
699 ok( 0 == sign, "sign wrong\n");
701 /* Numbers < 1.0 where rounding occurs at the point of precision */
702 str = _fcvt(0.00636, 2, &dec, &sign );
703 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
704 ok( -1 == dec, "dec wrong %d\n", dec);
705 ok( 0 == sign, "sign wrong\n");
707 str = _fcvt(0.00636, 3, &dec, &sign );
708 ok( 0 == strcmp(str,"6"), "bad return '%s'\n", str);
709 ok( -2 == dec, "dec wrong %d\n", dec);
710 ok( 0 == sign, "sign wrong\n");
712 str = _fcvt(0.09999999996, 2, &dec, &sign );
713 ok( 0 == strcmp(str,"10"), "bad return '%s'\n", str);
714 ok( 0 == dec, "dec wrong %d\n", dec);
715 ok( 0 == sign, "sign wrong\n");
717 str = _fcvt(0.6, 0, &dec, &sign );
718 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
719 ok( 1 == dec, "dec wrong %d\n", dec);
720 ok( 0 == sign, "sign wrong\n");
723 /* Don't test nrdigits < 0, msvcrt on Win9x and NT4 will corrupt memory by
724 * writing outside allocated memory */
725 static struct {
726 double value;
727 int nrdigits;
728 const char *expstr_e;
729 const char *expstr_f;
730 int expdecpt_e;
731 int expdecpt_f;
732 int expsign;
733 } test_cvt_testcases[] = {
734 { 45.0, 2, "45", "4500", 2, 2, 0 },
735 /* Numbers less than 1.0 with different precisions */
736 { 0.0001, 1, "1", "", -3, -3, 0 },
737 { 0.0001, 10,"1000000000", "1000000", -3, -3, 0 },
738 /* Basic sign test */
739 { -111.0001, 5, "11100", "11100010", 3, 3, 1 },
740 { 111.0001, 5, "11100", "11100010", 3, 3, 0 },
741 /* big numbers with low precision */
742 { 3333.3, 2, "33", "333330", 4, 4, 0 },
743 {999999999999.9, 3, "100","999999999999900", 13, 12, 0 },
744 /* 0.0 with different precisions */
745 { 0.0, 5, "00000", "00000", 0, 0, 0 },
746 { 0.0, 0, "", "", 0, 0, 0 },
747 { 0.0, -1, "", "", 0, 0, 0 },
748 /* Numbers > 1.0 with 0 or -ve precision */
749 { -123.0001, 0, "", "123", 3, 3, 1 },
750 { -123.0001, -1, "", "12", 3, 3, 1 },
751 { -123.0001, -2, "", "1", 3, 3, 1 },
752 { -123.0001, -3, "", "", 3, 3, 1 },
753 /* Numbers > 1.0, but with rounding at the point of precision */
754 { 99.99, 1, "1", "1000", 3, 3, 0 },
755 /* Numbers < 1.0 where rounding occurs at the point of precision */
756 { 0.0063, 2, "63", "1", -2, -1, 0 },
757 { 0.0063, 3, "630", "6", -2, -2, 0 },
758 { 0.09999999996, 2, "10", "10", 0, 0, 0 },
759 { 0.6, 1, "6", "6", 0, 0, 0 },
760 { 0.6, 0, "", "1", 1, 1, 0 },
761 { 0.4, 0, "", "", 0, 0, 0 },
762 { 0.49, 0, "", "", 0, 0, 0 },
763 { 0.51, 0, "", "1", 1, 1, 0 },
764 /* ask for ridiculous precision, ruin formatting this table */
765 { 1.0, 30, "100000000000000000000000000000",
766 "1000000000000000000000000000000", 1, 1, 0},
767 { 123456789012345678901.0, 30, "123456789012345680000000000000",
768 "123456789012345680000000000000000000000000000000000", 21, 21, 0},
769 /* end marker */
770 { 0, 0, "END"}
773 static void test_xcvt(void)
775 char *str;
776 int i, decpt, sign, err;
778 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
779 decpt = sign = 100;
780 str = _ecvt( test_cvt_testcases[i].value,
781 test_cvt_testcases[i].nrdigits,
782 &decpt,
783 &sign);
784 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15),
785 "_ecvt() bad return, got \n'%s' expected \n'%s'\n", str,
786 test_cvt_testcases[i].expstr_e);
787 ok( decpt == test_cvt_testcases[i].expdecpt_e,
788 "_ecvt() decimal point wrong, got %d expected %d\n", decpt,
789 test_cvt_testcases[i].expdecpt_e);
790 ok( sign == test_cvt_testcases[i].expsign,
791 "_ecvt() sign wrong, got %d expected %d\n", sign,
792 test_cvt_testcases[i].expsign);
794 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
795 decpt = sign = 100;
796 str = _fcvt( test_cvt_testcases[i].value,
797 test_cvt_testcases[i].nrdigits,
798 &decpt,
799 &sign);
800 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15),
801 "_fcvt() bad return, got \n'%s' expected \n'%s'\n", str,
802 test_cvt_testcases[i].expstr_f);
803 ok( decpt == test_cvt_testcases[i].expdecpt_f,
804 "_fcvt() decimal point wrong, got %d expected %d\n", decpt,
805 test_cvt_testcases[i].expdecpt_f);
806 ok( sign == test_cvt_testcases[i].expsign,
807 "_fcvt() sign wrong, got %d expected %d\n", sign,
808 test_cvt_testcases[i].expsign);
811 if (p__ecvt_s)
813 str = malloc(1024);
814 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
815 decpt = sign = 100;
816 err = p__ecvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign);
817 ok(err == 0, "_ecvt_s() failed with error code %d\n", err);
818 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15),
819 "_ecvt_s() bad return, got \n'%s' expected \n'%s'\n", str,
820 test_cvt_testcases[i].expstr_e);
821 ok( decpt == test_cvt_testcases[i].expdecpt_e,
822 "_ecvt_s() decimal point wrong, got %d expected %d\n", decpt,
823 test_cvt_testcases[i].expdecpt_e);
824 ok( sign == test_cvt_testcases[i].expsign,
825 "_ecvt_s() sign wrong, got %d expected %d\n", sign,
826 test_cvt_testcases[i].expsign);
828 free(str);
830 else
831 win_skip("_ecvt_s not available\n");
833 if (p__fcvt_s)
835 int i;
837 str = malloc(1024);
839 /* invalid arguments */
840 err = p__fcvt_s(NULL, 0, 0.0, 0, &i, &i);
841 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
843 err = p__fcvt_s(str, 0, 0.0, 0, &i, &i);
844 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
846 str[0] = ' ';
847 str[1] = 0;
848 err = p__fcvt_s(str, -1, 0.0, 0, &i, &i);
849 ok(err == 0, "got %d, expected 0\n", err);
850 ok(str[0] == 0, "got %c, expected 0\n", str[0]);
851 ok(str[1] == 0, "got %c, expected 0\n", str[1]);
853 err = p__fcvt_s(str, 1, 0.0, 0, NULL, &i);
854 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
856 err = p__fcvt_s(str, 1, 0.0, 0, &i, NULL);
857 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
859 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
860 decpt = sign = 100;
861 err = p__fcvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign);
862 ok(err == 0, "_fcvt_s() failed with error code %d", err);
863 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15),
864 "_fcvt_s() bad return, got '%s' expected '%s'. test %d\n", str,
865 test_cvt_testcases[i].expstr_f, i);
866 ok( decpt == test_cvt_testcases[i].expdecpt_f,
867 "_fcvt_s() decimal point wrong, got %d expected %d\n", decpt,
868 test_cvt_testcases[i].expdecpt_f);
869 ok( sign == test_cvt_testcases[i].expsign,
870 "_fcvt_s() sign wrong, got %d expected %d\n", sign,
871 test_cvt_testcases[i].expsign);
873 free(str);
875 else
876 win_skip("_fcvt_s not available\n");
879 static int __cdecl _vsnwprintf_wrapper(wchar_t *str, size_t len, const wchar_t *format, ...)
881 int ret;
882 __ms_va_list valist;
883 __ms_va_start(valist, format);
884 ret = _vsnwprintf(str, len, format, valist);
885 __ms_va_end(valist);
886 return ret;
889 static void test_vsnwprintf(void)
891 const wchar_t format[] = {'%','w','s','%','w','s','%','w','s',0};
892 const wchar_t one[] = {'o','n','e',0};
893 const wchar_t two[] = {'t','w','o',0};
894 const wchar_t three[] = {'t','h','r','e','e',0};
896 int ret;
897 wchar_t str[32];
898 char buf[32];
900 ret = _vsnwprintf_wrapper( str, sizeof(str)/sizeof(str[0]), format, one, two, three );
902 ok( ret == 11, "got %d expected 11\n", ret );
903 WideCharToMultiByte( CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL );
904 ok( !strcmp(buf, "onetwothree"), "got %s expected 'onetwothree'\n", buf );
907 static int __cdecl _vscprintf_wrapper(const char *format, ...)
909 int ret;
910 __ms_va_list valist;
911 __ms_va_start(valist, format);
912 ret = p__vscprintf(format, valist);
913 __ms_va_end(valist);
914 return ret;
917 static void test_vscprintf(void)
919 int ret;
921 if (!p__vscprintf)
923 win_skip("_vscprintf not available\n");
924 return;
927 ret = _vscprintf_wrapper( "%s %d", "number", 1 );
928 ok( ret == 8, "got %d expected 8\n", ret );
931 static int __cdecl _vscwprintf_wrapper(const wchar_t *format, ...)
933 int ret;
934 __ms_va_list valist;
935 __ms_va_start(valist, format);
936 ret = p__vscwprintf(format, valist);
937 __ms_va_end(valist);
938 return ret;
941 static void test_vscwprintf(void)
943 const wchar_t format[] = {'%','s',' ','%','d',0};
944 const wchar_t number[] = {'n','u','m','b','e','r',0};
946 int ret;
948 if (!p__vscwprintf)
950 win_skip("_vscwprintf not available\n");
951 return;
954 ret = _vscwprintf_wrapper( format, number, 1 );
955 ok( ret == 8, "got %d expected 8\n", ret );
958 static int __cdecl _vsnwprintf_s_wrapper(wchar_t *str, size_t sizeOfBuffer,
959 size_t count, const wchar_t *format, ...)
961 int ret;
962 __ms_va_list valist;
963 __ms_va_start(valist, format);
964 ret = p__vsnwprintf_s(str, sizeOfBuffer, count, format, valist);
965 __ms_va_end(valist);
966 return ret;
969 static void test_vsnwprintf_s(void)
971 const wchar_t format[] = { 'A','B','%','u','C',0 };
972 const wchar_t out7[] = { 'A','B','1','2','3','C',0 };
973 const wchar_t out6[] = { 'A','B','1','2','3',0 };
974 const wchar_t out2[] = { 'A',0 };
975 const wchar_t out1[] = { 0 };
976 wchar_t buffer[14] = { 0 };
977 int exp, got;
979 if (!p__vsnwprintf_s)
981 win_skip("_vsnwprintf_s not available\n");
982 return;
985 /* Enough room. */
986 exp = wcslen(out7);
988 got = _vsnwprintf_s_wrapper(buffer, 14, _TRUNCATE, format, 123);
989 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
990 ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
992 got = _vsnwprintf_s_wrapper(buffer, 12, _TRUNCATE, format, 123);
993 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
994 ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
996 got = _vsnwprintf_s_wrapper(buffer, 7, _TRUNCATE, format, 123);
997 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
998 ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1000 /* Not enough room. */
1001 exp = -1;
1003 got = _vsnwprintf_s_wrapper(buffer, 6, _TRUNCATE, format, 123);
1004 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1005 ok( !wcscmp(out6, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1007 got = _vsnwprintf_s_wrapper(buffer, 2, _TRUNCATE, format, 123);
1008 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1009 ok( !wcscmp(out2, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1011 got = _vsnwprintf_s_wrapper(buffer, 1, _TRUNCATE, format, 123);
1012 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1013 ok( !wcscmp(out1, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1016 START_TEST(printf)
1018 init();
1020 test_sprintf();
1021 test_swprintf();
1022 test_snprintf();
1023 test_fcvt();
1024 test_xcvt();
1025 test_vsnwprintf();
1026 test_vscprintf();
1027 test_vscwprintf();
1028 test_vsnwprintf_s();