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.
37 #include <sys/queue.h>
40 #define TEST_SUCCESS (0)
41 #define TEST_FAILED (-1)
43 /* Before including test.h file you can define
44 * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
45 * failures. Mostly useful in test development phase. */
46 #ifndef TEST_TRACE_FAILURE
47 # define TEST_TRACE_FAILURE(_file, _line, _func)
50 #define TEST_ASSERT(cond, msg, ...) do { \
52 printf("TestCase %s() line %d failed: " \
53 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
54 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
59 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do { \
61 printf("TestCase %s() line %d failed: " \
62 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
63 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
68 /* Compare two buffers (length in bytes) */
69 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len, msg, ...) do { \
70 if (memcmp(a, b, len)) { \
71 printf("TestCase %s() line %d failed: " \
72 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
73 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
78 /* Compare two buffers with offset (length and offset in bytes) */
79 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
80 const uint8_t *_a_with_off = (const uint8_t *)a + off; \
81 const uint8_t *_b_with_off = (const uint8_t *)b + off; \
82 TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg); \
85 /* Compare two buffers (length in bits) */
86 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do { \
87 uint8_t _last_byte_a, _last_byte_b; \
88 uint8_t _last_byte_mask, _last_byte_bits; \
89 TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg); \
91 _last_byte_bits = len % 8; \
92 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
93 _last_byte_a = ((const uint8_t *)a)[len >> 3]; \
94 _last_byte_b = ((const uint8_t *)b)[len >> 3]; \
95 _last_byte_a &= _last_byte_mask; \
96 _last_byte_b &= _last_byte_mask; \
97 if (_last_byte_a != _last_byte_b) { \
98 printf("TestCase %s() line %d failed: " \
99 msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
100 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
101 return TEST_FAILED; \
106 /* Compare two buffers with offset (length and offset in bits) */
107 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
108 uint8_t _first_byte_a, _first_byte_b; \
109 uint8_t _first_byte_mask, _first_byte_bits; \
110 uint32_t _len_without_first_byte = (off % 8) ? \
111 len - (8 - (off % 8)) : \
113 uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3); \
114 const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes; \
115 const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes; \
116 TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off, \
117 _len_without_first_byte, msg); \
119 _first_byte_bits = 8 - (off % 8); \
120 _first_byte_mask = (1 << _first_byte_bits) - 1; \
121 _first_byte_a = *(_a_with_off - 1); \
122 _first_byte_b = *(_b_with_off - 1); \
123 _first_byte_a &= _first_byte_mask; \
124 _first_byte_b &= _first_byte_mask; \
125 if (_first_byte_a != _first_byte_b) { \
126 printf("TestCase %s() line %d failed: " \
127 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
128 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
129 return TEST_FAILED; \
134 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
136 printf("TestCase %s() line %d failed: " \
137 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
138 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
139 return TEST_FAILED; \
143 #define TEST_ASSERT_SUCCESS(val, msg, ...) do { \
144 typeof(val) _val = (val); \
145 if (!(_val == 0)) { \
146 printf("TestCase %s() line %d failed (err %d): " \
147 msg "\n", __func__, __LINE__, _val, \
149 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
150 return TEST_FAILED; \
154 #define TEST_ASSERT_FAIL(val, msg, ...) do { \
156 printf("TestCase %s() line %d failed: " \
157 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
158 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
159 return TEST_FAILED; \
163 #define TEST_ASSERT_NULL(val, msg, ...) do { \
164 if (!(val == NULL)) { \
165 printf("TestCase %s() line %d failed: " \
166 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
167 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
168 return TEST_FAILED; \
172 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \
173 if (!(val != NULL)) { \
174 printf("TestCase %s() line %d failed: " \
175 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
176 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
177 return TEST_FAILED; \
181 struct unit_test_case {
183 void (*teardown)(void);
184 int (*testcase)(void);
185 const char *success_msg;
186 const char *fail_msg;
190 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed", 1 }
192 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
195 #define TEST_CASE_ST(setup, teardown, testcase) \
196 { setup, teardown, testcase, #testcase " succeeded", \
197 #testcase " failed ", 1 }
200 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn " succeeded", \
203 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
204 { setup, teardown, testcase, #testcase " succeeded", \
205 #testcase " failed ", 0 }
207 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0 }
209 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
210 #define TEST_HEXDUMP(file, title, buf, len) rte_hexdump(file, title, buf, len)
212 #define TEST_HEXDUMP(file, title, buf, len) do {} while (0)
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);
236 int test_mp_secondary(void);
238 int test_ivshmem(void);
239 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
240 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
241 int test_set_rxtx_sc(cmdline_fixed_string_t type);
243 typedef int (test_callback)(void);
244 TAILQ_HEAD(test_commands_list, test_command);
245 struct test_command {
246 TAILQ_ENTRY(test_command) next;
248 test_callback *callback;
251 void add_test_command(struct test_command *t);
253 /* Register a test function with its command string */
254 #define REGISTER_TEST_COMMAND(cmd, func) \
255 static struct test_command test_struct_##cmd = { \
256 .command = RTE_STR(cmd), \
259 static void __attribute__((constructor, used)) \
260 test_register_##cmd(void) \
262 add_test_command(&test_struct_##cmd); \