tailq: remove unneeded inclusions
[dpdk.git] / app / test / test_tailq.c
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 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdarg.h>
37 #include <errno.h>
38 #include <sys/queue.h>
39
40 #include <rte_eal.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_string_fns.h>
43
44 #include "test.h"
45
46 #define do_return(...) do { \
47         printf("Error at %s, line %d: ", __func__, __LINE__); \
48         printf(__VA_ARGS__); \
49         return 1; \
50 } while (0)
51
52 #define DEFAULT_TAILQ (RTE_TAILQ_NUM)
53
54 static struct rte_tailq_entry d_elem;
55
56 static int
57 test_tailq_create(void)
58 {
59         struct rte_tailq_entry_head *d_head;
60         unsigned i;
61
62         /* create a first tailq and check its non-null */
63         d_head = RTE_TAILQ_RESERVE_BY_IDX(DEFAULT_TAILQ, rte_tailq_entry_head);
64         if (d_head == NULL)
65                 do_return("Error allocating dummy_q0\n");
66
67         /* check we can add an item to it
68          */
69         TAILQ_INSERT_TAIL(d_head, &d_elem, next);
70
71         /* try allocating dummy_q0 again, and check for failure */
72         if (RTE_TAILQ_RESERVE_BY_IDX(DEFAULT_TAILQ, rte_tailq_entry_head) == NULL)
73                 do_return("Error, non-null result returned when attemption to "
74                                 "re-allocate a tailq\n");
75
76         /* now fill up the tailq slots available and check we get an error */
77         for (i = RTE_TAILQ_NUM; i < RTE_MAX_TAILQ; i++){
78                 if ((d_head = RTE_TAILQ_RESERVE_BY_IDX(i,
79                                 rte_tailq_entry_head)) == NULL)
80                         break;
81         }
82
83         /* check that we had an error return before RTE_MAX_TAILQ */
84         if (i != RTE_MAX_TAILQ)
85                 do_return("Error, we did not have a reservation as expected\n");
86
87         return 0;
88 }
89
90 static int
91 test_tailq_lookup(void)
92 {
93         /* run successful  test - check result is found */
94         struct rte_tailq_entry_head *d_head;
95         struct rte_tailq_entry *d_ptr;
96
97         d_head = RTE_TAILQ_LOOKUP_BY_IDX(DEFAULT_TAILQ, rte_tailq_entry_head);
98         if (d_head == NULL)
99                 do_return("Error with tailq lookup\n");
100
101         TAILQ_FOREACH(d_ptr, d_head, next)
102                 if (d_ptr != &d_elem)
103                         do_return("Error with tailq returned from lookup - "
104                                         "expected element not found\n");
105
106         /* now try a bad/error lookup */
107         d_head = RTE_TAILQ_LOOKUP_BY_IDX(RTE_MAX_TAILQ, rte_tailq_entry_head);
108         if (d_head != NULL)
109                 do_return("Error, lookup does not return NULL for bad tailq name\n");
110
111         return 0;
112 }
113
114 /* test for deprecated functions - mainly for coverage */
115 static int
116 test_tailq_deprecated(void)
117 {
118         struct rte_tailq_entry_head *d_head;
119
120         /* since TAILQ_RESERVE is not able to create new tailqs,
121          * we should find an existing one (IOW, RTE_TAILQ_RESERVE behaves identical
122          * to RTE_TAILQ_LOOKUP).
123          *
124          * PCI_RESOURCE_LIST tailq is guaranteed to
125          * be present in any DPDK app. */
126         d_head = RTE_TAILQ_RESERVE("PCI_RESOURCE_LIST", rte_tailq_entry_head);
127         if (d_head == NULL)
128                 do_return("Error finding PCI_RESOURCE_LIST\n");
129
130         d_head = RTE_TAILQ_LOOKUP("PCI_RESOURCE_LIST", rte_tailq_entry_head);
131         if (d_head == NULL)
132                 do_return("Error finding PCI_RESOURCE_LIST\n");
133
134         /* try doing that with non-existent names */
135         d_head = RTE_TAILQ_RESERVE("random name", rte_tailq_entry_head);
136         if (d_head != NULL)
137                 do_return("Non-existent tailq found!\n");
138
139         d_head = RTE_TAILQ_LOOKUP("random name", rte_tailq_entry_head);
140         if (d_head != NULL)
141                 do_return("Non-existent tailq found!\n");
142
143         /* try doing the same with NULL names */
144         d_head = RTE_TAILQ_RESERVE(NULL, rte_tailq_entry_head);
145         if (d_head != NULL)
146                 do_return("NULL tailq found!\n");
147
148         d_head = RTE_TAILQ_LOOKUP(NULL, rte_tailq_entry_head);
149         if (d_head != NULL)
150                 do_return("NULL tailq found!\n");
151
152         return 0;
153 }
154
155 static int
156 test_tailq(void)
157 {
158         int ret = 0;
159         ret |= test_tailq_create();
160         ret |= test_tailq_lookup();
161         ret |= test_tailq_deprecated();
162         return ret;
163 }
164
165 static struct test_command tailq_cmd = {
166         .command = "tailq_autotest",
167         .callback = test_tailq,
168 };
169 REGISTER_TEST_COMMAND(tailq_cmd);