ucrtbase/tests: Use public _CRT_INTERNAL_PRINTF_* defines.
[wine.git] / dlls / ucrtbase / tests / printf.c
blobe743397331e3db1ce8c602714823993959a98cc5
1 /*
2 * Conformance tests for *printf functions.
4 * Copyright 2002 Uwe Bonnes
5 * Copyright 2004 Aneurin Price
6 * Copyright 2005 Mike McCormack
7 * Copyright 2015 Martin Storsjo
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdio.h>
25 #include <errno.h>
26 #include <math.h>
27 #include <inttypes.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
33 #include "wine/test.h"
35 #define DEFINE_EXPECT(func) \
36 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
38 #define SET_EXPECT(func) \
39 expect_ ## func = TRUE
41 #define CHECK_EXPECT2(func) \
42 do { \
43 ok(expect_ ##func, "unexpected call " #func "\n"); \
44 called_ ## func = TRUE; \
45 }while(0)
47 #define CHECK_EXPECT(func) \
48 do { \
49 CHECK_EXPECT2(func); \
50 expect_ ## func = FALSE; \
51 }while(0)
53 #define CHECK_CALLED(func) \
54 do { \
55 ok(called_ ## func, "expected " #func "\n"); \
56 expect_ ## func = called_ ## func = FALSE; \
57 }while(0)
59 DEFINE_EXPECT(invalid_parameter_handler);
61 /* make sure we use the correct errno */
62 #undef errno
63 #define errno (*p_errno())
65 static inline float __port_ind(void)
67 static const unsigned __ind_bytes = 0xffc00000;
68 return *(const float *)&__ind_bytes;
70 #define IND __port_ind()
72 static int (__cdecl *p_vfprintf)(unsigned __int64 options, FILE *file, const char *format,
73 void *locale, __ms_va_list valist);
74 static int (__cdecl *p_vfwprintf)(unsigned __int64 options, FILE *file, const wchar_t *format,
75 void *locale, __ms_va_list valist);
76 static int (__cdecl *p_vsprintf)(unsigned __int64 options, char *str, size_t len, const char *format,
77 void *locale, __ms_va_list valist);
78 static int (__cdecl *p_vsnprintf_s)(unsigned __int64 options, char *str, size_t sizeOfBuffer, size_t count, const char *format,
79 void *locale, __ms_va_list valist);
80 static int (__cdecl *p_vsprintf_s)(unsigned __int64 options, char *str, size_t count, const char *format,
81 void *locale, __ms_va_list valist);
82 static int (__cdecl *p_vswprintf)(unsigned __int64 options, wchar_t *str, size_t len, const wchar_t *format,
83 void *locale, __ms_va_list valist);
84 static int (__cdecl *p_vsnwprintf_s)(unsigned __int64 options, WCHAR *str, size_t sizeOfBuffer, size_t count, const WCHAR *format,
85 void *locale, __ms_va_list valist);
87 static FILE *(__cdecl *p_fopen)(const char *name, const char *mode);
88 static int (__cdecl *p_fclose)(FILE *file);
89 static long (__cdecl *p_ftell)(FILE *file);
90 static char *(__cdecl *p_fgets)(char *str, int size, FILE *file);
91 static wchar_t *(__cdecl *p_fgetws)(wchar_t *str, int size, FILE *file);
93 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
94 static int* (__cdecl *p_errno)(void);
96 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
97 const wchar_t *function, const wchar_t *file,
98 unsigned line, uintptr_t arg)
100 CHECK_EXPECT(invalid_parameter_handler);
101 ok(expression == NULL, "expression is not NULL\n");
102 ok(function == NULL, "function is not NULL\n");
103 ok(file == NULL, "file is not NULL\n");
104 ok(line == 0, "line = %u\n", line);
105 ok(arg == 0, "arg = %lx\n", (UINT_PTR)arg);
108 static BOOL init( void )
110 HMODULE hmod = LoadLibraryA("ucrtbase.dll");
112 if (!hmod)
114 win_skip("ucrtbase.dll not installed\n");
115 return FALSE;
118 p_vfprintf = (void *)GetProcAddress(hmod, "__stdio_common_vfprintf");
119 p_vfwprintf = (void *)GetProcAddress(hmod, "__stdio_common_vfwprintf");
120 p_vsprintf = (void *)GetProcAddress(hmod, "__stdio_common_vsprintf");
121 p_vsnprintf_s = (void *)GetProcAddress(hmod, "__stdio_common_vsnprintf_s");
122 p_vsnwprintf_s = (void *)GetProcAddress(hmod, "__stdio_common_vsnwprintf_s");
123 p_vsprintf_s = (void *)GetProcAddress(hmod, "__stdio_common_vsprintf_s");
124 p_vswprintf = (void *)GetProcAddress(hmod, "__stdio_common_vswprintf");
126 p_fopen = (void *)GetProcAddress(hmod, "fopen");
127 p_fclose = (void *)GetProcAddress(hmod, "fclose");
128 p_ftell = (void *)GetProcAddress(hmod, "ftell");
129 p_fgets = (void *)GetProcAddress(hmod, "fgets");
130 p_fgetws = (void *)GetProcAddress(hmod, "fgetws");
132 p_set_invalid_parameter_handler = (void *)GetProcAddress(hmod, "_set_invalid_parameter_handler");
133 p_errno = (void *)GetProcAddress(hmod, "_errno");
134 return TRUE;
137 static int WINAPIV vsprintf_wrapper(unsigned __int64 options, char *str,
138 size_t len, const char *format, ...)
140 int ret;
141 __ms_va_list valist;
142 __ms_va_start(valist, format);
143 ret = p_vsprintf(options, str, len, format, NULL, valist);
144 __ms_va_end(valist);
145 return ret;
148 static void test_snprintf (void)
150 const char *tests[] = {"short", "justfit", "justfits", "muchlonger"};
151 char buffer[8];
152 const int bufsiz = sizeof buffer;
153 unsigned int i;
155 /* Legacy _snprintf style termination */
156 for (i = 0; i < ARRAY_SIZE(tests); i++) {
157 const char *fmt = tests[i];
158 const int expect = strlen(fmt) > bufsiz ? -1 : strlen(fmt);
159 const int n = vsprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, bufsiz, fmt);
160 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
162 ok (n == expect, "\"%s\": expected %d, returned %d\n",
163 fmt, expect, n);
164 ok (!memcmp (fmt, buffer, valid),
165 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
168 /* C99 snprintf style termination */
169 for (i = 0; i < ARRAY_SIZE(tests); i++) {
170 const char *fmt = tests[i];
171 const int expect = strlen(fmt);
172 const int n = vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, bufsiz, fmt);
173 const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
175 ok (n == expect, "\"%s\": expected %d, returned %d\n",
176 fmt, expect, n);
177 ok (!memcmp (fmt, buffer, valid),
178 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
179 ok (buffer[valid] == '\0',
180 "\"%s\": Missing null termination (ret %d) - is %d\n", fmt, n, buffer[valid]);
183 /* swprintf style termination */
184 for (i = 0; i < ARRAY_SIZE(tests); i++) {
185 const char *fmt = tests[i];
186 const int expect = strlen(fmt) >= bufsiz ? -2 : strlen(fmt);
187 const int n = vsprintf_wrapper (0, buffer, bufsiz, fmt);
188 const int valid = n < 0 ? bufsiz - 1 : n;
190 ok (n == expect, "\"%s\": expected %d, returned %d\n",
191 fmt, expect, n);
192 ok (!memcmp (fmt, buffer, valid),
193 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
194 ok (buffer[valid] == '\0',
195 "\"%s\": Missing null termination (ret %d) - is %d\n", fmt, n, buffer[valid]);
198 ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, NULL, 0, "abcd") == 4,
199 "Failure to snprintf to NULL\n");
200 ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, NULL, 0, "abcd") == 4,
201 "Failure to snprintf to NULL\n");
202 ok (vsprintf_wrapper (0, NULL, 0, "abcd") == 4,
203 "Failure to snprintf to NULL\n");
206 static int WINAPIV vswprintf_wrapper(unsigned __int64 options, wchar_t *str,
207 size_t len, const wchar_t *format, ...)
209 int ret;
210 __ms_va_list valist;
211 __ms_va_start(valist, format);
212 ret = p_vswprintf(options, str, len, format, NULL, valist);
213 __ms_va_end(valist);
214 return ret;
217 static void test_swprintf (void)
219 const wchar_t str_short[] = {'s','h','o','r','t',0};
220 const wchar_t str_justfit[] = {'j','u','s','t','f','i','t',0};
221 const wchar_t str_justfits[] = {'j','u','s','t','f','i','t','s',0};
222 const wchar_t str_muchlonger[] = {'m','u','c','h','l','o','n','g','e','r',0};
223 const wchar_t *tests[] = {str_short, str_justfit, str_justfits, str_muchlonger};
225 wchar_t buffer[8];
226 char narrow[8], narrow_fmt[16];
227 const int bufsiz = ARRAY_SIZE(buffer);
228 unsigned int i;
230 /* Legacy _snprintf style termination */
231 for (i = 0; i < ARRAY_SIZE(tests); i++) {
232 const wchar_t *fmt = tests[i];
233 const int expect = wcslen(fmt) > bufsiz ? -1 : wcslen(fmt);
234 const int n = vswprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, bufsiz, fmt);
235 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
237 WideCharToMultiByte (CP_ACP, 0, buffer, -1, narrow, sizeof(narrow), NULL, NULL);
238 WideCharToMultiByte (CP_ACP, 0, fmt, -1, narrow_fmt, sizeof(narrow_fmt), NULL, NULL);
239 ok (n == expect, "\"%s\": expected %d, returned %d\n",
240 narrow_fmt, expect, n);
241 ok (!memcmp (fmt, buffer, valid * sizeof(wchar_t)),
242 "\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
245 /* C99 snprintf style termination */
246 for (i = 0; i < ARRAY_SIZE(tests); i++) {
247 const wchar_t *fmt = tests[i];
248 const int expect = wcslen(fmt);
249 const int n = vswprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, bufsiz, fmt);
250 const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
252 WideCharToMultiByte (CP_ACP, 0, buffer, -1, narrow, sizeof(narrow), NULL, NULL);
253 WideCharToMultiByte (CP_ACP, 0, fmt, -1, narrow_fmt, sizeof(narrow_fmt), NULL, NULL);
254 ok (n == expect, "\"%s\": expected %d, returned %d\n",
255 narrow_fmt, expect, n);
256 ok (!memcmp (fmt, buffer, valid * sizeof(wchar_t)),
257 "\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
258 ok (buffer[valid] == '\0',
259 "\"%s\": Missing null termination (ret %d) - is %d\n", narrow_fmt, n, buffer[valid]);
262 /* swprintf style termination */
263 for (i = 0; i < ARRAY_SIZE(tests); i++) {
264 const wchar_t *fmt = tests[i];
265 const int expect = wcslen(fmt) >= bufsiz ? -2 : wcslen(fmt);
266 const int n = vswprintf_wrapper (0, buffer, bufsiz, fmt);
267 const int valid = n < 0 ? bufsiz - 1 : n;
269 WideCharToMultiByte (CP_ACP, 0, buffer, -1, narrow, sizeof(narrow), NULL, NULL);
270 WideCharToMultiByte (CP_ACP, 0, fmt, -1, narrow_fmt, sizeof(narrow_fmt), NULL, NULL);
271 ok (n == expect, "\"%s\": expected %d, returned %d\n",
272 narrow_fmt, expect, n);
273 ok (!memcmp (fmt, buffer, valid * sizeof(wchar_t)),
274 "\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
275 ok (buffer[valid] == '\0',
276 "\"%s\": Missing null termination (ret %d) - is %d\n", narrow_fmt, n, buffer[valid]);
279 ok (vswprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, NULL, 0, str_short) == 5,
280 "Failure to swprintf to NULL\n");
281 ok (vswprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, NULL, 0, str_short) == 5,
282 "Failure to swprintf to NULL\n");
283 ok (vswprintf_wrapper (0, NULL, 0, str_short) == 5,
284 "Failure to swprintf to NULL\n");
287 static int WINAPIV vfprintf_wrapper(FILE *file,
288 const char *format, ...)
290 int ret;
291 __ms_va_list valist;
292 __ms_va_start(valist, format);
293 ret = p_vfprintf(0, file, format, NULL, valist);
294 __ms_va_end(valist);
295 return ret;
298 static void test_fprintf(void)
300 static const char file_name[] = "fprintf.tst";
302 FILE *fp = p_fopen(file_name, "wb");
303 char buf[1024];
304 int ret;
306 ret = vfprintf_wrapper(fp, "simple test\n");
307 ok(ret == 12, "ret = %d\n", ret);
308 ret = p_ftell(fp);
309 ok(ret == 12, "ftell returned %d\n", ret);
311 ret = vfprintf_wrapper(fp, "contains%cnull\n", '\0');
312 ok(ret == 14, "ret = %d\n", ret);
313 ret = p_ftell(fp);
314 ok(ret == 26, "ftell returned %d\n", ret);
316 p_fclose(fp);
318 fp = p_fopen(file_name, "rb");
319 p_fgets(buf, sizeof(buf), fp);
320 ret = p_ftell(fp);
321 ok(ret == 12, "ftell returned %d\n", ret);
322 ok(!strcmp(buf, "simple test\n"), "buf = %s\n", buf);
324 p_fgets(buf, sizeof(buf), fp);
325 ret = p_ftell(fp);
326 ok(ret == 26, "ret = %d\n", ret);
327 ok(!memcmp(buf, "contains\0null\n", 14), "buf = %s\n", buf);
329 p_fclose(fp);
331 fp = p_fopen(file_name, "wt");
333 ret = vfprintf_wrapper(fp, "simple test\n");
334 ok(ret == 12, "ret = %d\n", ret);
335 ret = p_ftell(fp);
336 ok(ret == 13, "ftell returned %d\n", ret);
338 ret = vfprintf_wrapper(fp, "contains%cnull\n", '\0');
339 ok(ret == 14, "ret = %d\n", ret);
340 ret = p_ftell(fp);
341 ok(ret == 28, "ftell returned %d\n", ret);
343 p_fclose(fp);
345 fp = p_fopen(file_name, "rb");
346 p_fgets(buf, sizeof(buf), fp);
347 ret = p_ftell(fp);
348 ok(ret == 13, "ftell returned %d\n", ret);
349 ok(!strcmp(buf, "simple test\r\n"), "buf = %s\n", buf);
351 p_fgets(buf, sizeof(buf), fp);
352 ret = p_ftell(fp);
353 ok(ret == 28, "ret = %d\n", ret);
354 ok(!memcmp(buf, "contains\0null\r\n", 15), "buf = %s\n", buf);
356 p_fclose(fp);
357 unlink(file_name);
360 static int WINAPIV vfwprintf_wrapper(FILE *file,
361 const wchar_t *format, ...)
363 int ret;
364 __ms_va_list valist;
365 __ms_va_start(valist, format);
366 ret = p_vfwprintf(0, file, format, NULL, valist);
367 __ms_va_end(valist);
368 return ret;
371 static void test_fwprintf(void)
373 static const char file_name[] = "fprintf.tst";
374 static const WCHAR simple[] = {'s','i','m','p','l','e',' ','t','e','s','t','\n',0};
375 static const WCHAR cont_fmt[] = {'c','o','n','t','a','i','n','s','%','c','n','u','l','l','\n',0};
376 static const WCHAR cont[] = {'c','o','n','t','a','i','n','s','\0','n','u','l','l','\n',0};
378 FILE *fp = p_fopen(file_name, "wb");
379 wchar_t bufw[1024];
380 char bufa[1024];
381 int ret;
383 ret = vfwprintf_wrapper(fp, simple);
384 ok(ret == 12, "ret = %d\n", ret);
385 ret = p_ftell(fp);
386 ok(ret == 24, "ftell returned %d\n", ret);
388 ret = vfwprintf_wrapper(fp, cont_fmt, '\0');
389 ok(ret == 14, "ret = %d\n", ret);
390 ret = p_ftell(fp);
391 ok(ret == 52, "ftell returned %d\n", ret);
393 p_fclose(fp);
395 fp = p_fopen(file_name, "rb");
396 p_fgetws(bufw, ARRAY_SIZE(bufw), fp);
397 ret = p_ftell(fp);
398 ok(ret == 24, "ftell returned %d\n", ret);
399 ok(!wcscmp(bufw, simple), "buf = %s\n", wine_dbgstr_w(bufw));
401 p_fgetws(bufw, ARRAY_SIZE(bufw), fp);
402 ret = p_ftell(fp);
403 ok(ret == 52, "ret = %d\n", ret);
404 ok(!memcmp(bufw, cont, 28), "buf = %s\n", wine_dbgstr_w(bufw));
406 p_fclose(fp);
408 fp = p_fopen(file_name, "wt");
410 ret = vfwprintf_wrapper(fp, simple);
411 ok(ret == 12, "ret = %d\n", ret);
412 ret = p_ftell(fp);
413 ok(ret == 13, "ftell returned %d\n", ret);
415 ret = vfwprintf_wrapper(fp, cont_fmt, '\0');
416 ok(ret == 14, "ret = %d\n", ret);
417 ret = p_ftell(fp);
418 ok(ret == 28, "ftell returned %d\n", ret);
420 p_fclose(fp);
422 fp = p_fopen(file_name, "rb");
423 p_fgets(bufa, sizeof(bufa), fp);
424 ret = p_ftell(fp);
425 ok(ret == 13, "ftell returned %d\n", ret);
426 ok(!strcmp(bufa, "simple test\r\n"), "buf = %s\n", bufa);
428 p_fgets(bufa, sizeof(bufa), fp);
429 ret = p_ftell(fp);
430 ok(ret == 28, "ret = %d\n", ret);
431 ok(!memcmp(bufa, "contains\0null\r\n", 15), "buf = %s\n", bufa);
433 p_fclose(fp);
434 unlink(file_name);
436 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
437 "Invalid parameter handler was already set\n");
439 /* NULL format */
440 errno = 0xdeadbeef;
441 SET_EXPECT(invalid_parameter_handler);
442 ret = vfwprintf_wrapper(fp, NULL);
443 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
444 ok(ret == -1, "expected ret -1, got %d\n", ret);
445 CHECK_CALLED(invalid_parameter_handler);
447 /* NULL file */
448 errno = 0xdeadbeef;
449 SET_EXPECT(invalid_parameter_handler);
450 ret = vfwprintf_wrapper(NULL, simple);
451 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
452 ok(ret == -1, "expected ret -1, got %d\n", ret);
453 CHECK_CALLED(invalid_parameter_handler);
455 /* format using % with NULL arglist*/
456 /* crashes on Windows */
457 /* ret = p_vfwprintf(0, fp, cont_fmt, NULL, NULL); */
459 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
460 "Cannot reset invalid parameter handler\n");
463 static int WINAPIV _vsnprintf_s_wrapper(char *str, size_t sizeOfBuffer,
464 size_t count, const char *format, ...)
466 int ret;
467 __ms_va_list valist;
468 __ms_va_start(valist, format);
469 ret = p_vsnprintf_s(0, str, sizeOfBuffer, count, format, NULL, valist);
470 __ms_va_end(valist);
471 return ret;
474 static void test_vsnprintf_s(void)
476 const char format[] = "AB%uC";
477 const char out7[] = "AB123C";
478 const char out6[] = "AB123";
479 const char out2[] = "A";
480 const char out1[] = "";
481 char buffer[14] = { 0 };
482 int exp, got;
484 /* Enough room. */
485 exp = strlen(out7);
487 got = _vsnprintf_s_wrapper(buffer, 14, _TRUNCATE, format, 123);
488 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
489 ok( !strcmp(out7, buffer), "buffer wrong, got=%s\n", buffer);
491 got = _vsnprintf_s_wrapper(buffer, 12, _TRUNCATE, format, 123);
492 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
493 ok( !strcmp(out7, buffer), "buffer wrong, got=%s\n", buffer);
495 got = _vsnprintf_s_wrapper(buffer, 7, _TRUNCATE, format, 123);
496 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
497 ok( !strcmp(out7, buffer), "buffer wrong, got=%s\n", buffer);
499 /* Not enough room. */
500 exp = -1;
502 got = _vsnprintf_s_wrapper(buffer, 6, _TRUNCATE, format, 123);
503 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
504 ok( !strcmp(out6, buffer), "buffer wrong, got=%s\n", buffer);
506 got = _vsnprintf_s_wrapper(buffer, 2, _TRUNCATE, format, 123);
507 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
508 ok( !strcmp(out2, buffer), "buffer wrong, got=%s\n", buffer);
510 got = _vsnprintf_s_wrapper(buffer, 1, _TRUNCATE, format, 123);
511 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
512 ok( !strcmp(out1, buffer), "buffer wrong, got=%s\n", buffer);
515 static int WINAPIV _vsnwprintf_s_wrapper(WCHAR *str, size_t sizeOfBuffer,
516 size_t count, const WCHAR *format, ...)
518 int ret;
519 __ms_va_list valist;
520 __ms_va_start(valist, format);
521 ret = p_vsnwprintf_s(0, str, sizeOfBuffer, count, format, NULL, valist);
522 __ms_va_end(valist);
523 return ret;
526 static void test_vsnwprintf_s(void)
528 const WCHAR format[] = {'A','B','%','u','C',0};
529 const WCHAR out7[] = {'A','B','1','2','3','C',0};
530 const WCHAR out6[] = {'A','B','1','2','3',0};
531 const WCHAR out2[] = {'A',0};
532 const WCHAR out1[] = {0};
533 WCHAR buffer[14] = { 0 };
534 int exp, got;
536 /* Enough room. */
537 exp = lstrlenW(out7);
539 got = _vsnwprintf_s_wrapper(buffer, 14, _TRUNCATE, format, 123);
540 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
541 ok( !lstrcmpW(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
543 got = _vsnwprintf_s_wrapper(buffer, 12, _TRUNCATE, format, 123);
544 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
545 ok( !lstrcmpW(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
547 got = _vsnwprintf_s_wrapper(buffer, 7, _TRUNCATE, format, 123);
548 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
549 ok( !lstrcmpW(out7, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
551 /* Not enough room. */
552 exp = -1;
554 got = _vsnwprintf_s_wrapper(buffer, 6, _TRUNCATE, format, 123);
555 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
556 ok( !lstrcmpW(out6, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
558 got = _vsnwprintf_s_wrapper(buffer, 2, _TRUNCATE, format, 123);
559 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
560 ok( !lstrcmpW(out2, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
562 got = _vsnwprintf_s_wrapper(buffer, 1, _TRUNCATE, format, 123);
563 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
564 ok( !lstrcmpW(out1, buffer), "buffer wrong, got=%s\n", wine_dbgstr_w(buffer));
567 static void test_printf_legacy_wide(void)
569 const wchar_t wide[] = {'A','B','C','D',0};
570 const char narrow[] = "abcd";
571 const char out[] = "abcd ABCD";
572 /* The legacy wide flag doesn't affect narrow printfs, so the same
573 * format should behave the same both with and without the flag. */
574 const char narrow_fmt[] = "%s %ls";
575 /* The standard behaviour is to use the same format as for the narrow
576 * case, while the legacy case has got a different meaning for %s. */
577 const wchar_t std_wide_fmt[] = {'%','s',' ','%','l','s',0};
578 const wchar_t legacy_wide_fmt[] = {'%','h','s',' ','%','s',0};
579 char buffer[20];
580 wchar_t wbuffer[20];
582 vsprintf_wrapper(0, buffer, sizeof(buffer), narrow_fmt, narrow, wide);
583 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
584 vsprintf_wrapper(_CRT_INTERNAL_PRINTF_LEGACY_WIDE_SPECIFIERS, buffer, sizeof(buffer), narrow_fmt, narrow, wide);
585 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
587 vswprintf_wrapper(0, wbuffer, sizeof(wbuffer), std_wide_fmt, narrow, wide);
588 WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
589 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
590 vswprintf_wrapper(_CRT_INTERNAL_PRINTF_LEGACY_WIDE_SPECIFIERS, wbuffer, sizeof(wbuffer), legacy_wide_fmt, narrow, wide);
591 WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
592 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
595 static void test_printf_legacy_msvcrt(void)
597 char buf[50];
599 /* In standard mode, %F is a float format conversion, while it is a
600 * length modifier in legacy msvcrt mode. In legacy mode, N is also
601 * a length modifier. */
602 vsprintf_wrapper(0, buf, sizeof(buf), "%F", 1.23);
603 ok(!strcmp(buf, "1.230000"), "buf = %s\n", buf);
604 vsprintf_wrapper(_CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%Fd %Nd", 123, 456);
605 ok(!strcmp(buf, "123 456"), "buf = %s\n", buf);
607 vsprintf_wrapper(0, buf, sizeof(buf), "%f %F %f %e %E %g %G", INFINITY, INFINITY, -INFINITY, INFINITY, INFINITY, INFINITY, INFINITY);
608 ok(!strcmp(buf, "inf INF -inf inf INF inf INF"), "buf = %s\n", buf);
609 vsprintf_wrapper(_CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%f", INFINITY);
610 ok(!strcmp(buf, "1.#INF00"), "buf = %s\n", buf);
611 vsprintf_wrapper(0, buf, sizeof(buf), "%f %F", NAN, NAN);
612 ok(!strcmp(buf, "nan NAN"), "buf = %s\n", buf);
613 vsprintf_wrapper(_CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%f", NAN);
614 ok(!strcmp(buf, "1.#QNAN0"), "buf = %s\n", buf);
615 vsprintf_wrapper(0, buf, sizeof(buf), "%f %F", IND, IND);
616 ok(!strcmp(buf, "-nan(ind) -NAN(IND)"), "buf = %s\n", buf);
617 vsprintf_wrapper(_CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%f", IND);
618 ok(!strcmp(buf, "-1.#IND00"), "buf = %s\n", buf);
621 static void test_printf_legacy_three_digit_exp(void)
623 char buf[20];
625 vsprintf_wrapper(0, buf, sizeof(buf), "%E", 1.23);
626 ok(!strcmp(buf, "1.230000E+00"), "buf = %s\n", buf);
627 vsprintf_wrapper(_CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS, buf, sizeof(buf), "%E", 1.23);
628 ok(!strcmp(buf, "1.230000E+000"), "buf = %s\n", buf);
629 vsprintf_wrapper(0, buf, sizeof(buf), "%E", 1.23e+123);
630 ok(!strcmp(buf, "1.230000E+123"), "buf = %s\n", buf);
633 static void test_printf_c99(void)
635 char buf[30];
636 int i;
638 /* The msvcrt compatibility flag doesn't affect whether 'z' is interpreted
639 * as size_t size for integers. */
640 for (i = 0; i < 2; i++) {
641 unsigned __int64 options = (i == 0) ? 0 :
642 _CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY;
644 /* z modifier accepts size_t argument */
645 vsprintf_wrapper(options, buf, sizeof(buf), "%zx %d", SIZE_MAX, 1);
646 if (sizeof(size_t) == 8)
647 ok(!strcmp(buf, "ffffffffffffffff 1"), "buf = %s\n", buf);
648 else
649 ok(!strcmp(buf, "ffffffff 1"), "buf = %s\n", buf);
651 /* j modifier with signed format accepts intmax_t argument */
652 vsprintf_wrapper(options, buf, sizeof(buf), "%jd %d", INTMAX_MIN, 1);
653 ok(!strcmp(buf, "-9223372036854775808 1"), "buf = %s\n", buf);
655 /* j modifier with unsigned format accepts uintmax_t argument */
656 vsprintf_wrapper(options, buf, sizeof(buf), "%ju %d", UINTMAX_MAX, 1);
657 ok(!strcmp(buf, "18446744073709551615 1"), "buf = %s\n", buf);
659 /* t modifier accepts ptrdiff_t argument */
660 vsprintf_wrapper(options, buf, sizeof(buf), "%td %d", PTRDIFF_MIN, 1);
661 if (sizeof(ptrdiff_t) == 8)
662 ok(!strcmp(buf, "-9223372036854775808 1"), "buf = %s\n", buf);
663 else
664 ok(!strcmp(buf, "-2147483648 1"), "buf = %s\n", buf);
668 static void test_printf_natural_string(void)
670 const wchar_t wide[] = {'A','B','C','D',0};
671 const char narrow[] = "abcd";
672 const char narrow_fmt[] = "%s %Ts";
673 const char narrow_out[] = "abcd abcd";
674 const wchar_t wide_fmt[] = {'%','s',' ','%','T','s',0};
675 const wchar_t wide_out[] = {'a','b','c','d',' ','A','B','C','D',0};
676 char buffer[20];
677 wchar_t wbuffer[20];
679 vsprintf_wrapper(0, buffer, sizeof(buffer), narrow_fmt, narrow, narrow);
680 ok(!strcmp(buffer, narrow_out), "buffer wrong, got=%s\n", buffer);
682 vswprintf_wrapper(0, wbuffer, sizeof(wbuffer), wide_fmt, narrow, wide);
683 ok(!lstrcmpW(wbuffer, wide_out), "buffer wrong, got=%s\n", wine_dbgstr_w(wbuffer));
686 START_TEST(printf)
688 if (!init()) return;
690 test_snprintf();
691 test_swprintf();
692 test_fprintf();
693 test_fwprintf();
694 test_vsnprintf_s();
695 test_vsnwprintf_s();
696 test_printf_legacy_wide();
697 test_printf_legacy_msvcrt();
698 test_printf_legacy_three_digit_exp();
699 test_printf_c99();
700 test_printf_natural_string();