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__); \
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__); \
79 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
81 printf("TestCase %s() line %d failed: " \
82 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
83 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
88 #define TEST_ASSERT_SUCCESS(val, msg, ...) do { \
89 typeof(val) _val = (val); \
91 printf("TestCase %s() line %d failed (err %d): " \
92 msg "\n", __func__, __LINE__, _val, \
94 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
99 #define TEST_ASSERT_FAIL(val, msg, ...) do { \
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; \
108 #define TEST_ASSERT_NULL(val, msg, ...) do { \
109 if (!(val == NULL)) { \
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_NOT_NULL(val, msg, ...) do { \
118 if (!(val != NULL)) { \
119 printf("TestCase %s() line %d failed: " \
120 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
121 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
122 return TEST_FAILED; \
126 struct unit_test_case {
128 void (*teardown)(void);
129 int (*testcase)(void);
130 const char *success_msg;
131 const char *fail_msg;
135 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed", 1 }
137 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
140 #define TEST_CASE_ST(setup, teardown, testcase) \
141 { setup, teardown, testcase, #testcase " succeeded", \
142 #testcase " failed ", 1 }
145 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn " succeeded", \
148 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
149 { setup, teardown, testcase, #testcase " succeeded", \
150 #testcase " failed ", 0 }
152 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0 }
154 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
155 #define TEST_HEXDUMP(file, title, buf, len) rte_hexdump(file, title, buf, len)
157 #define TEST_HEXDUMP(file, title, buf, len) do {} while (0)
160 struct unit_test_suite {
161 const char *suite_name;
163 void (*teardown)(void);
164 struct unit_test_case unit_test_cases[];
167 int unit_test_suite_runner(struct unit_test_suite *suite);
169 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
171 #include <cmdline_parse.h>
172 #include <cmdline_parse_string.h>
174 extern const char *prgname;
176 int commands_init(void);
181 int test_mp_secondary(void);
183 int test_ivshmem(void);
184 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
185 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
186 int test_set_rxtx_sc(cmdline_fixed_string_t type);
188 typedef int (test_callback)(void);
189 TAILQ_HEAD(test_commands_list, test_command);
190 struct test_command {
191 TAILQ_ENTRY(test_command) next;
193 test_callback *callback;
196 void add_test_command(struct test_command *t);
198 #define REGISTER_TEST_COMMAND(t) \
199 static void __attribute__((used)) testfn_##t(void);\
200 void __attribute__((constructor, used)) testfn_##t(void)\
202 add_test_command(&t);\