diff options
author | Mattias Andrée <maandree@kth.se> | 2023-04-09 20:05:12 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2023-04-09 20:05:12 +0200 |
commit | bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2 (patch) | |
tree | 1bdaf0b894cf933f7d96951a516b037cde2f32c1 /strtou32.c | |
parent | Improve makefile and fix a bug (diff) | |
download | libsimple-bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2.tar.gz libsimple-bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2.tar.bz2 libsimple-bbc81527ffbbb1cb26dfd145d492bdd613cd7ae2.tar.xz |
Add tests and man pages
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'strtou32.c')
-rw-r--r-- | strtou32.c | 21 |
1 files changed, 19 insertions, 2 deletions
@@ -6,10 +6,10 @@ uint_least32_t -libsimple_strtou32(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtou32(const char *restrict nptr, char **restrict end, int base) { uintmax_t r = strtoumax(nptr, end, base); - if(r > RET_MAX) { + if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -23,6 +23,23 @@ libsimple_strtou32(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtou32("0xFFFFFFFF", NULL, 0) == UINT32_C(0xFFFFFFFF) && !errno); + assert(strtou32("0xFFFFFFFF", NULL, 16) == UINT32_C(0xFFFFFFFF) && !errno); + assert(strtou32("FFFFFFFF", NULL, 16) == UINT32_C(0xFFFFFFFF) && !errno); + assert(strtou32("0xFFFFFFFF", NULL, 10) == 0 && !errno); + assert(strtou32("0xFFFFFFFF", &e, 0) == UINT32_C(0xFFFFFFFF) && !*e && !errno); + assert(strtou32("0xFFFFFFFF ", &e, 16) == UINT32_C(0xFFFFFFFF) && *e == ' ' && !errno); + assert(strtou32("0xFFFFFFFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtou32("4294967295", &e, 10) == UINT32_C(0xFFFFFFFF) && !*e && !errno); + assert(strtou32("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtou32("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtou32("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtou32("4294967296", &e, 10) == UINT32_C(4294967295) && !*e && errno == ERANGE); + errno = 0; + assert(!strtou32("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } |