1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #undef G_DISABLE_ASSERT
36 #define C2P(c) ((gpointer) ((long) (c)))
37 #define P2C(p) ((gchar) ((long) (p)))
40 node_build_string (GNode *node,
47 c[0] = P2C (node->data);
49 string = g_strconcat (*p ? *p : "", c, NULL);
70 root = g_node_new (C2P ('A'));
71 node_B = g_node_new (C2P ('B'));
72 g_node_append (root, node_B);
73 g_node_append_data (node_B, C2P ('E'));
74 g_node_prepend_data (node_B, C2P ('C'));
75 node_D = g_node_new (C2P ('D'));
76 g_node_insert (node_B, 1, node_D);
77 node_F = g_node_new (C2P ('F'));
78 g_node_append (root, node_F);
79 node_G = g_node_new (C2P ('G'));
80 g_node_append (node_F, node_G);
81 node_J = g_node_new (C2P ('J'));
82 g_node_prepend (node_G, node_J);
83 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
84 g_node_insert_data (node_G, 0, C2P ('H'));
85 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
95 * for in-order traversal, 'G' is considered to be the "left"
96 * child of 'F', which will cause 'F' to be the last node visited.
99 node_C = node_B->children;
100 node_E = node_D->next;
102 n = g_node_last_sibling (node_C);
103 g_assert (n == node_E);
104 n = g_node_last_sibling (node_D);
105 g_assert (n == node_E);
106 n = g_node_last_sibling (node_E);
107 g_assert (n == node_E);
110 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
111 g_assert_cmpstr (tstring, ==, "ABCDEFGHIJK");
112 g_free (tstring); tstring = NULL;
113 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
114 g_assert_cmpstr (tstring, ==, "ABF");
115 g_free (tstring); tstring = NULL;
116 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
117 g_assert_cmpstr (tstring, ==, "CDEBHIJKGFA");
118 g_free (tstring); tstring = NULL;
119 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
120 g_assert_cmpstr (tstring, ==, "BFA");
121 g_free (tstring); tstring = NULL;
122 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
123 g_assert_cmpstr (tstring, ==, "CBDEAHGIJKF");
124 g_free (tstring); tstring = NULL;
125 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
126 g_assert_cmpstr (tstring, ==, "BAF");
127 g_free (tstring); tstring = NULL;
128 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
129 g_assert_cmpstr (tstring, ==, "ABFCDEGHIJK");
130 g_free (tstring); tstring = NULL;
131 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
132 g_assert_cmpstr (tstring, ==, "ABF");
133 g_free (tstring); tstring = NULL;
135 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
136 g_assert_cmpstr (tstring, ==, "CDEHIJK");
137 g_free (tstring); tstring = NULL;
138 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
139 g_assert_cmpstr (tstring, ==, "ABFG");
140 g_free (tstring); tstring = NULL;
142 g_node_reverse_children (node_B);
143 g_node_reverse_children (node_G);
145 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
146 g_assert_cmpstr (tstring, ==, "ABFEDCGKJIH");
147 g_free (tstring); tstring = NULL;
149 g_node_append (node_D, g_node_new (C2P ('L')));
150 g_node_append (node_D, g_node_new (C2P ('M')));
152 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
153 g_assert_cmpstr (tstring, ==, "ABFEDCGLMKJIH");
154 g_free (tstring); tstring = NULL;
156 g_node_destroy (root);
160 construct_test (void)
171 root = g_node_new (C2P ('A'));
172 g_assert_cmpint (g_node_depth (root), ==, 1);
173 g_assert_cmpint (g_node_max_height (root), ==, 1);
175 node_B = g_node_new (C2P ('B'));
176 g_node_append (root, node_B);
177 g_assert (root->children == node_B);
179 g_node_append_data (node_B, C2P ('E'));
180 g_node_prepend_data (node_B, C2P ('C'));
181 node_D = g_node_new (C2P ('D'));
182 g_node_insert (node_B, 1, node_D);
184 node_F = g_node_new (C2P ('F'));
185 g_node_append (root, node_F);
186 g_assert (root->children->next == node_F);
188 node_G = g_node_new (C2P ('G'));
189 g_node_append (node_F, node_G);
190 node_J = g_node_new (C2P ('J'));
191 g_node_prepend (node_G, node_J);
192 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
193 g_node_insert_data (node_G, 0, C2P ('H'));
194 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
204 g_assert_cmpint (g_node_depth (root), ==, 1);
205 g_assert_cmpint (g_node_max_height (root), ==, 4);
206 g_assert_cmpint (g_node_depth (node_G->children->next), ==, 4);
207 g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_LEAFS), ==, 7);
208 g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS), ==, 4);
209 g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 11);
210 g_assert_cmpint (g_node_max_height (node_F), ==, 3);
211 g_assert_cmpint (g_node_n_children (node_G), ==, 4);
212 g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
213 g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
214 g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
216 for (i = 0; i < g_node_n_children (node_B); i++)
218 node = g_node_nth_child (node_B, i);
219 g_assert_cmpint (P2C (node->data), ==, ('C' + i));
222 for (i = 0; i < g_node_n_children (node_G); i++)
223 g_assert_cmpint (g_node_child_position (node_G, g_node_nth_child (node_G, i)), ==, i);
225 g_node_destroy (root);
229 allocation_test (void)
235 root = g_node_new (NULL);
238 for (i = 0; i < 2048; i++)
240 g_node_append (node, g_node_new (NULL));
242 node = node->children->next;
244 g_assert_cmpint (g_node_max_height (root), >, 100);
245 g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 1 + 2048);
247 g_node_destroy (root);
261 root = g_node_new (C2P ('A'));
262 node_B = g_node_new (C2P ('B'));
263 g_node_append (root, node_B);
264 node_D = g_node_new (C2P ('D'));
265 g_node_append (root, node_D);
266 node_C = g_node_new (C2P ('C'));
267 g_node_insert_after (root, node_B, node_C);
268 node_E = g_node_new (C2P ('E'));
269 g_node_append (node_C, node_E);
271 g_assert (g_node_get_root (node_E) == root);
272 g_assert (g_node_is_ancestor (root, node_B));
273 g_assert (g_node_is_ancestor (root, node_E));
274 g_assert (!g_node_is_ancestor (node_B, node_D));
275 g_assert (g_node_first_sibling (node_D) == node_B);
276 g_assert (g_node_first_sibling (node_E) == node_E);
277 g_assert_cmpint (g_node_child_index (root, C2P ('B')), ==, 0);
278 g_assert_cmpint (g_node_child_index (root, C2P ('C')), ==, 1);
279 g_assert_cmpint (g_node_child_index (root, C2P ('D')), ==, 2);
280 g_assert_cmpint (g_node_child_index (root, C2P ('E')), ==, -1);
283 g_node_children_foreach (root, G_TRAVERSE_ALL, (GNodeForeachFunc)node_build_string, &tstring);
284 g_assert_cmpstr (tstring, ==, "BCD");
285 g_free (tstring); tstring = NULL;
287 g_node_children_foreach (root, G_TRAVERSE_LEAVES, (GNodeForeachFunc)node_build_string, &tstring);
288 g_assert_cmpstr (tstring, ==, "BD");
289 g_free (tstring); tstring = NULL;
291 g_node_children_foreach (root, G_TRAVERSE_NON_LEAVES, (GNodeForeachFunc)node_build_string, &tstring);
292 g_assert_cmpstr (tstring, ==, "C");
293 g_free (tstring); tstring = NULL;
295 g_node_destroy (root);
299 check_order (GNode *node,
302 gchar **expected = data;
305 d = GPOINTER_TO_INT (node->data);
306 g_assert_cmpint (d, ==, **expected);
321 * -------- a --------
329 root = g_node_new (C2P ('a'));
330 node = g_node_append_data (root, C2P ('b'));
331 g_node_append_data (node, C2P ('e'));
332 g_node_append_data (node, C2P ('f'));
333 g_node_append_data (node, C2P ('g'));
335 node = cnode = g_node_append_data (root, C2P ('c'));
336 g_node_append_data (node, C2P ('h'));
337 g_node_append_data (node, C2P ('i'));
338 g_node_append_data (node, C2P ('j'));
340 node = g_node_append_data (root, C2P ('d'));
341 g_node_append_data (node, C2P ('k'));
342 g_node_append_data (node, C2P ('l'));
343 g_node_append_data (node, C2P ('m'));
345 g_node_unlink (cnode);
347 expected = "abdefgklm";
348 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
351 g_node_traverse (cnode, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
353 g_node_destroy (root);
354 g_node_destroy (cnode);
358 copy_up (gconstpointer src,
363 l = GPOINTER_TO_INT (src);
364 u = g_ascii_toupper (l);
366 return GINT_TO_POINTER ((int)u);
376 root = g_node_new (C2P ('a'));
377 g_node_append_data (root, C2P ('b'));
378 g_node_append_data (root, C2P ('c'));
379 g_node_append_data (root, C2P ('d'));
382 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
384 copy = g_node_copy (root);
387 g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
389 g_node_destroy (copy);
391 copy = g_node_copy_deep (root, copy_up, NULL);
394 g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
396 g_node_destroy (copy);
398 g_node_destroy (root);
405 g_test_init (&argc, &argv, NULL);
407 g_test_add_func ("/node/allocation", allocation_test);
408 g_test_add_func ("/node/construction", construct_test);
409 g_test_add_func ("/node/traversal", traversal_test);
410 g_test_add_func ("/node/misc", misc_test);
411 g_test_add_func ("/node/unlink", unlink_test);
412 g_test_add_func ("/node/copy", copy_test);
414 return g_test_run ();