aboutsummaryrefslogtreecommitdiffstats
path: root/strtoi8.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2023-04-09 20:05:12 +0200
committerMattias Andrée <maandree@kth.se>2023-04-09 20:05:12 +0200
commitbbc81527ffbbb1cb26dfd145d492bdd613cd7ae2 (patch)
tree1bdaf0b894cf933f7d96951a516b037cde2f32c1 /strtoi8.c
parentImprove makefile and fix a bug (diff)
downloadlibsimple-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--strtoi8.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/strtoi8.c b/strtoi8.c
index a5d59a3..01f783a 100644
--- a/strtoi8.c
+++ b/strtoi8.c
@@ -7,13 +7,13 @@
int_least8_t
-libsimple_strtoi8(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */
+libsimple_strtoi8(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_strtoi8(const char *restrict nptr, char **restrict end, int base) /* T
int
main(void)
{
+ char *e;
+ errno = 0;
+ assert(strtoi8("0x7F", NULL, 0) == INT8_C(0x7F) && !errno);
+ assert(strtoi8("0x7F", NULL, 16) == INT8_C(0x7F) && !errno);
+ assert(strtoi8("7F", NULL, 16) == INT8_C(0x7F) && !errno);
+ assert(strtoi8("0x7F", NULL, 10) == 0 && !errno);
+ assert(strtoi8("0x7F", &e, 0) == INT8_C(0x7F) && !*e && !errno);
+ assert(strtoi8("0x7F ", &e, 16) == INT8_C(0x7F) && *e == ' ' && !errno);
+ assert(strtoi8("0x7F", &e, 10) == 0 && *e == 'x' && !errno);
+ assert(strtoi8("127", &e, 10) == INT8_C(0x7F) && !*e && !errno);
+ assert(strtoi8("-127", &e, 10) == INT8_C(-127) && !*e && !errno);
+ assert(strtoi8("-128", &e, 10) == INT8_C(-128) && !*e && !errno);
+ assert(strtoi8("12", &e, 10) == 12 && !*e && !errno);
+ assert(strtoi8("12", &e, 8) == 012 && !*e && !errno);
+ assert(strtoi8("012", &e, 0) == 012 && !*e && !errno);
+ assert(strtoi8("128", &e, 10) == INT8_C(127) && !*e && errno == ERANGE);
+ errno = 0;
+ assert(strtoi8("-129", &e, 10) == INT8_C(-128) && !*e && errno == ERANGE);
+ errno = 0;
+ assert(!strtoi8("1", &e, -10000) && errno == EINVAL);
+ errno = 0;
return 0;
}