glib/tests/node-test.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     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 #ifdef HAVE_CONFIG_H
       
    31 #include "config.h"
       
    32 #endif
       
    33 
       
    34 #include <stdio.h>
       
    35 #include <string.h>
       
    36 #include <stdlib.h>
       
    37 
       
    38 #ifdef HAVE_UNISTD_H
       
    39 #include <unistd.h>
       
    40 #endif
       
    41 
       
    42 #include "glib.h"
       
    43 
       
    44 #ifdef __SYMBIAN32__
       
    45 #include "mrt2_glib2_test.h"
       
    46 #endif /*__SYMBIAN32__*/
       
    47 
       
    48 
       
    49 int array[10000];
       
    50 gboolean failed = FALSE;
       
    51 
       
    52 #define	TEST(m,cond)	G_STMT_START { failed = !(cond); \
       
    53 if (failed) \
       
    54   { if (!m) \
       
    55       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
       
    56     else \
       
    57       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
       
    58       exit(1); \
       
    59   } \
       
    60 } G_STMT_END
       
    61 
       
    62 #define	C2P(c)		((gpointer) ((long) (c)))
       
    63 #define	P2C(p)		((gchar) ((long) (p)))
       
    64 
       
    65 #define GLIB_TEST_STRING "el dorado "
       
    66 #define GLIB_TEST_STRING_5 "el do"
       
    67 
       
    68 typedef struct {
       
    69 	guint age;
       
    70 	gchar name[40];
       
    71 } GlibTestInfo;
       
    72 
       
    73 static gboolean
       
    74 node_build_string (GNode    *node,
       
    75 		   gpointer  data)
       
    76 {
       
    77   gchar **p = data;
       
    78   gchar *string;
       
    79   gchar c[2] = "_";
       
    80 
       
    81   c[0] = P2C (node->data);
       
    82 
       
    83   string = g_strconcat (*p ? *p : "", c, NULL);
       
    84   g_free (*p);
       
    85   *p = string;
       
    86 
       
    87   return FALSE;
       
    88 }
       
    89 
       
    90 static void
       
    91 g_node_test (void)
       
    92 {
       
    93   GNode *root;
       
    94   GNode *node;
       
    95   GNode *node_B;
       
    96   GNode *node_D;
       
    97   GNode *node_F;
       
    98   GNode *node_G;
       
    99   GNode *node_J;
       
   100   guint i;
       
   101   gchar *tstring;
       
   102 
       
   103   failed = FALSE;
       
   104 
       
   105   root = g_node_new (C2P ('A'));
       
   106   TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
       
   107 
       
   108   node_B = g_node_new (C2P ('B'));
       
   109   g_node_append (root, node_B);
       
   110   TEST (NULL, root->children == node_B);
       
   111 
       
   112   g_node_append_data (node_B, C2P ('E'));
       
   113   g_node_prepend_data (node_B, C2P ('C'));
       
   114   node_D = g_node_new (C2P ('D'));
       
   115   g_node_insert (node_B, 1, node_D); 
       
   116 
       
   117   node_F = g_node_new (C2P ('F'));
       
   118   g_node_append (root, node_F);
       
   119   TEST (NULL, root->children->next == node_F);
       
   120 
       
   121   node_G = g_node_new (C2P ('G'));
       
   122   g_node_append (node_F, node_G);
       
   123   node_J = g_node_new (C2P ('J'));
       
   124   g_node_prepend (node_G, node_J);
       
   125   g_node_insert (node_G, 42, g_node_new (C2P ('K')));
       
   126   g_node_insert_data (node_G, 0, C2P ('H'));
       
   127   g_node_insert (node_G, 1, g_node_new (C2P ('I')));
       
   128 
       
   129   TEST (NULL, g_node_depth (root) == 1);
       
   130   TEST (NULL, g_node_max_height (root) == 4);
       
   131   TEST (NULL, g_node_depth (node_G->children->next) == 4);
       
   132   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
       
   133   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
       
   134   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
       
   135   TEST (NULL, g_node_max_height (node_F) == 3);
       
   136   TEST (NULL, g_node_n_children (node_G) == 4);
       
   137   TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
       
   138   TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
       
   139   TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
       
   140 
       
   141   for (i = 0; i < g_node_n_children (node_B); i++)
       
   142     {
       
   143       node = g_node_nth_child (node_B, i);
       
   144       TEST (NULL, P2C (node->data) == ('C' + i));
       
   145     }
       
   146   
       
   147   for (i = 0; i < g_node_n_children (node_G); i++)
       
   148     TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
       
   149 
       
   150   /* we have built:                    A
       
   151    *                                 /   \
       
   152    *                               B       F
       
   153    *                             / | \       \
       
   154    *                           C   D   E       G
       
   155    *                                         / /\ \
       
   156    *                                       H  I  J  K
       
   157    *
       
   158    * for in-order traversal, 'G' is considered to be the "left"
       
   159    * child of 'F', which will cause 'F' to be the last node visited.
       
   160    */
       
   161 
       
   162   tstring = NULL;
       
   163   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
       
   164   TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
       
   165   g_free (tstring); tstring = NULL;
       
   166   g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
       
   167   TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
       
   168   g_free (tstring); tstring = NULL;
       
   169   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
       
   170   TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
       
   171   g_free (tstring); tstring = NULL;
       
   172   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
       
   173   TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
       
   174   g_free (tstring); tstring = NULL;
       
   175   
       
   176   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
       
   177   TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
       
   178   g_free (tstring); tstring = NULL;
       
   179   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
       
   180   TEST (tstring, strcmp (tstring, "ABFG") == 0);
       
   181   g_free (tstring); tstring = NULL;
       
   182 
       
   183   g_node_reverse_children (node_B);
       
   184   g_node_reverse_children (node_G);
       
   185 
       
   186   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
       
   187   TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
       
   188   g_free (tstring); tstring = NULL;
       
   189   
       
   190   g_node_append (node_D, g_node_new (C2P ('L')));
       
   191   g_node_append (node_D, g_node_new (C2P ('M')));
       
   192 
       
   193   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
       
   194   TEST (tstring, strcmp (tstring, "ABFEDCGLMKJIH") == 0);
       
   195   g_free (tstring); tstring = NULL;
       
   196 
       
   197   g_node_destroy (root);
       
   198 
       
   199   /* allocation tests */
       
   200 
       
   201   root = g_node_new (NULL);
       
   202   node = root;
       
   203 
       
   204   for (i = 0; i < 2048; i++)
       
   205     {
       
   206       g_node_append (node, g_node_new (NULL));
       
   207       if ((i%5) == 4)
       
   208 	node = node->children->next;
       
   209     }
       
   210   TEST (NULL, g_node_max_height (root) > 100);
       
   211   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
       
   212 
       
   213   g_node_destroy (root);
       
   214   
       
   215   if (failed)
       
   216     exit(1);
       
   217 }
       
   218 
       
   219 
       
   220 int
       
   221 main (int   argc,
       
   222       char *argv[])
       
   223 {
       
   224   #ifdef __SYMBIAN32__
       
   225   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);
       
   226   g_set_print_handler(mrtPrintHandler);
       
   227   #endif /*__SYMBIAN32__*/
       
   228 	  
       
   229 
       
   230   g_node_test ();
       
   231 
       
   232 #ifdef __SYMBIAN32__
       
   233   testResultXml("node-test");
       
   234 #endif /* EMULATOR */
       
   235   return 0;
       
   236 }
       
   237