1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/node-test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,238 @@
1.4 +/* GLIB - Library of useful routines for C programming
1.5 + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
1.6 + * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.7 + * This library is free software; you can redistribute it and/or
1.8 + * modify it under the terms of the GNU Lesser General Public
1.9 + * License as published by the Free Software Foundation; either
1.10 + * version 2 of the License, or (at your option) any later version.
1.11 + *
1.12 + * This library is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 + * Lesser General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU Lesser General Public
1.18 + * License along with this library; if not, write to the
1.19 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 + * Boston, MA 02111-1307, USA.
1.21 + */
1.22 +
1.23 +/*
1.24 + * Modified by the GLib Team and others 1997-2000. See the AUTHORS
1.25 + * file for a list of people on the GLib Team. See the ChangeLog
1.26 + * files for a list of changes. These files are distributed with
1.27 + * GLib at ftp://ftp.gtk.org/pub/gtk/.
1.28 + */
1.29 +
1.30 +#undef G_DISABLE_ASSERT
1.31 +#undef G_LOG_DOMAIN
1.32 +
1.33 +#ifdef HAVE_CONFIG_H
1.34 +#include <config.h>
1.35 +#endif
1.36 +
1.37 +#include <stdio.h>
1.38 +#include <string.h>
1.39 +#include <stdlib.h>
1.40 +
1.41 +#ifdef HAVE_UNISTD_H
1.42 +#include <unistd.h>
1.43 +#endif
1.44 +
1.45 +#include "glib.h"
1.46 +
1.47 +#ifdef SYMBIAN
1.48 +#include "mrt2_glib2_test.h"
1.49 +#endif /*SYMBIAN*/
1.50 +
1.51 +
1.52 +int array[10000];
1.53 +gboolean failed = FALSE;
1.54 +
1.55 +#define TEST(m,cond) G_STMT_START { failed = !(cond); \
1.56 +if (failed) \
1.57 + { assert_failed = TRUE; \
1.58 + if (!m) \
1.59 + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
1.60 + else \
1.61 + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
1.62 + exit(1); \
1.63 + } \
1.64 +} G_STMT_END
1.65 +
1.66 +#define C2P(c) ((gpointer) ((long) (c)))
1.67 +#define P2C(p) ((gchar) ((long) (p)))
1.68 +
1.69 +#define GLIB_TEST_STRING "el dorado "
1.70 +#define GLIB_TEST_STRING_5 "el do"
1.71 +
1.72 +typedef struct {
1.73 + guint age;
1.74 + gchar name[40];
1.75 +} GlibTestInfo;
1.76 +
1.77 +static gboolean
1.78 +node_build_string (GNode *node,
1.79 + gpointer data)
1.80 +{
1.81 + gchar **p = data;
1.82 + gchar *string;
1.83 + gchar c[2] = "_";
1.84 +
1.85 + c[0] = P2C (node->data);
1.86 +
1.87 + string = g_strconcat (*p ? *p : "", c, NULL);
1.88 + g_free (*p);
1.89 + *p = string;
1.90 +
1.91 + return FALSE;
1.92 +}
1.93 +
1.94 +static void
1.95 +g_node_test (void)
1.96 +{
1.97 + GNode *root;
1.98 + GNode *node;
1.99 + GNode *node_B;
1.100 + GNode *node_D;
1.101 + GNode *node_F;
1.102 + GNode *node_G;
1.103 + GNode *node_J;
1.104 + guint i;
1.105 + gchar *tstring;
1.106 +
1.107 + failed = FALSE;
1.108 +
1.109 + root = g_node_new (C2P ('A'));
1.110 + TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
1.111 +
1.112 + node_B = g_node_new (C2P ('B'));
1.113 + g_node_append (root, node_B);
1.114 + TEST (NULL, root->children == node_B);
1.115 +
1.116 + g_node_append_data (node_B, C2P ('E'));
1.117 + g_node_prepend_data (node_B, C2P ('C'));
1.118 + node_D = g_node_new (C2P ('D'));
1.119 + g_node_insert (node_B, 1, node_D);
1.120 +
1.121 + node_F = g_node_new (C2P ('F'));
1.122 + g_node_append (root, node_F);
1.123 + TEST (NULL, root->children->next == node_F);
1.124 +
1.125 + node_G = g_node_new (C2P ('G'));
1.126 + g_node_append (node_F, node_G);
1.127 + node_J = g_node_new (C2P ('J'));
1.128 + g_node_prepend (node_G, node_J);
1.129 + g_node_insert (node_G, 42, g_node_new (C2P ('K')));
1.130 + g_node_insert_data (node_G, 0, C2P ('H'));
1.131 + g_node_insert (node_G, 1, g_node_new (C2P ('I')));
1.132 +
1.133 + TEST (NULL, g_node_depth (root) == 1);
1.134 + TEST (NULL, g_node_max_height (root) == 4);
1.135 + TEST (NULL, g_node_depth (node_G->children->next) == 4);
1.136 + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
1.137 + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
1.138 + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
1.139 + TEST (NULL, g_node_max_height (node_F) == 3);
1.140 + TEST (NULL, g_node_n_children (node_G) == 4);
1.141 + TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
1.142 + TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
1.143 + TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
1.144 +
1.145 + for (i = 0; i < g_node_n_children (node_B); i++)
1.146 + {
1.147 + node = g_node_nth_child (node_B, i);
1.148 + TEST (NULL, P2C (node->data) == ('C' + i));
1.149 + }
1.150 +
1.151 + for (i = 0; i < g_node_n_children (node_G); i++)
1.152 + TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
1.153 +
1.154 + /* we have built: A
1.155 + * / \
1.156 + * B F
1.157 + * / | \ \
1.158 + * C D E G
1.159 + * / /\ \
1.160 + * H I J K
1.161 + *
1.162 + * for in-order traversal, 'G' is considered to be the "left"
1.163 + * child of 'F', which will cause 'F' to be the last node visited.
1.164 + */
1.165 +
1.166 + tstring = NULL;
1.167 + g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
1.168 + TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
1.169 + g_free (tstring); tstring = NULL;
1.170 + g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
1.171 + TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
1.172 + g_free (tstring); tstring = NULL;
1.173 + g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
1.174 + TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
1.175 + g_free (tstring); tstring = NULL;
1.176 + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
1.177 + TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
1.178 + g_free (tstring); tstring = NULL;
1.179 +
1.180 + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
1.181 + TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
1.182 + g_free (tstring); tstring = NULL;
1.183 + g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
1.184 + TEST (tstring, strcmp (tstring, "ABFG") == 0);
1.185 + g_free (tstring); tstring = NULL;
1.186 +
1.187 + g_node_reverse_children (node_B);
1.188 + g_node_reverse_children (node_G);
1.189 +
1.190 + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
1.191 + TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
1.192 + g_free (tstring); tstring = NULL;
1.193 +
1.194 + g_node_append (node_D, g_node_new (C2P ('L')));
1.195 + g_node_append (node_D, g_node_new (C2P ('M')));
1.196 +
1.197 + g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
1.198 + TEST (tstring, strcmp (tstring, "ABFEDCGLMKJIH") == 0);
1.199 + g_free (tstring); tstring = NULL;
1.200 +
1.201 + g_node_destroy (root);
1.202 +
1.203 + /* allocation tests */
1.204 +
1.205 + root = g_node_new (NULL);
1.206 + node = root;
1.207 +
1.208 + for (i = 0; i < 200; i++)
1.209 + {
1.210 + g_node_append (node, g_node_new (NULL));
1.211 + if ((i%5) == 4)
1.212 + node = node->children->next;
1.213 + }
1.214 + TEST (NULL, g_node_max_height (root) > 40);
1.215 + TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 200);
1.216 +
1.217 + g_node_destroy (root);
1.218 +
1.219 + if (failed)
1.220 + exit(1);
1.221 +}
1.222 +
1.223 +
1.224 +int
1.225 +main (int argc,
1.226 + char *argv[])
1.227 +{
1.228 + #ifdef SYMBIAN
1.229 + g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
1.230 + g_set_print_handler(mrtPrintHandler);
1.231 + #endif /*SYMBIAN*/
1.232 +
1.233 +
1.234 + g_node_test ();
1.235 +
1.236 +#ifdef SYMBIAN
1.237 + testResultXml("node-test");
1.238 +#endif /* EMULATOR */
1.239 + return 0;
1.240 +}
1.241 +