blob: c9fc9cc8395bb91b323106357cff884494b130cd [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
41
42/* Assert that a condition is non-zero */
43#define ut_assert(cond) \
44 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050045 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050046 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070047 }
48
49/* Assert that a condition is non-zero, with printf() string */
50#define ut_assertf(cond, fmt, args...) \
51 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050052 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070053 fmt, ##args); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050054 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070055 }
56
57/* Assert that two int expressions are equal */
58#define ut_asserteq(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -070059 unsigned int _val1 = (expr1), _val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -070060 \
Simon Glass43c336b2020-01-27 08:49:41 -070061 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050062 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070063 #expr1 " == " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -070064 "Expected %#x (%d), got %#x (%d)", \
65 _val1, _val1, _val2, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050066 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070067 } \
68}
69
70/* Assert that two string expressions are equal */
71#define ut_asserteq_str(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -070072 const char *_val1 = (expr1), *_val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -070073 \
Simon Glass43c336b2020-01-27 08:49:41 -070074 if (strcmp(_val1, _val2)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050075 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070076 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -070077 "Expected \"%s\", got \"%s\"", _val1, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050078 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070079 } \
80}
81
Mario Sixffdf8ab2018-09-27 09:19:32 +020082/* Assert that two memory areas are equal */
83#define ut_asserteq_mem(expr1, expr2, len) { \
Simon Glass43c336b2020-01-27 08:49:41 -070084 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
Mario Sixffdf8ab2018-09-27 09:19:32 +020085 const uint __len = len; \
86 \
Simon Glass43c336b2020-01-27 08:49:41 -070087 if (memcmp(_val1, _val2, __len)) { \
Mario Sixffdf8ab2018-09-27 09:19:32 +020088 char __buf1[64 + 1] = "\0"; \
89 char __buf2[64 + 1] = "\0"; \
Simon Glass43c336b2020-01-27 08:49:41 -070090 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
91 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
Mario Sixffdf8ab2018-09-27 09:19:32 +020092 ut_failf(uts, __FILE__, __LINE__, __func__, \
93 #expr1 " = " #expr2, \
94 "Expected \"%s\", got \"%s\"", \
95 __buf1, __buf2); \
96 return CMD_RET_FAILURE; \
97 } \
98}
99
Simon Glassb2c1cac2014-02-26 15:59:21 -0700100/* Assert that two pointers are equal */
101#define ut_asserteq_ptr(expr1, expr2) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700102 const void *_val1 = (expr1), *_val2 = (expr2); \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700103 \
Simon Glass43c336b2020-01-27 08:49:41 -0700104 if (_val1 != _val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500105 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700106 #expr1 " = " #expr2, \
Simon Glass43c336b2020-01-27 08:49:41 -0700107 "Expected %p, got %p", _val1, _val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500108 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -0700109 } \
110}
111
Ramon Friedbecc3e02018-06-21 17:47:16 +0300112/* Assert that a pointer is NULL */
113#define ut_assertnull(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700114 const void *_val = (expr); \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300115 \
Simon Glass43c336b2020-01-27 08:49:41 -0700116 if (_val) { \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300117 ut_failf(uts, __FILE__, __LINE__, __func__, \
118 #expr " != NULL", \
Simon Glass43c336b2020-01-27 08:49:41 -0700119 "Expected NULL, got %p", _val); \
Ramon Friedbecc3e02018-06-21 17:47:16 +0300120 return CMD_RET_FAILURE; \
121 } \
122}
123
Simon Glass7df766e2014-12-10 08:55:55 -0700124/* Assert that a pointer is not NULL */
125#define ut_assertnonnull(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700126 const void *_val = (expr); \
Simon Glass7df766e2014-12-10 08:55:55 -0700127 \
Simon Glass43c336b2020-01-27 08:49:41 -0700128 if (!_val) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500129 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass7df766e2014-12-10 08:55:55 -0700130 #expr " = NULL", \
131 "Expected non-null, got NULL"); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500132 return CMD_RET_FAILURE; \
Simon Glass7df766e2014-12-10 08:55:55 -0700133 } \
134}
135
Simon Glass7e1cebf2015-07-06 12:54:37 -0600136/* Assert that a pointer is not an error pointer */
Simon Glassd21afd52017-05-18 20:10:00 -0600137#define ut_assertok_ptr(expr) { \
Simon Glass43c336b2020-01-27 08:49:41 -0700138 const void *_val = (expr); \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600139 \
Simon Glass43c336b2020-01-27 08:49:41 -0700140 if (IS_ERR(_val)) { \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600141 ut_failf(uts, __FILE__, __LINE__, __func__, \
142 #expr " = NULL", \
143 "Expected pointer, got error %ld", \
Simon Glass43c336b2020-01-27 08:49:41 -0700144 PTR_ERR(_val)); \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600145 return CMD_RET_FAILURE; \
146 } \
147}
148
Simon Glassb2c1cac2014-02-26 15:59:21 -0700149/* Assert that an operation succeeds (returns 0) */
150#define ut_assertok(cond) ut_asserteq(0, cond)
151
Simon Glass19920d72019-12-29 21:19:23 -0700152/**
153 * ut_check_free() - Return the number of bytes free in the malloc() pool
154 *
155 * @return bytes free
156 */
157ulong ut_check_free(void);
158
159/**
160 * ut_check_delta() - Return the number of bytes allocated/freed
161 *
162 * @last: Last value from ut_check_free
163 * @return free memory delta from @last; positive means more memory has been
164 * allocated, negative means less has been allocated (i.e. some is freed)
165 */
166long ut_check_delta(ulong last);
167
Simon Glassb2c1cac2014-02-26 15:59:21 -0700168#endif