glib/tsrc/glib_nonstif/group/tree-test.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 #include <stdio.h>
       
    31 #include <string.h>
       
    32 #include "glib.h"
       
    33 
       
    34 #ifdef __SYMBIAN32__
       
    35 #include "mrt2_glib2_test.h"
       
    36 #endif /*__SYMBIAN32__*/
       
    37 
       
    38 static gint
       
    39 my_compare (gconstpointer a,
       
    40 	    gconstpointer b)
       
    41 {
       
    42   const char *cha = a;
       
    43   const char *chb = b;
       
    44 
       
    45   return *cha - *chb;
       
    46 }
       
    47 
       
    48 static gint
       
    49 my_search (gconstpointer a,
       
    50 	   gconstpointer b)
       
    51 {
       
    52   return my_compare (b, a);
       
    53 }
       
    54 
       
    55 static gpointer destroyed_key = NULL;
       
    56 static gpointer destroyed_value = NULL;
       
    57 
       
    58 static void 
       
    59 my_key_destroy (gpointer key)
       
    60 {
       
    61   destroyed_key = key;
       
    62 }
       
    63 
       
    64 static void 
       
    65 my_value_destroy (gpointer value)
       
    66 {
       
    67   destroyed_value = value;
       
    68 }
       
    69 
       
    70 static gint
       
    71 my_traverse (gpointer key,
       
    72 	     gpointer value,
       
    73 	     gpointer data)
       
    74 {
       
    75   char *ch = key;
       
    76   g_assert ((*ch) > 0);
       
    77   return FALSE;
       
    78 }
       
    79 
       
    80 char chars[] = 
       
    81   "0123456789"
       
    82   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
       
    83   "abcdefghijklmnopqrstuvwxyz";
       
    84 
       
    85 char chars2[] = 
       
    86   "0123456789"
       
    87   "abcdefghijklmnopqrstuvwxyz";
       
    88 
       
    89 static gint
       
    90 check_order (gpointer key,
       
    91 	     gpointer value,
       
    92 	     gpointer data)
       
    93 {
       
    94   char **p = data;
       
    95   char *ch = key;
       
    96   
       
    97   g_assert (**p == *ch);
       
    98 
       
    99   (*p)++;
       
   100 
       
   101   return FALSE;
       
   102 }
       
   103 
       
   104 
       
   105 
       
   106 int
       
   107 main (int   argc,
       
   108       char *argv[])
       
   109 {
       
   110   gint i;
       
   111   GTree *tree;
       
   112   gboolean removed;
       
   113   char c, d;
       
   114   char *p;
       
   115   
       
   116   #ifdef __SYMBIAN32__
       
   117   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);
       
   118   g_set_print_handler(mrtPrintHandler);
       
   119   #endif /*__SYMBIAN32__*/
       
   120 
       
   121   tree = g_tree_new (my_compare);
       
   122 
       
   123   for (i = 0; chars[i]; i++)
       
   124     g_tree_insert (tree, &chars[i], &chars[i]);
       
   125 
       
   126   g_tree_foreach (tree, my_traverse, NULL);
       
   127 
       
   128   g_assert (g_tree_nnodes (tree) == strlen (chars));
       
   129   g_assert (g_tree_height (tree) == 6);
       
   130   
       
   131   p = chars;
       
   132   g_tree_foreach (tree, check_order, &p);
       
   133 
       
   134   for (i = 0; i < 26; i++)
       
   135     {
       
   136       removed = g_tree_remove (tree, &chars[i + 10]);
       
   137       g_assert (removed);
       
   138     }
       
   139 
       
   140   c = '\0';
       
   141   removed = g_tree_remove (tree, &c);
       
   142   g_assert (removed == FALSE);
       
   143 
       
   144   g_tree_foreach (tree, my_traverse, NULL);
       
   145 
       
   146   g_assert (g_tree_nnodes (tree) == strlen (chars2));
       
   147   g_assert (g_tree_height (tree) == 6);
       
   148 
       
   149   p = chars2;
       
   150   g_tree_foreach (tree, check_order, &p);
       
   151 
       
   152   for (i = 25; i >= 0; i--)
       
   153     g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
       
   154 
       
   155   p = chars;
       
   156   g_tree_foreach (tree, check_order, &p);
       
   157 
       
   158   c = '0';
       
   159   p = g_tree_lookup (tree, &c); 
       
   160   g_assert (p && *p == c);
       
   161 
       
   162   c = 'A';
       
   163   p = g_tree_lookup (tree, &c);
       
   164   g_assert (p && *p == c);
       
   165 
       
   166   c = 'a';
       
   167   p = g_tree_lookup (tree, &c);
       
   168   g_assert (p && *p == c);
       
   169 
       
   170   c = 'z';
       
   171   p = g_tree_lookup (tree, &c);
       
   172   g_assert (p && *p == c);
       
   173 
       
   174   c = '!';
       
   175   p = g_tree_lookup (tree, &c);
       
   176   g_assert (p == NULL);
       
   177 
       
   178   c = '=';
       
   179   p = g_tree_lookup (tree, &c);
       
   180   g_assert (p == NULL);
       
   181 
       
   182   c = '|';
       
   183   p = g_tree_lookup (tree, &c);
       
   184   g_assert (p == NULL);
       
   185 
       
   186   c = '0';
       
   187   p = g_tree_search (tree, my_search, &c); 
       
   188   g_assert (p && *p == c);
       
   189 
       
   190   c = 'A';
       
   191   p = g_tree_search (tree, my_search, &c);
       
   192   g_assert (p && *p == c);
       
   193 
       
   194   c = 'a';
       
   195   p = g_tree_search (tree, my_search, &c);
       
   196   g_assert (p &&*p == c);
       
   197 
       
   198   c = 'z';
       
   199   p = g_tree_search (tree, my_search, &c);
       
   200   g_assert (p && *p == c);
       
   201 
       
   202   c = '!';
       
   203   p = g_tree_search (tree, my_search, &c);
       
   204   g_assert (p == NULL);
       
   205 
       
   206   c = '=';
       
   207   p = g_tree_search (tree, my_search, &c);
       
   208   g_assert (p == NULL);
       
   209 
       
   210   c = '|';
       
   211   p = g_tree_search (tree, my_search, &c);
       
   212   g_assert (p == NULL);
       
   213 
       
   214 
       
   215   g_tree_destroy (tree);
       
   216 
       
   217   tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL, my_key_destroy, my_value_destroy);
       
   218 
       
   219   for (i = 0; chars[i]; i++)
       
   220     g_tree_insert (tree, &chars[i], &chars[i]);
       
   221   
       
   222   c = '0';
       
   223   g_tree_insert (tree, &c, &c);
       
   224   g_assert (destroyed_key == &c);
       
   225   g_assert (destroyed_value == &chars[0]);
       
   226   destroyed_key = NULL;
       
   227   destroyed_value = NULL;
       
   228 
       
   229   d = '1';
       
   230   g_tree_replace (tree, &d, &d);
       
   231   g_assert (destroyed_key == &chars[1]);
       
   232   g_assert (destroyed_value == &chars[1]);
       
   233   destroyed_key = NULL;
       
   234   destroyed_value = NULL;
       
   235 
       
   236   c = '2';
       
   237   removed = g_tree_remove (tree, &c);
       
   238   g_assert (removed);
       
   239   g_assert (destroyed_key == &chars[2]);
       
   240   g_assert (destroyed_value == &chars[2]);
       
   241   destroyed_key = NULL;
       
   242   destroyed_value = NULL;
       
   243 
       
   244   c = '3';
       
   245   removed = g_tree_steal (tree, &c);
       
   246   g_assert (removed);
       
   247   g_assert (destroyed_key == NULL);
       
   248   g_assert (destroyed_value == NULL);
       
   249 #ifdef __SYMBIAN32__
       
   250   testResultXml("tree-test");
       
   251 #endif /* EMULATOR */
       
   252 
       
   253   return 0;
       
   254 }
       
   255