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