c3b2a877ec5ccdeb02edc7c4830d82e7b2136464
[dpdk.git] / app / 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
14 #define TEST_SUCCESS EXIT_SUCCESS
15 #define TEST_FAILED  -1
16 #define TEST_SKIPPED  77
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 #include <rte_test.h>
26
27 #define TEST_ASSERT RTE_TEST_ASSERT
28
29 #define TEST_ASSERT_EQUAL RTE_TEST_ASSERT_EQUAL
30
31 /* Compare two buffers (length in bytes) */
32 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len,  msg, ...) do {        \
33         if (memcmp(a, b, len)) {                                        \
34                 printf("TestCase %s() line %d failed: "              \
35                         msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
36                 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
37                 return TEST_FAILED;                                  \
38         }                                                        \
39 } while (0)
40
41 /* Compare two buffers with offset (length and offset in bytes) */
42 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
43         const uint8_t *_a_with_off = (const uint8_t *)a + off;              \
44         const uint8_t *_b_with_off = (const uint8_t *)b + off;              \
45         TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg);  \
46 } while (0)
47
48 /* Compare two buffers (length in bits) */
49 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do {     \
50         uint8_t _last_byte_a, _last_byte_b;                       \
51         uint8_t _last_byte_mask, _last_byte_bits;                  \
52         TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg);     \
53         if (len % 8) {                                              \
54                 _last_byte_bits = len % 8;                   \
55                 _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
56                 _last_byte_a = ((const uint8_t *)a)[len >> 3];            \
57                 _last_byte_b = ((const uint8_t *)b)[len >> 3];            \
58                 _last_byte_a &= _last_byte_mask;                     \
59                 _last_byte_b &= _last_byte_mask;                    \
60                 if (_last_byte_a != _last_byte_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         }                                                            \
67 } while (0)
68
69 /* Compare two buffers with offset (length and offset in bits) */
70 #define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
71         uint8_t _first_byte_a, _first_byte_b;                                 \
72         uint8_t _first_byte_mask, _first_byte_bits;                           \
73         uint32_t _len_without_first_byte = (off % 8) ?                       \
74                                 len - (8 - (off % 8)) :                       \
75                                 len;                                          \
76         uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3);     \
77         const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes;      \
78         const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes;      \
79         TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off,           \
80                                 _len_without_first_byte, msg);                \
81         if (off % 8) {                                                        \
82                 _first_byte_bits = 8 - (off % 8);                             \
83                 _first_byte_mask = (1 << _first_byte_bits) - 1;               \
84                 _first_byte_a = *(_a_with_off - 1);                           \
85                 _first_byte_b = *(_b_with_off - 1);                           \
86                 _first_byte_a &= _first_byte_mask;                            \
87                 _first_byte_b &= _first_byte_mask;                            \
88                 if (_first_byte_a != _first_byte_b) {                         \
89                         printf("TestCase %s() line %d failed: "               \
90                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
91                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);     \
92                         return TEST_FAILED;                                   \
93                 }                                                             \
94         }                                                                     \
95 } while (0)
96
97 #define TEST_ASSERT_NOT_EQUAL RTE_TEST_ASSERT_NOT_EQUAL
98
99 #define TEST_ASSERT_SUCCESS RTE_TEST_ASSERT_SUCCESS
100
101 #define TEST_ASSERT_FAIL RTE_TEST_ASSERT_FAIL
102
103 #define TEST_ASSERT_NULL RTE_TEST_ASSERT_NULL
104
105 #define TEST_ASSERT_NOT_NULL RTE_TEST_ASSERT_NOT_NULL
106
107 struct unit_test_case {
108         int (*setup)(void);
109         void (*teardown)(void);
110         int (*testcase)(void);
111         int (*testcase_with_data)(const void *data);
112         const char *name;
113         unsigned enabled;
114         const void *data;
115 };
116
117 #define TEST_CASE(fn) { NULL, NULL, fn, NULL, #fn, 1, NULL }
118
119 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, NULL, name, 1, NULL }
120
121 #define TEST_CASE_ST(setup, teardown, testcase) \
122                 { setup, teardown, testcase, NULL, #testcase, 1, NULL }
123
124 #define TEST_CASE_WITH_DATA(setup, teardown, testcase, data) \
125                 { setup, teardown, NULL, testcase, #testcase, 1, data }
126
127 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, NULL, #fn, 0, NULL }
128
129 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
130                 { setup, teardown, testcase, NULL, #testcase, 0, NULL }
131
132 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0, NULL }
133
134 static inline void
135 debug_hexdump(FILE *file, const char *title, const void *buf, size_t len)
136 {
137         if (rte_log_get_global_level() == RTE_LOG_DEBUG)
138                 rte_hexdump(file, title, buf, len);
139 }
140
141 struct unit_test_suite {
142         const char *suite_name;
143         int (*setup)(void);
144         void (*teardown)(void);
145         unsigned int total;
146         unsigned int executed;
147         unsigned int succeeded;
148         unsigned int skipped;
149         unsigned int failed;
150         unsigned int unsupported;
151         struct unit_test_suite **unit_test_suites;
152         struct unit_test_case unit_test_cases[];
153 };
154
155 int unit_test_suite_runner(struct unit_test_suite *suite);
156 extern int last_test_result;
157
158 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
159
160 #include <cmdline_parse.h>
161 #include <cmdline_parse_string.h>
162
163 extern const char *prgname;
164
165 int commands_init(void);
166 int command_valid(const char *cmd);
167
168 int test_mp_secondary(void);
169 int test_timer_secondary(void);
170
171 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
172 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
173 int test_set_rxtx_sc(cmdline_fixed_string_t type);
174
175 typedef int (test_callback)(void);
176 TAILQ_HEAD(test_commands_list, test_command);
177 struct test_command {
178         TAILQ_ENTRY(test_command) next;
179         const char *command;
180         test_callback *callback;
181 };
182
183 void add_test_command(struct test_command *t);
184
185 /* Register a test function with its command string */
186 #define REGISTER_TEST_COMMAND(cmd, func) \
187         static struct test_command test_struct_##cmd = { \
188                 .command = RTE_STR(cmd), \
189                 .callback = func, \
190         }; \
191         RTE_INIT(test_register_##cmd) \
192         { \
193                 add_test_command(&test_struct_##cmd); \
194         }
195
196 #endif