Merge tag 'dm-pull-10apr20-take2' of git://git.denx.de/u-boot-dm
[oweals/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 <hexdump.h>
12 #include <linux/err.h>
13
14 struct unit_test_state;
15
16 /**
17  * ut_fail() - Record failure of a unit test
18  *
19  * @uts: Test state
20  * @fname: Filename where the error occurred
21  * @line: Line number where the error occurred
22  * @func: Function name where the error occurred
23  * @cond: The condition that failed
24  */
25 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
26              const char *func, const char *cond);
27
28 /**
29  * ut_failf() - Record failure of a unit test
30  *
31  * @uts: Test state
32  * @fname: Filename where the error occurred
33  * @line: Line number where the error occurred
34  * @func: Function name where the error occurred
35  * @cond: The condition that failed
36  * @fmt: printf() format string for the error, followed by args
37  */
38 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
39               const char *func, const char *cond, const char *fmt, ...)
40                         __attribute__ ((format (__printf__, 6, 7)));
41
42 /**
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  */
55 int 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  */
67 int 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  */
78 int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
79
80 /* Assert that a condition is non-zero */
81 #define ut_assert(cond)                                                 \
82         if (!(cond)) {                                                  \
83                 ut_fail(uts, __FILE__, __LINE__, __func__, #cond);      \
84                 return CMD_RET_FAILURE;                                 \
85         }
86
87 /* Assert that a condition is non-zero, with printf() string */
88 #define ut_assertf(cond, fmt, args...)                                  \
89         if (!(cond)) {                                                  \
90                 ut_failf(uts, __FILE__, __LINE__, __func__, #cond,      \
91                          fmt, ##args);                                  \
92                 return CMD_RET_FAILURE;                                 \
93         }
94
95 /* Assert that two int expressions are equal */
96 #define ut_asserteq(expr1, expr2) {                                     \
97         unsigned int _val1 = (expr1), _val2 = (expr2);                  \
98                                                                         \
99         if (_val1 != _val2) {                                           \
100                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
101                          #expr1 " == " #expr2,                          \
102                          "Expected %#x (%d), got %#x (%d)",             \
103                          _val1, _val1, _val2, _val2);                   \
104                 return CMD_RET_FAILURE;                                 \
105         }                                                               \
106 }
107
108 /* Assert that two 64 int expressions are equal */
109 #define ut_asserteq_64(expr1, expr2) {                                  \
110         u64 _val1 = (expr1), _val2 = (expr2);                           \
111                                                                         \
112         if (_val1 != _val2) {                                           \
113                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
114                          #expr1 " == " #expr2,                          \
115                          "Expected %#llx (%lld), got %#llx (%lld)",     \
116                          (unsigned long long)_val1,                     \
117                          (unsigned long long)_val1,                     \
118                          (unsigned long long)_val2,                     \
119                          (unsigned long long)_val2);                    \
120                 return CMD_RET_FAILURE;                                 \
121         }                                                               \
122 }
123
124 /* Assert that two string expressions are equal */
125 #define ut_asserteq_str(expr1, expr2) {                                 \
126         const char *_val1 = (expr1), *_val2 = (expr2);                  \
127                                                                         \
128         if (strcmp(_val1, _val2)) {                                     \
129                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
130                          #expr1 " = " #expr2,                           \
131                          "Expected \"%s\", got \"%s\"", _val1, _val2);  \
132                 return CMD_RET_FAILURE;                                 \
133         }                                                               \
134 }
135
136 /* Assert that two memory areas are equal */
137 #define ut_asserteq_mem(expr1, expr2, len) {                            \
138         const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2);        \
139         const uint __len = len;                                         \
140                                                                         \
141         if (memcmp(_val1, _val2, __len)) {                              \
142                 char __buf1[64 + 1] = "\0";                             \
143                 char __buf2[64 + 1] = "\0";                             \
144                 bin2hex(__buf1, _val1, min(__len, (uint)32));           \
145                 bin2hex(__buf2, _val2, min(__len, (uint)32));           \
146                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
147                          #expr1 " = " #expr2,                           \
148                          "Expected \"%s\", got \"%s\"",                 \
149                          __buf1, __buf2);                               \
150                 return CMD_RET_FAILURE;                                 \
151         }                                                               \
152 }
153
154 /* Assert that two pointers are equal */
155 #define ut_asserteq_ptr(expr1, expr2) {                                 \
156         const void *_val1 = (expr1), *_val2 = (expr2);                  \
157                                                                         \
158         if (_val1 != _val2) {                                           \
159                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
160                          #expr1 " = " #expr2,                           \
161                          "Expected %p, got %p", _val1, _val2);          \
162                 return CMD_RET_FAILURE;                                 \
163         }                                                               \
164 }
165
166 /* Assert that a pointer is NULL */
167 #define ut_assertnull(expr) {                                   \
168         const void *_val = (expr);                                      \
169                                                                         \
170         if (_val) {                                             \
171                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
172                          #expr " != NULL",                              \
173                          "Expected NULL, got %p", _val);                \
174                 return CMD_RET_FAILURE;                                 \
175         }                                                               \
176 }
177
178 /* Assert that a pointer is not NULL */
179 #define ut_assertnonnull(expr) {                                        \
180         const void *_val = (expr);                                      \
181                                                                         \
182         if (!_val) {                                            \
183                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
184                          #expr " = NULL",                               \
185                          "Expected non-null, got NULL");                \
186                 return CMD_RET_FAILURE;                                 \
187         }                                                               \
188 }
189
190 /* Assert that a pointer is not an error pointer */
191 #define ut_assertok_ptr(expr) {                                         \
192         const void *_val = (expr);                                      \
193                                                                         \
194         if (IS_ERR(_val)) {                                             \
195                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
196                          #expr " = NULL",                               \
197                          "Expected pointer, got error %ld",             \
198                          PTR_ERR(_val));                                \
199                 return CMD_RET_FAILURE;                                 \
200         }                                                               \
201 }
202
203 /* Assert that an operation succeeds (returns 0) */
204 #define ut_assertok(cond)       ut_asserteq(0, cond)
205
206 /* Assert that the next console output line matches */
207 #define ut_assert_nextline(fmt, args...)                                \
208         if (ut_check_console_line(uts, fmt, ##args)) {                  \
209                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
210                          "console", "\nExpected '%s',\n     got '%s'",  \
211                          uts->expect_str, uts->actual_str);             \
212                 return CMD_RET_FAILURE;                                 \
213         }                                                               \
214
215 /* Assert that there is no more console output */
216 #define ut_assert_console_end()                                         \
217         if (ut_check_console_end(uts)) {                                \
218                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
219                          "console", "Expected no more output, got '%s'",\
220                          uts->actual_str);                              \
221                 return CMD_RET_FAILURE;                                 \
222         }                                                               \
223
224 /* Assert that the next lines are print_buffer() dump at an address */
225 #define ut_assert_nextlines_are_dump(total_bytes)                       \
226         if (ut_check_console_dump(uts, total_bytes)) {                  \
227                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
228                          "console",                                     \
229                         "Expected dump of length %x bytes, got '%s'",   \
230                          total_bytes, uts->actual_str);                 \
231                 return CMD_RET_FAILURE;                                 \
232         }                                                               \
233
234 /**
235  * ut_check_free() - Return the number of bytes free in the malloc() pool
236  *
237  * @return bytes free
238  */
239 ulong ut_check_free(void);
240
241 /**
242  * ut_check_delta() - Return the number of bytes allocated/freed
243  *
244  * @last: Last value from ut_check_free
245  * @return free memory delta from @last; positive means more memory has been
246  *      allocated, negative means less has been allocated (i.e. some is freed)
247  */
248 long ut_check_delta(ulong last);
249
250 #endif