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;
68 my_traverse (gpointer key,
79 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
80 "abcdefghijklmnopqrstuvwxyz";
84 "abcdefghijklmnopqrstuvwxyz";
87 check_order (gpointer key,
94 g_assert (**p == *ch);
113 tree = g_tree_new (my_compare);
115 for (i = 0; chars[i]; i++)
116 g_tree_insert (tree, &chars[i], &chars[i]);
118 g_tree_foreach (tree, my_traverse, NULL);
120 g_assert (g_tree_nnodes (tree) == strlen (chars));
121 g_assert (g_tree_height (tree) == 6);
124 g_tree_foreach (tree, check_order, &p);
126 for (i = 0; i < 26; i++)
128 removed = g_tree_remove (tree, &chars[i + 10]);
133 removed = g_tree_remove (tree, &c);
134 g_assert (removed == FALSE);
136 g_tree_foreach (tree, my_traverse, NULL);
138 g_assert (g_tree_nnodes (tree) == strlen (chars2));
139 g_assert (g_tree_height (tree) == 6);
142 g_tree_foreach (tree, check_order, &p);
144 for (i = 25; i >= 0; i--)
145 g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
148 g_tree_foreach (tree, check_order, &p);
151 p = g_tree_lookup (tree, &c);
152 g_assert (p && *p == c);
155 p = g_tree_lookup (tree, &c);
156 g_assert (p && *p == c);
159 p = g_tree_lookup (tree, &c);
160 g_assert (p && *p == c);
163 p = g_tree_lookup (tree, &c);
164 g_assert (p && *p == c);
167 p = g_tree_lookup (tree, &c);
168 g_assert (p == NULL);
171 p = g_tree_lookup (tree, &c);
172 g_assert (p == NULL);
175 p = g_tree_lookup (tree, &c);
176 g_assert (p == NULL);
179 p = g_tree_search (tree, my_search, &c);
180 g_assert (p && *p == c);
183 p = g_tree_search (tree, my_search, &c);
184 g_assert (p && *p == c);
187 p = g_tree_search (tree, my_search, &c);
188 g_assert (p &&*p == c);
191 p = g_tree_search (tree, my_search, &c);
192 g_assert (p && *p == c);
195 p = g_tree_search (tree, my_search, &c);
196 g_assert (p == NULL);
199 p = g_tree_search (tree, my_search, &c);
200 g_assert (p == NULL);
203 p = g_tree_search (tree, my_search, &c);
204 g_assert (p == NULL);
207 g_tree_destroy (tree);
209 tree = g_tree_new_full (my_compare, NULL,
213 for (i = 0; chars[i]; i++)
214 g_tree_insert (tree, &chars[i], &chars[i]);
217 g_tree_insert (tree, &c, &c);
218 g_assert (destroyed_key == &c);
219 g_assert (destroyed_value == &chars[0]);
220 destroyed_key = NULL;
221 destroyed_value = NULL;
224 g_tree_replace (tree, &d, &d);
225 g_assert (destroyed_key == &chars[1]);
226 g_assert (destroyed_value == &chars[1]);
227 destroyed_key = NULL;
228 destroyed_value = NULL;
231 removed = g_tree_remove (tree, &c);
233 g_assert (destroyed_key == &chars[2]);
234 g_assert (destroyed_value == &chars[2]);
235 destroyed_key = NULL;
236 destroyed_value = NULL;
239 removed = g_tree_steal (tree, &c);
241 g_assert (destroyed_key == NULL);
242 g_assert (destroyed_value == NULL);