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);
102 test_tree_search (void)
110 tree = g_tree_new (my_compare);
112 for (i = 0; chars[i]; i++)
113 g_tree_insert (tree, &chars[i], &chars[i]);
115 g_tree_foreach (tree, my_traverse, NULL);
117 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars));
118 g_assert_cmpint (g_tree_height (tree), ==, 6);
121 g_tree_foreach (tree, check_order, &p);
123 for (i = 0; i < 26; i++)
125 removed = g_tree_remove (tree, &chars[i + 10]);
130 removed = g_tree_remove (tree, &c);
133 g_tree_foreach (tree, my_traverse, NULL);
135 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars2));
136 g_assert_cmpint (g_tree_height (tree), ==, 6);
139 g_tree_foreach (tree, check_order, &p);
141 for (i = 25; i >= 0; i--)
142 g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
145 g_tree_foreach (tree, check_order, &p);
148 p = g_tree_lookup (tree, &c);
149 g_assert (p && *p == c);
150 g_assert (g_tree_lookup_extended (tree, &c, (gpointer *)&d, (gpointer *)&p));
151 g_assert (c == *d && c == *p);
154 p = g_tree_lookup (tree, &c);
155 g_assert (p && *p == c);
158 p = g_tree_lookup (tree, &c);
159 g_assert (p && *p == c);
162 p = g_tree_lookup (tree, &c);
163 g_assert (p && *p == c);
166 p = g_tree_lookup (tree, &c);
167 g_assert (p == NULL);
170 p = g_tree_lookup (tree, &c);
171 g_assert (p == NULL);
174 p = g_tree_lookup (tree, &c);
175 g_assert (p == NULL);
178 p = g_tree_search (tree, my_search, &c);
179 g_assert (p && *p == c);
182 p = g_tree_search (tree, my_search, &c);
183 g_assert (p && *p == c);
186 p = g_tree_search (tree, my_search, &c);
187 g_assert (p &&*p == c);
190 p = g_tree_search (tree, my_search, &c);
191 g_assert (p && *p == c);
194 p = g_tree_search (tree, my_search, &c);
195 g_assert (p == NULL);
198 p = g_tree_search (tree, my_search, &c);
199 g_assert (p == NULL);
202 p = g_tree_search (tree, my_search, &c);
203 g_assert (p == NULL);
205 g_tree_destroy (tree);
209 test_tree_remove (void)
216 tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL,
220 for (i = 0; chars[i]; i++)
221 g_tree_insert (tree, &chars[i], &chars[i]);
224 g_tree_insert (tree, &c, &c);
225 g_assert (destroyed_key == &c);
226 g_assert (destroyed_value == &chars[0]);
227 destroyed_key = NULL;
228 destroyed_value = NULL;
231 g_tree_replace (tree, &d, &d);
232 g_assert (destroyed_key == &chars[1]);
233 g_assert (destroyed_value == &chars[1]);
234 destroyed_key = NULL;
235 destroyed_value = NULL;
238 removed = g_tree_remove (tree, &c);
240 g_assert (destroyed_key == &chars[2]);
241 g_assert (destroyed_value == &chars[2]);
242 destroyed_key = NULL;
243 destroyed_value = NULL;
246 removed = g_tree_steal (tree, &c);
248 g_assert (destroyed_key == NULL);
249 g_assert (destroyed_value == NULL);
251 g_tree_destroy (tree);
255 test_tree_destroy (void)
260 tree = g_tree_new (my_compare);
262 for (i = 0; chars[i]; i++)
263 g_tree_insert (tree, &chars[i], &chars[i]);
265 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars));
268 g_tree_destroy (tree);
270 g_assert_cmpint (g_tree_nnodes (tree), ==, 0);
276 main (int argc, char *argv[])
278 g_test_init (&argc, &argv, NULL);
280 g_test_add_func ("/tree/search", test_tree_search);
281 g_test_add_func ("/tree/remove", test_tree_remove);
282 g_test_add_func ("/tree/destroy", test_tree_destroy);
284 return g_test_run ();