blob: 39d15953ed3a70c19beb902bbd81b7617c11c13e [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 Glassc5e7f662020-04-08 16:57:40 -060011#include <hexdump.h>
Simon Glass7e1cebf2015-07-06 12:54:37 -060012#include <linux/err.h>
13
Joe Hershberger3a77be52015-05-20 14:27:27 -050014struct unit_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070015
16/**
17 * ut_fail() - Record failure of a unit test
18 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050019 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070020 * @fname: Filename where the error occurred
21 * @line: Line number where the error occurred
22 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070023 * @cond: The condition that failed
24 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050025void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070026 const char *func, const char *cond);
27
28/**
29 * ut_failf() - Record failure of a unit test
30 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050031 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070032 * @fname: Filename where the error occurred
33 * @line: Line number where the error occurred
34 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070035 * @cond: The condition that failed
36 * @fmt: printf() format string for the error, followed by args
37 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050038void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070039 const char *func, const char *cond, const char *fmt, ...)
40 __attribute__ ((format (__printf__, 6, 7)));
41
Simon Glassd856e912020-01-27 08:49:56 -070042/**
43 * ut_check_console_line() - Check the next console line against expectations
44 *
45 * This creates a string and then checks it against the next line of console
46 * output obtained with console_record_readline().
47 *
48 * After the function returns, uts->expect_str holds the expected string and
49 * uts->actual_str holds the actual string read from the console.
50 *
51 * @uts: Test state
52 * @fmt: printf() format string for the error, followed by args
53 * @return 0 if OK, other value on error
54 */
55int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
56 __attribute__ ((format (__printf__, 2, 3)));
57
58/**
59 * ut_check_console_end() - Check there is no more console output
60 *
61 * After the function returns, uts->actual_str holds the actual string read
62 * from the console
63 *
64 * @uts: Test state
65 * @return 0 if OK (console has no output), other value on error
66 */
67int ut_check_console_end(struct unit_test_state *uts);
68
69/**
70 * ut_check_console_dump() - Check that next lines have a print_buffer() dump
71 *
72 * This only supports a byte dump.
73 *
74 * @total_bytes: Size of the expected dump in bytes`
75 * @return 0 if OK (looks like a dump and the length matches), other value on
76 * error
77 */
78int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
Simon Glassb2c1cac2014-02-26 15:59:21 -070079
80/* Assert that a condition is non-zero */
81#define ut_assert(cond) \
82 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050083 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050084 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070085 }
86
87/* Assert that a condition is non-zero, with printf() string */
88#define ut_assertf(cond, fmt, args...) \
89 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050090 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070091 fmt, ##args); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050092 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070093 }
94
95/* Assert that two int expressions are equal */
96#define ut_asserteq(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -070097 unsigned int _val1 = (expr1), _val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -070098 \
Simon Glass43c336b2020-01-27 08:49:41 -070099 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500100 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700101 #expr1 " == " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700102 "Expected %#x (%d), got %#x (%d)", \
103 _val1, _val1, _val2, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500104 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700105 } \
106}
107
108/* Assert that two string expressions are equal */
109#define ut_asserteq_str(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700110 const char *_val1 = (expr1), *_val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700111 \
Simon Glass43c336b2020-01-27 08:49:41 -0700112 if (strcmp(_val1, _val2)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500113 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700114 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700115 "Expected \"%s\", got \"%s\"", _val1, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500116 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700117 } \
118}
119
Mario Sixffdf8ab2018-09-27 09:19:32 +0200120/* Assert that two memory areas are equal */
121#define ut_asserteq_mem(expr1, expr2, len) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700122 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200123 const uint __len = len; \
124 \
Simon Glass43c336b2020-01-27 08:49:41 -0700125 if (memcmp(_val1, _val2, __len)) { \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200126 char __buf1[64 + 1] = "\0"; \
127 char __buf2[64 + 1] = "\0"; \
Simon Glass43c336b2020-01-27 08:49:41 -0700128 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
129 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200130 ut_failf(uts, __FILE__, __LINE__, __func__, \
131 #expr1 " = " #expr2, \
132 "Expected \"%s\", got \"%s\"", \
133 __buf1, __buf2); \
134 return CMD_RET_FAILURE; \
135 } \
136}
137
Simon Glassb2c1cac2014-02-26 15:59:21 -0700138/* Assert that two pointers are equal */
139#define ut_asserteq_ptr(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700140 const void *_val1 = (expr1), *_val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700141 \
Simon Glass43c336b2020-01-27 08:49:41 -0700142 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500143 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700144 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700145 "Expected %p, got %p", _val1, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500146 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700147 } \
148}
149
Ramon Friedbecc3e02018-06-21 17:47:16 +0300150/* Assert that a pointer is NULL */
151#define ut_assertnull(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700152 const void *_val = (expr); \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300153 \
Simon Glass43c336b2020-01-27 08:49:41 -0700154 if (_val) { \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300155 ut_failf(uts, __FILE__, __LINE__, __func__, \
156 #expr " != NULL", \
Simon Glass43c336b2020-01-27 08:49:41 -0700157 "Expected NULL, got %p", _val); \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300158 return CMD_RET_FAILURE; \
159 } \
160}
161
Simon Glass7df766e2014-12-10 08:55:55 -0700162/* Assert that a pointer is not NULL */
163#define ut_assertnonnull(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700164 const void *_val = (expr); \
Simon Glass7df766e2014-12-10 08:55:55 -0700165 \
Simon Glass43c336b2020-01-27 08:49:41 -0700166 if (!_val) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500167 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass7df766e2014-12-10 08:55:55 -0700168 #expr " = NULL", \
169 "Expected non-null, got NULL"); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500170 return CMD_RET_FAILURE; \
Simon Glass7df766e2014-12-10 08:55:55 -0700171 } \
172}
173
Simon Glass7e1cebf2015-07-06 12:54:37 -0600174/* Assert that a pointer is not an error pointer */
Simon Glassd21afd52017-05-18 20:10:00 -0600175#define ut_assertok_ptr(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700176 const void *_val = (expr); \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600177 \
Simon Glass43c336b2020-01-27 08:49:41 -0700178 if (IS_ERR(_val)) { \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600179 ut_failf(uts, __FILE__, __LINE__, __func__, \
180 #expr " = NULL", \
181 "Expected pointer, got error %ld", \
Simon Glass43c336b2020-01-27 08:49:41 -0700182 PTR_ERR(_val)); \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600183 return CMD_RET_FAILURE; \
184 } \
185}
186
Simon Glassb2c1cac2014-02-26 15:59:21 -0700187/* Assert that an operation succeeds (returns 0) */
188#define ut_assertok(cond) ut_asserteq(0, cond)
189
Simon Glassd856e912020-01-27 08:49:56 -0700190/* Assert that the next console output line matches */
191#define ut_assert_nextline(fmt, args...) \
192 if (ut_check_console_line(uts, fmt, ##args)) { \
193 ut_failf(uts, __FILE__, __LINE__, __func__, \
194 "console", "\nExpected '%s',\n got '%s'", \
195 uts->expect_str, uts->actual_str); \
196 return CMD_RET_FAILURE; \
197 } \
198
199/* Assert that there is no more console output */
200#define ut_assert_console_end() \
201 if (ut_check_console_end(uts)) { \
202 ut_failf(uts, __FILE__, __LINE__, __func__, \
203 "console", "Expected no more output, got '%s'",\
204 uts->actual_str); \
205 return CMD_RET_FAILURE; \
206 } \
207
208/* Assert that the next lines are print_buffer() dump at an address */
209#define ut_assert_nextlines_are_dump(total_bytes) \
210 if (ut_check_console_dump(uts, total_bytes)) { \
211 ut_failf(uts, __FILE__, __LINE__, __func__, \
212 "console", \
213 "Expected dump of length %x bytes, got '%s'", \
214 total_bytes, uts->actual_str); \
215 return CMD_RET_FAILURE; \
216 } \
217
Simon Glass19920d72019-12-29 21:19:23 -0700218/**
219 * ut_check_free() - Return the number of bytes free in the malloc() pool
220 *
221 * @return bytes free
222 */
223ulong ut_check_free(void);
224
225/**
226 * ut_check_delta() - Return the number of bytes allocated/freed
227 *
228 * @last: Last value from ut_check_free
229 * @return free memory delta from @last; positive means more memory has been
230 * allocated, negative means less has been allocated (i.e. some is freed)
231 */
232long ut_check_delta(ulong last);
233
Simon Glassb2c1cac2014-02-26 15:59:21 -0700234#endif