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.1 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
28 /* We are testing some deprecated APIs here */
29 #define GLIB_DISABLE_DEPRECATION_WARNINGS
37 my_compare (gconstpointer a
,
47 my_compare_with_data (gconstpointer a
,
54 /* just check that we got the right data */
55 g_assert (GPOINTER_TO_INT(user_data
) == 123);
61 my_search (gconstpointer a
,
64 return my_compare (b
, a
);
67 static gpointer destroyed_key
= NULL
;
68 static gpointer destroyed_value
= NULL
;
71 my_key_destroy (gpointer key
)
77 my_value_destroy (gpointer value
)
79 destroyed_value
= value
;
83 my_traverse (gpointer key
,
99 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
100 "abcdefghijklmnopqrstuvwxyz";
104 "abcdefghijklmnopqrstuvwxyz";
107 check_order (gpointer key
,
114 g_assert (**p
== *ch
);
122 test_tree_search (void)
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);
141 g_tree_foreach (tree
, check_order
, &p
);
143 for (i
= 0; i
< 26; i
++)
145 removed
= g_tree_remove (tree
, &chars
[i
+ 10]);
150 removed
= g_tree_remove (tree
, &c
);
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);
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]);
165 g_tree_foreach (tree
, check_order
, &p
);
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
);
174 p
= g_tree_lookup (tree
, &c
);
175 g_assert (p
&& *p
== c
);
178 p
= g_tree_lookup (tree
, &c
);
179 g_assert (p
&& *p
== c
);
182 p
= g_tree_lookup (tree
, &c
);
183 g_assert (p
&& *p
== c
);
186 p
= g_tree_lookup (tree
, &c
);
187 g_assert (p
== NULL
);
190 p
= g_tree_lookup (tree
, &c
);
191 g_assert (p
== NULL
);
194 p
= g_tree_lookup (tree
, &c
);
195 g_assert (p
== NULL
);
198 p
= g_tree_search (tree
, my_search
, &c
);
199 g_assert (p
&& *p
== c
);
202 p
= g_tree_search (tree
, my_search
, &c
);
203 g_assert (p
&& *p
== c
);
206 p
= g_tree_search (tree
, my_search
, &c
);
207 g_assert (p
&&*p
== c
);
210 p
= g_tree_search (tree
, my_search
, &c
);
211 g_assert (p
&& *p
== c
);
214 p
= g_tree_search (tree
, my_search
, &c
);
215 g_assert (p
== NULL
);
218 p
= g_tree_search (tree
, my_search
, &c
);
219 g_assert (p
== NULL
);
222 p
= g_tree_search (tree
, my_search
, &c
);
223 g_assert (p
== NULL
);
225 g_tree_destroy (tree
);
229 test_tree_remove (void)
237 tree
= g_tree_new_full ((GCompareDataFunc
)my_compare
, NULL
,
241 for (i
= 0; chars
[i
]; i
++)
242 g_tree_insert (tree
, &chars
[i
], &chars
[i
]);
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
;
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
;
259 removed
= g_tree_remove (tree
, &c
);
261 g_assert (destroyed_key
== &chars
[2]);
262 g_assert (destroyed_value
== &chars
[2]);
263 destroyed_key
= NULL
;
264 destroyed_value
= NULL
;
267 removed
= g_tree_steal (tree
, &c
);
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
]);
279 g_tree_destroy (tree
);
283 test_tree_destroy (void)
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
));
296 g_tree_destroy (tree
);
298 g_assert_cmpint (g_tree_nnodes (tree
), ==, 0);
309 traverse_func (gpointer key
, gpointer value
, gpointer data
)
311 CallbackData
*d
= data
;
314 g_string_append_c (d
->s
, *c
);
325 GTraverseType traverse
;
327 const gchar
*expected
;
331 test_tree_traverse (void)
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" }
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
);
401 g_string_free (data
.s
, TRUE
);
405 test_tree_insert (void)
412 tree
= g_tree_new (my_compare
);
414 for (i
= 0; chars
[i
]; i
++)
415 g_tree_insert (tree
, &chars
[i
], &chars
[i
]);
417 g_tree_foreach (tree
, check_order
, &p
);
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
]);
425 g_tree_foreach (tree
, check_order
, &p
);
428 tree
= g_tree_new (my_compare
);
430 scrambled
= g_strdup (chars
);
432 for (i
= 0; i
< 30; i
++)
437 a
= g_random_int_range (0, strlen (scrambled
));
438 b
= g_random_int_range (0, strlen (scrambled
));
440 scrambled
[a
] = scrambled
[b
];
444 for (i
= 0; scrambled
[i
]; i
++)
445 g_tree_insert (tree
, &scrambled
[i
], &scrambled
[i
]);
447 g_tree_foreach (tree
, check_order
, &p
);
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 ();