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> |
| 14 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 15 | struct unit_test_state; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * ut_fail() - Record failure of a unit test |
| 19 | * |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | * @uts: Test state |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 21 | * @fname: Filename where the error occurred |
| 22 | * @line: Line number where the error occurred |
| 23 | * @func: Function name where the error occurred |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 24 | * @cond: The condition that failed |
| 25 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 26 | 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] | 27 | const char *func, const char *cond); |
| 28 | |
| 29 | /** |
| 30 | * ut_failf() - Record failure of a unit test |
| 31 | * |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 32 | * @uts: Test state |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 33 | * @fname: Filename where the error occurred |
| 34 | * @line: Line number where the error occurred |
| 35 | * @func: Function name where the error occurred |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 36 | * @cond: The condition that failed |
| 37 | * @fmt: printf() format string for the error, followed by args |
| 38 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 39 | 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] | 40 | const char *func, const char *cond, const char *fmt, ...) |
| 41 | __attribute__ ((format (__printf__, 6, 7))); |
| 42 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 43 | /** |
| 44 | * ut_check_console_line() - Check the next console line against expectations |
| 45 | * |
| 46 | * This creates a string and then checks it against the next line of console |
| 47 | * output obtained with console_record_readline(). |
| 48 | * |
| 49 | * After the function returns, uts->expect_str holds the expected string and |
| 50 | * uts->actual_str holds the actual string read from the console. |
| 51 | * |
| 52 | * @uts: Test state |
| 53 | * @fmt: printf() format string for the error, followed by args |
| 54 | * @return 0 if OK, other value on error |
| 55 | */ |
| 56 | int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) |
| 57 | __attribute__ ((format (__printf__, 2, 3))); |
| 58 | |
| 59 | /** |
| 60 | * ut_check_console_end() - Check there is no more console output |
| 61 | * |
| 62 | * After the function returns, uts->actual_str holds the actual string read |
| 63 | * from the console |
| 64 | * |
| 65 | * @uts: Test state |
| 66 | * @return 0 if OK (console has no output), other value on error |
| 67 | */ |
| 68 | int ut_check_console_end(struct unit_test_state *uts); |
| 69 | |
| 70 | /** |
| 71 | * ut_check_console_dump() - Check that next lines have a print_buffer() dump |
| 72 | * |
| 73 | * This only supports a byte dump. |
| 74 | * |
| 75 | * @total_bytes: Size of the expected dump in bytes` |
| 76 | * @return 0 if OK (looks like a dump and the length matches), other value on |
| 77 | * error |
| 78 | */ |
| 79 | 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] | 80 | |
| 81 | /* Assert that a condition is non-zero */ |
| 82 | #define ut_assert(cond) \ |
| 83 | if (!(cond)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 84 | ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 85 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* Assert that a condition is non-zero, with printf() string */ |
| 89 | #define ut_assertf(cond, fmt, args...) \ |
| 90 | if (!(cond)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 91 | ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 92 | fmt, ##args); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 93 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Assert that two int expressions are equal */ |
| 97 | #define ut_asserteq(expr1, expr2) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 98 | unsigned int _val1 = (expr1), _val2 = (expr2); \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 99 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 100 | if (_val1 != _val2) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 101 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 102 | #expr1 " == " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 103 | "Expected %#x (%d), got %#x (%d)", \ |
| 104 | _val1, _val1, _val2, _val2); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 105 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 106 | } \ |
| 107 | } |
| 108 | |
Dario Binacchi | 421e81e | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 109 | /* Assert that two 64 int expressions are equal */ |
| 110 | #define ut_asserteq_64(expr1, expr2) { \ |
| 111 | u64 _val1 = (expr1), _val2 = (expr2); \ |
| 112 | \ |
| 113 | if (_val1 != _val2) { \ |
| 114 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 115 | #expr1 " == " #expr2, \ |
| 116 | "Expected %#llx (%lld), got %#llx (%lld)", \ |
| 117 | (unsigned long long)_val1, \ |
| 118 | (unsigned long long)_val1, \ |
| 119 | (unsigned long long)_val2, \ |
| 120 | (unsigned long long)_val2); \ |
| 121 | return CMD_RET_FAILURE; \ |
| 122 | } \ |
| 123 | } |
| 124 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 125 | /* Assert that two string expressions are equal */ |
| 126 | #define ut_asserteq_str(expr1, expr2) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 127 | const char *_val1 = (expr1), *_val2 = (expr2); \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 128 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 129 | if (strcmp(_val1, _val2)) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 130 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 131 | #expr1 " = " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 132 | "Expected \"%s\", got \"%s\"", _val1, _val2); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 133 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 134 | } \ |
| 135 | } |
| 136 | |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 137 | /* Assert that two memory areas are equal */ |
| 138 | #define ut_asserteq_mem(expr1, expr2, len) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 139 | const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 140 | const uint __len = len; \ |
| 141 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 142 | if (memcmp(_val1, _val2, __len)) { \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 143 | char __buf1[64 + 1] = "\0"; \ |
| 144 | char __buf2[64 + 1] = "\0"; \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 145 | bin2hex(__buf1, _val1, min(__len, (uint)32)); \ |
| 146 | bin2hex(__buf2, _val2, min(__len, (uint)32)); \ |
Mario Six | ffdf8ab | 2018-09-27 09:19:32 +0200 | [diff] [blame] | 147 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 148 | #expr1 " = " #expr2, \ |
| 149 | "Expected \"%s\", got \"%s\"", \ |
| 150 | __buf1, __buf2); \ |
| 151 | return CMD_RET_FAILURE; \ |
| 152 | } \ |
| 153 | } |
| 154 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 155 | /* Assert that two pointers are equal */ |
| 156 | #define ut_asserteq_ptr(expr1, expr2) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 157 | const void *_val1 = (expr1), *_val2 = (expr2); \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 158 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 159 | if (_val1 != _val2) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 160 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 161 | #expr1 " = " #expr2, \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 162 | "Expected %p, got %p", _val1, _val2); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 163 | return CMD_RET_FAILURE; \ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 164 | } \ |
| 165 | } |
| 166 | |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 167 | /* Assert that a pointer is NULL */ |
| 168 | #define ut_assertnull(expr) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 169 | const void *_val = (expr); \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 170 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 171 | if (_val) { \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 172 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 173 | #expr " != NULL", \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 174 | "Expected NULL, got %p", _val); \ |
Ramon Fried | becc3e0 | 2018-06-21 17:47:16 +0300 | [diff] [blame] | 175 | return CMD_RET_FAILURE; \ |
| 176 | } \ |
| 177 | } |
| 178 | |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 179 | /* Assert that a pointer is not NULL */ |
| 180 | #define ut_assertnonnull(expr) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 181 | const void *_val = (expr); \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 182 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 183 | if (!_val) { \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 184 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 185 | #expr " = NULL", \ |
| 186 | "Expected non-null, got NULL"); \ |
Joe Hershberger | 436cfc7 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 187 | return CMD_RET_FAILURE; \ |
Simon Glass | 7df766e | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 188 | } \ |
| 189 | } |
| 190 | |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 191 | /* Assert that a pointer is not an error pointer */ |
Simon Glass | d21afd5 | 2017-05-18 20:10:00 -0600 | [diff] [blame] | 192 | #define ut_assertok_ptr(expr) { \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 193 | const void *_val = (expr); \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 194 | \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 195 | if (IS_ERR(_val)) { \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 196 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 197 | #expr " = NULL", \ |
| 198 | "Expected pointer, got error %ld", \ |
Simon Glass | 43c336b | 2020-01-27 08:49:41 -0700 | [diff] [blame] | 199 | PTR_ERR(_val)); \ |
Simon Glass | 7e1cebf | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 200 | return CMD_RET_FAILURE; \ |
| 201 | } \ |
| 202 | } |
| 203 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 204 | /* Assert that an operation succeeds (returns 0) */ |
| 205 | #define ut_assertok(cond) ut_asserteq(0, cond) |
| 206 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 207 | /* Assert that the next console output line matches */ |
| 208 | #define ut_assert_nextline(fmt, args...) \ |
| 209 | if (ut_check_console_line(uts, fmt, ##args)) { \ |
| 210 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 211 | "console", "\nExpected '%s',\n got '%s'", \ |
| 212 | uts->expect_str, uts->actual_str); \ |
| 213 | return CMD_RET_FAILURE; \ |
| 214 | } \ |
| 215 | |
| 216 | /* Assert that there is no more console output */ |
| 217 | #define ut_assert_console_end() \ |
| 218 | if (ut_check_console_end(uts)) { \ |
| 219 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 220 | "console", "Expected no more output, got '%s'",\ |
| 221 | uts->actual_str); \ |
| 222 | return CMD_RET_FAILURE; \ |
| 223 | } \ |
| 224 | |
| 225 | /* Assert that the next lines are print_buffer() dump at an address */ |
| 226 | #define ut_assert_nextlines_are_dump(total_bytes) \ |
| 227 | if (ut_check_console_dump(uts, total_bytes)) { \ |
| 228 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 229 | "console", \ |
| 230 | "Expected dump of length %x bytes, got '%s'", \ |
| 231 | total_bytes, uts->actual_str); \ |
| 232 | return CMD_RET_FAILURE; \ |
| 233 | } \ |
| 234 | |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 235 | /** |
| 236 | * ut_check_free() - Return the number of bytes free in the malloc() pool |
| 237 | * |
| 238 | * @return bytes free |
| 239 | */ |
| 240 | ulong ut_check_free(void); |
| 241 | |
| 242 | /** |
| 243 | * ut_check_delta() - Return the number of bytes allocated/freed |
| 244 | * |
| 245 | * @last: Last value from ut_check_free |
| 246 | * @return free memory delta from @last; positive means more memory has been |
| 247 | * allocated, negative means less has been allocated (i.e. some is freed) |
| 248 | */ |
| 249 | long ut_check_delta(ulong last); |
| 250 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 251 | #endif |