1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Simple unit test library
5 * Copyright (c) 2013 Google, Inc
13 #include <linux/err.h>
14 #include <test/test.h>
16 struct unit_test_state;
19 * ut_fail() - Record failure of a unit test
22 * @fname: Filename where the error occurred
23 * @line: Line number where the error occurred
24 * @func: Function name where the error occurred
25 * @cond: The condition that failed
27 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
28 const char *func, const char *cond);
31 * ut_failf() - Record failure of a unit test
34 * @fname: Filename where the error occurred
35 * @line: Line number where the error occurred
36 * @func: Function name where the error occurred
37 * @cond: The condition that failed
38 * @fmt: printf() format string for the error, followed by args
40 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
41 const char *func, const char *cond, const char *fmt, ...)
42 __attribute__ ((format (__printf__, 6, 7)));
45 * ut_check_console_line() - Check the next console line against expectations
47 * This creates a string and then checks it against the next line of console
48 * output obtained with console_record_readline().
50 * After the function returns, uts->expect_str holds the expected string and
51 * uts->actual_str holds the actual string read from the console.
54 * @fmt: printf() format string for the error, followed by args
55 * @return 0 if OK, other value on error
57 int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
58 __attribute__ ((format (__printf__, 2, 3)));
61 * ut_check_console_end() - Check there is no more console output
63 * After the function returns, uts->actual_str holds the actual string read
67 * @return 0 if OK (console has no output), other value on error
69 int ut_check_console_end(struct unit_test_state *uts);
72 * ut_check_console_dump() - Check that next lines have a print_buffer() dump
74 * This only supports a byte dump.
76 * @total_bytes: Size of the expected dump in bytes`
77 * @return 0 if OK (looks like a dump and the length matches), other value on
80 int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
82 /* Assert that a condition is non-zero */
83 #define ut_assert(cond) \
85 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
86 return CMD_RET_FAILURE; \
89 /* Assert that a condition is non-zero, with printf() string */
90 #define ut_assertf(cond, fmt, args...) \
92 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
94 return CMD_RET_FAILURE; \
97 /* Assert that two int expressions are equal */
98 #define ut_asserteq(expr1, expr2) { \
99 unsigned int _val1 = (expr1), _val2 = (expr2); \
101 if (_val1 != _val2) { \
102 ut_failf(uts, __FILE__, __LINE__, __func__, \
103 #expr1 " == " #expr2, \
104 "Expected %#x (%d), got %#x (%d)", \
105 _val1, _val1, _val2, _val2); \
106 return CMD_RET_FAILURE; \
110 /* Assert that two 64 int expressions are equal */
111 #define ut_asserteq_64(expr1, expr2) { \
112 u64 _val1 = (expr1), _val2 = (expr2); \
114 if (_val1 != _val2) { \
115 ut_failf(uts, __FILE__, __LINE__, __func__, \
116 #expr1 " == " #expr2, \
117 "Expected %#llx (%lld), got %#llx (%lld)", \
118 (unsigned long long)_val1, \
119 (unsigned long long)_val1, \
120 (unsigned long long)_val2, \
121 (unsigned long long)_val2); \
122 return CMD_RET_FAILURE; \
126 /* Assert that two string expressions are equal */
127 #define ut_asserteq_str(expr1, expr2) { \
128 const char *_val1 = (expr1), *_val2 = (expr2); \
130 if (strcmp(_val1, _val2)) { \
131 ut_failf(uts, __FILE__, __LINE__, __func__, \
132 #expr1 " = " #expr2, \
133 "Expected \"%s\", got \"%s\"", _val1, _val2); \
134 return CMD_RET_FAILURE; \
139 * Assert that two string expressions are equal, up to length of the
142 #define ut_asserteq_strn(expr1, expr2) { \
143 const char *_val1 = (expr1), *_val2 = (expr2); \
144 int _len = strlen(_val1); \
146 if (memcmp(_val1, _val2, _len)) { \
147 ut_failf(uts, __FILE__, __LINE__, __func__, \
148 #expr1 " = " #expr2, \
149 "Expected \"%.*s\", got \"%.*s\"", \
150 _len, _val1, _len, _val2); \
151 return CMD_RET_FAILURE; \
155 /* Assert that two memory areas are equal */
156 #define ut_asserteq_mem(expr1, expr2, len) { \
157 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
158 const uint __len = len; \
160 if (memcmp(_val1, _val2, __len)) { \
161 char __buf1[64 + 1] = "\0"; \
162 char __buf2[64 + 1] = "\0"; \
163 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
164 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
165 ut_failf(uts, __FILE__, __LINE__, __func__, \
166 #expr1 " = " #expr2, \
167 "Expected \"%s\", got \"%s\"", \
169 return CMD_RET_FAILURE; \
173 /* Assert that two pointers are equal */
174 #define ut_asserteq_ptr(expr1, expr2) { \
175 const void *_val1 = (expr1), *_val2 = (expr2); \
177 if (_val1 != _val2) { \
178 ut_failf(uts, __FILE__, __LINE__, __func__, \
179 #expr1 " = " #expr2, \
180 "Expected %p, got %p", _val1, _val2); \
181 return CMD_RET_FAILURE; \
185 /* Assert that a pointer is NULL */
186 #define ut_assertnull(expr) { \
187 const void *_val = (expr); \
190 ut_failf(uts, __FILE__, __LINE__, __func__, \
192 "Expected NULL, got %p", _val); \
193 return CMD_RET_FAILURE; \
197 /* Assert that a pointer is not NULL */
198 #define ut_assertnonnull(expr) { \
199 const void *_val = (expr); \
202 ut_failf(uts, __FILE__, __LINE__, __func__, \
204 "Expected non-null, got NULL"); \
205 return CMD_RET_FAILURE; \
209 /* Assert that a pointer is not an error pointer */
210 #define ut_assertok_ptr(expr) { \
211 const void *_val = (expr); \
213 if (IS_ERR(_val)) { \
214 ut_failf(uts, __FILE__, __LINE__, __func__, \
216 "Expected pointer, got error %ld", \
218 return CMD_RET_FAILURE; \
222 /* Assert that an operation succeeds (returns 0) */
223 #define ut_assertok(cond) ut_asserteq(0, cond)
225 /* Assert that the next console output line matches */
226 #define ut_assert_nextline(fmt, args...) \
227 if (ut_check_console_line(uts, fmt, ##args)) { \
228 ut_failf(uts, __FILE__, __LINE__, __func__, \
229 "console", "\nExpected '%s',\n got '%s'", \
230 uts->expect_str, uts->actual_str); \
231 return CMD_RET_FAILURE; \
234 /* Assert that there is no more console output */
235 #define ut_assert_console_end() \
236 if (ut_check_console_end(uts)) { \
237 ut_failf(uts, __FILE__, __LINE__, __func__, \
238 "console", "Expected no more output, got '%s'",\
240 return CMD_RET_FAILURE; \
243 /* Assert that the next lines are print_buffer() dump at an address */
244 #define ut_assert_nextlines_are_dump(total_bytes) \
245 if (ut_check_console_dump(uts, total_bytes)) { \
246 ut_failf(uts, __FILE__, __LINE__, __func__, \
248 "Expected dump of length %x bytes, got '%s'", \
249 total_bytes, uts->actual_str); \
250 return CMD_RET_FAILURE; \
254 * ut_check_free() - Return the number of bytes free in the malloc() pool
258 ulong ut_check_free(void);
261 * ut_check_delta() - Return the number of bytes allocated/freed
263 * @last: Last value from ut_check_free
264 * @return free memory delta from @last; positive means more memory has been
265 * allocated, negative means less has been allocated (i.e. some is freed)
267 long ut_check_delta(ulong last);