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
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
,
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 )
62 double pnumber
=789456123;
64 WCHAR wide
[] = { 'w','i','d','e',0};
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");
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");
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");
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");
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");
92 r
= sprintf(buffer
,format
,(LONGLONG
)100);
93 ok(!strcmp(buffer
," +100") && r
==8,"+8I64d failed: '%s'\n", buffer
);
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
);
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
);
111 r
= sprintf(buffer
,format
,(LONGLONG
)100);
112 ok(!strcmp(buffer
," 100") && r
==5,"5I64d failed: '%s'\n", buffer
);
115 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
116 ok(!strcmp(buffer
," -100") && r
==5,"5I64d failed: '%s'\n", buffer
);
119 r
= sprintf(buffer
,format
,(LONGLONG
)100);
120 ok(!strcmp(buffer
,"100 ") && r
==5,"-5I64d failed: '%s'\n", buffer
);
123 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
124 ok(!strcmp(buffer
,"-100 ") && r
==5,"-5I64d failed: '%s'\n", buffer
);
127 r
= sprintf(buffer
,format
,(LONGLONG
)100);
128 ok(!strcmp(buffer
,"00100") && r
==5,"-.5I64d failed: '%s'\n", buffer
);
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
);
143 r
= sprintf(buffer
,format
,(LONGLONG
)100);
144 ok(!strcmp(buffer
,"00100") && r
==5,"05I64d failed: '%s'\n", buffer
);
147 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
148 ok(!strcmp(buffer
,"-0100") && r
==5,"05I64d failed: '%s'\n", buffer
);
151 r
= sprintf(buffer
,format
,(LONGLONG
)100);
152 ok(!strcmp(buffer
," 100") && r
==4,"' I64d' failed: '%s'\n", buffer
);
155 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
156 ok(!strcmp(buffer
,"-100") && r
==4,"' I64d' failed: '%s'\n", buffer
);
159 r
= sprintf(buffer
,format
,(LONGLONG
)100);
160 ok(!strcmp(buffer
," 100") && r
==5,"' 5I64d' failed: '%s'\n", buffer
);
163 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
164 ok(!strcmp(buffer
," -100") && r
==5,"' 5I64d' failed: '%s'\n", buffer
);
167 r
= sprintf(buffer
,format
,(LONGLONG
)100);
168 ok(!strcmp(buffer
," 00100") && r
==6,"' .5I64d' failed: '%s'\n", buffer
);
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
);
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
);
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
);
243 r
= sprintf(buffer
,format
,1);
244 ok(r
==81,"%s format failed\n", format
);
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
);
252 ok(!strcmp(buffer
, "1"), "Problem with \"ll\" interpretation '%s'\n", buffer
);
255 r
= sprintf(buffer
,format
,1);
256 ok(!strcmp(buffer
, "I"), "Problem with \"I\" interpretation\n");
257 ok( r
==1, "return count wrong\n");
260 r
= sprintf(buffer
,format
,1);
261 ok(!strcmp(buffer
,"I0d"),"I0d failed\n");
262 ok( r
==3, "return count wrong\n");
265 r
= sprintf(buffer
,format
,1);
268 ok(!strcmp(buffer
,"1"),"I32d failed, got '%s'\n",buffer
);
272 /* Older versions don't grok I32 format */
273 ok(r
== 4 && !strcmp(buffer
,"I32d"),"I32d failed, got '%s',%d\n",buffer
,r
);
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");
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");
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");
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");
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");
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)
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");
314 r
= sprintf(buffer
,format
,(void *)57);
315 ok(!strcmp(buffer
," 0X0000000000000039"),"Pointer formatted incorrectly\n");
316 ok( r
==20, "return count wrong\n");
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");
324 r
= sprintf(buffer
,format
,(void *)57);
325 ok(!strcmp(buffer
,"0X0000000000000039 "),"Pointer formatted incorrectly\n");
326 ok( r
==20, "return count wrong\n");
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");
336 r
= sprintf(buffer
,format
,(void *)57);
337 ok(!strcmp(buffer
," 0X00000039"),"Pointer formatted incorrectly\n");
338 ok( r
==12, "return count wrong\n");
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");
346 r
= sprintf(buffer
,format
,(void *)57);
347 ok(!strcmp(buffer
,"0X00000039 "),"Pointer formatted incorrectly\n");
348 ok( r
==12, "return count wrong\n");
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");
357 r
= sprintf(buffer
,format
,"foo");
358 ok(!strcmp(buffer
,"f"),"Precision ignored \"%s\"\n",buffer
);
359 ok( r
==1, "return count wrong\n");
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");
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");
372 r
= sprintf(buffer
, format
);
373 ok(!strcmp(buffer
,"hello"), "failed\n");
374 ok( r
==5, "return count wrong\n");
377 r
= sprintf(buffer
, format
, wide
);
378 ok(!strcmp(buffer
,"wide"), "failed\n");
379 ok( r
==4, "return count wrong\n");
382 r
= sprintf(buffer
, format
, wide
);
383 ok(!strcmp(buffer
,"wide "), "failed\n");
384 ok( r
==10, "return count wrong\n");
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");
397 r
= sprintf(buffer
, format
, wide
);
398 ok(!strcmp(buffer
,"0s"), "failed\n");
399 ok( r
==2, "return count wrong\n");
402 r
= sprintf(buffer
, format
, wide
);
403 ok(!strcmp(buffer
,"-s"), "failed\n");
404 ok( r
==2, "return count wrong\n");
407 r
= sprintf(buffer
, format
, wide
);
408 ok(!strcmp(buffer
,"wide"), "failed\n");
409 ok( r
==4, "return count wrong\n");
412 r
= sprintf(buffer
, format
, "not wide" );
413 ok(!strcmp(buffer
,"not wide"), "failed\n");
414 ok( r
==8, "return count wrong\n");
417 r
= sprintf(buffer
, format
);
418 ok(!strcmp(buffer
,"b"), "failed\n");
419 ok( r
==1, "return count wrong\n");
422 r
= sprintf(buffer
, format
,'a');
423 ok(!strcmp(buffer
," a"), "failed\n");
424 ok( r
==3, "return count wrong\n");
427 r
= sprintf(buffer
, format
,1234);
428 ok(!strcmp(buffer
,"1234"), "failed\n");
429 ok( r
==4, "return count wrong\n");
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");
443 r
= sprintf(buffer
, format
, &x
);
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
);
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
);
458 r
= sprintf(buffer
, format
,2);
459 ok(!strcmp(buffer
,"2"), "failed\n");
460 ok( r
==1, "return count wrong\n");
463 r
= sprintf(buffer
, format
,8.6);
464 ok(!strcmp(buffer
,"8.6000"), "failed\n");
465 ok( r
==6, "return count wrong\n");
468 r
= sprintf(buffer
, format
,0.6);
469 ok(!strcmp(buffer
,"0.600000"), "failed\n");
470 ok( r
==8, "return count wrong\n");
473 r
= sprintf(buffer
, format
,0.6);
474 ok(!strcmp(buffer
,"1"), "failed\n");
475 ok( r
==1, "return count wrong\n");
478 r
= sprintf(buffer
, format
,8.6);
479 ok(!strcmp(buffer
,"8.6000e+000"), "failed\n");
480 ok( r
==11, "return count wrong\n");
483 r
= sprintf(buffer
, format
,8.6);
484 ok(!strcmp(buffer
,"8.6"), "failed\n");
485 ok( r
==3, "return count wrong\n");
488 r
= sprintf(buffer
, format
,-1);
489 ok(!strcmp(buffer
,"-1"), "failed\n");
490 ok( r
==2, "return count wrong\n");
493 r
= sprintf(buffer
, format
,1);
494 ok(!strcmp(buffer
,"1"), "failed\n");
495 ok( r
==1, "return count wrong\n");
498 r
= sprintf(buffer
, format
,1);
499 ok(!strcmp(buffer
,"+1"), "failed\n");
500 ok( r
==2, "return count wrong\n");
503 r
= sprintf(buffer
, format
,10);
504 ok(!strcmp(buffer
,"12"), "failed\n");
505 ok( r
==2, "return count wrong\n");
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");
516 ok(!strcmp(buffer
,"00000000"), "failed\n");
517 ok( r
==8, "return count wrong\n");
521 r
= sprintf(buffer
, format
,0);
522 ok(!strcmp(buffer
,"(null)"), "failed\n");
523 ok( r
==6, "return count wrong\n");
526 r
= sprintf(buffer
, format
,"%%%%");
527 ok(!strcmp(buffer
,"%%%%"), "failed\n");
528 ok( r
==4, "return count wrong\n");
531 r
= sprintf(buffer
, format
,-1);
532 ok(!strcmp(buffer
,"4294967295"), "failed\n");
533 ok( r
==10, "return count wrong\n");
536 r
= sprintf(buffer
, format
,-1);
537 ok(!strcmp(buffer
,""), "failed\n");
538 ok( r
==0, "return count wrong\n");
541 r
= sprintf(buffer
, format
,-1);
542 ok(!strcmp(buffer
,""), "failed\n");
543 ok( r
==0, "return count wrong\n");
546 r
= sprintf(buffer
, format
,-1);
547 ok(!strcmp(buffer
,"z"), "failed\n");
548 ok( r
==1, "return count wrong\n");
551 r
= sprintf(buffer
, format
,-1);
552 ok(!strcmp(buffer
,"j"), "failed\n");
553 ok( r
==1, "return count wrong\n");
556 r
= sprintf(buffer
, format
,-1);
557 ok(!strcmp(buffer
,""), "failed\n");
558 ok( r
==0, "return count wrong\n");
561 r
= sprintf(buffer
, format
,-1);
562 ok(!strcmp(buffer
,"H"), "failed\n");
563 ok( r
==1, "return count wrong\n");
566 r
= sprintf(buffer
, format
, 0x100+'X');
567 ok(!strcmp(buffer
,"xXx"), "failed\n");
568 ok( r
==3, "return count wrong\n");
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 )
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
{
604 /* Pre-2.1 libc behaviour, not C99 compliant. */
605 const struct snprintf_test tests
[] = {{"short", 5},
610 const int bufsiz
= sizeof buffer
;
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",
621 ok (!memcmp (fmt
, buffer
, valid
),
622 "\"%s\": rendered \"%.*s\"\n", fmt
, valid
, buffer
);
626 static void test_fcvt(void)
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 */
728 const char *expstr_e
;
729 const char *expstr_f
;
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},
773 static void test_xcvt(void)
776 int i
, decpt
, sign
, err
;
778 for( i
= 0; strcmp( test_cvt_testcases
[i
].expstr_e
, "END"); i
++){
780 str
= _ecvt( test_cvt_testcases
[i
].value
,
781 test_cvt_testcases
[i
].nrdigits
,
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
++){
796 str
= _fcvt( test_cvt_testcases
[i
].value
,
797 test_cvt_testcases
[i
].nrdigits
,
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
);
814 for( i
= 0; strcmp( test_cvt_testcases
[i
].expstr_e
, "END"); i
++){
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
);
831 win_skip("_ecvt_s not available\n");
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
);
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
++){
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
);
876 win_skip("_fcvt_s not available\n");
879 static int __cdecl
_vsnwprintf_wrapper(wchar_t *str
, size_t len
, const wchar_t *format
, ...)
883 __ms_va_start(valist
, format
);
884 ret
= _vsnwprintf(str
, len
, format
, valist
);
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};
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
, ...)
911 __ms_va_start(valist
, format
);
912 ret
= p__vscprintf(format
, valist
);
917 static void test_vscprintf(void)
923 win_skip("_vscprintf not available\n");
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
, ...)
935 __ms_va_start(valist
, format
);
936 ret
= p__vscwprintf(format
, valist
);
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};
950 win_skip("_vscwprintf not available\n");
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
, ...)
963 __ms_va_start(valist
, format
);
964 ret
= p__vsnwprintf_s(str
, sizeOfBuffer
, count
, format
, valist
);
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 };
979 if (!p__vsnwprintf_s
)
981 win_skip("_vsnwprintf_s not available\n");
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. */
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
));
1028 test_vsnwprintf_s();