6ab2f8830df1f1c38554d6d4a1e0a11245725492
[platform/kernel/u-boot.git] / include / test / ut.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Simple unit test library
4  *
5  * Copyright (c) 2013 Google, Inc
6  */
7
8 #ifndef __TEST_UT_H
9 #define __TEST_UT_H
10
11 #include <command.h>
12 #include <hexdump.h>
13 #include <linux/err.h>
14 #include <test/test.h>
15
16 struct unit_test_state;
17
18 /**
19  * ut_fail() - Record failure of a unit test
20  *
21  * @uts: Test state
22  * @fname: Filename where the error occurred
23  * @line: Line number where the error occurred
24  * @func: Function name where the error occurred
25  * @cond: The condition that failed
26  */
27 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
28              const char *func, const char *cond);
29
30 /**
31  * ut_failf() - Record failure of a unit test
32  *
33  * @uts: Test state
34  * @fname: Filename where the error occurred
35  * @line: Line number where the error occurred
36  * @func: Function name where the error occurred
37  * @cond: The condition that failed
38  * @fmt: printf() format string for the error, followed by args
39  */
40 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
41               const char *func, const char *cond, const char *fmt, ...)
42                         __attribute__ ((format (__printf__, 6, 7)));
43
44 /**
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  */
57 int 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  */
69 int 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  */
80 int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
81
82 /* Assert that a condition is non-zero */
83 #define ut_assert(cond)                                                 \
84         if (!(cond)) {                                                  \
85                 ut_fail(uts, __FILE__, __LINE__, __func__, #cond);      \
86                 return CMD_RET_FAILURE;                                 \
87         }
88
89 /* Assert that a condition is non-zero, with printf() string */
90 #define ut_assertf(cond, fmt, args...)                                  \
91         if (!(cond)) {                                                  \
92                 ut_failf(uts, __FILE__, __LINE__, __func__, #cond,      \
93                          fmt, ##args);                                  \
94                 return CMD_RET_FAILURE;                                 \
95         }
96
97 /* Assert that two int expressions are equal */
98 #define ut_asserteq(expr1, expr2) {                                     \
99         unsigned int _val1 = (expr1), _val2 = (expr2);                  \
100                                                                         \
101         if (_val1 != _val2) {                                           \
102                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
103                          #expr1 " == " #expr2,                          \
104                          "Expected %#x (%d), got %#x (%d)",             \
105                          _val1, _val1, _val2, _val2);                   \
106                 return CMD_RET_FAILURE;                                 \
107         }                                                               \
108 }
109
110 /* 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
126 /* Assert that two string expressions are equal */
127 #define ut_asserteq_str(expr1, expr2) {                                 \
128         const char *_val1 = (expr1), *_val2 = (expr2);                  \
129                                                                         \
130         if (strcmp(_val1, _val2)) {                                     \
131                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
132                          #expr1 " = " #expr2,                           \
133                          "Expected \"%s\", got \"%s\"", _val1, _val2);  \
134                 return CMD_RET_FAILURE;                                 \
135         }                                                               \
136 }
137
138 /*
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
155 /* Assert that two memory areas are equal */
156 #define ut_asserteq_mem(expr1, expr2, len) {                            \
157         const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2);        \
158         const uint __len = len;                                         \
159                                                                         \
160         if (memcmp(_val1, _val2, __len)) {                              \
161                 char __buf1[64 + 1] = "\0";                             \
162                 char __buf2[64 + 1] = "\0";                             \
163                 bin2hex(__buf1, _val1, min(__len, (uint)32));           \
164                 bin2hex(__buf2, _val2, min(__len, (uint)32));           \
165                 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
173 /* Assert that two pointers are equal */
174 #define ut_asserteq_ptr(expr1, expr2) {                                 \
175         const void *_val1 = (expr1), *_val2 = (expr2);                  \
176                                                                         \
177         if (_val1 != _val2) {                                           \
178                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
179                          #expr1 " = " #expr2,                           \
180                          "Expected %p, got %p", _val1, _val2);          \
181                 return CMD_RET_FAILURE;                                 \
182         }                                                               \
183 }
184
185 /* Assert that a pointer is NULL */
186 #define ut_assertnull(expr) {                                   \
187         const void *_val = (expr);                                      \
188                                                                         \
189         if (_val) {                                             \
190                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
191                          #expr " != NULL",                              \
192                          "Expected NULL, got %p", _val);                \
193                 return CMD_RET_FAILURE;                                 \
194         }                                                               \
195 }
196
197 /* Assert that a pointer is not NULL */
198 #define ut_assertnonnull(expr) {                                        \
199         const void *_val = (expr);                                      \
200                                                                         \
201         if (!_val) {                                            \
202                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
203                          #expr " = NULL",                               \
204                          "Expected non-null, got NULL");                \
205                 return CMD_RET_FAILURE;                                 \
206         }                                                               \
207 }
208
209 /* Assert that a pointer is not an error pointer */
210 #define ut_assertok_ptr(expr) {                                         \
211         const void *_val = (expr);                                      \
212                                                                         \
213         if (IS_ERR(_val)) {                                             \
214                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
215                          #expr " = NULL",                               \
216                          "Expected pointer, got error %ld",             \
217                          PTR_ERR(_val));                                \
218                 return CMD_RET_FAILURE;                                 \
219         }                                                               \
220 }
221
222 /* Assert that an operation succeeds (returns 0) */
223 #define ut_assertok(cond)       ut_asserteq(0, cond)
224
225 /* 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
253 /**
254  * ut_check_free() - Return the number of bytes free in the malloc() pool
255  *
256  * @return bytes free
257  */
258 ulong 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  */
267 long ut_check_delta(ulong last);
268
269 #endif