diff options
author | 2019-08-12 17:59:10 +0300 | |
---|---|---|
committer | 2019-08-12 17:59:10 +0300 | |
commit | 3fcd58c715993b3c2a42e21d41fece5d7d09243c (patch) | |
tree | 14793609fc8f94b2f9d8014dd8d554414cc6b53b /test/time.c | |
parent | 7c4f627ebb0e2d2f1922c4ec18032a3619ea5f1d (diff) | |
download | librin-3fcd58c715993b3c2a42e21d41fece5d7d09243c.tar.gz librin-3fcd58c715993b3c2a42e21d41fece5d7d09243c.tar.bz2 librin-3fcd58c715993b3c2a42e21d41fece5d7d09243c.zip |
time: add timeval versions.
We now got functions to operate on both struct timespec and
struct timeval.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'test/time.c')
-rw-r--r-- | test/time.c | 231 |
1 files changed, 221 insertions, 10 deletions
diff --git a/test/time.c b/test/time.c index 017597b..f0ad5a2 100644 --- a/test/time.c +++ b/test/time.c @@ -24,16 +24,26 @@ int time_test(char *testname) { static const struct test tests[] = { - {"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}, - {"cmpmoreequal", rin_time_cmpmoreequal_test}, - {"cmpequal",rin_time_cmpequal_test}, - {"cmpnonequal", rin_time_cmpnonequal_test} }; + {"timespec add", rin_time_add_test}, + {"timespec sub", rin_time_sub_test}, + {"timespec normalize", rin_time_normalize_test}, + {"timespec cmp", rin_time_cmp_test}, + {"timespec cmpless", rin_time_cmpless_test}, + {"timespec cmpmore", rin_time_cmpmore_test}, + {"timespec cmplessequal", rin_time_cmplessequal_test}, + {"timespec cmpmoreequal", rin_time_cmpmoreequal_test}, + {"timespec cmpequal",rin_time_cmpequal_test}, + {"timespec cmpnonequal", rin_time_cmpnonequal_test}, + {"timeval add", rin_timeval_add_test}, + {"timeval sub", rin_timeval_sub_test}, + {"timeval normalize", rin_timeval_normalize_test}, + {"timeval cmp", rin_timeval_cmp_test}, + {"timeval cmpless", rin_timeval_cmpless_test}, + {"timeval cmpmore", rin_timeval_cmpmore_test}, + {"timeval cmplessequal", rin_timeval_cmplessequal_test}, + {"timeval cmpmoreequal", rin_timeval_cmpmoreequal_test}, + {"timeval cmpequal",rin_timeval_cmpequal_test}, + {"timeval cmpnonequal", rin_timeval_cmpnonequal_test} }; size_t i; for (i = 0; i < arrlen(tests); ++i) { @@ -44,6 +54,7 @@ int time_test(char *testname) return EXIT_FAILURE; } + static int rin_time_add_test(void) { int ret; @@ -245,3 +256,203 @@ static int rin_time_cmpnonequal_test(void) return ret; } +static int rin_timeval_add_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 12289}, {0, 999999}, {0, 999998}, {0, 0} }; + static const struct timeval in_b[] = { {23, 17711}, {0, 21}, {999999, 2}, {0, 0} }; + static const struct timeval expected[] = { {210, 30000}, {1, 20}, {1000000, 0}, {0, 0} }; + struct timeval tmp; + + for (i = 0; i < arrlen(expected); ++i) { + tmp = rin_timeval_add(in_a + i, in_b + i); + ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_usec != expected[i].tv_usec, + "%s: expected: %li %li, got: %li %li", + __func__, + expected[i].tv_sec, + expected[i].tv_usec, + tmp.tv_sec, + tmp.tv_usec); + } + + return ret; +} + +static int rin_timeval_sub_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 17289}, {0, 999999}, {0, 99}, {0, 0} }; + static const struct timeval in_b[] = { {27, 12711}, {0, 1}, {999999, 999999}, {0, 0} }; + static const struct timeval expected[] = { {160, 4578}, {0, 999998}, {-1000000, 100}, {0, 0} }; + struct timeval tmp; + + for (i = 0; i < arrlen(expected); ++i) { + tmp = rin_timeval_sub(in_a + i, in_b + i); + ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_usec != expected[i].tv_usec, + "%s: expected: %li %li, got: %li %li", + __func__, + expected[i].tv_sec, + expected[i].tv_usec, + tmp.tv_sec, + tmp.tv_usec); + } + + return ret; +} + +static int rin_timeval_normalize_test(void) +{ + int ret; + size_t i; + static const struct timeval in[] = { {187, -100}, {0, 1999999}, {2, 99000000}, {10, 10}, {0, 1000000}, {0, 0} }; + static const struct timeval expected[] = { {186, 999900}, {1, 999999}, {101, 0}, {10, 10}, {1, 0}, {0, 0} }; + struct timeval tmp; + + for (i = 0; i < arrlen(expected); ++i) { + tmp = rin_timeval_normalize(in + i); + ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_usec != expected[i].tv_usec, + "%s: expected: %li %li, got: %li %li", + __func__, + expected[i].tv_sec, + expected[i].tv_usec, + tmp.tv_sec, + tmp.tv_usec); + } + + return ret; +} + +static int rin_timeval_cmp_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timeval 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_timeval_cmp(in_a + i, in_b + i) != expected[i], + "%s: expected: %li, got: %li", + __func__, + expected[i], + rin_timeval_cmp(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_timeval_cmpless_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timeval in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} }; + static const unsigned int expected[] = {0, 0, 0, 1, 1, 1, 0, 0}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_timeval_cmp_less(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_timeval_cmp_less(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_timeval_cmpmore_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timeval in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} }; + static const unsigned int expected[] = {0, 1, 0, 0, 0, 0, 1, 0}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_timeval_cmp_more(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_timeval_cmp_more(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_timeval_cmplessequal_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timeval in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} }; + static const unsigned int expected[] = {1, 0, 1, 1, 1, 1, 0, 1}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_timeval_cmp_lessequal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_timeval_cmp_lessequal(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_timeval_cmpmoreequal_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timeval in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} }; + static const unsigned int expected[] = {1, 1, 1, 0, 0, 0, 1, 1}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_timeval_cmp_moreequal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_timeval_cmp_moreequal(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_timeval_cmpequal_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timeval in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} }; + static const unsigned int expected[] = {1, 0, 1, 0, 0, 0, 0, 1}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_timeval_cmp_equal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_timeval_cmp_equal(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_timeval_cmpnonequal_test(void) +{ + int ret; + size_t i; + static const struct timeval in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} }; + static const struct timeval in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} }; + static const unsigned int expected[] = {0, 1, 0, 1, 1, 1, 1, 0}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_timeval_cmp_nonequal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_timeval_cmp_nonequal(in_a + i, in_b + i)); + } + + return ret; +} |