ucrtbase: Extend the printf tests even further.

Signed-off-by: Martin Storsjo <martin@martin.st>
Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
feature/deterministic
Martin Storsjo 2020-05-20 00:44:14 +03:00 committed by Alexandre Julliard
parent 4358ddc75f
commit 7265cd17b5
1 changed files with 42 additions and 39 deletions

View File

@ -90,52 +90,55 @@ static int WINAPIV vsprintf_wrapper(unsigned __int64 options, char *str,
static void test_snprintf (void) static void test_snprintf (void)
{ {
const char *tests[] = {"short", "justfit", "justfits", "muchlonger"}; const char *tests[] = {"short", "justfit", "justfits", "muchlonger", "", "1"};
char buffer[8]; char buffer[8];
const int bufsiz = sizeof buffer; int bufsizes[] = { 0, 1, sizeof(buffer) };
unsigned int i; unsigned int i, j;
/* Legacy _snprintf style termination */ for (j = 0; j < ARRAY_SIZE(bufsizes); j++) {
for (i = 0; i < ARRAY_SIZE(tests); i++) { const int bufsiz = bufsizes[j];
const char *fmt = tests[i]; /* Legacy _snprintf style termination */
const int expect = strlen(fmt) > bufsiz ? -1 : strlen(fmt); for (i = 0; i < ARRAY_SIZE(tests); i++) {
const int n = vsprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, bufsiz, fmt); const char *fmt = tests[i];
const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1); const int expect = strlen(fmt) > bufsiz ? -1 : strlen(fmt);
const int n = vsprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, bufsiz, fmt);
const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
ok (n == expect, "\"%s\": expected %d, returned %d\n", ok (n == expect, "\"%s\": expected %d, returned %d\n",
fmt, expect, n); fmt, expect, n);
ok (!memcmp (fmt, buffer, valid), ok (!memcmp (fmt, buffer, valid),
"\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer); "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
} }
/* C99 snprintf style termination */ /* C99 snprintf style termination */
for (i = 0; i < ARRAY_SIZE(tests); i++) { for (i = 0; i < ARRAY_SIZE(tests); i++) {
const char *fmt = tests[i]; const char *fmt = tests[i];
const int expect = strlen(fmt); const int expect = strlen(fmt);
const int n = vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, bufsiz, fmt); const int n = vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, bufsiz, fmt);
const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n; const int valid = n >= bufsiz ? (bufsiz > 0 ? bufsiz - 1 : 0) : n < 0 ? 0 : n;
ok (n == expect, "\"%s\": expected %d, returned %d\n", ok (n == expect, "\"%s\": expected %d, returned %d\n",
fmt, expect, n); fmt, expect, n);
ok (!memcmp (fmt, buffer, valid), ok (!memcmp (fmt, buffer, valid),
"\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer); "\"%s\": rendered \"%.*s\" bufsiz %d\n", fmt, valid, buffer, bufsiz);
ok (buffer[valid] == '\0', ok (bufsiz == 0 || buffer[valid] == '\0',
"\"%s\": Missing null termination (ret %d) - is %d\n", fmt, n, buffer[valid]); "\"%s\": Missing null termination (ret %d) - is %d (bufsiz %d)\n", fmt, n, buffer[valid], bufsiz);
} }
/* swprintf style termination */ /* swprintf style termination */
for (i = 0; i < ARRAY_SIZE(tests); i++) { for (i = 0; i < ARRAY_SIZE(tests); i++) {
const char *fmt = tests[i]; const char *fmt = tests[i];
const int expect = strlen(fmt) >= bufsiz ? -2 : strlen(fmt); const int expect = strlen(fmt) >= bufsiz ? bufsiz > 0 ? -2 : -1 : strlen(fmt);
const int n = vsprintf_wrapper (0, buffer, bufsiz, fmt); const int n = vsprintf_wrapper (0, buffer, bufsiz, fmt);
const int valid = n < 0 ? bufsiz - 1 : n; const int valid = n < 0 ? bufsiz > 0 ? bufsiz - 1 : 0 : n;
ok (n == expect, "\"%s\": expected %d, returned %d\n", ok (n == expect, "\"%s\": expected %d, returned %d\n",
fmt, expect, n); fmt, expect, n);
ok (!memcmp (fmt, buffer, valid), ok (!memcmp (fmt, buffer, valid),
"\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer); "\"%s\": rendered \"%.*s\" bufsiz %d\n", fmt, valid, buffer, bufsiz);
ok (buffer[valid] == '\0', ok (bufsiz == 0 || buffer[valid] == '\0',
"\"%s\": Missing null termination (ret %d) - is %d\n", fmt, n, buffer[valid]); "\"%s\": Missing null termination (ret %d) - is %d\n", fmt, n, buffer[valid]);
}
} }
ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, NULL, 0, "abcd") == 4, ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, NULL, 0, "abcd") == 4,