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