os/ossrv/glib/tests/node-test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tests/node-test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,237 @@
     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 __SYMBIAN32__
    1.48 +#include "mrt2_glib2_test.h"
    1.49 +#endif /*__SYMBIAN32__*/
    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 +  { if (!m) \
    1.58 +      g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
    1.59 +    else \
    1.60 +      g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
    1.61 +      exit(1); \
    1.62 +  } \
    1.63 +} G_STMT_END
    1.64 +
    1.65 +#define	C2P(c)		((gpointer) ((long) (c)))
    1.66 +#define	P2C(p)		((gchar) ((long) (p)))
    1.67 +
    1.68 +#define GLIB_TEST_STRING "el dorado "
    1.69 +#define GLIB_TEST_STRING_5 "el do"
    1.70 +
    1.71 +typedef struct {
    1.72 +	guint age;
    1.73 +	gchar name[40];
    1.74 +} GlibTestInfo;
    1.75 +
    1.76 +static gboolean
    1.77 +node_build_string (GNode    *node,
    1.78 +		   gpointer  data)
    1.79 +{
    1.80 +  gchar **p = data;
    1.81 +  gchar *string;
    1.82 +  gchar c[2] = "_";
    1.83 +
    1.84 +  c[0] = P2C (node->data);
    1.85 +
    1.86 +  string = g_strconcat (*p ? *p : "", c, NULL);
    1.87 +  g_free (*p);
    1.88 +  *p = string;
    1.89 +
    1.90 +  return FALSE;
    1.91 +}
    1.92 +
    1.93 +static void
    1.94 +g_node_test (void)
    1.95 +{
    1.96 +  GNode *root;
    1.97 +  GNode *node;
    1.98 +  GNode *node_B;
    1.99 +  GNode *node_D;
   1.100 +  GNode *node_F;
   1.101 +  GNode *node_G;
   1.102 +  GNode *node_J;
   1.103 +  guint i;
   1.104 +  gchar *tstring;
   1.105 +
   1.106 +  failed = FALSE;
   1.107 +
   1.108 +  root = g_node_new (C2P ('A'));
   1.109 +  TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
   1.110 +
   1.111 +  node_B = g_node_new (C2P ('B'));
   1.112 +  g_node_append (root, node_B);
   1.113 +  TEST (NULL, root->children == node_B);
   1.114 +
   1.115 +  g_node_append_data (node_B, C2P ('E'));
   1.116 +  g_node_prepend_data (node_B, C2P ('C'));
   1.117 +  node_D = g_node_new (C2P ('D'));
   1.118 +  g_node_insert (node_B, 1, node_D); 
   1.119 +
   1.120 +  node_F = g_node_new (C2P ('F'));
   1.121 +  g_node_append (root, node_F);
   1.122 +  TEST (NULL, root->children->next == node_F);
   1.123 +
   1.124 +  node_G = g_node_new (C2P ('G'));
   1.125 +  g_node_append (node_F, node_G);
   1.126 +  node_J = g_node_new (C2P ('J'));
   1.127 +  g_node_prepend (node_G, node_J);
   1.128 +  g_node_insert (node_G, 42, g_node_new (C2P ('K')));
   1.129 +  g_node_insert_data (node_G, 0, C2P ('H'));
   1.130 +  g_node_insert (node_G, 1, g_node_new (C2P ('I')));
   1.131 +
   1.132 +  TEST (NULL, g_node_depth (root) == 1);
   1.133 +  TEST (NULL, g_node_max_height (root) == 4);
   1.134 +  TEST (NULL, g_node_depth (node_G->children->next) == 4);
   1.135 +  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
   1.136 +  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
   1.137 +  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
   1.138 +  TEST (NULL, g_node_max_height (node_F) == 3);
   1.139 +  TEST (NULL, g_node_n_children (node_G) == 4);
   1.140 +  TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
   1.141 +  TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
   1.142 +  TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
   1.143 +
   1.144 +  for (i = 0; i < g_node_n_children (node_B); i++)
   1.145 +    {
   1.146 +      node = g_node_nth_child (node_B, i);
   1.147 +      TEST (NULL, P2C (node->data) == ('C' + i));
   1.148 +    }
   1.149 +  
   1.150 +  for (i = 0; i < g_node_n_children (node_G); i++)
   1.151 +    TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
   1.152 +
   1.153 +  /* we have built:                    A
   1.154 +   *                                 /   \
   1.155 +   *                               B       F
   1.156 +   *                             / | \       \
   1.157 +   *                           C   D   E       G
   1.158 +   *                                         / /\ \
   1.159 +   *                                       H  I  J  K
   1.160 +   *
   1.161 +   * for in-order traversal, 'G' is considered to be the "left"
   1.162 +   * child of 'F', which will cause 'F' to be the last node visited.
   1.163 +   */
   1.164 +
   1.165 +  tstring = NULL;
   1.166 +  g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
   1.167 +  TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
   1.168 +  g_free (tstring); tstring = NULL;
   1.169 +  g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
   1.170 +  TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
   1.171 +  g_free (tstring); tstring = NULL;
   1.172 +  g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
   1.173 +  TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
   1.174 +  g_free (tstring); tstring = NULL;
   1.175 +  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
   1.176 +  TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
   1.177 +  g_free (tstring); tstring = NULL;
   1.178 +  
   1.179 +  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
   1.180 +  TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
   1.181 +  g_free (tstring); tstring = NULL;
   1.182 +  g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
   1.183 +  TEST (tstring, strcmp (tstring, "ABFG") == 0);
   1.184 +  g_free (tstring); tstring = NULL;
   1.185 +
   1.186 +  g_node_reverse_children (node_B);
   1.187 +  g_node_reverse_children (node_G);
   1.188 +
   1.189 +  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
   1.190 +  TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
   1.191 +  g_free (tstring); tstring = NULL;
   1.192 +  
   1.193 +  g_node_append (node_D, g_node_new (C2P ('L')));
   1.194 +  g_node_append (node_D, g_node_new (C2P ('M')));
   1.195 +
   1.196 +  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
   1.197 +  TEST (tstring, strcmp (tstring, "ABFEDCGLMKJIH") == 0);
   1.198 +  g_free (tstring); tstring = NULL;
   1.199 +
   1.200 +  g_node_destroy (root);
   1.201 +
   1.202 +  /* allocation tests */
   1.203 +
   1.204 +  root = g_node_new (NULL);
   1.205 +  node = root;
   1.206 +
   1.207 +  for (i = 0; i < 2048; i++)
   1.208 +    {
   1.209 +      g_node_append (node, g_node_new (NULL));
   1.210 +      if ((i%5) == 4)
   1.211 +	node = node->children->next;
   1.212 +    }
   1.213 +  TEST (NULL, g_node_max_height (root) > 100);
   1.214 +  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
   1.215 +
   1.216 +  g_node_destroy (root);
   1.217 +  
   1.218 +  if (failed)
   1.219 +    exit(1);
   1.220 +}
   1.221 +
   1.222 +
   1.223 +int
   1.224 +main (int   argc,
   1.225 +      char *argv[])
   1.226 +{
   1.227 +  #ifdef __SYMBIAN32__
   1.228 +  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.229 +  g_set_print_handler(mrtPrintHandler);
   1.230 +  #endif /*__SYMBIAN32__*/
   1.231 +	  
   1.232 +
   1.233 +  g_node_test ();
   1.234 +
   1.235 +#ifdef __SYMBIAN32__
   1.236 +  testResultXml("node-test");
   1.237 +#endif /* EMULATOR */
   1.238 +  return 0;
   1.239 +}
   1.240 +