2 * Copyright (c) 2019 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.
15 #include <vppinfra/rbtree.h>
16 #include <vlib/vlib.h>
18 #define RBTREE_TEST_I(_cond, _comment, _args...) \
20 int _evald = (_cond); \
22 fformat(stderr, "FAIL:%d: " _comment "\n", \
25 fformat(stderr, "PASS:%d: " _comment "\n", \
31 #define RBTREE_TEST(_cond, _comment, _args...) \
33 if (!RBTREE_TEST_I(_cond, _comment, ##_args)) { \
39 rbtree_test_basic (vlib_main_t * vm, unformat_input_t * input)
41 int __clib_unused verbose, n_keys = 1e3, i;
42 u32 *test_keys = 0, search_key;
43 rb_tree_t _rt, *rt = &_rt;
46 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
48 if (unformat (input, "verbose"))
50 else if (unformat (input, "nkeys %u", &n_keys))
54 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
61 RBTREE_TEST (rb_tree_n_nodes (rt) == 1, "tnil created");
66 vec_validate (test_keys, n_keys - 1);
67 for (i = n_keys - 1; i >= 0; i--)
73 RBTREE_TEST (rb_tree_n_nodes (rt) == n_keys + 1, "all nodes added");
75 n = rb_tree_max_subtree (rt, rb_node (rt, rt->root));
76 RBTREE_TEST (n->key == n_keys - 1, "max should be %u", n_keys - 1);
78 n = rb_tree_min_subtree (rt, rb_node (rt, rt->root));
79 RBTREE_TEST (n->key == 0, "min should be %u", 0);
81 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), n_keys / 2);
82 RBTREE_TEST (n->key == n_keys / 2, "search result should be %u",
85 aux = rb_tree_successor (rt, n);
86 RBTREE_TEST (aux->key == n_keys / 2 + 1, "successor should be %u is %u",
87 n_keys / 2 + 1, aux->key);
89 aux = rb_tree_predecessor (rt, n);
90 RBTREE_TEST (aux->key == n_keys / 2 - 1, "predecessor should be %u is %u",
91 n_keys / 2 - 1, aux->key);
93 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), n_keys);
94 RBTREE_TEST (rb_node_is_tnil (rt, n), "search result should be tnil");
99 for (i = 0; i < n_keys; i += 2)
102 n = rb_tree_max_subtree (rt, rb_node (rt, rt->root));
103 RBTREE_TEST (n->key == n_keys - 1, "max should be %u", n_keys - 1);
105 n = rb_tree_min_subtree (rt, rb_node (rt, rt->root));
106 RBTREE_TEST (n->key == 1, "min should be %u and is %u", 1, n->key);
108 search_key = 2 * ((n_keys - 1) / 4);
109 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), search_key);
110 RBTREE_TEST (rb_node_is_tnil (rt, n), "search result for %u should be tnil",
114 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), search_key);
115 RBTREE_TEST (n->key == search_key, "search result should be %u",
118 aux = rb_tree_successor (rt, n);
119 RBTREE_TEST (aux->key == search_key + 2, "successor should be %u is %u",
120 search_key + 2, aux->key);
122 aux = rb_tree_predecessor (rt, n);
123 RBTREE_TEST (aux->key == search_key - 2, "predecessor should be %u is %u",
124 search_key - 2, aux->key);
129 for (i = 0; i < n_keys; i += 2)
132 RBTREE_TEST (rb_tree_n_nodes (rt) == n_keys + 1, "number nodes %u is %u",
133 n_keys + 1, rb_tree_n_nodes (rt));
135 n = rb_tree_max_subtree (rt, rb_node (rt, rt->root));
136 RBTREE_TEST (n->key == n_keys - 1, "max should be %u", n_keys - 1);
138 n = rb_tree_min_subtree (rt, rb_node (rt, rt->root));
139 RBTREE_TEST (n->key == 0, "min should be %u", 0);
141 search_key = 2 * ((n_keys - 1) / 4);
142 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), search_key);
143 RBTREE_TEST (n->key == search_key, "search result should be %u",
147 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), search_key);
148 RBTREE_TEST (n->key == search_key, "search result should be %u",
151 aux = rb_tree_successor (rt, n);
152 RBTREE_TEST (aux->key == search_key + 1, "successor should be %u is %u",
153 search_key + 1, aux->key);
155 aux = rb_tree_predecessor (rt, n);
156 RBTREE_TEST (aux->key == search_key - 1, "predecessor should be %u is %u",
157 search_key - 1, aux->key);
162 for (i = 0; i < n_keys; i++)
165 if (rt->nodes[RBTREE_TNIL_INDEX].color != RBTREE_BLACK)
166 RBTREE_TEST (0, "tnil should be black");
169 RBTREE_TEST (rb_tree_n_nodes (rt) == 1, "number nodes %u is %u",
170 1, rb_tree_n_nodes (rt));
172 n = rb_tree_min_subtree (rt, rb_node (rt, rt->root));
173 RBTREE_TEST (rb_node_is_tnil (rt, n), "min should be tnil");
175 n = rb_tree_max_subtree (rt, rb_node (rt, rt->root));
176 RBTREE_TEST (rb_node_is_tnil (rt, n), "max should be tnil");
178 search_key = 2 * ((n_keys - 1) / 4);
179 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), search_key);
180 RBTREE_TEST (rb_node_is_tnil (rt, n), "search result should be tnil");
183 * Test successor/predecessor
185 u8 test_vals[] = { 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20 };
186 for (i = 0; i < sizeof (test_vals) / sizeof (u8); i++)
187 rb_tree_add (rt, test_vals[i]);
190 n = rb_tree_search_subtree (rt, rb_node (rt, rt->root), search_key);
191 RBTREE_TEST (n->key == search_key, "search result should be %u",
194 aux = rb_tree_successor (rt, n);
195 RBTREE_TEST (aux->key == 15, "successor should be %u is %u", 15, aux->key);
197 aux = rb_tree_predecessor (rt, n);
198 RBTREE_TEST (aux->key == 9, "predecessor should be %u is %u", 9, aux->key);
201 aux = rb_tree_predecessor (rt, n);
202 RBTREE_TEST (aux->key == 7, "predecessor should be %u is %u", 7, aux->key);
207 rb_tree_free_nodes (rt);
208 RBTREE_TEST (rb_tree_n_nodes (rt) == 0, "number nodes %u is %u",
209 0, rb_tree_n_nodes (rt));
214 static clib_error_t *
215 rbtree_test (vlib_main_t * vm,
216 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
220 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
222 if (unformat (input, "basic"))
224 res = rbtree_test_basic (vm, input);
226 else if (unformat (input, "all"))
228 if ((res = rbtree_test_basic (vm, input)))
237 return clib_error_return (0, "rbtree unit test failed");
242 VLIB_CLI_COMMAND (rbtree_test_command, static) =
244 .path = "test rbtree",
245 .short_help = "internal rbtree unit tests",
246 .function = rbtree_test,
251 * fd.io coding-style-patch-verification: ON
254 * eval: (c-set-style "gnu")