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