xref: /dpdk/app/test/test_tailq.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3a9de470cSBruce Richardson  */
4a9de470cSBruce Richardson 
5a9de470cSBruce Richardson #include <stdio.h>
6a9de470cSBruce Richardson #include <stdint.h>
7a9de470cSBruce Richardson #include <stdarg.h>
8a9de470cSBruce Richardson #include <string.h>
9a9de470cSBruce Richardson #include <errno.h>
10a9de470cSBruce Richardson #include <sys/queue.h>
11a9de470cSBruce Richardson 
12a9de470cSBruce Richardson #include <rte_eal.h>
13a9de470cSBruce Richardson #include <rte_string_fns.h>
14028669bcSAnatoly Burakov #include <rte_tailq.h>
15a9de470cSBruce Richardson 
16a9de470cSBruce Richardson #include "test.h"
17a9de470cSBruce Richardson 
18a9de470cSBruce Richardson #define do_return(...) do { \
19a9de470cSBruce Richardson 	printf("Error at %s, line %d: ", __func__, __LINE__); \
20a9de470cSBruce Richardson 	printf(__VA_ARGS__); \
21a9de470cSBruce Richardson 	return 1; \
22a9de470cSBruce Richardson } while (0)
23a9de470cSBruce Richardson 
24a9de470cSBruce Richardson static struct rte_tailq_elem rte_dummy_tailq = {
25a9de470cSBruce Richardson 	.name = "dummy",
26a9de470cSBruce Richardson };
27a9de470cSBruce Richardson EAL_REGISTER_TAILQ(rte_dummy_tailq)
28a9de470cSBruce Richardson 
29a9de470cSBruce Richardson static struct rte_tailq_elem rte_dummy_dyn_tailq = {
30a9de470cSBruce Richardson 	.name = "dummy_dyn",
31a9de470cSBruce Richardson };
32a9de470cSBruce Richardson static struct rte_tailq_elem rte_dummy_dyn2_tailq = {
33a9de470cSBruce Richardson 	.name = "dummy_dyn",
34a9de470cSBruce Richardson };
35a9de470cSBruce Richardson 
36a9de470cSBruce Richardson static struct rte_tailq_entry d_elem;
37a9de470cSBruce Richardson static struct rte_tailq_entry d_dyn_elem;
38a9de470cSBruce Richardson 
39a9de470cSBruce Richardson static int
test_tailq_early(void)40a9de470cSBruce Richardson test_tailq_early(void)
41a9de470cSBruce Richardson {
42a9de470cSBruce Richardson 	struct rte_tailq_entry_head *d_head;
43a9de470cSBruce Richardson 
44a9de470cSBruce Richardson 	d_head = RTE_TAILQ_CAST(rte_dummy_tailq.head, rte_tailq_entry_head);
45a9de470cSBruce Richardson 	if (d_head == NULL)
46a9de470cSBruce Richardson 		do_return("Error %s has not been initialised\n",
47a9de470cSBruce Richardson 			  rte_dummy_tailq.name);
48a9de470cSBruce Richardson 
49a9de470cSBruce Richardson 	/* check we can add an item to it */
50a9de470cSBruce Richardson 	TAILQ_INSERT_TAIL(d_head, &d_elem, next);
51a9de470cSBruce Richardson 
52a9de470cSBruce Richardson 	return 0;
53a9de470cSBruce Richardson }
54a9de470cSBruce Richardson 
55a9de470cSBruce Richardson static int
test_tailq_create(void)56a9de470cSBruce Richardson test_tailq_create(void)
57a9de470cSBruce Richardson {
58a9de470cSBruce Richardson 	struct rte_tailq_entry_head *d_head;
59a9de470cSBruce Richardson 
60a9de470cSBruce Richardson 	/* create a tailq and check its non-null (since we are post-eal init) */
61a9de470cSBruce Richardson 	if ((rte_eal_tailq_register(&rte_dummy_dyn_tailq) < 0) ||
62a9de470cSBruce Richardson 	    (rte_dummy_dyn_tailq.head == NULL))
63a9de470cSBruce Richardson 		do_return("Error allocating %s\n", rte_dummy_dyn_tailq.name);
64a9de470cSBruce Richardson 
65a9de470cSBruce Richardson 	d_head = RTE_TAILQ_CAST(rte_dummy_dyn_tailq.head, rte_tailq_entry_head);
66a9de470cSBruce Richardson 
67a9de470cSBruce Richardson 	/* check we can add an item to it */
68a9de470cSBruce Richardson 	TAILQ_INSERT_TAIL(d_head, &d_dyn_elem, next);
69a9de470cSBruce Richardson 
70a9de470cSBruce Richardson 	if (strcmp(rte_dummy_dyn2_tailq.name, rte_dummy_dyn_tailq.name))
71a9de470cSBruce Richardson 		do_return("Error, something is wrong in the tailq test\n");
72a9de470cSBruce Richardson 
73a9de470cSBruce Richardson 	/* try allocating again, and check for failure */
74a9de470cSBruce Richardson 	if (!rte_eal_tailq_register(&rte_dummy_dyn2_tailq))
75a9de470cSBruce Richardson 		do_return("Error, registering the same tailq %s did not fail\n",
76a9de470cSBruce Richardson 			  rte_dummy_dyn2_tailq.name);
77a9de470cSBruce Richardson 
78a9de470cSBruce Richardson 	return 0;
79a9de470cSBruce Richardson }
80a9de470cSBruce Richardson 
81a9de470cSBruce Richardson static int
test_tailq_lookup(void)82a9de470cSBruce Richardson test_tailq_lookup(void)
83a9de470cSBruce Richardson {
84a9de470cSBruce Richardson 	/* run successful  test - check result is found */
85a9de470cSBruce Richardson 	struct rte_tailq_entry_head *d_head;
86a9de470cSBruce Richardson 	struct rte_tailq_entry *d_ptr;
87a9de470cSBruce Richardson 
88a9de470cSBruce Richardson 	d_head = RTE_TAILQ_LOOKUP(rte_dummy_tailq.name, rte_tailq_entry_head);
89a9de470cSBruce Richardson 	/* rte_dummy_tailq has been registered by EAL_REGISTER_TAILQ */
90a9de470cSBruce Richardson 	if (d_head == NULL ||
91a9de470cSBruce Richardson 	    d_head != RTE_TAILQ_CAST(rte_dummy_tailq.head, rte_tailq_entry_head))
92a9de470cSBruce Richardson 		do_return("Error with tailq lookup\n");
93a9de470cSBruce Richardson 
94a9de470cSBruce Richardson 	TAILQ_FOREACH(d_ptr, d_head, next)
95a9de470cSBruce Richardson 		if (d_ptr != &d_elem)
96a9de470cSBruce Richardson 			do_return("Error with tailq returned from lookup - "
97a9de470cSBruce Richardson 					"expected element not found\n");
98a9de470cSBruce Richardson 
99a9de470cSBruce Richardson 	d_head = RTE_TAILQ_LOOKUP(rte_dummy_dyn_tailq.name, rte_tailq_entry_head);
100a9de470cSBruce Richardson 	/* rte_dummy_dyn_tailq has been registered by test_tailq_create */
101a9de470cSBruce Richardson 	if (d_head == NULL ||
102a9de470cSBruce Richardson 	    d_head != RTE_TAILQ_CAST(rte_dummy_dyn_tailq.head, rte_tailq_entry_head))
103a9de470cSBruce Richardson 		do_return("Error with tailq lookup\n");
104a9de470cSBruce Richardson 
105a9de470cSBruce Richardson 	TAILQ_FOREACH(d_ptr, d_head, next)
106a9de470cSBruce Richardson 		if (d_ptr != &d_dyn_elem)
107a9de470cSBruce Richardson 			do_return("Error with tailq returned from lookup - "
108a9de470cSBruce Richardson 					"expected element not found\n");
109a9de470cSBruce Richardson 
110a9de470cSBruce Richardson 	/* now try a bad/error lookup */
111a9de470cSBruce Richardson 	d_head = RTE_TAILQ_LOOKUP("coucou", rte_tailq_entry_head);
112a9de470cSBruce Richardson 	if (d_head != NULL)
113a9de470cSBruce Richardson 		do_return("Error, lookup does not return NULL for bad tailq name\n");
114a9de470cSBruce Richardson 
115a9de470cSBruce Richardson 	return 0;
116a9de470cSBruce Richardson }
117a9de470cSBruce Richardson 
118a9de470cSBruce Richardson static int
test_tailq(void)119a9de470cSBruce Richardson test_tailq(void)
120a9de470cSBruce Richardson {
121a9de470cSBruce Richardson 	int ret = 0;
122a9de470cSBruce Richardson 	ret |= test_tailq_early();
123a9de470cSBruce Richardson 	ret |= test_tailq_create();
124a9de470cSBruce Richardson 	ret |= test_tailq_lookup();
125a9de470cSBruce Richardson 	return ret;
126a9de470cSBruce Richardson }
127a9de470cSBruce Richardson 
128*e0a8442cSBruce Richardson REGISTER_FAST_TEST(tailq_autotest, true, true, test_tailq);
129