test: rely on dynamic log level to display hexdumps
[dpdk.git] / test / 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
37 #include <stddef.h>
38 #include <sys/queue.h>
39
40 #include <rte_hexdump.h>
41 #include <rte_common.h>
42 #include <rte_log.h>
43
44 #define TEST_SUCCESS  (0)
45 #define TEST_FAILED  (-1)
46
47 /* Before including test.h file you can define
48  * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
49  * failures. Mostly useful in test development phase. */
50 #ifndef TEST_TRACE_FAILURE
51 # define TEST_TRACE_FAILURE(_file, _line, _func)
52 #endif
53
54 #define TEST_ASSERT(cond, msg, ...) do {                         \
55                 if (!(cond)) {                                           \
56                         printf("TestCase %s() line %d failed: "              \
57                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
58                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
59                         return TEST_FAILED;                                  \
60                 }                                                        \
61 } while (0)
62
63 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do {                   \
64                 if (!(a == b)) {                                         \
65                         printf("TestCase %s() line %d failed: "              \
66                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
67                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
68                         return TEST_FAILED;                                  \
69                 }                                                        \
70 } while (0)
71
72 /* Compare two buffers (length in bytes) */
73 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len,  msg, ...) do {        \
74         if (memcmp(a, b, len)) {                                        \
75                 printf("TestCase %s() line %d failed: "              \
76                         msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
77                 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
78                 return TEST_FAILED;                                  \
79         }                                                        \
80 } while (0)
81
82 /* Compare two buffers with offset (length and offset in bytes) */
83 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
84         const uint8_t *_a_with_off = (const uint8_t *)a + off;              \
85         const uint8_t *_b_with_off = (const uint8_t *)b + off;              \
86         TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg);  \
87 } while (0)
88
89 /* Compare two buffers (length in bits) */
90 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do {     \
91         uint8_t _last_byte_a, _last_byte_b;                       \
92         uint8_t _last_byte_mask, _last_byte_bits;                  \
93         TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg);     \
94         if (len % 8) {                                              \
95                 _last_byte_bits = len % 8;                   \
96                 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
97                 _last_byte_a = ((const uint8_t *)a)[len >> 3];            \
98                 _last_byte_b = ((const uint8_t *)b)[len >> 3];            \
99                 _last_byte_a &= _last_byte_mask;                     \
100                 _last_byte_b &= _last_byte_mask;                    \
101                 if (_last_byte_a != _last_byte_b) {                  \
102                         printf("TestCase %s() line %d failed: "              \
103                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
104                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
105                         return TEST_FAILED;                                  \
106                 }                                                        \
107         }                                                            \
108 } while (0)
109
110 /* Compare two buffers with offset (length and offset in bits) */
111 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
112         uint8_t _first_byte_a, _first_byte_b;                                 \
113         uint8_t _first_byte_mask, _first_byte_bits;                           \
114         uint32_t _len_without_first_byte = (off % 8) ?                       \
115                                 len - (8 - (off % 8)) :                       \
116                                 len;                                          \
117         uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3);     \
118         const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes;      \
119         const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes;      \
120         TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off,           \
121                                 _len_without_first_byte, msg);                \
122         if (off % 8) {                                                        \
123                 _first_byte_bits = 8 - (off % 8);                             \
124                 _first_byte_mask = (1 << _first_byte_bits) - 1;               \
125                 _first_byte_a = *(_a_with_off - 1);                           \
126                 _first_byte_b = *(_b_with_off - 1);                           \
127                 _first_byte_a &= _first_byte_mask;                            \
128                 _first_byte_b &= _first_byte_mask;                            \
129                 if (_first_byte_a != _first_byte_b) {                         \
130                         printf("TestCase %s() line %d failed: "               \
131                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
132                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);     \
133                         return TEST_FAILED;                                   \
134                 }                                                             \
135         }                                                                     \
136 } while (0)
137
138 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {               \
139                 if (!(a != b)) {                                         \
140                         printf("TestCase %s() line %d failed: "              \
141                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
142                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
143                         return TEST_FAILED;                                  \
144                 }                                                        \
145 } while (0)
146
147 #define TEST_ASSERT_SUCCESS(val, msg, ...) do {                  \
148                 typeof(val) _val = (val);                                \
149                 if (!(_val == 0)) {                                      \
150                         printf("TestCase %s() line %d failed (err %d): "     \
151                                 msg "\n", __func__, __LINE__, _val,              \
152                                 ##__VA_ARGS__);                                  \
153                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
154                         return TEST_FAILED;                                  \
155                 }                                                        \
156 } while (0)
157
158 #define TEST_ASSERT_FAIL(val, msg, ...) do {                     \
159                 if (!(val != 0)) {                                       \
160                         printf("TestCase %s() line %d failed: "              \
161                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
162                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
163                         return TEST_FAILED;                                  \
164                 }                                                        \
165 } while (0)
166
167 #define TEST_ASSERT_NULL(val, msg, ...) do {                     \
168                 if (!(val == NULL)) {                                    \
169                         printf("TestCase %s() line %d failed: "              \
170                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
171                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
172                         return TEST_FAILED;                                  \
173                 }                                                        \
174 } while (0)
175
176 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do {                 \
177                 if (!(val != NULL)) {                                    \
178                         printf("TestCase %s() line %d failed: "              \
179                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
180                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
181                         return TEST_FAILED;                                  \
182                 }                                                        \
183 } while (0)
184
185 struct unit_test_case {
186         int (*setup)(void);
187         void (*teardown)(void);
188         int (*testcase)(void);
189         const char *name;
190         unsigned enabled;
191 };
192
193 #define TEST_CASE(fn) { NULL, NULL, fn, #fn, 1 }
194
195 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name, 1 }
196
197 #define TEST_CASE_ST(setup, teardown, testcase) \
198                 { setup, teardown, testcase, #testcase, 1 }
199
200
201 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn, 0 }
202
203 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
204                 { setup, teardown, testcase, #testcase, 0 }
205
206 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, 0 }
207
208 static inline void
209 debug_hexdump(FILE *file, const char *title, const void *buf, size_t len)
210 {
211         if (rte_log_get_global_level() == RTE_LOG_DEBUG)
212                 rte_hexdump(file, title, buf, len);
213 }
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_mp_secondary(void);
234
235 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
236 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
237 int test_set_rxtx_sc(cmdline_fixed_string_t type);
238
239 typedef int (test_callback)(void);
240 TAILQ_HEAD(test_commands_list, test_command);
241 struct test_command {
242         TAILQ_ENTRY(test_command) next;
243         const char *command;
244         test_callback *callback;
245 };
246
247 void add_test_command(struct test_command *t);
248
249 /* Register a test function with its command string */
250 #define REGISTER_TEST_COMMAND(cmd, func) \
251         static struct test_command test_struct_##cmd = { \
252                 .command = RTE_STR(cmd), \
253                 .callback = func, \
254         }; \
255         static void __attribute__((constructor, used)) \
256         test_register_##cmd(void) \
257         { \
258                 add_test_command(&test_struct_##cmd); \
259         }
260
261 #endif