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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
44 gboolean failed
= FALSE
;
46 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
49 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
51 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
56 #define C2P(c) ((gpointer) ((long) (c)))
57 #define P2C(p) ((gchar) ((long) (p)))
59 #define GLIB_TEST_STRING "el dorado "
60 #define GLIB_TEST_STRING_5 "el do"
68 node_build_string (GNode
*node
,
75 c
[0] = P2C (node
->data
);
77 string
= g_strconcat (*p
? *p
: "", c
, NULL
);
98 root
= g_node_new (C2P ('A'));
99 TEST (NULL
, g_node_depth (root
) == 1 && g_node_max_height (root
) == 1);
101 node_B
= g_node_new (C2P ('B'));
102 g_node_append (root
, node_B
);
103 TEST (NULL
, root
->children
== node_B
);
105 g_node_append_data (node_B
, C2P ('E'));
106 g_node_prepend_data (node_B
, C2P ('C'));
107 g_node_insert (node_B
, 1, g_node_new (C2P ('D')));
109 node_F
= g_node_new (C2P ('F'));
110 g_node_append (root
, node_F
);
111 TEST (NULL
, root
->children
->next
== node_F
);
113 node_G
= g_node_new (C2P ('G'));
114 g_node_append (node_F
, node_G
);
115 node_J
= g_node_new (C2P ('J'));
116 g_node_prepend (node_G
, node_J
);
117 g_node_insert (node_G
, 42, g_node_new (C2P ('K')));
118 g_node_insert_data (node_G
, 0, C2P ('H'));
119 g_node_insert (node_G
, 1, g_node_new (C2P ('I')));
121 TEST (NULL
, g_node_depth (root
) == 1);
122 TEST (NULL
, g_node_max_height (root
) == 4);
123 TEST (NULL
, g_node_depth (node_G
->children
->next
) == 4);
124 TEST (NULL
, g_node_n_nodes (root
, G_TRAVERSE_LEAFS
) == 7);
125 TEST (NULL
, g_node_n_nodes (root
, G_TRAVERSE_NON_LEAFS
) == 4);
126 TEST (NULL
, g_node_n_nodes (root
, G_TRAVERSE_ALL
) == 11);
127 TEST (NULL
, g_node_max_height (node_F
) == 3);
128 TEST (NULL
, g_node_n_children (node_G
) == 4);
129 TEST (NULL
, g_node_find_child (root
, G_TRAVERSE_ALL
, C2P ('F')) == node_F
);
130 TEST (NULL
, g_node_find (root
, G_LEVEL_ORDER
, G_TRAVERSE_NON_LEAFS
, C2P ('I')) == NULL
);
131 TEST (NULL
, g_node_find (root
, G_IN_ORDER
, G_TRAVERSE_LEAFS
, C2P ('J')) == node_J
);
133 for (i
= 0; i
< g_node_n_children (node_B
); i
++)
135 node
= g_node_nth_child (node_B
, i
);
136 TEST (NULL
, P2C (node
->data
) == ('C' + i
));
139 for (i
= 0; i
< g_node_n_children (node_G
); i
++)
140 TEST (NULL
, g_node_child_position (node_G
, g_node_nth_child (node_G
, i
)) == i
);
150 * for in-order traversal, 'G' is considered to be the "left"
151 * child of 'F', which will cause 'F' to be the last node visited.
155 g_node_traverse (root
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
156 TEST (tstring
, strcmp (tstring
, "ABCDEFGHIJK") == 0);
157 g_free (tstring
); tstring
= NULL
;
158 g_node_traverse (root
, G_POST_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
159 TEST (tstring
, strcmp (tstring
, "CDEBHIJKGFA") == 0);
160 g_free (tstring
); tstring
= NULL
;
161 g_node_traverse (root
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
162 TEST (tstring
, strcmp (tstring
, "CBDEAHGIJKF") == 0);
163 g_free (tstring
); tstring
= NULL
;
164 g_node_traverse (root
, G_LEVEL_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
165 TEST (tstring
, strcmp (tstring
, "ABFCDEGHIJK") == 0);
166 g_free (tstring
); tstring
= NULL
;
168 g_node_traverse (root
, G_LEVEL_ORDER
, G_TRAVERSE_LEAFS
, -1, node_build_string
, &tstring
);
169 TEST (tstring
, strcmp (tstring
, "CDEHIJK") == 0);
170 g_free (tstring
); tstring
= NULL
;
171 g_node_traverse (root
, G_PRE_ORDER
, G_TRAVERSE_NON_LEAFS
, -1, node_build_string
, &tstring
);
172 TEST (tstring
, strcmp (tstring
, "ABFG") == 0);
173 g_free (tstring
); tstring
= NULL
;
175 g_node_reverse_children (node_B
);
176 g_node_reverse_children (node_G
);
178 g_node_traverse (root
, G_LEVEL_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
179 TEST (tstring
, strcmp (tstring
, "ABFEDCGKJIH") == 0);
180 g_free (tstring
); tstring
= NULL
;
182 g_node_destroy (root
);
184 /* allocation tests */
186 root
= g_node_new (NULL
);
189 for (i
= 0; i
< 2048; i
++)
191 g_node_append (node
, g_node_new (NULL
));
193 node
= node
->children
->next
;
195 TEST (NULL
, g_node_max_height (root
) > 100);
196 TEST (NULL
, g_node_n_nodes (root
, G_TRAVERSE_ALL
) == 1 + 2048);
198 g_node_destroy (root
);