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 my_compare (gconstpointer a,
46 my_search (gconstpointer a,
49 return my_compare (b, a);
52 static gpointer destroyed_key = NULL;
53 static gpointer destroyed_value = NULL;
56 my_key_destroy (gpointer key)
62 my_value_destroy (gpointer value)
64 destroyed_value = value;
67 static gint preorder_i;
68 static char *preorder;
71 traverse_preorder (gpointer key,
76 preorder[preorder_i++] = *ch;
81 calc_height_recur(char *inorder, char *preorder, int l, int r)
89 parent = preorder[l++];
90 parentpos_in = strchr(inorder, parent);
92 /* find the position of the right child in the preorder string */
94 for (i = l; i <= r; ++i)
95 if (strchr(inorder, preorder[i]) > parentpos_in) {
96 right = calc_height_recur(inorder, preorder, i, r);
97 r = i-1; /* exlcude the right subtree when looking at the left */
100 left = calc_height_recur(inorder, preorder, l, r);
101 return 1 + MAX(left, right);
105 calc_height(char *inorder, char *preorder, int n)
107 return calc_height_recur(inorder, preorder, 0, n-1);
110 static gint traverse_num = 0;
112 my_traverse (gpointer key,
117 g_assert ((*ch) > 0);
124 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
125 "abcdefghijklmnopqrstuvwxyz";
129 "abcdefghijklmnopqrstuvwxyz";
132 check_order (gpointer key,
139 g_assert (**p == *ch);
158 tree = g_tree_new (my_compare);
161 for (i = n-1; i >= 0; i--) {
164 r = g_tree_lookup_related (tree, &chars[i],
165 G_TREE_SEARCH_SUCCESSOR, 0);
166 g_assert((i == n-1 && r == NULL) || (r && *r == chars[i+1]));
168 g_tree_insert (tree, &chars[i], &chars[i]);
170 r = g_tree_lookup_related (tree, &chars[i],
171 G_TREE_SEARCH_PREDECESSOR, 0);
172 g_assert(r && *r == chars[i]);
173 r = g_tree_lookup_related (tree, &chars[i],
174 G_TREE_SEARCH_SUCCESSOR, 0);
175 g_assert(r && *r == chars[i]);
177 g_assert(n == g_tree_nnodes(tree));
179 for (i = 0; i < 26; i++)
181 removed = g_tree_remove (tree, &chars[i + 10]);
184 g_assert (g_tree_nnodes (tree) == strlen (chars2));
187 removed = g_tree_remove (tree, &c);
188 g_assert (removed == FALSE);
191 g_tree_foreach (tree, my_traverse, NULL);
192 g_assert(traverse_num == g_tree_nnodes(tree));
195 g_tree_foreach (tree, check_order, &p);
197 for (i = 25; i >= 0; i--)
198 g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
201 g_tree_foreach (tree, check_order, &p);
204 p = g_tree_lookup (tree, &c);
205 g_assert (p && *p == c);
208 p = g_tree_lookup (tree, &c);
209 g_assert (p && *p == c);
212 p = g_tree_lookup (tree, &c);
213 g_assert (p && *p == c);
216 p = g_tree_lookup (tree, &c);
217 g_assert (p && *p == c);
220 p = g_tree_lookup (tree, &c);
221 g_assert (p == NULL);
224 p = g_tree_lookup (tree, &c);
225 g_assert (p == NULL);
228 p = g_tree_lookup (tree, &c);
229 g_assert (p == NULL);
232 p = g_tree_search (tree, my_search, &c);
233 g_assert (p && *p == c);
236 p = g_tree_search (tree, my_search, &c);
237 g_assert (p && *p == c);
240 p = g_tree_search (tree, my_search, &c);
241 g_assert (p &&*p == c);
244 p = g_tree_search (tree, my_search, &c);
245 g_assert (p && *p == c);
248 p = g_tree_search (tree, my_search, &c);
249 g_assert (p == NULL);
252 p = g_tree_search (tree, my_search, &c);
253 g_assert (p == NULL);
256 p = g_tree_search (tree, my_search, &c);
257 g_assert (p == NULL);
261 /* test adding and removing items in a versioned tree */
262 tree = g_tree_new (my_compare);
264 for (i = 0; chars[i]; i++) {
268 r = g_tree_lookup_related (tree, &chars[i],
269 G_TREE_SEARCH_PREDECESSOR, i);
270 g_assert((i == 0 && r == NULL) || (r && *r == chars[i-1]));
274 r = g_tree_lookup_related (tree, &chars[i],
275 G_TREE_SEARCH_PREDECESSOR, i-1);
276 g_assert((i == 1 && r == NULL) || (r && *r == chars[i-2]));
279 g_tree_next_version (tree);
280 g_tree_insert (tree, &chars[i], &chars[i]);
282 r = g_tree_lookup_related (tree, &chars[i],
283 G_TREE_SEARCH_PREDECESSOR, i+1);
284 g_assert(r && *r == chars[i]);
285 r = g_tree_lookup_related (tree, &chars[i],
286 G_TREE_SEARCH_SUCCESSOR, i+1);
287 g_assert(r && *r == chars[i]);
289 for (j = 0; j <= i+1; ++j)
291 r = g_tree_lookup_related (tree, &chars[i],
292 G_TREE_SEARCH_PREDECESSOR, j);
293 g_assert((j == 0 && r == NULL) || (r && *r == chars[j-1]));
296 g_assert(i == g_tree_nnodes(tree));
299 g_tree_foreach (tree, my_traverse, NULL);
300 g_assert(traverse_num == g_tree_nnodes(tree));
302 g_assert (g_tree_nnodes (tree) == strlen (chars));
304 preorder = g_malloc(strlen(chars)+1);
305 preorder[strlen(chars)] = 0;
307 g_tree_traverse (tree, traverse_preorder, G_PRE_ORDER, NULL);
308 g_assert(traverse_num == g_tree_nnodes(tree));
309 g_assert(calc_height(chars, preorder, strlen(chars)) == g_tree_height(tree));
313 g_tree_foreach (tree, check_order, &p);
315 for (i = 0; i < 26; i++)
317 g_tree_next_version (tree);
318 removed = g_tree_remove (tree, &chars[i + 10]);
319 g_assert (removed == FALSE);
321 g_assert (g_tree_nnodes (tree) == strlen (chars2));
323 g_tree_next_version (tree);
324 removed = g_tree_remove (tree, &chars[36]);
325 g_assert (removed == FALSE);
326 removed = g_tree_remove (tree, &chars[37]);
327 g_assert (removed == FALSE);
328 removed = g_tree_remove (tree, &chars[38]);
329 g_assert (removed == FALSE);
332 for (i = 0; i <= n+26+1; ++i)
337 for (j = 0; chars[j]; ++j)
339 r = g_tree_lookup_related (tree, &chars[j],
340 G_TREE_SEARCH_PREDECESSOR, i);
342 if (i == 0) /* the tree was empty */
343 g_assert (r == NULL);
344 else if (i <= n) /* items were being inserted */
347 g_assert (r && *r == chars[j]);
349 g_assert (r && *r == chars[i-1]);
351 else /* some items have been removed */
354 g_assert (r && *r == chars[j]);
355 else if (i == n+26+1 && j >= 36 && j <= 38)
356 g_assert (r && *r == chars[9]);
357 else if (j > 10+i-(n+1)) /* last char that has been removed in
359 g_assert (r && *r == chars[j]);
361 g_assert (r && *r == chars[9]);
368 /* test removing versions from a tree */
369 tree = g_tree_new (my_compare);
371 for (i = 0; i < chars[i]; i++) {
372 g_tree_insert (tree, &chars[i], &chars[i]);
373 g_tree_next_version (tree);
375 g_assert(i == g_tree_nnodes(tree));
377 /* remove everything from the last version */
378 for (i = 0; chars[i]; i++)
379 g_tree_remove (tree, &chars[i]);
380 g_assert(g_tree_nnodes(tree) == 0);
382 g_assert (g_tree_current_version (tree) == i);
384 for (i = 0; i < g_tree_current_version (tree); ++i)
389 for (j = 0; chars[j]; j++)
391 r = g_tree_lookup_related (tree, &chars[j],
392 G_TREE_SEARCH_PREDECESSOR, i);
393 g_assert (r && *r == chars[MIN(j,i)]);
397 for (i = 0; i < g_tree_current_version (tree); ++i)
401 g_tree_delete_versions (tree, i);
403 for (k = 0; k < g_tree_current_version (tree); ++k)
408 for (j = 0; chars[j]; j++)
410 r = g_tree_lookup_related (tree, &chars[j],
411 G_TREE_SEARCH_PREDECESSOR, k);
412 g_assert ((k <= i/2*2 && r == NULL) ||
413 (k > i/2*2 && r && *r == chars[MIN(j,k)]));
418 g_tree_delete_versions (tree, g_tree_current_version (tree)-1);
420 for (i = 0; i < g_tree_current_version (tree); ++i)
425 for (j = 0; chars[j]; j++)
427 r = g_tree_lookup_related (tree, &chars[j],
428 G_TREE_SEARCH_PREDECESSOR, i);
429 g_assert (r == NULL);
435 /* test destroying a versioned tree */
436 tree = g_tree_new (my_compare);
438 for (i = 0; chars[i]; i++) {
439 g_tree_next_version (tree);
440 g_tree_insert (tree, &chars[i], &chars[i]);
445 tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL,
449 for (i = 0; chars[i]; i++)
450 g_tree_insert (tree, &chars[i], &chars[i]);
453 g_tree_insert (tree, &c, &c);
454 g_assert (destroyed_key == &c);
455 g_assert (destroyed_value == &chars[0]);
456 destroyed_key = NULL;
457 destroyed_value = NULL;
460 g_tree_replace (tree, &d, &d);
461 g_assert (destroyed_key == &chars[1]);
462 g_assert (destroyed_value == &chars[1]);
463 destroyed_key = NULL;
464 destroyed_value = NULL;
467 removed = g_tree_remove (tree, &c);
469 g_assert (destroyed_key == &chars[2]);
470 g_assert (destroyed_value == &chars[2]);
471 destroyed_key = NULL;
472 destroyed_value = NULL;
475 removed = g_tree_steal (tree, &c);
477 g_assert (destroyed_key == NULL);
478 g_assert (destroyed_value == NULL);