xref: /kvm-unit-tests/lib/util.h (revision cd5f2fb4ad641c51fe0f1a85264dc3f6ede6e131)
1e7c68b43SAndrew Jones #ifndef _UTIL_H_
2e7c68b43SAndrew Jones #define _UTIL_H_
3e7c68b43SAndrew Jones /*
4e7c68b43SAndrew Jones  * Collection of utility functions to share between unit tests.
5e7c68b43SAndrew Jones  *
6e7c68b43SAndrew Jones  * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
7e7c68b43SAndrew Jones  *
8e7c68b43SAndrew Jones  * This work is licensed under the terms of the GNU LGPL, version 2.
9e7c68b43SAndrew Jones  */
10e7c68b43SAndrew Jones 
11e7c68b43SAndrew Jones /*
12e7c68b43SAndrew Jones  * parse_keyval extracts the integer from a string formatted as
13e7c68b43SAndrew Jones  * string=integer. This is useful for passing expected values to
14e7c68b43SAndrew Jones  * the unit test on the command line, i.e. it helps parse QEMU
15e7c68b43SAndrew Jones  * command lines that include something like -append var1=1 var2=2
16e7c68b43SAndrew Jones  * @s is the input string, likely a command line parameter, and
17e7c68b43SAndrew Jones  * @val is a pointer to where the integer will be stored.
18e7c68b43SAndrew Jones  *
19e7c68b43SAndrew Jones  * Returns the offset of the '=', or -1 if no keyval pair is found.
20e7c68b43SAndrew Jones  */
21e7c68b43SAndrew Jones extern int parse_keyval(char *s, long *val);
22e7c68b43SAndrew Jones 
23*f563c18dSSean Christopherson #define __TEST_EQ(a, b, a_str, b_str, assertion, do_abort, fmt, args...)		\
24*f563c18dSSean Christopherson do {											\
25*f563c18dSSean Christopherson 	typeof(a) _a = a;								\
26*f563c18dSSean Christopherson 	typeof(b) _b = b;								\
27*f563c18dSSean Christopherson 	if (_a != _b) {									\
28*f563c18dSSean Christopherson 		char _bin_a[BINSTR_SZ];							\
29*f563c18dSSean Christopherson 		char _bin_b[BINSTR_SZ];							\
30*f563c18dSSean Christopherson 		binstr(_a, _bin_a);							\
31*f563c18dSSean Christopherson 		binstr(_b, _bin_b);							\
32*f563c18dSSean Christopherson 		report_fail("%s:%d: %s failed: (%s) == (%s)\n"				\
33*f563c18dSSean Christopherson 			    "\tLHS: %#018lx - %s - %lu\n"				\
34*f563c18dSSean Christopherson 			    "\tRHS: %#018lx - %s - %lu%s" fmt,				\
35*f563c18dSSean Christopherson 			    __FILE__, __LINE__,						\
36*f563c18dSSean Christopherson 			    assertion ? "Assertion" : "Expectation", a_str, b_str,	\
37*f563c18dSSean Christopherson 			    (unsigned long) _a, _bin_a, (unsigned long) _a,		\
38*f563c18dSSean Christopherson 			    (unsigned long) _b, _bin_b, (unsigned long) _b,		\
39*f563c18dSSean Christopherson 			    fmt[0] == '\0' ? "" : "\n", ## args);			\
40*f563c18dSSean Christopherson 		dump_stack();								\
41*f563c18dSSean Christopherson 		if (assertion)								\
42*f563c18dSSean Christopherson 			do_abort();							\
43*f563c18dSSean Christopherson 	}										\
44*f563c18dSSean Christopherson 	report_passed();								\
45*f563c18dSSean Christopherson } while (0)
46*f563c18dSSean Christopherson 
47*f563c18dSSean Christopherson /* FIXME: Extend VMX's assert/abort framework to SVM and other environs. */
dummy_abort(void)48*f563c18dSSean Christopherson static inline void dummy_abort(void) {}
49*f563c18dSSean Christopherson 
50*f563c18dSSean Christopherson #define TEST_EXPECT_EQ(a, b) __TEST_EQ(a, b, #a, #b, 0, dummy_abort, "")
51*f563c18dSSean Christopherson #define TEST_EXPECT_EQ_MSG(a, b, fmt, args...) \
52*f563c18dSSean Christopherson 	__TEST_EQ(a, b, #a, #b, 0, dummy_abort fmt, ## args)
53*f563c18dSSean Christopherson 
54e7c68b43SAndrew Jones #endif
55