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