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
30 /* We are testing some deprecated APIs here */
31 #define GLIB_DISABLE_DEPRECATION_WARNINGS
39 my_compare (gconstpointer a,
49 my_compare_with_data (gconstpointer a,
56 /* just check that we got the right data */
57 g_assert (GPOINTER_TO_INT(user_data) == 123);
63 my_search (gconstpointer a,
66 return my_compare (b, a);
69 static gpointer destroyed_key = NULL;
70 static gpointer destroyed_value = NULL;
73 my_key_destroy (gpointer key)
79 my_value_destroy (gpointer value)
81 destroyed_value = value;
85 my_traverse (gpointer key,
96 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
97 "abcdefghijklmnopqrstuvwxyz";
101 "abcdefghijklmnopqrstuvwxyz";
104 check_order (gpointer key,
111 g_assert (**p == *ch);
119 test_tree_search (void)
127 tree = g_tree_new_with_data (my_compare_with_data, GINT_TO_POINTER(123));
129 for (i = 0; chars[i]; i++)
130 g_tree_insert (tree, &chars[i], &chars[i]);
132 g_tree_foreach (tree, my_traverse, NULL);
134 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars));
135 g_assert_cmpint (g_tree_height (tree), ==, 6);
138 g_tree_foreach (tree, check_order, &p);
140 for (i = 0; i < 26; i++)
142 removed = g_tree_remove (tree, &chars[i + 10]);
147 removed = g_tree_remove (tree, &c);
150 g_tree_foreach (tree, my_traverse, NULL);
152 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars2));
153 g_assert_cmpint (g_tree_height (tree), ==, 6);
156 g_tree_foreach (tree, check_order, &p);
158 for (i = 25; i >= 0; i--)
159 g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
162 g_tree_foreach (tree, check_order, &p);
165 p = g_tree_lookup (tree, &c);
166 g_assert (p && *p == c);
167 g_assert (g_tree_lookup_extended (tree, &c, (gpointer *)&d, (gpointer *)&p));
168 g_assert (c == *d && c == *p);
171 p = g_tree_lookup (tree, &c);
172 g_assert (p && *p == c);
175 p = g_tree_lookup (tree, &c);
176 g_assert (p && *p == c);
179 p = g_tree_lookup (tree, &c);
180 g_assert (p && *p == c);
183 p = g_tree_lookup (tree, &c);
184 g_assert (p == NULL);
187 p = g_tree_lookup (tree, &c);
188 g_assert (p == NULL);
191 p = g_tree_lookup (tree, &c);
192 g_assert (p == NULL);
195 p = g_tree_search (tree, my_search, &c);
196 g_assert (p && *p == c);
199 p = g_tree_search (tree, my_search, &c);
200 g_assert (p && *p == c);
203 p = g_tree_search (tree, my_search, &c);
204 g_assert (p &&*p == c);
207 p = g_tree_search (tree, my_search, &c);
208 g_assert (p && *p == c);
211 p = g_tree_search (tree, my_search, &c);
212 g_assert (p == NULL);
215 p = g_tree_search (tree, my_search, &c);
216 g_assert (p == NULL);
219 p = g_tree_search (tree, my_search, &c);
220 g_assert (p == NULL);
222 g_tree_destroy (tree);
226 test_tree_remove (void)
234 tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL,
238 for (i = 0; chars[i]; i++)
239 g_tree_insert (tree, &chars[i], &chars[i]);
242 g_tree_insert (tree, &c, &c);
243 g_assert (destroyed_key == &c);
244 g_assert (destroyed_value == &chars[0]);
245 destroyed_key = NULL;
246 destroyed_value = NULL;
249 g_tree_replace (tree, &d, &d);
250 g_assert (destroyed_key == &chars[1]);
251 g_assert (destroyed_value == &chars[1]);
252 destroyed_key = NULL;
253 destroyed_value = NULL;
256 removed = g_tree_remove (tree, &c);
258 g_assert (destroyed_key == &chars[2]);
259 g_assert (destroyed_value == &chars[2]);
260 destroyed_key = NULL;
261 destroyed_value = NULL;
264 removed = g_tree_steal (tree, &c);
266 g_assert (destroyed_key == NULL);
267 g_assert (destroyed_value == NULL);
269 remove = "omkjigfedba";
270 for (i = 0; remove[i]; i++)
272 removed = g_tree_remove (tree, &remove[i]);
276 g_tree_destroy (tree);
280 test_tree_destroy (void)
285 tree = g_tree_new (my_compare);
287 for (i = 0; chars[i]; i++)
288 g_tree_insert (tree, &chars[i], &chars[i]);
290 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars));
293 g_tree_destroy (tree);
295 g_assert_cmpint (g_tree_nnodes (tree), ==, 0);
301 traverse_func (gpointer key, gpointer value, gpointer data)
313 test_tree_traverse (void)
319 tree = g_tree_new (my_compare);
321 for (i = 0; chars[i]; i++)
322 g_tree_insert (tree, &chars[i], &chars[i]);
324 result = g_new0 (gchar, strlen (chars) + 1);
327 g_tree_traverse (tree, traverse_func, G_IN_ORDER, &p);
328 g_assert_cmpstr (result, ==, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
331 g_tree_traverse (tree, traverse_func, G_PRE_ORDER, &p);
332 g_assert_cmpstr (result, ==, "VF73102546B98ADCENJHGILKMRPOQTSUldZXWYbachfegjiktpnmorqsxvuwyz");
335 g_tree_traverse (tree, traverse_func, G_POST_ORDER, &p);
336 g_assert_cmpstr (result, ==, "02146538A9CEDB7GIHKMLJOQPSUTRNFWYXacbZegfikjhdmonqsrpuwvzyxtlV");
343 main (int argc, char *argv[])
345 g_test_init (&argc, &argv, NULL);
347 g_test_add_func ("/tree/search", test_tree_search);
348 g_test_add_func ("/tree/remove", test_tree_remove);
349 g_test_add_func ("/tree/destroy", test_tree_destroy);
350 g_test_add_func ("/tree/traverse", test_tree_traverse);
352 return g_test_run ();