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