295c6dd30200660584edfdca8f082ff9cfad9561
[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 = "%zx";
325 r = sprintf(buffer,format,1);
326 ok(!strcmp(buffer, "zx"), "Problem with \"z\" interpretation\n");
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 place-holder: '%s'\n",buffer);
332 ok( r==2, "return count wrong\n");
333
334 format = "%+ d";
335 r = sprintf(buffer,format,1);
336 ok(!strcmp(buffer, "+1"),"Problem with sign flags: '%s'\n",buffer);
337 ok( r==2, "return count wrong\n");
338
339 format = "%S";
340 r = sprintf(buffer,format,wide);
341 ok(!strcmp(buffer,"wide"),"Problem with wide string format\n");
342 ok( r==4, "return count wrong\n");
343
344 format = "%04c";
345 r = sprintf(buffer,format,'1');
346 ok(!strcmp(buffer,"0001"),"Character not zero-prefixed \"%s\"\n",buffer);
347 ok( r==4, "return count wrong\n");
348
349 format = "%-04c";
350 r = sprintf(buffer,format,'1');
351 ok(!strcmp(buffer,"1 "),"Character zero-padded and/or not left-adjusted \"%s\"\n",buffer);
352 ok( r==4, "return count wrong\n");
353
354 format = "%#012x";
355 r = sprintf(buffer,format,1);
356 ok(!strcmp(buffer,"0x0000000001"),"Hexadecimal zero-padded \"%s\"\n",buffer);
357 ok( r==12, "return count wrong\n");
358
359 r = sprintf(buffer,format,0);
360 ok(!strcmp(buffer,"000000000000"),"Hexadecimal zero-padded \"%s\"\n",buffer);
361 ok( r==12, "return count wrong\n");
362
363 format = "%#04.8x";
364 r = sprintf(buffer,format,1);
365 ok(!strcmp(buffer,"0x00000001"), "Hexadecimal zero-padded precision \"%s\"\n",buffer);
366 ok( r==10, "return count wrong\n");
367
368 r = sprintf(buffer,format,0);
369 ok(!strcmp(buffer,"00000000"), "Hexadecimal zero-padded precision \"%s\"\n",buffer);
370 ok( r==8, "return count wrong\n");
371
372 format = "%#-08.2x";
373 r = sprintf(buffer,format,1);
374 ok(!strcmp(buffer,"0x01 "), "Hexadecimal zero-padded not left-adjusted \"%s\"\n",buffer);
375 ok( r==8, "return count wrong\n");
376
377 r = sprintf(buffer,format,0);
378 ok(!strcmp(buffer,"00 "), "Hexadecimal zero-padded not left-adjusted \"%s\"\n",buffer);
379 ok( r==8, "return count wrong\n");
380
381 format = "%#.0x";
382 r = sprintf(buffer,format,1);
383 ok(!strcmp(buffer,"0x1"), "Hexadecimal zero-padded zero-precision \"%s\"\n",buffer);
384 ok( r==3, "return count wrong\n");
385
386 r = sprintf(buffer,format,0);
387 ok(!strcmp(buffer,""), "Hexadecimal zero-padded zero-precision \"%s\"\n",buffer);
388 ok( r==0, "return count wrong\n");
389
390 format = "%#08o";
391 r = sprintf(buffer,format,1);
392 ok(!strcmp(buffer,"00000001"), "Octal zero-padded \"%s\"\n",buffer);
393 ok( r==8, "return count wrong\n");
394
395 format = "%#o";
396 r = sprintf(buffer,format,1);
397 ok(!strcmp(buffer,"01"), "Octal zero-padded \"%s\"\n",buffer);
398 ok( r==2, "return count wrong\n");
399
400 r = sprintf(buffer,format,0);
401 ok(!strcmp(buffer,"0"), "Octal zero-padded \"%s\"\n",buffer);
402 ok( r==1, "return count wrong\n");
403
404 if (sizeof(void *) == 8)
405 {
406 format = "%p";
407 r = sprintf(buffer,format,(void *)57);
408 ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
409 ok( r==16, "return count wrong\n");
410
411 format = "%#020p";
412 r = sprintf(buffer,format,(void *)57);
413 ok(!strcmp(buffer," 0X0000000000000039"),"Pointer formatted incorrectly\n");
414 ok( r==20, "return count wrong\n");
415
416 format = "%Fp";
417 r = sprintf(buffer,format,(void *)57);
418 ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
419 ok( r==16, "return count wrong\n");
420
421 format = "%Np";
422 r = sprintf(buffer,format,(void *)57);
423 ok(!strcmp(buffer,"0000000000000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
424 ok( r==16, "return count wrong\n");
425
426 format = "%#-020p";
427 r = sprintf(buffer,format,(void *)57);
428 ok(!strcmp(buffer,"0X0000000000000039 "),"Pointer formatted incorrectly\n");
429 ok( r==20, "return count wrong\n");
430
431 format = "%Ix %d";
432 r = sprintf(buffer,format,(size_t)0x12345678123456,1);
433 ok(!strcmp(buffer,"12345678123456 1"),"buffer = %s\n",buffer);
434 ok( r==16, "return count wrong\n");
435 }
436 else
437 {
438 format = "%p";
439 r = sprintf(buffer,format,(void *)57);
440 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
441 ok( r==8, "return count wrong\n");
442
443 format = "%#012p";
444 r = sprintf(buffer,format,(void *)57);
445 ok(!strcmp(buffer," 0X00000039"),"Pointer formatted incorrectly\n");
446 ok( r==12, "return count wrong\n");
447
448 format = "%Fp";
449 r = sprintf(buffer,format,(void *)57);
450 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
451 ok( r==8, "return count wrong\n");
452
453 format = "%Np";
454 r = sprintf(buffer,format,(void *)57);
455 ok(!strcmp(buffer,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer);
456 ok( r==8, "return count wrong\n");
457
458 format = "%#-012p";
459 r = sprintf(buffer,format,(void *)57);
460 ok(!strcmp(buffer,"0X00000039 "),"Pointer formatted incorrectly\n");
461 ok( r==12, "return count wrong\n");
462
463 format = "%Ix %d";
464 r = sprintf(buffer,format,0x123456,1);
465 ok(!strcmp(buffer,"123456 1"),"buffer = %s\n",buffer);
466 ok( r==8, "return count wrong\n");
467 }
468
469 format = "%04s";
470 r = sprintf(buffer,format,"foo");
471 ok(!strcmp(buffer,"0foo"),"String not zero-prefixed \"%s\"\n",buffer);
472 ok( r==4, "return count wrong\n");
473
474 format = "%.1s";
475 r = sprintf(buffer,format,"foo");
476 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
477 ok( r==1, "return count wrong\n");
478
479 format = "%.*s";
480 r = sprintf(buffer,format,1,"foo");
481 ok(!strcmp(buffer,"f"),"Precision ignored \"%s\"\n",buffer);
482 ok( r==1, "return count wrong\n");
483
484 format = "%*s";
485 r = sprintf(buffer,format,-5,"foo");
486 ok(!strcmp(buffer,"foo "),"Negative field width ignored \"%s\"\n",buffer);
487 ok( r==5, "return count wrong\n");
488
489 format = "hello";
490 r = sprintf(buffer, format);
491 ok(!strcmp(buffer,"hello"), "failed\n");
492 ok( r==5, "return count wrong\n");
493
494 format = "%ws";
495 r = sprintf(buffer, format, wide);
496 ok(!strcmp(buffer,"wide"), "failed\n");
497 ok( r==4, "return count wrong\n");
498
499 format = "%-10ws";
500 r = sprintf(buffer, format, wide );
501 ok(!strcmp(buffer,"wide "), "failed\n");
502 ok( r==10, "return count wrong\n");
503
504 format = "%10ws";
505 r = sprintf(buffer, format, wide );
506 ok(!strcmp(buffer," wide"), "failed\n");
507 ok( r==10, "return count wrong\n");
508
509 format = "%#+ -03whlls";
510 r = sprintf(buffer, format, wide );
511 ok(!strcmp(buffer,"wide"), "failed\n");
512 ok( r==4, "return count wrong\n");
513
514 format = "%w0s";
515 r = sprintf(buffer, format, wide );
516 ok(!strcmp(buffer,"0s"), "failed\n");
517 ok( r==2, "return count wrong\n");
518
519 format = "%w-s";
520 r = sprintf(buffer, format, wide );
521 ok(!strcmp(buffer,"-s"), "failed\n");
522 ok( r==2, "return count wrong\n");
523
524 format = "%ls";
525 r = sprintf(buffer, format, wide );
526 ok(!strcmp(buffer,"wide"), "failed\n");
527 ok( r==4, "return count wrong\n");
528
529 format = "%Ls";
530 r = sprintf(buffer, format, "not wide" );
531 ok(!strcmp(buffer,"not wide"), "failed\n");
532 ok( r==8, "return count wrong\n");
533
534 format = "%b";
535 r = sprintf(buffer, format);
536 ok(!strcmp(buffer,"b"), "failed\n");
537 ok( r==1, "return count wrong\n");
538
539 format = "%3c";
540 r = sprintf(buffer, format,'a');
541 ok(!strcmp(buffer," a"), "failed\n");
542 ok( r==3, "return count wrong\n");
543
544 format = "%3d";
545 r = sprintf(buffer, format,1234);
546 ok(!strcmp(buffer,"1234"), "failed\n");
547 ok( r==4, "return count wrong\n");
548
549 format = "%3h";
550 r = sprintf(buffer, format);
551 ok(!strcmp(buffer,""), "failed\n");
552 ok( r==0, "return count wrong\n");
553
554 format = "%j%k%m%q%r%t%v%y%z";
555 r = sprintf(buffer, format);
556 ok(!strcmp(buffer,"jkmqrtvyz"), "failed\n");
557 ok( r==9, "return count wrong\n");
558
559 format = "asdf%n";
560 x = 0;
561 r = sprintf(buffer, format, &x );
562 if (r == -1)
563 {
564 /* %n format is disabled by default on vista */
565 /* FIXME: should test with _set_printf_count_output */
566 ok(x == 0, "should not write to x: %d\n", x);
567 }
568 else
569 {
570 ok(x == 4, "should write to x: %d\n", x);
571 ok(!strcmp(buffer,"asdf"), "failed\n");
572 ok( r==4, "return count wrong: %d\n", r);
573 }
574
575 format = "%-1d";
576 r = sprintf(buffer, format,2);
577 ok(!strcmp(buffer,"2"), "failed\n");
578 ok( r==1, "return count wrong\n");
579
580 format = "%2.4f";
581 r = sprintf(buffer, format,8.6);
582 ok(!strcmp(buffer,"8.6000"), "failed\n");
583 ok( r==6, "return count wrong\n");
584
585 format = "%0f";
586 r = sprintf(buffer, format,0.6);
587 ok(!strcmp(buffer,"0.600000"), "failed\n");
588 ok( r==8, "return count wrong\n");
589
590 format = "%.0f";
591 r = sprintf(buffer, format,0.6);
592 ok(!strcmp(buffer,"1"), "failed\n");
593 ok( r==1, "return count wrong\n");
594
595 format = "%2.4e";
596 r = sprintf(buffer, format,8.6);
597 ok(!strcmp(buffer,"8.6000e+000"), "failed\n");
598 ok( r==11, "return count wrong\n");
599
600 format = "% 2.4e";
601 r = sprintf(buffer, format,8.6);
602 ok(!strcmp(buffer," 8.6000e+000"), "failed: %s\n", buffer);
603 ok( r==12, "return count wrong\n");
604
605 format = "% 014.4e";
606 r = sprintf(buffer, format,8.6);
607 ok(!strcmp(buffer," 008.6000e+000"), "failed: %s\n", buffer);
608 ok( r==14, "return count wrong\n");
609
610 format = "% 2.4e";
611 r = sprintf(buffer, format,-8.6);
612 ok(!strcmp(buffer,"-8.6000e+000"), "failed: %s\n", buffer);
613 ok( r==12, "return count wrong\n");
614
615 format = "%+2.4e";
616 r = sprintf(buffer, format,8.6);
617 ok(!strcmp(buffer,"+8.6000e+000"), "failed: %s\n", buffer);
618 ok( r==12, "return count wrong\n");
619
620 format = "%2.4g";
621 r = sprintf(buffer, format,8.6);
622 ok(!strcmp(buffer,"8.6"), "failed\n");
623 ok( r==3, "return count wrong\n");
624
625 format = "%-i";
626 r = sprintf(buffer, format,-1);
627 ok(!strcmp(buffer,"-1"), "failed\n");
628 ok( r==2, "return count wrong\n");
629
630 format = "%-i";
631 r = sprintf(buffer, format,1);
632 ok(!strcmp(buffer,"1"), "failed\n");
633 ok( r==1, "return count wrong\n");
634
635 format = "%+i";
636 r = sprintf(buffer, format,1);
637 ok(!strcmp(buffer,"+1"), "failed\n");
638 ok( r==2, "return count wrong\n");
639
640 format = "%o";
641 r = sprintf(buffer, format,10);
642 ok(!strcmp(buffer,"12"), "failed\n");
643 ok( r==2, "return count wrong\n");
644
645 format = "%p";
646 r = sprintf(buffer, format,0);
647 if (sizeof(void *) == 8)
648 {
649 ok(!strcmp(buffer,"0000000000000000"), "failed\n");
650 ok( r==16, "return count wrong\n");
651 }
652 else
653 {
654 ok(!strcmp(buffer,"00000000"), "failed\n");
655 ok( r==8, "return count wrong\n");
656 }
657
658 format = "%s";
659 r = sprintf(buffer, format,0);
660 ok(!strcmp(buffer,"(null)"), "failed\n");
661 ok( r==6, "return count wrong\n");
662
663 format = "%s";
664 r = sprintf(buffer, format,"%%%%");
665 ok(!strcmp(buffer,"%%%%"), "failed\n");
666 ok( r==4, "return count wrong\n");
667
668 format = "%u";
669 r = sprintf(buffer, format,-1);
670 ok(!strcmp(buffer,"4294967295"), "failed\n");
671 ok( r==10, "return count wrong\n");
672
673 format = "%w";
674 r = sprintf(buffer, format,-1);
675 ok(!strcmp(buffer,""), "failed\n");
676 ok( r==0, "return count wrong\n");
677
678 format = "%h";
679 r = sprintf(buffer, format,-1);
680 ok(!strcmp(buffer,""), "failed\n");
681 ok( r==0, "return count wrong\n");
682
683 format = "%z";
684 r = sprintf(buffer, format,-1);
685 ok(!strcmp(buffer,"z"), "failed\n");
686 ok( r==1, "return count wrong\n");
687
688 format = "%j";
689 r = sprintf(buffer, format,-1);
690 ok(!strcmp(buffer,"j"), "failed\n");
691 ok( r==1, "return count wrong\n");
692
693 format = "%F";
694 r = sprintf(buffer, format,-1);
695 ok(!strcmp(buffer,""), "failed\n");
696 ok( r==0, "return count wrong\n");
697
698 format = "%N";
699 r = sprintf(buffer, format,-1);
700 ok(!strcmp(buffer,""), "failed\n");
701 ok( r==0, "return count wrong\n");
702
703 format = "%H";
704 r = sprintf(buffer, format,-1);
705 ok(!strcmp(buffer,"H"), "failed\n");
706 ok( r==1, "return count wrong\n");
707
708 format = "x%cx";
709 r = sprintf(buffer, format, 0x100+'X');
710 ok(!strcmp(buffer,"xXx"), "failed\n");
711 ok( r==3, "return count wrong\n");
712
713 format = "%%0";
714 r = sprintf(buffer, format);
715 ok(!strcmp(buffer,"%0"), "failed: \"%s\"\n", buffer);
716 ok( r==2, "return count wrong\n");
717
718 format = "%hx";
719 r = sprintf(buffer, format, 0x12345);
720 ok(!strcmp(buffer,"2345"), "failed \"%s\"\n", buffer);
721
722 format = "%hhx";
723 r = sprintf(buffer, format, 0x123);
724 ok(!strcmp(buffer,"123"), "failed: \"%s\"\n", buffer);
725 r = sprintf(buffer, format, 0x12345);
726 ok(!strcmp(buffer,"2345"), "failed \"%s\"\n", buffer);
727
728 format = "%lf";
729 r = sprintf(buffer, format, IND);
730 ok(r==9, "r = %d\n", r);
731 ok(!strcmp(buffer, "-1.#IND00"), "failed: \"%s\"\n", buffer);
732 r = sprintf(buffer, format, NAN);
733 ok(r==8, "r = %d\n", r);
734 ok(!strcmp(buffer, "1.#QNAN0"), "failed: \"%s\"\n", buffer);
735 r = sprintf(buffer, format, INFINITY);
736 ok(r==8, "r = %d\n", r);
737 ok(!strcmp(buffer, "1.#INF00"), "failed: \"%s\"\n", buffer);
738
739 format = "%le";
740 r = sprintf(buffer, format, IND);
741 ok(r==14, "r = %d\n", r);
742 ok(!strcmp(buffer, "-1.#IND00e+000"), "failed: \"%s\"\n", buffer);
743 r = sprintf(buffer, format, NAN);
744 ok(r==13, "r = %d\n", r);
745 ok(!strcmp(buffer, "1.#QNAN0e+000"), "failed: \"%s\"\n", buffer);
746 r = sprintf(buffer, format, INFINITY);
747 ok(r==13, "r = %d\n", r);
748 ok(!strcmp(buffer, "1.#INF00e+000"), "failed: \"%s\"\n", buffer);
749
750 format = "%lg";
751 r = sprintf(buffer, format, IND);
752 ok(r==7, "r = %d\n", r);
753 ok(!strcmp(buffer, "-1.#IND"), "failed: \"%s\"\n", buffer);
754 r = sprintf(buffer, format, NAN);
755 ok(r==7, "r = %d\n", r);
756 ok(!strcmp(buffer, "1.#QNAN"), "failed: \"%s\"\n", buffer);
757 r = sprintf(buffer, format, INFINITY);
758 ok(r==6, "r = %d\n", r);
759 ok(!strcmp(buffer, "1.#INF"), "failed: \"%s\"\n", buffer);
760
761 format = "%010.2lf";
762 r = sprintf(buffer, format, IND);
763 ok(r==10, "r = %d\n", r);
764 ok(!strcmp(buffer, "-000001.#J"), "failed: \"%s\"\n", buffer);
765 r = sprintf(buffer, format, NAN);
766 ok(r==10, "r = %d\n", r);
767 ok(!strcmp(buffer, "0000001.#R"), "failed: \"%s\"\n", buffer);
768 r = sprintf(buffer, format, INFINITY);
769 ok(r==10, "r = %d\n", r);
770 ok(!strcmp(buffer, "0000001.#J"), "failed: \"%s\"\n", buffer);
771 }
772
773 static void test_swprintf( void )
774 {
775 wchar_t buffer[100];
776 const wchar_t I64d[] = {'%','I','6','4','d',0};
777 double pnumber=789456123;
778 const wchar_t TwentyThreePoint15e[]= {'%','+','#','2','3','.','1','5','e',0};
779 const wchar_t e008[] = {'e','+','0','0','8',0};
780 const wchar_t string_w[] = {'s','t','r','i','n','g',0};
781 const char string[] = "string";
782 const wchar_t S[]={'%','S',0};
783 const wchar_t hs[] = {'%', 'h', 's', 0};
784
785 swprintf(buffer,TwentyThreePoint15e,pnumber);
786 ok(wcsstr(buffer,e008) != 0,"Sprintf different\n");
787 swprintf(buffer,I64d,((ULONGLONG)0xffffffff)*0xffffffff);
788 ok(wcslen(buffer) == 11,"Problem with long long\n");
789 swprintf(buffer,S,string);
790 ok(wcslen(buffer) == 6,"Problem with \"%%S\" interpretation\n");
791 swprintf(buffer, hs, string);
792 ok( wcscmp(string_w,buffer) == 0, "swprintf failed with %%hs\n");
793 }
794
795 static void test_snprintf (void)
796 {
797 struct snprintf_test {
798 const char *format;
799 int expected;
800 };
801 /* Pre-2.1 libc behaviour, not C99 compliant. */
802 const struct snprintf_test tests[] = {{"short", 5},
803 {"justfit", 7},
804 {"justfits", 8},
805 {"muchlonger", -1}};
806 char buffer[8];
807 const int bufsiz = sizeof buffer;
808 unsigned int i;
809
810 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
811 const char *fmt = tests[i].format;
812 const int expect = tests[i].expected;
813 const int n = _snprintf (buffer, bufsiz, fmt);
814 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
815
816 ok (n == expect, "\"%s\": expected %d, returned %d\n",
817 fmt, expect, n);
818 ok (!memcmp (fmt, buffer, valid),
819 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
820 }
821 }
822
823 static void test_fprintf(void)
824 {
825 static const char file_name[] = "fprintf.tst";
826 static const WCHAR utf16_test[] = {'u','n','i','c','o','d','e','\n',0};
827
828 FILE *fp = fopen(file_name, "wb");
829 char buf[1024];
830 int ret;
831
832 ret = fprintf(fp, "simple test\n");
833 ok(ret == 12, "ret = %d\n", ret);
834 ret = ftell(fp);
835 ok(ret == 12, "ftell returned %d\n", ret);
836
837 ret = fprintf(fp, "contains%cnull\n", '\0');
838 ok(ret == 14, "ret = %d\n", ret);
839 ret = ftell(fp);
840 ok(ret == 26, "ftell returned %d\n", ret);
841
842 ret = fwprintf(fp, utf16_test);
843 ok(ret == 8, "ret = %d\n", ret);
844 ret = ftell(fp);
845 ok(ret == 42, "ftell returned %d\n", ret);
846
847 fclose(fp);
848
849 fp = fopen(file_name, "rb");
850 ret = fscanf(fp, "%[^\n] ", buf);
851 ok(ret == 1, "ret = %d\n", ret);
852 ret = ftell(fp);
853 ok(ret == 12, "ftell returned %d\n", ret);
854 ok(!strcmp(buf, "simple test"), "buf = %s\n", buf);
855
856 fgets(buf, sizeof(buf), fp);
857 ret = ftell(fp);
858 ok(ret == 26, "ret = %d\n", ret);
859 ok(!memcmp(buf, "contains\0null\n", 14), "buf = %s\n", buf);
860
861 memset(buf, 0, sizeof(buf));
862 fgets(buf, sizeof(buf), fp);
863 ret = ftell(fp);
864 ok(ret == 41, "ret = %d\n", ret);
865 ok(!memcmp(buf, utf16_test, sizeof(utf16_test)),
866 "buf = %s\n", wine_dbgstr_w((WCHAR*)buf));
867
868 fclose(fp);
869
870 fp = fopen(file_name, "wt");
871
872 ret = fprintf(fp, "simple test\n");
873 ok(ret == 12, "ret = %d\n", ret);
874 ret = ftell(fp);
875 ok(ret == 13, "ftell returned %d\n", ret);
876
877 ret = fprintf(fp, "contains%cnull\n", '\0');
878 ok(ret == 14, "ret = %d\n", ret);
879 ret = ftell(fp);
880 ok(ret == 28, "ftell returned %d\n", ret);
881
882 ret = fwprintf(fp, utf16_test);
883 ok(ret == 8, "ret = %d\n", ret);
884 ret = ftell(fp);
885 ok(ret == 37, "ftell returned %d\n", ret);
886
887 fclose(fp);
888
889 fp = fopen(file_name, "rb");
890 ret = fscanf(fp, "%[^\n] ", buf);
891 ok(ret == 1, "ret = %d\n", ret);
892 ret = ftell(fp);
893 ok(ret == 13, "ftell returned %d\n", ret);
894 ok(!strcmp(buf, "simple test\r"), "buf = %s\n", buf);
895
896 fgets(buf, sizeof(buf), fp);
897 ret = ftell(fp);
898 ok(ret == 28, "ret = %d\n", ret);
899 ok(!memcmp(buf, "contains\0null\r\n", 15), "buf = %s\n", buf);
900
901 fgets(buf, sizeof(buf), fp);
902 ret = ftell(fp);
903 ok(ret == 37, "ret = %d\n", ret);
904 ok(!strcmp(buf, "unicode\r\n"), "buf = %s\n", buf);
905
906 fclose(fp);
907 unlink(file_name);
908 }
909
910 static void test_fcvt(void)
911 {
912 char *str;
913 int dec=100, sign=100;
914
915 /* Numbers less than 1.0 with different precisions */
916 str = _fcvt(0.0001, 1, &dec, &sign );
917 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
918 ok( -3 == dec, "dec wrong %d\n", dec);
919 ok( 0 == sign, "sign wrong\n");
920
921 str = _fcvt(0.0001, -10, &dec, &sign );
922 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
923 ok( -3 == dec, "dec wrong %d\n", dec);
924 ok( 0 == sign, "sign wrong\n");
925
926 str = _fcvt(0.0001, 10, &dec, &sign );
927 ok( 0 == strcmp(str,"1000000"), "bad return '%s'\n", str);
928 ok( -3 == dec, "dec wrong %d\n", dec);
929 ok( 0 == sign, "sign wrong\n");
930
931 /* Basic sign test */
932 str = _fcvt(-111.0001, 5, &dec, &sign );
933 ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
934 ok( 3 == dec, "dec wrong %d\n", dec);
935 ok( 1 == sign, "sign wrong\n");
936
937 str = _fcvt(111.0001, 5, &dec, &sign );
938 ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str);
939 ok( 3 == dec, "dec wrong\n");
940 ok( 0 == sign, "sign wrong\n");
941
942 /* 0.0 with different precisions */
943 str = _fcvt(0.0, 5, &dec, &sign );
944 ok( 0 == strcmp(str,"00000"), "bad return '%s'\n", str);
945 ok( 0 == dec, "dec wrong %d\n", dec);
946 ok( 0 == sign, "sign wrong\n");
947
948 str = _fcvt(0.0, 0, &dec, &sign );
949 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
950 ok( 0 == dec, "dec wrong %d\n", dec);
951 ok( 0 == sign, "sign wrong\n");
952
953 str = _fcvt(0.0, -1, &dec, &sign );
954 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
955 ok( 0 == dec, "dec wrong %d\n", dec);
956 ok( 0 == sign, "sign wrong\n");
957
958 /* Numbers > 1.0 with 0 or -ve precision */
959 str = _fcvt(-123.0001, 0, &dec, &sign );
960 ok( 0 == strcmp(str,"123"), "bad return '%s'\n", str);
961 ok( 3 == dec, "dec wrong %d\n", dec);
962 ok( 1 == sign, "sign wrong\n");
963
964 str = _fcvt(-123.0001, -1, &dec, &sign );
965 ok( 0 == strcmp(str,"12"), "bad return '%s'\n", str);
966 ok( 3 == dec, "dec wrong %d\n", dec);
967 ok( 1 == sign, "sign wrong\n");
968
969 str = _fcvt(-123.0001, -2, &dec, &sign );
970 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
971 ok( 3 == dec, "dec wrong %d\n", dec);
972 ok( 1 == sign, "sign wrong\n");
973
974 str = _fcvt(-123.0001, -3, &dec, &sign );
975 ok( 0 == strcmp(str,""), "bad return '%s'\n", str);
976 ok( 3 == dec, "dec wrong %d\n", dec);
977 ok( 1 == sign, "sign wrong\n");
978
979 /* Numbers > 1.0, but with rounding at the point of precision */
980 str = _fcvt(99.99, 1, &dec, &sign );
981 ok( 0 == strcmp(str,"1000"), "bad return '%s'\n", str);
982 ok( 3 == dec, "dec wrong %d\n", dec);
983 ok( 0 == sign, "sign wrong\n");
984
985 /* Numbers < 1.0 where rounding occurs at the point of precision */
986 str = _fcvt(0.00636, 2, &dec, &sign );
987 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
988 ok( -1 == dec, "dec wrong %d\n", dec);
989 ok( 0 == sign, "sign wrong\n");
990
991 str = _fcvt(0.00636, 3, &dec, &sign );
992 ok( 0 == strcmp(str,"6"), "bad return '%s'\n", str);
993 ok( -2 == dec, "dec wrong %d\n", dec);
994 ok( 0 == sign, "sign wrong\n");
995
996 str = _fcvt(0.09999999996, 2, &dec, &sign );
997 ok( 0 == strcmp(str,"10"), "bad return '%s'\n", str);
998 ok( 0 == dec, "dec wrong %d\n", dec);
999 ok( 0 == sign, "sign wrong\n");
1000
1001 str = _fcvt(0.6, 0, &dec, &sign );
1002 ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str);
1003 ok( 1 == dec, "dec wrong %d\n", dec);
1004 ok( 0 == sign, "sign wrong\n");
1005 }
1006
1007 /* Don't test nrdigits < 0, msvcrt on Win9x and NT4 will corrupt memory by
1008 * writing outside allocated memory */
1009 static struct {
1010 double value;
1011 int nrdigits;
1012 const char *expstr_e;
1013 const char *expstr_f;
1014 int expdecpt_e;
1015 int expdecpt_f;
1016 int expsign;
1017 } test_cvt_testcases[] = {
1018 { 45.0, 2, "45", "4500", 2, 2, 0 },
1019 /* Numbers less than 1.0 with different precisions */
1020 { 0.0001, 1, "1", "", -3, -3, 0 },
1021 { 0.0001, 10,"1000000000", "1000000", -3, -3, 0 },
1022 /* Basic sign test */
1023 { -111.0001, 5, "11100", "11100010", 3, 3, 1 },
1024 { 111.0001, 5, "11100", "11100010", 3, 3, 0 },
1025 /* big numbers with low precision */
1026 { 3333.3, 2, "33", "333330", 4, 4, 0 },
1027 {999999999999.9, 3, "100","999999999999900", 13, 12, 0 },
1028 /* 0.0 with different precisions */
1029 { 0.0, 5, "00000", "00000", 0, 0, 0 },
1030 { 0.0, 0, "", "", 0, 0, 0 },
1031 { 0.0, -1, "", "", 0, 0, 0 },
1032 /* Numbers > 1.0 with 0 or -ve precision */
1033 { -123.0001, 0, "", "123", 3, 3, 1 },
1034 { -123.0001, -1, "", "12", 3, 3, 1 },
1035 { -123.0001, -2, "", "1", 3, 3, 1 },
1036 { -123.0001, -3, "", "", 3, 3, 1 },
1037 /* Numbers > 1.0, but with rounding at the point of precision */
1038 { 99.99, 1, "1", "1000", 3, 3, 0 },
1039 /* Numbers < 1.0 where rounding occurs at the point of precision */
1040 { 0.0063, 2, "63", "1", -2, -1, 0 },
1041 { 0.0063, 3, "630", "6", -2, -2, 0 },
1042 { 0.09999999996, 2, "10", "10", 0, 0, 0 },
1043 { 0.6, 1, "6", "6", 0, 0, 0 },
1044 { 0.6, 0, "", "1", 1, 1, 0 },
1045 { 0.4, 0, "", "", 0, 0, 0 },
1046 { 0.49, 0, "", "", 0, 0, 0 },
1047 { 0.51, 0, "", "1", 1, 1, 0 },
1048 /* ask for ridiculous precision, ruin formatting this table */
1049 { 1.0, 30, "100000000000000000000000000000",
1050 "1000000000000000000000000000000", 1, 1, 0},
1051 { 123456789012345678901.0, 30, "123456789012345680000000000000",
1052 "123456789012345680000000000000000000000000000000000", 21, 21, 0},
1053 /* end marker */
1054 { 0, 0, "END"}
1055 };
1056
1057 static void test_xcvt(void)
1058 {
1059 char *str;
1060 int i, decpt, sign, err;
1061
1062 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1063 decpt = sign = 100;
1064 str = _ecvt( test_cvt_testcases[i].value,
1065 test_cvt_testcases[i].nrdigits,
1066 &decpt,
1067 &sign);
1068 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15),
1069 "_ecvt() bad return, got \n'%s' expected \n'%s'\n", str,
1070 test_cvt_testcases[i].expstr_e);
1071 ok( decpt == test_cvt_testcases[i].expdecpt_e,
1072 "_ecvt() decimal point wrong, got %d expected %d\n", decpt,
1073 test_cvt_testcases[i].expdecpt_e);
1074 ok( sign == test_cvt_testcases[i].expsign,
1075 "_ecvt() sign wrong, got %d expected %d\n", sign,
1076 test_cvt_testcases[i].expsign);
1077 }
1078 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1079 decpt = sign = 100;
1080 str = _fcvt( test_cvt_testcases[i].value,
1081 test_cvt_testcases[i].nrdigits,
1082 &decpt,
1083 &sign);
1084 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15),
1085 "_fcvt() bad return, got \n'%s' expected \n'%s'\n", str,
1086 test_cvt_testcases[i].expstr_f);
1087 ok( decpt == test_cvt_testcases[i].expdecpt_f,
1088 "_fcvt() decimal point wrong, got %d expected %d\n", decpt,
1089 test_cvt_testcases[i].expdecpt_f);
1090 ok( sign == test_cvt_testcases[i].expsign,
1091 "_fcvt() sign wrong, got %d expected %d\n", sign,
1092 test_cvt_testcases[i].expsign);
1093 }
1094
1095 if (p__ecvt_s)
1096 {
1097 str = malloc(1024);
1098 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1099 decpt = sign = 100;
1100 err = p__ecvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign);
1101 ok(err == 0, "_ecvt_s() failed with error code %d\n", err);
1102 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15),
1103 "_ecvt_s() bad return, got \n'%s' expected \n'%s'\n", str,
1104 test_cvt_testcases[i].expstr_e);
1105 ok( decpt == test_cvt_testcases[i].expdecpt_e,
1106 "_ecvt_s() decimal point wrong, got %d expected %d\n", decpt,
1107 test_cvt_testcases[i].expdecpt_e);
1108 ok( sign == test_cvt_testcases[i].expsign,
1109 "_ecvt_s() sign wrong, got %d expected %d\n", sign,
1110 test_cvt_testcases[i].expsign);
1111 }
1112 free(str);
1113 }
1114 else
1115 win_skip("_ecvt_s not available\n");
1116
1117 if (p__fcvt_s)
1118 {
1119 int i;
1120
1121 str = malloc(1024);
1122
1123 /* invalid arguments */
1124 err = p__fcvt_s(NULL, 0, 0.0, 0, &i, &i);
1125 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1126
1127 err = p__fcvt_s(str, 0, 0.0, 0, &i, &i);
1128 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1129
1130 str[0] = ' ';
1131 str[1] = 0;
1132 err = p__fcvt_s(str, -1, 0.0, 0, &i, &i);
1133 ok(err == 0, "got %d, expected 0\n", err);
1134 ok(str[0] == 0, "got %c, expected 0\n", str[0]);
1135 ok(str[1] == 0, "got %c, expected 0\n", str[1]);
1136
1137 err = p__fcvt_s(str, 1, 0.0, 0, NULL, &i);
1138 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1139
1140 err = p__fcvt_s(str, 1, 0.0, 0, &i, NULL);
1141 ok(err == EINVAL, "got %d, expected EINVAL\n", err);
1142
1143 for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){
1144 decpt = sign = 100;
1145 err = p__fcvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign);
1146 ok(err == 0, "_fcvt_s() failed with error code %d\n", err);
1147 ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15),
1148 "_fcvt_s() bad return, got '%s' expected '%s'. test %d\n", str,
1149 test_cvt_testcases[i].expstr_f, i);
1150 ok( decpt == test_cvt_testcases[i].expdecpt_f,
1151 "_fcvt_s() decimal point wrong, got %d expected %d\n", decpt,
1152 test_cvt_testcases[i].expdecpt_f);
1153 ok( sign == test_cvt_testcases[i].expsign,
1154 "_fcvt_s() sign wrong, got %d expected %d\n", sign,
1155 test_cvt_testcases[i].expsign);
1156 }
1157 free(str);
1158 }
1159 else
1160 win_skip("_fcvt_s not available\n");
1161 }
1162
1163 static int __cdecl _vsnwprintf_wrapper(wchar_t *str, size_t len, const wchar_t *format, ...)
1164 {
1165 int ret;
1166 __ms_va_list valist;
1167 __ms_va_start(valist, format);
1168 ret = _vsnwprintf(str, len, format, valist);
1169 __ms_va_end(valist);
1170 return ret;
1171 }
1172
1173 static void test_vsnwprintf(void)
1174 {
1175 const wchar_t format[] = {'%','w','s','%','w','s','%','w','s',0};
1176 const wchar_t one[] = {'o','n','e',0};
1177 const wchar_t two[] = {'t','w','o',0};
1178 const wchar_t three[] = {'t','h','r','e','e',0};
1179
1180 int ret;
1181 wchar_t str[32];
1182 char buf[32];
1183
1184 ret = _vsnwprintf_wrapper( str, sizeof(str)/sizeof(str[0]), format, one, two, three );
1185
1186 ok( ret == 11, "got %d expected 11\n", ret );
1187 WideCharToMultiByte( CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL );
1188 ok( !strcmp(buf, "onetwothree"), "got %s expected 'onetwothree'\n", buf );
1189
1190 ret = _vsnwprintf_wrapper( str, 0, format, one, two, three );
1191 ok( ret == -1, "got %d, expected -1\n", ret );
1192
1193 ret = _vsnwprintf_wrapper( NULL, 0, format, one, two, three );
1194 ok( ret == 11 || broken(ret == -1 /* Win2k */), "got %d, expected 11\n", ret );
1195 }
1196
1197 static int __cdecl vswprintf_wrapper(wchar_t *str, const wchar_t *format, ...)
1198 {
1199 int ret;
1200 __ms_va_list valist;
1201 __ms_va_start(valist, format);
1202 ret = p_vswprintf(str, format, valist);
1203 __ms_va_end(valist);
1204 return ret;
1205 }
1206
1207 static int __cdecl _vswprintf_wrapper(wchar_t *str, const wchar_t *format, ...)
1208 {
1209 int ret;
1210 __ms_va_list valist;
1211 __ms_va_start(valist, format);
1212 ret = p__vswprintf(str, format, valist);
1213 __ms_va_end(valist);
1214 return ret;
1215 }
1216
1217 static int __cdecl _vswprintf_l_wrapper(wchar_t *str, 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_l(str, format, locale, valist);
1223 __ms_va_end(valist);
1224 return ret;
1225 }
1226
1227 static int __cdecl _vswprintf_c_wrapper(wchar_t *str, size_t size, const wchar_t *format, ...)
1228 {
1229 int ret;
1230 __ms_va_list valist;
1231 __ms_va_start(valist, format);
1232 ret = p__vswprintf_c(str, size, format, valist);
1233 __ms_va_end(valist);
1234 return ret;
1235 }
1236
1237 static int __cdecl _vswprintf_c_l_wrapper(wchar_t *str, size_t size, const wchar_t *format, void *locale, ...)
1238 {
1239 int ret;
1240 __ms_va_list valist;
1241 __ms_va_start(valist, locale);
1242 ret = p__vswprintf_c_l(str, size, format, locale, valist);
1243 __ms_va_end(valist);
1244 return ret;
1245 }
1246
1247 static int __cdecl _vswprintf_p_l_wrapper(wchar_t *str, size_t size, const wchar_t *format, void *locale, ...)
1248 {
1249 int ret;
1250 __ms_va_list valist;
1251 __ms_va_start(valist, locale);
1252 ret = p__vswprintf_p_l(str, size, format, locale, valist);
1253 __ms_va_end(valist);
1254 return ret;
1255 }
1256
1257 static void test_vswprintf(void)
1258 {
1259 const wchar_t format[] = {'%','s',' ','%','d',0};
1260 const wchar_t number[] = {'n','u','m','b','e','r',0};
1261 const wchar_t out[] = {'n','u','m','b','e','r',' ','1','2','3',0};
1262 wchar_t buf[20];
1263
1264 int ret;
1265
1266 if (!p_vswprintf || !p__vswprintf || !p__vswprintf_l ||!p__vswprintf_c
1267 || !p__vswprintf_c_l || !p__vswprintf_p_l)
1268 {
1269 win_skip("_vswprintf or vswprintf not available\n");
1270 return;
1271 }
1272
1273 ret = vswprintf_wrapper(buf, format, number, 123);
1274 ok(ret == 10, "got %d, expected 10\n", ret);
1275 ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1276
1277 memset(buf, 0, sizeof(buf));
1278 ret = _vswprintf_wrapper(buf, format, number, 123);
1279 ok(ret == 10, "got %d, expected 10\n", ret);
1280 ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1281
1282 memset(buf, 0, sizeof(buf));
1283 ret = _vswprintf_l_wrapper(buf, format, NULL, number, 123);
1284 ok(ret == 10, "got %d, expected 10\n", ret);
1285 ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1286
1287 memset(buf, 0, sizeof(buf));
1288 ret = _vswprintf_c_wrapper(buf, 20, format, number, 123);
1289 ok(ret == 10, "got %d, expected 10\n", ret);
1290 ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1291
1292 memset(buf, 0, sizeof(buf));
1293 ret = _vswprintf_c_l_wrapper(buf, 20, format, NULL, number, 123);
1294 ok(ret == 10, "got %d, expected 10\n", ret);
1295 ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1296
1297 memset(buf, 0, sizeof(buf));
1298 ret = _vswprintf_p_l_wrapper(buf, 20, format, NULL, number, 123);
1299 ok(ret == 10, "got %d, expected 10\n", ret);
1300 ok(!memcmp(buf, out, sizeof(out)), "buf = %s\n", wine_dbgstr_w(buf));
1301 }
1302
1303 static int __cdecl _vscprintf_wrapper(const char *format, ...)
1304 {
1305 int ret;
1306 __ms_va_list valist;
1307 __ms_va_start(valist, format);
1308 ret = p__vscprintf(format, valist);
1309 __ms_va_end(valist);
1310 return ret;
1311 }
1312
1313 static void test_vscprintf(void)
1314 {
1315 int ret;
1316
1317 if (!p__vscprintf)
1318 {
1319 win_skip("_vscprintf not available\n");
1320 return;
1321 }
1322
1323 ret = _vscprintf_wrapper( "%s %d", "number", 1 );
1324 ok( ret == 8, "got %d expected 8\n", ret );
1325 }
1326
1327 static int __cdecl _vscwprintf_wrapper(const wchar_t *format, ...)
1328 {
1329 int ret;
1330 __ms_va_list valist;
1331 __ms_va_start(valist, format);
1332 ret = p__vscwprintf(format, valist);
1333 __ms_va_end(valist);
1334 return ret;
1335 }
1336
1337 static void test_vscwprintf(void)
1338 {
1339 const wchar_t format[] = {'%','s',' ','%','d',0};
1340 const wchar_t number[] = {'n','u','m','b','e','r',0};
1341
1342 int ret;
1343
1344 if (!p__vscwprintf)
1345 {
1346 win_skip("_vscwprintf not available\n");
1347 return;
1348 }
1349
1350 ret = _vscwprintf_wrapper( format, number, 1 );
1351 ok( ret == 8, "got %d expected 8\n", ret );
1352 }
1353
1354 static int __cdecl _vsnwprintf_s_wrapper(wchar_t *str, size_t sizeOfBuffer,
1355 size_t count, const wchar_t *format, ...)
1356 {
1357 int ret;
1358 __ms_va_list valist;
1359 __ms_va_start(valist, format);
1360 ret = p__vsnwprintf_s(str, sizeOfBuffer, count, format, valist);
1361 __ms_va_end(valist);
1362 return ret;
1363 }
1364
1365 static void test_vsnwprintf_s(void)
1366 {
1367 const wchar_t format[] = { 'A','B','%','u','C',0 };
1368 const wchar_t out7[] = { 'A','B','1','2','3','C',0 };
1369 const wchar_t out6[] = { 'A','B','1','2','3',0 };
1370 const wchar_t out2[] = { 'A',0 };
1371 const wchar_t out1[] = { 0 };
1372 wchar_t buffer[14] = { 0 };
1373 int exp, got;
1374
1375 if (!p__vsnwprintf_s)
1376 {
1377 win_skip("_vsnwprintf_s not available\n");
1378 return;
1379 }
1380
1381 /* Enough room. */
1382 exp = wcslen(out7);
1383
1384 got = _vsnwprintf_s_wrapper(buffer, 14, _TRUNCATE, format, 123);
1385 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1386 ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1387
1388 got = _vsnwprintf_s_wrapper(buffer, 12, _TRUNCATE, format, 123);
1389 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1390 ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1391
1392 got = _vsnwprintf_s_wrapper(buffer, 7, _TRUNCATE, format, 123);
1393 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1394 ok( !wcscmp(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1395
1396 /* Not enough room. */
1397 exp = -1;
1398
1399 got = _vsnwprintf_s_wrapper(buffer, 6, _TRUNCATE, format, 123);
1400 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1401 ok( !wcscmp(out6, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1402
1403 got = _vsnwprintf_s_wrapper(buffer, 2, _TRUNCATE, format, 123);
1404 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1405 ok( !wcscmp(out2, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1406
1407 got = _vsnwprintf_s_wrapper(buffer, 1, _TRUNCATE, format, 123);
1408 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
1409 ok( !wcscmp(out1, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
1410 }
1411
1412 static int __cdecl _vsprintf_p_wrapper(char *str, size_t sizeOfBuffer,
1413 const char *format, ...)
1414 {
1415 int ret;
1416 __ms_va_list valist;
1417 __ms_va_start(valist, format);
1418 ret = p__vsprintf_p(str, sizeOfBuffer, format, valist);
1419 __ms_va_end(valist);
1420 return ret;
1421 }
1422
1423 static void test_vsprintf_p(void)
1424 {
1425 char buf[1024];
1426 int ret;
1427
1428 if(!p__vsprintf_p) {
1429 win_skip("vsprintf_p not available\n");
1430 return;
1431 }
1432
1433 ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%s %d", "test", 1234);
1434 ok(ret == 9, "ret = %d\n", ret);
1435 ok(!memcmp(buf, "test 1234", 10), "buf = %s\n", buf);
1436
1437 ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%1$d", 1234, "additional param");
1438 ok(ret == 4, "ret = %d\n", ret);
1439 ok(!memcmp(buf, "1234", 5), "buf = %s\n", buf);
1440
1441 ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%2$s %1$d", 1234, "test");
1442 ok(ret == 9, "ret = %d\n", ret);
1443 ok(!memcmp(buf, "test 1234", 10), "buf = %s\n", buf);
1444
1445 ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%2$*3$s %2$.*1$s", 2, "test", 3);
1446 ok(ret == 7, "ret = %d\n", ret);
1447 ok(!memcmp(buf, "test te", 8), "buf = %s\n", buf);
1448
1449 /* Following test invokes invalid parameter handler */
1450 /* ret = _vsprintf_p_wrapper(buf, sizeof(buf), "%d %1$d", 1234); */
1451 }
1452
1453 static void test__get_output_format(void)
1454 {
1455 unsigned int ret;
1456 char buf[64];
1457 int c;
1458
1459 if (!p__get_output_format || !p__set_output_format)
1460 {
1461 win_skip("_get_output_format or _set_output_format is not available\n");
1462 return;
1463 }
1464
1465 ret = p__get_output_format();
1466 ok(ret == 0, "got %d\n", ret);
1467
1468 c = sprintf(buf, "%E", 1.23);
1469 ok(c == 13, "c = %d\n", c);
1470 ok(!strcmp(buf, "1.230000E+000"), "buf = %s\n", buf);
1471
1472 ret = p__set_output_format(_TWO_DIGIT_EXPONENT);
1473 ok(ret == 0, "got %d\n", ret);
1474
1475 c = sprintf(buf, "%E", 1.23);
1476 ok(c == 12, "c = %d\n", c);
1477 ok(!strcmp(buf, "1.230000E+00"), "buf = %s\n", buf);
1478
1479 ret = p__get_output_format();
1480 ok(ret == _TWO_DIGIT_EXPONENT, "got %d\n", ret);
1481
1482 ret = p__set_output_format(_TWO_DIGIT_EXPONENT);
1483 ok(ret == _TWO_DIGIT_EXPONENT, "got %d\n", ret);
1484 }
1485
1486 START_TEST(printf)
1487 {
1488 init();
1489
1490 test_sprintf();
1491 test_swprintf();
1492 test_snprintf();
1493 test_fprintf();
1494 test_fcvt();
1495 test_xcvt();
1496 test_vsnwprintf();
1497 test_vscprintf();
1498 test_vscwprintf();
1499 test_vswprintf();
1500 test_vsnwprintf_s();
1501 test_vsprintf_p();
1502 test__get_output_format();
1503 }