blob: c5535c854a60db8341ef247203ce2e0be692ad00 [file] [log] [blame]
Dave Barach034fccc2016-06-24 18:53:21 -04001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vppinfra/dlist.h>
17
Dave Barachc3799992016-08-15 11:12:27 -040018typedef struct
19{
20 dlist_elt_t *test_pool;
Dave Barach034fccc2016-06-24 18:53:21 -040021 u32 head_index;
22} test_main_t;
23
24test_main_t test_main;
25
Dave Barachc3799992016-08-15 11:12:27 -040026int
27test_dlist_main (unformat_input_t * input)
Dave Barach034fccc2016-06-24 18:53:21 -040028{
Dave Barachc3799992016-08-15 11:12:27 -040029 test_main_t *tm = &test_main;
30 dlist_elt_t *head, *elt;
Dave Barach034fccc2016-06-24 18:53:21 -040031 u32 elt_index, head_index;
32 u32 value;
33 int i;
Dave Barachc3799992016-08-15 11:12:27 -040034
Dave Barach034fccc2016-06-24 18:53:21 -040035 pool_get (tm->test_pool, head);
36 head_index = head - tm->test_pool;
37 clib_dlist_init (tm->test_pool, head - tm->test_pool);
38
39 for (i = 1; i <= 3; i++)
40 {
41 pool_get (tm->test_pool, elt);
42 elt_index = elt - tm->test_pool;
43
44 clib_dlist_init (tm->test_pool, elt_index);
45 elt->value = i;
46 clib_dlist_addtail (tm->test_pool, head_index, elt_index);
47 }
48
49 head = pool_elt_at_index (tm->test_pool, head_index);
50
51 fformat (stdout, "Dump forward links\n");
52 elt_index = head->next;
53 i = 1;
54 value = 0;
55 while (value != ~0)
56 {
57 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -040058 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -040059 elt_index = elt->next;
60 value = elt->value;
61 }
62
63 fformat (stdout, "Dump reverse links\n");
64 elt_index = head->prev;
65 i = 1;
66 value = 0;
67 while (value != ~0)
68 {
69 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -040070 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -040071 elt_index = elt->prev;
72 value = elt->value;
73 }
74
75 fformat (stdout, "remove first element\n");
76
77 elt_index = clib_dlist_remove_head (tm->test_pool, head_index);
78 elt = pool_elt_at_index (tm->test_pool, elt_index);
79
80 fformat (stdout, "removed index %d value %d\n", elt_index, elt->value);
81
82 head = pool_elt_at_index (tm->test_pool, head_index);
83
84 fformat (stdout, "Dump forward links\n");
85 elt_index = head->next;
86 i = 1;
87 value = 0;
88 while (value != ~0)
89 {
90 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -040091 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -040092 elt_index = elt->next;
93 value = elt->value;
94 }
95
96 fformat (stdout, "Dump reverse links\n");
97 elt_index = head->prev;
98 i = 1;
99 value = 0;
100 while (value != ~0)
101 {
102 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -0400103 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -0400104 elt_index = elt->prev;
105 value = elt->value;
106 }
107
108 fformat (stdout, "re-insert index %d value %d at head\n", 1, 1);
109
110 clib_dlist_addhead (tm->test_pool, head_index, 1);
111
112 fformat (stdout, "Dump forward links\n");
113 elt_index = head->next;
114 i = 1;
115 value = 0;
116 while (value != ~0)
117 {
118 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -0400119 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -0400120 elt_index = elt->next;
121 value = elt->value;
122 }
123
124 fformat (stdout, "Dump reverse links\n");
125 elt_index = head->prev;
126 i = 1;
127 value = 0;
128 while (value != ~0)
129 {
130 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -0400131 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -0400132 elt_index = elt->prev;
133 value = elt->value;
134 }
135
136 fformat (stdout, "Remove middle element\n");
137
138 clib_dlist_remove (tm->test_pool, 2);
139 elt = pool_elt_at_index (tm->test_pool, 2);
140
141 fformat (stdout, "removed index %d value %d\n", elt_index, elt->value);
Dave Barachc3799992016-08-15 11:12:27 -0400142
Dave Barach034fccc2016-06-24 18:53:21 -0400143 fformat (stdout, "Dump forward links\n");
144 elt_index = head->next;
145 i = 1;
146 value = 0;
147 while (value != ~0)
148 {
149 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -0400150 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -0400151 elt_index = elt->next;
152 value = elt->value;
153 }
154
155 fformat (stdout, "Dump reverse links\n");
156 elt_index = head->prev;
157 i = 1;
158 value = 0;
159 while (value != ~0)
160 {
161 elt = pool_elt_at_index (tm->test_pool, elt_index);
Dave Barachc3799992016-08-15 11:12:27 -0400162 fformat (stdout, "elt %d value %d\n", i++, elt->value);
Dave Barach034fccc2016-06-24 18:53:21 -0400163 elt_index = elt->prev;
164 value = elt->value;
165 }
166
167 return 0;
168}
169
170#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400171int
172main (int argc, char *argv[])
Dave Barach034fccc2016-06-24 18:53:21 -0400173{
174 unformat_input_t i;
175 int ret;
176
Dave Barachc3799992016-08-15 11:12:27 -0400177 clib_mem_init (0, 3ULL << 30);
Dave Barach034fccc2016-06-24 18:53:21 -0400178
179 unformat_init_command_line (&i, argv);
180 ret = test_dlist_main (&i);
181 unformat_free (&i);
182
183 return ret;
184}
185#endif /* CLIB_UNIX */
Dave Barachc3799992016-08-15 11:12:27 -0400186
187/*
188 * fd.io coding-style-patch-verification: ON
189 *
190 * Local Variables:
191 * eval: (c-set-style "gnu")
192 * End:
193 */