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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vppinfra/dlist.h>
19 dlist_elt_t * test_pool;
23 test_main_t test_main;
25 int test_dlist_main (unformat_input_t * input)
27 test_main_t * tm = &test_main;
28 dlist_elt_t * head, * elt;
29 u32 elt_index, head_index;
33 pool_get (tm->test_pool, head);
34 head_index = head - tm->test_pool;
35 clib_dlist_init (tm->test_pool, head - tm->test_pool);
37 for (i = 1; i <= 3; i++)
39 pool_get (tm->test_pool, elt);
40 elt_index = elt - tm->test_pool;
42 clib_dlist_init (tm->test_pool, elt_index);
44 clib_dlist_addtail (tm->test_pool, head_index, elt_index);
47 head = pool_elt_at_index (tm->test_pool, head_index);
49 fformat (stdout, "Dump forward links\n");
50 elt_index = head->next;
55 elt = pool_elt_at_index (tm->test_pool, elt_index);
56 fformat(stdout, "elt %d value %d\n",
58 elt_index = elt->next;
62 fformat (stdout, "Dump reverse links\n");
63 elt_index = head->prev;
68 elt = pool_elt_at_index (tm->test_pool, elt_index);
69 fformat(stdout, "elt %d value %d\n",
71 elt_index = elt->prev;
75 fformat (stdout, "remove first element\n");
77 elt_index = clib_dlist_remove_head (tm->test_pool, head_index);
78 elt = pool_elt_at_index (tm->test_pool, elt_index);
80 fformat (stdout, "removed index %d value %d\n", elt_index, elt->value);
82 head = pool_elt_at_index (tm->test_pool, head_index);
84 fformat (stdout, "Dump forward links\n");
85 elt_index = head->next;
90 elt = pool_elt_at_index (tm->test_pool, elt_index);
91 fformat(stdout, "elt %d value %d\n",
93 elt_index = elt->next;
97 fformat (stdout, "Dump reverse links\n");
98 elt_index = head->prev;
103 elt = pool_elt_at_index (tm->test_pool, elt_index);
104 fformat(stdout, "elt %d value %d\n",
106 elt_index = elt->prev;
110 fformat (stdout, "re-insert index %d value %d at head\n", 1, 1);
112 clib_dlist_addhead (tm->test_pool, head_index, 1);
114 fformat (stdout, "Dump forward links\n");
115 elt_index = head->next;
120 elt = pool_elt_at_index (tm->test_pool, elt_index);
121 fformat(stdout, "elt %d value %d\n",
123 elt_index = elt->next;
127 fformat (stdout, "Dump reverse links\n");
128 elt_index = head->prev;
133 elt = pool_elt_at_index (tm->test_pool, elt_index);
134 fformat(stdout, "elt %d value %d\n",
136 elt_index = elt->prev;
140 fformat (stdout, "Remove middle element\n");
142 clib_dlist_remove (tm->test_pool, 2);
143 elt = pool_elt_at_index (tm->test_pool, 2);
145 fformat (stdout, "removed index %d value %d\n", elt_index, elt->value);
147 fformat (stdout, "Dump forward links\n");
148 elt_index = head->next;
153 elt = pool_elt_at_index (tm->test_pool, elt_index);
154 fformat(stdout, "elt %d value %d\n",
156 elt_index = elt->next;
160 fformat (stdout, "Dump reverse links\n");
161 elt_index = head->prev;
166 elt = pool_elt_at_index (tm->test_pool, elt_index);
167 fformat(stdout, "elt %d value %d\n",
169 elt_index = elt->prev;
177 int main (int argc, char * argv[])
182 clib_mem_init (0, 3ULL<<30);
184 unformat_init_command_line (&i, argv);
185 ret = test_dlist_main (&i);
190 #endif /* CLIB_UNIX */