blob: 6ab2f8830df1f1c38554d6d4a1e0a11245725492 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass2e7d35d2014-02-26 15:59:21 -07002/*
Joe Hershbergere721b882015-05-20 14:27:27 -05003 * Simple unit test library
Simon Glass2e7d35d2014-02-26 15:59:21 -07004 *
5 * Copyright (c) 2013 Google, Inc
Simon Glass2e7d35d2014-02-26 15:59:21 -07006 */
7
Joe Hershbergere721b882015-05-20 14:27:27 -05008#ifndef __TEST_UT_H
9#define __TEST_UT_H
Simon Glass2e7d35d2014-02-26 15:59:21 -070010
Simon Glass09140112020-05-10 11:40:03 -060011#include <command.h>
Simon Glass1f4431e2020-04-08 16:57:40 -060012#include <hexdump.h>
Simon Glass85aeda42015-07-06 12:54:37 -060013#include <linux/err.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060014#include <test/test.h>
Simon Glass85aeda42015-07-06 12:54:37 -060015
Joe Hershbergere721b882015-05-20 14:27:27 -050016struct unit_test_state;
Simon Glass2e7d35d2014-02-26 15:59:21 -070017
18/**
19 * ut_fail() - Record failure of a unit test
20 *
Joe Hershbergere721b882015-05-20 14:27:27 -050021 * @uts: Test state
Vagrant Cascadianeae4b2b2016-04-30 19:18:00 -070022 * @fname: Filename where the error occurred
23 * @line: Line number where the error occurred
24 * @func: Function name where the error occurred
Simon Glass2e7d35d2014-02-26 15:59:21 -070025 * @cond: The condition that failed
26 */
Joe Hershbergere721b882015-05-20 14:27:27 -050027void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glass2e7d35d2014-02-26 15:59:21 -070028 const char *func, const char *cond);
29
30/**
31 * ut_failf() - Record failure of a unit test
32 *
Joe Hershbergere721b882015-05-20 14:27:27 -050033 * @uts: Test state
Vagrant Cascadianeae4b2b2016-04-30 19:18:00 -070034 * @fname: Filename where the error occurred
35 * @line: Line number where the error occurred
36 * @func: Function name where the error occurred
Simon Glass2e7d35d2014-02-26 15:59:21 -070037 * @cond: The condition that failed
38 * @fmt: printf() format string for the error, followed by args
39 */
Joe Hershbergere721b882015-05-20 14:27:27 -050040void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glass2e7d35d2014-02-26 15:59:21 -070041 const char *func, const char *cond, const char *fmt, ...)
42 __attribute__ ((format (__printf__, 6, 7)));
43
Simon Glass400175b2020-01-27 08:49:56 -070044/**
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 */
57int 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 */
69int 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 */
80int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
Simon Glass2e7d35d2014-02-26 15:59:21 -070081
82/* Assert that a condition is non-zero */
83#define ut_assert(cond) \
84 if (!(cond)) { \
Joe Hershbergere721b882015-05-20 14:27:27 -050085 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -050086 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -070087 }
88
89/* Assert that a condition is non-zero, with printf() string */
90#define ut_assertf(cond, fmt, args...) \
91 if (!(cond)) { \
Joe Hershbergere721b882015-05-20 14:27:27 -050092 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glass2e7d35d2014-02-26 15:59:21 -070093 fmt, ##args); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -050094 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -070095 }
96
97/* Assert that two int expressions are equal */
98#define ut_asserteq(expr1, expr2) { \
Simon Glassba8444a2020-01-27 08:49:41 -070099 unsigned int _val1 = (expr1), _val2 = (expr2); \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700100 \
Simon Glassba8444a2020-01-27 08:49:41 -0700101 if (_val1 != _val2) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500102 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700103 #expr1 " == " #expr2, \
Simon Glassba8444a2020-01-27 08:49:41 -0700104 "Expected %#x (%d), got %#x (%d)", \
105 _val1, _val1, _val2, _val2); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500106 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700107 } \
108}
109
Dario Binacchi70573c62020-03-29 18:04:40 +0200110/* 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 Glass2e7d35d2014-02-26 15:59:21 -0700126/* Assert that two string expressions are equal */
127#define ut_asserteq_str(expr1, expr2) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700128 const char *_val1 = (expr1), *_val2 = (expr2); \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700129 \
Simon Glassba8444a2020-01-27 08:49:41 -0700130 if (strcmp(_val1, _val2)) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500131 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700132 #expr1 " = " #expr2, \
Simon Glassba8444a2020-01-27 08:49:41 -0700133 "Expected \"%s\", got \"%s\"", _val1, _val2); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500134 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700135 } \
136}
137
Simon Glass7aed90d2020-07-07 13:11:54 -0600138/*
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 Six41f67e32018-09-27 09:19:32 +0200155/* Assert that two memory areas are equal */
156#define ut_asserteq_mem(expr1, expr2, len) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700157 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
Mario Six41f67e32018-09-27 09:19:32 +0200158 const uint __len = len; \
159 \
Simon Glassba8444a2020-01-27 08:49:41 -0700160 if (memcmp(_val1, _val2, __len)) { \
Mario Six41f67e32018-09-27 09:19:32 +0200161 char __buf1[64 + 1] = "\0"; \
162 char __buf2[64 + 1] = "\0"; \
Simon Glassba8444a2020-01-27 08:49:41 -0700163 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
164 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
Mario Six41f67e32018-09-27 09:19:32 +0200165 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 Glass2e7d35d2014-02-26 15:59:21 -0700173/* Assert that two pointers are equal */
174#define ut_asserteq_ptr(expr1, expr2) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700175 const void *_val1 = (expr1), *_val2 = (expr2); \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700176 \
Simon Glassba8444a2020-01-27 08:49:41 -0700177 if (_val1 != _val2) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500178 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700179 #expr1 " = " #expr2, \
Simon Glassba8444a2020-01-27 08:49:41 -0700180 "Expected %p, got %p", _val1, _val2); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500181 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700182 } \
183}
184
Ramon Fried8d545792018-06-21 17:47:16 +0300185/* Assert that a pointer is NULL */
186#define ut_assertnull(expr) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700187 const void *_val = (expr); \
Ramon Fried8d545792018-06-21 17:47:16 +0300188 \
Simon Glassba8444a2020-01-27 08:49:41 -0700189 if (_val) { \
Ramon Fried8d545792018-06-21 17:47:16 +0300190 ut_failf(uts, __FILE__, __LINE__, __func__, \
191 #expr " != NULL", \
Simon Glassba8444a2020-01-27 08:49:41 -0700192 "Expected NULL, got %p", _val); \
Ramon Fried8d545792018-06-21 17:47:16 +0300193 return CMD_RET_FAILURE; \
194 } \
195}
196
Simon Glassecc2ed52014-12-10 08:55:55 -0700197/* Assert that a pointer is not NULL */
198#define ut_assertnonnull(expr) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700199 const void *_val = (expr); \
Simon Glassecc2ed52014-12-10 08:55:55 -0700200 \
Simon Glassba8444a2020-01-27 08:49:41 -0700201 if (!_val) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500202 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassecc2ed52014-12-10 08:55:55 -0700203 #expr " = NULL", \
204 "Expected non-null, got NULL"); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500205 return CMD_RET_FAILURE; \
Simon Glassecc2ed52014-12-10 08:55:55 -0700206 } \
207}
208
Simon Glass85aeda42015-07-06 12:54:37 -0600209/* Assert that a pointer is not an error pointer */
Simon Glassfe3c0b52017-05-18 20:10:00 -0600210#define ut_assertok_ptr(expr) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700211 const void *_val = (expr); \
Simon Glass85aeda42015-07-06 12:54:37 -0600212 \
Simon Glassba8444a2020-01-27 08:49:41 -0700213 if (IS_ERR(_val)) { \
Simon Glass85aeda42015-07-06 12:54:37 -0600214 ut_failf(uts, __FILE__, __LINE__, __func__, \
215 #expr " = NULL", \
216 "Expected pointer, got error %ld", \
Simon Glassba8444a2020-01-27 08:49:41 -0700217 PTR_ERR(_val)); \
Simon Glass85aeda42015-07-06 12:54:37 -0600218 return CMD_RET_FAILURE; \
219 } \
220}
221
Simon Glass2e7d35d2014-02-26 15:59:21 -0700222/* Assert that an operation succeeds (returns 0) */
223#define ut_assertok(cond) ut_asserteq(0, cond)
224
Simon Glass400175b2020-01-27 08:49:56 -0700225/* 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 Glass81098632019-12-29 21:19:23 -0700253/**
254 * ut_check_free() - Return the number of bytes free in the malloc() pool
255 *
256 * @return bytes free
257 */
258ulong 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 */
267long ut_check_delta(ulong last);
268
Simon Glass2e7d35d2014-02-26 15:59:21 -0700269#endif