/* * The Rin Library – library "conformance" tests * * Copyright (C) 2017-2019 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 "rin/time.h" 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); static int rin_time_cmpmoreequal_test(void); static int rin_time_cmpequal_test(void); static int rin_time_cmpnonequal_test(void); static int rin_timeval_add_test(void); static int rin_timeval_sub_test(void); static int rin_timeval_normalize_test(void); static int rin_timeval_cmp_test(void); static int rin_timeval_cmpless_test(void); static int rin_timeval_cmpmore_test(void); static int rin_timeval_cmplessequal_test(void); static int rin_timeval_cmpmoreequal_test(void); static int rin_timeval_cmpequal_test(void); static int rin_timeval_cmpnonequal_test(void); int time_test(char *testname) { static const struct test tests[] = { {"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) { 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, 999999900}, {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_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; 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; } 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; }