ucrtbase: Simplify the snprintf/swprintf tests.
Avoid repeating the test strings, calculate the expected return value with code. Signed-off-by: Martin Storsjo <martin@martin.st> Signed-off-by: Piotr Caban <piotr@codeweavers.com> Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
parent
2a74c9a8ed
commit
4b47da3ad7
|
@ -82,27 +82,15 @@ static int __cdecl vsprintf_wrapper(unsigned __int64 options, char *str,
|
||||||
|
|
||||||
static void test_snprintf (void)
|
static void test_snprintf (void)
|
||||||
{
|
{
|
||||||
struct snprintf_test {
|
const char *tests[] = {"short", "justfit", "justfits", "muchlonger"};
|
||||||
const char *format;
|
|
||||||
int expected;
|
|
||||||
};
|
|
||||||
/* Legacy behaviour, not C99 compliant. */
|
|
||||||
const struct snprintf_test tests_legacy[] = {{"short", 5},
|
|
||||||
{"justfit", 7},
|
|
||||||
{"justfits", 8},
|
|
||||||
{"muchlonger", -1}};
|
|
||||||
/* New C99 compliant behaviour. */
|
|
||||||
const struct snprintf_test tests_standard[] = {{"short", 5},
|
|
||||||
{"justfit", 7},
|
|
||||||
{"justfits", 8},
|
|
||||||
{"muchlonger", 10}};
|
|
||||||
char buffer[8];
|
char buffer[8];
|
||||||
const int bufsiz = sizeof buffer;
|
const int bufsiz = sizeof buffer;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < sizeof tests_legacy / sizeof tests_legacy[0]; i++) {
|
/* Legacy _snprintf style termination */
|
||||||
const char *fmt = tests_legacy[i].format;
|
for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
|
||||||
const int expect = tests_legacy[i].expected;
|
const char *fmt = tests[i];
|
||||||
|
const int expect = strlen(fmt) > bufsiz ? -1 : strlen(fmt);
|
||||||
const int n = vsprintf_wrapper (1, buffer, bufsiz, fmt);
|
const int n = vsprintf_wrapper (1, buffer, bufsiz, fmt);
|
||||||
const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
|
const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
|
||||||
|
|
||||||
|
@ -112,9 +100,10 @@ static void test_snprintf (void)
|
||||||
"\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
|
"\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < sizeof tests_standard / sizeof tests_standard[0]; i++) {
|
/* C99 snprintf style termination */
|
||||||
const char *fmt = tests_standard[i].format;
|
for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
|
||||||
const int expect = tests_standard[i].expected;
|
const char *fmt = tests[i];
|
||||||
|
const int expect = strlen(fmt);
|
||||||
const int n = vsprintf_wrapper (2, buffer, bufsiz, fmt);
|
const int n = vsprintf_wrapper (2, buffer, bufsiz, fmt);
|
||||||
const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
|
const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
|
||||||
|
|
||||||
|
@ -137,34 +126,24 @@ static int __cdecl vswprintf_wrapper(unsigned __int64 options, wchar_t *str,
|
||||||
__ms_va_end(valist);
|
__ms_va_end(valist);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_swprintf (void)
|
static void test_swprintf (void)
|
||||||
{
|
{
|
||||||
struct swprintf_test {
|
|
||||||
const wchar_t *format;
|
|
||||||
int expected;
|
|
||||||
};
|
|
||||||
const wchar_t str_short[] = {'s','h','o','r','t',0};
|
const wchar_t str_short[] = {'s','h','o','r','t',0};
|
||||||
const wchar_t str_justfit[] = {'j','u','s','t','f','i','t',0};
|
const wchar_t str_justfit[] = {'j','u','s','t','f','i','t',0};
|
||||||
const wchar_t str_justfits[] = {'j','u','s','t','f','i','t','s',0};
|
const wchar_t str_justfits[] = {'j','u','s','t','f','i','t','s',0};
|
||||||
const wchar_t str_muchlonger[] = {'m','u','c','h','l','o','n','g','e','r',0};
|
const wchar_t str_muchlonger[] = {'m','u','c','h','l','o','n','g','e','r',0};
|
||||||
/* Legacy behaviour, not C99 compliant. */
|
const wchar_t *tests[] = {str_short, str_justfit, str_justfits, str_muchlonger};
|
||||||
const struct swprintf_test tests_legacy[] = {{str_short, 5},
|
|
||||||
{str_justfit, 7},
|
|
||||||
{str_justfits, 8},
|
|
||||||
{str_muchlonger, -1}};
|
|
||||||
/* New C99 compliant behaviour. */
|
|
||||||
const struct swprintf_test tests_standard[] = {{str_short, 5},
|
|
||||||
{str_justfit, 7},
|
|
||||||
{str_justfits, 8},
|
|
||||||
{str_muchlonger, 10}};
|
|
||||||
wchar_t buffer[8];
|
wchar_t buffer[8];
|
||||||
char narrow[8], narrow_fmt[16];
|
char narrow[8], narrow_fmt[16];
|
||||||
const int bufsiz = sizeof buffer / sizeof buffer[0];
|
const int bufsiz = sizeof buffer / sizeof buffer[0];
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < sizeof tests_legacy / sizeof tests_legacy[0]; i++) {
|
/* Legacy _snprintf style termination */
|
||||||
const wchar_t *fmt = tests_legacy[i].format;
|
for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
|
||||||
const int expect = tests_legacy[i].expected;
|
const wchar_t *fmt = tests[i];
|
||||||
|
const int expect = wcslen(fmt) > bufsiz ? -1 : wcslen(fmt);
|
||||||
const int n = vswprintf_wrapper (1, buffer, bufsiz, fmt);
|
const int n = vswprintf_wrapper (1, buffer, bufsiz, fmt);
|
||||||
const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
|
const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
|
||||||
|
|
||||||
|
@ -176,9 +155,10 @@ static void test_swprintf (void)
|
||||||
"\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
|
"\"%s\": rendered \"%.*s\"\n", narrow_fmt, valid, narrow);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < sizeof tests_standard / sizeof tests_standard[0]; i++) {
|
/* C99 snprintf style termination */
|
||||||
const wchar_t *fmt = tests_standard[i].format;
|
for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
|
||||||
const int expect = tests_standard[i].expected;
|
const wchar_t *fmt = tests[i];
|
||||||
|
const int expect = wcslen(fmt);
|
||||||
const int n = vswprintf_wrapper (2, buffer, bufsiz, fmt);
|
const int n = vswprintf_wrapper (2, buffer, bufsiz, fmt);
|
||||||
const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
|
const int valid = n >= bufsiz ? bufsiz - 1 : n < 0 ? 0 : n;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue