summaryrefslogtreecommitdiff
path: root/unittests/unittest.c
blob: bc75bb6e0e930c3e864bbd4154c1e1b582f3419f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * fio unittest
 * Copyright (C) 2018 Tomohiro Kusumi <kusumi.tomohiro@osnexus.com>
 */

#include <stdio.h>
#include <stdlib.h>

#include "./unittest.h"

CU_ErrorCode fio_unittest_add_suite(const char *name, CU_InitializeFunc initfn,
	CU_CleanupFunc cleanfn, struct fio_unittest_entry *tvec)
{
	CU_pSuite pSuite;
	struct fio_unittest_entry *t;

	pSuite = CU_add_suite(name, initfn, cleanfn);
	if (!pSuite) {
		CU_cleanup_registry();
		return CU_get_error();
	}

	t = tvec;
	while (t && t->name) {
		if (!CU_add_test(pSuite, t->name, t->fn)) {
			CU_cleanup_registry();
			return CU_get_error();
		}
		t++;
	}

	return CUE_SUCCESS;
}

static void fio_unittest_register(CU_ErrorCode (*fn)(void))
{
	if (fn && fn() != CUE_SUCCESS) {
		fprintf(stderr, "%s\n", CU_get_error_msg());
		exit(1);
	}
}

int main(void)
{
	if (CU_initialize_registry() != CUE_SUCCESS) {
		fprintf(stderr, "%s\n", CU_get_error_msg());
		exit(1);
	}

	/* Register unittest suites. */
	fio_unittest_register(NULL); /* prevent unused warning */

	CU_basic_set_mode(CU_BRM_VERBOSE);
	CU_basic_run_tests();
	CU_cleanup_registry();

	return CU_get_error();
}