apicompatanamdw/bcdrivers/os/ossrv/glib/tests/tree-test.c
changeset 2 0cb2248d0edc
equal deleted inserted replaced
1:61e9400fe245 2:0cb2248d0edc
       
     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 SYMBIAN
       
    35 #include "mrt2_glib2_test.h"
       
    36 #endif /*SYMBIAN*/
       
    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   #ifdef SYMBIAN
       
   116   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);
       
   117   g_set_print_handler(mrtPrintHandler);
       
   118   #endif /*SYMBIAN*/
       
   119 
       
   120   tree = g_tree_new (my_compare);
       
   121 
       
   122   for (i = 0; chars[i]; i++)
       
   123     g_tree_insert (tree, &chars[i], &chars[i]);
       
   124 
       
   125   g_tree_foreach (tree, my_traverse, NULL);
       
   126 
       
   127   g_assert (g_tree_nnodes (tree) == strlen (chars));
       
   128   g_assert (g_tree_height (tree) == 6);
       
   129   
       
   130   p = chars;
       
   131   g_tree_foreach (tree, check_order, &p);
       
   132 
       
   133   for (i = 0; i < 26; i++)
       
   134     {
       
   135       removed = g_tree_remove (tree, &chars[i + 10]);
       
   136       g_assert (removed);
       
   137     }
       
   138 
       
   139   c = '\0';
       
   140   removed = g_tree_remove (tree, &c);
       
   141   g_assert (removed == FALSE);
       
   142 
       
   143   g_tree_foreach (tree, my_traverse, NULL);
       
   144 
       
   145   g_assert (g_tree_nnodes (tree) == strlen (chars2));
       
   146   g_assert (g_tree_height (tree) == 6);
       
   147 
       
   148   p = chars2;
       
   149   g_tree_foreach (tree, check_order, &p);
       
   150 
       
   151   for (i = 25; i >= 0; i--)
       
   152     g_tree_insert (tree, &chars[i + 10], &chars[i + 10]);
       
   153 
       
   154   p = chars;
       
   155   g_tree_foreach (tree, check_order, &p);
       
   156 
       
   157   c = '0';
       
   158   p = g_tree_lookup (tree, &c); 
       
   159   g_assert (p && *p == c);
       
   160 
       
   161   c = 'A';
       
   162   p = g_tree_lookup (tree, &c);
       
   163   g_assert (p && *p == c);
       
   164 
       
   165   c = 'a';
       
   166   p = g_tree_lookup (tree, &c);
       
   167   g_assert (p && *p == c);
       
   168 
       
   169   c = 'z';
       
   170   p = g_tree_lookup (tree, &c);
       
   171   g_assert (p && *p == c);
       
   172 
       
   173   c = '!';
       
   174   p = g_tree_lookup (tree, &c);
       
   175   g_assert (p == NULL);
       
   176 
       
   177   c = '=';
       
   178   p = g_tree_lookup (tree, &c);
       
   179   g_assert (p == NULL);
       
   180 
       
   181   c = '|';
       
   182   p = g_tree_lookup (tree, &c);
       
   183   g_assert (p == NULL);
       
   184 
       
   185   c = '0';
       
   186   p = g_tree_search (tree, my_search, &c); 
       
   187   g_assert (p && *p == c);
       
   188 
       
   189   c = 'A';
       
   190   p = g_tree_search (tree, my_search, &c);
       
   191   g_assert (p && *p == c);
       
   192 
       
   193   c = 'a';
       
   194   p = g_tree_search (tree, my_search, &c);
       
   195   g_assert (p &&*p == c);
       
   196 
       
   197   c = 'z';
       
   198   p = g_tree_search (tree, my_search, &c);
       
   199   g_assert (p && *p == c);
       
   200 
       
   201   c = '!';
       
   202   p = g_tree_search (tree, my_search, &c);
       
   203   g_assert (p == NULL);
       
   204 
       
   205   c = '=';
       
   206   p = g_tree_search (tree, my_search, &c);
       
   207   g_assert (p == NULL);
       
   208 
       
   209   c = '|';
       
   210   p = g_tree_search (tree, my_search, &c);
       
   211   g_assert (p == NULL);
       
   212 
       
   213 
       
   214   g_tree_destroy (tree);
       
   215 
       
   216   tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL, my_key_destroy, my_value_destroy);
       
   217 
       
   218   for (i = 0; chars[i]; i++)
       
   219     g_tree_insert (tree, &chars[i], &chars[i]);
       
   220   
       
   221   c = '0';
       
   222   g_tree_insert (tree, &c, &c);
       
   223   g_assert (destroyed_key == &c);
       
   224   g_assert (destroyed_value == &chars[0]);
       
   225   destroyed_key = NULL;
       
   226   destroyed_value = NULL;
       
   227 
       
   228   d = '1';
       
   229   g_tree_replace (tree, &d, &d);
       
   230   g_assert (destroyed_key == &chars[1]);
       
   231   g_assert (destroyed_value == &chars[1]);
       
   232   destroyed_key = NULL;
       
   233   destroyed_value = NULL;
       
   234 
       
   235   c = '2';
       
   236   removed = g_tree_remove (tree, &c);
       
   237   g_assert (removed);
       
   238   g_assert (destroyed_key == &chars[2]);
       
   239   g_assert (destroyed_value == &chars[2]);
       
   240   destroyed_key = NULL;
       
   241   destroyed_value = NULL;
       
   242 
       
   243   c = '3';
       
   244   removed = g_tree_steal (tree, &c);
       
   245   g_assert (removed);
       
   246   g_assert (destroyed_key == NULL);
       
   247   g_assert (destroyed_value == NULL);
       
   248 #ifdef SYMBIAN
       
   249   testResultXml("tree-test");
       
   250 #endif /* EMULATOR */
       
   251 
       
   252   return 0;
       
   253 }
       
   254