summaryrefslogtreecommitdiffstats
path: root/drivers/staging
diff options
context:
space:
mode:
authorSpencer E. Olson <olsonse@umich.edu>2018-10-03 14:56:00 -0600
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-10-09 15:32:06 +0200
commit040e9e4dfa14de632824b9368462c1bd20d05733 (patch)
tree1c82e19c4f03ac6428005d2a368388497ae7f07a /drivers/staging
parente0b2ca8979c4b923d28eb4daa3a783463debd31c (diff)
downloadlinux-040e9e4dfa14de632824b9368462c1bd20d05733.tar.gz
linux-040e9e4dfa14de632824b9368462c1bd20d05733.tar.bz2
linux-040e9e4dfa14de632824b9368462c1bd20d05733.zip
staging: comedi: tests: add unittest framework for comedi
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@umich.edu> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/staging')
-rw-r--r--drivers/staging/comedi/drivers/Makefile1
-rw-r--r--drivers/staging/comedi/drivers/tests/Makefile6
-rw-r--r--drivers/staging/comedi/drivers/tests/example_test.c72
-rw-r--r--drivers/staging/comedi/drivers/tests/unittest.h63
4 files changed, 142 insertions, 0 deletions
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@umich.edu>
+ *
+ * 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@umich.edu>");
+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@umich.edu>
+ * 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 */