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