Commit 7265cd17 authored by Martin Storsjo's avatar Martin Storsjo Committed by Alexandre Julliard

ucrtbase: Extend the printf tests even further.

parent 4358ddc7
...@@ -90,11 +90,13 @@ static int WINAPIV vsprintf_wrapper(unsigned __int64 options, char *str, ...@@ -90,11 +90,13 @@ 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;
for (j = 0; j < ARRAY_SIZE(bufsizes); j++) {
const int bufsiz = bufsizes[j];
/* Legacy _snprintf style termination */ /* Legacy _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];
...@@ -113,30 +115,31 @@ static void test_snprintf (void) ...@@ -113,30 +115,31 @@ static void test_snprintf (void)
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,
"Failure to snprintf to NULL\n"); "Failure to snprintf to NULL\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment