1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* vim: set ts=8 sw=8 noet tw=80 nowrap: */
3 /*
4 * comedi/drivers/tests/unittest.h
5 * Simple framework for unittests for comedi drivers.
6 *
7 * COMEDI - Linux Control and Measurement Device Interface
8 * Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
9 * based of parts of drivers/of/unittest.c
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 */
21
22 #ifndef _COMEDI_DRIVERS_TESTS_UNITTEST_H
23 #define _COMEDI_DRIVERS_TESTS_UNITTEST_H
24
25 static struct unittest_results {
26 int passed;
27 int failed;
28 } unittest_results;
29
30 typedef void *(*unittest_fptr)(void);
31
32 #define unittest(result, fmt, ...) ({ \
33 bool failed = !(result); \
34 if (failed) { \
35 ++unittest_results.failed; \
36 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, \
37 ##__VA_ARGS__); \
38 } else { \
39 ++unittest_results.passed; \
40 pr_debug("pass %s():%i " fmt, __func__, __LINE__, \
41 ##__VA_ARGS__); \
42 } \
43 failed; \
44 })
45
46 /**
47 * Execute an array of unit tests.
48 * @name: Name of set of unit tests--will be shown at INFO log level.
49 * @unit_tests: A null-terminated list of unit tests to execute.
50 */
51 static inline void exec_unittests(const char *name,
52 const unittest_fptr *unit_tests)
53 {
54 pr_info("begin comedi:\"%s\" unittests\n", name);
55
56 for (; (*unit_tests) != NULL; ++unit_tests)
57 (*unit_tests)();
58
59 pr_info("end of comedi:\"%s\" unittests - %i passed, %i failed\n", name,
60 unittest_results.passed, unittest_results.failed);
61 }
62
63 #endif /* _COMEDI_DRIVERS_TESTS_UNITTEST_H */