xref: /dpdk/app/test/test_tailq.c (revision cf435a07)
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 <string.h>
38 #include <errno.h>
39 #include <sys/queue.h>
40 
41 #include <rte_eal.h>
42 #include <rte_eal_memconfig.h>
43 #include <rte_string_fns.h>
44 
45 #include "test.h"
46 
47 #define do_return(...) do { \
48 	printf("Error at %s, line %d: ", __func__, __LINE__); \
49 	printf(__VA_ARGS__); \
50 	return 1; \
51 } while (0)
52 
53 static struct rte_tailq_elem rte_dummy_tailq = {
54 	.name = "dummy",
55 };
56 EAL_REGISTER_TAILQ(rte_dummy_tailq)
57 
58 static struct rte_tailq_elem rte_dummy_dyn_tailq = {
59 	.name = "dummy_dyn",
60 };
61 static struct rte_tailq_elem rte_dummy_dyn2_tailq = {
62 	.name = "dummy_dyn",
63 };
64 
65 static struct rte_tailq_entry d_elem;
66 static struct rte_tailq_entry d_dyn_elem;
67 
68 static int
69 test_tailq_early(void)
70 {
71 	struct rte_tailq_entry_head *d_head;
72 
73 	d_head = RTE_TAILQ_CAST(rte_dummy_tailq.head, rte_tailq_entry_head);
74 	if (d_head == NULL)
75 		do_return("Error %s has not been initialised\n",
76 			  rte_dummy_tailq.name);
77 
78 	/* check we can add an item to it */
79 	TAILQ_INSERT_TAIL(d_head, &d_elem, next);
80 
81 	return 0;
82 }
83 
84 static int
85 test_tailq_create(void)
86 {
87 	struct rte_tailq_entry_head *d_head;
88 
89 	/* create a tailq and check its non-null (since we are post-eal init) */
90 	if ((rte_eal_tailq_register(&rte_dummy_dyn_tailq) < 0) ||
91 	    (rte_dummy_dyn_tailq.head == NULL))
92 		do_return("Error allocating %s\n", rte_dummy_dyn_tailq.name);
93 
94 	d_head = RTE_TAILQ_CAST(rte_dummy_dyn_tailq.head, rte_tailq_entry_head);
95 
96 	/* check we can add an item to it */
97 	TAILQ_INSERT_TAIL(d_head, &d_dyn_elem, next);
98 
99 	if (strcmp(rte_dummy_dyn2_tailq.name, rte_dummy_dyn_tailq.name))
100 		do_return("Error, something is wrong in the tailq test\n");
101 
102 	/* try allocating again, and check for failure */
103 	if (!rte_eal_tailq_register(&rte_dummy_dyn2_tailq))
104 		do_return("Error, registering the same tailq %s did not fail\n",
105 			  rte_dummy_dyn2_tailq.name);
106 
107 	return 0;
108 }
109 
110 static int
111 test_tailq_lookup(void)
112 {
113 	/* run successful  test - check result is found */
114 	struct rte_tailq_entry_head *d_head;
115 	struct rte_tailq_entry *d_ptr;
116 
117 	d_head = RTE_TAILQ_LOOKUP(rte_dummy_tailq.name, rte_tailq_entry_head);
118 	/* rte_dummy_tailq has been registered by EAL_REGISTER_TAILQ */
119 	if (d_head == NULL ||
120 	    d_head != RTE_TAILQ_CAST(rte_dummy_tailq.head, rte_tailq_entry_head))
121 		do_return("Error with tailq lookup\n");
122 
123 	TAILQ_FOREACH(d_ptr, d_head, next)
124 		if (d_ptr != &d_elem)
125 			do_return("Error with tailq returned from lookup - "
126 					"expected element not found\n");
127 
128 	d_head = RTE_TAILQ_LOOKUP(rte_dummy_dyn_tailq.name, rte_tailq_entry_head);
129 	/* rte_dummy_dyn_tailq has been registered by test_tailq_create */
130 	if (d_head == NULL ||
131 	    d_head != RTE_TAILQ_CAST(rte_dummy_dyn_tailq.head, rte_tailq_entry_head))
132 		do_return("Error with tailq lookup\n");
133 
134 	TAILQ_FOREACH(d_ptr, d_head, next)
135 		if (d_ptr != &d_dyn_elem)
136 			do_return("Error with tailq returned from lookup - "
137 					"expected element not found\n");
138 
139 	/* now try a bad/error lookup */
140 	d_head = RTE_TAILQ_LOOKUP("coucou", rte_tailq_entry_head);
141 	if (d_head != NULL)
142 		do_return("Error, lookup does not return NULL for bad tailq name\n");
143 
144 	return 0;
145 }
146 
147 static int
148 test_tailq(void)
149 {
150 	int ret = 0;
151 	ret |= test_tailq_early();
152 	ret |= test_tailq_create();
153 	ret |= test_tailq_lookup();
154 	return ret;
155 }
156 
157 REGISTER_TEST_COMMAND(tailq_autotest, test_tailq);
158