xref: /kvm-unit-tests/lib/report.c (revision a0837ab6c95ece72b926aca6c245891088836a51)
1 /*
2  * Test result reporting
3  *
4  * Copyright (c) Siemens AG, 2014
5  *
6  * Authors:
7  *  Jan Kiszka <jan.kiszka@siemens.com>
8  *  Andrew Jones <drjones@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU LGPL, version 2.
11  */
12 
13 #include "libcflat.h"
14 
15 static unsigned int tests, failures, xfailures;
16 static char prefixes[256];
17 
18 void report_prefix_push(const char *prefix)
19 {
20 	strcat(prefixes, prefix);
21 	strcat(prefixes, ": ");
22 }
23 
24 void report_prefix_pop(void)
25 {
26 	char *p, *q;
27 
28 	if (!*prefixes)
29 		return;
30 
31 	for (p = prefixes, q = strstr(p, ": ") + 2;
32 			*q;
33 			p = q, q = strstr(p, ": ") + 2)
34 		;
35 	*p = '\0';
36 }
37 
38 void va_report_xfail(const char *msg_fmt, bool xfail, bool cond, va_list va)
39 {
40 	char *pass = xfail ? "XPASS" : "PASS";
41 	char *fail = xfail ? "XFAIL" : "FAIL";
42 	char buf[2000];
43 
44 	tests++;
45 	printf("%s: ", cond ? pass : fail);
46 	puts(prefixes);
47 	vsnprintf(buf, sizeof(buf), msg_fmt, va);
48 	puts(buf);
49 	puts("\n");
50 	if (xfail && cond)
51 		failures++;
52 	else if (xfail)
53 		xfailures++;
54 	else if (!cond)
55 		failures++;
56 }
57 
58 void report(const char *msg_fmt, bool pass, ...)
59 {
60 	va_list va;
61 	va_start(va, pass);
62 	va_report_xfail(msg_fmt, false, pass, va);
63 	va_end(va);
64 }
65 
66 void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...)
67 {
68 	va_list va;
69 	va_start(va, pass);
70 	va_report_xfail(msg_fmt, xfail, pass, va);
71 	va_end(va);
72 }
73 
74 int report_summary(void)
75 {
76 	printf("\nSUMMARY: %d tests, %d unexpected failures", tests, failures);
77 	if (xfailures)
78 		printf(", %d expected failures\n", xfailures);
79 	else
80 		printf("\n");
81 	return failures > 0 ? 1 : 0;
82 }
83