app/test: new assert macros and test suite runner
[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 #define TEST_ASSERT(cond, msg, ...) do {                                                \
38                 if (!(cond)) {                                                                                  \
39                         printf("TestCase %s() line %d failed: "                 \
40                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
41                         return -1;                                                                                      \
42                 }                                                                                                               \
43 } while (0)
44
45 #define TEST_ASSERT_EQUAL(a, b, msg, ...)  {                                    \
46                 if (!(a == b)) {                                                                                \
47                         printf("TestCase %s() line %d failed: "                         \
48                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
49                         return -1;                                                                                      \
50                 }                                                                                                               \
51 } while (0)
52
53 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {                              \
54                 if (!(a != b)) {                                                                                \
55                         printf("TestCase %s() line %d failed: "                 \
56                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
57                         return -1;                                                                                      \
58                 }                                                                                                               \
59 } while (0)
60
61 #define TEST_ASSERT_SUCCESS(val, msg, ...) do {                                 \
62                 if (!(val == 0)) {                                                                              \
63                         printf("TestCase %s() line %d failed: "                 \
64                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
65                         return -1;                                                                                      \
66                 }                                                                                                               \
67 } while (0)
68
69 #define TEST_ASSERT_FAIL(val, msg, ...) do {                                    \
70                 if (!(val != -1)) {                                                                             \
71                         printf("TestCase %s() line %d failed: "                 \
72                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
73                         return -1;                                                                                      \
74                 }                                                                                                               \
75 } while (0)
76
77
78 #define TEST_ASSERT_NULL(val, msg, ...) do {                                    \
79                 if (!(val == NULL)) {                                                                   \
80                         printf("TestCase %s() line %d failed: "                 \
81                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
82                         return -1;                                                                                      \
83                 }                                                                                                               \
84 } while (0)
85
86 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do {                                \
87                 if (!(val != NULL)) {                                                                   \
88                         printf("TestCase %s() line %d failed: "                 \
89                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
90                         return -1;                                                                                      \
91                 }                                                                                                               \
92 } while (0)
93
94 struct unit_test_case {
95         int (*setup)(void);
96         int (*teardown)(void);
97         int (*testcase)(void);
98         const char *success_msg;
99         const char *fail_msg;
100 };
101
102 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed"}
103
104 #define TEST_CASE_ST(setup, teardown, testcase)                 \
105                 { setup, teardown, testcase, #testcase " succeeded",    \
106                 #testcase " failed "}
107
108 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL }
109
110 struct unit_test_suite {
111         const char *suite_name;
112         int (*setup)(void);
113         int (*teardown)(void);
114         struct unit_test_case unit_test_cases[];
115 };
116
117 int unit_test_suite_runner(struct unit_test_suite *suite);
118
119 /* icc on baremetal gives us troubles with function named 'main' */
120 #ifdef RTE_EXEC_ENV_BAREMETAL
121 #define main _main
122 #endif
123
124 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
125
126 extern const char *prgname;
127
128 int main(int argc, char **argv);
129
130 int test_pci(void);
131 int test_memory(void);
132 int test_per_lcore(void);
133 int test_spinlock(void);
134 int test_rwlock(void);
135 int test_atomic(void);
136 int test_byteorder(void);
137 int test_prefetch(void);
138 int test_cycles(void);
139 int test_logs(void);
140 int test_memzone(void);
141 int test_ring(void);
142 int test_table(void);
143 int test_ring_perf(void);
144 int test_mempool(void);
145 int test_mempool_perf(void);
146 int test_mbuf(void);
147 int test_timer(void);
148 int test_timer_perf(void);
149 int test_malloc(void);
150 int test_memcpy(void);
151 int test_memcpy_perf(void);
152 int test_hash(void);
153 int test_hash_perf(void);
154 int test_lpm(void);
155 int test_lpm6(void);
156 int test_debug(void);
157 int test_errno(void);
158 int test_tailq(void);
159 int test_string_fns(void);
160 int test_mp_secondary(void);
161 int test_cpuflags(void);
162 int test_eal_flags(void);
163 int test_alarm(void);
164 int test_interrupt(void);
165 int test_version(void);
166 int test_eal_fs(void);
167 int test_cmdline(void);
168 int test_func_reentrancy(void);
169 int test_red(void);
170 int test_sched(void);
171 int test_meter(void);
172 int test_acl(void);
173 int test_kni(void);
174 int test_power(void);
175 int test_common(void);
176 int test_pmd_ring(void);
177 int test_ivshmem(void);
178 int test_distributor(void);
179 int test_distributor_perf(void);
180 int test_kvargs(void);
181 int test_devargs(void);
182 int test_link_bonding(void);
183
184 int test_pci_run;
185
186 #endif