diff options
author | 2019-06-19 11:58:03 +0300 | |
---|---|---|
committer | 2019-06-19 11:58:03 +0300 | |
commit | c499fc7bc9e1c3b8fe04ea5c80e5a7e0b16b11fc (patch) | |
tree | 5ec5eaeb3eb951f65715aa62a043c6ae7eac7854 | |
parent | ef5cc380045bba05acfcba7d0e1c03046fdc82d0 (diff) | |
download | librin-c499fc7bc9e1c3b8fe04ea5c80e5a7e0b16b11fc.tar.gz librin-c499fc7bc9e1c3b8fe04ea5c80e5a7e0b16b11fc.tar.bz2 librin-c499fc7bc9e1c3b8fe04ea5c80e5a7e0b16b11fc.zip |
time: add a generalized time comparison function.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
-rw-r--r-- | include/rin/time.h | 1 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/time/time.c | 9 | ||||
-rw-r--r-- | test/time.c | 20 | ||||
-rw-r--r-- | test/time_test_private.h | 1 |
5 files changed, 32 insertions, 0 deletions
diff --git a/include/rin/time.h b/include/rin/time.h index aa9bc66..523103f 100644 --- a/include/rin/time.h +++ b/include/rin/time.h @@ -26,6 +26,7 @@ struct timespec rin_time_add(const struct timespec * const a, const struct timespec * const b); struct timespec rin_time_sub(const struct timespec * const a, const struct timespec * const b); struct timespec rin_time_normalize(const struct timespec * const t); +long int rin_time_cmp(const struct timespec * const a, const struct timespec * const b); unsigned int rin_time_cmp_less(const struct timespec * const a, const struct timespec * const b); unsigned int rin_time_cmp_more(const struct timespec * const a, const struct timespec * const b); unsigned int rin_time_cmp_lessequal(const struct timespec * const a, const struct timespec * const b); diff --git a/meson.build b/meson.build index 17de118..4aee09d 100644 --- a/meson.build +++ b/meson.build @@ -37,6 +37,7 @@ if get_option('tests') test('time add', test_e, args : ['time', 'add']) test('time subtract', test_e, args : ['time', 'sub']) test('time normalize', test_e, args : ['time', 'normalize']) + test('time compare', test_e, args : ['time', 'cmp']) test('time compare less', test_e, args : ['time', 'cmpless']) test('time compare more', test_e, args : ['time', 'cmpmore']) test('time compare less or equal', test_e, args : ['time', 'cmplessequal']) diff --git a/src/time/time.c b/src/time/time.c index 13a102f..d9f00b7 100644 --- a/src/time/time.c +++ b/src/time/time.c @@ -60,8 +60,17 @@ struct timespec rin_time_normalize(const struct timespec * const t) } return ret; +} +long int rin_time_cmp(const struct timespec * const a, const struct timespec * const b) +{ + if (a->tv_sec != b->tv_sec) { + return a->tv_sec - b->tv_sec; + } else { + return a->tv_nsec - b->tv_nsec; + } } + unsigned int rin_time_cmp_less(const struct timespec * const a, const struct timespec * const b) { if (a->tv_sec > b->tv_sec) { diff --git a/test/time.c b/test/time.c index 4307fd1..017597b 100644 --- a/test/time.c +++ b/test/time.c @@ -27,6 +27,7 @@ int time_test(char *testname) {"add", rin_time_add_test}, {"sub", rin_time_sub_test}, {"normalize", rin_time_normalize_test}, + {"cmp", rin_time_cmp_test}, {"cmpless", rin_time_cmpless_test}, {"cmpmore", rin_time_cmpmore_test}, {"cmplessequal", rin_time_cmplessequal_test}, @@ -111,6 +112,25 @@ static int rin_time_normalize_test(void) return ret; } +static int rin_time_cmp_test(void) +{ + int ret; + size_t i; + static const struct timespec in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timespec in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} }; + static const long int expected[] = {0, 1, 0, -100, -1, -1, 1, 0}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_time_cmp(in_a + i, in_b + i) != expected[i], + "%s: expected: %li, got: %li", + __func__, + expected[i], + rin_time_cmp(in_a + i, in_b + i)); + } + + return ret; +} + static int rin_time_cmpless_test(void) { int ret; diff --git a/test/time_test_private.h b/test/time_test_private.h index 9afbda2..c7d505c 100644 --- a/test/time_test_private.h +++ b/test/time_test_private.h @@ -26,6 +26,7 @@ static int rin_time_add_test(void); static int rin_time_sub_test(void); static int rin_time_normalize_test(void); +static int rin_time_cmp_test(void); static int rin_time_cmpless_test(void); static int rin_time_cmpmore_test(void); static int rin_time_cmplessequal_test(void); |