From bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 9 Apr 2023 20:05:12 +0200 Subject: Add tests and man pages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- strtouhh.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'strtouhh.c') diff --git a/strtouhh.c b/strtouhh.c index c6ec130..b163b7f 100644 --- a/strtouhh.c +++ b/strtouhh.c @@ -4,10 +4,10 @@ unsigned char -libsimple_strtouhh(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtouhh(const char *restrict nptr, char **restrict end, int base) { unsigned long int r = strtoul(nptr, end, base); - if(r > UCHAR_MAX) { + if (r > UCHAR_MAX) { r = UCHAR_MAX; errno = ERANGE; } @@ -18,9 +18,41 @@ libsimple_strtouhh(const char *restrict nptr, char **restrict end, int base) /* #else #include "test.h" +static void +add_one(char *buf) +{ + char *p = strchr(buf, '\0'); + while (*--p == '9') + *p = '0'; + *p += 1; +} + int main(void) { + char str[128]; + char *e; + sprintf(str, "0x%x", (unsigned int)UCHAR_MAX); + errno = 0; + assert(strtouhh(str, NULL, 0) == UCHAR_MAX && !errno); + assert(strtouhh(str, NULL, 16) == UCHAR_MAX && !errno); + assert(strtouhh(&str[2], NULL, 16) == UCHAR_MAX && !errno); + assert(strtouhh(str, NULL, 10) == 0 && !errno); + assert(strtouhh(str, &e, 0) == UCHAR_MAX && !*e && !errno); + assert(strtouhh(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%x ", (unsigned int)UCHAR_MAX); + assert(strtouhh(str, &e, 16) == UCHAR_MAX && *e == ' ' && !errno); + sprintf(str, "%u", (unsigned int)UCHAR_MAX); + assert(strtouhh(str, &e, 10) == UCHAR_MAX && !*e && !errno); + assert(strtouhh("12", &e, 10) == 12 && !*e && !errno); + assert(strtouhh("12", &e, 8) == 012 && !*e && !errno); + assert(strtouhh("012", &e, 0) == 012 && !*e && !errno); + sprintf(str, "%u", (unsigned int)UCHAR_MAX); + add_one(str); + assert(strtouhh(str, &e, 10) == UCHAR_MAX && !*e && errno == ERANGE); + errno = 0; + assert(!strtouhh("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } -- cgit v1.2.3-70-g09d2