GTree: debugging improvements

* Add a debug assert checking whether we have really removed all nodes in
g_tree_remove_all(),

* Print a "LEFT" and "RIGHT" headers before printing a particular tree
branch in g_tree_node_dump(),

* Make the whole thing actually buildable again in the debug mode by
conditionally providing g_tree_dump() declaration in glib/gtree.h.

Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
This commit is contained in:
Maciej S. Szmigiero 2020-08-04 21:23:54 +02:00 committed by Philip Withnall
parent 5017de6567
commit 260e80f1df
2 changed files with 28 additions and 5 deletions

View File

@ -66,8 +66,6 @@
* To destroy a #GTree, use g_tree_destroy(). * To destroy a #GTree, use g_tree_destroy().
**/ **/
#undef G_TREE_DEBUG
#define MAX_GTREE_HEIGHT 40 #define MAX_GTREE_HEIGHT 40
typedef struct _GTreeNode GTreeNode; typedef struct _GTreeNode GTreeNode;
@ -294,11 +292,22 @@ g_tree_remove_all (GTree *tree)
tree->value_destroy_func (node->value); tree->value_destroy_func (node->value);
g_slice_free (GTreeNode, node); g_slice_free (GTreeNode, node);
#ifdef G_TREE_DEBUG
g_assert (tree->nnodes > 0);
tree->nnodes--;
#endif
node = next; node = next;
} }
#ifdef G_TREE_DEBUG
g_assert (tree->nnodes == 0);
#endif
tree->root = NULL; tree->root = NULL;
#ifndef G_TREE_DEBUG
tree->nnodes = 0; tree->nnodes = 0;
#endif
} }
/** /**
@ -1388,17 +1397,22 @@ g_tree_node_dump (GTreeNode *node,
g_print ("%*s%c\n", indent, "", *(char *)node->key); g_print ("%*s%c\n", indent, "", *(char *)node->key);
if (node->left_child) if (node->left_child)
g_tree_node_dump (node->left, indent + 2); {
g_print ("%*sLEFT\n", indent, "");
g_tree_node_dump (node->left, indent + 2);
}
else if (node->left) else if (node->left)
g_print ("%*s<%c\n", indent + 2, "", *(char *)node->left->key); g_print ("%*s<%c\n", indent + 2, "", *(char *)node->left->key);
if (node->right_child) if (node->right_child)
g_tree_node_dump (node->right, indent + 2); {
g_print ("%*sRIGHT\n", indent, "");
g_tree_node_dump (node->right, indent + 2);
}
else if (node->right) else if (node->right)
g_print ("%*s>%c\n", indent + 2, "", *(char *)node->right->key); g_print ("%*s>%c\n", indent + 2, "", *(char *)node->right->key);
} }
void void
g_tree_dump (GTree *tree) g_tree_dump (GTree *tree)
{ {

View File

@ -33,6 +33,8 @@
G_BEGIN_DECLS G_BEGIN_DECLS
#undef G_TREE_DEBUG
typedef struct _GTree GTree; typedef struct _GTree GTree;
typedef gboolean (*GTraverseFunc) (gpointer key, typedef gboolean (*GTraverseFunc) (gpointer key,
@ -99,6 +101,13 @@ gint g_tree_height (GTree *tree);
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
gint g_tree_nnodes (GTree *tree); gint g_tree_nnodes (GTree *tree);
#ifdef G_TREE_DEBUG
/*< private >*/
#ifndef __GTK_DOC_IGNORE__
void g_tree_dump (GTree *tree);
#endif /* !__GTK_DOC_IGNORE__ */
#endif /* G_TREE_DEBUG */
G_END_DECLS G_END_DECLS
#endif /* __G_TREE_H__ */ #endif /* __G_TREE_H__ */