Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 2 | /* |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 3 | * Simple unit test library |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 8 | #ifndef __TEST_UT_H |
| 9 | #define __TEST_UT_H |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 10 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 1f4431e | 2020-04-08 16:57:40 -0600 | [diff] [blame] | 12 | #include <hexdump.h> |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 13 | #include <linux/err.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 14 | #include <test/test.h> |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 15 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | struct unit_test_state; |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * ut_fail() - Record failure of a unit test |
| 20 | * |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 21 | * @uts: Test state |
Vagrant Cascadian | eae4b2b | 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 | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 25 | * @cond: The condition that failed |
| 26 | */ |
Joe Hershberger | e721b88 | 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 | 2e7d35d | 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 | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 33 | * @uts: Test state |
Vagrant Cascadian | eae4b2b | 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 | 2e7d35d | 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 | e721b88 | 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 | 2e7d35d | 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 | 400175b | 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 | /** |
| 61 | * ut_check_console_end() - Check there is no more console output |
| 62 | * |
| 63 | * After the function returns, uts->actual_str holds the actual string read |
| 64 | * from the console |
| 65 | * |
| 66 | * @uts: Test state |
| 67 | * @return 0 if OK (console has no output), other value on error |
| 68 | */ |
| 69 | int ut_check_console_end(struct unit_test_state *uts); |
| 70 | |
| 71 | /** |
| 72 | * ut_check_console_dump() - Check that next lines have a print_buffer() dump |
| 73 | * |
| 74 | * This only supports a byte dump. |
| 75 | * |
| 76 | * @total_bytes: Size of the expected dump in bytes` |
| 77 | * @return 0 if OK (looks like a dump and the length matches), other value on |
| 78 | * error |
| 79 | */ |
| 80 | int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 81 | |
| 82 | /* Assert that a condition is non-zero */ |
| 83 | #define ut_assert(cond) \ |
| 84 | if (!(cond)) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 85 | ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 86 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Assert that a condition is non-zero, with printf() string */ |
| 90 | #define ut_assertf(cond, fmt, args...) \ |
| 91 | if (!(cond)) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 92 | ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 93 | fmt, ##args); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 94 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Assert that two int expressions are equal */ |
| 98 | #define ut_asserteq(expr1, expr2) { \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 99 | unsigned int _val1 = (expr1), _val2 = (expr2); \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 100 | \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 101 | if (_val1 != _val2) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 102 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 103 | #expr1 " == " #expr2, \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 104 | "Expected %#x (%d), got %#x (%d)", \ |
| 105 | _val1, _val1, _val2, _val2); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 106 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 107 | } \ |
| 108 | } |
| 109 | |
Dario Binacchi | 70573c6 | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 110 | /* Assert that two 64 int expressions are equal */ |
| 111 | #define ut_asserteq_64(expr1, expr2) { \ |
| 112 | u64 _val1 = (expr1), _val2 = (expr2); \ |
| 113 | \ |
| 114 | if (_val1 != _val2) { \ |
| 115 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 116 | #expr1 " == " #expr2, \ |
| 117 | "Expected %#llx (%lld), got %#llx (%lld)", \ |
| 118 | (unsigned long long)_val1, \ |
| 119 | (unsigned long long)_val1, \ |
| 120 | (unsigned long long)_val2, \ |
| 121 | (unsigned long long)_val2); \ |
| 122 | return CMD_RET_FAILURE; \ |
| 123 | } \ |
| 124 | } |
| 125 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 126 | /* Assert that two string expressions are equal */ |
| 127 | #define ut_asserteq_str(expr1, expr2) { \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 128 | const char *_val1 = (expr1), *_val2 = (expr2); \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 129 | \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 130 | if (strcmp(_val1, _val2)) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 131 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 132 | #expr1 " = " #expr2, \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 133 | "Expected \"%s\", got \"%s\"", _val1, _val2); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 134 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 135 | } \ |
| 136 | } |
| 137 | |
Simon Glass | 7aed90d | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 138 | /* |
| 139 | * Assert that two string expressions are equal, up to length of the |
| 140 | * first |
| 141 | */ |
| 142 | #define ut_asserteq_strn(expr1, expr2) { \ |
| 143 | const char *_val1 = (expr1), *_val2 = (expr2); \ |
| 144 | int _len = strlen(_val1); \ |
| 145 | \ |
| 146 | if (memcmp(_val1, _val2, _len)) { \ |
| 147 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 148 | #expr1 " = " #expr2, \ |
| 149 | "Expected \"%.*s\", got \"%.*s\"", \ |
| 150 | _len, _val1, _len, _val2); \ |
| 151 | return CMD_RET_FAILURE; \ |
| 152 | } \ |
| 153 | } |
| 154 | |
Mario Six | 41f67e3 | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 155 | /* Assert that two memory areas are equal */ |
| 156 | #define ut_asserteq_mem(expr1, expr2, len) { \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 157 | const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \ |
Mario Six | 41f67e3 | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 158 | const uint __len = len; \ |
| 159 | \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 160 | if (memcmp(_val1, _val2, __len)) { \ |
Mario Six | 41f67e3 | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 161 | char __buf1[64 + 1] = "\0"; \ |
| 162 | char __buf2[64 + 1] = "\0"; \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 163 | bin2hex(__buf1, _val1, min(__len, (uint)32)); \ |
| 164 | bin2hex(__buf2, _val2, min(__len, (uint)32)); \ |
Mario Six | 41f67e3 | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 165 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 166 | #expr1 " = " #expr2, \ |
| 167 | "Expected \"%s\", got \"%s\"", \ |
| 168 | __buf1, __buf2); \ |
| 169 | return CMD_RET_FAILURE; \ |
| 170 | } \ |
| 171 | } |
| 172 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 173 | /* Assert that two pointers are equal */ |
| 174 | #define ut_asserteq_ptr(expr1, expr2) { \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 175 | const void *_val1 = (expr1), *_val2 = (expr2); \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 176 | \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 177 | if (_val1 != _val2) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 178 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 179 | #expr1 " = " #expr2, \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 180 | "Expected %p, got %p", _val1, _val2); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 181 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 182 | } \ |
| 183 | } |
| 184 | |
Ramon Fried | 8d54579 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 185 | /* Assert that a pointer is NULL */ |
| 186 | #define ut_assertnull(expr) { \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 187 | const void *_val = (expr); \ |
Ramon Fried | 8d54579 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 188 | \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 189 | if (_val) { \ |
Ramon Fried | 8d54579 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 190 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 191 | #expr " != NULL", \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 192 | "Expected NULL, got %p", _val); \ |
Ramon Fried | 8d54579 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 193 | return CMD_RET_FAILURE; \ |
| 194 | } \ |
| 195 | } |
| 196 | |
Simon Glass | ecc2ed5 | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 197 | /* Assert that a pointer is not NULL */ |
| 198 | #define ut_assertnonnull(expr) { \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 199 | const void *_val = (expr); \ |
Simon Glass | ecc2ed5 | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 200 | \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 201 | if (!_val) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 202 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | ecc2ed5 | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 203 | #expr " = NULL", \ |
| 204 | "Expected non-null, got NULL"); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 205 | return CMD_RET_FAILURE; \ |
Simon Glass | ecc2ed5 | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 206 | } \ |
| 207 | } |
| 208 | |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 209 | /* Assert that a pointer is not an error pointer */ |
Simon Glass | fe3c0b5 | 2017-05-18 20:10:00 -0600 | [diff] [blame] | 210 | #define ut_assertok_ptr(expr) { \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 211 | const void *_val = (expr); \ |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 212 | \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 213 | if (IS_ERR(_val)) { \ |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 214 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 215 | #expr " = NULL", \ |
| 216 | "Expected pointer, got error %ld", \ |
Simon Glass | ba8444a | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 217 | PTR_ERR(_val)); \ |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 218 | return CMD_RET_FAILURE; \ |
| 219 | } \ |
| 220 | } |
| 221 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 222 | /* Assert that an operation succeeds (returns 0) */ |
| 223 | #define ut_assertok(cond) ut_asserteq(0, cond) |
| 224 | |
Simon Glass | 400175b | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 225 | /* Assert that the next console output line matches */ |
| 226 | #define ut_assert_nextline(fmt, args...) \ |
| 227 | if (ut_check_console_line(uts, fmt, ##args)) { \ |
| 228 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 229 | "console", "\nExpected '%s',\n got '%s'", \ |
| 230 | uts->expect_str, uts->actual_str); \ |
| 231 | return CMD_RET_FAILURE; \ |
| 232 | } \ |
| 233 | |
| 234 | /* Assert that there is no more console output */ |
| 235 | #define ut_assert_console_end() \ |
| 236 | if (ut_check_console_end(uts)) { \ |
| 237 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 238 | "console", "Expected no more output, got '%s'",\ |
| 239 | uts->actual_str); \ |
| 240 | return CMD_RET_FAILURE; \ |
| 241 | } \ |
| 242 | |
| 243 | /* Assert that the next lines are print_buffer() dump at an address */ |
| 244 | #define ut_assert_nextlines_are_dump(total_bytes) \ |
| 245 | if (ut_check_console_dump(uts, total_bytes)) { \ |
| 246 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 247 | "console", \ |
| 248 | "Expected dump of length %x bytes, got '%s'", \ |
| 249 | total_bytes, uts->actual_str); \ |
| 250 | return CMD_RET_FAILURE; \ |
| 251 | } \ |
| 252 | |
Simon Glass | 8109863 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 253 | /** |
| 254 | * ut_check_free() - Return the number of bytes free in the malloc() pool |
| 255 | * |
| 256 | * @return bytes free |
| 257 | */ |
| 258 | ulong ut_check_free(void); |
| 259 | |
| 260 | /** |
| 261 | * ut_check_delta() - Return the number of bytes allocated/freed |
| 262 | * |
| 263 | * @last: Last value from ut_check_free |
| 264 | * @return free memory delta from @last; positive means more memory has been |
| 265 | * allocated, negative means less has been allocated (i.e. some is freed) |
| 266 | */ |
| 267 | long ut_check_delta(ulong last); |
| 268 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 269 | #endif |