From e59072005414970eda98dee2acf22487805e091c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 26 Aug 2018 14:31:09 +0200 Subject: Tests and fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- timespec2timeval.c | 111 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 16 deletions(-) (limited to 'timespec2timeval.c') 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 -- cgit v1.2.3-70-g09d2