app: no more bare metal environment
[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_ASSERT(cond, msg, ...) do {                                                \
40                 if (!(cond)) {                                                                                  \
41                         printf("TestCase %s() line %d failed: "                 \
42                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
43                         return -1;                                                                                      \
44                 }                                                                                                               \
45 } while (0)
46
47 #define TEST_ASSERT_EQUAL(a, b, msg, ...)  {                                    \
48                 if (!(a == b)) {                                                                                \
49                         printf("TestCase %s() line %d failed: "                         \
50                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
51                         return -1;                                                                                      \
52                 }                                                                                                               \
53 } while (0)
54
55 #define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {                              \
56                 if (!(a != b)) {                                                                                \
57                         printf("TestCase %s() line %d failed: "                 \
58                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
59                         return -1;                                                                                      \
60                 }                                                                                                               \
61 } while (0)
62
63 #define TEST_ASSERT_SUCCESS(val, msg, ...) do {                                 \
64                 if (!(val == 0)) {                                                                              \
65                         printf("TestCase %s() line %d failed (err %d): "        \
66                                 msg "\n", __func__, __LINE__, val,                              \
67                                 ##__VA_ARGS__);                                                                 \
68                         return -1;                                                                                      \
69                 }                                                                                                               \
70 } while (0)
71
72 #define TEST_ASSERT_FAIL(val, msg, ...) do {                                    \
73                 if (!(val != 0)) {                                                                              \
74                         printf("TestCase %s() line %d failed: "                 \
75                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
76                         return -1;                                                                                      \
77                 }                                                                                                               \
78 } while (0)
79
80
81 #define TEST_ASSERT_NULL(val, msg, ...) do {                                    \
82                 if (!(val == NULL)) {                                                                   \
83                         printf("TestCase %s() line %d failed: "                 \
84                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
85                         return -1;                                                                                      \
86                 }                                                                                                               \
87 } while (0)
88
89 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do {                                \
90                 if (!(val != NULL)) {                                                                   \
91                         printf("TestCase %s() line %d failed: "                 \
92                                 msg "\n", __func__, __LINE__, ##__VA_ARGS__);   \
93                         return -1;                                                                                      \
94                 }                                                                                                               \
95 } while (0)
96
97 struct unit_test_case {
98         int (*setup)(void);
99         int (*teardown)(void);
100         int (*testcase)(void);
101         const char *success_msg;
102         const char *fail_msg;
103 };
104
105 #define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed"}
106
107 #define TEST_CASE_ST(setup, teardown, testcase)                 \
108                 { setup, teardown, testcase, #testcase " succeeded",    \
109                 #testcase " failed "}
110
111 #define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL }
112
113 struct unit_test_suite {
114         const char *suite_name;
115         int (*setup)(void);
116         int (*teardown)(void);
117         struct unit_test_case unit_test_cases[];
118 };
119
120 int unit_test_suite_runner(struct unit_test_suite *suite);
121
122 #define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
123
124 #include <cmdline_parse.h>
125 #include <cmdline_parse_string.h>
126
127 extern const char *prgname;
128
129 int commands_init(void);
130
131 int test_pci(void);
132 int test_pci_run;
133
134 int test_mp_secondary(void);
135
136 int test_ivshmem(void);
137 int test_set_rxtx_conf(cmdline_fixed_string_t mode);
138 int test_set_rxtx_anchor(cmdline_fixed_string_t type);
139 int test_set_rxtx_sc(cmdline_fixed_string_t type);
140
141 typedef int (test_callback)(void);
142 TAILQ_HEAD(test_commands_list, test_command);
143 struct test_command {
144         TAILQ_ENTRY(test_command) next;
145         const char *command;
146         test_callback *callback;
147 };
148
149 void add_test_command(struct test_command *t);
150
151 #define REGISTER_TEST_COMMAND(t) \
152 static void testfn_##t(void);\
153 void __attribute__((constructor, used)) testfn_##t(void)\
154 {\
155         add_test_command(&t);\
156 }
157
158 #endif