1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_hexdump.h>
12 #include <rte_common.h>
15 #define TEST_SUCCESS (0)
16 #define TEST_FAILED (-1)
18 /* Before including test.h file you can define
19 * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
20 * failures. Mostly useful in test development phase. */
21 #ifndef TEST_TRACE_FAILURE
22 # define TEST_TRACE_FAILURE(_file, _line, _func)
25 #define TEST_ASSERT(cond, msg, ...) do { \
27 printf("TestCase %s() line %d failed: " \
28 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
29 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
34 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do { \
36 printf("TestCase %s() line %d failed: " \
37 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
38 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
43 /* Compare two buffers (length in bytes) */
44 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len, msg, ...) do { \
45 if (memcmp(a, b, len)) { \
46 printf("TestCase %s() line %d failed: " \
47 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
48 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
53 /* Compare two buffers with offset (length and offset in bytes) */
54 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
55 const uint8_t *_a_with_off = (const uint8_t *)a + off; \
56 const uint8_t *_b_with_off = (const uint8_t *)b + off; \
57 TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg); \
60 /* Compare two buffers (length in bits) */
61 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do { \
62 uint8_t _last_byte_a, _last_byte_b; \
63 uint8_t _last_byte_mask, _last_byte_bits; \
64 TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg); \
66 _last_byte_bits = len % 8; \
67 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
68 _last_byte_a = ((const uint8_t *)a)[len >> 3]; \
69 _last_byte_b = ((const uint8_t *)b)[len >> 3]; \
70 _last_byte_a &= _last_byte_mask; \
71 _last_byte_b &= _last_byte_mask; \
72 if (_last_byte_a != _last_byte_b) { \
73 printf("TestCase %s() line %d failed: " \
74 msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
75 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
81 /* Compare two buffers with offset (length and offset in bits) */
82 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
83 uint8_t _first_byte_a, _first_byte_b; \
84 uint8_t _first_byte_mask, _first_byte_bits; \
85 uint32_t _len_without_first_byte = (off % 8) ? \
86 len - (8 - (off % 8)) : \
88 uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3); \
89 const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes; \
90 const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes; \
91 TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off, \
92 _len_without_first_byte, msg); \
94 _first_byte_bits = 8 - (off % 8); \
95 _first_byte_mask = (1 << _first_byte_bits) - 1; \
96 _first_byte_a = *(_a_with_off - 1); \
97 _first_byte_b = *(_b_with_off - 1); \
98 _first_byte_a &= _first_byte_mask; \
99 _first_byte_b &= _first_byte_mask; \
100 if (_first_byte_a != _first_byte_b) { \
101 printf("TestCase %s() line %d failed: " \
102 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
103 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
104 return TEST_FAILED; \
109 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
111 printf("TestCase %s() line %d failed: " \
112 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
113 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
114 return TEST_FAILED; \
118 #define TEST_ASSERT_SUCCESS(val, msg, ...) do { \
119 typeof(val) _val = (val); \
120 if (!(_val == 0)) { \
121 printf("TestCase %s() line %d failed (err %d): " \
122 msg "\n", __func__, __LINE__, _val, \
124 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
125 return TEST_FAILED; \
129 #define TEST_ASSERT_FAIL(val, msg, ...) do { \
131 printf("TestCase %s() line %d failed: " \
132 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
133 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
134 return TEST_FAILED; \
138 #define TEST_ASSERT_NULL(val, msg, ...) do { \
139 if (!(val == NULL)) { \
140 printf("TestCase %s() line %d failed: " \
141 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
142 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
143 return TEST_FAILED; \
147 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \
148 if (!(val != NULL)) { \
149 printf("TestCase %s() line %d failed: " \
150 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
151 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
152 return TEST_FAILED; \
156 struct unit_test_case {
158 void (*teardown)(void);
159 int (*testcase)(void);
164 #define TEST_CASE(fn) { NULL, NULL, fn, #fn, 1 }
166 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name, 1 }
168 #define TEST_CASE_ST(setup, teardown, testcase) \
169 { setup, teardown, testcase, #testcase, 1 }
172 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn, 0 }
174 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
175 { setup, teardown, testcase, #testcase, 0 }
177 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, 0 }
180 debug_hexdump(FILE *file, const char *title, const void *buf, size_t len)
182 if (rte_log_get_global_level() == RTE_LOG_DEBUG)
183 rte_hexdump(file, title, buf, len);
186 struct unit_test_suite {
187 const char *suite_name;
189 void (*teardown)(void);
190 struct unit_test_case unit_test_cases[];
193 int unit_test_suite_runner(struct unit_test_suite *suite);
195 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
197 #include <cmdline_parse.h>
198 #include <cmdline_parse_string.h>
200 extern const char *prgname;
202 int commands_init(void);
204 int test_mp_secondary(void);
206 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
207 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
208 int test_set_rxtx_sc(cmdline_fixed_string_t type);
210 typedef int (test_callback)(void);
211 TAILQ_HEAD(test_commands_list, test_command);
212 struct test_command {
213 TAILQ_ENTRY(test_command) next;
215 test_callback *callback;
218 void add_test_command(struct test_command *t);
220 /* Register a test function with its command string */
221 #define REGISTER_TEST_COMMAND(cmd, func) \
222 static struct test_command test_struct_##cmd = { \
223 .command = RTE_STR(cmd), \
226 static void __attribute__((constructor, used)) \
227 test_register_##cmd(void) \
229 add_test_command(&test_struct_##cmd); \