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
34 #include "wine/test.h"
36 static int (__cdecl
*p__vscprintf
)(const char *format
, __ms_va_list valist
);
37 static int (__cdecl
*p__vscwprintf
)(const wchar_t *format
, __ms_va_list valist
);
38 static int (__cdecl
*p__vsnwprintf_s
)(wchar_t *str
, size_t sizeOfBuffer
,
39 size_t count
, const wchar_t *format
,
42 static void init( void )
44 HMODULE hmod
= GetModuleHandleA("msvcrt.dll");
46 p__vscprintf
= (void *)GetProcAddress(hmod
, "_vscprintf");
47 p__vscwprintf
= (void *)GetProcAddress(hmod
, "_vscwprintf");
48 p__vsnwprintf_s
= (void *)GetProcAddress(hmod
, "_vsnwprintf_s");
51 static void test_sprintf( void )
55 double pnumber
=789456123;
57 WCHAR wide
[] = { 'w','i','d','e',0};
60 r
= sprintf(buffer
,format
,pnumber
);
61 ok(!strcmp(buffer
,"+7.894561230000000e+008"),"+#23.15e failed: '%s'\n", buffer
);
62 ok( r
==23, "return count wrong\n");
65 r
= sprintf(buffer
,format
,pnumber
);
66 ok(!strcmp(buffer
,"7.894561230000000e+008 "),"-#23.15e failed: '%s'\n", buffer
);
67 ok( r
==23, "return count wrong\n");
70 r
= sprintf(buffer
,format
,pnumber
);
71 ok(!strcmp(buffer
," 7.894561230000000e+008"),"#23.15e failed: '%s'\n", buffer
);
72 ok( r
==23, "return count wrong\n");
75 r
= sprintf(buffer
,format
,pnumber
);
76 ok(!strcmp(buffer
,"8.e+008"),"#1.1g failed: '%s'\n", buffer
);
77 ok( r
==7, "return count wrong\n");
80 r
= sprintf(buffer
,format
,((ULONGLONG
)0xffffffff)*0xffffffff);
81 ok(!strcmp(buffer
,"-8589934591"),"Problem with long long\n");
82 ok( r
==11, "return count wrong\n");
85 r
= sprintf(buffer
,format
,(LONGLONG
)100);
86 ok(!strcmp(buffer
," +100") && r
==8,"+8I64d failed: '%s'\n", buffer
);
89 r
= sprintf(buffer
,format
,(LONGLONG
)100);
90 ok(!strcmp(buffer
,"+00000100") && r
==9,"+.8I64d failed: '%s'\n", buffer
);
92 format
= "%+10.8I64d";
93 r
= sprintf(buffer
,format
,(LONGLONG
)100);
94 ok(!strcmp(buffer
," +00000100") && r
==10,"+10.8I64d failed: '%s'\n", buffer
);
96 r
= sprintf(buffer
,format
,(LONGLONG
)100);
97 ok(!strcmp(buffer
,"_1I64d") && r
==6,"_1I64d failed\n");
100 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
101 ok(!strcmp(buffer
,"-00100") && r
==6,"-1.5I64d failed: '%s'\n", buffer
);
104 r
= sprintf(buffer
,format
,(LONGLONG
)100);
105 ok(!strcmp(buffer
," 100") && r
==5,"5I64d failed: '%s'\n", buffer
);
108 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
109 ok(!strcmp(buffer
," -100") && r
==5,"5I64d failed: '%s'\n", buffer
);
112 r
= sprintf(buffer
,format
,(LONGLONG
)100);
113 ok(!strcmp(buffer
,"100 ") && r
==5,"-5I64d failed: '%s'\n", buffer
);
116 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
117 ok(!strcmp(buffer
,"-100 ") && r
==5,"-5I64d failed: '%s'\n", buffer
);
120 r
= sprintf(buffer
,format
,(LONGLONG
)100);
121 ok(!strcmp(buffer
,"00100") && r
==5,"-.5I64d failed: '%s'\n", buffer
);
124 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
125 ok(!strcmp(buffer
,"-00100") && r
==6,"-.5I64d failed: '%s'\n", buffer
);
127 format
= "%-8.5I64d";
128 r
= sprintf(buffer
,format
,(LONGLONG
)100);
129 ok(!strcmp(buffer
,"00100 ") && r
==8,"-8.5I64d failed: '%s'\n", buffer
);
131 format
= "%-8.5I64d";
132 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
133 ok(!strcmp(buffer
,"-00100 ") && r
==8,"-8.5I64d failed: '%s'\n", buffer
);
136 r
= sprintf(buffer
,format
,(LONGLONG
)100);
137 ok(!strcmp(buffer
,"00100") && r
==5,"05I64d failed: '%s'\n", buffer
);
140 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
141 ok(!strcmp(buffer
,"-0100") && r
==5,"05I64d failed: '%s'\n", buffer
);
144 r
= sprintf(buffer
,format
,(LONGLONG
)100);
145 ok(!strcmp(buffer
," 100") && r
==4,"' I64d' failed: '%s'\n", buffer
);
148 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
149 ok(!strcmp(buffer
,"-100") && r
==4,"' I64d' failed: '%s'\n", buffer
);
152 r
= sprintf(buffer
,format
,(LONGLONG
)100);
153 ok(!strcmp(buffer
," 100") && r
==5,"' 5I64d' failed: '%s'\n", buffer
);
156 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
157 ok(!strcmp(buffer
," -100") && r
==5,"' 5I64d' failed: '%s'\n", buffer
);
160 r
= sprintf(buffer
,format
,(LONGLONG
)100);
161 ok(!strcmp(buffer
," 00100") && r
==6,"' .5I64d' failed: '%s'\n", buffer
);
164 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
165 ok(!strcmp(buffer
,"-00100") && r
==6,"' .5I64d' failed: '%s'\n", buffer
);
167 format
= "% 8.5I64d";
168 r
= sprintf(buffer
,format
,(LONGLONG
)100);
169 ok(!strcmp(buffer
," 00100") && r
==8,"' 8.5I64d' failed: '%s'\n", buffer
);
171 format
= "% 8.5I64d";
172 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
173 ok(!strcmp(buffer
," -00100") && r
==8,"' 8.5I64d' failed: '%s'\n", buffer
);
176 r
= sprintf(buffer
,format
,(LONGLONG
)0);
177 ok(r
==0,".0I64d failed: '%s'\n", buffer
);
179 format
= "%#+21.18I64x";
180 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
181 ok(!strcmp(buffer
," 0x00ffffffffffffff9c") && r
==21,"#+21.18I64x failed: '%s'\n", buffer
);
183 format
= "%#.25I64o";
184 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
185 ok(!strcmp(buffer
,"0001777777777777777777634") && r
==25,"#.25I64o failed: '%s'\n", buffer
);
187 format
= "%#+24.20I64o";
188 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
189 ok(!strcmp(buffer
," 01777777777777777777634") && r
==24,"#+24.20I64o failed: '%s'\n", buffer
);
191 format
= "%#+18.21I64X";
192 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
193 ok(!strcmp(buffer
,"0X00000FFFFFFFFFFFFFF9C") && r
==23,"#+18.21I64X failed: '%s '\n", buffer
);
195 format
= "%#+20.24I64o";
196 r
= sprintf(buffer
,format
,(LONGLONG
)-100);
197 ok(!strcmp(buffer
,"001777777777777777777634") && r
==24,"#+20.24I64o failed: '%s'\n", buffer
);
199 format
= "%#+25.22I64u";
200 r
= sprintf(buffer
,format
,(LONGLONG
)-1);
201 ok(!strcmp(buffer
," 0018446744073709551615") && r
==25,"#+25.22I64u conversion failed: '%s'\n", buffer
);
203 format
= "%#+25.22I64u";
204 r
= sprintf(buffer
,format
,(LONGLONG
)-1);
205 ok(!strcmp(buffer
," 0018446744073709551615") && r
==25,"#+25.22I64u failed: '%s'\n", buffer
);
207 format
= "%#+30.25I64u";
208 r
= sprintf(buffer
,format
,(LONGLONG
)-1);
209 ok(!strcmp(buffer
," 0000018446744073709551615") && r
==30,"#+30.25I64u failed: '%s'\n", buffer
);
211 format
= "%+#25.22I64d";
212 r
= sprintf(buffer
,format
,(LONGLONG
)-1);
213 ok(!strcmp(buffer
," -0000000000000000000001") && r
==25,"+#25.22I64d failed: '%s'\n", buffer
);
215 format
= "%#-8.5I64o";
216 r
= sprintf(buffer
,format
,(LONGLONG
)100);
217 ok(!strcmp(buffer
,"00144 ") && r
==8,"-8.5I64o failed: '%s'\n", buffer
);
219 format
= "%#-+ 08.5I64d";
220 r
= sprintf(buffer
,format
,(LONGLONG
)100);
221 ok(!strcmp(buffer
,"+00100 ") && r
==8,"'#-+ 08.5I64d failed: '%s'\n", buffer
);
223 format
= "%#-+ 08.5I64d";
224 r
= sprintf(buffer
,format
,(LONGLONG
)100);
225 ok(!strcmp(buffer
,"+00100 ") && r
==8,"#-+ 08.5I64d failed: '%s'\n", buffer
);
228 r
= sprintf(buffer
,format
,(LONGLONG
)1);
229 ok(r
==80,"%s format failed\n", format
);
231 format
= "% .80I64d";
232 r
= sprintf(buffer
,format
,(LONGLONG
)1);
233 ok(r
==81,"%s format failed\n", format
);
236 r
= sprintf(buffer
,format
,1);
237 ok(r
==81,"%s format failed\n", format
);
240 r
= sprintf(buffer
,format
,((ULONGLONG
)0xffffffff)*0xffffffff);
241 ok( r
== 1 || r
== 11, "return count wrong %d\n", r
);
242 if (r
== 11) /* %ll works on Vista */
243 ok(!strcmp(buffer
, "-8589934591"), "Problem with \"ll\" interpretation '%s'\n", buffer
);
245 ok(!strcmp(buffer
, "1"), "Problem with \"ll\" interpretation '%s'\n", buffer
);
248 r
= sprintf(buffer
,format
,1);
249 ok(!strcmp(buffer
, "I"), "Problem with \"I\" interpretation\n");
250 ok( r
==1, "return count wrong\n");
253 r
= sprintf(buffer
,format
,1);
254 ok(!strcmp(buffer
,"I0d"),"I0d failed\n");
255 ok( r
==3, "return count wrong\n");
258 r
= sprintf(buffer
,format
,1);
261 ok(!strcmp(buffer
,"1"),"I32d failed, got '%s'\n",buffer
);
265 /* Older versions don't grok I32 format */
266 ok(r
== 4 && !strcmp(buffer
,"I32d"),"I32d failed, got '%s',%d\n",buffer
,r
);
270 r
= sprintf(buffer
,format
,(LONGLONG
)-1);
271 ok(!strcmp(buffer
,"D"),"I64D failed: %s\n",buffer
);
272 ok( r
==1, "return count wrong\n");
275 r
= sprintf(buffer
,format
,1);
276 ok(!strcmp(buffer
, " 1"),"Problem with sign place-holder: '%s'\n",buffer
);
277 ok( r
==2, "return count wrong\n");
280 r
= sprintf(buffer
,format
,1);
281 ok(!strcmp(buffer
, "+1"),"Problem with sign flags: '%s'\n",buffer
);
282 ok( r
==2, "return count wrong\n");
285 r
= sprintf(buffer
,format
,wide
);
286 ok(!strcmp(buffer
,"wide"),"Problem with wide string format\n");
287 ok( r
==4, "return count wrong\n");
290 r
= sprintf(buffer
,format
,'1');
291 ok(!strcmp(buffer
,"0001"),"Character not zero-prefixed \"%s\"\n",buffer
);
292 ok( r
==4, "return count wrong\n");
295 r
= sprintf(buffer
,format
,'1');
296 ok(!strcmp(buffer
,"1 "),"Character zero-padded and/or not left-adjusted \"%s\"\n",buffer
);
297 ok( r
==4, "return count wrong\n");
299 if (sizeof(void *) == 8)
302 r
= sprintf(buffer
,format
,(void *)57);
303 ok(!strcmp(buffer
,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer
);
304 ok( r
==16, "return count wrong\n");
307 r
= sprintf(buffer
,format
,(void *)57);
308 ok(!strcmp(buffer
," 0X0000000000000039"),"Pointer formatted incorrectly\n");
309 ok( r
==20, "return count wrong\n");
312 r
= sprintf(buffer
,format
,(void *)57);
313 ok(!strcmp(buffer
,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer
);
314 ok( r
==16, "return count wrong\n");
317 r
= sprintf(buffer
,format
,(void *)57);
318 ok(!strcmp(buffer
,"0X0000000000000039 "),"Pointer formatted incorrectly\n");
319 ok( r
==20, "return count wrong\n");
324 r
= sprintf(buffer
,format
,(void *)57);
325 ok(!strcmp(buffer
,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer
);
326 ok( r
==8, "return count wrong\n");
329 r
= sprintf(buffer
,format
,(void *)57);
330 ok(!strcmp(buffer
," 0X00000039"),"Pointer formatted incorrectly\n");
331 ok( r
==12, "return count wrong\n");
334 r
= sprintf(buffer
,format
,(void *)57);
335 ok(!strcmp(buffer
,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer
);
336 ok( r
==8, "return count wrong\n");
339 r
= sprintf(buffer
,format
,(void *)57);
340 ok(!strcmp(buffer
,"0X00000039 "),"Pointer formatted incorrectly\n");
341 ok( r
==12, "return count wrong\n");
345 r
= sprintf(buffer
,format
,"foo");
346 ok(!strcmp(buffer
,"0foo"),"String not zero-prefixed \"%s\"\n",buffer
);
347 ok( r
==4, "return count wrong\n");
350 r
= sprintf(buffer
,format
,"foo");
351 ok(!strcmp(buffer
,"f"),"Precision ignored \"%s\"\n",buffer
);
352 ok( r
==1, "return count wrong\n");
355 r
= sprintf(buffer
,format
,1,"foo");
356 ok(!strcmp(buffer
,"f"),"Precision ignored \"%s\"\n",buffer
);
357 ok( r
==1, "return count wrong\n");
360 r
= sprintf(buffer
,format
,-5,"foo");
361 ok(!strcmp(buffer
,"foo "),"Negative field width ignored \"%s\"\n",buffer
);
362 ok( r
==5, "return count wrong\n");
365 r
= sprintf(buffer
, format
);
366 ok(!strcmp(buffer
,"hello"), "failed\n");
367 ok( r
==5, "return count wrong\n");
370 r
= sprintf(buffer
, format
, wide
);
371 ok(!strcmp(buffer
,"wide"), "failed\n");
372 ok( r
==4, "return count wrong\n");
375 r
= sprintf(buffer
, format
, wide
);
376 ok(!strcmp(buffer
,"wide "), "failed\n");
377 ok( r
==10, "return count wrong\n");
380 r
= sprintf(buffer
, format
, wide
);
381 ok(!strcmp(buffer
," wide"), "failed\n");
382 ok( r
==10, "return count wrong\n");
384 format
= "%#+ -03whlls";
385 r
= sprintf(buffer
, format
, wide
);
386 ok(!strcmp(buffer
,"wide"), "failed\n");
387 ok( r
==4, "return count wrong\n");
390 r
= sprintf(buffer
, format
, wide
);
391 ok(!strcmp(buffer
,"0s"), "failed\n");
392 ok( r
==2, "return count wrong\n");
395 r
= sprintf(buffer
, format
, wide
);
396 ok(!strcmp(buffer
,"-s"), "failed\n");
397 ok( r
==2, "return count wrong\n");
400 r
= sprintf(buffer
, format
, wide
);
401 ok(!strcmp(buffer
,"wide"), "failed\n");
402 ok( r
==4, "return count wrong\n");
405 r
= sprintf(buffer
, format
, "not wide" );
406 ok(!strcmp(buffer
,"not wide"), "failed\n");
407 ok( r
==8, "return count wrong\n");
410 r
= sprintf(buffer
, format
);
411 ok(!strcmp(buffer
,"b"), "failed\n");
412 ok( r
==1, "return count wrong\n");
415 r
= sprintf(buffer
, format
,'a');
416 ok(!strcmp(buffer
," a"), "failed\n");
417 ok( r
==3, "return count wrong\n");
420 r
= sprintf(buffer
, format
,1234);
421 ok(!strcmp(buffer
,"1234"), "failed\n");
422 ok( r
==4, "return count wrong\n");
425 r
= sprintf(buffer
, format
);
426 ok(!strcmp(buffer
,""), "failed\n");
427 ok( r
==0, "return count wrong\n");
429 format
= "%j%k%m%q%r%t%v%y%z";
430 r
= sprintf(buffer
, format
);
431 ok(!strcmp(buffer
,"jkmqrtvyz"), "failed\n");
432 ok( r
==9, "return count wrong\n");
436 r
= sprintf(buffer
, format
, &x
);
439 /* %n format is disabled by default on vista */
440 /* FIXME: should test with _set_printf_count_output */
441 ok(x
== 0, "should not write to x: %d\n", x
);
445 ok(x
== 4, "should write to x: %d\n", x
);
446 ok(!strcmp(buffer
,"asdf"), "failed\n");
447 ok( r
==4, "return count wrong: %d\n", r
);
451 r
= sprintf(buffer
, format
,2);
452 ok(!strcmp(buffer
,"2"), "failed\n");
453 ok( r
==1, "return count wrong\n");
456 r
= sprintf(buffer
, format
,8.6);
457 ok(!strcmp(buffer
,"8.6000"), "failed\n");
458 ok( r
==6, "return count wrong\n");
461 r
= sprintf(buffer
, format
,0.6);
462 ok(!strcmp(buffer
,"0.600000"), "failed\n");
463 ok( r
==8, "return count wrong\n");
466 r
= sprintf(buffer
, format
,0.6);
467 ok(!strcmp(buffer
,"1"), "failed\n");
468 ok( r
==1, "return count wrong\n");
471 r
= sprintf(buffer
, format
,8.6);
472 ok(!strcmp(buffer
,"8.6000e+000"), "failed\n");
473 ok( r
==11, "return count wrong\n");
476 r
= sprintf(buffer
, format
,8.6);
477 ok(!strcmp(buffer
,"8.6"), "failed\n");
478 ok( r
==3, "return count wrong\n");
481 r
= sprintf(buffer
, format
,-1);
482 ok(!strcmp(buffer
,"-1"), "failed\n");
483 ok( r
==2, "return count wrong\n");
486 r
= sprintf(buffer
, format
,1);
487 ok(!strcmp(buffer
,"1"), "failed\n");
488 ok( r
==1, "return count wrong\n");
491 r
= sprintf(buffer
, format
,1);
492 ok(!strcmp(buffer
,"+1"), "failed\n");
493 ok( r
==2, "return count wrong\n");
496 r
= sprintf(buffer
, format
,10);
497 ok(!strcmp(buffer
,"12"), "failed\n");
498 ok( r
==2, "return count wrong\n");
501 r
= sprintf(buffer
, format
,0);
502 if (sizeof(void *) == 8)
504 ok(!strcmp(buffer
,"0000000000000000"), "failed\n");
505 ok( r
==16, "return count wrong\n");
509 ok(!strcmp(buffer
,"00000000"), "failed\n");
510 ok( r
==8, "return count wrong\n");
514 r
= sprintf(buffer
, format
,0);
515 ok(!strcmp(buffer
,"(null)"), "failed\n");
516 ok( r
==6, "return count wrong\n");
519 r
= sprintf(buffer
, format
,"%%%%");
520 ok(!strcmp(buffer
,"%%%%"), "failed\n");
521 ok( r
==4, "return count wrong\n");
524 r
= sprintf(buffer
, format
,-1);
525 ok(!strcmp(buffer
,"4294967295"), "failed\n");
526 ok( r
==10, "return count wrong\n");
529 r
= sprintf(buffer
, format
,-1);
530 ok(!strcmp(buffer
,""), "failed\n");
531 ok( r
==0, "return count wrong\n");
534 r
= sprintf(buffer
, format
,-1);
535 ok(!strcmp(buffer
,""), "failed\n");
536 ok( r
==0, "return count wrong\n");
539 r
= sprintf(buffer
, format
,-1);
540 ok(!strcmp(buffer
,"z"), "failed\n");
541 ok( r
==1, "return count wrong\n");
544 r
= sprintf(buffer
, format
,-1);
545 ok(!strcmp(buffer
,"j"), "failed\n");
546 ok( r
==1, "return count wrong\n");
549 r
= sprintf(buffer
, format
,-1);
550 ok(!strcmp(buffer
,""), "failed\n");
551 ok( r
==0, "return count wrong\n");
554 r
= sprintf(buffer
, format
,-1);
555 ok(!strcmp(buffer
,"H"), "failed\n");
556 ok( r
==1, "return count wrong\n");
559 r
= sprintf(buffer
, format
, 0x100+'X');
560 ok(!strcmp(buffer
,"xXx"), "failed\n");
561 ok( r
==3, "return count wrong\n");
564 r
= sprintf(buffer
, format
);
565 ok(!strcmp(buffer
,"%0"), "failed: \"%s\"\n", buffer
);
566 ok( r
==2, "return count wrong\n");
569 static void test_swprintf( void )
572 const wchar_t I64d
[] = {'%','I','6','4','d',0};
573 double pnumber
=789456123;
574 const wchar_t TwentyThreePoint15e
[]= {'%','+','#','2','3','.','1','5','e',0};
575 const wchar_t e008
[] = {'e','+','0','0','8',0};
576 const wchar_t string_w
[] = {'s','t','r','i','n','g',0};
577 const char string
[] = "string";
578 const wchar_t S
[]={'%','S',0};
579 const wchar_t hs
[] = {'%', 'h', 's', 0};
581 swprintf(buffer
,TwentyThreePoint15e
,pnumber
);
582 ok(wcsstr(buffer
,e008
) != 0,"Sprintf different\n");
583 swprintf(buffer
,I64d
,((ULONGLONG
)0xffffffff)*0xffffffff);
584 ok(wcslen(buffer
) == 11,"Problem with long long\n");
585 swprintf(buffer
,S
,string
);
586 ok(wcslen(buffer
) == 6,"Problem with \"%%S\" interpretation\n");
587 swprintf(buffer
, hs
, string
);
588 ok( wcscmp(string_w
,buffer
) == 0, "swprintf failed with %%hs\n");
591 static void test_snprintf (void)
593 struct snprintf_test
{
597 /* Pre-2.1 libc behaviour, not C99 compliant. */
598 const struct snprintf_test tests
[] = {{"short", 5},
603 const int bufsiz
= sizeof buffer
;
606 for (i
= 0; i
< sizeof tests
/ sizeof tests
[0]; i
++) {
607 const char *fmt
= tests
[i
].format
;
608 const int expect
= tests
[i
].expected
;
609 const int n
= _snprintf (buffer
, bufsiz
, fmt
);
610 const int valid
= n
< 0 ? bufsiz
: (n
== bufsiz
? n
: n
+1);
612 ok (n
== expect
, "\"%s\": expected %d, returned %d\n",
614 ok (!memcmp (fmt
, buffer
, valid
),
615 "\"%s\": rendered \"%.*s\"\n", fmt
, valid
, buffer
);
619 static void test_fcvt(void)
622 int dec
=100, sign
=100;
624 /* Numbers less than 1.0 with different precisions */
625 str
= _fcvt(0.0001, 1, &dec
, &sign
);
626 ok( 0 == strcmp(str
,""), "bad return '%s'\n", str
);
627 ok( -3 == dec
, "dec wrong %d\n", dec
);
628 ok( 0 == sign
, "sign wrong\n");
630 str
= _fcvt(0.0001, -10, &dec
, &sign
);
631 ok( 0 == strcmp(str
,""), "bad return '%s'\n", str
);
632 ok( -3 == dec
, "dec wrong %d\n", dec
);
633 ok( 0 == sign
, "sign wrong\n");
635 str
= _fcvt(0.0001, 10, &dec
, &sign
);
636 ok( 0 == strcmp(str
,"1000000"), "bad return '%s'\n", str
);
637 ok( -3 == dec
, "dec wrong %d\n", dec
);
638 ok( 0 == sign
, "sign wrong\n");
640 /* Basic sign test */
641 str
= _fcvt(-111.0001, 5, &dec
, &sign
);
642 ok( 0 == strcmp(str
,"11100010"), "bad return '%s'\n", str
);
643 ok( 3 == dec
, "dec wrong %d\n", dec
);
644 ok( 1 == sign
, "sign wrong\n");
646 str
= _fcvt(111.0001, 5, &dec
, &sign
);
647 ok( 0 == strcmp(str
,"11100010"), "bad return '%s'\n", str
);
648 ok( 3 == dec
, "dec wrong\n");
649 ok( 0 == sign
, "sign wrong\n");
651 /* 0.0 with different precisions */
652 str
= _fcvt(0.0, 5, &dec
, &sign
);
653 ok( 0 == strcmp(str
,"00000"), "bad return '%s'\n", str
);
654 ok( 0 == dec
, "dec wrong %d\n", dec
);
655 ok( 0 == sign
, "sign wrong\n");
657 str
= _fcvt(0.0, 0, &dec
, &sign
);
658 ok( 0 == strcmp(str
,""), "bad return '%s'\n", str
);
659 ok( 0 == dec
, "dec wrong %d\n", dec
);
660 ok( 0 == sign
, "sign wrong\n");
662 str
= _fcvt(0.0, -1, &dec
, &sign
);
663 ok( 0 == strcmp(str
,""), "bad return '%s'\n", str
);
664 ok( 0 == dec
, "dec wrong %d\n", dec
);
665 ok( 0 == sign
, "sign wrong\n");
667 /* Numbers > 1.0 with 0 or -ve precision */
668 str
= _fcvt(-123.0001, 0, &dec
, &sign
);
669 ok( 0 == strcmp(str
,"123"), "bad return '%s'\n", str
);
670 ok( 3 == dec
, "dec wrong %d\n", dec
);
671 ok( 1 == sign
, "sign wrong\n");
673 str
= _fcvt(-123.0001, -1, &dec
, &sign
);
674 ok( 0 == strcmp(str
,"12"), "bad return '%s'\n", str
);
675 ok( 3 == dec
, "dec wrong %d\n", dec
);
676 ok( 1 == sign
, "sign wrong\n");
678 str
= _fcvt(-123.0001, -2, &dec
, &sign
);
679 ok( 0 == strcmp(str
,"1"), "bad return '%s'\n", str
);
680 ok( 3 == dec
, "dec wrong %d\n", dec
);
681 ok( 1 == sign
, "sign wrong\n");
683 str
= _fcvt(-123.0001, -3, &dec
, &sign
);
684 ok( 0 == strcmp(str
,""), "bad return '%s'\n", str
);
685 ok( 3 == dec
, "dec wrong %d\n", dec
);
686 ok( 1 == sign
, "sign wrong\n");
688 /* Numbers > 1.0, but with rounding at the point of precision */
689 str
= _fcvt(99.99, 1, &dec
, &sign
);
690 ok( 0 == strcmp(str
,"1000"), "bad return '%s'\n", str
);
691 ok( 3 == dec
, "dec wrong %d\n", dec
);
692 ok( 0 == sign
, "sign wrong\n");
694 /* Numbers < 1.0 where rounding occurs at the point of precision */
695 str
= _fcvt(0.00636, 2, &dec
, &sign
);
696 ok( 0 == strcmp(str
,"1"), "bad return '%s'\n", str
);
697 ok( -1 == dec
, "dec wrong %d\n", dec
);
698 ok( 0 == sign
, "sign wrong\n");
700 str
= _fcvt(0.00636, 3, &dec
, &sign
);
701 ok( 0 == strcmp(str
,"6"), "bad return '%s'\n", str
);
702 ok( -2 == dec
, "dec wrong %d\n", dec
);
703 ok( 0 == sign
, "sign wrong\n");
705 str
= _fcvt(0.09999999996, 2, &dec
, &sign
);
706 ok( 0 == strcmp(str
,"10"), "bad return '%s'\n", str
);
707 ok( 0 == dec
, "dec wrong %d\n", dec
);
708 ok( 0 == sign
, "sign wrong\n");
710 str
= _fcvt(0.6, 0, &dec
, &sign
);
711 ok( 0 == strcmp(str
,"1"), "bad return '%s'\n", str
);
712 ok( 1 == dec
, "dec wrong %d\n", dec
);
713 ok( 0 == sign
, "sign wrong\n");
716 /* Don't test nrdigits < 0, msvcrt on Win9x and NT4 will corrupt memory by
717 * writing outside allocated memory */
721 const char *expstr_e
;
722 const char *expstr_f
;
726 } test_cvt_testcases
[] = {
727 { 45.0, 2, "45", "4500", 2, 2, 0 },
728 /* Numbers less than 1.0 with different precisions */
729 { 0.0001, 1, "1", "", -3, -3, 0 },
730 { 0.0001, 10,"1000000000", "1000000", -3, -3, 0 },
731 /* Basic sign test */
732 { -111.0001, 5, "11100", "11100010", 3, 3, 1 },
733 { 111.0001, 5, "11100", "11100010", 3, 3, 0 },
734 /* big numbers with low precision */
735 { 3333.3, 2, "33", "333330", 4, 4, 0 },
736 {999999999999.9, 3, "100","999999999999900", 13, 12, 0 },
737 /* 0.0 with different precisions */
738 { 0.0, 5, "00000", "00000", 0, 0, 0 },
739 { 0.0, 0, "", "", 0, 0, 0 },
740 { 0.0, -1, "", "", 0, 0, 0 },
741 /* Numbers > 1.0 with 0 or -ve precision */
742 { -123.0001, 0, "", "123", 3, 3, 1 },
743 { -123.0001, -1, "", "12", 3, 3, 1 },
744 { -123.0001, -2, "", "1", 3, 3, 1 },
745 { -123.0001, -3, "", "", 3, 3, 1 },
746 /* Numbers > 1.0, but with rounding at the point of precision */
747 { 99.99, 1, "1", "1000", 3, 3, 0 },
748 /* Numbers < 1.0 where rounding occurs at the point of precision */
749 { 0.0063, 2, "63", "1", -2, -1, 0 },
750 { 0.0063, 3, "630", "6", -2, -2, 0 },
751 { 0.09999999996, 2, "10", "10", 0, 0, 0 },
752 { 0.6, 1, "6", "6", 0, 0, 0 },
753 { 0.6, 0, "", "1", 1, 1, 0 },
754 { 0.4, 0, "", "", 0, 0, 0 },
755 { 0.49, 0, "", "", 0, 0, 0 },
756 { 0.51, 0, "", "1", 1, 1, 0 },
757 /* ask ridiculous amunt of precision, ruin formatting this table */
758 { 1.0, 30, "100000000000000000000000000000",
759 "1000000000000000000000000000000", 1, 1, 0},
760 { 123456789012345678901.0, 30, "123456789012345680000000000000",
761 "123456789012345680000000000000000000000000000000000", 21, 21, 0},
766 static void test_xcvt(void)
770 for( i
= 0; strcmp( test_cvt_testcases
[i
].expstr_e
, "END"); i
++){
772 str
= _ecvt( test_cvt_testcases
[i
].value
,
773 test_cvt_testcases
[i
].nrdigits
,
776 ok( 0 == strncmp( str
, test_cvt_testcases
[i
].expstr_e
, 15),
777 "_ecvt() bad return, got \n'%s' expected \n'%s'\n", str
,
778 test_cvt_testcases
[i
].expstr_e
);
779 ok( decpt
== test_cvt_testcases
[i
].expdecpt_e
,
780 "_ecvt() decimal point wrong, got %d expected %d\n", decpt
,
781 test_cvt_testcases
[i
].expdecpt_e
);
783 for( i
= 0; strcmp( test_cvt_testcases
[i
].expstr_e
, "END"); i
++){
785 str
= _fcvt( test_cvt_testcases
[i
].value
,
786 test_cvt_testcases
[i
].nrdigits
,
789 ok( 0 == strncmp( str
, test_cvt_testcases
[i
].expstr_f
, 15),
790 "_fcvt() bad return, got \n'%s' expected \n'%s'\n", str
,
791 test_cvt_testcases
[i
].expstr_f
);
792 ok( decpt
== test_cvt_testcases
[i
].expdecpt_f
,
793 "_fcvt() decimal point wrong, got %d expected %d\n", decpt
,
794 test_cvt_testcases
[i
].expdecpt_f
);
795 ok( sign
== test_cvt_testcases
[i
].expsign
,
796 "_ecvt() sign wrong, got %d expected %d\n", sign
,
797 test_cvt_testcases
[i
].expsign
);
801 static int __cdecl
_vsnwprintf_wrapper(wchar_t *str
, size_t len
, const wchar_t *format
, ...)
805 __ms_va_start(valist
, format
);
806 ret
= _vsnwprintf(str
, len
, format
, valist
);
811 static void test_vsnwprintf(void)
813 const wchar_t format
[] = {'%','w','s','%','w','s','%','w','s',0};
814 const wchar_t one
[] = {'o','n','e',0};
815 const wchar_t two
[] = {'t','w','o',0};
816 const wchar_t three
[] = {'t','h','r','e','e',0};
822 ret
= _vsnwprintf_wrapper( str
, sizeof(str
)/sizeof(str
[0]), format
, one
, two
, three
);
824 ok( ret
== 11, "got %d expected 11\n", ret
);
825 WideCharToMultiByte( CP_ACP
, 0, str
, -1, buf
, sizeof(buf
), NULL
, NULL
);
826 ok( !strcmp(buf
, "onetwothree"), "got %s expected 'onetwothree'\n", buf
);
829 static int __cdecl
_vscprintf_wrapper(const char *format
, ...)
833 __ms_va_start(valist
, format
);
834 ret
= p__vscprintf(format
, valist
);
839 static void test_vscprintf(void)
845 win_skip("_vscprintf not available\n");
849 ret
= _vscprintf_wrapper( "%s %d", "number", 1 );
850 ok( ret
== 8, "got %d expected 8\n", ret
);
853 static int __cdecl
_vscwprintf_wrapper(const wchar_t *format
, ...)
857 __ms_va_start(valist
, format
);
858 ret
= p__vscwprintf(format
, valist
);
863 static void test_vscwprintf(void)
865 const wchar_t format
[] = {'%','s',' ','%','d',0};
866 const wchar_t number
[] = {'n','u','m','b','e','r',0};
872 win_skip("_vscwprintf not available\n");
876 ret
= _vscwprintf_wrapper( format
, number
, 1 );
877 ok( ret
== 8, "got %d expected 8\n", ret
);
880 static int __cdecl
_vsnwprintf_s_wrapper(wchar_t *str
, size_t sizeOfBuffer
,
881 size_t count
, const wchar_t *format
, ...)
885 __ms_va_start(valist
, format
);
886 ret
= p__vsnwprintf_s(str
, sizeOfBuffer
, count
, format
, valist
);
891 static void test_vsnwprintf_s(void)
893 const wchar_t format
[] = { 'A','B','%','u','C',0 };
894 const wchar_t out7
[] = { 'A','B','1','2','3','C',0 };
895 const wchar_t out6
[] = { 'A','B','1','2','3',0 };
896 const wchar_t out2
[] = { 'A',0 };
897 const wchar_t out1
[] = { 0 };
898 wchar_t buffer
[14] = { 0 };
901 if (!p__vsnwprintf_s
)
903 win_skip("_vsnwprintf_s not available\n");
910 got
= _vsnwprintf_s_wrapper(buffer
, 14, _TRUNCATE
, format
, 123);
911 ok( exp
== got
, "length wrong, expect=%d, got=%d\n", exp
, got
);
912 ok( !wcscmp(out7
, buffer
), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer
));
914 got
= _vsnwprintf_s_wrapper(buffer
, 12, _TRUNCATE
, format
, 123);
915 ok( exp
== got
, "length wrong, expect=%d, got=%d\n", exp
, got
);
916 ok( !wcscmp(out7
, buffer
), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer
));
918 got
= _vsnwprintf_s_wrapper(buffer
, 7, _TRUNCATE
, format
, 123);
919 ok( exp
== got
, "length wrong, expect=%d, got=%d\n", exp
, got
);
920 ok( !wcscmp(out7
, buffer
), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer
));
922 /* Not enough room. */
925 got
= _vsnwprintf_s_wrapper(buffer
, 6, _TRUNCATE
, format
, 123);
926 ok( exp
== got
, "length wrong, expect=%d, got=%d\n", exp
, got
);
927 ok( !wcscmp(out6
, buffer
), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer
));
929 got
= _vsnwprintf_s_wrapper(buffer
, 2, _TRUNCATE
, format
, 123);
930 ok( exp
== got
, "length wrong, expect=%d, got=%d\n", exp
, got
);
931 ok( !wcscmp(out2
, buffer
), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer
));
933 got
= _vsnwprintf_s_wrapper(buffer
, 1, _TRUNCATE
, format
, 123);
934 ok( exp
== got
, "length wrong, expect=%d, got=%d\n", exp
, got
);
935 ok( !wcscmp(out1
, buffer
), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer
));