Adds a framework for unittests for comedi drivers. It was certainly possible to write some unit tests before and test various aspects of a particular driver, but this framework makes this a bit easier and hopefully inspires more unittest modules to be written. Signed-off-by: Spencer E. Olson <olsonse@xxxxxxxxx> --- drivers/staging/comedi/drivers/Makefile | 1 + drivers/staging/comedi/drivers/tests/Makefile | 6 ++ .../comedi/drivers/tests/example_test.c | 72 +++++++++++++++++++ .../staging/comedi/drivers/tests/unittest.h | 63 ++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 drivers/staging/comedi/drivers/tests/Makefile create mode 100644 drivers/staging/comedi/drivers/tests/example_test.c create mode 100644 drivers/staging/comedi/drivers/tests/unittest.h diff --git a/drivers/staging/comedi/drivers/Makefile b/drivers/staging/comedi/drivers/Makefile index 98b42b47dfe1..8cb518190fc7 100644 --- a/drivers/staging/comedi/drivers/Makefile +++ b/drivers/staging/comedi/drivers/Makefile @@ -145,3 +145,4 @@ obj-$(CONFIG_COMEDI_8255_SA) += 8255.o obj-$(CONFIG_COMEDI_AMPLC_DIO200) += amplc_dio200_common.o obj-$(CONFIG_COMEDI_AMPLC_PC236) += amplc_pc236_common.o obj-$(CONFIG_COMEDI_DAS08) += das08.o +obj-$(CONFIG_COMEDI_TESTS) += tests/ diff --git a/drivers/staging/comedi/drivers/tests/Makefile b/drivers/staging/comedi/drivers/tests/Makefile new file mode 100644 index 000000000000..1d58ede0bdf6 --- /dev/null +++ b/drivers/staging/comedi/drivers/tests/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for comedi drivers unit tests +# +ccflags-$(CONFIG_COMEDI_DEBUG) := -DDEBUG + +obj-$(CONFIG_COMEDI_TESTS) += example_test.o diff --git a/drivers/staging/comedi/drivers/tests/example_test.c b/drivers/staging/comedi/drivers/tests/example_test.c new file mode 100644 index 000000000000..fc65158b8e8e --- /dev/null +++ b/drivers/staging/comedi/drivers/tests/example_test.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* vim: set ts=8 sw=8 noet tw=80 nowrap: */ +/* + * comedi/drivers/tests/example_test.c + * Example set of unit tests. + * + * COMEDI - Linux Control and Measurement Device Interface + * Copyright (C) 2016 Spencer E. Olson <olsonse@xxxxxxxxx> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + */ + +#include <linux/module.h> + +#include "unittest.h" + +/* *** BEGIN fake board data *** */ +struct comedi_device { + const char *board_name; + int item; +}; + +static struct comedi_device dev = { + .board_name = "fake_device", +}; + +/* *** END fake board data *** */ + +/* *** BEGIN fake data init *** */ +void init_fake(void) +{ + dev.item = 10; +} + +/* *** END fake data init *** */ + +void test0(void) +{ + init_fake(); + unittest(dev.item != 11, "negative result\n"); + unittest(dev.item == 10, "positive result\n"); +} + +/* **** BEGIN simple module entry/exit functions **** */ +static int __init unittest_enter(void) +{ + const unittest_fptr unit_tests[] = { + (unittest_fptr)test0, + NULL, + }; + + exec_unittests("example", unit_tests); + return 0; +} + +static void __exit unittest_exit(void) { } + +module_init(unittest_enter); +module_exit(unittest_exit); + +MODULE_AUTHOR("Spencer Olson <olsonse@xxxxxxxxx>"); +MODULE_DESCRIPTION("Comedi unit-tests example"); +MODULE_LICENSE("GPL"); +/* **** END simple module entry/exit functions **** */ diff --git a/drivers/staging/comedi/drivers/tests/unittest.h b/drivers/staging/comedi/drivers/tests/unittest.h new file mode 100644 index 000000000000..b8e622ea1de1 --- /dev/null +++ b/drivers/staging/comedi/drivers/tests/unittest.h @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* vim: set ts=8 sw=8 noet tw=80 nowrap: */ +/* + * comedi/drivers/tests/unittest.h + * Simple framework for unittests for comedi drivers. + * + * COMEDI - Linux Control and Measurement Device Interface + * Copyright (C) 2016 Spencer E. Olson <olsonse@xxxxxxxxx> + * based of parts of drivers/of/unittest.c + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + */ + +#ifndef _COMEDI_DRIVERS_TESTS_UNITTEST_H +#define _COMEDI_DRIVERS_TESTS_UNITTEST_H + +static struct unittest_results { + int passed; + int failed; +} unittest_results; + +typedef void *(*unittest_fptr)(void); + +#define unittest(result, fmt, ...) ({ \ + bool failed = !(result); \ + if (failed) { \ + ++unittest_results.failed; \ + pr_err("FAIL %s():%i " fmt, __func__, __LINE__, \ + ##__VA_ARGS__); \ + } else { \ + ++unittest_results.passed; \ + pr_debug("pass %s():%i " fmt, __func__, __LINE__, \ + ##__VA_ARGS__); \ + } \ + failed; \ +}) + +/** + * Execute an array of unit tests. + * @name: Name of set of unit tests--will be shown at INFO log level. + * @unit_tests: A null-terminated list of unit tests to execute. + */ +static inline void exec_unittests(const char *name, + const unittest_fptr *unit_tests) +{ + pr_info("begin comedi:\"%s\" unittests\n", name); + + for (; (*unit_tests) != NULL; ++unit_tests) + (*unit_tests)(); + + pr_info("end of comedi:\"%s\" unittests - %i passed, %i failed\n", name, + unittest_results.passed, unittest_results.failed); +} + +#endif /* _COMEDI_DRIVERS_TESTS_UNITTEST_H */ -- 2.17.1 _______________________________________________ devel mailing list devel@xxxxxxxxxxxxxxxxxxxxxx http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel