os/ossrv/glib/tsrc/BC/tests/node-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* GLIB - Library of useful routines for C programming
sl@0
     2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
sl@0
     3
 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 *
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General Public
sl@0
    15
 * License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
/*
sl@0
    21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
sl@0
    22
 * file for a list of people on the GLib Team.  See the ChangeLog
sl@0
    23
 * files for a list of changes.  These files are distributed with
sl@0
    24
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
sl@0
    25
 */
sl@0
    26
sl@0
    27
#undef G_DISABLE_ASSERT
sl@0
    28
#undef G_LOG_DOMAIN
sl@0
    29
sl@0
    30
#ifdef HAVE_CONFIG_H
sl@0
    31
#include <config.h>
sl@0
    32
#endif
sl@0
    33
sl@0
    34
#include <stdio.h>
sl@0
    35
#include <string.h>
sl@0
    36
#include <stdlib.h>
sl@0
    37
sl@0
    38
#ifdef HAVE_UNISTD_H
sl@0
    39
#include <unistd.h>
sl@0
    40
#endif
sl@0
    41
sl@0
    42
#include "glib.h"
sl@0
    43
sl@0
    44
#ifdef SYMBIAN
sl@0
    45
#include "mrt2_glib2_test.h"
sl@0
    46
#endif /*SYMBIAN*/
sl@0
    47
sl@0
    48
sl@0
    49
int array[10000];
sl@0
    50
gboolean failed = FALSE;
sl@0
    51
sl@0
    52
#define	TEST(m,cond)	G_STMT_START { failed = !(cond); \
sl@0
    53
if (failed) \
sl@0
    54
  { assert_failed = TRUE; \
sl@0
    55
  	if (!m) \
sl@0
    56
      g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
sl@0
    57
    else \
sl@0
    58
      g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
sl@0
    59
      exit(1); \
sl@0
    60
  } \
sl@0
    61
} G_STMT_END
sl@0
    62
sl@0
    63
#define	C2P(c)		((gpointer) ((long) (c)))
sl@0
    64
#define	P2C(p)		((gchar) ((long) (p)))
sl@0
    65
sl@0
    66
#define GLIB_TEST_STRING "el dorado "
sl@0
    67
#define GLIB_TEST_STRING_5 "el do"
sl@0
    68
sl@0
    69
typedef struct {
sl@0
    70
	guint age;
sl@0
    71
	gchar name[40];
sl@0
    72
} GlibTestInfo;
sl@0
    73
sl@0
    74
static gboolean
sl@0
    75
node_build_string (GNode    *node,
sl@0
    76
		   gpointer  data)
sl@0
    77
{
sl@0
    78
  gchar **p = data;
sl@0
    79
  gchar *string;
sl@0
    80
  gchar c[2] = "_";
sl@0
    81
sl@0
    82
  c[0] = P2C (node->data);
sl@0
    83
sl@0
    84
  string = g_strconcat (*p ? *p : "", c, NULL);
sl@0
    85
  g_free (*p);
sl@0
    86
  *p = string;
sl@0
    87
sl@0
    88
  return FALSE;
sl@0
    89
}
sl@0
    90
sl@0
    91
static void
sl@0
    92
g_node_test (void)
sl@0
    93
{
sl@0
    94
  GNode *root;
sl@0
    95
  GNode *node;
sl@0
    96
  GNode *node_B;
sl@0
    97
  GNode *node_D;
sl@0
    98
  GNode *node_F;
sl@0
    99
  GNode *node_G;
sl@0
   100
  GNode *node_J;
sl@0
   101
  guint i;
sl@0
   102
  gchar *tstring;
sl@0
   103
sl@0
   104
  failed = FALSE;
sl@0
   105
sl@0
   106
  root = g_node_new (C2P ('A'));
sl@0
   107
  TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
sl@0
   108
sl@0
   109
  node_B = g_node_new (C2P ('B'));
sl@0
   110
  g_node_append (root, node_B);
sl@0
   111
  TEST (NULL, root->children == node_B);
sl@0
   112
sl@0
   113
  g_node_append_data (node_B, C2P ('E'));
sl@0
   114
  g_node_prepend_data (node_B, C2P ('C'));
sl@0
   115
  node_D = g_node_new (C2P ('D'));
sl@0
   116
  g_node_insert (node_B, 1, node_D); 
sl@0
   117
sl@0
   118
  node_F = g_node_new (C2P ('F'));
sl@0
   119
  g_node_append (root, node_F);
sl@0
   120
  TEST (NULL, root->children->next == node_F);
sl@0
   121
sl@0
   122
  node_G = g_node_new (C2P ('G'));
sl@0
   123
  g_node_append (node_F, node_G);
sl@0
   124
  node_J = g_node_new (C2P ('J'));
sl@0
   125
  g_node_prepend (node_G, node_J);
sl@0
   126
  g_node_insert (node_G, 42, g_node_new (C2P ('K')));
sl@0
   127
  g_node_insert_data (node_G, 0, C2P ('H'));
sl@0
   128
  g_node_insert (node_G, 1, g_node_new (C2P ('I')));
sl@0
   129
sl@0
   130
  TEST (NULL, g_node_depth (root) == 1);
sl@0
   131
  TEST (NULL, g_node_max_height (root) == 4);
sl@0
   132
  TEST (NULL, g_node_depth (node_G->children->next) == 4);
sl@0
   133
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
sl@0
   134
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
sl@0
   135
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
sl@0
   136
  TEST (NULL, g_node_max_height (node_F) == 3);
sl@0
   137
  TEST (NULL, g_node_n_children (node_G) == 4);
sl@0
   138
  TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
sl@0
   139
  TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
sl@0
   140
  TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
sl@0
   141
sl@0
   142
  for (i = 0; i < g_node_n_children (node_B); i++)
sl@0
   143
    {
sl@0
   144
      node = g_node_nth_child (node_B, i);
sl@0
   145
      TEST (NULL, P2C (node->data) == ('C' + i));
sl@0
   146
    }
sl@0
   147
  
sl@0
   148
  for (i = 0; i < g_node_n_children (node_G); i++)
sl@0
   149
    TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
sl@0
   150
sl@0
   151
  /* we have built:                    A
sl@0
   152
   *                                 /   \
sl@0
   153
   *                               B       F
sl@0
   154
   *                             / | \       \
sl@0
   155
   *                           C   D   E       G
sl@0
   156
   *                                         / /\ \
sl@0
   157
   *                                       H  I  J  K
sl@0
   158
   *
sl@0
   159
   * for in-order traversal, 'G' is considered to be the "left"
sl@0
   160
   * child of 'F', which will cause 'F' to be the last node visited.
sl@0
   161
   */
sl@0
   162
sl@0
   163
  tstring = NULL;
sl@0
   164
  g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
sl@0
   165
  TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
sl@0
   166
  g_free (tstring); tstring = NULL;
sl@0
   167
  g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
sl@0
   168
  TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
sl@0
   169
  g_free (tstring); tstring = NULL;
sl@0
   170
  g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
sl@0
   171
  TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
sl@0
   172
  g_free (tstring); tstring = NULL;
sl@0
   173
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
sl@0
   174
  TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
sl@0
   175
  g_free (tstring); tstring = NULL;
sl@0
   176
  
sl@0
   177
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
sl@0
   178
  TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
sl@0
   179
  g_free (tstring); tstring = NULL;
sl@0
   180
  g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
sl@0
   181
  TEST (tstring, strcmp (tstring, "ABFG") == 0);
sl@0
   182
  g_free (tstring); tstring = NULL;
sl@0
   183
sl@0
   184
  g_node_reverse_children (node_B);
sl@0
   185
  g_node_reverse_children (node_G);
sl@0
   186
sl@0
   187
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
sl@0
   188
  TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
sl@0
   189
  g_free (tstring); tstring = NULL;
sl@0
   190
  
sl@0
   191
  g_node_append (node_D, g_node_new (C2P ('L')));
sl@0
   192
  g_node_append (node_D, g_node_new (C2P ('M')));
sl@0
   193
sl@0
   194
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
sl@0
   195
  TEST (tstring, strcmp (tstring, "ABFEDCGLMKJIH") == 0);
sl@0
   196
  g_free (tstring); tstring = NULL;
sl@0
   197
sl@0
   198
  g_node_destroy (root);
sl@0
   199
sl@0
   200
  /* allocation tests */
sl@0
   201
sl@0
   202
  root = g_node_new (NULL);
sl@0
   203
  node = root;
sl@0
   204
sl@0
   205
  for (i = 0; i < 200; i++)
sl@0
   206
    {
sl@0
   207
      g_node_append (node, g_node_new (NULL));
sl@0
   208
      if ((i%5) == 4)
sl@0
   209
	node = node->children->next;
sl@0
   210
    }
sl@0
   211
  TEST (NULL, g_node_max_height (root) > 40);
sl@0
   212
  TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 200);
sl@0
   213
sl@0
   214
  g_node_destroy (root);
sl@0
   215
  
sl@0
   216
  if (failed)
sl@0
   217
    exit(1);
sl@0
   218
}
sl@0
   219
sl@0
   220
sl@0
   221
int
sl@0
   222
main (int   argc,
sl@0
   223
      char *argv[])
sl@0
   224
{
sl@0
   225
  #ifdef SYMBIAN
sl@0
   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);
sl@0
   227
  g_set_print_handler(mrtPrintHandler);
sl@0
   228
  #endif /*SYMBIAN*/
sl@0
   229
	  
sl@0
   230
sl@0
   231
  g_node_test ();
sl@0
   232
sl@0
   233
#ifdef SYMBIAN
sl@0
   234
  testResultXml("node-test");
sl@0
   235
#endif /* EMULATOR */
sl@0
   236
  return 0;
sl@0
   237
}
sl@0
   238