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