1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_common.h>
14 #define TEST_SUCCESS (0)
15 #define TEST_FAILED (-1)
17 /* Before including test.h file you can define
18 * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
19 * failures. Mostly useful in test development phase. */
20 #ifndef TEST_TRACE_FAILURE
21 # define TEST_TRACE_FAILURE(_file, _line, _func)
24 #define TEST_ASSERT(cond, msg, ...) do { \
26 printf("TestCase %s() line %d failed: " \
27 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
28 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
33 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do { \
35 printf("TestCase %s() line %d failed: " \
36 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
37 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
42 /* Compare two buffers (length in bytes) */
43 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len, msg, ...) do { \
44 if (memcmp(a, b, len)) { \
45 printf("TestCase %s() line %d failed: " \
46 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
47 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
52 /* Compare two buffers with offset (length and offset in bytes) */
53 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
54 const uint8_t *_a_with_off = (const uint8_t *)a + off; \
55 const uint8_t *_b_with_off = (const uint8_t *)b + off; \
56 TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg); \
59 /* Compare two buffers (length in bits) */
60 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do { \
61 uint8_t _last_byte_a, _last_byte_b; \
62 uint8_t _last_byte_mask, _last_byte_bits; \
63 TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg); \
65 _last_byte_bits = len % 8; \
66 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
67 _last_byte_a = ((const uint8_t *)a)[len >> 3]; \
68 _last_byte_b = ((const uint8_t *)b)[len >> 3]; \
69 _last_byte_a &= _last_byte_mask; \
70 _last_byte_b &= _last_byte_mask; \
71 if (_last_byte_a != _last_byte_b) { \
72 printf("TestCase %s() line %d failed: " \
73 msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
74 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
80 /* Compare two buffers with offset (length and offset in bits) */
81 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
82 uint8_t _first_byte_a, _first_byte_b; \
83 uint8_t _first_byte_mask, _first_byte_bits; \
84 uint32_t _len_without_first_byte = (off % 8) ? \
85 len - (8 - (off % 8)) : \
87 uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3); \
88 const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes; \
89 const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes; \
90 TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off, \
91 _len_without_first_byte, msg); \
93 _first_byte_bits = 8 - (off % 8); \
94 _first_byte_mask = (1 << _first_byte_bits) - 1; \
95 _first_byte_a = *(_a_with_off - 1); \
96 _first_byte_b = *(_b_with_off - 1); \
97 _first_byte_a &= _first_byte_mask; \
98 _first_byte_b &= _first_byte_mask; \
99 if (_first_byte_a != _first_byte_b) { \
100 printf("TestCase %s() line %d failed: " \
101 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
102 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
103 return TEST_FAILED; \
108 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
110 printf("TestCase %s() line %d failed: " \
111 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
112 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
113 return TEST_FAILED; \
117 #define TEST_ASSERT_SUCCESS(val, msg, ...) do { \
118 typeof(val) _val = (val); \
119 if (!(_val == 0)) { \
120 printf("TestCase %s() line %d failed (err %d): " \
121 msg "\n", __func__, __LINE__, _val, \
123 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
124 return TEST_FAILED; \
128 #define TEST_ASSERT_FAIL(val, msg, ...) do { \
130 printf("TestCase %s() line %d failed: " \
131 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
132 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
133 return TEST_FAILED; \
137 #define TEST_ASSERT_NULL(val, msg, ...) do { \
138 if (!(val == NULL)) { \
139 printf("TestCase %s() line %d failed: " \
140 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
141 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
142 return TEST_FAILED; \
146 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \
147 if (!(val != NULL)) { \
148 printf("TestCase %s() line %d failed: " \
149 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
150 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
151 return TEST_FAILED; \
155 struct unit_test_case {
157 void (*teardown)(void);
158 int (*testcase)(void);
163 #define TEST_CASE(fn) { NULL, NULL, fn, #fn, 1 }
165 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name, 1 }
167 #define TEST_CASE_ST(setup, teardown, testcase) \
168 { setup, teardown, testcase, #testcase, 1 }
171 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn, 0 }
173 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
174 { setup, teardown, testcase, #testcase, 0 }
176 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, 0 }
178 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
179 #define TEST_HEXDUMP(file, title, buf, len) rte_hexdump(file, title, buf, len)
181 #define TEST_HEXDUMP(file, title, buf, len) do {} while (0)
184 struct unit_test_suite {
185 const char *suite_name;
187 void (*teardown)(void);
188 struct unit_test_case unit_test_cases[];
191 int unit_test_suite_runner(struct unit_test_suite *suite);
193 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
195 #include <cmdline_parse.h>
196 #include <cmdline_parse_string.h>
198 extern const char *prgname;
200 int commands_init(void);
202 int test_mp_secondary(void);
204 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
205 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
206 int test_set_rxtx_sc(cmdline_fixed_string_t type);
208 typedef int (test_callback)(void);
209 TAILQ_HEAD(test_commands_list, test_command);
210 struct test_command {
211 TAILQ_ENTRY(test_command) next;
213 test_callback *callback;
216 void add_test_command(struct test_command *t);
218 /* Register a test function with its command string */
219 #define REGISTER_TEST_COMMAND(cmd, func) \
220 static struct test_command test_struct_##cmd = { \
221 .command = RTE_STR(cmd), \
224 static void __attribute__((constructor, used)) \
225 test_register_##cmd(void) \
227 add_test_command(&test_struct_##cmd); \