test: rely on dynamic log level to display hexdumps
[dpdk.git] / test / test / test.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _TEST_H_
6 #define _TEST_H_
7
8 #include <stddef.h>
9 #include <sys/queue.h>
10
11 #include <rte_hexdump.h>
12 #include <rte_common.h>
13 #include <rte_log.h>
14
15 #define TEST_SUCCESS  (0)
16 #define TEST_FAILED  (-1)
17
18 /* Before including test.h file you can define
19  * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
20  * failures. Mostly useful in test development phase. */
21 #ifndef TEST_TRACE_FAILURE
22 # define TEST_TRACE_FAILURE(_file, _line, _func)
23 #endif
24
25 #define TEST_ASSERT(cond, msg, ...) do {                         \
26                 if (!(cond)) {                                           \
27                         printf("TestCase %s() line %d failed: "              \
28                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
29                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
30                         return TEST_FAILED;                                  \
31                 }                                                        \
32 } while (0)
33
34 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do {                   \
35                 if (!(a == b)) {                                         \
36                         printf("TestCase %s() line %d failed: "              \
37                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
38                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
39                         return TEST_FAILED;                                  \
40                 }                                                        \
41 } while (0)
42
43 /* Compare two buffers (length in bytes) */
44 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len,  msg, ...) do {        \
45         if (memcmp(a, b, len)) {                                        \
46                 printf("TestCase %s() line %d failed: "              \
47                         msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
48                 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
49                 return TEST_FAILED;                                  \
50         }                                                        \
51 } while (0)
52
53 /* Compare two buffers with offset (length and offset in bytes) */
54 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
55         const uint8_t *_a_with_off = (const uint8_t *)a + off;              \
56         const uint8_t *_b_with_off = (const uint8_t *)b + off;              \
57         TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg);  \
58 } while (0)
59
60 /* Compare two buffers (length in bits) */
61 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do {     \
62         uint8_t _last_byte_a, _last_byte_b;                       \
63         uint8_t _last_byte_mask, _last_byte_bits;                  \
64         TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg);     \
65         if (len % 8) {                                              \
66                 _last_byte_bits = len % 8;                   \
67                 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
68                 _last_byte_a = ((const uint8_t *)a)[len >> 3];            \
69                 _last_byte_b = ((const uint8_t *)b)[len >> 3];            \
70                 _last_byte_a &= _last_byte_mask;                     \
71                 _last_byte_b &= _last_byte_mask;                    \
72                 if (_last_byte_a != _last_byte_b) {                  \
73                         printf("TestCase %s() line %d failed: "              \
74                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
75                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
76                         return TEST_FAILED;                                  \
77                 }                                                        \
78         }                                                            \
79 } while (0)
80
81 /* Compare two buffers with offset (length and offset in bits) */
82 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
83         uint8_t _first_byte_a, _first_byte_b;                                 \
84         uint8_t _first_byte_mask, _first_byte_bits;                           \
85         uint32_t _len_without_first_byte = (off % 8) ?                       \
86                                 len - (8 - (off % 8)) :                       \
87                                 len;                                          \
88         uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3);     \
89         const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes;      \
90         const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes;      \
91         TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off,           \
92                                 _len_without_first_byte, msg);                \
93         if (off % 8) {                                                        \
94                 _first_byte_bits = 8 - (off % 8);                             \
95                 _first_byte_mask = (1 << _first_byte_bits) - 1;               \
96                 _first_byte_a = *(_a_with_off - 1);                           \
97                 _first_byte_b = *(_b_with_off - 1);                           \
98                 _first_byte_a &= _first_byte_mask;                            \
99                 _first_byte_b &= _first_byte_mask;                            \
100                 if (_first_byte_a != _first_byte_b) {                         \
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         }                                                                     \
107 } while (0)
108
109 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {               \
110                 if (!(a != b)) {                                         \
111                         printf("TestCase %s() line %d failed: "              \
112                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
113                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
114                         return TEST_FAILED;                                  \
115                 }                                                        \
116 } while (0)
117
118 #define TEST_ASSERT_SUCCESS(val, msg, ...) do {                  \
119                 typeof(val) _val = (val);                                \
120                 if (!(_val == 0)) {                                      \
121                         printf("TestCase %s() line %d failed (err %d): "     \
122                                 msg "\n", __func__, __LINE__, _val,              \
123                                 ##__VA_ARGS__);                                  \
124                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
125                         return TEST_FAILED;                                  \
126                 }                                                        \
127 } while (0)
128
129 #define TEST_ASSERT_FAIL(val, msg, ...) do {                     \
130                 if (!(val != 0)) {                                       \
131                         printf("TestCase %s() line %d failed: "              \
132                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
133                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
134                         return TEST_FAILED;                                  \
135                 }                                                        \
136 } while (0)
137
138 #define TEST_ASSERT_NULL(val, msg, ...) do {                     \
139                 if (!(val == NULL)) {                                    \
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_NOT_NULL(val, msg, ...) do {                 \
148                 if (!(val != NULL)) {                                    \
149                         printf("TestCase %s() line %d failed: "              \
150                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
151                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
152                         return TEST_FAILED;                                  \
153                 }                                                        \
154 } while (0)
155
156 struct unit_test_case {
157         int (*setup)(void);
158         void (*teardown)(void);
159         int (*testcase)(void);
160         const char *name;
161         unsigned enabled;
162 };
163
164 #define TEST_CASE(fn) { NULL, NULL, fn, #fn, 1 }
165
166 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name, 1 }
167
168 #define TEST_CASE_ST(setup, teardown, testcase) \
169                 { setup, teardown, testcase, #testcase, 1 }
170
171
172 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn, 0 }
173
174 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
175                 { setup, teardown, testcase, #testcase, 0 }
176
177 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, 0 }
178
179 static inline void
180 debug_hexdump(FILE *file, const char *title, const void *buf, size_t len)
181 {
182         if (rte_log_get_global_level() == RTE_LOG_DEBUG)
183                 rte_hexdump(file, title, buf, len);
184 }
185
186 struct unit_test_suite {
187         const char *suite_name;
188         int (*setup)(void);
189         void (*teardown)(void);
190         struct unit_test_case unit_test_cases[];
191 };
192
193 int unit_test_suite_runner(struct unit_test_suite *suite);
194
195 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
196
197 #include <cmdline_parse.h>
198 #include <cmdline_parse_string.h>
199
200 extern const char *prgname;
201
202 int commands_init(void);
203
204 int test_mp_secondary(void);
205
206 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
207 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
208 int test_set_rxtx_sc(cmdline_fixed_string_t type);
209
210 typedef int (test_callback)(void);
211 TAILQ_HEAD(test_commands_list, test_command);
212 struct test_command {
213         TAILQ_ENTRY(test_command) next;
214         const char *command;
215         test_callback *callback;
216 };
217
218 void add_test_command(struct test_command *t);
219
220 /* Register a test function with its command string */
221 #define REGISTER_TEST_COMMAND(cmd, func) \
222         static struct test_command test_struct_##cmd = { \
223                 .command = RTE_STR(cmd), \
224                 .callback = func, \
225         }; \
226         static void __attribute__((constructor, used)) \
227         test_register_##cmd(void) \
228         { \
229                 add_test_command(&test_struct_##cmd); \
230         }
231
232 #endif