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>
39 #define TEST_SUCCESS (0)
40 #define TEST_FAILED (-1)
42 /* Before including test.h file you can define
43 * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
44 * failures. Mostly useful in test development phase. */
45 #ifndef TEST_TRACE_FAILURE
46 # define TEST_TRACE_FAILURE(_file, _line, _func)
49 #define TEST_ASSERT(cond, msg, ...) do { \
51 printf("TestCase %s() line %d failed: " \
52 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
53 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
58 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do { \
60 printf("TestCase %s() line %d failed: " \
61 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
62 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
68 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len, msg, ...) do { \
69 if (memcmp(a, b, len)) { \
70 printf("TestCase %s() line %d failed: " \
71 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
72 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
78 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
80 printf("TestCase %s() line %d failed: " \
81 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
82 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
87 #define TEST_ASSERT_SUCCESS(val, msg, ...) do { \
88 typeof(val) _val = (val); \
90 printf("TestCase %s() line %d failed (err %d): " \
91 msg "\n", __func__, __LINE__, _val, \
93 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
98 #define TEST_ASSERT_FAIL(val, msg, ...) do { \
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; \
107 #define TEST_ASSERT_NULL(val, msg, ...) do { \
108 if (!(val == NULL)) { \
109 printf("TestCase %s() line %d failed: " \
110 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
111 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
112 return TEST_FAILED; \
116 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \
117 if (!(val != NULL)) { \
118 printf("TestCase %s() line %d failed: " \
119 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
120 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \
121 return TEST_FAILED; \
125 struct unit_test_case {
127 void (*teardown)(void);
128 int (*testcase)(void);
129 const char *success_msg;
130 const char *fail_msg;
134 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed", 1 }
136 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
139 #define TEST_CASE_ST(setup, teardown, testcase) \
140 { setup, teardown, testcase, #testcase " succeeded", \
141 #testcase " failed ", 1 }
144 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn " succeeded", \
147 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
148 { setup, teardown, testcase, #testcase " succeeded", \
149 #testcase " failed ", 0 }
151 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0 }
153 struct unit_test_suite {
154 const char *suite_name;
156 void (*teardown)(void);
157 struct unit_test_case unit_test_cases[];
160 int unit_test_suite_runner(struct unit_test_suite *suite);
162 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
164 #include <cmdline_parse.h>
165 #include <cmdline_parse_string.h>
167 extern const char *prgname;
169 int commands_init(void);
174 int test_mp_secondary(void);
176 int test_ivshmem(void);
177 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
178 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
179 int test_set_rxtx_sc(cmdline_fixed_string_t type);
181 typedef int (test_callback)(void);
182 TAILQ_HEAD(test_commands_list, test_command);
183 struct test_command {
184 TAILQ_ENTRY(test_command) next;
186 test_callback *callback;
189 void add_test_command(struct test_command *t);
191 #define REGISTER_TEST_COMMAND(t) \
192 static void __attribute__((used)) testfn_##t(void);\
193 void __attribute__((constructor, used)) testfn_##t(void)\
195 add_test_command(&t);\