aboutsummaryrefslogtreecommitdiffstats
path: root/strtou64.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 /strtou64.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--strtou64.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/strtou64.c b/strtou64.c
index b13ee1d..a9ce787 100644
--- a/strtou64.c
+++ b/strtou64.c
@@ -6,11 +6,11 @@
uint_least64_t
-libsimple_strtou64(const char *restrict nptr, char **restrict end, int base) /* TODO test, man */
+libsimple_strtou64(const char *restrict nptr, char **restrict end, int base)
{
uintmax_t r = strtoumax(nptr, end, base);
#if UINTMAX_MAX > RET_MAX
- if(r > RET_MAX) {
+ if (r > RET_MAX) {
r = RET_MAX;
errno = ERANGE;
}
@@ -25,6 +25,23 @@ libsimple_strtou64(const char *restrict nptr, char **restrict end, int base) /*
int
main(void)
{
+ char *e;
+ errno = 0;
+ assert(strtou64("0xFFFFFFFFFFFFFFFF", NULL, 0) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !errno);
+ assert(strtou64("0xFFFFFFFFFFFFFFFF", NULL, 16) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !errno);
+ assert(strtou64("FFFFFFFFFFFFFFFF", NULL, 16) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !errno);
+ assert(strtou64("0xFFFFFFFFFFFFFFFF", NULL, 10) == 0 && !errno);
+ assert(strtou64("0xFFFFFFFFFFFFFFFF", &e, 0) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !*e && !errno);
+ assert(strtou64("0xFFFFFFFFFFFFFFFF ", &e, 16) == UINT64_C(0xFFFFFFFFFFFFFFFF) && *e == ' ' && !errno);
+ assert(strtou64("0xFFFFFFFFFFFFFFFF", &e, 10) == 0 && *e == 'x' && !errno);
+ assert(strtou64("18446744073709551615", &e, 10) == UINT64_C(0xFFFFFFFFFFFFFFFF) && !*e && !errno);
+ assert(strtou64("1234", &e, 10) == 1234 && !*e && !errno);
+ assert(strtou64("1234", &e, 8) == 01234 && !*e && !errno);
+ assert(strtou64("01234", &e, 0) == 01234 && !*e && !errno);
+ assert(strtou64("18446744073709551616", &e, 10) == UINT64_C(18446744073709551615) && !*e && errno == ERANGE);
+ errno = 0;
+ assert(!strtou64("1", &e, -10000) && errno == EINVAL);
+ errno = 0;
return 0;
}