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 /strtoz.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 '')
-rw-r--r-- | strtoz.c | 45 |
1 files changed, 44 insertions, 1 deletions
@@ -3,15 +3,58 @@ #ifndef TEST -extern inline ssize_t libsimple_strtoz(const char *restrict, char **restrict, int); /* TODO test, man */ +extern inline ssize_t libsimple_strtoz(const char *restrict, char **restrict, int); #else #include "test.h" +#ifndef SSIZE_MIN +#define SSIZE_MIN (-SSIZE_MAX - (-LONG_MAX != LONG_MIN)) +#endif + +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%zx", SSIZE_MAX); + errno = 0; + assert(strtoz(str, NULL, 0) == SSIZE_MAX && !errno); + assert(strtoz(str, NULL, 16) == SSIZE_MAX && !errno); + assert(strtoz(&str[2], NULL, 16) == SSIZE_MAX && !errno); + assert(strtoz(str, NULL, 10) == 0 && !errno); + assert(strtoz(str, &e, 0) == SSIZE_MAX && !*e && !errno); + assert(strtoz(str, &e, 10) == 0 && *e == 'x' && !errno); + sprintf(str, "0x%zx ", SSIZE_MAX); + assert(strtoz(str, &e, 16) == SSIZE_MAX && *e == ' ' && !errno); + sprintf(str, "-%zi", SSIZE_MAX); + assert(strtoz(&str[1], &e, 10) == SSIZE_MAX && !*e && !errno); + assert(strtoz(str, &e, 10) == -SSIZE_MAX && !*e && !errno); + sprintf(str, "%zi", SSIZE_MIN); + assert(strtoz(str, &e, 10) == SSIZE_MIN && !*e && !errno); + assert(strtoz("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoz("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoz("01234", &e, 0) == 01234 && !*e && !errno); + sprintf(str, "%zi", SSIZE_MAX); + add_one(str); + assert(strtoz(str, &e, 10) == SSIZE_MAX && !*e && errno == ERANGE); + errno = 0; + sprintf(str, "%zi", SSIZE_MIN); + add_one(str); + assert(strtoz(str, &e, 10) == SSIZE_MIN && !*e && errno == ERANGE); + errno = 0; + assert(!strtoz("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } |