node.c 17.7 KB
Newer Older
1 2 3 4
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * 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.1 of the License, or (at your option) any later version.
8 9 10 11
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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, see <http://www.gnu.org/licenses/>.
16
 */
17 18

/*
19
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

25
#undef G_DISABLE_ASSERT
26 27 28 29
#undef G_LOG_DOMAIN

#include <stdio.h>
#include <string.h>
30
#include <stdlib.h>
31

32 33
#include "glib.h"

34 35
#define C2P(c)          ((gpointer) ((long) (c)))
#define P2C(p)          ((gchar) ((long) (p)))
36

37 38 39 40 41
typedef struct {
  GString *s;
  gint count;
} CallbackData;

42 43
static gboolean
node_build_string (GNode    *node,
44
                   gpointer  data)
45
{
46 47 48
  CallbackData *d = data;

  g_string_append_c (d->s, P2C (node->data));
49

50
  d->count--;
51

52 53
  if (d->count == 0)
    return TRUE;
54 55 56 57

  return FALSE;
}

58 59 60 61 62 63 64 65
typedef struct {
    GTraverseType   traverse;
    GTraverseFlags  flags;
    gint            depth;
    gint            limit;
    const gchar    *expected;
} TraverseData;

66
static void
67
traversal_test (void)
68 69 70
{
  GNode *root;
  GNode *node_B;
71
  GNode *node_C;
72
  GNode *node_D;
73
  GNode *node_E;
74 75 76
  GNode *node_F;
  GNode *node_G;
  GNode *node_J;
77
  GNode *n;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  TraverseData orders[] = {
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1, -1, "ABCDEFGHIJK" },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        1, -1, "A"           },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        2, -1, "ABF"         },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3, -1, "ABCDEFG"     },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3, -1, "ABCDEFG"     },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1, -1, "CDEBHIJKGFA" },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        1, -1, "A"           },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        2, -1, "BFA"         },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3, -1, "CDEBGFA"     },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1, -1, "CBDEAHGIJKF" },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        1, -1, "A"           },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        2, -1, "BAF"         },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3, -1, "CBDEAGF"     },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1, -1, "ABFCDEGHIJK" },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        1, -1, "A"           },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        2, -1, "ABF"         },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3, -1, "ABFCDEG"     },
    { G_LEVEL_ORDER, G_TRAVERSE_LEAFS,     -1, -1, "CDEHIJK"     },
    { G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, -1, -1, "ABFG"        },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  1, "A"           },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  2, "AB"          },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  3, "ABC"         },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  4, "ABCD"        },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  5, "ABCDE"       },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  6, "ABCDEF"      },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  7, "ABCDEFG"     },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  8, "ABCDEFGH"    },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  9, "ABCDEFGHI"   },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1, 10, "ABCDEFGHIJ"  },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  1, "A"           },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  2, "AB"          },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  3, "ABC"         },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  4, "ABCD"        },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  5, "ABCDE"       },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  6, "ABCDEF"      },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  7, "ABCDEFG"     },
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  8, "ABCDEFG"     },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  1, "C"           },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  2, "CD"          },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  3, "CDE"         },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  4, "CDEB"        },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  5, "CDEBH"       },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  6, "CDEBHI"      },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  7, "CDEBHIJ"     },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  8, "CDEBHIJK"    },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  9, "CDEBHIJKG"   },
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1, 10, "CDEBHIJKGF"  },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  1, "C"           },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  2, "CD"          },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  3, "CDE"         },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  4, "CDEB"        },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  5, "CDEBG"       },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  6, "CDEBGF"      },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  7, "CDEBGFA"     },
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  8, "CDEBGFA"     },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  1, "C"           },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  2, "CB"          },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  3, "CBD"         },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  4, "CBDE"        },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  5, "CBDEA"       },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  6, "CBDEAH"      },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  7, "CBDEAHG"     },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  8, "CBDEAHGI"    },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  9, "CBDEAHGIJ"   },
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1, 10, "CBDEAHGIJK"  },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  1, "C"           },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  2, "CB"          },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  3, "CBD"         },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  4, "CBDE"        },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  5, "CBDEA"       },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  6, "CBDEAG"      },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  7, "CBDEAGF"     },
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  8, "CBDEAGF"     },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  1, "A"           },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  2, "AB"          },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  3, "ABF"         },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  4, "ABFC"        },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  5, "ABFCD"       },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  6, "ABFCDE"      },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  7, "ABFCDEG"     },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  8, "ABFCDEGH"    },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  9, "ABFCDEGHI"   },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1, 10, "ABFCDEGHIJ"  },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  1, "A"           },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  2, "AB"          },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  3, "ABF"         },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  4, "ABFC"        },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  5, "ABFCD"       },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  6, "ABFCDE"      },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  7, "ABFCDEG"     },
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  8, "ABFCDEG"     },
  };
  gint i;
  CallbackData data;
173 174 175 176 177 178

  root = g_node_new (C2P ('A'));
  node_B = g_node_new (C2P ('B'));
  g_node_append (root, node_B);
  g_node_append_data (node_B, C2P ('E'));
  g_node_prepend_data (node_B, C2P ('C'));
179
  node_D = g_node_new (C2P ('D'));
180
  g_node_insert (node_B, 1, node_D);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  node_F = g_node_new (C2P ('F'));
  g_node_append (root, node_F);
  node_G = g_node_new (C2P ('G'));
  g_node_append (node_F, node_G);
  node_J = g_node_new (C2P ('J'));
  g_node_prepend (node_G, node_J);
  g_node_insert (node_G, 42, g_node_new (C2P ('K')));
  g_node_insert_data (node_G, 0, C2P ('H'));
  g_node_insert (node_G, 1, g_node_new (C2P ('I')));

  /* we have built:                    A
   *                                 /   \
   *                               B       F
   *                             / | \       \
   *                           C   D   E       G
   *                                         / /\ \
197
   *                                        H I J  K
198 199 200 201 202
   *
   * for in-order traversal, 'G' is considered to be the "left"
   * child of 'F', which will cause 'F' to be the last node visited.
   */

203 204 205 206 207 208 209 210 211 212
  node_C = node_B->children;
  node_E = node_D->next;

  n = g_node_last_sibling (node_C);
  g_assert (n == node_E);
  n = g_node_last_sibling (node_D);
  g_assert (n == node_E);
  n = g_node_last_sibling (node_E);
  g_assert (n == node_E);

213 214 215 216 217 218 219 220
  data.s = g_string_new ("");  
  for (i = 0; i < G_N_ELEMENTS (orders); i++)
    {
      g_string_set_size (data.s, 0);
      data.count = orders[i].limit;
      g_node_traverse (root, orders[i].traverse, orders[i].flags, orders[i].depth, node_build_string, &data);
      g_assert_cmpstr (data.s->str, ==,  orders[i].expected);
    }
221 222 223 224

  g_node_reverse_children (node_B);
  g_node_reverse_children (node_G);

225 226 227 228
  g_string_set_size (data.s, 0);
  data.count = -1;
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &data);
  g_assert_cmpstr (data.s->str, ==, "ABFEDCGKJIH");
229
  
230
  g_node_append (node_D, g_node_new (C2P ('L')));
231
  g_node_insert (node_D, -1, g_node_new (C2P ('M')));
232

233 234 235 236
  g_string_set_size (data.s, 0);
  data.count = -1;
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &data);
  g_assert_cmpstr (data.s->str, ==, "ABFEDCGLMKJIH");
237

238
  g_node_destroy (root);
239
  g_string_free (data.s, TRUE);
240 241 242 243 244 245 246 247 248 249 250 251
}

static void
construct_test (void)
{
  GNode *root;
  GNode *node;
  GNode *node_B;
  GNode *node_D;
  GNode *node_F;
  GNode *node_G;
  GNode *node_J;
252
  GNode *node_H;
253 254 255 256 257 258 259 260 261 262 263 264 265 266
  guint i;

  root = g_node_new (C2P ('A'));
  g_assert_cmpint (g_node_depth (root), ==, 1);
  g_assert_cmpint (g_node_max_height (root), ==, 1);

  node_B = g_node_new (C2P ('B'));
  g_node_append (root, node_B);
  g_assert (root->children == node_B);

  g_node_append_data (node_B, C2P ('E'));
  g_node_prepend_data (node_B, C2P ('C'));
  node_D = g_node_new (C2P ('D'));
  g_node_insert (node_B, 1, node_D);
267

268 269 270 271 272 273 274
  node_F = g_node_new (C2P ('F'));
  g_node_append (root, node_F);
  g_assert (root->children->next == node_F);

  node_G = g_node_new (C2P ('G'));
  g_node_append (node_F, node_G);
  node_J = g_node_new (C2P ('J'));
275
  g_node_insert_after (node_G, NULL, node_J);
276
  g_node_insert (node_G, 42, g_node_new (C2P ('K')));
277 278
  node_H = g_node_new (C2P ('H'));
  g_node_insert_after (node_G, NULL, node_H);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  g_node_insert (node_G, 1, g_node_new (C2P ('I')));

  /* we have built:                    A
   *                                 /   \
   *                               B       F
   *                             / | \       \
   *                           C   D   E       G
   *                                         / /\ \
   *                                       H  I  J  K
   */
  g_assert_cmpint (g_node_depth (root), ==, 1);
  g_assert_cmpint (g_node_max_height (root), ==, 4);
  g_assert_cmpint (g_node_depth (node_G->children->next), ==, 4);
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_LEAFS), ==, 7);
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS), ==, 4);
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 11);
  g_assert_cmpint (g_node_max_height (node_F), ==, 3);
  g_assert_cmpint (g_node_n_children (node_G), ==, 4);
  g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
298 299
  g_assert (g_node_find_child (node_G, G_TRAVERSE_LEAFS, C2P ('H')) == node_H);
  g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('H')) == NULL);
300 301 302 303 304 305 306 307 308 309 310
  g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
  g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);

  for (i = 0; i < g_node_n_children (node_B); i++)
    {
      node = g_node_nth_child (node_B, i);
      g_assert_cmpint (P2C (node->data), ==, ('C' + i));
    }

  for (i = 0; i < g_node_n_children (node_G); i++)
    g_assert_cmpint (g_node_child_position (node_G, g_node_nth_child (node_G, i)), ==, i);
311 312

  g_node_destroy (root);
313 314 315 316 317 318 319 320
}

static void
allocation_test (void)
{
  GNode *root;
  GNode *node;
  gint i;
321 322 323 324 325 326 327

  root = g_node_new (NULL);
  node = root;

  for (i = 0; i < 2048; i++)
    {
      g_node_append (node, g_node_new (NULL));
328 329
      if ((i % 5) == 4)
        node = node->children->next;
330
    }
331 332
  g_assert_cmpint (g_node_max_height (root), >, 100);
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 1 + 2048);
333 334 335 336 337

  g_node_destroy (root);
}


338 339 340 341 342 343 344 345
static void
misc_test (void)
{
  GNode *root;
  GNode *node_B;
  GNode *node_C;
  GNode *node_D;
  GNode *node_E;
346
  CallbackData data;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

  root = g_node_new (C2P ('A'));
  node_B = g_node_new (C2P ('B'));
  g_node_append (root, node_B);
  node_D = g_node_new (C2P ('D'));
  g_node_append (root, node_D);
  node_C = g_node_new (C2P ('C'));
  g_node_insert_after (root, node_B, node_C);
  node_E = g_node_new (C2P ('E'));
  g_node_append (node_C, node_E);

  g_assert (g_node_get_root (node_E) == root);
  g_assert (g_node_is_ancestor (root, node_B));
  g_assert (g_node_is_ancestor (root, node_E));
  g_assert (!g_node_is_ancestor (node_B, node_D));
  g_assert (g_node_first_sibling (node_D) == node_B);
  g_assert (g_node_first_sibling (node_E) == node_E);
364
  g_assert (g_node_first_sibling (root) == root);
365 366 367 368 369
  g_assert_cmpint (g_node_child_index (root, C2P ('B')), ==, 0);
  g_assert_cmpint (g_node_child_index (root, C2P ('C')), ==, 1);
  g_assert_cmpint (g_node_child_index (root, C2P ('D')), ==, 2);
  g_assert_cmpint (g_node_child_index (root, C2P ('E')), ==, -1);

370 371 372 373
  data.s = g_string_new ("");
  data.count = -1;
  g_node_children_foreach (root, G_TRAVERSE_ALL, (GNodeForeachFunc)node_build_string, &data);
  g_assert_cmpstr (data.s->str, ==, "BCD");
374

375 376 377 378
  g_string_set_size (data.s, 0);
  data.count = -1;
  g_node_children_foreach (root, G_TRAVERSE_LEAVES, (GNodeForeachFunc)node_build_string, &data);
  g_assert_cmpstr (data.s->str, ==, "BD");
379

380 381 382 383 384
  g_string_set_size (data.s, 0);
  data.count = -1;
  g_node_children_foreach (root, G_TRAVERSE_NON_LEAVES, (GNodeForeachFunc)node_build_string, &data);
  g_assert_cmpstr (data.s->str, ==, "C");
  g_string_free (data.s, TRUE);
385 386 387 388

  g_node_destroy (root);
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
static gboolean
check_order (GNode    *node,
             gpointer  data)
{
  gchar **expected = data;
  gchar d;

  d = GPOINTER_TO_INT (node->data);
  g_assert_cmpint (d, ==, **expected);
  (*expected)++;

  return FALSE;
}

static void
unlink_test (void)
{
  GNode *root;
  GNode *node;
408 409
  GNode *bnode;
  GNode *cnode;
410 411 412 413 414 415 416 417 418 419 420 421
  gchar *expected;

  /*
   *        -------- a --------
   *       /         |          \
   *     b           c           d
   *   / | \       / | \       / | \
   * e   f   g   h   i   j   k   l   m
   *
   */

  root = g_node_new (C2P ('a'));
422
  node = bnode = g_node_append_data (root, C2P ('b'));
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  g_node_append_data (node, C2P ('e'));
  g_node_append_data (node, C2P ('f'));
  g_node_append_data (node, C2P ('g'));

  node = cnode = g_node_append_data (root, C2P ('c'));
  g_node_append_data (node, C2P ('h'));
  g_node_append_data (node, C2P ('i'));
  g_node_append_data (node, C2P ('j'));

  node = g_node_append_data (root, C2P ('d'));
  g_node_append_data (node, C2P ('k'));
  g_node_append_data (node, C2P ('l'));
  g_node_append_data (node, C2P ('m'));

  g_node_unlink (cnode);

  expected = "abdefgklm";
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);

442 443 444
  expected = "abd";
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, 1, check_order, &expected);

445 446 447
  expected = "chij";
  g_node_traverse (cnode, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);

448 449 450 451 452
  g_node_destroy (bnode);

  expected = "adklm";
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);

453 454 455 456 457 458 459 460 461 462 463 464 465
  g_node_destroy (root);
  g_node_destroy (cnode);
}

static gpointer
copy_up (gconstpointer src,
         gpointer      data)
{
  gchar l, u;

  l = GPOINTER_TO_INT (src);
  u = g_ascii_toupper (l);

466
  return GINT_TO_POINTER ((int)u);
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
}

static void
copy_test (void)
{
  GNode *root;
  GNode *copy;
  gchar *expected;

  root = g_node_new (C2P ('a'));
  g_node_append_data (root, C2P ('b'));
  g_node_append_data (root, C2P ('c'));
  g_node_append_data (root, C2P ('d'));

  expected = "abcd";
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);

  copy = g_node_copy (root);

  expected = "abcd";
  g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);

  g_node_destroy (copy);

  copy = g_node_copy_deep (root, copy_up, NULL);

  expected = "ABCD";
  g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);

  g_node_destroy (copy);

  g_node_destroy (root);
}

501 502 503 504
int
main (int   argc,
      char *argv[])
{
505 506 507 508 509
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/node/allocation", allocation_test);
  g_test_add_func ("/node/construction", construct_test);
  g_test_add_func ("/node/traversal", traversal_test);
510
  g_test_add_func ("/node/misc", misc_test);
511 512
  g_test_add_func ("/node/unlink", unlink_test);
  g_test_add_func ("/node/copy", copy_test);
513

514
  return g_test_run ();
515 516
}