xref: /kvm-unit-tests/lib/report.c (revision d95bd8f6c804027984dec6468a57cb47de556d19)
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 #include "asm/spinlock.h"
15 
16 static unsigned int tests, failures, xfailures;
17 static char prefixes[256];
18 static struct spinlock lock;
19 
20 void report_prefix_push(const char *prefix)
21 {
22 	spin_lock(&lock);
23 	strcat(prefixes, prefix);
24 	strcat(prefixes, ": ");
25 	spin_unlock(&lock);
26 }
27 
28 void report_prefix_pop(void)
29 {
30 	char *p, *q;
31 
32 	spin_lock(&lock);
33 
34 	if (!*prefixes)
35 		return;
36 
37 	for (p = prefixes, q = strstr(p, ": ") + 2;
38 			*q;
39 			p = q, q = strstr(p, ": ") + 2)
40 		;
41 	*p = '\0';
42 
43 	spin_unlock(&lock);
44 }
45 
46 void va_report_xfail(const char *msg_fmt, bool xfail, bool cond, va_list va)
47 {
48 	char *pass = xfail ? "XPASS" : "PASS";
49 	char *fail = xfail ? "XFAIL" : "FAIL";
50 	char buf[2000];
51 
52 	spin_lock(&lock);
53 
54 	tests++;
55 	printf("%s: ", cond ? pass : fail);
56 	puts(prefixes);
57 	vsnprintf(buf, sizeof(buf), msg_fmt, va);
58 	puts(buf);
59 	puts("\n");
60 	if (xfail && cond)
61 		failures++;
62 	else if (xfail)
63 		xfailures++;
64 	else if (!cond)
65 		failures++;
66 
67 	spin_unlock(&lock);
68 }
69 
70 void report(const char *msg_fmt, bool pass, ...)
71 {
72 	va_list va;
73 	va_start(va, pass);
74 	va_report_xfail(msg_fmt, false, pass, va);
75 	va_end(va);
76 }
77 
78 void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...)
79 {
80 	va_list va;
81 	va_start(va, pass);
82 	va_report_xfail(msg_fmt, xfail, pass, va);
83 	va_end(va);
84 }
85 
86 int report_summary(void)
87 {
88 	spin_lock(&lock);
89 
90 	printf("\nSUMMARY: %d tests, %d unexpected failures", tests, failures);
91 	if (xfailures)
92 		printf(", %d expected failures\n", xfailures);
93 	else
94 		printf("\n");
95 	return failures > 0 ? 1 : 0;
96 
97 	spin_unlock(&lock);
98 }
99