diff options
| author | Alexander Barton <alex@barton.de> | 2014-01-01 16:38:36 +0100 |
|---|---|---|
| committer | Alexander Barton <alex@barton.de> | 2014-01-01 16:38:36 +0100 |
| commit | f024a4992a9a38d64d0fd63283cd0c484bdec683 (patch) | |
| tree | b85073c265e6ed13966f8dc080dfd0c66dfdc378 /src | |
| parent | d38747d951a8a5007e97693cade3551e11e50569 (diff) | |
| download | ngircd-f024a4992a9a38d64d0fd63283cd0c484bdec683.tar.gz ngircd-f024a4992a9a38d64d0fd63283cd0c484bdec683.zip | |
portabtest: Add checks for strdup(), strndup(), and strtok_r()
Diffstat (limited to 'src')
| -rw-r--r-- | src/portab/portabtest.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/portab/portabtest.c b/src/portab/portabtest.c index 4f53ce76..9e6bd228 100644 --- a/src/portab/portabtest.c +++ b/src/portab/portabtest.c @@ -45,6 +45,36 @@ Check_snprintf(void) } 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 Check_strlcpy(void) { char str[5]; @@ -72,6 +102,30 @@ Check_strlcat(void) 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 static void Check_vsnprintf(const int Len, const char *Format, ...) @@ -118,8 +172,11 @@ main(void) /* check functions */ Check_snprintf(); + Check_strdup(); + Check_strndup(); Check_strlcpy(); Check_strlcat(); + Check_strtok_r(); Check_vsnprintf(2+10, "%s%s", "ab", "1234567890"); return 0; |