blob: 0ca9b5569760d7ac345c70cd3ce20ae360e173d9 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2013, Google, Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef _LIB_UNITTEST_INCLUDE_UNITTEST_H_
24#define _LIB_UNITTEST_INCLUDE_UNITTEST_H_
25/*
26 * Macros for writing unit tests.
27 *
28 * Sample usage:
29 *
30 * A test case runs a collection of tests like this, with
31 * BEGIN_TEST_CASE and END_TEST_CASE and the beginning and end of the
32 * function and RUN_TEST to call each individual test, as follows:
33 *
34 * BEGIN_TEST_CASE(foo_tests);
35 *
36 * RUN_TEST(test_foo);
37 * RUN_TEST(test_bar);
38 * RUN_TEST(test_baz);
39 *
40 * END_TEST_CASE;
41 *
42 * This creates a static function foo_tests() and registers it with the
43 * unit test framework. foo_tests() can be executed either by a shell
44 * command or by a call to run_all_tests(), which runs all registered
45 * unit tests.
46 *
47 * A test looks like this, using the BEGIN_TEST and END_TEST macros at
48 * the beginning and end of the test and the EXPECT_* macros to
49 * validate test results, as shown:
50 *
51 * static bool test_foo(void)
52 * {
53 * BEGIN_TEST;
54 *
55 * ...declare variables and do stuff...
56 * int foo_value = foo_func();
57 * ...See if the stuff produced the correct value...
58 * EXPECT_EQ(1, foo_value, "foo_func failed");
59 * ... there are EXPECT_* macros for many conditions...
60 * EXPECT_TRUE(foo_condition(), "condition should be true");
61 * EXPECT_NEQ(ERR_TIMED_OUT, foo_event(), "event timed out");
62 *
63 * END_TEST;
64 * }
65 *
66 * To your rules.mk file, add lib/unittest to MODULE_DEPS:
67 *
68 * MODULE_DEPS += \
69 * lib/unittest \
70 */
71#include <stdbool.h>
72#include <stdio.h>
73#include <stddef.h>
74#include <stdint.h>
75#include <string.h>
76#include <trace.h>
77#include <stdarg.h>
78
79#define PRINT_BUFFER_SIZE (512)
80
81/*
82 * Type for unit test result Output
83 */
84typedef void (*test_output_func) (const char *line, int len, void *arg);
85
86/*
87 * Printf dedicated to the unittest library
88 * the default output is the printf
89 */
90void unittest_printf (const char *format, ...);
91
92/*
93 * Function to set the callback for printing
94 * the unit test output
95 */
96void unittest_set_output_function (test_output_func fun, void *arg);
97
98/*
99 * Macros to format the error string
100 */
101#define EXPECTED_STRING "%s:\n expected "
102#define UNITTEST_TRACEF(str, x...) do { \
103 unittest_printf(" [FAILED] \n %s:%d:\n " str, \
104 __PRETTY_FUNCTION__, __LINE__, ## x); \
105 } while (0)
106
107/*
108 * BEGIN_TEST_CASE and END_TEST_CASE define a function that calls
109 * RUN_TEST.
110 */
111#define BEGIN_TEST_CASE(case_name) \
112 bool case_name(void) \
113 { \
114 bool all_success = true; \
115 unittest_printf("\nCASE %-49s [STARTED] \n", #case_name);
116
117#define DEFINE_REGISTER_TEST_CASE(case_name) \
118 static void _register_##case_name(void) \
119 { \
120 unittest_register_test_case(&_##case_name##_element); \
121 } \
122 void (*_register_##case_name##_ptr)(void) __SECTION(".ctors") = \
123 _register_##case_name;
124
125#define END_TEST_CASE(case_name) \
126 if (all_success) { \
127 unittest_printf("CASE %-59s [PASSED]\n", #case_name); \
128 } else { \
129 unittest_printf("CASE %-59s [FAILED]\n", #case_name); \
130 } \
131 return all_success; \
132 } \
133 static struct test_case_element _##case_name##_element = { \
134 .next = NULL, \
135 .failed_next = NULL, \
136 .name = #case_name, \
137 .test_case = case_name, \
138 }; \
139 DEFINE_REGISTER_TEST_CASE(case_name);
140
141#define RUN_TEST(test) \
142 unittest_printf(" %-50s [RUNNING]", #test ); \
143 if (! test ()) { \
144 all_success = false; \
145 } else { \
146 unittest_printf(" [PASSED] \n"); \
147 }
148
149/*
150 * BEGIN_TEST and END_TEST go in a function that is called by RUN_TEST
151 * and that call the EXPECT_ macros.
152 */
153#define BEGIN_TEST bool all_ok = true
154#define END_TEST return all_ok
155
156/*
157 * Use the EXPECT_* macros to check test results.
158 */
159#define EXPECT_EQ(expected, actual, msg) \
160 { \
161 typeof(actual) _e = expected; \
162 typeof(actual) _a = actual; \
163 if (_e != _a) { \
164 UNITTEST_TRACEF (EXPECTED_STRING #expected " (%d), " \
165 "actual " #actual " (%d)\n", \
166 msg, (int)_e, (int)_a); \
167 all_ok = false; \
168 } \
169 }
170
171#define EXPECT_NEQ(expected, actual, msg) \
172 { \
173 const typeof(expected) _e = expected; \
174 if (_e == actual) { \
175 UNITTEST_TRACEF(EXPECTED_STRING #expected " (%d), " #actual \
176 " to differ, but they are the same %d\n", \
177 msg, (int)_e); \
178 all_ok = false; \
179 } \
180 }
181
182#define EXPECT_LE(expected, actual, msg) \
183 { \
184 const typeof(actual) _e = expected; \
185 const typeof(actual) _a = actual; \
186 if (_e > _a) { \
187 UNITTEST_TRACEF(EXPECTED_STRING #expected " (%d) to be" \
188 " less-than-or-equal-to actual " \
189 #actual " (%d)\n", \
190 msg, (int)_e, (int)_a); \
191 all_ok = false; \
192 } \
193 }
194
195#define EXPECT_LT(expected, actual, msg) \
196 { \
197 const typeof(actual) _e = expected; \
198 const typeof(actual) _a = actual; \
199 if (_e >= _a) { \
200 UNITTEST_TRACEF(EXPECTED_STRING #expected " (%d) to be" \
201 " less-than actual " \
202 #actual " (%d)\n", \
203 msg, (int)_e, (int)_a); \
204 all_ok = false; \
205 } \
206 }
207
208#define EXPECT_GE(expected, actual, msg) \
209 { \
210 const typeof(actual) _e = expected; \
211 const typeof(actual) _a = actual; \
212 if (_e < _a) { \
213 UNITTEST_TRACEF(EXPECTED_STRING #expected " (%d) to be" \
214 " greater-than-or-equal-to actual " \
215 #actual " (%d)\n", \
216 msg, (int)_e, (int)_a); \
217 all_ok = false; \
218 } \
219 }
220
221#define EXPECT_GT(expected, actual, msg) \
222 { \
223 const typeof(actual) _e = expected; \
224 const typeof(actual) _a = actual; \
225 if (_e <= _a) { \
226 UNITTEST_TRACEF(EXPECTED_STRING #expected " (%d) to be" \
227 " greater-than actual " \
228 #actual " (%d)\n", \
229 msg, (int)_e, (int)_a); \
230 all_ok = false; \
231 } \
232 }
233
234#define EXPECT_TRUE(actual, msg) \
235 if (!(actual)) { \
236 UNITTEST_TRACEF("%s: " #actual " is false\n", msg); \
237 all_ok = false; \
238 }
239
240#define EXPECT_FALSE(actual, msg) \
241 if (actual) { \
242 UNITTEST_TRACEF("%s: " #actual " is true\n", msg); \
243 all_ok = false; \
244 }
245
246#define EXPECT_BYTES_EQ(expected, actual, length, msg) \
247 if (!expect_bytes_eq(expected, actual, length, msg)) { \
248 all_ok = false; \
249 }
250
251#define EXPECT_BYTES_NE(bytes1, bytes2, length, msg) \
252 if (!memcmp(bytes1, bytes2, length)) { \
253 UNITTEST_TRACEF(#bytes1 " and " #bytes2 " are the same; " \
254 "expected different\n"); \
255 hexdump8(bytes1, length); \
256 all_ok = false; \
257 }
258
259/* For comparing uint64_t, like hw_id_t. */
260#define EXPECT_EQ_LL(expected, actual, msg) \
261 { \
262 const typeof(actual) _e = expected; \
263 const typeof(actual) _a = actual; \
264 if (_e != _a) { \
265 UNITTEST_TRACEF("%s: expected %llu, actual %llu\n", \
266 msg, _e, _a); \
267 all_ok = false; \
268 } \
269 }
270
271/*
272 * The ASSERT_* macros are similar to the EXPECT_* macros except that
273 * they return on failure.
274 */
275#define ASSERT_NOT_NULL(p) \
276 if (!p) { \
277 UNITTEST_TRACEF("ERROR: NULL pointer\n"); \
278 return false; \
279 }
280
281/*
282 * The list of test cases is made up of these elements.
283 */
284struct test_case_element {
285 struct test_case_element *next;
286 struct test_case_element *failed_next;
287 const char *name;
288 bool (*test_case)(void);
289};
290
291
292/*
293 * Registers a test case with the unit test framework.
294 */
295void unittest_register_test_case(struct test_case_element *elem);
296
297/*
298 * Runs all registered test cases.
299 */
300bool run_all_tests(void);
301
302/*
303 * Returns false if expected does not equal actual and prints msg and a hexdump8
304 * of the input buffers.
305 */
306bool expect_bytes_eq(const uint8_t *expected, const uint8_t *actual, size_t len,
307 const char *msg);
308
309#endif /* _LIB_UNITTEST_INCLUDE_UNITTEST_H_ */