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_common.h>
43 #define TEST_SUCCESS (0)
44 #define TEST_FAILED (-1)
46 /* Before including test.h file you can define
47 * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
48 * failures. Mostly useful in test development phase. */
49 #ifndef TEST_TRACE_FAILURE
50 # define TEST_TRACE_FAILURE(_file, _line, _func)
53 #define TEST_ASSERT(cond, msg, ...) do { \
55 printf("TestCase %s() line %d failed: " \
56 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
57 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
62 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do { \
64 printf("TestCase %s() line %d failed: " \
65 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
66 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
71 /* Compare two buffers (length in bytes) */
72 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len, msg, ...) do { \
73 if (memcmp(a, b, len)) { \
74 printf("TestCase %s() line %d failed: " \
75 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
76 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
81 /* Compare two buffers with offset (length and offset in bytes) */
82 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
83 const uint8_t *_a_with_off = (const uint8_t *)a + off; \
84 const uint8_t *_b_with_off = (const uint8_t *)b + off; \
85 TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg); \
88 /* Compare two buffers (length in bits) */
89 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do { \
90 uint8_t _last_byte_a, _last_byte_b; \
91 uint8_t _last_byte_mask, _last_byte_bits; \
92 TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg); \
94 _last_byte_bits = len % 8; \
95 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
96 _last_byte_a = ((const uint8_t *)a)[len >> 3]; \
97 _last_byte_b = ((const uint8_t *)b)[len >> 3]; \
98 _last_byte_a &= _last_byte_mask; \
99 _last_byte_b &= _last_byte_mask; \
100 if (_last_byte_a != _last_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 /* Compare two buffers with offset (length and offset in bits) */
110 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
111 uint8_t _first_byte_a, _first_byte_b; \
112 uint8_t _first_byte_mask, _first_byte_bits; \
113 uint32_t _len_without_first_byte = (off % 8) ? \
114 len - (8 - (off % 8)) : \
116 uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3); \
117 const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes; \
118 const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes; \
119 TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off, \
120 _len_without_first_byte, msg); \
122 _first_byte_bits = 8 - (off % 8); \
123 _first_byte_mask = (1 << _first_byte_bits) - 1; \
124 _first_byte_a = *(_a_with_off - 1); \
125 _first_byte_b = *(_b_with_off - 1); \
126 _first_byte_a &= _first_byte_mask; \
127 _first_byte_b &= _first_byte_mask; \
128 if (_first_byte_a != _first_byte_b) { \
129 printf("TestCase %s() line %d failed: " \
130 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
131 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
132 return TEST_FAILED; \
137 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
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_SUCCESS(val, msg, ...) do { \
147 typeof(val) _val = (val); \
148 if (!(_val == 0)) { \
149 printf("TestCase %s() line %d failed (err %d): " \
150 msg "\n", __func__, __LINE__, _val, \
152 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
153 return TEST_FAILED; \
157 #define TEST_ASSERT_FAIL(val, msg, ...) do { \
159 printf("TestCase %s() line %d failed: " \
160 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
161 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
162 return TEST_FAILED; \
166 #define TEST_ASSERT_NULL(val, msg, ...) do { \
167 if (!(val == NULL)) { \
168 printf("TestCase %s() line %d failed: " \
169 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
170 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
171 return TEST_FAILED; \
175 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \
176 if (!(val != NULL)) { \
177 printf("TestCase %s() line %d failed: " \
178 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
179 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
180 return TEST_FAILED; \
184 struct unit_test_case {
186 void (*teardown)(void);
187 int (*testcase)(void);
192 #define TEST_CASE(fn) { NULL, NULL, fn, #fn, 1 }
194 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name, 1 }
196 #define TEST_CASE_ST(setup, teardown, testcase) \
197 { setup, teardown, testcase, #testcase, 1 }
200 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn, 0 }
202 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
203 { setup, teardown, testcase, #testcase, 0 }
205 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, 0 }
207 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
208 #define TEST_HEXDUMP(file, title, buf, len) rte_hexdump(file, title, buf, len)
210 #define TEST_HEXDUMP(file, title, buf, len) do {} while (0)
213 struct unit_test_suite {
214 const char *suite_name;
216 void (*teardown)(void);
217 struct unit_test_case unit_test_cases[];
220 int unit_test_suite_runner(struct unit_test_suite *suite);
222 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
224 #include <cmdline_parse.h>
225 #include <cmdline_parse_string.h>
227 extern const char *prgname;
229 int commands_init(void);
231 int test_mp_secondary(void);
233 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
234 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
235 int test_set_rxtx_sc(cmdline_fixed_string_t type);
237 typedef int (test_callback)(void);
238 TAILQ_HEAD(test_commands_list, test_command);
239 struct test_command {
240 TAILQ_ENTRY(test_command) next;
242 test_callback *callback;
245 void add_test_command(struct test_command *t);
247 /* Register a test function with its command string */
248 #define REGISTER_TEST_COMMAND(cmd, func) \
249 static struct test_command test_struct_##cmd = { \
250 .command = RTE_STR(cmd), \
253 static void __attribute__((constructor, used)) \
254 test_register_##cmd(void) \
256 add_test_command(&test_struct_##cmd); \