2 * Simple unit test library
4 * Copyright (c) 2013 Google, Inc
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/err.h>
14 struct unit_test_state;
17 * ut_fail() - Record failure of a unit test
20 * @fname: Filename where the error occurred
21 * @line: Line number where the error occurred
22 * @func: Function name where the error occurred
23 * @cond: The condition that failed
25 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
26 const char *func, const char *cond);
29 * ut_failf() - Record failure of a unit test
32 * @fname: Filename where the error occurred
33 * @line: Line number where the error occurred
34 * @func: Function name where the error occurred
35 * @cond: The condition that failed
36 * @fmt: printf() format string for the error, followed by args
38 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
39 const char *func, const char *cond, const char *fmt, ...)
40 __attribute__ ((format (__printf__, 6, 7)));
43 /* Assert that a condition is non-zero */
44 #define ut_assert(cond) \
46 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
47 return CMD_RET_FAILURE; \
50 /* Assert that a condition is non-zero, with printf() string */
51 #define ut_assertf(cond, fmt, args...) \
53 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
55 return CMD_RET_FAILURE; \
58 /* Assert that two int expressions are equal */
59 #define ut_asserteq(expr1, expr2) { \
60 unsigned int val1 = (expr1), val2 = (expr2); \
63 ut_failf(uts, __FILE__, __LINE__, __func__, \
64 #expr1 " == " #expr2, \
65 "Expected %d, got %d", val1, val2); \
66 return CMD_RET_FAILURE; \
70 /* Assert that two string expressions are equal */
71 #define ut_asserteq_str(expr1, expr2) { \
72 const char *val1 = (expr1), *val2 = (expr2); \
74 if (strcmp(val1, val2)) { \
75 ut_failf(uts, __FILE__, __LINE__, __func__, \
76 #expr1 " = " #expr2, \
77 "Expected \"%s\", got \"%s\"", val1, val2); \
78 return CMD_RET_FAILURE; \
82 /* Assert that two pointers are equal */
83 #define ut_asserteq_ptr(expr1, expr2) { \
84 const void *val1 = (expr1), *val2 = (expr2); \
87 ut_failf(uts, __FILE__, __LINE__, __func__, \
88 #expr1 " = " #expr2, \
89 "Expected %p, got %p", val1, val2); \
90 return CMD_RET_FAILURE; \
94 /* Assert that a pointer is not NULL */
95 #define ut_assertnonnull(expr) { \
96 const void *val = (expr); \
99 ut_failf(uts, __FILE__, __LINE__, __func__, \
101 "Expected non-null, got NULL"); \
102 return CMD_RET_FAILURE; \
106 /* Assert that a pointer is not an error pointer */
107 #define ut_assertok_ptr(expr) { \
108 const void *val = (expr); \
111 ut_failf(uts, __FILE__, __LINE__, __func__, \
113 "Expected pointer, got error %ld", \
115 return CMD_RET_FAILURE; \
119 /* Assert that an operation succeeds (returns 0) */
120 #define ut_assertok(cond) ut_asserteq(0, cond)