Blame glib/tests/node.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include "glib.h"
Packit ae235b
Packit ae235b
#define C2P(c)          ((gpointer) ((long) (c)))
Packit ae235b
#define P2C(p)          ((gchar) ((long) (p)))
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GString *s;
Packit ae235b
  gint count;
Packit ae235b
} CallbackData;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
node_build_string (GNode    *node,
Packit ae235b
                   gpointer  data)
Packit ae235b
{
Packit ae235b
  CallbackData *d = data;
Packit ae235b
Packit ae235b
  g_string_append_c (d->s, P2C (node->data));
Packit ae235b
Packit ae235b
  d->count--;
Packit ae235b
Packit ae235b
  if (d->count == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
    GTraverseType   traverse;
Packit ae235b
    GTraverseFlags  flags;
Packit ae235b
    gint            depth;
Packit ae235b
    gint            limit;
Packit ae235b
    const gchar    *expected;
Packit ae235b
} TraverseData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
traversal_test (void)
Packit ae235b
{
Packit ae235b
  GNode *root;
Packit ae235b
  GNode *node_B;
Packit ae235b
  GNode *node_C;
Packit ae235b
  GNode *node_D;
Packit ae235b
  GNode *node_E;
Packit ae235b
  GNode *node_F;
Packit ae235b
  GNode *node_G;
Packit ae235b
  GNode *node_J;
Packit ae235b
  GNode *n;
Packit ae235b
  TraverseData orders[] = {
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1, -1, "ABCDEFGHIJK" },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        1, -1, "A"           },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        2, -1, "ABF"         },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3, -1, "ABCDEFG"     },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3, -1, "ABCDEFG"     },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1, -1, "CDEBHIJKGFA" },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        1, -1, "A"           },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        2, -1, "BFA"         },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3, -1, "CDEBGFA"     },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1, -1, "CBDEAHGIJKF" },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        1, -1, "A"           },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        2, -1, "BAF"         },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3, -1, "CBDEAGF"     },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1, -1, "ABFCDEGHIJK" },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        1, -1, "A"           },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        2, -1, "ABF"         },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3, -1, "ABFCDEG"     },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_LEAFS,     -1, -1, "CDEHIJK"     },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, -1, -1, "ABFG"        },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  1, "A"           },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  2, "AB"          },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  3, "ABC"         },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  4, "ABCD"        },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  5, "ABCDE"       },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  6, "ABCDEF"      },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  7, "ABCDEFG"     },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  8, "ABCDEFGH"    },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1,  9, "ABCDEFGHI"   },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,       -1, 10, "ABCDEFGHIJ"  },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  1, "A"           },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  2, "AB"          },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  3, "ABC"         },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  4, "ABCD"        },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  5, "ABCDE"       },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  6, "ABCDEF"      },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  7, "ABCDEFG"     },
Packit ae235b
    { G_PRE_ORDER,   G_TRAVERSE_ALL,        3,  8, "ABCDEFG"     },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  1, "C"           },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  2, "CD"          },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  3, "CDE"         },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  4, "CDEB"        },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  5, "CDEBH"       },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  6, "CDEBHI"      },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  7, "CDEBHIJ"     },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  8, "CDEBHIJK"    },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1,  9, "CDEBHIJKG"   },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,       -1, 10, "CDEBHIJKGF"  },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  1, "C"           },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  2, "CD"          },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  3, "CDE"         },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  4, "CDEB"        },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  5, "CDEBG"       },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  6, "CDEBGF"      },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  7, "CDEBGFA"     },
Packit ae235b
    { G_POST_ORDER,  G_TRAVERSE_ALL,        3,  8, "CDEBGFA"     },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  1, "C"           },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  2, "CB"          },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  3, "CBD"         },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  4, "CBDE"        },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  5, "CBDEA"       },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  6, "CBDEAH"      },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  7, "CBDEAHG"     },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  8, "CBDEAHGI"    },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1,  9, "CBDEAHGIJ"   },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,       -1, 10, "CBDEAHGIJK"  },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  1, "C"           },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  2, "CB"          },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  3, "CBD"         },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  4, "CBDE"        },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  5, "CBDEA"       },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  6, "CBDEAG"      },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  7, "CBDEAGF"     },
