diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | libsimple.c | 23 | ||||
-rw-r--r-- | libsimple.h | 2 | ||||
-rw-r--r-- | sumtimespec.c | 79 | ||||
-rw-r--r-- | sumtimeval.c | 77 | ||||
-rw-r--r-- | timespec2timeval.c | 111 |
6 files changed, 275 insertions, 20 deletions
@@ -78,6 +78,9 @@ TESTS =\ strrcasestr.test\ strrstr.test\ strstarts.test\ + sumtimespec.test\ + sumtimeval.test\ + timespec2timeval.test\ timespectostr.test\ timevaltostr.test\ vasprintf.test\ diff --git a/libsimple.c b/libsimple.c index 15326d1..df01ea1 100644 --- a/libsimple.c +++ b/libsimple.c @@ -96,6 +96,7 @@ test_timeval(double d, time_t sec, long int usec, double rd, const char *s, cons int main(void) { + struct timespec ts; const char *cs; char buf[1024], *s; int intarray[10]; @@ -757,6 +758,28 @@ main(void) assert(test_timeval(0, 0, 0, 0, "+0.000000", "0")); assert(test_timeval(-10, -10, 0, -10, "-10.000000", "-10")); + libsimple_timeval2timespec(&ts, &(struct timeval){0, 0L}); + assert(ts.tv_sec == 0); + assert(ts.tv_nsec == 0L); + libsimple_timeval2timespec(&ts, &(struct timeval){0, 1L}); + assert(ts.tv_sec == 0); + assert(ts.tv_nsec == 1000L); + libsimple_timeval2timespec(&ts, &(struct timeval){0, 999999L}); + assert(ts.tv_sec == 0); + assert(ts.tv_nsec == 999999000L); + libsimple_timeval2timespec(&ts, &(struct timeval){10, 0L}); + assert(ts.tv_sec == 10); + assert(ts.tv_nsec == 0L); + libsimple_timeval2timespec(&ts, &(struct timeval){10, 1L}); + assert(ts.tv_sec == 10); + assert(ts.tv_nsec == 1000L); + libsimple_timeval2timespec(&ts, &(struct timeval){-10, 0L}); + assert(ts.tv_sec == -10); + assert(ts.tv_nsec == 0L); + libsimple_timeval2timespec(&ts, &(struct timeval){-10, 1L}); + assert(ts.tv_sec == -10); + assert(ts.tv_nsec == 1000L); + return 0; } diff --git a/libsimple.h b/libsimple.h index 298c1d9..e3064e3 100644 --- a/libsimple.h +++ b/libsimple.h @@ -1454,7 +1454,7 @@ libsimple_cmptimeval(const struct timeval *__a, const struct timeval *__b) /* TO _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline void -libsimple_timeval2timespec(struct timespec *restrict __ts, const struct timeval *restrict __tv) /* TODO test */ +libsimple_timeval2timespec(struct timespec *restrict __ts, const struct timeval *restrict __tv) { __ts->tv_sec = __tv->tv_sec; __ts->tv_nsec = __tv->tv_usec; diff --git a/sumtimespec.c b/sumtimespec.c index c4bf68c..a359971 100644 --- a/sumtimespec.c +++ b/sumtimespec.c @@ -1,9 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int -libsimple_sumtimespec(struct timespec *sum, const struct timespec *augend, const struct timespec *addend) /* TODO test */ +libsimple_sumtimespec(struct timespec *sum, const struct timespec *augend, const struct timespec *addend) { long int ns = augend->tv_nsec + addend->tv_nsec; time_t s; @@ -16,7 +17,7 @@ libsimple_sumtimespec(struct timespec *sum, const struct timespec *augend, const ns = 999999999L; errno = ERANGE; ret = -1; - } else if (augend->tv_sec < 0 && augend->tv_sec + addend->tv_sec < TIME_MIN) { + } else if (augend->tv_sec < 0 && augend->tv_sec < TIME_MIN - addend->tv_sec) { s = TIME_MIN; ns = 0; errno = ERANGE; @@ -48,3 +49,77 @@ libsimple_sumtimespec(struct timespec *sum, const struct timespec *augend, const sum->tv_nsec = ns; return ret; } + + +#else +#include "test.h" + +int +main(void) +{ + struct timespec r, a, b; + + a.tv_sec = 0, a.tv_nsec = 0; + b.tv_sec = 0, b.tv_nsec = 0; + assert(!libsimple_sumtimespec(&r, &a, &b)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0); + + a.tv_sec = 10, a.tv_nsec = 10000L; + b.tv_sec = 0, b.tv_nsec = 0L; + assert(!libsimple_sumtimespec(&r, &a, &b)); + assert(r.tv_sec == 10); + assert(r.tv_nsec == 10000L); + + a.tv_sec = 10, a.tv_nsec = 10000L; + b.tv_sec = -10, b.tv_nsec = 0L; + assert(!libsimple_sumtimespec(&r, &a, &b)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 10000L); + + a.tv_sec = 10, a.tv_nsec = 10000L; + b.tv_sec = -20, b.tv_nsec = 20000L; + assert(!libsimple_sumtimespec(&r, &a, &b)); + assert(r.tv_sec == -10); + assert(r.tv_nsec == 30000L); + + a.tv_sec = 1, a.tv_nsec = 999999999L; + b.tv_sec = 1, b.tv_nsec = 1L; + assert(!libsimple_sumtimespec(&r, &a, &b)); + assert(r.tv_sec == 3); + assert(r.tv_nsec == 0L); + + a.tv_sec = TIME_MAX, a.tv_nsec = 0L; + b.tv_sec = TIME_MAX, b.tv_nsec = 0L; + assert(libsimple_sumtimespec(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_nsec == 999999999L); + + a.tv_sec = TIME_MAX, a.tv_nsec = 999999999L; + b.tv_sec = 0, b.tv_nsec = 1L; + assert(libsimple_sumtimespec(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_nsec == 999999999L); + + a.tv_sec = TIME_MAX, a.tv_nsec = 1L; + b.tv_sec = 0, b.tv_nsec = 999999999L; + assert(libsimple_sumtimespec(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_nsec == 999999999L); + + a.tv_sec = TIME_MIN, a.tv_nsec = 0L; + b.tv_sec = TIME_MIN, b.tv_nsec = 0L; + assert(libsimple_sumtimespec(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MIN); + assert(r.tv_nsec == 0L); + + a.tv_sec = TIME_MIN, a.tv_nsec = 100L; + b.tv_sec = TIME_MIN, b.tv_nsec = 100L; + assert(libsimple_sumtimespec(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MIN); + assert(r.tv_nsec == 0L); + + return 0; +} + +#endif diff --git a/sumtimeval.c b/sumtimeval.c index 409500d..b4e80bb 100644 --- a/sumtimeval.c +++ b/sumtimeval.c @@ -1,9 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int -libsimple_sumtimeval(struct timeval *sum, const struct timeval *augend, const struct timeval *addend) /* TODO test */ +libsimple_sumtimeval(struct timeval *sum, const struct timeval *augend, const struct timeval *addend) { struct timespec a, b, s; int r; @@ -14,3 +15,77 @@ libsimple_sumtimeval(struct timeval *sum, const struct timeval *augend, const st return r; return r | libsimple_timespec2timeval(sum, &s); } + + +#else +#include "test.h" + +int +main(void) +{ + struct timeval r, a, b; + + a.tv_sec = 0, a.tv_usec = 0; + b.tv_sec = 0, b.tv_usec = 0; + assert(!libsimple_sumtimeval(&r, &a, &b)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0); + + a.tv_sec = 10, a.tv_usec = 10000L; + b.tv_sec = 0, b.tv_usec = 0L; + assert(!libsimple_sumtimeval(&r, &a, &b)); + assert(r.tv_sec == 10); + assert(r.tv_usec == 10000L); + + a.tv_sec = 10, a.tv_usec = 10000L; + b.tv_sec = -10, b.tv_usec = 0L; + assert(!libsimple_sumtimeval(&r, &a, &b)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 10000L); + + a.tv_sec = 10, a.tv_usec = 10000L; + b.tv_sec = -20, b.tv_usec = 20000L; + assert(!libsimple_sumtimeval(&r, &a, &b)); + assert(r.tv_sec == -10); + assert(r.tv_usec == 30000L); + + a.tv_sec = 1, a.tv_usec = 999999L; + b.tv_sec = 1, b.tv_usec = 1L; + assert(!libsimple_sumtimeval(&r, &a, &b)); + assert(r.tv_sec == 3); + assert(r.tv_usec == 0L); + + a.tv_sec = TIME_MAX, a.tv_usec = 0L; + b.tv_sec = TIME_MAX, b.tv_usec = 0L; + assert(libsimple_sumtimeval(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_usec == 999999L); + + a.tv_sec = TIME_MAX, a.tv_usec = 999999L; + b.tv_sec = 0, b.tv_usec = 1L; + assert(libsimple_sumtimeval(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_usec == 999999L); + + a.tv_sec = TIME_MAX, a.tv_usec = 1L; + b.tv_sec = 0, b.tv_usec = 999999L; + assert(libsimple_sumtimeval(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_usec == 999999L); + + a.tv_sec = TIME_MIN, a.tv_usec = 0L; + b.tv_sec = TIME_MIN, b.tv_usec = 0L; + assert(libsimple_sumtimeval(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MIN); + assert(r.tv_usec == 0L); + + a.tv_sec = TIME_MIN, a.tv_usec = 100L; + b.tv_sec = TIME_MIN, b.tv_usec = 100L; + assert(libsimple_sumtimeval(&r, &a, &b) == -1); + assert(r.tv_sec == TIME_MIN); + assert(r.tv_usec == 0L); + + return 0; +} + +#endif 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 |