From 779ccf8c11d3990052f3e0f400319dbb8c2d7f77 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 26 Aug 2018 20:55:48 +0200 Subject: Fixes and tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 2 + libsimple.h | 2 +- multimespec.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- multimeval.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- strtotimeval.c | 4 +- sumtimeval.c | 4 +- 6 files changed, 318 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index d235867..4c53b19 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,8 @@ TESTS =\ memrmem.test\ memstarts.test\ minimise_number_string.test\ + multimespec.test\ + multimeval.test\ rawmemchr.test\ rawmemrchr.test\ strcaseends.test\ diff --git a/libsimple.h b/libsimple.h index 3a18365..4e21e68 100644 --- a/libsimple.h +++ b/libsimple.h @@ -731,7 +731,7 @@ char *libsimple_strrncasestr(const char *, const char *, size_t); /* TODO test * _LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) -char *libsimple_strnstr(const char *, const char *, size_t); /* TODO test */ +char *libsimple_strncasestr(const char *, const char *, size_t); /* TODO test */ #ifndef strncasestr # define strncasestr libsimple_strncasestr #endif diff --git a/multimespec.c b/multimespec.c index ec60c1e..c2a58c1 100644 --- a/multimespec.c +++ b/multimespec.c @@ -1,9 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int -libsimple_multimespec(struct timespec *prod, const struct timespec *multiplicand, int multiplier) /* TODO test */ +libsimple_multimespec(struct timespec *prod, const struct timespec *multiplicand, int multiplier) { time_t s = multiplicand->tv_sec; long long int ns = (long long int)(multiplicand->tv_nsec); @@ -63,3 +64,154 @@ overflow: errno = ERANGE; return -1; } + + +#else +#include "test.h" + +int +main(void) +{ + struct timespec r, a; + + a.tv_sec = 0, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 10, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 0, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 10, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 0, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, 1)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 10, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, 1)); + assert(r.tv_sec == 10); + assert(r.tv_nsec == 0L); + + a.tv_sec = 0, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, 1)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 10L); + + a.tv_sec = 10, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, 1)); + assert(r.tv_sec == 10); + assert(r.tv_nsec == 10L); + + a.tv_sec = 0, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, 10)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 10, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, 10)); + assert(r.tv_sec == 100); + assert(r.tv_nsec == 0L); + + a.tv_sec = 0, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, 10)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 100L); + + a.tv_sec = 10, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, 10)); + assert(r.tv_sec == 100); + assert(r.tv_nsec == 100L); + + a.tv_sec = 0, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, -1)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 10, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, -1)); + assert(r.tv_sec == -10); + assert(r.tv_nsec == 0L); + + a.tv_sec = 0, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, -1)); + assert(r.tv_sec == -1); + assert(r.tv_nsec == 1000000000L - 10L); + + a.tv_sec = 10, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, -1)); + assert(r.tv_sec == -11); + assert(r.tv_nsec == 1000000000L - 10L); + + a.tv_sec = 0, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, -10)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0L); + + a.tv_sec = 10, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, -10)); + assert(r.tv_sec == -100); + assert(r.tv_nsec == 0L); + + a.tv_sec = 0, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, -10)); + assert(r.tv_sec == -1); + assert(r.tv_nsec == 1000000000L - 100L); + + a.tv_sec = 10, a.tv_nsec = 10L; + assert(!libsimple_multimespec(&r, &a, -10)); + assert(r.tv_sec == -101); + assert(r.tv_nsec == 1000000000L - 100L); + + a.tv_sec = TIME_MAX, a.tv_nsec = 999999999L; + assert(!libsimple_multimespec(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_nsec == 0); + + a.tv_sec = TIME_MAX, a.tv_nsec = 999999999L; + assert(!libsimple_multimespec(&r, &a, 1)); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_nsec == 999999999L); + + a.tv_sec = TIME_MAX, a.tv_nsec = 0L; + assert(libsimple_multimespec(&r, &a, 2) == -1 && errno == ERANGE); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_nsec == 999999999L); + + a.tv_sec = TIME_MAX, a.tv_nsec = 0L; + assert(libsimple_multimespec(&r, &a, -2) == -1 && errno == ERANGE); + assert(r.tv_sec == TIME_MIN); + assert(r.tv_nsec == 0L); + + a.tv_sec = TIME_MAX, a.tv_nsec = 0L; + assert(!libsimple_multimespec(&r, &a, -1)); + assert(r.tv_sec == -TIME_MAX); + assert(r.tv_nsec == 0L); + + if (-TIME_MAX > TIME_MIN) { + a.tv_sec = TIME_MAX, a.tv_nsec = 999999999L; + assert(!libsimple_multimespec(&r, &a, -1)); + assert(r.tv_sec == -TIME_MAX - (time_t)1); + assert(r.tv_nsec == 1L); + } + + a.tv_sec = 10, a.tv_nsec = 100000001L; + assert(!libsimple_multimespec(&r, &a, 10)); + assert(r.tv_sec == 101); + assert(r.tv_nsec == 10L); + + return 0; +} + +#endif diff --git a/multimeval.c b/multimeval.c index bb8c951..5f8010e 100644 --- a/multimeval.c +++ b/multimeval.c @@ -1,9 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include "libsimple.h" +#ifndef TEST int -libsimple_multimeval(struct timeval *prod, const struct timeval *multiplicand, int multiplier) /* TODO test */ +libsimple_multimeval(struct timeval *prod, const struct timeval *multiplicand, int multiplier) { struct timespec a, p; int r; @@ -11,5 +12,158 @@ libsimple_multimeval(struct timeval *prod, const struct timeval *multiplicand, i r = libsimple_multimespec(&p, &a, multiplier); if (r && errno != ERANGE) return r; - return r | libsimple_timespec2timeval(prod, &p); + if (libsimple_timespec2timeval(prod, &p) && r) + errno = ERANGE; + return r; } + + +#else +#include "test.h" + +int +main(void) +{ + struct timeval r, a; + + a.tv_sec = 0, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 10, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 0, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 10, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 0, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, 1)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 10, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, 1)); + assert(r.tv_sec == 10); + assert(r.tv_usec == 0L); + + a.tv_sec = 0, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, 1)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 10L); + + a.tv_sec = 10, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, 1)); + assert(r.tv_sec == 10); + assert(r.tv_usec == 10L); + + a.tv_sec = 0, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, 10)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 10, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, 10)); + assert(r.tv_sec == 100); + assert(r.tv_usec == 0L); + + a.tv_sec = 0, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, 10)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 100L); + + a.tv_sec = 10, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, 10)); + assert(r.tv_sec == 100); + assert(r.tv_usec == 100L); + + a.tv_sec = 0, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, -1)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 10, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, -1)); + assert(r.tv_sec == -10); + assert(r.tv_usec == 0L); + + a.tv_sec = 0, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, -1)); + assert(r.tv_sec == -1); + assert(r.tv_usec == 1000000L - 10L); + + a.tv_sec = 10, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, -1)); + assert(r.tv_sec == -11); + assert(r.tv_usec == 1000000L - 10L); + + a.tv_sec = 0, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, -10)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0L); + + a.tv_sec = 10, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, -10)); + assert(r.tv_sec == -100); + assert(r.tv_usec == 0L); + + a.tv_sec = 0, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, -10)); + assert(r.tv_sec == -1); + assert(r.tv_usec == 1000000L - 100L); + + a.tv_sec = 10, a.tv_usec = 10L; + assert(!libsimple_multimeval(&r, &a, -10)); + assert(r.tv_sec == -101); + assert(r.tv_usec == 1000000L - 100L); + + a.tv_sec = TIME_MAX, a.tv_usec = 999999L; + assert(!libsimple_multimeval(&r, &a, 0)); + assert(r.tv_sec == 0); + assert(r.tv_usec == 0); + + a.tv_sec = TIME_MAX, a.tv_usec = 999999L; + assert(!libsimple_multimeval(&r, &a, 1)); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_usec == 999999L); + + a.tv_sec = TIME_MAX, a.tv_usec = 0L; + assert(libsimple_multimeval(&r, &a, 2) == -1 && errno == ERANGE); + assert(r.tv_sec == TIME_MAX); + assert(r.tv_usec == 999999L); + + a.tv_sec = TIME_MAX, a.tv_usec = 0L; + assert(libsimple_multimeval(&r, &a, -2) == -1 && errno == ERANGE); + assert(r.tv_sec == TIME_MIN); + assert(r.tv_usec == 0L); + + a.tv_sec = TIME_MAX, a.tv_usec = 0L; + assert(!libsimple_multimeval(&r, &a, -1)); + assert(r.tv_sec == -TIME_MAX); + assert(r.tv_usec == 0L); + + if (-TIME_MAX > TIME_MIN) { + a.tv_sec = TIME_MAX, a.tv_usec = 999999L; + assert(!libsimple_multimeval(&r, &a, -1)); + assert(r.tv_sec == -TIME_MAX - (time_t)1); + assert(r.tv_usec == 1L); + } + + a.tv_sec = 10, a.tv_usec = 100001L; + assert(!libsimple_multimeval(&r, &a, 10)); + assert(r.tv_sec == 101); + assert(r.tv_usec == 10L); + + return 0; +} + +#endif diff --git a/strtotimeval.c b/strtotimeval.c index 4df5674..a0a18b9 100644 --- a/strtotimeval.c +++ b/strtotimeval.c @@ -9,5 +9,7 @@ libsimple_strtotimeval(struct timeval *restrict tv, const char *restrict s, char int r = libsimple_strtotimespec(&ts, s, end); if (r && errno != ERANGE) return r; - return r | libsimple_timespec2timeval(tv, &ts); + if (libsimple_timespec2timeval(tv, &ts) && r) + errno = ERANGE; + return r; } diff --git a/sumtimeval.c b/sumtimeval.c index b4e80bb..47ef705 100644 --- a/sumtimeval.c +++ b/sumtimeval.c @@ -13,7 +13,9 @@ libsimple_sumtimeval(struct timeval *sum, const struct timeval *augend, const st r = libsimple_sumtimespec(&s, &a, &b); if (r && errno != ERANGE) return r; - return r | libsimple_timespec2timeval(sum, &s); + if (libsimple_timespec2timeval(sum, &s) && r) + errno = ERANGE; + return r; } -- cgit v1.2.3-70-g09d2