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 /sumtimespec.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 '')
-rw-r--r-- | sumtimespec.c | 79 |
1 files changed, 77 insertions, 2 deletions
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 |