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 /*
12 * parse_keyval extracts the integer from a string formatted as
13 * string=integer. This is useful for passing expected values to
14 * the unit test on the command line, i.e. it helps parse QEMU
15 * command lines that include something like -append var1=1 var2=2
16 * @s is the input string, likely a command line parameter, and
17 * @val is a pointer to where the integer will be stored.
18 *
19 * Returns the offset of the '=', or -1 if no keyval pair is found.
20 */
21 extern int parse_keyval(char *s, long *val);
22
23 #define __TEST_EQ(a, b, a_str, b_str, assertion, do_abort, fmt, args...) \
24 do { \
25 typeof(a) _a = a; \
26 typeof(b) _b = b; \
27 if (_a != _b) { \
28 char _bin_a[BINSTR_SZ]; \
29 char _bin_b[BINSTR_SZ]; \
30 binstr(_a, _bin_a); \
31 binstr(_b, _bin_b); \
32 report_fail("%s:%d: %s failed: (%s) == (%s)\n" \
33 "\tLHS: %#018lx - %s - %lu\n" \
34 "\tRHS: %#018lx - %s - %lu%s" fmt, \
35 __FILE__, __LINE__, \
36 assertion ? "Assertion" : "Expectation", a_str, b_str, \
37 (unsigned long) _a, _bin_a, (unsigned long) _a, \
38 (unsigned long) _b, _bin_b, (unsigned long) _b, \
39 fmt[0] == '\0' ? "" : "\n", ## args); \
40 dump_stack(); \
41 if (assertion) \
42 do_abort(); \
43 } \
44 report_passed(); \
45 } while (0)
46
47 /* FIXME: Extend VMX's assert/abort framework to SVM and other environs. */
dummy_abort(void)48 static inline void dummy_abort(void) {}
49
50 #define TEST_EXPECT_EQ(a, b) __TEST_EQ(a, b, #a, #b, 0, dummy_abort, "")
51 #define TEST_EXPECT_EQ_MSG(a, b, fmt, args...) \
52 __TEST_EQ(a, b, #a, #b, 0, dummy_abort fmt, ## args)
53
54 #endif
55