Packit ae235b
    { G_IN_ORDER,    G_TRAVERSE_ALL,        3,  8, "CBDEAGF"     },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  1, "A"           },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  2, "AB"          },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  3, "ABF"         },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  4, "ABFC"        },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  5, "ABFCD"       },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  6, "ABFCDE"      },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  7, "ABFCDEG"     },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  8, "ABFCDEGH"    },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1,  9, "ABFCDEGHI"   },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,       -1, 10, "ABFCDEGHIJ"  },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  1, "A"           },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  2, "AB"          },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  3, "ABF"         },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  4, "ABFC"        },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  5, "ABFCD"       },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  6, "ABFCDE"      },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  7, "ABFCDEG"     },
Packit ae235b
    { G_LEVEL_ORDER, G_TRAVERSE_ALL,        3,  8, "ABFCDEG"     },
Packit ae235b
  };
Packit ae235b
  gint i;
Packit ae235b
  CallbackData data;
Packit ae235b
Packit ae235b
  root = g_node_new (C2P ('A'));
Packit ae235b
  node_B = g_node_new (C2P ('B'));
Packit ae235b
  g_node_append (root, node_B);
Packit ae235b
  g_node_append_data (node_B, C2P ('E'));
Packit ae235b
  g_node_prepend_data (node_B, C2P ('C'));
Packit ae235b
  node_D = g_node_new (C2P ('D'));
Packit ae235b
  g_node_insert (node_B, 1, node_D);
Packit ae235b
  node_F = g_node_new (C2P ('F'));
Packit ae235b
  g_node_append (root, node_F);
Packit ae235b
  node_G = g_node_new (C2P ('G'));
Packit ae235b
  g_node_append (node_F, node_G);
Packit ae235b
  node_J = g_node_new (C2P ('J'));
Packit ae235b
  g_node_prepend (node_G, node_J);
Packit ae235b
  g_node_insert (node_G, 42, g_node_new (C2P ('K')));
Packit ae235b
  g_node_insert_data (node_G, 0, C2P ('H'));
Packit ae235b
  g_node_insert (node_G, 1, g_node_new (C2P ('I')));
