18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ */
28c2ecf20Sopenharmony_ci#ifndef __KSELFTEST_MODULE_H
38c2ecf20Sopenharmony_ci#define __KSELFTEST_MODULE_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * Test framework for writing test modules to be loaded by kselftest.
98c2ecf20Sopenharmony_ci * See Documentation/dev-tools/kselftest.rst for an example test module.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define KSTM_MODULE_GLOBALS()			\
138c2ecf20Sopenharmony_cistatic unsigned int total_tests __initdata;	\
148c2ecf20Sopenharmony_cistatic unsigned int failed_tests __initdata
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define KSTM_CHECK_ZERO(x) do {						\
178c2ecf20Sopenharmony_ci	total_tests++;							\
188c2ecf20Sopenharmony_ci	if (x) {							\
198c2ecf20Sopenharmony_ci		pr_warn("TC failed at %s:%d\n", __func__, __LINE__);	\
208c2ecf20Sopenharmony_ci		failed_tests++;						\
218c2ecf20Sopenharmony_ci	}								\
228c2ecf20Sopenharmony_ci} while (0)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic inline int kstm_report(unsigned int total_tests, unsigned int failed_tests)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	if (failed_tests == 0)
278c2ecf20Sopenharmony_ci		pr_info("all %u tests passed\n", total_tests);
288c2ecf20Sopenharmony_ci	else
298c2ecf20Sopenharmony_ci		pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	return failed_tests ? -EINVAL : 0;
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define KSTM_MODULE_LOADERS(__module)			\
358c2ecf20Sopenharmony_cistatic int __init __module##_init(void)			\
368c2ecf20Sopenharmony_ci{							\
378c2ecf20Sopenharmony_ci	pr_info("loaded.\n");				\
388c2ecf20Sopenharmony_ci	selftest();					\
398c2ecf20Sopenharmony_ci	return kstm_report(total_tests, failed_tests);	\
408c2ecf20Sopenharmony_ci}							\
418c2ecf20Sopenharmony_cistatic void __exit __module##_exit(void)		\
428c2ecf20Sopenharmony_ci{							\
438c2ecf20Sopenharmony_ci	pr_info("unloaded.\n");				\
448c2ecf20Sopenharmony_ci}							\
458c2ecf20Sopenharmony_cimodule_init(__module##_init);				\
468c2ecf20Sopenharmony_cimodule_exit(__module##_exit)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#endif	/* __KSELFTEST_MODULE_H */
49