4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/queue.h>
40 #include <rte_hexdump.h>
41 #include <rte_common.h>
44 #define TEST_SUCCESS (0)
45 #define TEST_FAILED (-1)
47 /* Before including test.h file you can define
48 * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
49 * failures. Mostly useful in test development phase. */
50 #ifndef TEST_TRACE_FAILURE
51 # define TEST_TRACE_FAILURE(_file, _line, _func)
54 #define TEST_ASSERT(cond, msg, ...) do { \
56 printf("TestCase %s() line %d failed: " \
57 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
58 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
63 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do { \
65 printf("TestCase %s() line %d failed: " \
66 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
67 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
72 /* Compare two buffers (length in bytes) */
73 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len, msg, ...) do { \
74 if (memcmp(a, b, len)) { \
75 printf("TestCase %s() line %d failed: " \
76 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
77 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
82 /* Compare two buffers with offset (length and offset in bytes) */
83 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
84 const uint8_t *_a_with_off = (const uint8_t *)a + off; \
85 const uint8_t *_b_with_off = (const uint8_t *)b + off; \
86 TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg); \
89 /* Compare two buffers (length in bits) */
90 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do { \
91 uint8_t _last_byte_a, _last_byte_b; \
92 uint8_t _last_byte_mask, _last_byte_bits; \
93 TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg); \
95 _last_byte_bits = len % 8; \
96 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
97 _last_byte_a = ((const uint8_t *)a)[len >> 3]; \
98 _last_byte_b = ((const uint8_t *)b)[len >> 3]; \
99 _last_byte_a &= _last_byte_mask; \
100 _last_byte_b &= _last_byte_mask; \
101 if (_last_byte_a != _last_byte_b) { \
102 printf("TestCase %s() line %d failed: " \
103 msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
104 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
105 return TEST_FAILED; \
110 /* Compare two buffers with offset (length and offset in bits) */
111 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
112 uint8_t _first_byte_a, _first_byte_b; \
113 uint8_t _first_byte_mask, _first_byte_bits; \
114 uint32_t _len_without_first_byte = (off % 8) ? \
115 len - (8 - (off % 8)) : \
117 uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3); \
118 const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes; \
119 const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes; \
120 TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off, \
121 _len_without_first_byte, msg); \
123 _first_byte_bits = 8 - (off % 8); \
124 _first_byte_mask = (1 << _first_byte_bits) - 1; \
125 _first_byte_a = *(_a_with_off - 1); \
126 _first_byte_b = *(_b_with_off - 1); \
127 _first_byte_a &= _first_byte_mask; \
128 _first_byte_b &= _first_byte_mask; \
129 if (_first_byte_a != _first_byte_b) { \
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; \
138 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
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_SUCCESS(val, msg, ...) do { \
148 typeof(val) _val = (val); \
149 if (!(_val == 0)) { \
150 printf("TestCase %s() line %d failed (err %d): " \
151 msg "\n", __func__, __LINE__, _val, \
153 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
154 return TEST_FAILED; \
158 #define TEST_ASSERT_FAIL(val, msg, ...) do { \
160 printf("TestCase %s() line %d failed: " \
161 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
162 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
163 return TEST_FAILED; \
167 #define TEST_ASSERT_NULL(val, msg, ...) do { \
168 if (!(val == NULL)) { \
169 printf("TestCase %s() line %d failed: " \
170 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
171 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
172 return TEST_FAILED; \
176 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \
177 if (!(val != NULL)) { \
178 printf("TestCase %s() line %d failed: " \
179 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
180 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
181 return TEST_FAILED; \
185 struct unit_test_case {
187 void (*teardown)(void);
188 int (*testcase)(void);
193 #define TEST_CASE(fn) { NULL, NULL, fn, #fn, 1 }
195 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name, 1 }
197 #define TEST_CASE_ST(setup, teardown, testcase) \
198 { setup, teardown, testcase, #testcase, 1 }
201 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn, 0 }
203 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
204 { setup, teardown, testcase, #testcase, 0 }
206 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, 0 }
209 debug_hexdump(FILE *file, const char *title, const void *buf, size_t len)
211 if (rte_log_get_global_level() == RTE_LOG_DEBUG)
212 rte_hexdump(file, title, buf, len);
215 struct unit_test_suite {
216 const char *suite_name;
218 void (*teardown)(void);
219 struct unit_test_case unit_test_cases[];
222 int unit_test_suite_runner(struct unit_test_suite *suite);
224 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
226 #include <cmdline_parse.h>
227 #include <cmdline_parse_string.h>
229 extern const char *prgname;
231 int commands_init(void);
233 int test_mp_secondary(void);
235 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
236 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
237 int test_set_rxtx_sc(cmdline_fixed_string_t type);
239 typedef int (test_callback)(void);
240 TAILQ_HEAD(test_commands_list, test_command);
241 struct test_command {
242 TAILQ_ENTRY(test_command) next;
244 test_callback *callback;
247 void add_test_command(struct test_command *t);
249 /* Register a test function with its command string */
250 #define REGISTER_TEST_COMMAND(cmd, func) \
251 static struct test_command test_struct_##cmd = { \
252 .command = RTE_STR(cmd), \
255 static void __attribute__((constructor, used)) \
256 test_register_##cmd(void) \
258 add_test_command(&test_struct_##cmd); \