ucrtbase: Add a test for snprintf to a NULL buffer.
[wine.git] / dlls / ucrtbase / tests / printf.c
blobd3ed0a200361970b7781027a12ed39de3d99fe8d
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>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
31 #include "wine/test.h"
33 #define UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION (0x0001)
34 #define UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR (0x0002)
35 #define UCRTBASE_PRINTF_LEGACY_WIDE_SPECIFIERS (0x0004)
36 #define UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY (0x0008)
37 #define UCRTBASE_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS (0x0010)
39 static inline float __port_infinity(void)
41 static const unsigned __inf_bytes = 0x7f800000;
42 return *(const float *)&__inf_bytes;
44 #define INFINITY __port_infinity()
46 static inline float __port_nan(void)
48 static const unsigned __nan_bytes = 0x7fc00000;
49 return *(const float *)&__nan_bytes;
51 #define NAN __port_nan()
53 static inline float __port_ind(void)
55 static const unsigned __ind_bytes = 0xffc00000;
56 return *(const float *)&__ind_bytes;
58 #define IND __port_ind()
60 static int (__cdecl *p_vfprintf)(unsigned __int64 options, FILE *file, const char *format,
61 void *locale, __ms_va_list valist);
62 static int (__cdecl *p_vsprintf)(unsigned __int64 options, char *str, size_t len, const char *format,
63 void *locale, __ms_va_list valist);
64 static int (__cdecl *p_vsnprintf_s)(unsigned __int64 options, char *str, size_t sizeOfBuffer, size_t count, const char *format,
65 void *locale, __ms_va_list valist);
66 static int (__cdecl *p_vsprintf_s)(unsigned __int64 options, char *str, size_t count, const char *format,
67 void *locale, __ms_va_list valist);
68 static int (__cdecl *p_vswprintf)(unsigned __int64 options, wchar_t *str, size_t len, const wchar_t *format,
69 void *locale, __ms_va_list valist);
71 static FILE *(__cdecl *p_fopen)(const char *name, const char *mode);
72 static int (__cdecl *p_fclose)(FILE *file);
73 static long (__cdecl *p_ftell)(FILE *file);
74 static char *(__cdecl *p_fgets)(char *str, int size, FILE *file);
76 static BOOL init( void )
78 HMODULE hmod = LoadLibraryA("ucrtbase.dll");
80 if (!hmod)
82 win_skip("ucrtbase.dll not installed\n");
83 return FALSE;
86 p_vfprintf = (void *)GetProcAddress(hmod, "__stdio_common_vfprintf");
87 p_vsprintf = (void *)GetProcAddress(hmod, "__stdio_common_vsprintf");
88 p_vsnprintf_s = (void *)GetProcAddress(hmod, "__stdio_common_vsnprintf_s");
89 p_vsprintf_s = (void *)GetProcAddress(hmod, "__stdio_common_vsprintf_s");
90 p_vswprintf = (void *)GetProcAddress(hmod, "__stdio_common_vswprintf");
92 p_fopen = (void *)GetProcAddress(hmod, "fopen");
93 p_fclose = (void *)GetProcAddress(hmod, "fclose");
94 p_ftell = (void *)GetProcAddress(hmod, "ftell");
95 p_fgets = (void *)GetProcAddress(hmod, "fgets");
96 return TRUE;
99 static int __cdecl vsprintf_wrapper(unsigned __int64 options, char *str,
100 size_t len, const char *format, ...)
102 int ret;
103 __ms_va_list valist;
104 __ms_va_start(valist, format);
105 ret = p_vsprintf(options, str, len, format, NULL, valist);
106 __ms_va_end(valist);
107 return ret;
110 static void test_snprintf (void)
112 const char *tests[] = {"short", "justfit", "justfits", "muchlonger"};
113 char buffer[8];
114 const int bufsiz = sizeof buffer;
115 unsigned int i;
117 /* Legacy _snprintf style termination */
118 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
119 const char *fmt = tests[i];
120 const int expect = strlen(fmt) > bufsiz ? -1 : strlen(fmt);
121 const int n = vsprintf_wrapper (UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, bufsiz, fmt);
122 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
124 ok (n == expect, "\"%s\": expected %d, returned %d\n",
125 fmt, expect, n);
126 ok (!memcmp (fmt, buffer, valid),
127 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
130 /* C99 snprintf style termination */
131 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
132 const char *fmt = tests[i];
133 const int expect = strlen(fmt);
134 const int n = vsprintf_wrapper (UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR, buffer, bufsiz, fmt);
135 const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
137 ok (n == expect, "\"%s\": expected %d, returned %d\n",
138 fmt, expect, n);
139 ok (!memcmp (fmt, buffer, valid),
140 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
141 ok (buffer[valid] == '\0',
142 "\"%s\": Missing null termination (ret %d) - is %d\n", fmt, n, buffer[valid]);
145 /* swprintf style termination */
146 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
147 const char *fmt = tests[i];
148 const int expect = strlen(fmt) >= bufsiz ? -2 : strlen(fmt);
149 const int n = vsprintf_wrapper (0, buffer, bufsiz, fmt);
150 const int valid = n < 0 ? bufsiz - 1 : n;
152 ok (n == expect, "\"%s\": expected %d, returned %d\n",
153 fmt, expect, n);
154 ok (!memcmp (fmt, buffer, valid),
155 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
156 ok (buffer[valid] == '\0',
157 "\"%s\": Missing null termination (ret %d) - is %d\n", fmt, n, buffer[valid]);
160 ok (vsprintf_wrapper (UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR, NULL, 0, "abcd") == 4,
161 "Failure to snprintf to NULL\n");
164 static int __cdecl vswprintf_wrapper(unsigned __int64 options, wchar_t *str,
165 size_t len, const wchar_t *format, ...)
167 int ret;
168 __ms_va_list valist;
169 __ms_va_start(valist, format);
170 ret = p_vswprintf(options, str, len, format, NULL, valist);
171 __ms_va_end(valist);
172 return ret;
175 static void test_swprintf (void)
177 const wchar_t str_short[] = {'s','h','o','r','t',0};
178 const wchar_t str_justfit[] = {'j','u','s','t','f','i','t',0};
179 const wchar_t str_justfits[] = {'j','u','s','t','f','i','t','s',0};
180 const wchar_t str_muchlonger[] = {'m','u','c','h','l','o','n','g','e','r',0};
181 const wchar_t *tests[] = {str_short, str_justfit, str_justfits, str_muchlonger};
183 wchar_t buffer[8];
184 char narrow[8], narrow_fmt[16];
185 const int bufsiz = sizeof buffer / sizeof buffer[0];
186 unsigned int i;
188 /* Legacy _snprintf style termination */
189 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
190 const wchar_t *fmt = tests[i];
191 const int expect = wcslen(fmt) > bufsiz ? -1 : wcslen(fmt);
192 const int n = vswprintf_wrapper (UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, bufsiz, fmt);
193 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
195 WideCharToMultiByte (CP_ACP, 0, buffer, -1, narrow, sizeof(narrow), NULL, NULL);
196 WideCharToMultiByte (CP_ACP, 0, fmt, -1, narrow_fmt, sizeof(narrow_fmt), NULL, NULL);
197 ok (n == expect, "\"%s\": expected %d, returned %d\n",
198 narrow_fmt, expect, n);
199 ok (!memcmp (fmt, buffer, valid * sizeof(wchar_t)),
200 "\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
203 /* C99 snprintf style termination */
204 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
205 const wchar_t *fmt = tests[i];
206 const int expect = wcslen(fmt);
207 const int n = vswprintf_wrapper (UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR, buffer, bufsiz, fmt);
208 const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
210 WideCharToMultiByte (CP_ACP, 0, buffer, -1, narrow, sizeof(narrow), NULL, NULL);
211 WideCharToMultiByte (CP_ACP, 0, fmt, -1, narrow_fmt, sizeof(narrow_fmt), NULL, NULL);
212 ok (n == expect, "\"%s\": expected %d, returned %d\n",
213 narrow_fmt, expect, n);
214 ok (!memcmp (fmt, buffer, valid * sizeof(wchar_t)),
215 "\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
216 ok (buffer[valid] == '\0',
217 "\"%s\": Missing null termination (ret %d) - is %d\n", narrow_fmt, n, buffer[valid]);
220 /* swprintf style termination */
221 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
222 const wchar_t *fmt = tests[i];
223 const int expect = wcslen(fmt) >= bufsiz ? -2 : wcslen(fmt);
224 const int n = vswprintf_wrapper (0, buffer, bufsiz, fmt);
225 const int valid = n < 0 ? bufsiz - 1 : n;
227 WideCharToMultiByte (CP_ACP, 0, buffer, -1, narrow, sizeof(narrow), NULL, NULL);
228 WideCharToMultiByte (CP_ACP, 0, fmt, -1, narrow_fmt, sizeof(narrow_fmt), NULL, NULL);
229 ok (n == expect, "\"%s\": expected %d, returned %d\n",
230 narrow_fmt, expect, n);
231 ok (!memcmp (fmt, buffer, valid * sizeof(wchar_t)),
232 "\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
233 ok (buffer[valid] == '\0',
234 "\"%s\": Missing null termination (ret %d) - is %d\n", narrow_fmt, n, buffer[valid]);
238 static int __cdecl vfprintf_wrapper(FILE *file,
239 const char *format, ...)
241 int ret;
242 __ms_va_list valist;
243 __ms_va_start(valist, format);
244 ret = p_vfprintf(0, file, format, NULL, valist);
245 __ms_va_end(valist);
246 return ret;
249 static void test_fprintf(void)
251 static const char file_name[] = "fprintf.tst";
253 FILE *fp = p_fopen(file_name, "wb");
254 char buf[1024];
255 int ret;
257 ret = vfprintf_wrapper(fp, "simple test\n");
258 ok(ret == 12, "ret = %d\n", ret);
259 ret = p_ftell(fp);
260 ok(ret == 12, "ftell returned %d\n", ret);
262 ret = vfprintf_wrapper(fp, "contains%cnull\n", '\0');
263 ok(ret == 14, "ret = %d\n", ret);
264 ret = p_ftell(fp);
265 ok(ret == 26, "ftell returned %d\n", ret);
267 p_fclose(fp);
269 fp = p_fopen(file_name, "rb");
270 p_fgets(buf, sizeof(buf), fp);
271 ret = p_ftell(fp);
272 ok(ret == 12, "ftell returned %d\n", ret);
273 ok(!strcmp(buf, "simple test\n"), "buf = %s\n", buf);
275 p_fgets(buf, sizeof(buf), fp);
276 ret = p_ftell(fp);
277 ok(ret == 26, "ret = %d\n", ret);
278 ok(!memcmp(buf, "contains\0null\n", 14), "buf = %s\n", buf);
280 p_fclose(fp);
282 fp = p_fopen(file_name, "wt");
284 ret = vfprintf_wrapper(fp, "simple test\n");
285 ok(ret == 12, "ret = %d\n", ret);
286 ret = p_ftell(fp);
287 ok(ret == 13, "ftell returned %d\n", ret);
289 ret = vfprintf_wrapper(fp, "contains%cnull\n", '\0');
290 ok(ret == 14, "ret = %d\n", ret);
291 ret = p_ftell(fp);
292 ok(ret == 28, "ftell returned %d\n", ret);
294 p_fclose(fp);
296 fp = p_fopen(file_name, "rb");
297 p_fgets(buf, sizeof(buf), fp);
298 ret = p_ftell(fp);
299 ok(ret == 13, "ftell returned %d\n", ret);
300 ok(!strcmp(buf, "simple test\r\n"), "buf = %s\n", buf);
302 p_fgets(buf, sizeof(buf), fp);
303 ret = p_ftell(fp);
304 ok(ret == 28, "ret = %d\n", ret);
305 ok(!memcmp(buf, "contains\0null\r\n", 15), "buf = %s\n", buf);
307 p_fclose(fp);
308 unlink(file_name);
311 static int __cdecl _vsnprintf_s_wrapper(char *str, size_t sizeOfBuffer,
312 size_t count, const char *format, ...)
314 int ret;
315 __ms_va_list valist;
316 __ms_va_start(valist, format);
317 ret = p_vsnprintf_s(0, str, sizeOfBuffer, count, format, NULL, valist);
318 __ms_va_end(valist);
319 return ret;
322 static void test_vsnprintf_s(void)
324 const char format[] = "AB%uC";
325 const char out7[] = "AB123C";
326 const char out6[] = "AB123";
327 const char out2[] = "A";
328 const char out1[] = "";
329 char buffer[14] = { 0 };
330 int exp, got;
332 /* Enough room. */
333 exp = strlen(out7);
335 got = _vsnprintf_s_wrapper(buffer, 14, _TRUNCATE, format, 123);
336 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
337 ok( !strcmp(out7, buffer), "buffer wrong, got=%s\n", buffer);
339 got = _vsnprintf_s_wrapper(buffer, 12, _TRUNCATE, format, 123);
340 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
341 ok( !strcmp(out7, buffer), "buffer wrong, got=%s\n", buffer);
343 got = _vsnprintf_s_wrapper(buffer, 7, _TRUNCATE, format, 123);
344 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
345 ok( !strcmp(out7, buffer), "buffer wrong, got=%s\n", buffer);
347 /* Not enough room. */
348 exp = -1;
350 got = _vsnprintf_s_wrapper(buffer, 6, _TRUNCATE, format, 123);
351 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
352 ok( !strcmp(out6, buffer), "buffer wrong, got=%s\n", buffer);
354 got = _vsnprintf_s_wrapper(buffer, 2, _TRUNCATE, format, 123);
355 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
356 ok( !strcmp(out2, buffer), "buffer wrong, got=%s\n", buffer);
358 got = _vsnprintf_s_wrapper(buffer, 1, _TRUNCATE, format, 123);
359 ok( exp == got, "length wrong, expect=%d, got=%d\n", exp, got);
360 ok( !strcmp(out1, buffer), "buffer wrong, got=%s\n", buffer);
363 static void test_printf_legacy_wide(void)
365 const wchar_t wide[] = {'A','B','C','D',0};
366 const char narrow[] = "abcd";
367 const char out[] = "abcd ABCD";
368 /* The legacy wide flag doesn't affect narrow printfs, so the same
369 * format should behave the same both with and without the flag. */
370 const char narrow_fmt[] = "%s %ls";
371 /* The standard behaviour is to use the same format as for the narrow
372 * case, while the legacy case has got a different meaning for %s. */
373 const wchar_t std_wide_fmt[] = {'%','s',' ','%','l','s',0};
374 const wchar_t legacy_wide_fmt[] = {'%','h','s',' ','%','s',0};
375 char buffer[20];
376 wchar_t wbuffer[20];
378 vsprintf_wrapper(0, buffer, sizeof(buffer), narrow_fmt, narrow, wide);
379 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
380 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_WIDE_SPECIFIERS, buffer, sizeof(buffer), narrow_fmt, narrow, wide);
381 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
383 vswprintf_wrapper(0, wbuffer, sizeof(wbuffer), std_wide_fmt, narrow, wide);
384 WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
385 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
386 vswprintf_wrapper(UCRTBASE_PRINTF_LEGACY_WIDE_SPECIFIERS, wbuffer, sizeof(wbuffer), legacy_wide_fmt, narrow, wide);
387 WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
388 ok(!strcmp(buffer, out), "buffer wrong, got=%s\n", buffer);
391 static void test_printf_legacy_msvcrt(void)
393 char buf[50];
395 /* In standard mode, %F is a float format conversion, while it is a
396 * length modifier in legacy msvcrt mode. In legacy mode, N is also
397 * a length modifier. */
398 vsprintf_wrapper(0, buf, sizeof(buf), "%F", 1.23);
399 ok(!strcmp(buf, "1.230000"), "buf = %s\n", buf);
400 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%Fd %Nd", 123, 456);
401 ok(!strcmp(buf, "123 456"), "buf = %s\n", buf);
403 vsprintf_wrapper(0, buf, sizeof(buf), "%f %F %f %e %E %g %G", INFINITY, INFINITY, -INFINITY, INFINITY, INFINITY, INFINITY, INFINITY);
404 ok(!strcmp(buf, "inf INF -inf inf INF inf INF"), "buf = %s\n", buf);
405 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%f", INFINITY);
406 ok(!strcmp(buf, "1.#INF00"), "buf = %s\n", buf);
407 vsprintf_wrapper(0, buf, sizeof(buf), "%f %F", NAN, NAN);
408 ok(!strcmp(buf, "nan NAN"), "buf = %s\n", buf);
409 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%f", NAN);
410 ok(!strcmp(buf, "1.#QNAN0"), "buf = %s\n", buf);
411 vsprintf_wrapper(0, buf, sizeof(buf), "%f %F", IND, IND);
412 ok(!strcmp(buf, "-nan(ind) -NAN(IND)"), "buf = %s\n", buf);
413 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY, buf, sizeof(buf), "%f", IND);
414 ok(!strcmp(buf, "-1.#IND00"), "buf = %s\n", buf);
417 static void test_printf_legacy_three_digit_exp(void)
419 char buf[20];
421 vsprintf_wrapper(0, buf, sizeof(buf), "%E", 1.23);
422 ok(!strcmp(buf, "1.230000E+00"), "buf = %s\n", buf);
423 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS, buf, sizeof(buf), "%E", 1.23);
424 ok(!strcmp(buf, "1.230000E+000"), "buf = %s\n", buf);
425 vsprintf_wrapper(0, buf, sizeof(buf), "%E", 1.23e+123);
426 ok(!strcmp(buf, "1.230000E+123"), "buf = %s\n", buf);
429 static void test_printf_c99(void)
431 char buf[20];
433 /* The msvcrt compatibility flag doesn't affect whether 'z' is interpreted
434 * as size_t size for integers. */
435 if (sizeof(void*) == 8) {
436 vsprintf_wrapper(0, buf, sizeof(buf), "%zx %d",
437 (size_t) 0x12345678123456, 1);
438 ok(!strcmp(buf, "12345678123456 1"), "buf = %s\n", buf);
439 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY,
440 buf, sizeof(buf), "%zx %d", (size_t) 0x12345678123456, 1);
441 ok(!strcmp(buf, "12345678123456 1"), "buf = %s\n", buf);
442 } else {
443 vsprintf_wrapper(0, buf, sizeof(buf), "%zx %d",
444 (size_t) 0x123456, 1);
445 ok(!strcmp(buf, "123456 1"), "buf = %s\n", buf);
446 vsprintf_wrapper(UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY,
447 buf, sizeof(buf), "%zx %d", (size_t) 0x123456, 1);
448 ok(!strcmp(buf, "123456 1"), "buf = %s\n", buf);
452 START_TEST(printf)
454 if (!init()) return;
456 test_snprintf();
457 test_swprintf();
458 test_fprintf();
459 test_vsnprintf_s();
460 test_printf_legacy_wide();
461 test_printf_legacy_msvcrt();
462 test_printf_legacy_three_digit_exp();
463 test_printf_c99();