test/signal-logging: simplify tests using sprintf
Ever looked at your own code and thought 'WTF was I thinking?'. yeah, that. Instead of passing in the expected string just use sprintf to print the number for us and compare. In the end we're just trying to emulate printf behaviour anyway. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Keith Packard <keithp@keithp.com>
This commit is contained in:
parent
fa6ab7d9b2
commit
f53b2012f3
|
@ -42,14 +42,16 @@ struct signed_number_format_test {
|
||||||
};
|
};
|
||||||
|
|
||||||
static Bool
|
static Bool
|
||||||
check_signed_number_format_test(const struct signed_number_format_test *test)
|
check_signed_number_format_test(long int number)
|
||||||
{
|
{
|
||||||
char string[21];
|
char string[21];
|
||||||
|
char expected[21];
|
||||||
|
|
||||||
FormatInt64(test->number, string);
|
sprintf(expected, "%ld", number);
|
||||||
if(strncmp(string, test->string, 21) != 0) {
|
FormatInt64(number, string);
|
||||||
fprintf(stderr, "Failed to convert %jd to decimal string (%s vs %s)\n",
|
if(strncmp(string, expected, 21) != 0) {
|
||||||
test->number, test->string, string);
|
fprintf(stderr, "Failed to convert %jd to decimal string (expected %s but got %s)\n",
|
||||||
|
number, expected, string);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,21 +59,25 @@ check_signed_number_format_test(const struct signed_number_format_test *test)
|
||||||
}
|
}
|
||||||
|
|
||||||
static Bool
|
static Bool
|
||||||
check_number_format_test(const struct number_format_test *test)
|
check_number_format_test(long unsigned int number)
|
||||||
{
|
{
|
||||||
char string[21];
|
char string[21];
|
||||||
|
char expected[21];
|
||||||
|
|
||||||
FormatUInt64(test->number, string);
|
sprintf(expected, "%lu", number);
|
||||||
if(strncmp(string, test->string, 21) != 0) {
|
|
||||||
|
FormatUInt64(number, string);
|
||||||
|
if(strncmp(string, expected, 21) != 0) {
|
||||||
fprintf(stderr, "Failed to convert %ju to decimal string (%s vs %s)\n",
|
fprintf(stderr, "Failed to convert %ju to decimal string (%s vs %s)\n",
|
||||||
test->number, test->string, string);
|
number, expected, string);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
FormatUInt64Hex(test->number, string);
|
|
||||||
if(strncmp(string, test->hex_string, 17) != 0) {
|
sprintf(expected, "%lx", number);
|
||||||
fprintf(stderr,
|
FormatUInt64Hex(number, string);
|
||||||
"Failed to convert %ju to hexadecimal string (%s vs %s)\n",
|
if(strncmp(string, expected, 17) != 0) {
|
||||||
test->number, test->hex_string, string);
|
fprintf(stderr, "Failed to convert %ju to hexadecimal string (%s vs %s)\n",
|
||||||
|
number, expected, string);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,100 +88,34 @@ static void
|
||||||
number_formatting(void)
|
number_formatting(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct number_format_test unsigned_tests[] = {
|
long unsigned int unsigned_tests[] = { 0,/* Zero */
|
||||||
{ /* Zero */
|
5, /* Single digit number */
|
||||||
0,
|
12, /* Two digit decimal number */
|
||||||
"0",
|
37, /* Two digit hex number */
|
||||||
"0",
|
0xC90B2, /* Large < 32 bit number */
|
||||||
},
|
0x15D027BF211B37A, /* Large > 32 bit number */
|
||||||
{ /* Single digit number */
|
0xFFFFFFFFFFFFFFFF, /* Maximum 64-bit number */
|
||||||
5,
|
|
||||||
"5",
|
|
||||||
"5",
|
|
||||||
},
|
|
||||||
{ /* Two digit decimal number */
|
|
||||||
12,
|
|
||||||
"12",
|
|
||||||
"c",
|
|
||||||
},
|
|
||||||
{ /* Two digit hex number */
|
|
||||||
37,
|
|
||||||
"37",
|
|
||||||
"25",
|
|
||||||
},
|
|
||||||
{ /* Large < 32 bit number */
|
|
||||||
0xC90B2,
|
|
||||||
"823474",
|
|
||||||
"c90b2",
|
|
||||||
},
|
|
||||||
{ /* Large > 32 bit number */
|
|
||||||
0x15D027BF211B37A,
|
|
||||||
"98237498237498234",
|
|
||||||
"15d027bf211b37a",
|
|
||||||
},
|
|
||||||
{ /* Maximum 64-bit number */
|
|
||||||
0xFFFFFFFFFFFFFFFF,
|
|
||||||
"18446744073709551615",
|
|
||||||
"ffffffffffffffff",
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct signed_number_format_test signed_tests[] = {
|
long int signed_tests[] = { 0,/* Zero */
|
||||||
{ /* Zero */
|
5, /* Single digit number */
|
||||||
0,
|
12, /* Two digit decimal number */
|
||||||
"0",
|
37, /* Two digit hex number */
|
||||||
},
|
0xC90B2, /* Large < 32 bit number */
|
||||||
{ /* Single digit number */
|
0x15D027BF211B37A, /* Large > 32 bit number */
|
||||||
5,
|
0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
|
||||||
"5",
|
-1, /* Single digit number */
|
||||||
},
|
-12, /* Two digit decimal number */
|
||||||
{ /* Two digit decimal number */
|
-0xC90B2, /* Large < 32 bit number */
|
||||||
12,
|
-0x15D027BF211B37A, /* Large > 32 bit number */
|
||||||
"12",
|
-0x7FFFFFFFFFFFFFFF, /* Maximum 64-bit signed number */
|
||||||
},
|
} ;
|
||||||
{ /* Two digit hex number */
|
|
||||||
37,
|
|
||||||
"37",
|
|
||||||
},
|
|
||||||
{ /* Large < 32 bit number */
|
|
||||||
0xC90B2,
|
|
||||||
"823474",
|
|
||||||
},
|
|
||||||
{ /* Large > 32 bit number */
|
|
||||||
0x15D027BF211B37A,
|
|
||||||
"98237498237498234",
|
|
||||||
},
|
|
||||||
{ /* Maximum 64-bit signed number */
|
|
||||||
0x7FFFFFFFFFFFFFFF,
|
|
||||||
"9223372036854775807",
|
|
||||||
},
|
|
||||||
{ /* Single digit number */
|
|
||||||
-1,
|
|
||||||
"-1",
|
|
||||||
},
|
|
||||||
{ /* Two digit decimal number */
|
|
||||||
-12,
|
|
||||||
"-12",
|
|
||||||
},
|
|
||||||
{ /* Large < 32 bit number */
|
|
||||||
-0xC90B2,
|
|
||||||
"-823474",
|
|
||||||
},
|
|
||||||
{ /* Large > 32 bit number */
|
|
||||||
-0x15D027BF211B37A,
|
|
||||||
"-98237498237498234",
|
|
||||||
},
|
|
||||||
{ /* Maximum 64-bit number */
|
|
||||||
-0x7FFFFFFFFFFFFFFF,
|
|
||||||
"-9223372036854775807",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
for (i = 0; i < sizeof(unsigned_tests) / sizeof(unsigned_tests[0]); i++)
|
for (i = 0; i < sizeof(unsigned_tests) / sizeof(unsigned_tests[0]); i++)
|
||||||
assert(check_number_format_test(unsigned_tests + i));
|
assert(check_number_format_test(unsigned_tests[i]));
|
||||||
|
|
||||||
for (i = 0; i < sizeof(unsigned_tests) / sizeof(signed_tests[0]); i++)
|
for (i = 0; i < sizeof(unsigned_tests) / sizeof(signed_tests[0]); i++)
|
||||||
assert(check_signed_number_format_test(signed_tests + i));
|
assert(check_signed_number_format_test(signed_tests[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma GCC diagnostic ignored "-Wformat-security"
|
#pragma GCC diagnostic ignored "-Wformat-security"
|
||||||
|
|
Loading…
Reference in New Issue