os/ossrv/glib/tsrc/BC/tests/tree-test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/tree-test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,254 @@
     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 +#include <stdio.h>
    1.34 +#include <string.h>
    1.35 +#include "glib.h"
    1.36 +
    1.37 +#ifdef SYMBIAN
    1.38 +#include "mrt2_glib2_test.h"
    1.39 +#endif /*SYMBIAN*/
    1.40 +
    1.41 +static gint
    1.42 +my_compare (gconstpointer a,
    1.43 +	    gconstpointer b)
    1.44 +{
    1.45 +  const char *cha = a;
    1.46 +  const char *chb = b;
    1.47 +
    1.48 +  return *cha - *chb;
    1.49 +}
    1.50 +
    1.51 +static gint
    1.52 +my_search (gconstpointer a,
    1.53 +	   gconstpointer b)
    1.54 +{
    1.55 +  return my_compare (b, a);
    1.56 +}
    1.57 +
    1.58 +static gpointer destroyed_key = NULL;
    1.59 +static gpointer destroyed_value = NULL;
    1.60 +
    1.61 +static void 
    1.62 +my_key_destroy (gpointer key)
    1.63 +{
    1.64 +  destroyed_key = key;
    1.65 +}
    1.66 +
    1.67 +static void 
    1.68 +my_value_destroy (gpointer value)
    1.69 +{
    1.70 +  destroyed_value = value;
    1.71 +}
    1.72 +
    1.73 +static gint
    1.74 +my_traverse (gpointer key,
    1.75 +	     gpointer value,
    1.76 +	     gpointer data)
    1.77 +{
    1.78 +  char *ch = key;
    1.79 +  g_assert ((*ch) > 0);
    1.80 +  return FALSE;
    1.81 +}
    1.82 +
    1.83 +char chars[] = 
    1.84 +  "0123456789"
    1.85 +  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    1.86 +  "abcdefghijklmnopqrstuvwxyz";
    1.87 +
    1.88 +char chars2[] = 
    1.89 +  "0123456789"
    1.90 +  "abcdefghijklmnopqrstuvwxyz";
    1.91 +
    1.92 +static gint
    1.93 +check_order (gpointer key,
    1.94 +	     gpointer value,
    1.95 +	     gpointer data)
    1.96 +{
    1.97 +  char **p = data;
    1.98 +  char *ch = key;
    1.99 +  
   1.100 +  g_assert (**p == *ch);
   1.101 +
   1.102 +  (*p)++;
   1.103 +
   1.104 +  return FALSE;
   1.105 +}
   1.106 +
   1.107 +
   1.108 +
   1.109 +int
   1.110 +main (int   argc,
   1.111 +      char *argv[])
   1.112 +{
   1.113 +  gint i;
   1.114 +  GTree *tree;
   1.115 +  gboolean removed;
   1.116 +  char c, d;
   1.117 +  char *p;
   1.118 +  #ifdef SYMBIAN
   1.119 +  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.120 +  g_set_print_handler(mrtPrintHandler);
   1.121 +  #endif /*SYMBIAN*/
   1.122 +
   1.123 +  tree = g_tree_new (my_compare);
   1.124 +
   1.125 +  for (i = 0; chars[i]; i++)
   1.126 +    g_tree_insert (tree, &chars[i], &chars[i]);
   1.127 +
   1.128 +  g_tree_foreach (tree, my_traverse, NULL);
   1.129 +
   1.130 +  g_assert (g_tree_nnodes (tree) == strlen (chars));
   1.131 +  g_assert (g_tree_height (tree) == 6);
   1.132 +  
   1.133 +  p = chars;
   1.134 +  g_tree_foreach (tree, check_order, &p);
   1.135 +
   1.136 +  for (i = 0; i < 26; i++)
   1.137 +    {
   1.138 +      removed = g_tree_remove (tree, &chars[i + 10]);
   1.139 +      g_assert (removed);
   1.140 +    }
   1.141 +
   1.142 +  c = '\0';
   1.143 +  removed = g_tree_remove (tree, &c);
   1.144 +  g_assert (removed == FALSE);
   1.145 +
   1.146 +  g_tree_foreach (tree, my_traverse, NULL);
   1.147 +
   1.148 +  g_assert (g_tree_nnodes (tree) == strlen (chars2));
   1.149 +  g_assert (g_tree_height (tree) == 6);
   1.150 +
   1.151 +  p = chars2;
   1.152 +  g_tree_foreach (tree, check_order, &p);
   1.153 +
   1.154 +  for (i = 25; i >= 0; i--)
   1.155 +    g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
   1.156 +
   1.157 +  p = chars;
   1.158 +  g_tree_foreach (tree, check_order, &p);
   1.159 +
   1.160 +  c = '0';
   1.161 +  p = g_tree_lookup (tree, &c); 
   1.162 +  g_assert (p && *p == c);
   1.163 +
   1.164 +  c = 'A';
   1.165 +  p = g_tree_lookup (tree, &c);
   1.166 +  g_assert (p && *p == c);
   1.167 +
   1.168 +  c = 'a';
   1.169 +  p = g_tree_lookup (tree, &c);
   1.170 +  g_assert (p && *p == c);
   1.171 +
   1.172 +  c = 'z';
   1.173 +  p = g_tree_lookup (tree, &c);
   1.174 +  g_assert (p && *p == c);
   1.175 +
   1.176 +  c = '!';
   1.177 +  p = g_tree_lookup (tree, &c);
   1.178 +  g_assert (p == NULL);
   1.179 +
   1.180 +  c = '=';
   1.181 +  p = g_tree_lookup (tree, &c);
   1.182 +  g_assert (p == NULL);
   1.183 +
   1.184 +  c = '|';
   1.185 +  p = g_tree_lookup (tree, &c);
   1.186 +  g_assert (p == NULL);
   1.187 +
   1.188 +  c = '0';
   1.189 +  p = g_tree_search (tree, my_search, &c); 
   1.190 +  g_assert (p && *p == c);
   1.191 +
   1.192 +  c = 'A';
   1.193 +  p = g_tree_search (tree, my_search, &c);
   1.194 +  g_assert (p && *p == c);
   1.195 +
   1.196 +  c = 'a';
   1.197 +  p = g_tree_search (tree, my_search, &c);
   1.198 +  g_assert (p &&*p == c);
   1.199 +
   1.200 +  c = 'z';
   1.201 +  p = g_tree_search (tree, my_search, &c);
   1.202 +  g_assert (p && *p == c);
   1.203 +
   1.204 +  c = '!';
   1.205 +  p = g_tree_search (tree, my_search, &c);
   1.206 +  g_assert (p == NULL);
   1.207 +
   1.208 +  c = '=';
   1.209 +  p = g_tree_search (tree, my_search, &c);
   1.210 +  g_assert (p == NULL);
   1.211 +
   1.212 +  c = '|';
   1.213 +  p = g_tree_search (tree, my_search, &c);
   1.214 +  g_assert (p == NULL);
   1.215 +
   1.216 +
   1.217 +  g_tree_destroy (tree);
   1.218 +
   1.219 +  tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL, my_key_destroy, my_value_destroy);
   1.220 +
   1.221 +  for (i = 0; chars[i]; i++)
   1.222 +    g_tree_insert (tree, &chars[i], &chars[i]);
   1.223 +  
   1.224 +  c = '0';
   1.225 +  g_tree_insert (tree, &c, &c);
   1.226 +  g_assert (destroyed_key == &c);
   1.227 +  g_assert (destroyed_value == &chars[0]);
   1.228 +  destroyed_key = NULL;
   1.229 +  destroyed_value = NULL;
   1.230 +
   1.231 +  d = '1';
   1.232 +  g_tree_replace (tree, &d, &d);
   1.233 +  g_assert (destroyed_key == &chars[1]);
   1.234 +  g_assert (destroyed_value == &chars[1]);
   1.235 +  destroyed_key = NULL;
   1.236 +  destroyed_value = NULL;
   1.237 +
   1.238 +  c = '2';
   1.239 +  removed = g_tree_remove (tree, &c);
   1.240 +  g_assert (removed);
   1.241 +  g_assert (destroyed_key == &chars[2]);
   1.242 +  g_assert (destroyed_value == &chars[2]);
   1.243 +  destroyed_key = NULL;
   1.244 +  destroyed_value = NULL;
   1.245 +
   1.246 +  c = '3';
   1.247 +  removed = g_tree_steal (tree, &c);
   1.248 +  g_assert (removed);
   1.249 +  g_assert (destroyed_key == NULL);
   1.250 +  g_assert (destroyed_value == NULL);
   1.251 +#ifdef SYMBIAN
   1.252 +  testResultXml("tree-test");
   1.253 +#endif /* EMULATOR */
   1.254 +
   1.255 +  return 0;
   1.256 +}
   1.257 +