blob: d3172af8083f4ffeaeeeeef0b19185a1e3c3377a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
Joe Hershberger3a77be52015-05-20 14:27:27 -05003 * Simple unit test library
Simon Glassb2c1cac2014-02-26 15:59:21 -07004 *
5 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07006 */
7
Joe Hershberger3a77be52015-05-20 14:27:27 -05008#ifndef __TEST_UT_H
9#define __TEST_UT_H
Simon Glassb2c1cac2014-02-26 15:59:21 -070010
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Simon Glassc5e7f662020-04-08 16:57:40 -060012#include <hexdump.h>
Simon Glass7e1cebf2015-07-06 12:54:37 -060013#include <linux/err.h>
Simon Glass75c4d412020-07-19 10:15:37 -060014#include <test/test.h>
Simon Glass7e1cebf2015-07-06 12:54:37 -060015
Joe Hershberger3a77be52015-05-20 14:27:27 -050016struct unit_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070017
18/**
19 * ut_fail() - Record failure of a unit test
20 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050021 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070022 * @fname: Filename where the error occurred
23 * @line: Line number where the error occurred
24 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070025 * @cond: The condition that failed
26 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050027void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070028 const char *func, const char *cond);
29
30/**
31 * ut_failf() - Record failure of a unit test
32 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050033 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070034 * @fname: Filename where the error occurred
35 * @line: Line number where the error occurred
36 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070037 * @cond: The condition that failed
38 * @fmt: printf() format string for the error, followed by args
39 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050040void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070041 const char *func, const char *cond, const char *fmt, ...)
42 __attribute__ ((format (__printf__, 6, 7)));
43
Simon Glassd856e912020-01-27 08:49:56 -070044/**
45 * ut_check_console_line() - Check the next console line against expectations
46 *
47 * This creates a string and then checks it against the next line of console
48 * output obtained with console_record_readline().
49 *
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.
52 *
53 * @uts: Test state
54 * @fmt: printf() format string for the error, followed by args
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010055 * Return: 0 if OK, other value on error
Simon Glassd856e912020-01-27 08:49:56 -070056 */
57int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
58 __attribute__ ((format (__printf__, 2, 3)));
59
60/**
Simon Glassc963df92020-07-28 19:41:10 -060061 * ut_check_console_linen() - Check part of the next console line
62 *
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
65 * string is checked
66 *
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.
69 *
70 * @uts: Test state
71 * @fmt: printf() format string for the error, followed by args
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010072 * Return: 0 if OK, other value on error
Simon Glassc963df92020-07-28 19:41:10 -060073 */
74int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
75 __attribute__ ((format (__printf__, 2, 3)));
76
77/**
78 * ut_check_skipline() - Check that the next console line exists and skip it
79 *
80 * @uts: Test state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010081 * Return: 0 if OK, other value on error
Simon Glassc963df92020-07-28 19:41:10 -060082 */
83int ut_check_skipline(struct unit_test_state *uts);
84
85/**
Simon Glass12892b02021-08-18 21:40:33 -060086 * ut_check_skip_to_line() - skip output until a line is found
87 *
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.
90 *
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.
93 *
94 * @uts: Test state
95 * @fmt: printf() format string to look for, followed by args
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010096 * Return: 0 if OK, -ENOENT if not found, other value on error
Simon Glass12892b02021-08-18 21:40:33 -060097 */
98int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...);
99
100/**
Simon Glasse85cd122023-10-01 19:15:13 -0600101 * ut_check_skip_to_linen() - skip output until a partial line is found
102 *
103 * This creates a string and then checks it against the following lines of
104 * console output obtained with console_record_readline() until it is found.
105 * Only the characters up to the length of the string are checked, so the line
106 * may extend further
107 *
108 * After the function returns, uts->expect_str holds the expected string and
109 * uts->actual_str holds the actual string read from the console.
110 *
111 * @uts: Test state
112 * @fmt: printf() format string to look for, followed by args
113 * Return: 0 if OK, -ENOENT if not found, other value on error
114 */
115int ut_check_skip_to_linen(struct unit_test_state *uts, const char *fmt, ...);
116
117/**
Simon Glassd856e912020-01-27 08:49:56 -0700118 * ut_check_console_end() - Check there is no more console output
119 *
120 * After the function returns, uts->actual_str holds the actual string read
121 * from the console
122 *
123 * @uts: Test state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100124 * Return: 0 if OK (console has no output), other value on error
Simon Glassd856e912020-01-27 08:49:56 -0700125 */
126int ut_check_console_end(struct unit_test_state *uts);
127
128/**
129 * ut_check_console_dump() - Check that next lines have a print_buffer() dump
130 *
131 * This only supports a byte dump.
132 *
133 * @total_bytes: Size of the expected dump in bytes`
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100134 * Return: 0 if OK (looks like a dump and the length matches), other value on
Simon Glassd856e912020-01-27 08:49:56 -0700135 * error
136 */
137int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700138
Simon Glassf912a722022-09-06 20:27:27 -0600139/* Report a failure, with printf() string */
140#define ut_reportf(fmt, args...) \
141 ut_failf(uts, __FILE__, __LINE__, __func__, "report", \
142 fmt, ##args)
143
Simon Glassb2c1cac2014-02-26 15:59:21 -0700144/* Assert that a condition is non-zero */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100145#define ut_assert(cond) ({ \
146 int __ret = 0; \
147 \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700148 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500149 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Simon Glassbf61c542023-06-01 10:22:29 -0600150 return CMD_RET_FAILURE; \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100151 } \
152 __ret; \
153})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700154
155/* Assert that a condition is non-zero, with printf() string */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100156#define ut_assertf(cond, fmt, args...) ({ \
157 int __ret = 0; \
158 \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700159 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500160 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700161 fmt, ##args); \
Simon Glassbf61c542023-06-01 10:22:29 -0600162 return CMD_RET_FAILURE; \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100163 } \
164 __ret; \
165})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700166
167/* Assert that two int expressions are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100168#define ut_asserteq(expr1, expr2) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700169 unsigned int _val1 = (expr1), _val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100170 int __ret = 0; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700171 \
Simon Glass43c336b2020-01-27 08:49:41 -0700172 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500173 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700174 #expr1 " == " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700175 "Expected %#x (%d), got %#x (%d)", \
176 _val1, _val1, _val2, _val2); \
Simon Glassbf61c542023-06-01 10:22:29 -0600177 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700178 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100179 __ret; \
180})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700181
Dario Binacchi421e81e2020-03-29 18:04:40 +0200182/* Assert that two 64 int expressions are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100183#define ut_asserteq_64(expr1, expr2) ({ \
Dario Binacchi421e81e2020-03-29 18:04:40 +0200184 u64 _val1 = (expr1), _val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100185 int __ret = 0; \
Dario Binacchi421e81e2020-03-29 18:04:40 +0200186 \
187 if (_val1 != _val2) { \
188 ut_failf(uts, __FILE__, __LINE__, __func__, \
189 #expr1 " == " #expr2, \
190 "Expected %#llx (%lld), got %#llx (%lld)", \
191 (unsigned long long)_val1, \
192 (unsigned long long)_val1, \
193 (unsigned long long)_val2, \
194 (unsigned long long)_val2); \
Simon Glassbf61c542023-06-01 10:22:29 -0600195 return CMD_RET_FAILURE; \
Dario Binacchi421e81e2020-03-29 18:04:40 +0200196 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100197 __ret; \
198})
Dario Binacchi421e81e2020-03-29 18:04:40 +0200199
Simon Glassb2c1cac2014-02-26 15:59:21 -0700200/* Assert that two string expressions are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100201#define ut_asserteq_str(expr1, expr2) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700202 const char *_val1 = (expr1), *_val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100203 int __ret = 0; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700204 \
Simon Glass43c336b2020-01-27 08:49:41 -0700205 if (strcmp(_val1, _val2)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500206 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700207 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700208 "Expected \"%s\", got \"%s\"", _val1, _val2); \
Simon Glassbf61c542023-06-01 10:22:29 -0600209 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700210 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100211 __ret; \
212})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700213
Simon Glass0dc3d512020-07-07 13:11:54 -0600214/*
215 * Assert that two string expressions are equal, up to length of the
216 * first
217 */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100218#define ut_asserteq_strn(expr1, expr2) ({ \
Simon Glass0dc3d512020-07-07 13:11:54 -0600219 const char *_val1 = (expr1), *_val2 = (expr2); \
220 int _len = strlen(_val1); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100221 int __ret = 0; \
Simon Glass0dc3d512020-07-07 13:11:54 -0600222 \
223 if (memcmp(_val1, _val2, _len)) { \
224 ut_failf(uts, __FILE__, __LINE__, __func__, \
225 #expr1 " = " #expr2, \
226 "Expected \"%.*s\", got \"%.*s\"", \
227 _len, _val1, _len, _val2); \
Simon Glassbf61c542023-06-01 10:22:29 -0600228 return CMD_RET_FAILURE; \
Simon Glass0dc3d512020-07-07 13:11:54 -0600229 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100230 __ret; \
231})
Simon Glass0dc3d512020-07-07 13:11:54 -0600232
Mario Sixffdf8ab2018-09-27 09:19:32 +0200233/* Assert that two memory areas are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100234#define ut_asserteq_mem(expr1, expr2, len) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700235 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200236 const uint __len = len; \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100237 int __ret = 0; \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200238 \
Simon Glass43c336b2020-01-27 08:49:41 -0700239 if (memcmp(_val1, _val2, __len)) { \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200240 char __buf1[64 + 1] = "\0"; \
241 char __buf2[64 + 1] = "\0"; \
Simon Glass43c336b2020-01-27 08:49:41 -0700242 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
243 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200244 ut_failf(uts, __FILE__, __LINE__, __func__, \
245 #expr1 " = " #expr2, \
246 "Expected \"%s\", got \"%s\"", \
247 __buf1, __buf2); \
Simon Glassbf61c542023-06-01 10:22:29 -0600248 return CMD_RET_FAILURE; \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200249 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100250 __ret; \
251})
Mario Sixffdf8ab2018-09-27 09:19:32 +0200252
Simon Glassb2c1cac2014-02-26 15:59:21 -0700253/* Assert that two pointers are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100254#define ut_asserteq_ptr(expr1, expr2) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700255 const void *_val1 = (expr1), *_val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100256 int __ret = 0; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700257 \
Simon Glass43c336b2020-01-27 08:49:41 -0700258 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500259 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700260 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700261 "Expected %p, got %p", _val1, _val2); \
Simon Glassbf61c542023-06-01 10:22:29 -0600262 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700263 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100264 __ret; \
265})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700266
Simon Glass7c58b082020-09-19 18:49:27 -0600267/* Assert that two addresses (converted from pointers) are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100268#define ut_asserteq_addr(expr1, expr2) ({ \
Simon Glass7c58b082020-09-19 18:49:27 -0600269 ulong _val1 = map_to_sysmem(expr1); \
270 ulong _val2 = map_to_sysmem(expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100271 int __ret = 0; \
Simon Glass7c58b082020-09-19 18:49:27 -0600272 \
273 if (_val1 != _val2) { \
274 ut_failf(uts, __FILE__, __LINE__, __func__, \
275 #expr1 " = " #expr2, \
276 "Expected %lx, got %lx", _val1, _val2); \
Simon Glassbf61c542023-06-01 10:22:29 -0600277 return CMD_RET_FAILURE; \
Simon Glass7c58b082020-09-19 18:49:27 -0600278 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100279 __ret; \
280})
Simon Glass7c58b082020-09-19 18:49:27 -0600281
Ramon Friedbecc3e02018-06-21 17:47:16 +0300282/* Assert that a pointer is NULL */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100283#define ut_assertnull(expr) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700284 const void *_val = (expr); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100285 int __ret = 0; \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300286 \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100287 if (_val) { \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300288 ut_failf(uts, __FILE__, __LINE__, __func__, \
289 #expr " != NULL", \
Simon Glass43c336b2020-01-27 08:49:41 -0700290 "Expected NULL, got %p", _val); \
Simon Glassbf61c542023-06-01 10:22:29 -0600291 return CMD_RET_FAILURE; \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300292 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100293 __ret; \
294})
Ramon Friedbecc3e02018-06-21 17:47:16 +0300295
Simon Glass7df766e2014-12-10 08:55:55 -0700296/* Assert that a pointer is not NULL */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100297#define ut_assertnonnull(expr) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700298 const void *_val = (expr); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100299 int __ret = 0; \
Simon Glass7df766e2014-12-10 08:55:55 -0700300 \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100301 if (!_val) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500302 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass7df766e2014-12-10 08:55:55 -0700303 #expr " = NULL", \
304 "Expected non-null, got NULL"); \
Simon Glassbf61c542023-06-01 10:22:29 -0600305 return CMD_RET_FAILURE; \
Simon Glass7df766e2014-12-10 08:55:55 -0700306 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100307 __ret; \
308})
Simon Glass7df766e2014-12-10 08:55:55 -0700309
Simon Glass7e1cebf2015-07-06 12:54:37 -0600310/* Assert that a pointer is not an error pointer */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100311#define ut_assertok_ptr(expr) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700312 const void *_val = (expr); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100313 int __ret = 0; \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600314 \
Simon Glass43c336b2020-01-27 08:49:41 -0700315 if (IS_ERR(_val)) { \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600316 ut_failf(uts, __FILE__, __LINE__, __func__, \
317 #expr " = NULL", \
318 "Expected pointer, got error %ld", \
Simon Glass43c336b2020-01-27 08:49:41 -0700319 PTR_ERR(_val)); \
Simon Glassbf61c542023-06-01 10:22:29 -0600320 return CMD_RET_FAILURE; \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600321 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100322 __ret; \
323})
Simon Glass7e1cebf2015-07-06 12:54:37 -0600324
Simon Glassb2c1cac2014-02-26 15:59:21 -0700325/* Assert that an operation succeeds (returns 0) */
326#define ut_assertok(cond) ut_asserteq(0, cond)
327
Simon Glassd856e912020-01-27 08:49:56 -0700328/* Assert that the next console output line matches */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100329#define ut_assert_nextline(fmt, args...) ({ \
330 int __ret = 0; \
331 \
Simon Glassd856e912020-01-27 08:49:56 -0700332 if (ut_check_console_line(uts, fmt, ##args)) { \
333 ut_failf(uts, __FILE__, __LINE__, __func__, \
334 "console", "\nExpected '%s',\n got '%s'", \
335 uts->expect_str, uts->actual_str); \
Simon Glassbf61c542023-06-01 10:22:29 -0600336 return CMD_RET_FAILURE; \
Simon Glassd856e912020-01-27 08:49:56 -0700337 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100338 __ret; \
339})
Simon Glassd856e912020-01-27 08:49:56 -0700340
Simon Glassc963df92020-07-28 19:41:10 -0600341/* Assert that the next console output line matches up to the length */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100342#define ut_assert_nextlinen(fmt, args...) ({ \
343 int __ret = 0; \
344 \
Simon Glassc963df92020-07-28 19:41:10 -0600345 if (ut_check_console_linen(uts, fmt, ##args)) { \
346 ut_failf(uts, __FILE__, __LINE__, __func__, \
347 "console", "\nExpected '%s',\n got '%s'", \
348 uts->expect_str, uts->actual_str); \
Simon Glassbf61c542023-06-01 10:22:29 -0600349 return CMD_RET_FAILURE; \
Simon Glassc963df92020-07-28 19:41:10 -0600350 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100351 __ret; \
352})
Simon Glassc963df92020-07-28 19:41:10 -0600353
354/* Assert that there is a 'next' console output line, and skip it */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100355#define ut_assert_skipline() ({ \
356 int __ret = 0; \
357 \
Simon Glassc963df92020-07-28 19:41:10 -0600358 if (ut_check_skipline(uts)) { \
359 ut_failf(uts, __FILE__, __LINE__, __func__, \
360 "console", "\nExpected a line, got end"); \
Simon Glassbf61c542023-06-01 10:22:29 -0600361 return CMD_RET_FAILURE; \
Simon Glassc963df92020-07-28 19:41:10 -0600362 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100363 __ret; \
364})
Simon Glassc963df92020-07-28 19:41:10 -0600365
Simon Glass12892b02021-08-18 21:40:33 -0600366/* Assert that a following console output line matches */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100367#define ut_assert_skip_to_line(fmt, args...) ({ \
368 int __ret = 0; \
369 \
Simon Glass12892b02021-08-18 21:40:33 -0600370 if (ut_check_skip_to_line(uts, fmt, ##args)) { \
371 ut_failf(uts, __FILE__, __LINE__, __func__, \
372 "console", "\nExpected '%s',\n got to '%s'", \
373 uts->expect_str, uts->actual_str); \
Simon Glassbf61c542023-06-01 10:22:29 -0600374 return CMD_RET_FAILURE; \
Simon Glass12892b02021-08-18 21:40:33 -0600375 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100376 __ret; \
377})
Simon Glass12892b02021-08-18 21:40:33 -0600378
Simon Glasse85cd122023-10-01 19:15:13 -0600379/* Assert that a following console output line matches */
380#define ut_assert_skip_to_linen(fmt, args...) ({ \
381 int __ret = 0; \
382 \
383 if (ut_check_skip_to_linen(uts, fmt, ##args)) { \
384 ut_failf(uts, __FILE__, __LINE__, __func__, \
385 "console", "\nExpected '%s',\n got to '%s'", \
386 uts->expect_str, uts->actual_str); \
387 return CMD_RET_FAILURE; \
388 } \
389 __ret; \
390})
391
Simon Glassd856e912020-01-27 08:49:56 -0700392/* Assert that there is no more console output */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100393#define ut_assert_console_end() ({ \
394 int __ret = 0; \
395 \
Simon Glassd856e912020-01-27 08:49:56 -0700396 if (ut_check_console_end(uts)) { \
397 ut_failf(uts, __FILE__, __LINE__, __func__, \
398 "console", "Expected no more output, got '%s'",\
399 uts->actual_str); \
Simon Glassbf61c542023-06-01 10:22:29 -0600400 return CMD_RET_FAILURE; \
Simon Glassd856e912020-01-27 08:49:56 -0700401 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100402 __ret; \
403})
Simon Glassd856e912020-01-27 08:49:56 -0700404
405/* Assert that the next lines are print_buffer() dump at an address */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100406#define ut_assert_nextlines_are_dump(total_bytes) ({ \
407 int __ret = 0; \
408 \
Simon Glassd856e912020-01-27 08:49:56 -0700409 if (ut_check_console_dump(uts, total_bytes)) { \
410 ut_failf(uts, __FILE__, __LINE__, __func__, \
411 "console", \
412 "Expected dump of length %x bytes, got '%s'", \
413 total_bytes, uts->actual_str); \
Simon Glassbf61c542023-06-01 10:22:29 -0600414 return CMD_RET_FAILURE; \
Simon Glassd856e912020-01-27 08:49:56 -0700415 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100416 __ret; \
417})
Simon Glassd856e912020-01-27 08:49:56 -0700418
Marek Vasut8abbbf22023-03-02 04:08:24 +0100419/* Assert that the next console output line is empty */
420#define ut_assert_nextline_empty() \
421 ut_assert_nextline("%s", "")
422
Simon Glass19920d72019-12-29 21:19:23 -0700423/**
424 * ut_check_free() - Return the number of bytes free in the malloc() pool
425 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100426 * Return: bytes free
Simon Glass19920d72019-12-29 21:19:23 -0700427 */
428ulong ut_check_free(void);
429
430/**
431 * ut_check_delta() - Return the number of bytes allocated/freed
432 *
433 * @last: Last value from ut_check_free
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100434 * Return: free memory delta from @last; positive means more memory has been
Simon Glass19920d72019-12-29 21:19:23 -0700435 * allocated, negative means less has been allocated (i.e. some is freed)
436 */
437long ut_check_delta(ulong last);
438
Simon Glassb8f00822020-11-08 21:08:43 -0700439/**
440 * ut_silence_console() - Silence the console if requested by the user
441 *
442 * This stops test output from appear on the console. It is the default on
443 * sandbox, unless the -v flag is given. For other boards, this does nothing.
444 *
445 * @uts: Test state (in case in future we want to keep state here)
446 */
447void ut_silence_console(struct unit_test_state *uts);
448
449/**
450 * ut_unsilence_console() - Unsilence the console after a test
451 *
452 * This restarts console output again and turns off console recording. This
453 * happens on all boards, including sandbox.
454 */
455void ut_unsilence_console(struct unit_test_state *uts);
456
Simon Glass5722fb22021-03-07 17:34:47 -0700457/**
Simon Glass59cad962021-03-07 17:34:55 -0700458 * ut_set_skip_delays() - Sets whether delays should be skipped
459 *
460 * Normally functions like mdelay() cause U-Boot to wait for a while. This
461 * allows all such delays to be skipped on sandbox, to speed up tests
462 *
463 * @uts: Test state (in case in future we want to keep state here)
464 * @skip_delays: true to skip delays, false to process them normally
465 */
466void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
467
468/**
Simon Glass4066d8d2021-03-07 17:35:04 -0700469 * test_get_state() - Get the active test state
470 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100471 * Return: the currently active test state, or NULL if none
Simon Glass4066d8d2021-03-07 17:35:04 -0700472 */
473struct unit_test_state *test_get_state(void);
474
475/**
476 * test_set_state() - Set the active test state
477 *
478 * @uts: Test state to use as currently active test state, or NULL if none
479 */
480void test_set_state(struct unit_test_state *uts);
481
482/**
Simon Glass5722fb22021-03-07 17:34:47 -0700483 * ut_run_tests() - Run a set of tests
484 *
485 * This runs the test, handling any preparation and clean-up needed. It prints
486 * the name of each test before running it.
487 *
488 * @category: Category of these tests. This is a string printed at the start to
489 * announce the the number of tests
490 * @prefix: String prefix for the tests. Any tests that have this prefix will be
491 * printed without the prefix, so that it is easier to see the unique part
492 * of the test name. If NULL, no prefix processing is done
493 * @tests: List of tests to run
494 * @count: Number of tests to run
495 * @select_name: Name of a single test to run (from the list provided). If NULL
496 * then all tests are run
Simon Glass91a187b2022-08-01 07:58:45 -0600497 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glass1f1614b2022-10-20 18:22:50 -0600498 * @force_run: Run tests that are marked as manual-only (UT_TESTF_MANUAL)
Simon Glass85ba7c32022-10-29 19:47:13 -0600499 * @test_insert: String describing a test to run after n other tests run, in the
500 * format n:name where n is the number of tests to run before this one and
501 * name is the name of the test to run. This is used to find which test causes
502 * another test to fail. If the one test fails, testing stops immediately.
503 * Pass NULL to disable this
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100504 * Return: 0 if all tests passed, -1 if any failed
Simon Glass5722fb22021-03-07 17:34:47 -0700505 */
506int ut_run_list(const char *name, const char *prefix, struct unit_test *tests,
Simon Glass1f1614b2022-10-20 18:22:50 -0600507 int count, const char *select_name, int runs_per_test,
Simon Glass85ba7c32022-10-29 19:47:13 -0600508 bool force_run, const char *test_insert);
Simon Glass5722fb22021-03-07 17:34:47 -0700509
Simon Glassb2c1cac2014-02-26 15:59:21 -0700510#endif