diff options
author | Mattias Andrée <maandree@kth.se> | 2018-08-26 14:31:09 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-08-26 14:31:09 +0200 |
commit | e59072005414970eda98dee2acf22487805e091c (patch) | |
tree | 6085a34dd8498063d4aa704b45fe8851ffe224b5 /timespec2timeval.c | |
parent | Fix aliases (diff) | |
download | libsimple-e59072005414970eda98dee2acf22487805e091c.tar.gz libsimple-e59072005414970eda98dee2acf22487805e091c.tar.bz2 libsimple-e59072005414970eda98dee2acf22487805e091c.tar.xz |
Tests and fixes
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'timespec2timeval.c')
-rw-r--r-- | timespec2timeval.c | 111 |
1 files changed, 95 insertions, 16 deletions
diff --git a/timespec2timeval.c b/timespec2timeval.c index 282f794..92c9b3e 100644 --- a/timespec2timeval.c +++ b/timespec2timeval.c @@ -1,23 +1,102 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int -libsimple_timespec2timeval(struct timeval *restrict tv, const struct timespec *restrict ts) /* TODO test */ +libsimple_timespec2timeval(struct timeval *restrict tv, const struct timespec *restrict ts) { - tv->tv_sec = ts->tv_sec; - tv->tv_usec = ts->tv_nsec / 1000L; - if ((ts->tv_nsec % 1000L) >= 500L) { - if (++(tv->tv_usec) == 1000000L) { - tv->tv_usec = 0; - if (tv->tv_sec == TIME_MAX) { - tv->tv_usec = 999999L; - errno = EOVERFLOW; - return -1; - } else { - tv->tv_sec += 1; - } - } - } - return 0; + tv->tv_sec = ts->tv_sec; + tv->tv_usec = ts->tv_nsec / 1000L; + if ((ts->tv_nsec % 1000L) >= 500L) { + if (++(tv->tv_usec) == 1000000L) { + tv->tv_usec = 0; + if (tv->tv_sec == TIME_MAX) { + tv->tv_usec = 999999L; + errno = EOVERFLOW; + return -1; + } else { + tv->tv_sec += 1; + } + } + } + return 0; } + + +#else +#include "test.h" + +int +main(void) +{ + struct timeval tv; + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){0, 0})); + assert(tv.tv_sec == 0); + assert(tv.tv_usec == 0); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 0})); + assert(tv.tv_sec == 100); + assert(tv.tv_usec == 0); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 100000000L})); + assert(tv.tv_sec == 100); + assert(tv.tv_usec == 100000L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 100000400L})); + assert(tv.tv_sec == 100); + assert(tv.tv_usec == 100000L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 100000500L})); + assert(tv.tv_sec == 100); + assert(tv.tv_usec == 100001L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 999999999L})); + assert(tv.tv_sec == 101); + assert(tv.tv_usec == 0L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 0})); + assert(tv.tv_sec == -100); + assert(tv.tv_usec == 0); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 100000000L})); + assert(tv.tv_sec == -100); + assert(tv.tv_usec == 100000L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 100000400L})); + assert(tv.tv_sec == -100); + assert(tv.tv_usec == 100000L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 100000500L})); + assert(tv.tv_sec == -100); + assert(tv.tv_usec == 100001L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 999999999L})); + assert(tv.tv_sec == -99); + assert(tv.tv_usec == 0L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MAX, 100000500L})); + assert(tv.tv_sec == TIME_MAX); + assert(tv.tv_usec == 100001L); + + assert(libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MAX, 999999500L}) == -1 && errno == EOVERFLOW); + assert(tv.tv_sec == TIME_MAX); + assert(tv.tv_usec == 999999L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MIN, 100000500L})); + assert(tv.tv_sec == TIME_MIN); + assert(tv.tv_usec == 100001L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MIN, 0L})); + assert(tv.tv_sec == TIME_MIN); + assert(tv.tv_usec == 0L); + + assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MIN, 500L})); + assert(tv.tv_sec == TIME_MIN); + assert(tv.tv_usec == 1L); + + return 0; +} + +#endif |