blob: ab861588a84a097b4b00b395beb6da09620763e3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
Joe Hershberger3a77be52015-05-20 14:27:27 -05003 * Simple unit test library
Simon Glassb2c1cac2014-02-26 15:59:21 -07004 *
5 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07006 */
7
Joe Hershberger3a77be52015-05-20 14:27:27 -05008#ifndef __TEST_UT_H
9#define __TEST_UT_H
Simon Glassb2c1cac2014-02-26 15:59:21 -070010
Simon Glass7e1cebf2015-07-06 12:54:37 -060011#include <linux/err.h>
12
Joe Hershberger3a77be52015-05-20 14:27:27 -050013struct unit_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070014
15/**
16 * ut_fail() - Record failure of a unit test
17 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050018 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070019 * @fname: Filename where the error occurred
20 * @line: Line number where the error occurred
21 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070022 * @cond: The condition that failed
23 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050024void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070025 const char *func, const char *cond);
26
27/**
28 * ut_failf() - Record failure of a unit test
29 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050030 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070031 * @fname: Filename where the error occurred
32 * @line: Line number where the error occurred
33 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070034 * @cond: The condition that failed
35 * @fmt: printf() format string for the error, followed by args
36 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050037void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070038 const char *func, const char *cond, const char *fmt, ...)
39 __attribute__ ((format (__printf__, 6, 7)));
40
Simon Glassd856e912020-01-27 08:49:56 -070041/**
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 */
54int 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 */
66int 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 */
77int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
Simon Glassb2c1cac2014-02-26 15:59:21 -070078
79/* Assert that a condition is non-zero */
80#define ut_assert(cond) \
81 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050082 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050083 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070084 }
85
86/* Assert that a condition is non-zero, with printf() string */
87#define ut_assertf(cond, fmt, args...) \
88 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050089 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070090 fmt, ##args); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050091 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070092 }
93
94/* Assert that two int expressions are equal */
95#define ut_asserteq(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -070096 unsigned int _val1 = (expr1), _val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -070097 \
Simon Glass43c336b2020-01-27 08:49:41 -070098 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050099 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700100 #expr1 " == " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700101 "Expected %#x (%d), got %#x (%d)", \
102 _val1, _val1, _val2, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500103 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700104 } \
105}
106
Dario Binacchi421e81e2020-03-29 18:04:40 +0200107/* Assert that two 64 int expressions are equal */
108#define ut_asserteq_64(expr1, expr2) { \
109 u64 _val1 = (expr1), _val2 = (expr2); \
110 \
111 if (_val1 != _val2) { \
112 ut_failf(uts, __FILE__, __LINE__, __func__, \
113 #expr1 " == " #expr2, \
114 "Expected %#llx (%lld), got %#llx (%lld)", \
115 (unsigned long long)_val1, \
116 (unsigned long long)_val1, \
117 (unsigned long long)_val2, \
118 (unsigned long long)_val2); \
119 return CMD_RET_FAILURE; \
120 } \
121}
122
Simon Glassb2c1cac2014-02-26 15:59:21 -0700123/* Assert that two string expressions are equal */
124#define ut_asserteq_str(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700125 const char *_val1 = (expr1), *_val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700126 \
Simon Glass43c336b2020-01-27 08:49:41 -0700127 if (strcmp(_val1, _val2)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500128 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700129 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700130 "Expected \"%s\", got \"%s\"", _val1, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500131 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700132 } \
133}
134
Mario Sixffdf8ab2018-09-27 09:19:32 +0200135/* Assert that two memory areas are equal */
136#define ut_asserteq_mem(expr1, expr2, len) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700137 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200138 const uint __len = len; \
139 \
Simon Glass43c336b2020-01-27 08:49:41 -0700140 if (memcmp(_val1, _val2, __len)) { \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200141 char __buf1[64 + 1] = "\0"; \
142 char __buf2[64 + 1] = "\0"; \
Simon Glass43c336b2020-01-27 08:49:41 -0700143 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
144 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200145 ut_failf(uts, __FILE__, __LINE__, __func__, \
146 #expr1 " = " #expr2, \
147 "Expected \"%s\", got \"%s\"", \
148 __buf1, __buf2); \
149 return CMD_RET_FAILURE; \
150 } \
151}
152
Simon Glassb2c1cac2014-02-26 15:59:21 -0700153/* Assert that two pointers are equal */
154#define ut_asserteq_ptr(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700155 const void *_val1 = (expr1), *_val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700156 \
Simon Glass43c336b2020-01-27 08:49:41 -0700157 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500158 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700159 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700160 "Expected %p, got %p", _val1, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500161 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700162 } \
163}
164
Ramon Friedbecc3e02018-06-21 17:47:16 +0300165/* Assert that a pointer is NULL */
166#define ut_assertnull(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700167 const void *_val = (expr); \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300168 \
Simon Glass43c336b2020-01-27 08:49:41 -0700169 if (_val) { \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300170 ut_failf(uts, __FILE__, __LINE__, __func__, \
171 #expr " != NULL", \
Simon Glass43c336b2020-01-27 08:49:41 -0700172 "Expected NULL, got %p", _val); \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300173 return CMD_RET_FAILURE; \
174 } \
175}
176
Simon Glass7df766e2014-12-10 08:55:55 -0700177/* Assert that a pointer is not NULL */
178#define ut_assertnonnull(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700179 const void *_val = (expr); \
Simon Glass7df766e2014-12-10 08:55:55 -0700180 \
Simon Glass43c336b2020-01-27 08:49:41 -0700181 if (!_val) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500182 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass7df766e2014-12-10 08:55:55 -0700183 #expr " = NULL", \
184 "Expected non-null, got NULL"); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500185 return CMD_RET_FAILURE; \
Simon Glass7df766e2014-12-10 08:55:55 -0700186 } \
187}
188
Simon Glass7e1cebf2015-07-06 12:54:37 -0600189/* Assert that a pointer is not an error pointer */
Simon Glassd21afd52017-05-18 20:10:00 -0600190#define ut_assertok_ptr(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700191 const void *_val = (expr); \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600192 \
Simon Glass43c336b2020-01-27 08:49:41 -0700193 if (IS_ERR(_val)) { \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600194 ut_failf(uts, __FILE__, __LINE__, __func__, \
195 #expr " = NULL", \
196 "Expected pointer, got error %ld", \
Simon Glass43c336b2020-01-27 08:49:41 -0700197 PTR_ERR(_val)); \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600198 return CMD_RET_FAILURE; \
199 } \
200}
201
Simon Glassb2c1cac2014-02-26 15:59:21 -0700202/* Assert that an operation succeeds (returns 0) */
203#define ut_assertok(cond) ut_asserteq(0, cond)
204
Simon Glassd856e912020-01-27 08:49:56 -0700205/* Assert that the next console output line matches */
206#define ut_assert_nextline(fmt, args...) \
207 if (ut_check_console_line(uts, fmt, ##args)) { \
208 ut_failf(uts, __FILE__, __LINE__, __func__, \
209 "console", "\nExpected '%s',\n got '%s'", \
210 uts->expect_str, uts->actual_str); \
211 return CMD_RET_FAILURE; \
212 } \
213
214/* Assert that there is no more console output */
215#define ut_assert_console_end() \
216 if (ut_check_console_end(uts)) { \
217 ut_failf(uts, __FILE__, __LINE__, __func__, \
218 "console", "Expected no more output, got '%s'",\
219 uts->actual_str); \
220 return CMD_RET_FAILURE; \
221 } \
222
223/* Assert that the next lines are print_buffer() dump at an address */
224#define ut_assert_nextlines_are_dump(total_bytes) \
225 if (ut_check_console_dump(uts, total_bytes)) { \
226 ut_failf(uts, __FILE__, __LINE__, __func__, \
227 "console", \
228 "Expected dump of length %x bytes, got '%s'", \
229 total_bytes, uts->actual_str); \
230 return CMD_RET_FAILURE; \
231 } \
232
Simon Glass19920d72019-12-29 21:19:23 -0700233/**
234 * ut_check_free() - Return the number of bytes free in the malloc() pool
235 *
236 * @return bytes free
237 */
238ulong ut_check_free(void);
239
240/**
241 * ut_check_delta() - Return the number of bytes allocated/freed
242 *
243 * @last: Last value from ut_check_free
244 * @return free memory delta from @last; positive means more memory has been
245 * allocated, negative means less has been allocated (i.e. some is freed)
246 */
247long ut_check_delta(ulong last);
248
Simon Glassb2c1cac2014-02-26 15:59:21 -0700249#endif