Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 1 | /* |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 18 | typedef struct |
| 19 | { |
| 20 | dlist_elt_t *test_pool; |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 21 | u32 head_index; |
| 22 | } test_main_t; |
| 23 | |
| 24 | test_main_t test_main; |
| 25 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 26 | int |
| 27 | test_dlist_main (unformat_input_t * input) |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 28 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 29 | test_main_t *tm = &test_main; |
| 30 | dlist_elt_t *head, *elt; |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 31 | u32 elt_index, head_index; |
| 32 | u32 value; |
| 33 | int i; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 34 | |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 35 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 58 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 59 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 70 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 71 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 91 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 92 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 103 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 104 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 120 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 132 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 142 | |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 143 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 150 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 151 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 162 | fformat (stdout, "elt %d value %d\n", i++, elt->value); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 163 | elt_index = elt->prev; |
| 164 | value = elt->value; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 171 | int |
| 172 | main (int argc, char *argv[]) |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 173 | { |
| 174 | unformat_input_t i; |
| 175 | int ret; |
| 176 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 177 | clib_mem_init (0, 3ULL << 30); |
Dave Barach | 034fccc | 2016-06-24 18:53:21 -0400 | [diff] [blame] | 178 | |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * fd.io coding-style-patch-verification: ON |
| 189 | * |
| 190 | * Local Variables: |
| 191 | * eval: (c-set-style "gnu") |
| 192 | * End: |
| 193 | */ |