first public release
[dpdk.git] / app / test / test_tailq.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41
42 #include <cmdline_parse.h>
43
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 "dummy_q0"
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         char name[RTE_TAILQ_NAMESIZE];
64         unsigned i;
65
66         /* create a first tailq and check its non-null */
67         d_head = RTE_TAILQ_RESERVE(DEFAULT_TAILQ, rte_dummy_head);
68         if (d_head == NULL)
69                 do_return("Error allocating "DEFAULT_TAILQ"\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(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 = 1; i < RTE_MAX_TAILQ; i++){
82                 rte_snprintf(name, sizeof(name), "dummy_q%u", i);
83                 if ((d_head = RTE_TAILQ_RESERVE(name, rte_dummy_head)) == NULL)
84                         break;
85         }
86
87         /* check that we had an error return before RTE_MAX_TAILQ */
88         if (i == RTE_MAX_TAILQ)
89                 do_return("Error, we did not have a reservation failure as expected\n");
90
91         return 0;
92 }
93
94 static int
95 test_tailq_lookup(void)
96 {
97         /* run successful  test - check result is found */
98         struct rte_dummy_head *d_head;
99         struct rte_dummy *d_ptr;
100
101         d_head = RTE_TAILQ_LOOKUP(DEFAULT_TAILQ, rte_dummy_head);
102         if (d_head == NULL)
103                 do_return("Error with tailq lookup\n");
104
105         TAILQ_FOREACH(d_ptr, d_head, next)
106                 if (d_ptr != &d_elem)
107                         do_return("Error with tailq returned from lookup - "
108                                         "expected element not found\n");
109
110         /* now try a bad/error lookup */
111         d_head = RTE_TAILQ_LOOKUP("does_not_exist_queue", rte_dummy_head);
112         if (d_head != NULL)
113                 do_return("Error, lookup does not return NULL for bad tailq name\n");
114
115         return 0;
116 }
117
118 int
119 test_tailq(void)
120 {
121         int ret = 0;
122         ret |= test_tailq_create();
123         ret |= test_tailq_lookup();
124         return ret;
125 }