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