From 1d02d81278becb538ba2fdee113c8c90ad083f2e Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Thu, 9 Nov 2017 16:44:31 +0200 Subject: tests: add tests for time functions. --- test/main.c | 4 +- test/meson.build | 3 +- test/time.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++ test/time_test.h | 28 ++++++ test/time_test_private.h | 36 ++++++++ 5 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 test/time.c create mode 100644 test/time_test.h create mode 100644 test/time_test_private.h (limited to 'test') diff --git a/test/main.c b/test/main.c index c0217eb..c87c24c 100644 --- a/test/main.c +++ b/test/main.c @@ -22,6 +22,7 @@ #include "float_test.h" #include "diagnostic_test.h" #include "gpio_test.h" +#include "time_test.h" /* TODO: allow individual tests to be selected */ @@ -30,7 +31,8 @@ int main(int argc, char **argv) static const struct section sections[] = { {"float", float_test}, {"diagnostic", diagnostic_test}, - {"gpio", gpio_test} }; + {"gpio", gpio_test}, + {"time", time_test} }; size_t i; if (argc != 3) { diff --git a/test/meson.build b/test/meson.build index 3cc925b..b23a438 100644 --- a/test/meson.build +++ b/test/meson.build @@ -3,7 +3,8 @@ test_filenames = [ 'test.c', 'float.c', 'diagnostic.c', - 'gpio.c' + 'gpio.c', + 'time.c' ] test_sources = files(test_filenames) 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; +} + diff --git a/test/time_test.h b/test/time_test.h new file mode 100644 index 0000000..9a96d4f --- /dev/null +++ b/test/time_test.h @@ -0,0 +1,28 @@ +/* + * 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 + */ + +#ifndef LIBRIN_TIME_TEST_INCLUDED +#define LIBRIN_TIME_TEST_INCLUDED + +#include "test.h" + +int time_test(char *testname); + +#endif /* LIBRIN_TIME_TEST_INCLUDED */ diff --git a/test/time_test_private.h b/test/time_test_private.h new file mode 100644 index 0000000..8ccd192 --- /dev/null +++ b/test/time_test_private.h @@ -0,0 +1,36 @@ +/* + * 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 + */ + +#ifndef LIBRIN_TIME_TEST_INCLUDED +#define LIBRIN_TIME_TEST_INCLUDED + +#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_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); + +#endif /* LIBRIN_TIME_TEST_INCLUDED */ -- cgit v1.2.3