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