vsnprintf: Added null string unit test.

This commit is contained in:
Branimir Karadžić
2017-02-08 22:44:37 -08:00
parent db48bee874
commit f35926dbfa
3 changed files with 18 additions and 4 deletions

View File

@@ -166,7 +166,11 @@ namespace bx
size += writeRep(_writer, _param.fill, padding, _err);
}
if (_param.upper)
if (NULL == _str)
{
size += write(_writer, "(null)", 6, _err);
}
else if (_param.upper)
{
for (int32_t ii = 0; ii < len; ++ii)
{

View File

@@ -107,8 +107,13 @@ namespace bx
size_t strnlen(const char* _str, size_t _max)
{
const char* ptr;
for (ptr = _str; 0 < _max && *ptr != '\0'; ++ptr, --_max) {};
if (NULL == _str)
{
return 0;
}
const char* ptr = _str;
for (; 0 < _max && *ptr != '\0'; ++ptr, --_max) {};
return ptr - _str;
}

View File

@@ -38,7 +38,7 @@ static bool test(const char* _expected, const char* _format, ...)
if (!result)
{
printf("result (%d) %s, expected (%d) %s\n", len, temp, max-1, _expected);
printf("result (%d) '%s', expected (%d) '%s'\n", len, temp, max-1, _expected);
}
return result;
@@ -89,6 +89,11 @@ TEST_CASE("vsnprintf p", "")
REQUIRE(test("0xbadc0de ", "%-20p", (void*)0xbadc0de) );
}
TEST_CASE("vsnprintf s", "")
{
REQUIRE(test("(null)", "%s", NULL) );
}
TEST_CASE("vsnprintf", "")
{
REQUIRE(test("x", "%c", 'x') );