Add some more cases to the app-id unit tests
[glib.git] / glib / tests / tree.c
bloba8da67df01670daafd5303092988d548497399a0
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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #undef G_DISABLE_ASSERT
26 #undef G_LOG_DOMAIN
28 /* We are testing some deprecated APIs here */
29 #define GLIB_DISABLE_DEPRECATION_WARNINGS
31 #include <stdio.h>
32 #include <string.h>
33 #include "glib.h"
36 static gint
37 my_compare (gconstpointer a,
38 gconstpointer b)
40 const char *cha = a;
41 const char *chb = b;
43 return *cha - *chb;
46 static gint
47 my_compare_with_data (gconstpointer a,
48 gconstpointer b,
49 gpointer user_data)
51 const char *cha = a;
52 const char *chb = b;
54 /* just check that we got the right data */
55 g_assert (GPOINTER_TO_INT(user_data) == 123);
57 return *cha - *chb;
60 static gint
61 my_search (gconstpointer a,
62 gconstpointer b)
64 return my_compare (b, a);
67 static gpointer destroyed_key = NULL;
68 static gpointer destroyed_value = NULL;
70 static void
71 my_key_destroy (gpointer key)
73 destroyed_key = key;
76 static void
77 my_value_destroy (gpointer value)
79 destroyed_value = value;
82 static gint
83 my_traverse (gpointer key,
84 gpointer value,
85 gpointer data)
87 char *ch = key;
89 g_assert ((*ch) > 0);
91 if (*ch == 'd')
92 return TRUE;
94 return FALSE;
97 char chars[] =
98 "0123456789"
99 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
100 "abcdefghijklmnopqrstuvwxyz";
102 char chars2[] =
103 "0123456789"
104 "abcdefghijklmnopqrstuvwxyz";
106 static gint
107 check_order (gpointer key,
108 gpointer value,
109 gpointer data)
111 char **p = data;
112 char *ch = key;
114 g_assert (**p == *ch);
116 (*p)++;
118 return FALSE;
121 static void
122 test_tree_search (void)
124 gint i;
125 GTree *tree;
126 gboolean removed;
127 gchar c;
128 gchar *p, *d;
130 tree = g_tree_new_with_data (my_compare_with_data, GINT_TO_POINTER(123));
132 for (i = 0; chars[i]; i++)
133 g_tree_insert (tree, &chars[i], &chars[i]);
135 g_tree_foreach (tree, my_traverse, NULL);
137 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars));
138 g_assert_cmpint (g_tree_height (tree), ==, 6);
140 p = chars;
141 g_tree_foreach (tree, check_order, &p);
143 for (i = 0; i < 26; i++)
145 removed = g_tree_remove (tree, &chars[i + 10]);
146 g_assert (removed);
149 c = '\0';
150 removed = g_tree_remove (tree, &c);
151 g_assert (!removed);
153 g_tree_foreach (tree, my_traverse, NULL);
155 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars2));
156 g_assert_cmpint (g_tree_height (tree), ==, 6);
158 p = chars2;
159 g_tree_foreach (tree, check_order, &p);
161 for (i = 25; i >= 0; i--)
162 g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
164 p = chars;
165 g_tree_foreach (tree, check_order, &p);
167 c = '0';
168 p = g_tree_lookup (tree, &c);
169 g_assert (p && *p == c);
170 g_assert (g_tree_lookup_extended (tree, &c, (gpointer *)&d, (gpointer *)&p));
171 g_assert (c == *d && c == *p);
173 c = 'A';
174 p = g_tree_lookup (tree, &c);
175 g_assert (p && *p == c);
177 c = 'a';
178 p = g_tree_lookup (tree, &c);
179 g_assert (p && *p == c);
181 c = 'z';
182 p = g_tree_lookup (tree, &c);
183 g_assert (p && *p == c);
185 c = '!';
186 p = g_tree_lookup (tree, &c);
187 g_assert (p == NULL);
189 c = '=';
190 p = g_tree_lookup (tree, &c);
191 g_assert (p == NULL);
193 c = '|';
194 p = g_tree_lookup (tree, &c);
195 g_assert (p == NULL);
197 c = '0';
198 p = g_tree_search (tree, my_search, &c);
199 g_assert (p && *p == c);
201 c = 'A';
202 p = g_tree_search (tree, my_search, &c);
203 g_assert (p && *p == c);
205 c = 'a';
206 p = g_tree_search (tree, my_search, &c);
207 g_assert (p &&*p == c);
209 c = 'z';
210 p = g_tree_search (tree, my_search, &c);
211 g_assert (p && *p == c);
213 c = '!';
214 p = g_tree_search (tree, my_search, &c);
215 g_assert (p == NULL);
217 c = '=';
218 p = g_tree_search (tree, my_search, &c);
219 g_assert (p == NULL);
221 c = '|';
222 p = g_tree_search (tree, my_search, &c);
223 g_assert (p == NULL);
225 g_tree_destroy (tree);
228 static void
229 test_tree_remove (void)
231 GTree *tree;
232 char c, d;
233 gint i;
234 gboolean removed;
235 gchar *remove;
237 tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL,
238 my_key_destroy,
239 my_value_destroy);
241 for (i = 0; chars[i]; i++)
242 g_tree_insert (tree, &chars[i], &chars[i]);
244 c = '0';
245 g_tree_insert (tree, &c, &c);
246 g_assert (destroyed_key == &c);
247 g_assert (destroyed_value == &chars[0]);
248 destroyed_key = NULL;
249 destroyed_value = NULL;
251 d = '1';
252 g_tree_replace (tree, &d, &d);
253 g_assert (destroyed_key == &chars[1]);
254 g_assert (destroyed_value == &chars[1]);
255 destroyed_key = NULL;
256 destroyed_value = NULL;
258 c = '2';
259 removed = g_tree_remove (tree, &c);
260 g_assert (removed);
261 g_assert (destroyed_key == &chars[2]);
262 g_assert (destroyed_value == &chars[2]);
263 destroyed_key = NULL;
264 destroyed_value = NULL;
266 c = '3';
267 removed = g_tree_steal (tree, &c);
268 g_assert (removed);
269 g_assert (destroyed_key == NULL);
270 g_assert (destroyed_value == NULL);
272 remove = "omkjigfedba";
273 for (i = 0; remove[i]; i++)
275 removed = g_tree_remove (tree, &remove[i]);
276 g_assert (removed);
279 g_tree_destroy (tree);
282 static void
283 test_tree_destroy (void)
285 GTree *tree;
286 gint i;
288 tree = g_tree_new (my_compare);
290 for (i = 0; chars[i]; i++)
291 g_tree_insert (tree, &chars[i], &chars[i]);
293 g_assert_cmpint (g_tree_nnodes (tree), ==, strlen (chars));
295 g_tree_ref (tree);
296 g_tree_destroy (tree);
298 g_assert_cmpint (g_tree_nnodes (tree), ==, 0);
300 g_tree_unref (tree);
303 typedef struct {
304 GString *s;
305 gint count;
306 } CallbackData;
308 static gboolean
309 traverse_func (gpointer key, gpointer value, gpointer data)
311 CallbackData *d = data;
313 gchar *c = value;
314 g_string_append_c (d->s, *c);
316 d->count--;
318 if (d->count == 0)
319 return TRUE;
321 return FALSE;
324 typedef struct {
325 GTraverseType traverse;
326 gint limit;
327 const gchar *expected;
328 } TraverseData;
330 static void
331 test_tree_traverse (void)
333 GTree *tree;
334 gint i;
335 TraverseData orders[] = {
336 { G_IN_ORDER, -1, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" },
337 { G_IN_ORDER, 1, "0" },
338 { G_IN_ORDER, 2, "01" },
339 { G_IN_ORDER, 3, "012" },
340 { G_IN_ORDER, 4, "0123" },
341 { G_IN_ORDER, 5, "01234" },
342 { G_IN_ORDER, 6, "012345" },
343 { G_IN_ORDER, 7, "0123456" },
344 { G_IN_ORDER, 8, "01234567" },
345 { G_IN_ORDER, 9, "012345678" },
346 { G_IN_ORDER, 10, "0123456789" },
347 { G_IN_ORDER, 11, "0123456789A" },
348 { G_IN_ORDER, 12, "0123456789AB" },
349 { G_IN_ORDER, 13, "0123456789ABC" },
350 { G_IN_ORDER, 14, "0123456789ABCD" },
352 { G_PRE_ORDER, -1, "VF73102546B98ADCENJHGILKMRPOQTSUldZXWYbachfegjiktpnmorqsxvuwyz" },
353 { G_PRE_ORDER, 1, "V" },
354 { G_PRE_ORDER, 2, "VF" },
355 { G_PRE_ORDER, 3, "VF7" },
356 { G_PRE_ORDER, 4, "VF73" },
357 { G_PRE_ORDER, 5, "VF731" },
358 { G_PRE_ORDER, 6, "VF7310" },
359 { G_PRE_ORDER, 7, "VF73102" },
360 { G_PRE_ORDER, 8, "VF731025" },
361 { G_PRE_ORDER, 9, "VF7310254" },
362 { G_PRE_ORDER, 10, "VF73102546" },
363 { G_PRE_ORDER, 11, "VF73102546B" },
364 { G_PRE_ORDER, 12, "VF73102546B9" },
365 { G_PRE_ORDER, 13, "VF73102546B98" },
366 { G_PRE_ORDER, 14, "VF73102546B98A" },
368 { G_POST_ORDER, -1, "02146538A9CEDB7GIHKMLJOQPSUTRNFWYXacbZegfikjhdmonqsrpuwvzyxtlV" },
369 { G_POST_ORDER, 1, "0" },
370 { G_POST_ORDER, 2, "02" },
371 { G_POST_ORDER, 3, "021" },
372 { G_POST_ORDER, 4, "0214" },
373 { G_POST_ORDER, 5, "02146" },
374 { G_POST_ORDER, 6, "021465" },
375 { G_POST_ORDER, 7, "0214653" },
376 { G_POST_ORDER, 8, "02146538" },
377 { G_POST_ORDER, 9, "02146538A" },
378 { G_POST_ORDER, 10, "02146538A9" },
379 { G_POST_ORDER, 11, "02146538A9C" },
380 { G_POST_ORDER, 12, "02146538A9CE" },
381 { G_POST_ORDER, 13, "02146538A9CED" },
382 { G_POST_ORDER, 14, "02146538A9CEDB" }
384 CallbackData data;
386 tree = g_tree_new (my_compare);
388 for (i = 0; chars[i]; i++)
389 g_tree_insert (tree, &chars[i], &chars[i]);
391 data.s = g_string_new ("");
392 for (i = 0; i < G_N_ELEMENTS (orders); i++)
394 g_string_set_size (data.s, 0);
395 data.count = orders[i].limit;
396 g_tree_traverse (tree, traverse_func, orders[i].traverse, &data);
397 g_assert_cmpstr (data.s->str, ==, orders[i].expected);
400 g_tree_unref (tree);
401 g_string_free (data.s, TRUE);
404 static void
405 test_tree_insert (void)
407 GTree *tree;
408 gchar *p;
409 gint i;
410 gchar *scrambled;
412 tree = g_tree_new (my_compare);
414 for (i = 0; chars[i]; i++)
415 g_tree_insert (tree, &chars[i], &chars[i]);
416 p = chars;
417 g_tree_foreach (tree, check_order, &p);
419 g_tree_unref (tree);
420 tree = g_tree_new (my_compare);
422 for (i = strlen (chars) - 1; i >= 0; i--)
423 g_tree_insert (tree, &chars[i], &chars[i]);
424 p = chars;
425 g_tree_foreach (tree, check_order, &p);
427 g_tree_unref (tree);
428 tree = g_tree_new (my_compare);
430 scrambled = g_strdup (chars);
432 for (i = 0; i < 30; i++)
434 gchar tmp;
435 gint a, b;
437 a = g_random_int_range (0, strlen (scrambled));
438 b = g_random_int_range (0, strlen (scrambled));
439 tmp = scrambled[a];
440 scrambled[a] = scrambled[b];
441 scrambled[b] = tmp;
444 for (i = 0; scrambled[i]; i++)
445 g_tree_insert (tree, &scrambled[i], &scrambled[i]);
446 p = chars;
447 g_tree_foreach (tree, check_order, &p);
449 g_free (scrambled);
450 g_tree_unref (tree);
454 main (int argc, char *argv[])
456 g_test_init (&argc, &argv, NULL);
458 g_test_add_func ("/tree/search", test_tree_search);
459 g_test_add_func ("/tree/remove", test_tree_remove);
460 g_test_add_func ("/tree/destroy", test_tree_destroy);
461 g_test_add_func ("/tree/traverse", test_tree_traverse);
462 g_test_add_func ("/tree/insert", test_tree_insert);
464 return g_test_run ();