Moving files to packaging and extracing new tarball.
[profile/ivi/glib2.git] / glib / tests / node.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #include "glib.h"
43
44 #define C2P(c)          ((gpointer) ((long) (c)))
45 #define P2C(p)          ((gchar) ((long) (p)))
46
47 static gboolean
48 node_build_string (GNode    *node,
49                    gpointer  data)
50 {
51   gchar **p = data;
52   gchar *string;
53   gchar c[2] = "_";
54
55   c[0] = P2C (node->data);
56
57   string = g_strconcat (*p ? *p : "", c, NULL);
58   g_free (*p);
59   *p = string;
60
61   return FALSE;
62 }
63
64 static void
65 traversal_test (void)
66 {
67   GNode *root;
68   GNode *node_B;
69   GNode *node_C;
70   GNode *node_D;
71   GNode *node_E;
72   GNode *node_F;
73   GNode *node_G;
74   GNode *node_J;
75   GNode *n;
76   gchar *tstring;
77
78   root = g_node_new (C2P ('A'));
79   node_B = g_node_new (C2P ('B'));
80   g_node_append (root, node_B);
81   g_node_append_data (node_B, C2P ('E'));
82   g_node_prepend_data (node_B, C2P ('C'));
83   node_D = g_node_new (C2P ('D'));
84   g_node_insert (node_B, 1, node_D);
85   node_F = g_node_new (C2P ('F'));
86   g_node_append (root, node_F);
87   node_G = g_node_new (C2P ('G'));
88   g_node_append (node_F, node_G);
89   node_J = g_node_new (C2P ('J'));
90   g_node_prepend (node_G, node_J);
91   g_node_insert (node_G, 42, g_node_new (C2P ('K')));
92   g_node_insert_data (node_G, 0, C2P ('H'));
93   g_node_insert (node_G, 1, g_node_new (C2P ('I')));
94
95   /* we have built:                    A
96    *                                 /   \
97    *                               B       F
98    *                             / | \       \
99    *                           C   D   E       G
100    *                                         / /\ \
101    *                                       H  I  J  K
102    *
103    * for in-order traversal, 'G' is considered to be the "left"
104    * child of 'F', which will cause 'F' to be the last node visited.
105    */
106
107   node_C = node_B->children;
108   node_E = node_D->next;
109
110   n = g_node_last_sibling (node_C);
111   g_assert (n == node_E);
112   n = g_node_last_sibling (node_D);
113   g_assert (n == node_E);
114   n = g_node_last_sibling (node_E);
115   g_assert (n == node_E);
116
117   tstring = NULL;
118   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
119   g_assert_cmpstr (tstring, ==,  "ABCDEFGHIJK");
120   g_free (tstring); tstring = NULL;
121   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
122   g_assert_cmpstr (tstring, ==,  "ABF");
123   g_free (tstring); tstring = NULL;
124   g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
125   g_assert_cmpstr (tstring, ==, "CDEBHIJKGFA");
126   g_free (tstring); tstring = NULL;
127   g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
128   g_assert_cmpstr (tstring, ==, "BFA");
129   g_free (tstring); tstring = NULL;
130   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
131   g_assert_cmpstr (tstring, ==, "CBDEAHGIJKF");
132   g_free (tstring); tstring = NULL;
133   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
134   g_assert_cmpstr (tstring, ==, "BAF");
135   g_free (tstring); tstring = NULL;
136   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
137   g_assert_cmpstr (tstring, ==, "ABFCDEGHIJK");
138   g_free (tstring); tstring = NULL;
139   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, 2, node_build_string, &tstring);
140   g_assert_cmpstr (tstring, ==, "ABF");
141   g_free (tstring); tstring = NULL;
142   
143   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
144   g_assert_cmpstr (tstring, ==, "CDEHIJK");
145   g_free (tstring); tstring = NULL;
146   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
147   g_assert_cmpstr (tstring, ==, "ABFG");
148   g_free (tstring); tstring = NULL;
149
150   g_node_reverse_children (node_B);
151   g_node_reverse_children (node_G);
152
153   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
154   g_assert_cmpstr (tstring, ==, "ABFEDCGKJIH");
155   g_free (tstring); tstring = NULL;
156   
157   g_node_append (node_D, g_node_new (C2P ('L')));
158   g_node_append (node_D, g_node_new (C2P ('M')));
159
160   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
161   g_assert_cmpstr (tstring, ==, "ABFEDCGLMKJIH");
162   g_free (tstring); tstring = NULL;
163
164   g_node_destroy (root);
165 }
166
167 static void
168 construct_test (void)
169 {
170   GNode *root;
171   GNode *node;
172   GNode *node_B;
173   GNode *node_D;
174   GNode *node_F;
175   GNode *node_G;
176   GNode *node_J;
177   guint i;
178
179   root = g_node_new (C2P ('A'));
180   g_assert_cmpint (g_node_depth (root), ==, 1);
181   g_assert_cmpint (g_node_max_height (root), ==, 1);
182
183   node_B = g_node_new (C2P ('B'));
184   g_node_append (root, node_B);
185   g_assert (root->children == node_B);
186
187   g_node_append_data (node_B, C2P ('E'));
188   g_node_prepend_data (node_B, C2P ('C'));
189   node_D = g_node_new (C2P ('D'));
190   g_node_insert (node_B, 1, node_D);
191
192   node_F = g_node_new (C2P ('F'));
193   g_node_append (root, node_F);
194   g_assert (root->children->next == node_F);
195
196   node_G = g_node_new (C2P ('G'));
197   g_node_append (node_F, node_G);
198   node_J = g_node_new (C2P ('J'));
199   g_node_prepend (node_G, node_J);
200   g_node_insert (node_G, 42, g_node_new (C2P ('K')));
201   g_node_insert_data (node_G, 0, C2P ('H'));
202   g_node_insert (node_G, 1, g_node_new (C2P ('I')));
203
204   /* we have built:                    A
205    *                                 /   \
206    *                               B       F
207    *                             / | \       \
208    *                           C   D   E       G
209    *                                         / /\ \
210    *                                       H  I  J  K
211    */
212   g_assert_cmpint (g_node_depth (root), ==, 1);
213   g_assert_cmpint (g_node_max_height (root), ==, 4);
214   g_assert_cmpint (g_node_depth (node_G->children->next), ==, 4);
215   g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_LEAFS), ==, 7);
216   g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS), ==, 4);
217   g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 11);
218   g_assert_cmpint (g_node_max_height (node_F), ==, 3);
219   g_assert_cmpint (g_node_n_children (node_G), ==, 4);
220   g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
221   g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
222   g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
223
224   for (i = 0; i < g_node_n_children (node_B); i++)
225     {
226       node = g_node_nth_child (node_B, i);
227       g_assert_cmpint (P2C (node->data), ==, ('C' + i));
228     }
229
230   for (i = 0; i < g_node_n_children (node_G); i++)
231     g_assert_cmpint (g_node_child_position (node_G, g_node_nth_child (node_G, i)), ==, i);
232
233   g_node_destroy (root);
234 }
235
236 static void
237 allocation_test (void)
238 {
239   GNode *root;
240   GNode *node;
241   gint i;
242
243   root = g_node_new (NULL);
244   node = root;
245
246   for (i = 0; i < 2048; i++)
247     {
248       g_node_append (node, g_node_new (NULL));
249       if ((i % 5) == 4)
250         node = node->children->next;
251     }
252   g_assert_cmpint (g_node_max_height (root), >, 100);
253   g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 1 + 2048);
254
255   g_node_destroy (root);
256 }
257
258
259 static void
260 misc_test (void)
261 {
262   GNode *root;
263   GNode *node_B;
264   GNode *node_C;
265   GNode *node_D;
266   GNode *node_E;
267   gchar *tstring;
268
269   root = g_node_new (C2P ('A'));
270   node_B = g_node_new (C2P ('B'));
271   g_node_append (root, node_B);
272   node_D = g_node_new (C2P ('D'));
273   g_node_append (root, node_D);
274   node_C = g_node_new (C2P ('C'));
275   g_node_insert_after (root, node_B, node_C);
276   node_E = g_node_new (C2P ('E'));
277   g_node_append (node_C, node_E);
278
279   g_assert (g_node_get_root (node_E) == root);
280   g_assert (g_node_is_ancestor (root, node_B));
281   g_assert (g_node_is_ancestor (root, node_E));
282   g_assert (!g_node_is_ancestor (node_B, node_D));
283   g_assert (g_node_first_sibling (node_D) == node_B);
284   g_assert (g_node_first_sibling (node_E) == node_E);
285   g_assert_cmpint (g_node_child_index (root, C2P ('B')), ==, 0);
286   g_assert_cmpint (g_node_child_index (root, C2P ('C')), ==, 1);
287   g_assert_cmpint (g_node_child_index (root, C2P ('D')), ==, 2);
288   g_assert_cmpint (g_node_child_index (root, C2P ('E')), ==, -1);
289
290   tstring = NULL;
291   g_node_children_foreach (root, G_TRAVERSE_ALL, (GNodeForeachFunc)node_build_string, &tstring);
292   g_assert_cmpstr (tstring, ==, "BCD");
293   g_free (tstring); tstring = NULL;
294
295   g_node_children_foreach (root, G_TRAVERSE_LEAVES, (GNodeForeachFunc)node_build_string, &tstring);
296   g_assert_cmpstr (tstring, ==, "BD");
297   g_free (tstring); tstring = NULL;
298
299   g_node_children_foreach (root, G_TRAVERSE_NON_LEAVES, (GNodeForeachFunc)node_build_string, &tstring);
300   g_assert_cmpstr (tstring, ==, "C");
301   g_free (tstring); tstring = NULL;
302
303   g_node_destroy (root);
304 }
305
306 static gboolean
307 check_order (GNode    *node,
308              gpointer  data)
309 {
310   gchar **expected = data;
311   gchar d;
312
313   d = GPOINTER_TO_INT (node->data);
314   g_assert_cmpint (d, ==, **expected);
315   (*expected)++;
316
317   return FALSE;
318 }
319
320 static void
321 unlink_test (void)
322 {
323   GNode *root;
324   GNode *cnode;
325   GNode *node;
326   gchar *expected;
327
328   /*
329    *        -------- a --------
330    *       /         |          \
331    *     b           c           d
332    *   / | \       / | \       / | \
333    * e   f   g   h   i   j   k   l   m
334    *
335    */
336
337   root = g_node_new (C2P ('a'));
338   node = g_node_append_data (root, C2P ('b'));
339   g_node_append_data (node, C2P ('e'));
340   g_node_append_data (node, C2P ('f'));
341   g_node_append_data (node, C2P ('g'));
342
343   node = cnode = g_node_append_data (root, C2P ('c'));
344   g_node_append_data (node, C2P ('h'));
345   g_node_append_data (node, C2P ('i'));
346   g_node_append_data (node, C2P ('j'));
347
348   node = g_node_append_data (root, C2P ('d'));
349   g_node_append_data (node, C2P ('k'));
350   g_node_append_data (node, C2P ('l'));
351   g_node_append_data (node, C2P ('m'));
352
353   g_node_unlink (cnode);
354
355   expected = "abdefgklm";
356   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
357
358   expected = "chij";
359   g_node_traverse (cnode, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
360
361   g_node_destroy (root);
362   g_node_destroy (cnode);
363 }
364
365 static gpointer
366 copy_up (gconstpointer src,
367          gpointer      data)
368 {
369   gchar l, u;
370
371   l = GPOINTER_TO_INT (src);
372   u = g_ascii_toupper (l);
373
374   return GINT_TO_POINTER ((int)u);
375 }
376
377 static void
378 copy_test (void)
379 {
380   GNode *root;
381   GNode *copy;
382   gchar *expected;
383
384   root = g_node_new (C2P ('a'));
385   g_node_append_data (root, C2P ('b'));
386   g_node_append_data (root, C2P ('c'));
387   g_node_append_data (root, C2P ('d'));
388
389   expected = "abcd";
390   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
391
392   copy = g_node_copy (root);
393
394   expected = "abcd";
395   g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
396
397   g_node_destroy (copy);
398
399   copy = g_node_copy_deep (root, copy_up, NULL);
400
401   expected = "ABCD";
402   g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
403
404   g_node_destroy (copy);
405
406   g_node_destroy (root);
407 }
408
409 int
410 main (int   argc,
411       char *argv[])
412 {
413   g_test_init (&argc, &argv, NULL);
414
415   g_test_add_func ("/node/allocation", allocation_test);
416   g_test_add_func ("/node/construction", construct_test);
417   g_test_add_func ("/node/traversal", traversal_test);
418   g_test_add_func ("/node/misc", misc_test);
419   g_test_add_func ("/node/unlink", unlink_test);
420   g_test_add_func ("/node/copy", copy_test);
421
422   return g_test_run ();
423 }
424