Commit 040e9e4d authored by Spencer E. Olson's avatar Spencer E. Olson Committed by Greg Kroah-Hartman
Browse files

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: default avatarSpencer E. Olson <olsonse@umich.edu>
Reviewed-by: default avatarIan Abbott <abbotti@mev.co.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent e0b2ca89
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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/
+6 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0
# Makefile for comedi drivers unit tests
#
ccflags-$(CONFIG_COMEDI_DEBUG)		:= -DDEBUG

obj-$(CONFIG_COMEDI_TESTS)		+= example_test.o
+72 −0
Original line number Diff line number Diff line
// 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 **** */
+63 −0
Original line number Diff line number Diff line
/* 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 */