blob: dddf9ad241fe8575c78b0d16772e3b3c5c569296 [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 Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Simon Glassc5e7f662020-04-08 16:57:40 -060012#include <hexdump.h>
Simon Glass7e1cebf2015-07-06 12:54:37 -060013#include <linux/err.h>
Simon Glass75c4d412020-07-19 10:15:37 -060014#include <test/test.h>
Simon Glass7e1cebf2015-07-06 12:54:37 -060015
Joe Hershberger3a77be52015-05-20 14:27:27 -050016struct unit_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070017
18/**
19 * ut_fail() - Record failure of a unit test
20 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050021 * @uts: Test state
Vagrant Cascadianedfdb992016-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 Glassb2c1cac2014-02-26 15:59:21 -070025 * @cond: The condition that failed
26 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050027void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-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 Hershberger3a77be52015-05-20 14:27:27 -050033 * @uts: Test state
Vagrant Cascadianedfdb992016-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 Glassb2c1cac2014-02-26 15:59:21 -070037 * @cond: The condition that failed
38 * @fmt: printf() format string for the error, followed by args
39 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050040void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070041 const char *func, const char *cond, const char *fmt, ...)
42 __attribute__ ((format (__printf__, 6, 7)));
43
Simon Glassd856e912020-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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010055 * Return: 0 if OK, other value on error
Simon Glassd856e912020-01-27 08:49:56 -070056 */
57int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
58 __attribute__ ((format (__printf__, 2, 3)));
59
60/**
Simon Glassc963df92020-07-28 19:41:10 -060061 * ut_check_console_linen() - Check part of the next console line
62 *
63 * This creates a string and then checks it against the next line of console
64 * output obtained with console_record_readline(). Only the length of the
65 * string is checked
66 *
67 * After the function returns, uts->expect_str holds the expected string and
68 * uts->actual_str holds the actual string read from the console.
69 *
70 * @uts: Test state
71 * @fmt: printf() format string for the error, followed by args
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010072 * Return: 0 if OK, other value on error
Simon Glassc963df92020-07-28 19:41:10 -060073 */
74int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
75 __attribute__ ((format (__printf__, 2, 3)));
76
77/**
78 * ut_check_skipline() - Check that the next console line exists and skip it
79 *
80 * @uts: Test state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010081 * Return: 0 if OK, other value on error
Simon Glassc963df92020-07-28 19:41:10 -060082 */
83int ut_check_skipline(struct unit_test_state *uts);
84
85/**
Simon Glass12892b02021-08-18 21:40:33 -060086 * ut_check_skip_to_line() - skip output until a line is found
87 *
88 * This creates a string and then checks it against the following lines of
89 * console output obtained with console_record_readline() until it is found.
90 *
91 * After the function returns, uts->expect_str holds the expected string and
92 * uts->actual_str holds the actual string read from the console.
93 *
94 * @uts: Test state
95 * @fmt: printf() format string to look for, followed by args
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010096 * Return: 0 if OK, -ENOENT if not found, other value on error
Simon Glass12892b02021-08-18 21:40:33 -060097 */
98int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...);
99
100/**
Simon Glassd856e912020-01-27 08:49:56 -0700101 * ut_check_console_end() - Check there is no more console output
102 *
103 * After the function returns, uts->actual_str holds the actual string read
104 * from the console
105 *
106 * @uts: Test state
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100107 * Return: 0 if OK (console has no output), other value on error
Simon Glassd856e912020-01-27 08:49:56 -0700108 */
109int ut_check_console_end(struct unit_test_state *uts);
110
111/**
112 * ut_check_console_dump() - Check that next lines have a print_buffer() dump
113 *
114 * This only supports a byte dump.
115 *
116 * @total_bytes: Size of the expected dump in bytes`
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100117 * Return: 0 if OK (looks like a dump and the length matches), other value on
Simon Glassd856e912020-01-27 08:49:56 -0700118 * error
119 */
120int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700121
Simon Glassf912a722022-09-06 20:27:27 -0600122/* Report a failure, with printf() string */
123#define ut_reportf(fmt, args...) \
124 ut_failf(uts, __FILE__, __LINE__, __func__, "report", \
125 fmt, ##args)
126
Simon Glassb2c1cac2014-02-26 15:59:21 -0700127/* Assert that a condition is non-zero */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100128#define ut_assert(cond) ({ \
129 int __ret = 0; \
130 \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700131 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500132 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100133 __ret = CMD_RET_FAILURE; \
134 } \
135 __ret; \
136})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700137
138/* Assert that a condition is non-zero, with printf() string */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100139#define ut_assertf(cond, fmt, args...) ({ \
140 int __ret = 0; \
141 \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700142 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500143 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700144 fmt, ##args); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100145 __ret = CMD_RET_FAILURE; \
146 } \
147 __ret; \
148})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700149
150/* Assert that two int expressions are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100151#define ut_asserteq(expr1, expr2) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700152 unsigned int _val1 = (expr1), _val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100153 int __ret = 0; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700154 \
Simon Glass43c336b2020-01-27 08:49:41 -0700155 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500156 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700157 #expr1 " == " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700158 "Expected %#x (%d), got %#x (%d)", \
159 _val1, _val1, _val2, _val2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100160 __ret = CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700161 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100162 __ret; \
163})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700164
Dario Binacchi421e81e2020-03-29 18:04:40 +0200165/* Assert that two 64 int expressions are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100166#define ut_asserteq_64(expr1, expr2) ({ \
Dario Binacchi421e81e2020-03-29 18:04:40 +0200167 u64 _val1 = (expr1), _val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100168 int __ret = 0; \
Dario Binacchi421e81e2020-03-29 18:04:40 +0200169 \
170 if (_val1 != _val2) { \
171 ut_failf(uts, __FILE__, __LINE__, __func__, \
172 #expr1 " == " #expr2, \
173 "Expected %#llx (%lld), got %#llx (%lld)", \
174 (unsigned long long)_val1, \
175 (unsigned long long)_val1, \
176 (unsigned long long)_val2, \
177 (unsigned long long)_val2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100178 __ret = CMD_RET_FAILURE; \
Dario Binacchi421e81e2020-03-29 18:04:40 +0200179 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100180 __ret; \
181})
Dario Binacchi421e81e2020-03-29 18:04:40 +0200182
Simon Glassb2c1cac2014-02-26 15:59:21 -0700183/* Assert that two string expressions are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100184#define ut_asserteq_str(expr1, expr2) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700185 const char *_val1 = (expr1), *_val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100186 int __ret = 0; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700187 \
Simon Glass43c336b2020-01-27 08:49:41 -0700188 if (strcmp(_val1, _val2)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500189 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700190 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700191 "Expected \"%s\", got \"%s\"", _val1, _val2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100192 __ret = CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700193 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100194 __ret; \
195})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700196
Simon Glass0dc3d512020-07-07 13:11:54 -0600197/*
198 * Assert that two string expressions are equal, up to length of the
199 * first
200 */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100201#define ut_asserteq_strn(expr1, expr2) ({ \
Simon Glass0dc3d512020-07-07 13:11:54 -0600202 const char *_val1 = (expr1), *_val2 = (expr2); \
203 int _len = strlen(_val1); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100204 int __ret = 0; \
Simon Glass0dc3d512020-07-07 13:11:54 -0600205 \
206 if (memcmp(_val1, _val2, _len)) { \
207 ut_failf(uts, __FILE__, __LINE__, __func__, \
208 #expr1 " = " #expr2, \
209 "Expected \"%.*s\", got \"%.*s\"", \
210 _len, _val1, _len, _val2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100211 __ret = CMD_RET_FAILURE; \
Simon Glass0dc3d512020-07-07 13:11:54 -0600212 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100213 __ret; \
214})
Simon Glass0dc3d512020-07-07 13:11:54 -0600215
Mario Sixffdf8ab2018-09-27 09:19:32 +0200216/* Assert that two memory areas are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100217#define ut_asserteq_mem(expr1, expr2, len) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700218 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200219 const uint __len = len; \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100220 int __ret = 0; \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200221 \
Simon Glass43c336b2020-01-27 08:49:41 -0700222 if (memcmp(_val1, _val2, __len)) { \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200223 char __buf1[64 + 1] = "\0"; \
224 char __buf2[64 + 1] = "\0"; \
Simon Glass43c336b2020-01-27 08:49:41 -0700225 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
226 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200227 ut_failf(uts, __FILE__, __LINE__, __func__, \
228 #expr1 " = " #expr2, \
229 "Expected \"%s\", got \"%s\"", \
230 __buf1, __buf2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100231 __ret = CMD_RET_FAILURE; \
Mario Sixffdf8ab2018-09-27 09:19:32 +0200232 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100233 __ret; \
234})
Mario Sixffdf8ab2018-09-27 09:19:32 +0200235
Simon Glassb2c1cac2014-02-26 15:59:21 -0700236/* Assert that two pointers are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100237#define ut_asserteq_ptr(expr1, expr2) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700238 const void *_val1 = (expr1), *_val2 = (expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100239 int __ret = 0; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700240 \
Simon Glass43c336b2020-01-27 08:49:41 -0700241 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500242 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700243 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700244 "Expected %p, got %p", _val1, _val2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100245 __ret = CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700246 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100247 __ret; \
248})
Simon Glassb2c1cac2014-02-26 15:59:21 -0700249
Simon Glass7c58b082020-09-19 18:49:27 -0600250/* Assert that two addresses (converted from pointers) are equal */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100251#define ut_asserteq_addr(expr1, expr2) ({ \
Simon Glass7c58b082020-09-19 18:49:27 -0600252 ulong _val1 = map_to_sysmem(expr1); \
253 ulong _val2 = map_to_sysmem(expr2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100254 int __ret = 0; \
Simon Glass7c58b082020-09-19 18:49:27 -0600255 \
256 if (_val1 != _val2) { \
257 ut_failf(uts, __FILE__, __LINE__, __func__, \
258 #expr1 " = " #expr2, \
259 "Expected %lx, got %lx", _val1, _val2); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100260 __ret = CMD_RET_FAILURE; \
Simon Glass7c58b082020-09-19 18:49:27 -0600261 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100262 __ret; \
263})
Simon Glass7c58b082020-09-19 18:49:27 -0600264
Ramon Friedbecc3e02018-06-21 17:47:16 +0300265/* Assert that a pointer is NULL */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100266#define ut_assertnull(expr) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700267 const void *_val = (expr); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100268 int __ret = 0; \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300269 \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100270 if (_val) { \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300271 ut_failf(uts, __FILE__, __LINE__, __func__, \
272 #expr " != NULL", \
Simon Glass43c336b2020-01-27 08:49:41 -0700273 "Expected NULL, got %p", _val); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100274 __ret = CMD_RET_FAILURE; \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300275 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100276 __ret; \
277})
Ramon Friedbecc3e02018-06-21 17:47:16 +0300278
Simon Glass7df766e2014-12-10 08:55:55 -0700279/* Assert that a pointer is not NULL */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100280#define ut_assertnonnull(expr) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700281 const void *_val = (expr); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100282 int __ret = 0; \
Simon Glass7df766e2014-12-10 08:55:55 -0700283 \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100284 if (!_val) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500285 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass7df766e2014-12-10 08:55:55 -0700286 #expr " = NULL", \
287 "Expected non-null, got NULL"); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100288 __ret = CMD_RET_FAILURE; \
Simon Glass7df766e2014-12-10 08:55:55 -0700289 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100290 __ret; \
291})
Simon Glass7df766e2014-12-10 08:55:55 -0700292
Simon Glass7e1cebf2015-07-06 12:54:37 -0600293/* Assert that a pointer is not an error pointer */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100294#define ut_assertok_ptr(expr) ({ \
Simon Glass43c336b2020-01-27 08:49:41 -0700295 const void *_val = (expr); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100296 int __ret = 0; \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600297 \
Simon Glass43c336b2020-01-27 08:49:41 -0700298 if (IS_ERR(_val)) { \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600299 ut_failf(uts, __FILE__, __LINE__, __func__, \
300 #expr " = NULL", \
301 "Expected pointer, got error %ld", \
Simon Glass43c336b2020-01-27 08:49:41 -0700302 PTR_ERR(_val)); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100303 __ret = CMD_RET_FAILURE; \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600304 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100305 __ret; \
306})
Simon Glass7e1cebf2015-07-06 12:54:37 -0600307
Simon Glassb2c1cac2014-02-26 15:59:21 -0700308/* Assert that an operation succeeds (returns 0) */
309#define ut_assertok(cond) ut_asserteq(0, cond)
310
Simon Glassd856e912020-01-27 08:49:56 -0700311/* Assert that the next console output line matches */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100312#define ut_assert_nextline(fmt, args...) ({ \
313 int __ret = 0; \
314 \
Simon Glassd856e912020-01-27 08:49:56 -0700315 if (ut_check_console_line(uts, fmt, ##args)) { \
316 ut_failf(uts, __FILE__, __LINE__, __func__, \
317 "console", "\nExpected '%s',\n got '%s'", \
318 uts->expect_str, uts->actual_str); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100319 __ret = CMD_RET_FAILURE; \
Simon Glassd856e912020-01-27 08:49:56 -0700320 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100321 __ret; \
322})
Simon Glassd856e912020-01-27 08:49:56 -0700323
Simon Glassc963df92020-07-28 19:41:10 -0600324/* Assert that the next console output line matches up to the length */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100325#define ut_assert_nextlinen(fmt, args...) ({ \
326 int __ret = 0; \
327 \
Simon Glassc963df92020-07-28 19:41:10 -0600328 if (ut_check_console_linen(uts, fmt, ##args)) { \
329 ut_failf(uts, __FILE__, __LINE__, __func__, \
330 "console", "\nExpected '%s',\n got '%s'", \
331 uts->expect_str, uts->actual_str); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100332 __ret = CMD_RET_FAILURE; \
Simon Glassc963df92020-07-28 19:41:10 -0600333 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100334 __ret; \
335})
Simon Glassc963df92020-07-28 19:41:10 -0600336
337/* Assert that there is a 'next' console output line, and skip it */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100338#define ut_assert_skipline() ({ \
339 int __ret = 0; \
340 \
Simon Glassc963df92020-07-28 19:41:10 -0600341 if (ut_check_skipline(uts)) { \
342 ut_failf(uts, __FILE__, __LINE__, __func__, \
343 "console", "\nExpected a line, got end"); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100344 __ret = CMD_RET_FAILURE; \
Simon Glassc963df92020-07-28 19:41:10 -0600345 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100346 __ret; \
347})
Simon Glassc963df92020-07-28 19:41:10 -0600348
Simon Glass12892b02021-08-18 21:40:33 -0600349/* Assert that a following console output line matches */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100350#define ut_assert_skip_to_line(fmt, args...) ({ \
351 int __ret = 0; \
352 \
Simon Glass12892b02021-08-18 21:40:33 -0600353 if (ut_check_skip_to_line(uts, fmt, ##args)) { \
354 ut_failf(uts, __FILE__, __LINE__, __func__, \
355 "console", "\nExpected '%s',\n got to '%s'", \
356 uts->expect_str, uts->actual_str); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100357 __ret = CMD_RET_FAILURE; \
Simon Glass12892b02021-08-18 21:40:33 -0600358 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100359 __ret; \
360})
Simon Glass12892b02021-08-18 21:40:33 -0600361
Simon Glassd856e912020-01-27 08:49:56 -0700362/* Assert that there is no more console output */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100363#define ut_assert_console_end() ({ \
364 int __ret = 0; \
365 \
Simon Glassd856e912020-01-27 08:49:56 -0700366 if (ut_check_console_end(uts)) { \
367 ut_failf(uts, __FILE__, __LINE__, __func__, \
368 "console", "Expected no more output, got '%s'",\
369 uts->actual_str); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100370 __ret = CMD_RET_FAILURE; \
Simon Glassd856e912020-01-27 08:49:56 -0700371 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100372 __ret; \
373})
Simon Glassd856e912020-01-27 08:49:56 -0700374
375/* Assert that the next lines are print_buffer() dump at an address */
Marek Vasut1531c4e2023-03-10 04:33:13 +0100376#define ut_assert_nextlines_are_dump(total_bytes) ({ \
377 int __ret = 0; \
378 \
Simon Glassd856e912020-01-27 08:49:56 -0700379 if (ut_check_console_dump(uts, total_bytes)) { \
380 ut_failf(uts, __FILE__, __LINE__, __func__, \
381 "console", \
382 "Expected dump of length %x bytes, got '%s'", \
383 total_bytes, uts->actual_str); \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100384 __ret = CMD_RET_FAILURE; \
Simon Glassd856e912020-01-27 08:49:56 -0700385 } \
Marek Vasut1531c4e2023-03-10 04:33:13 +0100386 __ret; \
387})
Simon Glassd856e912020-01-27 08:49:56 -0700388
Marek Vasut8abbbf22023-03-02 04:08:24 +0100389/* Assert that the next console output line is empty */
390#define ut_assert_nextline_empty() \
391 ut_assert_nextline("%s", "")
392
Simon Glass19920d72019-12-29 21:19:23 -0700393/**
394 * ut_check_free() - Return the number of bytes free in the malloc() pool
395 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100396 * Return: bytes free
Simon Glass19920d72019-12-29 21:19:23 -0700397 */
398ulong ut_check_free(void);
399
400/**
401 * ut_check_delta() - Return the number of bytes allocated/freed
402 *
403 * @last: Last value from ut_check_free
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100404 * Return: free memory delta from @last; positive means more memory has been
Simon Glass19920d72019-12-29 21:19:23 -0700405 * allocated, negative means less has been allocated (i.e. some is freed)
406 */
407long ut_check_delta(ulong last);
408
Simon Glassb8f00822020-11-08 21:08:43 -0700409/**
410 * ut_silence_console() - Silence the console if requested by the user
411 *
412 * This stops test output from appear on the console. It is the default on
413 * sandbox, unless the -v flag is given. For other boards, this does nothing.
414 *
415 * @uts: Test state (in case in future we want to keep state here)
416 */
417void ut_silence_console(struct unit_test_state *uts);
418
419/**
420 * ut_unsilence_console() - Unsilence the console after a test
421 *
422 * This restarts console output again and turns off console recording. This
423 * happens on all boards, including sandbox.
424 */
425void ut_unsilence_console(struct unit_test_state *uts);
426
Simon Glass5722fb22021-03-07 17:34:47 -0700427/**
Simon Glass59cad962021-03-07 17:34:55 -0700428 * ut_set_skip_delays() - Sets whether delays should be skipped
429 *
430 * Normally functions like mdelay() cause U-Boot to wait for a while. This
431 * allows all such delays to be skipped on sandbox, to speed up tests
432 *
433 * @uts: Test state (in case in future we want to keep state here)
434 * @skip_delays: true to skip delays, false to process them normally
435 */
436void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
437
438/**
Simon Glass4066d8d2021-03-07 17:35:04 -0700439 * test_get_state() - Get the active test state
440 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100441 * Return: the currently active test state, or NULL if none
Simon Glass4066d8d2021-03-07 17:35:04 -0700442 */
443struct unit_test_state *test_get_state(void);
444
445/**
446 * test_set_state() - Set the active test state
447 *
448 * @uts: Test state to use as currently active test state, or NULL if none
449 */
450void test_set_state(struct unit_test_state *uts);
451
452/**
Simon Glass5722fb22021-03-07 17:34:47 -0700453 * ut_run_tests() - Run a set of tests
454 *
455 * This runs the test, handling any preparation and clean-up needed. It prints
456 * the name of each test before running it.
457 *
458 * @category: Category of these tests. This is a string printed at the start to
459 * announce the the number of tests
460 * @prefix: String prefix for the tests. Any tests that have this prefix will be
461 * printed without the prefix, so that it is easier to see the unique part
462 * of the test name. If NULL, no prefix processing is done
463 * @tests: List of tests to run
464 * @count: Number of tests to run
465 * @select_name: Name of a single test to run (from the list provided). If NULL
466 * then all tests are run
Simon Glass91a187b2022-08-01 07:58:45 -0600467 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glass1f1614b2022-10-20 18:22:50 -0600468 * @force_run: Run tests that are marked as manual-only (UT_TESTF_MANUAL)
Simon Glass85ba7c32022-10-29 19:47:13 -0600469 * @test_insert: String describing a test to run after n other tests run, in the
470 * format n:name where n is the number of tests to run before this one and
471 * name is the name of the test to run. This is used to find which test causes
472 * another test to fail. If the one test fails, testing stops immediately.
473 * Pass NULL to disable this
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100474 * Return: 0 if all tests passed, -1 if any failed
Simon Glass5722fb22021-03-07 17:34:47 -0700475 */
476int ut_run_list(const char *name, const char *prefix, struct unit_test *tests,
Simon Glass1f1614b2022-10-20 18:22:50 -0600477 int count, const char *select_name, int runs_per_test,
Simon Glass85ba7c32022-10-29 19:47:13 -0600478 bool force_run, const char *test_insert);
Simon Glass5722fb22021-03-07 17:34:47 -0700479
Simon Glassb2c1cac2014-02-26 15:59:21 -0700480#endif