1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Simple unit test library
5 * Copyright (c) 2013 Google, Inc
11 #include <linux/err.h>
13 struct unit_test_state;
16 * ut_fail() - Record failure of a unit test
19 * @fname: Filename where the error occurred
20 * @line: Line number where the error occurred
21 * @func: Function name where the error occurred
22 * @cond: The condition that failed
24 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
25 const char *func, const char *cond);
28 * ut_failf() - Record failure of a unit test
31 * @fname: Filename where the error occurred
32 * @line: Line number where the error occurred
33 * @func: Function name where the error occurred
34 * @cond: The condition that failed
35 * @fmt: printf() format string for the error, followed by args
37 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
38 const char *func, const char *cond, const char *fmt, ...)
39 __attribute__ ((format (__printf__, 6, 7)));
42 /* Assert that a condition is non-zero */
43 #define ut_assert(cond) \
45 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
46 return CMD_RET_FAILURE; \
49 /* Assert that a condition is non-zero, with printf() string */
50 #define ut_assertf(cond, fmt, args...) \
52 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
54 return CMD_RET_FAILURE; \
57 /* Assert that two int expressions are equal */
58 #define ut_asserteq(expr1, expr2) { \
59 unsigned int val1 = (expr1), val2 = (expr2); \
62 ut_failf(uts, __FILE__, __LINE__, __func__, \
63 #expr1 " == " #expr2, \
64 "Expected %d, got %d", val1, val2); \
65 return CMD_RET_FAILURE; \
69 /* Assert that two string expressions are equal */
70 #define ut_asserteq_str(expr1, expr2) { \
71 const char *val1 = (expr1), *val2 = (expr2); \
73 if (strcmp(val1, val2)) { \
74 ut_failf(uts, __FILE__, __LINE__, __func__, \
75 #expr1 " = " #expr2, \
76 "Expected \"%s\", got \"%s\"", val1, val2); \
77 return CMD_RET_FAILURE; \
81 /* Assert that two memory areas are equal */
82 #define ut_asserteq_mem(expr1, expr2, len) { \
83 const u8 *val1 = (u8 *)(expr1), *val2 = (u8 *)(expr2); \
84 const uint __len = len; \
86 if (memcmp(val1, val2, __len)) { \
87 char __buf1[64 + 1] = "\0"; \
88 char __buf2[64 + 1] = "\0"; \
89 bin2hex(__buf1, val1, min(__len, (uint)32)); \
90 bin2hex(__buf2, val2, min(__len, (uint)32)); \
91 ut_failf(uts, __FILE__, __LINE__, __func__, \
92 #expr1 " = " #expr2, \
93 "Expected \"%s\", got \"%s\"", \
95 return CMD_RET_FAILURE; \
99 /* Assert that two pointers are equal */
100 #define ut_asserteq_ptr(expr1, expr2) { \
101 const void *val1 = (expr1), *val2 = (expr2); \
103 if (val1 != val2) { \
104 ut_failf(uts, __FILE__, __LINE__, __func__, \
105 #expr1 " = " #expr2, \
106 "Expected %p, got %p", val1, val2); \
107 return CMD_RET_FAILURE; \
111 /* Assert that a pointer is NULL */
112 #define ut_assertnull(expr) { \
113 const void *val = (expr); \
116 ut_failf(uts, __FILE__, __LINE__, __func__, \
118 "Expected NULL, got %p", val); \
119 return CMD_RET_FAILURE; \
123 /* Assert that a pointer is not NULL */
124 #define ut_assertnonnull(expr) { \
125 const void *val = (expr); \
128 ut_failf(uts, __FILE__, __LINE__, __func__, \
130 "Expected non-null, got NULL"); \
131 return CMD_RET_FAILURE; \
135 /* Assert that a pointer is not an error pointer */
136 #define ut_assertok_ptr(expr) { \
137 const void *val = (expr); \
140 ut_failf(uts, __FILE__, __LINE__, __func__, \
142 "Expected pointer, got error %ld", \
144 return CMD_RET_FAILURE; \
148 /* Assert that an operation succeeds (returns 0) */
149 #define ut_assertok(cond) ut_asserteq(0, cond)