staging: comedi: tests: add unittest framework for comedi
authorSpencer E. Olson <olsonse@umich.edu>
Wed, 3 Oct 2018 20:56:00 +0000 (14:56 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 9 Oct 2018 13:32:06 +0000 (15:32 +0200)
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>
drivers/staging/comedi/drivers/Makefile
drivers/staging/comedi/drivers/tests/Makefile [new file with mode: 0644]
drivers/staging/comedi/drivers/tests/example_test.c [new file with mode: 0644]
drivers/staging/comedi/drivers/tests/unittest.h [new file with mode: 0644]

index 98b42b4..8cb5181 100644 (file)
@@ -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 (file)
index 0000000..1d58ede
--- /dev/null
@@ -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 (file)
index 0000000..fc65158
--- /dev/null
@@ -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 (file)
index 0000000..b8e622e
--- /dev/null
@@ -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 */