Imported Upstream version 16.04
[deb_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
39 #define TEST_SUCCESS  (0)
40 #define TEST_FAILED  (-1)
41
42 /* Before including test.h file you can define
43  * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
44  * failures. Mostly useful in test development phase. */
45 #ifndef TEST_TRACE_FAILURE
46 # define TEST_TRACE_FAILURE(_file, _line, _func)
47 #endif
48
49 #define TEST_ASSERT(cond, msg, ...) do {                         \
50                 if (!(cond)) {                                           \
51                         printf("TestCase %s() line %d failed: "              \
52                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
53                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
54                         return TEST_FAILED;                                  \
55                 }                                                        \
56 } while (0)
57
58 #define TEST_ASSERT_EQUAL(a, b, msg, ...) do {                   \
59                 if (!(a == b)) {                                         \
60                         printf("TestCase %s() line %d failed: "              \
61                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
62                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
63                         return TEST_FAILED;                                  \
64                 }                                                        \
65 } while (0)
66
67
68 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len,  msg, ...) do {        \
69         if (memcmp(a, b, len)) {                                        \
70                 printf("TestCase %s() line %d failed: "              \
71                         msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
72                 TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
73                 return TEST_FAILED;                                  \
74         }                                                        \
75 } while (0)
76
77
78 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {               \
79                 if (!(a != b)) {                                         \
80                         printf("TestCase %s() line %d failed: "              \
81                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
82                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
83                         return TEST_FAILED;                                  \
84                 }                                                        \
85 } while (0)
86
87 #define TEST_ASSERT_SUCCESS(val, msg, ...) do {                  \
88                 typeof(val) _val = (val);                                \
89                 if (!(_val == 0)) {                                      \
90                         printf("TestCase %s() line %d failed (err %d): "     \
91                                 msg "\n", __func__, __LINE__, _val,              \
92                                 ##__VA_ARGS__);                                  \
93                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
94                         return TEST_FAILED;                                  \
95                 }                                                        \
96 } while (0)
97
98 #define TEST_ASSERT_FAIL(val, msg, ...) do {                     \
99                 if (!(val != 0)) {                                       \
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 } while (0)
106
107 #define TEST_ASSERT_NULL(val, msg, ...) do {                     \
108                 if (!(val == NULL)) {                                    \
109                         printf("TestCase %s() line %d failed: "              \
110                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
111                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
112                         return TEST_FAILED;                                  \
113                 }                                                        \
114 } while (0)
115
116 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do {                 \
117                 if (!(val != NULL)) {                                    \
118                         printf("TestCase %s() line %d failed: "              \
119                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
120                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
121                         return TEST_FAILED;                                  \
122                 }                                                        \
123 } while (0)
124
125 struct unit_test_case {
126         int (*setup)(void);
127         void (*teardown)(void);
128         int (*testcase)(void);
129         const char *success_msg;
130         const char *fail_msg;
131         unsigned enabled;
132 };
133
134 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed", 1 }
135
136 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
137                 name " failed", 1 }
138
139 #define TEST_CASE_ST(setup, teardown, testcase)         \
140                 { setup, teardown, testcase, #testcase " succeeded",    \
141                 #testcase " failed ", 1 }
142
143
144 #define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn " succeeded", \
145         #fn " failed", 0 }
146
147 #define TEST_CASE_ST_DISABLED(setup, teardown, testcase)         \
148                 { setup, teardown, testcase, #testcase " succeeded",    \
149                 #testcase " failed ", 0 }
150
151 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0 }
152
153 struct unit_test_suite {
154         const char *suite_name;
155         int (*setup)(void);
156         void (*teardown)(void);
157         struct unit_test_case unit_test_cases[];
158 };
159
160 int unit_test_suite_runner(struct unit_test_suite *suite);
161
162 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
163
164 #include <cmdline_parse.h>
165 #include <cmdline_parse_string.h>
166
167 extern const char *prgname;
168
169 int commands_init(void);
170
171 int test_pci(void);
172 int test_pci_run;
173
174 int test_mp_secondary(void);
175
176 int test_ivshmem(void);
177 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
178 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
179 int test_set_rxtx_sc(cmdline_fixed_string_t type);
180
181 typedef int (test_callback)(void);
182 TAILQ_HEAD(test_commands_list, test_command);
183 struct test_command {
184         TAILQ_ENTRY(test_command) next;
185         const char *command;
186         test_callback *callback;
187 };
188
189 void add_test_command(struct test_command *t);
190
191 #define REGISTER_TEST_COMMAND(t) \
192 static void __attribute__((used)) testfn_##t(void);\
193 void __attribute__((constructor, used)) testfn_##t(void)\
194 {\
195         add_test_command(&t);\
196 }
197
198 #endif