Packit ae235b
Packit ae235b
  /* we have built:                    A
Packit ae235b
   *                                 /   \
Packit ae235b
   *                               B       F
Packit ae235b
   *                             / | \       \
Packit ae235b
   *                           C   D   E       G
Packit ae235b
   *                                         / /\ \
Packit ae235b
   *                                        H I J  K
Packit ae235b
   *
Packit ae235b
   * for in-order traversal, 'G' is considered to be the "left"
Packit ae235b
   * child of 'F', which will cause 'F' to be the last node visited.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  node_C = node_B->children;
Packit ae235b
  node_E = node_D->next;
Packit ae235b
Packit ae235b
  n = g_node_last_sibling (node_C);
Packit ae235b
  g_assert (n == node_E);
Packit ae235b
  n = g_node_last_sibling (node_D);
Packit ae235b
  g_assert (n == node_E);
Packit ae235b
  n = g_node_last_sibling (node_E);
Packit ae235b
  g_assert (n == node_E);
Packit ae235b
Packit ae235b
  data.s = g_string_new ("");  
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (orders); i++)
Packit ae235b
    {
Packit ae235b
      g_string_set_size (data.s, 0);
Packit ae235b
      data.count = orders[i].limit;
Packit ae235b
      g_node_traverse (root, orders[i].traverse, orders[i].flags, orders[i].depth, node_build_string, &data);
Packit ae235b
      g_assert_cmpstr (data.s->str, ==,  orders[i].expected);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_node_reverse_children (node_B);
Packit ae235b
  g_node_reverse_children (node_G);
Packit ae235b
Packit ae235b
  g_string_set_size (data.s, 0);
Packit ae235b
  data.count = -1;
Packit ae235b
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &data);
Packit ae235b
  g_assert_cmpstr (data.s->str, ==, "ABFEDCGKJIH");
Packit ae235b
  
Packit ae235b
  g_node_append (node_D, g_node_new (C2P ('L')));
Packit ae235b
  g_node_insert (node_D, -1, g_node_new (C2P ('M')));
Packit ae235b
Packit ae235b
  g_string_set_size (data.s, 0);
Packit ae235b
  data.count = -1;
Packit ae235b
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &data);
Packit ae235b
  g_assert_cmpstr (data.s->str, ==, "ABFEDCGLMKJIH");
Packit ae235b
Packit ae235b
  g_node_destroy (root);
Packit ae235b
  g_string_free (data.s, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
construct_test (void)
Packit ae235b
{
Packit ae235b
  GNode *root;
Packit ae235b
  GNode *node;
Packit ae235b
  GNode *node_B;
Packit ae235b
  GNode *node_D;
Packit ae235b
  GNode *node_F;
Packit ae235b
  GNode *node_G;
Packit ae235b
  GNode *node_J;
Packit ae235b
  GNode *node_H;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  root = g_node_new (C2P ('A'));
Packit ae235b
  g_assert_cmpint (g_node_depth (root), ==, 1);
Packit ae235b
  g_assert_cmpint (g_node_max_height (root), ==, 1);
Packit ae235b
Packit ae235b
  node_B = g_node_new (C2P ('B'));
Packit ae235b
  g_node_append (root, node_B);
Packit ae235b
  g_assert (root->children == node_B);
Packit ae235b
Packit ae235b
  g_node_append_data (node_B, C2P ('E'));
Packit ae235b
  g_node_prepend_data (node_B, C2P ('C'));
Packit ae235b
  node_D = g_node_new (C2P ('D'));
Packit ae235b
  g_node_insert (node_B, 1, node_D);
Packit ae235b
Packit ae235b
  node_F = g_node_new (C2P ('F'));
Packit ae235b
  g_node_append (root, node_F);
Packit ae235b
  g_assert (root->children->next == node_F);
Packit ae235b
Packit ae235b
  node_G = g_node_new (C2P ('G'));
Packit ae235b
  g_node_append (node_F, node_G);
Packit ae235b
  node_J = g_node_new (C2P ('J'));
Packit ae235b
  g_node_insert_after (node_G, NULL, node_J);
Packit ae235b
  g_node_insert (node_G, 42, g_node_new (C2P ('K')));
Packit ae235b
  node_H = g_node_new (C2P ('H'));
Packit ae235b
  g_node_insert_after (node_G, NULL, node_H);
Packit ae235b
  g_node_insert (node_G, 1, g_node_new (C2P ('I')));
Packit ae235b
Packit ae235b
  /* we have built:                    A
Packit ae235b
   *                                 /   \
Packit ae235b
   *                               B       F
Packit ae235b
   *                             / | \       \
Packit ae235b
   *                           C   D   E       G
Packit ae235b
   *                                         / /\ \
Packit ae235b
   *                                       H  I  J  K
Packit ae235b
   */
Packit ae235b
  g_assert_cmpint (g_node_depth (root), ==, 1);
Packit ae235b
  g_assert_cmpint (g_node_max_height (root), ==, 4);
Packit ae235b
  g_assert_cmpint (g_node_depth (node_G->children->next), ==, 4);
Packit ae235b
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_LEAFS), ==, 7);
Packit ae235b
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS), ==, 4);
Packit ae235b
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 11);
Packit ae235b
  g_assert_cmpint (g_node_max_height (node_F), ==, 3);
Packit ae235b
  g_assert_cmpint (g_node_n_children (node_G), ==, 4);
Packit ae235b
  g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
Packit ae235b
  g_assert (g_node_find_child (node_G, G_TRAVERSE_LEAFS, C2P ('H')) == node_H);
Packit ae235b
  g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('H')) == NULL);
Packit ae235b
  g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
Packit ae235b
  g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
Packit ae235b
Packit ae235b
  for (i = 0; i < g_node_n_children (node_B); i++)
Packit ae235b
    {
Packit ae235b
      node = g_node_nth_child (node_B, i);
Packit ae235b
      g_assert_cmpint (P2C (node->data), ==, ('C' + i));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 0; i < g_node_n_children (node_G); i++)
Packit ae235b
    g_assert_cmpint (g_node_child_position (node_G, g_node_nth_child (node_G, i)), ==, i);
Packit ae235b
Packit ae235b
  g_node_destroy (root);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
