Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 2 | /* |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 3 | * Simple unit test library |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 8 | #ifndef __TEST_UT_H |
| 9 | #define __TEST_UT_H |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 10 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | c5e7f66 | 2020-04-08 16:57:40 -0600 | [diff] [blame] | 12 | #include <hexdump.h> |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 13 | #include <linux/err.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 14 | #include <test/test.h> |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 15 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | struct unit_test_state; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * ut_fail() - Record failure of a unit test |
| 20 | * |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 21 | * @uts: Test state |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 22 | * @fname: Filename where the error occurred |
| 23 | * @line: Line number where the error occurred |
| 24 | * @func: Function name where the error occurred |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 25 | * @cond: The condition that failed |
| 26 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 27 | void ut_fail(struct unit_test_state *uts, const char *fname, int line, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 28 | const char *func, const char *cond); |
| 29 | |
| 30 | /** |
| 31 | * ut_failf() - Record failure of a unit test |
| 32 | * |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 33 | * @uts: Test state |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 34 | * @fname: Filename where the error occurred |
| 35 | * @line: Line number where the error occurred |
| 36 | * @func: Function name where the error occurred |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 37 | * @cond: The condition that failed |
| 38 | * @fmt: printf() format string for the error, followed by args |
| 39 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 40 | void ut_failf(struct unit_test_state *uts, const char *fname, int line, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 41 | const char *func, const char *cond, const char *fmt, ...) |
| 42 | __attribute__ ((format (__printf__, 6, 7))); |
| 43 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 44 | /** |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 55 | * Return: 0 if OK, other value on error |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 56 | */ |
| 57 | int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) |
| 58 | __attribute__ ((format (__printf__, 2, 3))); |
| 59 | |
| 60 | /** |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 61 | * 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 72 | * Return: 0 if OK, other value on error |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 73 | */ |
| 74 | int 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 81 | * Return: 0 if OK, other value on error |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 82 | */ |
| 83 | int ut_check_skipline(struct unit_test_state *uts); |
| 84 | |
| 85 | /** |
Simon Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 86 | * 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 96 | * Return: 0 if OK, -ENOENT if not found, other value on error |
Simon Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 97 | */ |
| 98 | int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...); |
| 99 | |
| 100 | /** |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 101 | * ut_check_console_end() - Check there is no more console output |
| 102 | * |
| 103 | * After the function returns, uts->actual_str holds the actual string read |
| 104 | * from the console |
| 105 | * |
| 106 | * @uts: Test state |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 107 | * Return: 0 if OK (console has no output), other value on error |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 108 | */ |
| 109 | int ut_check_console_end(struct unit_test_state *uts); |
| 110 | |
| 111 | /** |
| 112 | * ut_check_console_dump() - Check that next lines have a print_buffer() dump |
| 113 | * |
| 114 | * This only supports a byte dump. |
| 115 | * |
| 116 | * @total_bytes: Size of the expected dump in bytes` |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 117 | * Return: 0 if OK (looks like a dump and the length matches), other value on |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 118 | * error |
| 119 | */ |
| 120 | int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 121 | |
| 122 | /* Assert that a condition is non-zero */ |
| 123 | #define ut_assert(cond) \ |
| 124 | if (!(cond)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 125 | ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 126 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* Assert that a condition is non-zero, with printf() string */ |
| 130 | #define ut_assertf(cond, fmt, args...) \ |
| 131 | if (!(cond)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 132 | ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 133 | fmt, ##args); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 134 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Assert that two int expressions are equal */ |
| 138 | #define ut_asserteq(expr1, expr2) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 139 | unsigned int _val1 = (expr1), _val2 = (expr2); \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 140 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 141 | if (_val1 != _val2) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 142 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 143 | #expr1 " == " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 144 | "Expected %#x (%d), got %#x (%d)", \ |
| 145 | _val1, _val1, _val2, _val2); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 146 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 147 | } \ |
| 148 | } |
| 149 | |
Dario Binacchi | 421e81e | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 150 | /* Assert that two 64 int expressions are equal */ |
| 151 | #define ut_asserteq_64(expr1, expr2) { \ |
| 152 | u64 _val1 = (expr1), _val2 = (expr2); \ |
| 153 | \ |
| 154 | if (_val1 != _val2) { \ |
| 155 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 156 | #expr1 " == " #expr2, \ |
| 157 | "Expected %#llx (%lld), got %#llx (%lld)", \ |
| 158 | (unsigned long long)_val1, \ |
| 159 | (unsigned long long)_val1, \ |
| 160 | (unsigned long long)_val2, \ |
| 161 | (unsigned long long)_val2); \ |
| 162 | return CMD_RET_FAILURE; \ |
| 163 | } \ |
| 164 | } |
| 165 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 166 | /* Assert that two string expressions are equal */ |
| 167 | #define ut_asserteq_str(expr1, expr2) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 168 | const char *_val1 = (expr1), *_val2 = (expr2); \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 169 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 170 | if (strcmp(_val1, _val2)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 171 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 172 | #expr1 " = " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 173 | "Expected \"%s\", got \"%s\"", _val1, _val2); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 174 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 175 | } \ |
| 176 | } |
| 177 | |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 178 | /* |
| 179 | * Assert that two string expressions are equal, up to length of the |
| 180 | * first |
| 181 | */ |
| 182 | #define ut_asserteq_strn(expr1, expr2) { \ |
| 183 | const char *_val1 = (expr1), *_val2 = (expr2); \ |
| 184 | int _len = strlen(_val1); \ |
| 185 | \ |
| 186 | if (memcmp(_val1, _val2, _len)) { \ |
| 187 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 188 | #expr1 " = " #expr2, \ |
| 189 | "Expected \"%.*s\", got \"%.*s\"", \ |
| 190 | _len, _val1, _len, _val2); \ |
| 191 | return CMD_RET_FAILURE; \ |
| 192 | } \ |
| 193 | } |
| 194 | |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 195 | /* Assert that two memory areas are equal */ |
| 196 | #define ut_asserteq_mem(expr1, expr2, len) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 197 | const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 198 | const uint __len = len; \ |
| 199 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 200 | if (memcmp(_val1, _val2, __len)) { \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 201 | char __buf1[64 + 1] = "\0"; \ |
| 202 | char __buf2[64 + 1] = "\0"; \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 203 | bin2hex(__buf1, _val1, min(__len, (uint)32)); \ |
| 204 | bin2hex(__buf2, _val2, min(__len, (uint)32)); \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 205 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 206 | #expr1 " = " #expr2, \ |
| 207 | "Expected \"%s\", got \"%s\"", \ |
| 208 | __buf1, __buf2); \ |
| 209 | return CMD_RET_FAILURE; \ |
| 210 | } \ |
| 211 | } |
| 212 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 213 | /* Assert that two pointers are equal */ |
| 214 | #define ut_asserteq_ptr(expr1, expr2) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 215 | const void *_val1 = (expr1), *_val2 = (expr2); \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 216 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 217 | if (_val1 != _val2) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 218 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 219 | #expr1 " = " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 220 | "Expected %p, got %p", _val1, _val2); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 221 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 222 | } \ |
| 223 | } |
| 224 | |
Simon Glass | 7c58b08 | 2020-09-19 18:49:27 -0600 | [diff] [blame] | 225 | /* Assert that two addresses (converted from pointers) are equal */ |
| 226 | #define ut_asserteq_addr(expr1, expr2) { \ |
| 227 | ulong _val1 = map_to_sysmem(expr1); \ |
| 228 | ulong _val2 = map_to_sysmem(expr2); \ |
| 229 | \ |
| 230 | if (_val1 != _val2) { \ |
| 231 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 232 | #expr1 " = " #expr2, \ |
| 233 | "Expected %lx, got %lx", _val1, _val2); \ |
| 234 | return CMD_RET_FAILURE; \ |
| 235 | } \ |
| 236 | } |
| 237 | |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 238 | /* Assert that a pointer is NULL */ |
| 239 | #define ut_assertnull(expr) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 240 | const void *_val = (expr); \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 241 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 242 | if (_val) { \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 243 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 244 | #expr " != NULL", \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 245 | "Expected NULL, got %p", _val); \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 246 | return CMD_RET_FAILURE; \ |
| 247 | } \ |
| 248 | } |
| 249 | |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 250 | /* Assert that a pointer is not NULL */ |
| 251 | #define ut_assertnonnull(expr) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 252 | const void *_val = (expr); \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 253 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 254 | if (!_val) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 255 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 256 | #expr " = NULL", \ |
| 257 | "Expected non-null, got NULL"); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 258 | return CMD_RET_FAILURE; \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 259 | } \ |
| 260 | } |
| 261 | |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 262 | /* Assert that a pointer is not an error pointer */ |
Simon Glass | d21afd5 | 2017-05-18 20:10:00 -0600 | [diff] [blame] | 263 | #define ut_assertok_ptr(expr) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 264 | const void *_val = (expr); \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 265 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 266 | if (IS_ERR(_val)) { \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 267 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 268 | #expr " = NULL", \ |
| 269 | "Expected pointer, got error %ld", \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 270 | PTR_ERR(_val)); \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 271 | return CMD_RET_FAILURE; \ |
| 272 | } \ |
| 273 | } |
| 274 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 275 | /* Assert that an operation succeeds (returns 0) */ |
| 276 | #define ut_assertok(cond) ut_asserteq(0, cond) |
| 277 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 278 | /* Assert that the next console output line matches */ |
| 279 | #define ut_assert_nextline(fmt, args...) \ |
| 280 | if (ut_check_console_line(uts, fmt, ##args)) { \ |
| 281 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 282 | "console", "\nExpected '%s',\n got '%s'", \ |
| 283 | uts->expect_str, uts->actual_str); \ |
| 284 | return CMD_RET_FAILURE; \ |
| 285 | } \ |
| 286 | |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 287 | /* Assert that the next console output line matches up to the length */ |
| 288 | #define ut_assert_nextlinen(fmt, args...) \ |
| 289 | if (ut_check_console_linen(uts, fmt, ##args)) { \ |
| 290 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 291 | "console", "\nExpected '%s',\n got '%s'", \ |
| 292 | uts->expect_str, uts->actual_str); \ |
| 293 | return CMD_RET_FAILURE; \ |
| 294 | } \ |
| 295 | |
| 296 | /* Assert that there is a 'next' console output line, and skip it */ |
| 297 | #define ut_assert_skipline() \ |
| 298 | if (ut_check_skipline(uts)) { \ |
| 299 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 300 | "console", "\nExpected a line, got end"); \ |
| 301 | return CMD_RET_FAILURE; \ |
| 302 | } \ |
| 303 | |
Simon Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 304 | /* Assert that a following console output line matches */ |
| 305 | #define ut_assert_skip_to_line(fmt, args...) \ |
| 306 | if (ut_check_skip_to_line(uts, fmt, ##args)) { \ |
| 307 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 308 | "console", "\nExpected '%s',\n got to '%s'", \ |
| 309 | uts->expect_str, uts->actual_str); \ |
| 310 | return CMD_RET_FAILURE; \ |
| 311 | } \ |
| 312 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 313 | /* Assert that there is no more console output */ |
| 314 | #define ut_assert_console_end() \ |
| 315 | if (ut_check_console_end(uts)) { \ |
| 316 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 317 | "console", "Expected no more output, got '%s'",\ |
| 318 | uts->actual_str); \ |
| 319 | return CMD_RET_FAILURE; \ |
| 320 | } \ |
| 321 | |
| 322 | /* Assert that the next lines are print_buffer() dump at an address */ |
| 323 | #define ut_assert_nextlines_are_dump(total_bytes) \ |
| 324 | if (ut_check_console_dump(uts, total_bytes)) { \ |
| 325 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 326 | "console", \ |
| 327 | "Expected dump of length %x bytes, got '%s'", \ |
| 328 | total_bytes, uts->actual_str); \ |
| 329 | return CMD_RET_FAILURE; \ |
| 330 | } \ |
| 331 | |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 332 | /** |
| 333 | * ut_check_free() - Return the number of bytes free in the malloc() pool |
| 334 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 335 | * Return: bytes free |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 336 | */ |
| 337 | ulong ut_check_free(void); |
| 338 | |
| 339 | /** |
| 340 | * ut_check_delta() - Return the number of bytes allocated/freed |
| 341 | * |
| 342 | * @last: Last value from ut_check_free |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 343 | * Return: free memory delta from @last; positive means more memory has been |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 344 | * allocated, negative means less has been allocated (i.e. some is freed) |
| 345 | */ |
| 346 | long ut_check_delta(ulong last); |
| 347 | |
Simon Glass | b8f0082 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 348 | /** |
| 349 | * ut_silence_console() - Silence the console if requested by the user |
| 350 | * |
| 351 | * This stops test output from appear on the console. It is the default on |
| 352 | * sandbox, unless the -v flag is given. For other boards, this does nothing. |
| 353 | * |
| 354 | * @uts: Test state (in case in future we want to keep state here) |
| 355 | */ |
| 356 | void ut_silence_console(struct unit_test_state *uts); |
| 357 | |
| 358 | /** |
| 359 | * ut_unsilence_console() - Unsilence the console after a test |
| 360 | * |
| 361 | * This restarts console output again and turns off console recording. This |
| 362 | * happens on all boards, including sandbox. |
| 363 | */ |
| 364 | void ut_unsilence_console(struct unit_test_state *uts); |
| 365 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 366 | /** |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 367 | * ut_set_skip_delays() - Sets whether delays should be skipped |
| 368 | * |
| 369 | * Normally functions like mdelay() cause U-Boot to wait for a while. This |
| 370 | * allows all such delays to be skipped on sandbox, to speed up tests |
| 371 | * |
| 372 | * @uts: Test state (in case in future we want to keep state here) |
| 373 | * @skip_delays: true to skip delays, false to process them normally |
| 374 | */ |
| 375 | void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays); |
| 376 | |
| 377 | /** |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 378 | * test_get_state() - Get the active test state |
| 379 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 380 | * Return: the currently active test state, or NULL if none |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 381 | */ |
| 382 | struct unit_test_state *test_get_state(void); |
| 383 | |
| 384 | /** |
| 385 | * test_set_state() - Set the active test state |
| 386 | * |
| 387 | * @uts: Test state to use as currently active test state, or NULL if none |
| 388 | */ |
| 389 | void test_set_state(struct unit_test_state *uts); |
| 390 | |
| 391 | /** |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 392 | * ut_run_tests() - Run a set of tests |
| 393 | * |
| 394 | * This runs the test, handling any preparation and clean-up needed. It prints |
| 395 | * the name of each test before running it. |
| 396 | * |
| 397 | * @category: Category of these tests. This is a string printed at the start to |
| 398 | * announce the the number of tests |
| 399 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 400 | * printed without the prefix, so that it is easier to see the unique part |
| 401 | * of the test name. If NULL, no prefix processing is done |
| 402 | * @tests: List of tests to run |
| 403 | * @count: Number of tests to run |
| 404 | * @select_name: Name of a single test to run (from the list provided). If NULL |
| 405 | * then all tests are run |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 406 | * @runs_per_test: Number of times to run each test (typically 1) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 407 | * Return: 0 if all tests passed, -1 if any failed |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 408 | */ |
| 409 | int ut_run_list(const char *name, const char *prefix, struct unit_test *tests, |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 410 | int count, const char *select_name, int runs_per_test); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 411 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 412 | #endif |