app/test: rework command registration
[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 /* 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__);    \
74                 return TEST_FAILED;                                  \
75         }                                                        \
76 } while (0)
77
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);  \
83 } while (0)
84
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);     \
90         if (len % 8) {                                              \
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;                                  \
102                 }                                                        \
103         }                                                            \
104 } while (0)
105
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)) :                       \
112                                 len;                                          \
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);                \
118         if (off % 8) {                                                        \
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;                                   \
130                 }                                                             \
131         }                                                                     \
132 } while (0)
133
134 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {               \
135                 if (!(a != b)) {                                         \
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;                                  \
140                 }                                                        \
141 } while (0)
142
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,              \
148                                 ##__VA_ARGS__);                                  \
149                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
150                         return TEST_FAILED;                                  \
151                 }                                                        \
152 } while (0)
153
154 #define TEST_ASSERT_FAIL(val, msg, ...) do {                     \
155                 if (!(val != 0)) {                                       \
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;                                  \
160                 }                                                        \
161 } while (0)
162
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;                                  \
169                 }                                                        \
170 } while (0)
171
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;                                  \
178                 }                                                        \
179 } while (0)
180
181 struct unit_test_case {
182         int (*setup)(void);
183         void (*teardown)(void);
184         int (*testcase)(void);
185         const char *success_msg;
186         const char *fail_msg;
187         unsigned enabled;
188 };
189
190 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed", 1 }
191
192 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
193                 name " failed", 1 }
194
195 #define TEST_CASE_ST(setup, teardown, testcase)         \
196                 { setup, teardown, testcase, #testcase " succeeded",    \
197                 #testcase " failed ", 1 }
198
199
200 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn " succeeded", \
201         #fn " failed", 0 }
202
203 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase)         \
204                 { setup, teardown, testcase, #testcase " succeeded",    \
205                 #testcase " failed ", 0 }
206
207 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0 }
208
209 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
210 #define TEST_HEXDUMP(file, title, buf, len) rte_hexdump(file, title, buf, len)
211 #else
212 #define TEST_HEXDUMP(file, title, buf, len) do {} while (0)
213 #endif
214
215 struct unit_test_suite {
216         const char *suite_name;
217         int (*setup)(void);
218         void (*teardown)(void);
219         struct unit_test_case unit_test_cases[];
220 };
221
222 int unit_test_suite_runner(struct unit_test_suite *suite);
223
224 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
225
226 #include <cmdline_parse.h>
227 #include <cmdline_parse_string.h>
228
229 extern const char *prgname;
230
231 int commands_init(void);
232
233 int test_pci(void);
234 int test_pci_run;
235
236 int test_mp_secondary(void);
237
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);
242
243 typedef int (test_callback)(void);
244 TAILQ_HEAD(test_commands_list, test_command);
245 struct test_command {
246         TAILQ_ENTRY(test_command) next;
247         const char *command;
248         test_callback *callback;
249 };
250
251 void add_test_command(struct test_command *t);
252
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), \
257                 .callback = func, \
258         }; \
259         static void __attribute__((constructor, used)) \
260         test_register_##cmd(void) \
261         { \
262                 add_test_command(&test_struct_##cmd); \
263         }
264
265 #endif