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 int array[10000]; 45 gboolean failed = FALSE; 46 47 #define TEST(m,cond) G_STMT_START { failed = !(cond); \ 48 if (failed) \ 49 { if (!m) \ 50 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ 51 else \ 52 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ 53 exit(1); \ 54 } \ 55 } G_STMT_END 56 57 #define C2P(c) ((gpointer) ((long) (c))) 58 #define P2C(p) ((gchar) ((long) (p))) 59 60 #define GLIB_TEST_STRING "el dorado " 61 #define GLIB_TEST_STRING_5 "el do" 62 63 typedef struct { 64 guint age; 65 gchar name[40]; 66 } GlibTestInfo; 67 68 static gboolean 69 node_build_string (GNode *node, 70 gpointer data) 71 { 72 gchar **p = data; 73 gchar *string; 74 gchar c[2] = "_"; 75 76 c[0] = P2C (node->data); 77 78 string = g_strconcat (*p ? *p : "", c, NULL); 79 g_free (*p); 80 *p = string; 81 82 return FALSE; 83 } 84 85 static void 86 g_node_test (void) 87 { 88 GNode *root; 89 GNode *node; 90 GNode *node_B; 91 GNode *node_D; 92 GNode *node_F; 93 GNode *node_G; 94 GNode *node_J; 95 guint i; 96 gchar *tstring; 97 98 failed = FALSE; 99 100 root = g_node_new (C2P ('A')); 101 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1); 102 103 node_B = g_node_new (C2P ('B')); 104 g_node_append (root, node_B); 105 TEST (NULL, root->children == node_B); 106 107 g_node_append_data (node_B, C2P ('E')); 108 g_node_prepend_data (node_B, C2P ('C')); 109 node_D = g_node_new (C2P ('D')); 110 g_node_insert (node_B, 1, node_D); 111 112 node_F = g_node_new (C2P ('F')); 113 g_node_append (root, node_F); 114 TEST (NULL, root->children->next == node_F); 115 116 node_G = g_node_new (C2P ('G')); 117 g_node_append (node_F, node_G); 118 node_J = g_node_new (C2P ('J')); 119 g_node_prepend (node_G, node_J); 120 g_node_insert (node_G, 42, g_node_new (C2P ('K'))); 121 g_node_insert_data (node_G, 0, C2P ('H')); 122 g_node_insert (node_G, 1, g_node_new (C2P ('I'))); 123 124 TEST (NULL, g_node_depth (root) == 1); 125 TEST (NULL, g_node_max_height (root) == 4); 126 TEST (NULL, g_node_depth (node_G->children->next) == 4); 127 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7); 128 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4); 129 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11); 130 TEST (NULL, g_node_max_height (node_F) == 3); 131 TEST (NULL, g_node_n_children (node_G) == 4); 132 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F); 133 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL); 134 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J); 135 136 for (i = 0; i < g_node_n_children (node_B); i++) 137 { 138 node = g_node_nth_child (node_B, i); 139 TEST (NULL, P2C (node->data) == ('C' + i)); 140 } 141 142 for (i = 0; i < g_node_n_children (node_G); i++) 143 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i); 144 145 /* we have built: A 146 * / \ 147 * B F 148 * / | \ \ 149 * C D E G 150 * / /\ \ 151 * H I J K 152 * 153 * for in-order traversal, 'G' is considered to be the "left" 154 * child of 'F', which will cause 'F' to be the last node visited. 155 */ 156 157 tstring = NULL; 158 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); 159 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0); 160 g_free (tstring); tstring = NULL; 161 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); 162 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0); 163 g_free (tstring); tstring = NULL; 164 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); 165 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0); 166 g_free (tstring); tstring = NULL; 167 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); 168 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0); 169 g_free (tstring); tstring = NULL; 170 171 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring); 172 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0); 173 g_free (tstring); tstring = NULL; 174 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring); 175 TEST (tstring, strcmp (tstring, "ABFG") == 0); 176 g_free (tstring); tstring = NULL; 177 178 g_node_reverse_children (node_B); 179 g_node_reverse_children (node_G); 180 181 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); 182 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0); 183 g_free (tstring); tstring = NULL; 184 185 g_node_append (node_D, g_node_new (C2P ('L'))); 186 g_node_append (node_D, g_node_new (C2P ('M'))); 187 188 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); 189 TEST (tstring, strcmp (tstring, "ABFEDCGLMKJIH") == 0); 190 g_free (tstring); tstring = NULL; 191 192 g_node_destroy (root); 193 194 /* allocation tests */ 195 196 root = g_node_new (NULL); 197 node = root; 198 199 for (i = 0; i < 2048; i++) 200 { 201 g_node_append (node, g_node_new (NULL)); 202 if ((i%5) == 4) 203 node = node->children->next; 204 } 205 TEST (NULL, g_node_max_height (root) > 100); 206 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048); 207 208 g_node_destroy (root); 209 210 if (failed) 211 exit(1); 212 } 213 214 215 int 216 main (int argc, 217 char *argv[]) 218 { 219 g_node_test (); 220 221 return 0; 222 } 223 224