allocation_test (void)
Packit ae235b
{
Packit ae235b
  GNode *root;
Packit ae235b
  GNode *node;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  root = g_node_new (NULL);
Packit ae235b
  node = root;
Packit ae235b
Packit ae235b
  for (i = 0; i < 2048; i++)
Packit ae235b
    {
Packit ae235b
      g_node_append (node, g_node_new (NULL));
Packit ae235b
      if ((i % 5) == 4)
Packit ae235b
        node = node->children->next;
Packit ae235b
    }
Packit ae235b
  g_assert_cmpint (g_node_max_height (root), >, 100);
Packit ae235b
  g_assert_cmpint (g_node_n_nodes (root, G_TRAVERSE_ALL), ==, 1 + 2048);
Packit ae235b
Packit ae235b
  g_node_destroy (root);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
misc_test (void)
Packit ae235b
{
Packit ae235b
  GNode *root;
Packit ae235b
  GNode *node_B;
Packit ae235b
  GNode *node_C;
Packit ae235b
  GNode *node_D;
Packit ae235b
  GNode *node_E;
Packit ae235b
  CallbackData data;
Packit ae235b
Packit ae235b
  root = g_node_new (C2P ('A'));
Packit ae235b
  node_B = g_node_new (C2P ('B'));
Packit ae235b
  g_node_append (root, node_B);
Packit ae235b
  node_D = g_node_new (C2P ('D'));
Packit ae235b
  g_node_append (root, node_D);
Packit ae235b
  node_C = g_node_new (C2P ('C'));
Packit ae235b
  g_node_insert_after (root, node_B, node_C);
Packit ae235b
  node_E = g_node_new (C2P ('E'));
Packit ae235b
  g_node_append (node_C, node_E);
Packit ae235b
Packit ae235b
  g_assert (g_node_get_root (node_E) == root);
Packit ae235b
  g_assert (g_node_is_ancestor (root, node_B));
Packit ae235b
  g_assert (g_node_is_ancestor (root, node_E));
Packit ae235b
  g_assert (!g_node_is_ancestor (node_B, node_D));
Packit ae235b
  g_assert (g_node_first_sibling (node_D) == node_B);
Packit ae235b
  g_assert (g_node_first_sibling (node_E) == node_E);
Packit ae235b
  g_assert (g_node_first_sibling (root) == root);
Packit ae235b
  g_assert_cmpint (g_node_child_index (root, C2P ('B')), ==, 0);
Packit ae235b
  g_assert_cmpint (g_node_child_index (root, C2P ('C')), ==, 1);
Packit ae235b
  g_assert_cmpint (g_node_child_index (root, C2P ('D')), ==, 2);
Packit ae235b
  g_assert_cmpint (g_node_child_index (root, C2P ('E')), ==, -1);
Packit ae235b
Packit ae235b
  data.s = g_string_new ("");
Packit ae235b
  data.count = -1;
Packit ae235b
  g_node_children_foreach (root, G_TRAVERSE_ALL, (GNodeForeachFunc)node_build_string, &data);
Packit ae235b
  g_assert_cmpstr (data.s->str, ==, "BCD");
Packit ae235b
Packit ae235b
  g_string_set_size (data.s, 0);
Packit ae235b
  data.count = -1;
Packit ae235b
  g_node_children_foreach (root, G_TRAVERSE_LEAVES, (GNodeForeachFunc)node_build_string, &data);
Packit ae235b
  g_assert_cmpstr (data.s->str, ==, "BD");
Packit ae235b
Packit ae235b
  g_string_set_size (data.s, 0);
Packit ae235b
  data.count = -1;
Packit ae235b
  g_node_children_foreach (root, G_TRAVERSE_NON_LEAVES, (GNodeForeachFunc)node_build_string, &data);
Packit ae235b
  g_assert_cmpstr (data.s->str, ==, "C");
Packit ae235b
  g_string_free (data.s, TRUE);
Packit ae235b
Packit ae235b
  g_node_destroy (root);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
check_order (GNode    *node,
Packit ae235b
             gpointer  data)
Packit ae235b
{
Packit ae235b
  gchar **expected = data;
Packit ae235b
  gchar d;
Packit ae235b
Packit ae235b
  d = GPOINTER_TO_INT (node->data);
Packit ae235b
  g_assert_cmpint (d, ==, **expected);
Packit ae235b
  (*expected)++;
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
unlink_test (void)
Packit ae235b
{
Packit ae235b
  GNode *root;
Packit ae235b
  GNode *node;
Packit ae235b
  GNode *bnode;
Packit ae235b
  GNode *cnode;
Packit ae235b
  gchar *expected;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   *        -------- a --------
Packit ae235b
   *       /         |          \
Packit ae235b
   *     b           c           d
Packit ae235b
   *   / | \       / | \       / | \
Packit ae235b
   * e   f   g   h   i   j   k   l   m
Packit ae235b
   *
Packit ae235b
   */
Packit ae235b
Packit ae235b
  root = g_node_new (C2P ('a'));
Packit ae235b
  node = bnode = g_node_append_data (root, C2P ('b'));
Packit ae235b
  g_node_append_data (node, C2P ('e'));
Packit ae235b
  g_node_append_data (node, C2P ('f'));
Packit ae235b
  g_node_append_data (node, C2P ('g'));
Packit ae235b
Packit ae235b
  node = cnode = g_node_append_data (root, C2P ('c'));
Packit ae235b
  g_node_append_data (node, C2P ('h'));
Packit ae235b
  g_node_append_data (node, C2P ('i'));
Packit ae235b
  g_node_append_data (node, C2P ('j'));
Packit ae235b
Packit ae235b
  node = g_node_append_data (root, C2P ('d'));
Packit ae235b
  g_node_append_data (node, C2P ('k'));
Packit ae235b
  g_node_append_data (node, C2P ('l'));
Packit ae235b
  g_node_append_data (node, C2P ('m'));
Packit ae235b
Packit ae235b
  g_node_unlink (cnode);
Packit ae235b
Packit ae235b
  expected = "abdefgklm";
Packit ae235b
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
Packit ae235b
Packit ae235b
  expected = "abd";
Packit ae235b
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, 1, check_order, &expected);
Packit ae235b
Packit ae235b
  expected = "chij";
Packit ae235b
  g_node_traverse (cnode, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
Packit ae235b
Packit ae235b
  g_node_destroy (bnode);
Packit ae235b
Packit ae235b
  expected = "adklm";
Packit ae235b
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
Packit ae235b
Packit ae235b
  g_node_destroy (root);
Packit ae235b
  g_node_destroy (cnode);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
copy_up (gconstpointer src,
Packit ae235b
         gpointer      data)
Packit ae235b
{
Packit ae235b
  gchar l, u;
Packit ae235b
Packit ae235b
  l = GPOINTER_TO_INT (src);
Packit ae235b
  u = g_ascii_toupper (l);
Packit ae235b
Packit ae235b
  return GINT_TO_POINTER ((int)u);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
copy_test (void)
Packit ae235b
{
Packit ae235b
  GNode *root;
Packit ae235b
  GNode *copy;
Packit ae235b
  gchar *expected;
Packit ae235b
Packit ae235b
  root = g_node_new (C2P ('a'));
Packit ae235b
  g_node_append_data (root, C2P ('b'));
Packit ae235b
  g_node_append_data (root, C2P ('c'));
Packit ae235b
  g_node_append_data (root, C2P ('d'));
Packit ae235b
Packit ae235b
  expected = "abcd";
Packit ae235b
  g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
Packit ae235b
Packit ae235b
  copy = g_node_copy (root);
Packit ae235b
Packit ae235b
  expected = "abcd";
Packit ae235b
  g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
Packit ae235b
Packit ae235b
  g_node_destroy (copy);
Packit ae235b
Packit ae235b
  copy = g_node_copy_deep (root, copy_up, NULL);
Packit ae235b
Packit ae235b
  expected = "ABCD";
Packit ae235b
  g_node_traverse (copy, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, check_order, &expected);
Packit ae235b
Packit ae235b
  g_node_destroy (copy);
Packit ae235b
Packit ae235b
  g_node_destroy (root);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/node/allocation", allocation_test);
Packit ae235b
  g_test_add_func ("/node/construction", construct_test);
Packit ae235b
  g_test_add_func ("/node/traversal", traversal_test);
Packit ae235b
  g_test_add_func ("/node/misc", misc_test);
Packit ae235b
  g_test_add_func ("/node/unlink", unlink_test);
Packit ae235b
  g_test_add_func ("/node/copy", copy_test);
Packit ae235b
Packit ae235b
  return g_test_run ();
Packit ae235b
}
Packit ae235b