push 378fe7a60681a28e8b22f62dcfe122d585b92570
[wine/hacks.git] / dlls / msvcrt / tests / printf.c
blobd25734e195e71b348ee6c3bc96826ba45028e986
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>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winnls.h"
34 #include "wine/test.h"
36 static void test_sprintf( void )
38 char buffer[100];
39 const char *format;
40 double pnumber=789456123;
41 int x, r;
42 WCHAR wide[] = { 'w','i','d','e',0};
44 format = "%+#23.15e";
45 r = sprintf(buffer,format,pnumber);
46 ok(!strcmp(buffer,"+7.894561230000000e+008"),"+#23.15e failed: '%s'\n", buffer);
47 ok( r==23, "return count wrong\n");
49 format = "%-#23.15e";
50 r = sprintf(buffer,format,pnumber);
51 ok(!strcmp(buffer,"7.894561230000000e+008 "),"-#23.15e failed: '%s'\n", buffer);
52 ok( r==23, "return count wrong\n");
54 format = "%#23.15e";
55 r = sprintf(buffer,format,pnumber);
56 ok(!strcmp(buffer," 7.894561230000000e+008"),"#23.15e failed: '%s'\n", buffer);
57 ok( r==23, "return count wrong\n");
59 format = "%#1.1g";
60 r = sprintf(buffer,format,pnumber);
61 ok(!strcmp(buffer,"8.e+008"),"#1.1g failed: '%s'\n", buffer);
62 ok( r==7, "return count wrong\n");
64 format = "%I64d";
65 r = sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
66 ok(!strcmp(buffer,"-8589934591"),"Problem with long long\n");
67 ok( r==11, "return count wrong\n");
69 format = "%+8I64d";
70 r = sprintf(buffer,format,(LONGLONG)100);
71 ok(!strcmp(buffer," +100") && r==8,"+8I64d failed: '%s'\n", buffer);
73 format = "%+.8I64d";
74 r = sprintf(buffer,format,(LONGLONG)100);
75 ok(!strcmp(buffer,"+00000100") && r==9,"+.8I64d failed: '%s'\n", buffer);
77 format = "%+10.8I64d";
78 r = sprintf(buffer,format,(LONGLONG)100);
79 ok(!strcmp(buffer," +00000100") && r==10,"+10.8I64d failed: '%s'\n", buffer);
80 format = "%_1I64d";
81 r = sprintf(buffer,format,(LONGLONG)100);
82 ok(!strcmp(buffer,"_1I64d") && r==6,"_1I64d failed\n");
84 format = "%-1.5I64d";
85 r = sprintf(buffer,format,(LONGLONG)-100);
86 ok(!strcmp(buffer,"-00100") && r==6,"-1.5I64d failed: '%s'\n", buffer);
88 format = "%5I64d";
89 r = sprintf(buffer,format,(LONGLONG)100);
90 ok(!strcmp(buffer," 100") && r==5,"5I64d failed: '%s'\n", buffer);
92 format = "%5I64d";
93 r = sprintf(buffer,format,(LONGLONG)-100);
94 ok(!strcmp(buffer," -100") && r==5,"5I64d failed: '%s'\n", buffer);
96 format = "%-5I64d";
97 r = sprintf(buffer,format,(LONGLONG)100);
98 ok(!strcmp(buffer,"100 ") && r==5,"-5I64d failed: '%s'\n", buffer);
100 format = "%-5I64d";
101 r = sprintf(buffer,format,(LONGLONG)-100);
102 ok(!strcmp(buffer,"-100 ") && r==5,"-5I64d failed: '%s'\n", buffer);
104 format = "%-.5I64d";
105 r = sprintf(buffer,format,(LONGLONG)100);
106 ok(!strcmp(buffer,"00100") && r==5,"-.5I64d failed: '%s'\n", buffer);
108 format = "%-.5I64d";
109 r = sprintf(buffer,format,(LONGLONG)-100);
110 ok(!strcmp(buffer,"-00100") && r==6,"-.5I64d failed: '%s'\n", buffer);
112 format = "%-8.5I64d";
113 r = sprintf(buffer,format,(LONGLONG)100);
114 ok(!strcmp(buffer,"00100 ") && r==8,"-8.5I64d failed: '%s'\n", buffer);
116 format = "%-8.5I64d";
117 r = sprintf(buffer,format,(LONGLONG)-100);
118 ok(!strcmp(buffer,"-00100 ") && r==8,"-8.5I64d failed: '%s'\n", buffer);
120 format = "%05I64d";
121 r = sprintf(buffer,format,(LONGLONG)100);
122 ok(!strcmp(buffer,"00100") && r==5,"05I64d failed: '%s'\n", buffer);
124 format = "%05I64d";
125 r = sprintf(buffer,format,(LONGLONG)-100);
126 ok(!strcmp(buffer,"-0100") && r==5,"05I64d failed: '%s'\n", buffer);
128 format = "% I64d";
129 r = sprintf(buffer,format,(LONGLONG)100);
130 ok(!strcmp(buffer," 100") && r==4,"' I64d' failed: '%s'\n", buffer);
132 format = "% I64d";
133 r = sprintf(buffer,format,(LONGLONG)-100);
134 ok(!strcmp(buffer,"-100") && r==4,"' I64d' failed: '%s'\n", buffer);
136 format = "% 5I64d";
137 r = sprintf(buffer,format,(LONGLONG)100);
138 ok(!strcmp(buffer," 100") && r==5,"' 5I64d' failed: '%s'\n", buffer);
140 format = "% 5I64d";
141 r = sprintf(buffer,format,(LONGLONG)-100);
142 ok(!strcmp(buffer," -100") && r==5,"' 5I64d' failed: '%s'\n", buffer);
144 format = "% .5I64d";
145 r = sprintf(buffer,format,(LONGLONG)100);
146 ok(!strcmp(buffer," 00100") && r==6,"' .5I64d' failed: '%s'\n", buffer);
148 format = "% .5I64d";
149 r = sprintf(buffer,format,(LONGLONG)-100);
150 ok(!strcmp(buffer,"-00100") && r==6,"' .5I64d' failed: '%s'\n", buffer);
152 format = "% 8.5I64d";
153 r = sprintf(buffer,format,(LONGLONG)100);
154 ok(!strcmp(buffer," 00100") && r==8,"' 8.5I64d' failed: '%s'\n", buffer);
156 format = "% 8.5I64d";
157 r = sprintf(buffer,format,(LONGLONG)-100);
158 ok(!strcmp(buffer," -00100") && r==8,"' 8.5I64d' failed: '%s'\n", buffer);
160 format = "%.0I64d";
161 r = sprintf(buffer,format,(LONGLONG)0);
162 ok(r==0,".0I64d failed: '%s'\n", buffer);
164 format = "%#+21.18I64x";
165 r = sprintf(buffer,format,(LONGLONG)-100);
166 ok(!strcmp(buffer," 0x00ffffffffffffff9c") && r==21,"#+21.18I64x failed: '%s'\n", buffer);
168 format = "%#.25I64o";
169 r = sprintf(buffer,format,(LONGLONG)-100);
170 ok(!strcmp(buffer,"0001777777777777777777634") && r==25,"#.25I64o failed: '%s'\n", buffer);
172 format = "%#+24.20I64o";
173 r = sprintf(buffer,format,(LONGLONG)-100);
174 ok(!strcmp(buffer," 01777777777777777777634") && r==24,"#+24.20I64o failed: '%s'\n", buffer);
176 format = "%#+18.21I64X";
177 r = sprintf(buffer,format,(LONGLONG)-100);
178 ok(!strcmp(buffer,"0X00000FFFFFFFFFFFFFF9C") && r==23,"#+18.21I64X failed: '%s '\n", buffer);
180 format = "%#+20.24I64o";
181 r = sprintf(buffer,format,(LONGLONG)-100);
182 ok(!strcmp(buffer,"001777777777777777777634") && r==24,"#+20.24I64o failed: '%s'\n", buffer);
184 format = "%#+25.22I64u";
185 r = sprintf(buffer,format,(LONGLONG)-1);
186 ok(!strcmp(buffer," 0018446744073709551615") && r==25,"#+25.22I64u conversion failed: '%s'\n", buffer);
188 format = "%#+25.22I64u";
189 r = sprintf(buffer,format,(LONGLONG)-1);
190 ok(!strcmp(buffer," 0018446744073709551615") && r==25,"#+25.22I64u failed: '%s'\n", buffer);
192 format = "%#+30.25I64u";
193 r = sprintf(buffer,format,(LONGLONG)-1);
194 ok(!strcmp(buffer," 0000018446744073709551615") && r==30,"#+30.25I64u failed: '%s'\n", buffer);
196 format = "%+#25.22I64d";
197 r = sprintf(buffer,format,(LONGLONG)-1);
198 ok(!strcmp(buffer," -0000000000000000000001") && r==25,"+#25.22I64d failed: '%s'\n", buffer);
200 format = "%#-8.5I64o";
201 r = sprintf(buffer,format,(LONGLONG)100);
202 ok(!strcmp(buffer,"00144 ") && r==8,"-8.5I64o failed: '%s'\n", buffer);
204 format = "%#-+ 08.5I64d";
205 r = sprintf(buffer,format,(LONGLONG)100);
206 ok(!strcmp(buffer,"+00100 ") && r==8,"'#-+ 08.5I64d failed: '%s'\n", buffer);
208 format = "%#-+ 08.5I64d";
209 r = sprintf(buffer,format,(LONGLONG)100);
210 ok(!strcmp(buffer,"+00100 ") && r==8,"#-+ 08.5I64d failed: '%s'\n", buffer);
212 format = "%.80I64d";
213 r = sprintf(buffer,format,(LONGLONG)1);
214 ok(r==80,"%s format failed\n", format);
216 format = "% .80I64d";
217 r = sprintf(buffer,format,(LONGLONG)1);
218 ok(r==81,"%s format failed\n", format);
220 format = "% .80d";
221 r = sprintf(buffer,format,1);
222 ok(r==81,"%s format failed\n", format);
224 format = "%lld";
225 r = sprintf(buffer,format,((ULONGLONG)0xffffffff)*0xffffffff);
226 ok( r == 1 || r == 11, "return count wrong %d\n", r);
227 if (r == 11) /* %ll works on Vista */
228 ok(!strcmp(buffer, "-8589934591"), "Problem with \"ll\" interpretation '%s'\n", buffer);
229 else
230 ok(!strcmp(buffer, "1"), "Problem with \"ll\" interpretation '%s'\n", buffer);
232 format = "%I";
233 r = sprintf(buffer,format,1);
234 ok(!strcmp(buffer, "I"), "Problem with \"I\" interpretation\n");
235 ok( r==1, "return count wrong\n");
237 format = "%I0d";
238 r = sprintf(buffer,format,1);
239 ok(!strcmp(buffer,"I0d"),"I0d failed\n");
240 ok( r==3, "return count wrong\n");
242 format = "%I32d";
243 r = sprintf(buffer,format,1);
244 if (r == 1)
246 ok(!strcmp(buffer,"1"),"I32d failed, got '%s'\n",buffer);
248 else
250 /* Older versions don't grok I32 format */
251 ok(r == 4 && !strcmp(buffer,"I32d"),"I32d failed, got '%s',%d\n",buffer,r);
254 format = "%I64D";
255 r = sprintf(buffer,format,(LONGLONG)-1);
256 ok(!strcmp(buffer,"D"),"I64D failed: %s\n",buffer);
257 ok( r==1, "return count wrong\n");
259 format = "% d";
260 r = sprintf(buffer,format,1);
261 ok(!strcmp(buffer, " 1"),"Problem with sign place-holder: '%s'\n",buffer);
262 ok( r==2, "return count wrong\n");
264 format = "%+ d";
265 r = sprintf(buffer,format,1);
266 ok(!strcmp(buffer, "+1"),"Problem with sign flags: '%s'\n",buffer);
267 ok( r==2, "return count wrong\n");
269 format = "%S";
270 r = sprintf(buffer,format,wide);
271 ok(!strcmp(buffer,"wide"),"Problem with wide string format\n");
272 ok( r==4, "return count wrong\n");
274 format = "%04c";
275 r = sprintf(buffer,format,'1');
276 ok(!strcmp(buffer,"0001"),"Character not zero-prefixed \"%s\"\n",buffer);
277 ok( r==4, "return count wrong\n");
279 format = "%-04c";
280 r = sprintf(buffer,format,'1');
281 ok(!strcmp(buffer,"1 "),"Character zero-padded and/or not left-adjusted \"%s\"\n",buffer);
282 ok( r==4, "return count wrong\n");
284 if (sizeof(void *) == 8)
286 format = "%p";
287 r = sprintf(buffer,format,(void *)57);
288 ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
289 ok( r==16, "return count wrong\n");
291 format = "%#020p";
292 r = sprintf(buffer,format,(void *)57);
293 ok(!strcmp(buffer," 0X0000000000000039"),"Pointer formatted incorrectly\n");
294 ok( r==20, "return count wrong\n");
296 format = "%Fp";
297 r = sprintf(buffer,format,(void *)57);
298 ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
299 ok( r==16, "return count wrong\n");
301 format = "%#-020p";
302 r = sprintf(buffer,format,(void *)57);
303 ok(!strcmp(buffer,"0X0000000000000039 "),"Pointer formatted incorrectly\n");
304 ok( r==20, "return count wrong\n");
306 else
308 format = "%p";
309 r = sprintf(buffer,format,(void *)57);
310 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
311 ok( r==8, "return count wrong\n");
313 format = "%#012p";
314 r = sprintf(buffer,format,(void *)57);
315 ok(!strcmp(buffer," 0X00000039"),"Pointer formatted incorrectly\n");
316 ok( r==12, "return count wrong\n");
318 format = "%Fp";
319 r = sprintf(buffer,format,(void *)57);
320 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
321 ok( r==8, "return count wrong\n");
323 format = "%#-012p";
324 r = sprintf(buffer,format,(void *)57);
325 ok(!strcmp(buffer,"0X00000039 "),"Pointer formatted incorrectly\n");
326 ok( r==12, "return count wrong\n");
329 format = "%04s";
330 r = sprintf(buffer,format,"foo");
331 ok(!strcmp(buffer,"0foo"),"String not zero-prefixed \"%s\"\n",buffer);
332 ok( r==4, "return count wrong\n");
334 format = "%.1s";
335 r = sprintf(buffer,format,"foo");
336 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
337 ok( r==1, "return count wrong\n");
339 format = "%.*s";
340 r = sprintf(buffer,format,1,"foo");
341 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
342 ok( r==1, "return count wrong\n");
344 format = "%*s";
345 r = sprintf(buffer,format,-5,"foo");
346 ok(!strcmp(buffer,"foo "),"Negative field width ignored \"%s\"\n",buffer);
347 ok( r==5, "return count wrong\n");
349 format = "hello";
350 r = sprintf(buffer, format);
351 ok(!strcmp(buffer,"hello"), "failed\n");
352 ok( r==5, "return count wrong\n");
354 format = "%ws";
355 r = sprintf(buffer, format, wide);
356 ok(!strcmp(buffer,"wide"), "failed\n");
357 ok( r==4, "return count wrong\n");
359 format = "%-10ws";
360 r = sprintf(buffer, format, wide );
361 ok(!strcmp(buffer,"wide "), "failed\n");
362 ok( r==10, "return count wrong\n");
364 format = "%10ws";
365 r = sprintf(buffer, format, wide );
366 ok(!strcmp(buffer," wide"), "failed\n");
367 ok( r==10, "return count wrong\n");
369 format = "%#+ -03whlls";
370 r = sprintf(buffer, format, wide );
371 ok(!strcmp(buffer,"wide"), "failed\n");
372 ok( r==4, "return count wrong\n");
374 format = "%w0s";
375 r = sprintf(buffer, format, wide );
376 ok(!strcmp(buffer,"0s"), "failed\n");
377 ok( r==2, "return count wrong\n");
379 format = "%w-s";
380 r = sprintf(buffer, format, wide );
381 ok(!strcmp(buffer,"-s"), "failed\n");
382 ok( r==2, "return count wrong\n");
384 format = "%ls";
385 r = sprintf(buffer, format, wide );
386 ok(!strcmp(buffer,"wide"), "failed\n");
387 ok( r==4, "return count wrong\n");
389 format = "%Ls";
390 r = sprintf(buffer, format, "not wide" );
391 ok(!strcmp(buffer,"not wide"), "failed\n");
392 ok( r==8, "return count wrong\n");
394 format = "%b";
395 r = sprintf(buffer, format);
396 ok(!strcmp(buffer,"b"), "failed\n");
397 ok( r==1, "return count wrong\n");
399 format = "%3c";
400 r = sprintf(buffer, format,'a');
401 ok(!strcmp(buffer," a"), "failed\n");
402 ok( r==3, "return count wrong\n");
404 format = "%3d";
405 r = sprintf(buffer, format,1234);
406 ok(!strcmp(buffer,"1234"), "failed\n");
407 ok( r==4, "return count wrong\n");
409 format = "%3h";
410 r = sprintf(buffer, format);
411 ok(!strcmp(buffer,""), "failed\n");
412 ok( r==0, "return count wrong\n");
414 format = "%j%k%m%q%r%t%v%y%z";
415 r = sprintf(buffer, format);
416 ok(!strcmp(buffer,"jkmqrtvyz"), "failed\n");
417 ok( r==9, "return count wrong\n");
419 format = "asdf%n";
420 x = 0;
421 r = sprintf(buffer, format, &x );
422 if (r == -1)
424 /* %n format is disabled by default on vista */
425 /* FIXME: should test with _set_printf_count_output */
426 ok(x == 0, "should not write to x: %d\n", x);
428 else
430 ok(x == 4, "should write to x: %d\n", x);
431 ok(!strcmp(buffer,"asdf"), "failed\n");
432 ok( r==4, "return count wrong: %d\n", r);
435 format = "%-1d";
436 r = sprintf(buffer, format,2);
437 ok(!strcmp(buffer,"2"), "failed\n");
438 ok( r==1, "return count wrong\n");
440 format = "%2.4f";
441 r = sprintf(buffer, format,8.6);
442 ok(!strcmp(buffer,"8.6000"), "failed\n");
443 ok( r==6, "return count wrong\n");
445 format = "%0f";
446 r = sprintf(buffer, format,0.6);
447 ok(!strcmp(buffer,"0.600000"), "failed\n");
448 ok( r==8, "return count wrong\n");
450 format = "%.0f";
451 r = sprintf(buffer, format,0.6);
452 ok(!strcmp(buffer,"1"), "failed\n");
453 ok( r==1, "return count wrong\n");
455 format = "%2.4e";
456 r = sprintf(buffer, format,8.6);
457 ok(!strcmp(buffer,"8.6000e+000"), "failed\n");
458 ok( r==11, "return count wrong\n");
460 format = "%2.4g";
461 r = sprintf(buffer, format,8.6);
462 ok(!strcmp(buffer,"8.6"), "failed\n");
463 ok( r==3, "return count wrong\n");
465 format = "%-i";
466 r = sprintf(buffer, format,-1);
467 ok(!strcmp(buffer,"-1"), "failed\n");
468 ok( r==2, "return count wrong\n");
470 format = "%-i";
471 r = sprintf(buffer, format,1);
472 ok(!strcmp(buffer,"1"), "failed\n");
473 ok( r==1, "return count wrong\n");
475 format = "%+i";
476 r = sprintf(buffer, format,1);
477 ok(!strcmp(buffer,"+1"), "failed\n");
478 ok( r==2, "return count wrong\n");
480 format = "%o";
481 r = sprintf(buffer, format,10);
482 ok(!strcmp(buffer,"12"), "failed\n");
483 ok( r==2, "return count wrong\n");
485 format = "%p";
486 r = sprintf(buffer, format,0);
487 if (sizeof(void *) == 8)
489 ok(!strcmp(buffer,"0000000000000000"), "failed\n");
490 ok( r==16, "return count wrong\n");
492 else
494 ok(!strcmp(buffer,"00000000"), "failed\n");
495 ok( r==8, "return count wrong\n");
498 format = "%s";
499 r = sprintf(buffer, format,0);
500 ok(!strcmp(buffer,"(null)"), "failed\n");
501 ok( r==6, "return count wrong\n");
503 format = "%s";
504 r = sprintf(buffer, format,"%%%%");
505 ok(!strcmp(buffer,"%%%%"), "failed\n");
506 ok( r==4, "return count wrong\n");
508 format = "%u";
509 r = sprintf(buffer, format,-1);
510 ok(!strcmp(buffer,"4294967295"), "failed\n");
511 ok( r==10, "return count wrong\n");
513 format = "%w";
514 r = sprintf(buffer, format,-1);
515 ok(!strcmp(buffer,""), "failed\n");
516 ok( r==0, "return count wrong\n");
518 format = "%h";
519 r = sprintf(buffer, format,-1);
520 ok(!strcmp(buffer,""), "failed\n");
521 ok( r==0, "return count wrong\n");
523 format = "%z";
524 r = sprintf(buffer, format,-1);
525 ok(!strcmp(buffer,"z"), "failed\n");
526 ok( r==1, "return count wrong\n");
528 format = "%j";
529 r = sprintf(buffer, format,-1);
530 ok(!strcmp(buffer,"j"), "failed\n");
531 ok( r==1, "return count wrong\n");
533 format = "%F";
534 r = sprintf(buffer, format,-1);
535 ok(!strcmp(buffer,""), "failed\n");
536 ok( r==0, "return count wrong\n");
538 format = "%H";
539 r = sprintf(buffer, format,-1);
540 ok(!strcmp(buffer,"H"), "failed\n");
541 ok( r==1, "return count wrong\n");
543 format = "x%cx";
544 r = sprintf(buffer, format, 0x100+'X');
545 ok(!strcmp(buffer,"xXx"), "failed\n");
546 ok( r==3, "return count wrong\n");
548 format = "%%0";
549 r = sprintf(buffer, format);
550 ok(!strcmp(buffer,"%0"), "failed: \"%s\"\n", buffer);
551 ok( r==2, "return count wrong\n");
554 static void test_swprintf( void )
556 wchar_t buffer[100];
557 const wchar_t I64d[] = {'%','I','6','4','d',0};
558 double pnumber=789456123;
559 const wchar_t TwentyThreePoint15e[]= {'%','+','#','2','3','.','1','5','e',0};
560 const wchar_t e008[] = {'e','+','0','0','8',0};
561 const wchar_t string_w[] = {'s','t','r','i','n','g',0};
562 const char string[] = "string";
563 const wchar_t S[]={'%','S',0};
564 const wchar_t hs[] = {'%', 'h', 's', 0};
566 swprintf(buffer,TwentyThreePoint15e,pnumber);
567 ok(wcsstr(buffer,e008) != 0,"Sprintf different\n");
568 swprintf(buffer,I64d,((ULONGLONG)0xffffffff)*0xffffffff);
569 ok(wcslen(buffer) == 11,"Problem with long long\n");
570 swprintf(buffer,S,string);
571 ok(wcslen(buffer) == 6,"Problem with \"%%S\" interpretation\n");
572 swprintf(buffer, hs, string);
573 ok( wcscmp(string_w,buffer) == 0, "swprintf failed with %%hs\n");
576 static void test_snprintf (void)
578 struct snprintf_test {
579 const char *format;
580 int expected;
582 /* Pre-2.1 libc behaviour, not C99 compliant. */
583 const struct snprintf_test tests[] = {{"short", 5},
584 {"justfit", 7},
585 {"justfits", 8},
586 {"muchlonger", -1}};
587 char buffer[8];
588 const int bufsiz = sizeof buffer;
589 unsigned int i;
591 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
592 const char *fmt = tests[i].format;
593 const int expect = tests[i].expected;
594 const int n = _snprintf (buffer, bufsiz, fmt);
595 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
597 ok (n == expect, "\"%s\": expected %d, returned %d\n",
598 fmt, expect, n);
599 ok (!memcmp (fmt, buffer, valid),
600 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
604 static void test_fcvt(void)
606 char *str;
607 int dec=100, sign=100;
609 /* Numbers less than 1.0 with different precisions */
610 str = _fcvt(0.0001, 1, &dec, &sign );
611 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
612 ok( -3 == dec, "dec wrong %d\n", dec);
613 ok( 0 == sign, "sign wrong\n");
615 str = _fcvt(0.0001, -10, &dec, &sign );
616 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
617 ok( -3 == dec, "dec wrong %d\n", dec);
618 ok( 0 == sign, "sign wrong\n");
620 str = _fcvt(0.0001, 10, &dec, &sign );
621 ok( 0 == strcmp(str,"1000000"), "bad return '%s'\n", str);
622 ok( -3 == dec, "dec wrong %d\n", dec);
623 ok( 0 == sign, "sign wrong\n");
625 /* Basic sign test */
626 str = _fcvt(-111.0001, 5, &dec, &sign );
627 ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
628 ok( 3 == dec, "dec wrong %d\n", dec);
629 ok( 1 == sign, "sign wrong\n");
631 str = _fcvt(111.0001, 5, &dec, &sign );
632 ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
633 ok( 3 == dec, "dec wrong\n");
634 ok( 0 == sign, "sign wrong\n");
636 /* 0.0 with different precisions */
637 str = _fcvt(0.0, 5, &dec, &sign );
638 ok( 0 == strcmp(str,"00000"), "bad return '%s'\n", str);
639 ok( 0 == dec, "dec wrong %d\n", dec);
640 ok( 0 == sign, "sign wrong\n");
642 str = _fcvt(0.0, 0, &dec, &sign );
643 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
644 ok( 0 == dec, "dec wrong %d\n", dec);
645 ok( 0 == sign, "sign wrong\n");
647 str = _fcvt(0.0, -1, &dec, &sign );
648 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
649 ok( 0 == dec, "dec wrong %d\n", dec);
650 ok( 0 == sign, "sign wrong\n");
652 /* Numbers > 1.0 with 0 or -ve precision */
653 str = _fcvt(-123.0001, 0, &dec, &sign );
654 ok( 0 == strcmp(str,"123"), "bad return '%s'\n", str);
655 ok( 3 == dec, "dec wrong %d\n", dec);
656 ok( 1 == sign, "sign wrong\n");
658 str = _fcvt(-123.0001, -1, &dec, &sign );
659 ok( 0 == strcmp(str,"12"), "bad return '%s'\n", str);
660 ok( 3 == dec, "dec wrong %d\n", dec);
661 ok( 1 == sign, "sign wrong\n");
663 str = _fcvt(-123.0001, -2, &dec, &sign );
664 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
665 ok( 3 == dec, "dec wrong %d\n", dec);
666 ok( 1 == sign, "sign wrong\n");
668 str = _fcvt(-123.0001, -3, &dec, &sign );
669 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
670 ok( 3 == dec, "dec wrong %d\n", dec);
671 ok( 1 == sign, "sign wrong\n");
673 /* Numbers > 1.0, but with rounding at the point of precision */
674 str = _fcvt(99.99, 1, &dec, &sign );
675 ok( 0 == strcmp(str,"1000"), "bad return '%s'\n", str);
676 ok( 3 == dec, "dec wrong %d\n", dec);
677 ok( 0 == sign, "sign wrong\n");
679 /* Numbers < 1.0 where rounding occurs at the point of precision */
680 str = _fcvt(0.00636, 2, &dec, &sign );
681 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
682 ok( -1 == dec, "dec wrong %d\n", dec);
683 ok( 0 == sign, "sign wrong\n");
685 str = _fcvt(0.00636, 3, &dec, &sign );
686 ok( 0 == strcmp(str,"6"), "bad return '%s'\n", str);
687 ok( -2 == dec, "dec wrong %d\n", dec);
688 ok( 0 == sign, "sign wrong\n");
690 str = _fcvt(0.09999999996, 2, &dec, &sign );
691 ok( 0 == strcmp(str,"10"), "bad return '%s'\n", str);
692 ok( 0 == dec, "dec wrong %d\n", dec);
693 ok( 0 == sign, "sign wrong\n");
695 str = _fcvt(0.6, 0, &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");
701 static int _vsnwprintf_wrapper(wchar_t *str, size_t len, const wchar_t *format, ...)
703 int ret;
704 __ms_va_list valist;
705 __ms_va_start(valist, format);
706 ret = _vsnwprintf(str, len, format, valist);
707 __ms_va_end(valist);
708 return ret;
711 static void test_vsnwprintf(void)
713 const wchar_t format[] = {'%','w','s','%','w','s','%','w','s',0};
714 const wchar_t one[] = {'o','n','e',0};
715 const wchar_t two[] = {'t','w','o',0};
716 const wchar_t three[] = {'t','h','r','e','e',0};
718 int ret;
719 wchar_t str[32];
720 char buf[32];
722 ret = _vsnwprintf_wrapper( str, sizeof(str)/sizeof(str[0]), format, one, two, three );
724 ok( ret == 11, "got %d expected 11\n", ret );
725 WideCharToMultiByte( CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL );
726 ok( !strcmp(buf, "onetwothree"), "got %s expected 'onetwothree'\n", buf );
729 START_TEST(printf)
731 test_sprintf();
732 test_swprintf();
733 test_snprintf();
734 test_fcvt();
735 test_vsnwprintf();