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