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 | e85cd12 | 2023-10-01 19:15:13 -0600 | [diff] [blame] | 101 | * 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 | */ |
| 115 | int ut_check_skip_to_linen(struct unit_test_state *uts, const char *fmt, ...); |
| 116 | |
| 117 | /** |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 118 | * 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 124 | * Return: 0 if OK (console has no output), other value on error |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 125 | */ |
| 126 | int 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 134 | * 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] | 135 | * error |
| 136 | */ |
| 137 | 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] | 138 | |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 139 | /* 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 Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 144 | /* Assert that a condition is non-zero */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 145 | #define ut_assert(cond) ({ \ |
| 146 | int __ret = 0; \ |
| 147 | \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 148 | if (!(cond)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 149 | ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 150 | return CMD_RET_FAILURE; \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 151 | } \ |
| 152 | __ret; \ |
| 153 | }) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 154 | |
| 155 | /* Assert that a condition is non-zero, with printf() string */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 156 | #define ut_assertf(cond, fmt, args...) ({ \ |
| 157 | int __ret = 0; \ |
| 158 | \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 159 | if (!(cond)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 160 | ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 161 | fmt, ##args); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 162 | return CMD_RET_FAILURE; \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 163 | } \ |
| 164 | __ret; \ |
| 165 | }) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 166 | |
| 167 | /* Assert that two int expressions are equal */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 168 | #define ut_asserteq(expr1, expr2) ({ \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 169 | unsigned int _val1 = (expr1), _val2 = (expr2); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 170 | int __ret = 0; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 171 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 172 | if (_val1 != _val2) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 173 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 174 | #expr1 " == " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 175 | "Expected %#x (%d), got %#x (%d)", \ |
| 176 | _val1, _val1, _val2, _val2); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 177 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 178 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 179 | __ret; \ |
| 180 | }) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 181 | |
Dario Binacchi | 421e81e | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 182 | /* Assert that two 64 int expressions are equal */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 183 | #define ut_asserteq_64(expr1, expr2) ({ \ |
Dario Binacchi | 421e81e | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 184 | u64 _val1 = (expr1), _val2 = (expr2); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 185 | int __ret = 0; \ |
Dario Binacchi | 421e81e | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 186 | \ |
| 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 Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 195 | return CMD_RET_FAILURE; \ |
Dario Binacchi | 421e81e | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 196 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 197 | __ret; \ |
| 198 | }) |
Dario Binacchi | 421e81e | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 199 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 200 | /* Assert that two string expressions are equal */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 201 | #define ut_asserteq_str(expr1, expr2) ({ \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 202 | const char *_val1 = (expr1), *_val2 = (expr2); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 203 | int __ret = 0; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 204 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 205 | if (strcmp(_val1, _val2)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 206 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 207 | #expr1 " = " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 208 | "Expected \"%s\", got \"%s\"", _val1, _val2); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 209 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 210 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 211 | __ret; \ |
| 212 | }) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 213 | |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 214 | /* |
| 215 | * Assert that two string expressions are equal, up to length of the |
| 216 | * first |
| 217 | */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 218 | #define ut_asserteq_strn(expr1, expr2) ({ \ |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 219 | const char *_val1 = (expr1), *_val2 = (expr2); \ |
| 220 | int _len = strlen(_val1); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 221 | int __ret = 0; \ |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 222 | \ |
| 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 Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 228 | return CMD_RET_FAILURE; \ |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 229 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 230 | __ret; \ |
| 231 | }) |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 232 | |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 233 | /* Assert that two memory areas are equal */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 234 | #define ut_asserteq_mem(expr1, expr2, len) ({ \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 235 | const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 236 | const uint __len = len; \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 237 | int __ret = 0; \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 238 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 239 | if (memcmp(_val1, _val2, __len)) { \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 240 | char __buf1[64 + 1] = "\0"; \ |
| 241 | char __buf2[64 + 1] = "\0"; \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 242 | bin2hex(__buf1, _val1, min(__len, (uint)32)); \ |
| 243 | bin2hex(__buf2, _val2, min(__len, (uint)32)); \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 244 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 245 | #expr1 " = " #expr2, \ |
| 246 | "Expected \"%s\", got \"%s\"", \ |
| 247 | __buf1, __buf2); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 248 | return CMD_RET_FAILURE; \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 249 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 250 | __ret; \ |
| 251 | }) |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 252 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 253 | /* Assert that two pointers are equal */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 254 | #define ut_asserteq_ptr(expr1, expr2) ({ \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 255 | const void *_val1 = (expr1), *_val2 = (expr2); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 256 | int __ret = 0; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 257 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 258 | if (_val1 != _val2) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 259 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 260 | #expr1 " = " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 261 | "Expected %p, got %p", _val1, _val2); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 262 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 263 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 264 | __ret; \ |
| 265 | }) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 266 | |
Simon Glass | 7c58b08 | 2020-09-19 18:49:27 -0600 | [diff] [blame] | 267 | /* Assert that two addresses (converted from pointers) are equal */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 268 | #define ut_asserteq_addr(expr1, expr2) ({ \ |
Simon Glass | 7c58b08 | 2020-09-19 18:49:27 -0600 | [diff] [blame] | 269 | ulong _val1 = map_to_sysmem(expr1); \ |
| 270 | ulong _val2 = map_to_sysmem(expr2); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 271 | int __ret = 0; \ |
Simon Glass | 7c58b08 | 2020-09-19 18:49:27 -0600 | [diff] [blame] | 272 | \ |
| 273 | if (_val1 != _val2) { \ |
| 274 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 275 | #expr1 " = " #expr2, \ |
| 276 | "Expected %lx, got %lx", _val1, _val2); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 277 | return CMD_RET_FAILURE; \ |
Simon Glass | 7c58b08 | 2020-09-19 18:49:27 -0600 | [diff] [blame] | 278 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 279 | __ret; \ |
| 280 | }) |
Simon Glass | 7c58b08 | 2020-09-19 18:49:27 -0600 | [diff] [blame] | 281 | |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 282 | /* Assert that a pointer is NULL */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 283 | #define ut_assertnull(expr) ({ \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 284 | const void *_val = (expr); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 285 | int __ret = 0; \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 286 | \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 287 | if (_val) { \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 288 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 289 | #expr " != NULL", \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 290 | "Expected NULL, got %p", _val); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 291 | return CMD_RET_FAILURE; \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 292 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 293 | __ret; \ |
| 294 | }) |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 295 | |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 296 | /* Assert that a pointer is not NULL */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 297 | #define ut_assertnonnull(expr) ({ \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 298 | const void *_val = (expr); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 299 | int __ret = 0; \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 300 | \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 301 | if (!_val) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 302 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 303 | #expr " = NULL", \ |
| 304 | "Expected non-null, got NULL"); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 305 | return CMD_RET_FAILURE; \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 306 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 307 | __ret; \ |
| 308 | }) |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 309 | |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 310 | /* Assert that a pointer is not an error pointer */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 311 | #define ut_assertok_ptr(expr) ({ \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 312 | const void *_val = (expr); \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 313 | int __ret = 0; \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 314 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 315 | if (IS_ERR(_val)) { \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 316 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 317 | #expr " = NULL", \ |
| 318 | "Expected pointer, got error %ld", \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 319 | PTR_ERR(_val)); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 320 | return CMD_RET_FAILURE; \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 321 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 322 | __ret; \ |
| 323 | }) |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 324 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 325 | /* Assert that an operation succeeds (returns 0) */ |
| 326 | #define ut_assertok(cond) ut_asserteq(0, cond) |
| 327 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 328 | /* Assert that the next console output line matches */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 329 | #define ut_assert_nextline(fmt, args...) ({ \ |
| 330 | int __ret = 0; \ |
| 331 | \ |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 332 | 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 Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 336 | return CMD_RET_FAILURE; \ |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 337 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 338 | __ret; \ |
| 339 | }) |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 340 | |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 341 | /* Assert that the next console output line matches up to the length */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 342 | #define ut_assert_nextlinen(fmt, args...) ({ \ |
| 343 | int __ret = 0; \ |
| 344 | \ |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 345 | 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 Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 349 | return CMD_RET_FAILURE; \ |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 350 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 351 | __ret; \ |
| 352 | }) |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 353 | |
| 354 | /* Assert that there is a 'next' console output line, and skip it */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 355 | #define ut_assert_skipline() ({ \ |
| 356 | int __ret = 0; \ |
| 357 | \ |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 358 | if (ut_check_skipline(uts)) { \ |
| 359 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 360 | "console", "\nExpected a line, got end"); \ |
Simon Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 361 | return CMD_RET_FAILURE; \ |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 362 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 363 | __ret; \ |
| 364 | }) |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 365 | |
Simon Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 366 | /* Assert that a following console output line matches */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 367 | #define ut_assert_skip_to_line(fmt, args...) ({ \ |
| 368 | int __ret = 0; \ |
| 369 | \ |
Simon Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 370 | 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 Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 374 | return CMD_RET_FAILURE; \ |
Simon Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 375 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 376 | __ret; \ |
| 377 | }) |
Simon Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 378 | |
Simon Glass | e85cd12 | 2023-10-01 19:15:13 -0600 | [diff] [blame] | 379 | /* 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 Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 392 | /* Assert that there is no more console output */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 393 | #define ut_assert_console_end() ({ \ |
| 394 | int __ret = 0; \ |
| 395 | \ |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 396 | 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 Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 400 | return CMD_RET_FAILURE; \ |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 401 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 402 | __ret; \ |
| 403 | }) |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 404 | |
| 405 | /* Assert that the next lines are print_buffer() dump at an address */ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 406 | #define ut_assert_nextlines_are_dump(total_bytes) ({ \ |
| 407 | int __ret = 0; \ |
| 408 | \ |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 409 | 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 Glass | bf61c54 | 2023-06-01 10:22:29 -0600 | [diff] [blame] | 414 | return CMD_RET_FAILURE; \ |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 415 | } \ |
Marek Vasut | 1531c4e | 2023-03-10 04:33:13 +0100 | [diff] [blame] | 416 | __ret; \ |
| 417 | }) |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 418 | |
Marek Vasut | 8abbbf2 | 2023-03-02 04:08:24 +0100 | [diff] [blame] | 419 | /* Assert that the next console output line is empty */ |
| 420 | #define ut_assert_nextline_empty() \ |
| 421 | ut_assert_nextline("%s", "") |
| 422 | |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 423 | /** |
| 424 | * ut_check_free() - Return the number of bytes free in the malloc() pool |
| 425 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 426 | * Return: bytes free |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 427 | */ |
| 428 | ulong 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 434 | * Return: free memory delta from @last; positive means more memory has been |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 435 | * allocated, negative means less has been allocated (i.e. some is freed) |
| 436 | */ |
| 437 | long ut_check_delta(ulong last); |
| 438 | |
Simon Glass | b8f0082 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 439 | /** |
| 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 | */ |
| 447 | void 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 | */ |
| 455 | void ut_unsilence_console(struct unit_test_state *uts); |
| 456 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 457 | /** |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 458 | * 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 | */ |
| 466 | void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays); |
| 467 | |
| 468 | /** |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 469 | * test_get_state() - Get the active test state |
| 470 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 471 | * Return: the currently active test state, or NULL if none |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 472 | */ |
| 473 | struct 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 | */ |
| 480 | void test_set_state(struct unit_test_state *uts); |
| 481 | |
| 482 | /** |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 483 | * 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 Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 497 | * @runs_per_test: Number of times to run each test (typically 1) |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 498 | * @force_run: Run tests that are marked as manual-only (UT_TESTF_MANUAL) |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 499 | * @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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 504 | * Return: 0 if all tests passed, -1 if any failed |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 505 | */ |
| 506 | int ut_run_list(const char *name, const char *prefix, struct unit_test *tests, |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 507 | int count, const char *select_name, int runs_per_test, |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 508 | bool force_run, const char *test_insert); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 509 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 510 | #endif |