app/test: use hexdump if debug log is enabled
[dpdk.git] / app / test / test.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #ifndef _TEST_H_
35 #define _TEST_H_
36 #include <stddef.h>
37 #include <sys/queue.h>
38 #include "rte_log.h"
39
40 #define TEST_SUCCESS  (0)
41 #define TEST_FAILED  (-1)
42
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)
48 #endif
49
50 #define TEST_ASSERT(cond, msg, ...) do {                         \
51                 if (!(cond)) {                                           \
52                         printf("TestCase %s() line %d failed: "              \
53                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
54                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
55                         return TEST_FAILED;                                  \
56                 }                                                        \
57 } while (0)
58
59 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do {                   \
60                 if (!(a == b)) {                                         \
61                         printf("TestCase %s() line %d failed: "              \
62                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
63                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
64                         return TEST_FAILED;                                  \
65                 }                                                        \
66 } while (0)
67
68
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__);    \
74                 return TEST_FAILED;                                  \
75         }                                                        \
76 } while (0)
77
78
79 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {               \
80                 if (!(a != b)) {                                         \
81                         printf("TestCase %s() line %d failed: "              \
82                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
83                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
84                         return TEST_FAILED;                                  \
85                 }                                                        \
86 } while (0)
87
88 #define TEST_ASSERT_SUCCESS(val, msg, ...) do {                  \
89                 typeof(val) _val = (val);                                \
90                 if (!(_val == 0)) {                                      \
91                         printf("TestCase %s() line %d failed (err %d): "     \
92                                 msg "\n", __func__, __LINE__, _val,              \
93                                 ##__VA_ARGS__);                                  \
94                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
95                         return TEST_FAILED;                                  \
96                 }                                                        \
97 } while (0)
98
99 #define TEST_ASSERT_FAIL(val, msg, ...) do {                     \
100                 if (!(val != 0)) {                                       \
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;                                  \
105                 }                                                        \
106 } while (0)
107
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;                                  \
114                 }                                                        \
115 } while (0)
116
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;                                  \
123                 }                                                        \
124 } while (0)
125
126 struct unit_test_case {
127         int (*setup)(void);
128         void (*teardown)(void);
129         int (*testcase)(void);
130         const char *success_msg;
131         const char *fail_msg;
132         unsigned enabled;
133 };
134
135 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed", 1 }
136
137 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
138                 name " failed", 1 }
139
140 #define TEST_CASE_ST(setup, teardown, testcase)         \
141                 { setup, teardown, testcase, #testcase " succeeded",    \
142                 #testcase " failed ", 1 }
143
144
145 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn " succeeded", \
146         #fn " failed", 0 }
147
148 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase)         \
149                 { setup, teardown, testcase, #testcase " succeeded",    \
150                 #testcase " failed ", 0 }
151
152 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0 }
153
154 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
155 #define TEST_HEXDUMP(file, title, buf, len) rte_hexdump(file, title, buf, len)
156 #else
157 #define TEST_HEXDUMP(file, title, buf, len) do {} while (0)
158 #endif
159
160 struct unit_test_suite {
161         const char *suite_name;
162         int (*setup)(void);
163         void (*teardown)(void);
164         struct unit_test_case unit_test_cases[];
165 };
166
167 int unit_test_suite_runner(struct unit_test_suite *suite);
168
169 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
170
171 #include <cmdline_parse.h>
172 #include <cmdline_parse_string.h>
173
174 extern const char *prgname;
175
176 int commands_init(void);
177
178 int test_pci(void);
179 int test_pci_run;
180
181 int test_mp_secondary(void);
182
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);
187
188 typedef int (test_callback)(void);
189 TAILQ_HEAD(test_commands_list, test_command);
190 struct test_command {
191         TAILQ_ENTRY(test_command) next;
192         const char *command;
193         test_callback *callback;
194 };
195
196 void add_test_command(struct test_command *t);
197
198 #define REGISTER_TEST_COMMAND(t) \
199 static void __attribute__((used)) testfn_##t(void);\
200 void __attribute__((constructor, used)) testfn_##t(void)\
201 {\
202         add_test_command(&t);\
203 }
204
205 #endif