diff options
Diffstat (limited to 'test/time.c')
-rw-r--r-- | test/time.c | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/test/time.c b/test/time.c new file mode 100644 index 0000000..fe8a195 --- /dev/null +++ b/test/time.c @@ -0,0 +1,227 @@ +/* + * The Rin Library – library "conformance" tests + * + * Copyright (C) 2017 Gediminas Jakutis + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "test.h" +#include "time_test_private.h" + +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}, + {"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} }; + size_t i; + + for (i = 0; i < arrlen(tests); ++i) { + if (!strcmp(testname, tests[i].name)) { + return tests[i].testfunc(); + } + } + + return EXIT_FAILURE; +} +static int rin_time_add_test(void) +{ + int ret; + size_t i; + static const struct timespec in_a[] = { {187, 12289}, {0, 999999999}, {0, 999999998}, {0, 0} }; + static const struct timespec in_b[] = { {23, 17711}, {0, 21}, {999999999, 2}, {0, 0} }; + static const struct timespec expected[] = { {210, 30000}, {1, 20}, {1000000000, 0}, {0, 0} }; + struct timespec tmp; + + for (i = 0; i < arrlen(expected); ++i) { + tmp = rin_time_add(in_a + i, in_b + i); + ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_nsec != expected[i].tv_nsec, + "%s: expected: %li %li, got: %li %li", + __func__, + expected[i].tv_sec, + expected[i].tv_nsec, + tmp.tv_sec, + tmp.tv_nsec); + } + + return ret; +} + +static int rin_time_sub_test(void) +{ + int ret; + size_t i; + static const struct timespec in_a[] = { {187, 17289}, {0, 999999999}, {0, 99}, {0, 0} }; + static const struct timespec in_b[] = { {27, 12711}, {0, 1}, {999999999, 999999999}, {0, 0} }; + static const struct timespec expected[] = { {160, 4578}, {0, 999999998}, {-1000000000, 100}, {0, 0} }; + struct timespec tmp; + + for (i = 0; i < arrlen(expected); ++i) { + tmp = rin_time_sub(in_a + i, in_b + i); + ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_nsec != expected[i].tv_nsec, + "%s: expected: %li %li, got: %li %li", + __func__, + expected[i].tv_sec, + expected[i].tv_nsec, + tmp.tv_sec, + tmp.tv_nsec); + } + + return ret; +} + +static int rin_time_normalize_test(void) +{ + int ret; + size_t i; + static const struct timespec in[] = { {187, -100}, {0, 1999999999}, {2, 99000000000}, {10, 10}, {0, 1000000000}, {0, 0} }; + static const struct timespec expected[] = { {186, 999999899}, {1, 999999999}, {101, 0}, {10, 10}, {1, 0}, {0, 0} }; + struct timespec tmp; + + for (i = 0; i < arrlen(expected); ++i) { + tmp = rin_time_normalize(in + i); + ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_nsec != expected[i].tv_nsec, + "%s: expected: %li %li, got: %li %li", + __func__, + expected[i].tv_sec, + expected[i].tv_nsec, + tmp.tv_sec, + tmp.tv_nsec); + } + + return ret; +} + +static int rin_time_cmpless_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 unsigned int expected[] = {0, 0, 0, 1, 1, 1, 0, 0}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_time_cmp_less(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_time_cmp_less(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_time_cmpmore_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 unsigned int expected[] = {0, 1, 0, 0, 0, 0, 1, 0}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_time_cmp_more(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_time_cmp_more(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_time_cmplessequal_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 unsigned int expected[] = {1, 0, 1, 1, 1, 1, 0, 1}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_time_cmp_lessequal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_time_cmp_lessequal(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_time_cmpmoreequal_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 unsigned int expected[] = {1, 1, 1, 0, 0, 0, 1, 1}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_time_cmp_moreequal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_time_cmp_moreequal(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_time_cmpequal_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 unsigned int expected[] = {1, 0, 1, 0, 0, 0, 0, 1}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_time_cmp_equal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_time_cmp_equal(in_a + i, in_b + i)); + } + + return ret; +} + +static int rin_time_cmpnonequal_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 unsigned int expected[] = {0, 1, 0, 1, 1, 1, 1, 0}; + + for (i = 0; i < arrlen(expected); ++i) { + ret = ok(rin_time_cmp_nonequal(in_a + i, in_b + i) != expected[i], + "%s: expected: %u, got: %u", + __func__, + expected[i], + rin_time_cmp_nonequal(in_a + i, in_b + i)); + } + + return ret; +} + |