summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/test_dlist.c
blob: c5535c854a60db8341ef247203ce2e0be692ad00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { 
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vppinfra/dlist.h>

typedef struct
{
  dlist_elt_t *test_pool;
  u32 head_index;
} test_main_t;

test_main_t test_main;

int
test_dlist_main (unformat_input_t * input)
{
  test_main_t *tm = &test_main;
  dlist_elt_t *head, *elt;
  u32 elt_index, head_index;
  u32 value;
  int i;

  pool_get (tm->test_pool, head);
  head_index = head - tm->test_pool;
  clib_dlist_init (tm->test_pool, head - tm->test_pool);

  for (i = 1; i <= 3; i++)
    {
      pool_get (tm->test_pool, elt);
      elt_index = elt - tm->test_pool;

      clib_dlist_init (tm->test_pool, elt_index);
      elt->value = i;
      clib_dlist_addtail (tm->test_pool, head_index, elt_index);
    }

  head = pool_elt_at_index (tm->test_pool, head_index);

  fformat (stdout, "Dump forward links\n");
  elt_index = head->next;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->next;
      value = elt->value;
    }

  fformat (stdout, "Dump reverse links\n");
  elt_index = head->prev;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->prev;
      value = elt->value;
    }

  fformat (stdout, "remove first element\n");

  elt_index = clib_dlist_remove_head (tm->test_pool, head_index);
  elt = pool_elt_at_index (tm->test_pool, elt_index);

  fformat (stdout, "removed index %d value %d\n", elt_index, elt->value);

  head = pool_elt_at_index (tm->test_pool, head_index);

  fformat (stdout, "Dump forward links\n");
  elt_index = head->next;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->next;
      value = elt->value;
    }

  fformat (stdout, "Dump reverse links\n");
  elt_index = head->prev;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->prev;
      value = elt->value;
    }

  fformat (stdout, "re-insert index %d value %d at head\n", 1, 1);

  clib_dlist_addhead (tm->test_pool, head_index, 1);

  fformat (stdout, "Dump forward links\n");
  elt_index = head->next;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->next;
      value = elt->value;
    }

  fformat (stdout, "Dump reverse links\n");
  elt_index = head->prev;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->prev;
      value = elt->value;
    }

  fformat (stdout, "Remove middle element\n");

  clib_dlist_remove (tm->test_pool, 2);
  elt = pool_elt_at_index (tm->test_pool, 2);

  fformat (stdout, "removed index %d value %d\n", elt_index, elt->value);

  fformat (stdout, "Dump forward links\n");
  elt_index = head->next;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->next;
      value = elt->value;
    }

  fformat (stdout, "Dump reverse links\n");
  elt_index = head->prev;
  i = 1;
  value = 0;
  while (value != ~0)
    {
      elt = pool_elt_at_index (tm->test_pool, elt_index);
      fformat (stdout, "elt %d value %d\n", i++, elt->value);
      elt_index = elt->prev;
      value = elt->value;
    }

  return 0;
}

#ifdef CLIB_UNIX
int
main (int argc, char *argv[])
{
  unformat_input_t i;
  int ret;

  clib_mem_init (0, 3ULL << 30);

  unformat_init_command_line (&i, argv);
  ret = test_dlist_main (&i);
  unformat_free (&i);

  return ret;
}
#endif /* CLIB_UNIX */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */