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 /strtoi16.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 'strtoi16.c')
-rw-r--r-- | strtoi16.c | 27 |
1 files changed, 24 insertions, 3 deletions
@@ -7,13 +7,13 @@ int_least16_t -libsimple_strtoi16(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */ +libsimple_strtoi16(const char *restrict nptr, char **restrict end, int base) { intmax_t r = strtoimax(nptr, end, base); - if(r < RET_MIN) { + if (r < RET_MIN) { r = RET_MIN; errno = ERANGE; - } else if(r > RET_MAX) { + } else if (r > RET_MAX) { r = RET_MAX; errno = ERANGE; } @@ -27,6 +27,27 @@ libsimple_strtoi16(const char *restrict nptr, char **restrict end, int base) /* int main(void) { + char *e; + errno = 0; + assert(strtoi16("0x7FFF", NULL, 0) == INT16_C(0x7FFF) && !errno); + assert(strtoi16("0x7FFF", NULL, 16) == INT16_C(0x7FFF) && !errno); + assert(strtoi16("7FFF", NULL, 16) == INT16_C(0x7FFF) && !errno); + assert(strtoi16("0x7FFF", NULL, 10) == 0 && !errno); + assert(strtoi16("0x7FFF", &e, 0) == INT16_C(0x7FFF) && !*e && !errno); + assert(strtoi16("0x7FFF ", &e, 16) == INT16_C(0x7FFF) && *e == ' ' && !errno); + assert(strtoi16("0x7FFF", &e, 10) == 0 && *e == 'x' && !errno); + assert(strtoi16("32767", &e, 10) == INT16_C(0x7FFF) && !*e && !errno); + assert(strtoi16("-32767", &e, 10) == INT16_C(-32767) && !*e && !errno); + assert(strtoi16("-32768", &e, 10) == INT16_C(-32768) && !*e && !errno); + assert(strtoi16("1234", &e, 10) == 1234 && !*e && !errno); + assert(strtoi16("1234", &e, 8) == 01234 && !*e && !errno); + assert(strtoi16("01234", &e, 0) == 01234 && !*e && !errno); + assert(strtoi16("32768", &e, 10) == INT16_C(32767) && !*e && errno == ERANGE); + errno = 0; + assert(strtoi16("-32769", &e, 10) == INT16_C(-32768) && !*e && errno == ERANGE); + errno = 0; + assert(!strtoi16("1", &e, -10000) && errno == EINVAL); + errno = 0; return 0; } |