app/test: check cloning a clone
[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
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 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {               \
68                 if (!(a != b)) {                                         \
69                         printf("TestCase %s() line %d failed: "              \
70                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
71                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
72                         return TEST_FAILED;                                  \
73                 }                                                        \
74 } while (0)
75
76 #define TEST_ASSERT_SUCCESS(val, msg, ...) do {                  \
77                 typeof(val) _val = (val);                                \
78                 if (!(_val == 0)) {                                      \
79                         printf("TestCase %s() line %d failed (err %d): "     \
80                                 msg "\n", __func__, __LINE__, _val,              \
81                                 ##__VA_ARGS__);                                  \
82                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
83                         return TEST_FAILED;                                  \
84                 }                                                        \
85 } while (0)
86
87 #define TEST_ASSERT_FAIL(val, msg, ...) do {                     \
88                 if (!(val != 0)) {                                       \
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 } while (0)
95
96 #define TEST_ASSERT_NULL(val, msg, ...) do {                     \
97                 if (!(val == NULL)) {                                    \
98                         printf("TestCase %s() line %d failed: "              \
99                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
100                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
101                         return TEST_FAILED;                                  \
102                 }                                                        \
103 } while (0)
104
105 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do {                 \
106                 if (!(val != NULL)) {                                    \
107                         printf("TestCase %s() line %d failed: "              \
108                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
109                         TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
110                         return TEST_FAILED;                                  \
111                 }                                                        \
112 } while (0)
113
114 struct unit_test_case {
115         int (*setup)(void);
116         int (*teardown)(void);
117         int (*testcase)(void);
118         const char *success_msg;
119         const char *fail_msg;
120 };
121
122 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed"}
123
124 #define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
125                 name " failed"}
126
127 #define TEST_CASE_ST(setup, teardown, testcase)         \
128                 { setup, teardown, testcase, #testcase " succeeded",    \
129                 #testcase " failed "}
130
131 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL }
132
133 struct unit_test_suite {
134         const char *suite_name;
135         int (*setup)(void);
136         int (*teardown)(void);
137         struct unit_test_case unit_test_cases[];
138 };
139
140 int unit_test_suite_runner(struct unit_test_suite *suite);
141
142 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
143
144 #include <cmdline_parse.h>
145 #include <cmdline_parse_string.h>
146
147 extern const char *prgname;
148
149 int commands_init(void);
150
151 int test_pci(void);
152 int test_pci_run;
153
154 int test_mp_secondary(void);
155
156 int test_ivshmem(void);
157 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
158 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
159 int test_set_rxtx_sc(cmdline_fixed_string_t type);
160
161 typedef int (test_callback)(void);
162 TAILQ_HEAD(test_commands_list, test_command);
163 struct test_command {
164         TAILQ_ENTRY(test_command) next;
165         const char *command;
166         test_callback *callback;
167 };
168
169 void add_test_command(struct test_command *t);
170
171 #define REGISTER_TEST_COMMAND(t) \
172 static void __attribute__((used)) testfn_##t(void);\
173 void __attribute__((constructor, used)) testfn_##t(void)\
174 {\
175         add_test_command(&t);\
176 }
177
178 #endif