From c7198103b9816ef761ddbdf5d23f94e52afdc730 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Wed, 8 Nov 2017 14:46:54 +0200 Subject: tests: split test and let meson handle them. --- test/diagnostic.c | 2 +- test/diagnostic_test.h | 2 +- test/float.c | 48 ++++++++++++++++++++++++++--------------------- test/float_test.h | 2 +- test/float_test_private.h | 12 ++++++------ test/gpio.c | 28 +++++++++++++++++++++++++++ test/gpio_test.h | 26 +++++++++++++++++++++++++ test/gpio_test_private.h | 26 +++++++++++++++++++++++++ test/main.c | 21 +++++++++++++++------ test/meson.build | 1 + test/test.c | 12 +++++------- test/test.h | 17 +++++++++++++---- 12 files changed, 150 insertions(+), 47 deletions(-) create mode 100644 test/gpio.c create mode 100644 test/gpio_test.h create mode 100644 test/gpio_test_private.h (limited to 'test') diff --git a/test/diagnostic.c b/test/diagnostic.c index f8a2bd4..b6958ad 100644 --- a/test/diagnostic.c +++ b/test/diagnostic.c @@ -21,7 +21,7 @@ #include "test.h" #include "diagnostic_test_private.h" -struct test_results diagnostic_test(void) +int diagnostic_test(char *testname) { /* basically a NOOP */ return ok(0, ""); diff --git a/test/diagnostic_test.h b/test/diagnostic_test.h index e8f2967..0929abb 100644 --- a/test/diagnostic_test.h +++ b/test/diagnostic_test.h @@ -21,6 +21,6 @@ #ifndef LIBRIN_DIAGNOSTIC_TEST_INCLUDED #define LIBRIN_DIAGNOSTIC_TEST_INCLUDED -struct test_results diagnostic_test(void); +int diagnostic_test(char *testname); #endif /* LIBRIN_DIAGNOSTIC_TEST_INCLUDED */ diff --git a/test/float.c b/test/float.c index 9940ff4..f95ae8c 100644 --- a/test/float.c +++ b/test/float.c @@ -21,23 +21,29 @@ #include "test.h" #include "float_test_private.h" -struct test_results float_test(void) +int float_test(char *testname) { - struct test_results ret; + static const struct test tests[] = { + {"signbitf", rin_signbitf_test}, + {"signbitd", rin_signbitd_test}, + {"floattohexstring", rin_float_to_hexstring_test}, + {"doubletohexstring", rin_double_to_hexstring_test}, + {"comparefloat", rin_compare_float_test}, + {"comparedouble", rin_compare_double_test} }; + size_t i; - ret = rin_signbitf_test(); - ret = rin_signbitd_test(); - ret = rin_float_to_hexstring_test(); - ret = rin_double_to_hexstring_test(); - ret = rin_compare_float_test(); - ret = rin_compare_double_test(); + for (i = 0; i < arrlen(tests); ++i) { + if (!strcmp(testname, tests[i].name)) { + return tests[i].testfunc(); + } + } - return ret; + return EXIT_FAILURE; } -static struct test_results rin_signbitf_test(void) +static int rin_signbitf_test(void) { - struct test_results ret; + int ret; size_t i; static const float in[] = { 1.0f, -1.0f, 987656.4321f, -987656.4321f }; static const uint32_t expected[] = { 0, 0x80000000u, 0, 0x80000000u }; @@ -54,9 +60,9 @@ static struct test_results rin_signbitf_test(void) return ret; } -static struct test_results rin_signbitd_test(void) +static int rin_signbitd_test(void) { - struct test_results ret; + int ret; size_t i; static const double in[] = { 1.0, -1.0, 987656.4321, -987656.4321 }; static const uint64_t expected[] = { 0, 0x8000000000000000ull, 0, 0x8000000000000000ull }; @@ -73,9 +79,9 @@ static struct test_results rin_signbitd_test(void) return ret; } -static struct test_results rin_float_to_hexstring_test(void) +static int rin_float_to_hexstring_test(void) { - struct test_results ret; + int ret; size_t i; static const float in[] = { 1.0f, -1.0f, 98765.4321f, -98765.4321f }; static const char *expected_default[] = { "0x3f800000", "0xbf800000", "0x47c0e6b7", "0xc7c0e6b7" }; @@ -102,9 +108,9 @@ static struct test_results rin_float_to_hexstring_test(void) } -static struct test_results rin_double_to_hexstring_test(void) +static int rin_double_to_hexstring_test(void) { - struct test_results ret; + int ret; size_t i; static const double in[] = { 1.0, -1.0, 98765.4321, -98765.4321 }; static const char *expected_default[] = { "0x3ff0000000000000", "0xbff0000000000000", "0x40f81cd6e9e1b08a", "0xc0f81cd6e9e1b08a" }; @@ -130,9 +136,9 @@ static struct test_results rin_double_to_hexstring_test(void) return ret; } -static struct test_results rin_compare_float_test(void) +static int rin_compare_float_test(void) { - struct test_results ret; + int ret; size_t i; static const float a[] = { 0.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 98765.4321f, 98765.4321f, 98765.4321f }; static const float b[] = { -0.0f, 1.0f, 1.0f, 1.0000001f, -9.8f, 1.000001f, 98765.436f, 98777.7777f, -98765.4321f }; @@ -151,9 +157,9 @@ static struct test_results rin_compare_float_test(void) } -static struct test_results rin_compare_double_test(void) +static int rin_compare_double_test(void) { - struct test_results ret; + int ret; size_t i; static const double a[] = { 0.0, 1.0, -1.0, 1.0, -1.0f, 1.0, 98765.4321, 98765.4321, 98765.4321 }; static const double b[] = { -0.0, 1.0, 1.0, 1.0000000000000003, -9.8f, 1.000001, 98765.43210000002, 98777.7777, -98765.4321 }; diff --git a/test/float_test.h b/test/float_test.h index 1787bd7..89bcf21 100644 --- a/test/float_test.h +++ b/test/float_test.h @@ -23,6 +23,6 @@ #include "test.h" -struct test_results float_test(void); +int float_test(char *testname); #endif /* LIBRIN_FLOAT_TEST_INCLUDED */ diff --git a/test/float_test_private.h b/test/float_test_private.h index a64aaeb..c865646 100644 --- a/test/float_test_private.h +++ b/test/float_test_private.h @@ -27,11 +27,11 @@ #include #include "rin/float.h" -static struct test_results rin_signbitf_test(void); -static struct test_results rin_signbitd_test(void); -static struct test_results rin_float_to_hexstring_test(void); -static struct test_results rin_double_to_hexstring_test(void); -static struct test_results rin_compare_float_test(void); -static struct test_results rin_compare_double_test(void); +static int rin_signbitf_test(void); +static int rin_signbitd_test(void); +static int rin_float_to_hexstring_test(void); +static int rin_double_to_hexstring_test(void); +static int rin_compare_float_test(void); +static int rin_compare_double_test(void); #endif /* LIBRIN_FLOAT_TEST_INCLUDED */ diff --git a/test/gpio.c b/test/gpio.c new file mode 100644 index 0000000..c132e8f --- /dev/null +++ b/test/gpio.c @@ -0,0 +1,28 @@ +/* + * The Rin Library – library "conformance" tests + * + * Copyright (C) 2015 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 "gpio_test_private.h" + +int gpio_test(char *testname) +{ + /* basically a NOOP */ + return ok(0, ""); +} diff --git a/test/gpio_test.h b/test/gpio_test.h new file mode 100644 index 0000000..2de30fd --- /dev/null +++ b/test/gpio_test.h @@ -0,0 +1,26 @@ +/* + * The Rin Library – library "conformance" tests + * + * Copyright (C) 2015 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_GPIO_TEST_INCLUDED +#define LIBRIN_GPIO_TEST_INCLUDED + +int gpio_test(char *testname); + +#endif /* LIBRIN_GPIO_TEST_INCLUDED */ diff --git a/test/gpio_test_private.h b/test/gpio_test_private.h new file mode 100644 index 0000000..4a6a13a --- /dev/null +++ b/test/gpio_test_private.h @@ -0,0 +1,26 @@ +/* + * The Rin Library – library "conformance" tests + * + * Copyright (C) 2015 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_GPIO_TEST_PRIVATE_INCLUDED +#define LIBRIN_GPIO_TEST_PRIVATE_INCLUDED + +/* NOOP */ + +#endif /* LIBRIN_GPIO_TEST_PRIVATE_INCLUDED */ diff --git a/test/main.c b/test/main.c index c8fae63..6d7d8bf 100644 --- a/test/main.c +++ b/test/main.c @@ -21,18 +21,27 @@ #include "test.h" #include "float_test.h" #include "diagnostic_test.h" +#include "gpio_test.h" /* TODO: allow individual tests to be selected */ - int main(int argc, char **argv) { - struct test_results res; + static const struct section sections[] = { + {"float", float_test}, + {"diagnostic", diagnostic_test}, + {"gpio", gpio_test} }; + size_t i; - res = float_test(); - res = diagnostic_test(); + if (argc != 3) { + return EXIT_FAILURE; + } - printf("%u tests executed, %u test succeeded, %u tests failed.\n", res.executed, res.executed - res.failed, res.failed); + for (i = 0; i < arrlen(sections); ++i) { + if (!strcmp(argv[1], sections[i].name)) { + return sections[i].testfunc(argv[2]); + } + } - return EXIT_SUCCESS; + return EXIT_FAILURE; } diff --git a/test/meson.build b/test/meson.build index 70dfcf9..3cc925b 100644 --- a/test/meson.build +++ b/test/meson.build @@ -3,6 +3,7 @@ test_filenames = [ 'test.c', 'float.c', 'diagnostic.c', + 'gpio.c' ] test_sources = files(test_filenames) diff --git a/test/test.c b/test/test.c index e790538..7e10d9f 100644 --- a/test/test.c +++ b/test/test.c @@ -20,18 +20,16 @@ #include "test.h" -struct test_results ok(const unsigned int test, const char *format, ...) +int ok(const unsigned int test, const char *format, ...) { - static struct test_results ret; + static int ret; va_list args; - ++ret.executed; - if (test) { - ++ret.failed; + ++ret; va_start(args, format); - vprintf(format, args); - putchar('\n'); + vfprintf(stderr, format, args); + fputc('\n', stderr); } return ret; diff --git a/test/test.h b/test/test.h index 0597207..5314fc5 100644 --- a/test/test.h +++ b/test/test.h @@ -24,12 +24,21 @@ #include #include #include +#include -struct test_results { - unsigned int executed; - unsigned int failed; +/* TODO: move to a shared util header */ +#define arrlen(a) (sizeof(a)/sizeof(a[0])) + +struct section { + char *name; + int (*testfunc)(char*); +}; + +struct test { + char *name; + int (*testfunc)(void); }; -struct test_results ok(const unsigned int test, const char *format, ...); +int ok(const unsigned int test, const char *format, ...); #endif /* LIBRIN_TEST_INCLUDED */ -- cgit v1.2.3