]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - include/test/ut.h
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_linen() - Check part of the next console line
63 * This creates a string and then checks it against the next line of console
64 * output obtained with console_record_readline(). Only the length of the
67 * After the function returns, uts->expect_str holds the expected string and
68 * uts->actual_str holds the actual string read from the console.
71 * @fmt: printf() format string for the error, followed by args
72 * Return: 0 if OK, other value on error
74 int ut_check_console_linen(struct unit_test_state
*uts
, const char *fmt
, ...)
75 __attribute__ ((format (__printf__
, 2, 3)));
78 * ut_check_skipline() - Check that the next console line exists and skip it
81 * Return: 0 if OK, other value on error
83 int ut_check_skipline(struct unit_test_state
*uts
);
86 * ut_check_skip_to_line() - skip output until a line is found
88 * This creates a string and then checks it against the following lines of
89 * console output obtained with console_record_readline() until it is found.
91 * After the function returns, uts->expect_str holds the expected string and
92 * uts->actual_str holds the actual string read from the console.
95 * @fmt: printf() format string to look for, followed by args
96 * Return: 0 if OK, -ENOENT if not found, other value on error
98 int ut_check_skip_to_line(struct unit_test_state
*uts
, const char *fmt
, ...);
101 * ut_check_console_end() - Check there is no more console output
103 * After the function returns, uts->actual_str holds the actual string read
107 * Return: 0 if OK (console has no output), other value on error
109 int ut_check_console_end(struct unit_test_state
*uts
);
112 * ut_check_console_dump() - Check that next lines have a print_buffer() dump
114 * This only supports a byte dump.
116 * @total_bytes: Size of the expected dump in bytes`
117 * Return: 0 if OK (looks like a dump and the length matches), other value on
120 int ut_check_console_dump(struct unit_test_state
*uts
, int total_bytes
);
122 /* Report a failure, with printf() string */
123 #define ut_reportf(fmt, args...) \
124 ut_failf(uts, __FILE__, __LINE__, __func__, "report", \
127 /* Assert that a condition is non-zero */
128 #define ut_assert(cond) ({ \
132 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
133 __ret = CMD_RET_FAILURE; \
138 /* Assert that a condition is non-zero, with printf() string */
139 #define ut_assertf(cond, fmt, args...) ({ \
143 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
145 __ret = CMD_RET_FAILURE; \
150 /* Assert that two int expressions are equal */
151 #define ut_asserteq(expr1, expr2) ({ \
152 unsigned int _val1 = (expr1), _val2 = (expr2); \
155 if (_val1 != _val2) { \
156 ut_failf(uts, __FILE__, __LINE__, __func__, \
157 #expr1 " == " #expr2, \
158 "Expected %#x (%d), got %#x (%d)", \
159 _val1, _val1, _val2, _val2); \
160 __ret = CMD_RET_FAILURE; \
165 /* Assert that two 64 int expressions are equal */
166 #define ut_asserteq_64(expr1, expr2) ({ \
167 u64 _val1 = (expr1), _val2 = (expr2); \
170 if (_val1 != _val2) { \
171 ut_failf(uts, __FILE__, __LINE__, __func__, \
172 #expr1 " == " #expr2, \
173 "Expected %#llx (%lld), got %#llx (%lld)", \
174 (unsigned long long)_val1, \
175 (unsigned long long)_val1, \
176 (unsigned long long)_val2, \
177 (unsigned long long)_val2); \
178 __ret = CMD_RET_FAILURE; \
183 /* Assert that two string expressions are equal */
184 #define ut_asserteq_str(expr1, expr2) ({ \
185 const char *_val1 = (expr1), *_val2 = (expr2); \
188 if (strcmp(_val1, _val2)) { \
189 ut_failf(uts, __FILE__, __LINE__, __func__, \
190 #expr1 " = " #expr2, \
191 "Expected \"%s\", got \"%s\"", _val1, _val2); \
192 __ret = CMD_RET_FAILURE; \
198 * Assert that two string expressions are equal, up to length of the
201 #define ut_asserteq_strn(expr1, expr2) ({ \
202 const char *_val1 = (expr1), *_val2 = (expr2); \
203 int _len = strlen(_val1); \
206 if (memcmp(_val1, _val2, _len)) { \
207 ut_failf(uts, __FILE__, __LINE__, __func__, \
208 #expr1 " = " #expr2, \
209 "Expected \"%.*s\", got \"%.*s\"", \
210 _len, _val1, _len, _val2); \
211 __ret = CMD_RET_FAILURE; \
216 /* Assert that two memory areas are equal */
217 #define ut_asserteq_mem(expr1, expr2, len) ({ \
218 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
219 const uint __len = len; \
222 if (memcmp(_val1, _val2, __len)) { \
223 char __buf1[64 + 1] = "\0"; \
224 char __buf2[64 + 1] = "\0"; \
225 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
226 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
227 ut_failf(uts, __FILE__, __LINE__, __func__, \
228 #expr1 " = " #expr2, \
229 "Expected \"%s\", got \"%s\"", \
231 __ret = CMD_RET_FAILURE; \
236 /* Assert that two pointers are equal */
237 #define ut_asserteq_ptr(expr1, expr2) ({ \
238 const void *_val1 = (expr1), *_val2 = (expr2); \
241 if (_val1 != _val2) { \
242 ut_failf(uts, __FILE__, __LINE__, __func__, \
243 #expr1 " = " #expr2, \
244 "Expected %p, got %p", _val1, _val2); \
245 __ret = CMD_RET_FAILURE; \
250 /* Assert that two addresses (converted from pointers) are equal */
251 #define ut_asserteq_addr(expr1, expr2) ({ \
252 ulong _val1 = map_to_sysmem(expr1); \
253 ulong _val2 = map_to_sysmem(expr2); \
256 if (_val1 != _val2) { \
257 ut_failf(uts, __FILE__, __LINE__, __func__, \
258 #expr1 " = " #expr2, \
259 "Expected %lx, got %lx", _val1, _val2); \
260 __ret = CMD_RET_FAILURE; \
265 /* Assert that a pointer is NULL */
266 #define ut_assertnull(expr) ({ \
267 const void *_val = (expr); \
271 ut_failf(uts, __FILE__, __LINE__, __func__, \
273 "Expected NULL, got %p", _val); \
274 __ret = CMD_RET_FAILURE; \
279 /* Assert that a pointer is not NULL */
280 #define ut_assertnonnull(expr) ({ \
281 const void *_val = (expr); \
285 ut_failf(uts, __FILE__, __LINE__, __func__, \
287 "Expected non-null, got NULL"); \
288 __ret = CMD_RET_FAILURE; \
293 /* Assert that a pointer is not an error pointer */
294 #define ut_assertok_ptr(expr) ({ \
295 const void *_val = (expr); \
298 if (IS_ERR(_val)) { \
299 ut_failf(uts, __FILE__, __LINE__, __func__, \
301 "Expected pointer, got error %ld", \
303 __ret = CMD_RET_FAILURE; \
308 /* Assert that an operation succeeds (returns 0) */
309 #define ut_assertok(cond) ut_asserteq(0, cond)
311 /* Assert that the next console output line matches */
312 #define ut_assert_nextline(fmt, args...) ({ \
315 if (ut_check_console_line(uts, fmt, ##args)) { \
316 ut_failf(uts, __FILE__, __LINE__, __func__, \
317 "console", "\nExpected '%s',\n got '%s'", \
318 uts->expect_str, uts->actual_str); \
319 __ret = CMD_RET_FAILURE; \
324 /* Assert that the next console output line matches up to the length */
325 #define ut_assert_nextlinen(fmt, args...) ({ \
328 if (ut_check_console_linen(uts, fmt, ##args)) { \
329 ut_failf(uts, __FILE__, __LINE__, __func__, \
330 "console", "\nExpected '%s',\n got '%s'", \
331 uts->expect_str, uts->actual_str); \
332 __ret = CMD_RET_FAILURE; \
337 /* Assert that there is a 'next' console output line, and skip it */
338 #define ut_assert_skipline() ({ \
341 if (ut_check_skipline(uts)) { \
342 ut_failf(uts, __FILE__, __LINE__, __func__, \
343 "console", "\nExpected a line, got end"); \
344 __ret = CMD_RET_FAILURE; \
349 /* Assert that a following console output line matches */
350 #define ut_assert_skip_to_line(fmt, args...) ({ \
353 if (ut_check_skip_to_line(uts, fmt, ##args)) { \
354 ut_failf(uts, __FILE__, __LINE__, __func__, \
355 "console", "\nExpected '%s',\n got to '%s'", \
356 uts->expect_str, uts->actual_str); \
357 __ret = CMD_RET_FAILURE; \
362 /* Assert that there is no more console output */
363 #define ut_assert_console_end() ({ \
366 if (ut_check_console_end(uts)) { \
367 ut_failf(uts, __FILE__, __LINE__, __func__, \
368 "console", "Expected no more output, got '%s'",\
370 __ret = CMD_RET_FAILURE; \
375 /* Assert that the next lines are print_buffer() dump at an address */
376 #define ut_assert_nextlines_are_dump(total_bytes) ({ \
379 if (ut_check_console_dump(uts, total_bytes)) { \
380 ut_failf(uts, __FILE__, __LINE__, __func__, \
382 "Expected dump of length %x bytes, got '%s'", \
383 total_bytes, uts->actual_str); \
384 __ret = CMD_RET_FAILURE; \
389 /* Assert that the next console output line is empty */
390 #define ut_assert_nextline_empty() \
391 ut_assert_nextline("%s", "")
394 * ut_check_free() - Return the number of bytes free in the malloc() pool
398 ulong
ut_check_free(void);
401 * ut_check_delta() - Return the number of bytes allocated/freed
403 * @last: Last value from ut_check_free
404 * Return: free memory delta from @last; positive means more memory has been
405 * allocated, negative means less has been allocated (i.e. some is freed)
407 long ut_check_delta(ulong last
);
410 * ut_silence_console() - Silence the console if requested by the user
412 * This stops test output from appear on the console. It is the default on
413 * sandbox, unless the -v flag is given. For other boards, this does nothing.
415 * @uts: Test state (in case in future we want to keep state here)
417 void ut_silence_console(struct unit_test_state
*uts
);
420 * ut_unsilence_console() - Unsilence the console after a test
422 * This restarts console output again and turns off console recording. This
423 * happens on all boards, including sandbox.
425 void ut_unsilence_console(struct unit_test_state
*uts
);
428 * ut_set_skip_delays() - Sets whether delays should be skipped
430 * Normally functions like mdelay() cause U-Boot to wait for a while. This
431 * allows all such delays to be skipped on sandbox, to speed up tests
433 * @uts: Test state (in case in future we want to keep state here)
434 * @skip_delays: true to skip delays, false to process them normally
436 void ut_set_skip_delays(struct unit_test_state
*uts
, bool skip_delays
);
439 * test_get_state() - Get the active test state
441 * Return: the currently active test state, or NULL if none
443 struct unit_test_state
*test_get_state(void);
446 * test_set_state() - Set the active test state
448 * @uts: Test state to use as currently active test state, or NULL if none
450 void test_set_state(struct unit_test_state
*uts
);
453 * ut_run_tests() - Run a set of tests
455 * This runs the test, handling any preparation and clean-up needed. It prints
456 * the name of each test before running it.
458 * @category: Category of these tests. This is a string printed at the start to
459 * announce the the number of tests
460 * @prefix: String prefix for the tests. Any tests that have this prefix will be
461 * printed without the prefix, so that it is easier to see the unique part
462 * of the test name. If NULL, no prefix processing is done
463 * @tests: List of tests to run
464 * @count: Number of tests to run
465 * @select_name: Name of a single test to run (from the list provided). If NULL
466 * then all tests are run
467 * @runs_per_test: Number of times to run each test (typically 1)
468 * @force_run: Run tests that are marked as manual-only (UT_TESTF_MANUAL)
469 * @test_insert: String describing a test to run after n other tests run, in the
470 * format n:name where n is the number of tests to run before this one and
471 * name is the name of the test to run. This is used to find which test causes
472 * another test to fail. If the one test fails, testing stops immediately.
473 * Pass NULL to disable this
474 * Return: 0 if all tests passed, -1 if any failed
476 int ut_run_list(const char *name
, const char *prefix
, struct unit_test
*tests
,
477 int count
, const char *select_name
, int runs_per_test
,
478 bool force_run
, const char *test_insert
);