aboutsummaryrefslogtreecommitdiffstats
path: root/strtou8.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 /strtou8.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--strtou8.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/strtou8.c b/strtou8.c
index 1721d85..2f299b4 100644
--- a/strtou8.c
+++ b/strtou8.c
@@ -6,10 +6,10 @@
uint_least8_t
-libsimple_strtou8(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */
+libsimple_strtou8(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_strtou8(const char *restrict nptr, char **restrict end, int base) /* T
int
main(void)
{
+ char *e;
+ errno = 0;
+ assert(strtou8("0xFF", NULL, 0) == UINT8_C(0xFF) && !errno);
+ assert(strtou8("0xFF", NULL, 16) == UINT8_C(0xFF) && !errno);
+ assert(strtou8("FF", NULL, 16) == UINT8_C(0xFF) && !errno);
+ assert(strtou8("0xFF", NULL, 10) == 0 && !errno);
+ assert(strtou8("0xFF", &e, 0) == UINT8_C(0xFF) && !*e && !errno);
+ assert(strtou8("0xFF ", &e, 16) == UINT8_C(0xFF) && *e == ' ' && !errno);
+ assert(strtou8("0xFF", &e, 10) == 0 && *e == 'x' && !errno);
+ assert(strtou8("255", &e, 10) == UINT8_C(0xFF) && !*e && !errno);
+ assert(strtou8("12", &e, 10) == 12 && !*e && !errno);
+ assert(strtou8("12", &e, 8) == 012 && !*e && !errno);
+ assert(strtou8("012", &e, 0) == 012 && !*e && !errno);
+ assert(strtou8("256", &e, 10) == UINT8_C(255) && !*e && errno == ERANGE);
+ errno = 0;
+ assert(!strtou8("1", &e, -10000) && errno == EINVAL);
+ errno = 0;
return 0;
}