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