portabtest: Add checks for strdup(), strndup(), and strtok_r()

This commit is contained in:
Alexander Barton 2014-01-01 16:38:36 +01:00
parent d38747d951
commit f024a4992a
1 changed files with 57 additions and 0 deletions

View File

@ -44,6 +44,36 @@ Check_snprintf(void)
Panic("snprintf string length"); Panic("snprintf string length");
} }
static void
Check_strdup(void)
{
char *ptr;
ptr = strdup("1234567890");
if (!ptr)
Panic("strdup");
if (ptr[10] != '\0')
Panic("strdup NULL byte");
if (strlen(ptr) != 10)
Panic("strdup string length");
free(ptr);
}
static void
Check_strndup(void)
{
char *ptr;
ptr = strndup("1234567890", 5);
if (!ptr)
Panic("strndup");
if (ptr[5] != '\0')
Panic("strndup NULL byte");
if (strlen(ptr) != 5)
Panic("strndup string length");
free(ptr);
}
static void static void
Check_strlcpy(void) Check_strlcpy(void)
{ {
@ -72,6 +102,30 @@ Check_strlcat(void)
Panic("strlcat string length"); Panic("strlcat string length");
} }
static void
Check_strtok_r(void)
{
char *ptr, *last;
ptr = strdup("12,abc");
ptr = strtok_r(ptr, ",", &last);
if (!ptr)
Panic("strtok_r result #1");
if (strcmp(ptr, "12") != 0)
Panic("strtok_r token #1");
ptr = strtok_r(NULL, ",", &last);
if (!ptr)
Panic("strtok_r result #2");
if (strcmp(ptr, "abc") != 0)
Panic("strtok_r token #2");
ptr = strtok_r(NULL, ",", &last);
if (ptr)
Panic("strtok_r result #3");
}
#ifdef PROTOTYPES #ifdef PROTOTYPES
static void static void
Check_vsnprintf(const int Len, const char *Format, ...) Check_vsnprintf(const int Len, const char *Format, ...)
@ -118,8 +172,11 @@ main(void)
/* check functions */ /* check functions */
Check_snprintf(); Check_snprintf();
Check_strdup();
Check_strndup();
Check_strlcpy(); Check_strlcpy();
Check_strlcat(); Check_strlcat();
Check_strtok_r();
Check_vsnprintf(2+10, "%s%s", "ab", "1234567890"); Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
return 0; return 0;