2019-01-07 03:28:09 +00:00
|
|
|
/* GtkTreeRBTree tests.
|
2011-11-21 21:26:42 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011, Red Hat, Inc.
|
|
|
|
* Authors: Benjamin Otte <otte@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-11-21 21:26:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
#include "../../gtk/gtktreerbtreeprivate.h"
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
/* gtk_tree_rbtree_test */
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
static guint
|
2019-01-07 03:28:09 +00:00
|
|
|
get_total_count (GtkTreeRBNode *node)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
guint child_total = 0;
|
|
|
|
|
|
|
|
child_total += (guint) node->left->total_count;
|
|
|
|
child_total += (guint) node->right->total_count;
|
|
|
|
|
|
|
|
if (node->children)
|
|
|
|
child_total += (guint) node->children->root->total_count;
|
|
|
|
|
|
|
|
return child_total + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
2019-01-07 03:28:09 +00:00
|
|
|
count_total (GtkTreeRBTree *tree,
|
|
|
|
GtkTreeRBNode *node)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
guint res;
|
2019-01-07 03:28:09 +00:00
|
|
|
|
|
|
|
if (gtk_tree_rbtree_is_nil (node))
|
2011-11-21 21:26:42 +00:00
|
|
|
return 0;
|
2019-01-07 03:28:09 +00:00
|
|
|
|
2011-11-21 21:26:42 +00:00
|
|
|
res =
|
|
|
|
count_total (tree, node->left) +
|
|
|
|
count_total (tree, node->right) +
|
2019-01-07 03:28:09 +00:00
|
|
|
(guint) 1 +
|
2011-11-21 21:26:42 +00:00
|
|
|
(node->children ? count_total (node->children, node->children->root) : 0);
|
|
|
|
|
|
|
|
if (res != node->total_count)
|
|
|
|
g_print ("total count incorrect for node\n");
|
|
|
|
|
|
|
|
if (get_total_count (node) != node->total_count)
|
|
|
|
g_error ("Node has incorrect total count %u, should be %u", node->total_count, get_total_count (node));
|
2019-01-07 03:28:09 +00:00
|
|
|
|
2011-11-21 21:26:42 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2019-01-07 03:28:09 +00:00
|
|
|
_count_nodes (GtkTreeRBTree *tree,
|
|
|
|
GtkTreeRBNode *node)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
gint res;
|
2019-01-07 03:28:09 +00:00
|
|
|
if (gtk_tree_rbtree_is_nil (node))
|
2011-11-21 21:26:42 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
g_assert (node->left);
|
|
|
|
g_assert (node->right);
|
|
|
|
|
|
|
|
res = (_count_nodes (tree, node->left) +
|
|
|
|
_count_nodes (tree, node->right) + 1);
|
|
|
|
|
|
|
|
if (res != node->count)
|
|
|
|
g_print ("Tree failed\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test_height (GtkTreeRBTree *tree,
|
|
|
|
GtkTreeRBNode *node)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
gint computed_offset = 0;
|
|
|
|
|
|
|
|
/* This whole test is sort of a useless truism. */
|
2019-01-07 03:28:09 +00:00
|
|
|
|
|
|
|
if (!gtk_tree_rbtree_is_nil (node->left))
|
2011-11-21 21:26:42 +00:00
|
|
|
computed_offset += node->left->offset;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->right))
|
2011-11-21 21:26:42 +00:00
|
|
|
computed_offset += node->right->offset;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (node->children && !gtk_tree_rbtree_is_nil (node->children->root))
|
2011-11-21 21:26:42 +00:00
|
|
|
computed_offset += node->children->root->offset;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (GTK_TREE_RBNODE_GET_HEIGHT (node) + computed_offset != node->offset)
|
2016-02-28 17:22:55 +00:00
|
|
|
g_error ("node has broken offset");
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->left))
|
|
|
|
gtk_tree_rbtree_test_height (tree, node->left);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->right))
|
|
|
|
gtk_tree_rbtree_test_height (tree, node->right);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (node->children && !gtk_tree_rbtree_is_nil (node->children->root))
|
|
|
|
gtk_tree_rbtree_test_height (node->children, node->children->root);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test_dirty (GtkTreeRBTree *tree,
|
|
|
|
GtkTreeRBNode *node,
|
|
|
|
gint expected_dirtyness)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
if (expected_dirtyness)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
g_assert (GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_COLUMN_INVALID) ||
|
|
|
|
GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_INVALID) ||
|
|
|
|
GTK_TREE_RBNODE_FLAG_SET (node->left, GTK_TREE_RBNODE_DESCENDANTS_INVALID) ||
|
|
|
|
GTK_TREE_RBNODE_FLAG_SET (node->right, GTK_TREE_RBNODE_DESCENDANTS_INVALID) ||
|
|
|
|
(node->children && GTK_TREE_RBNODE_FLAG_SET (node->children->root, GTK_TREE_RBNODE_DESCENDANTS_INVALID)));
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
g_assert (!GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_COLUMN_INVALID) &&
|
|
|
|
!GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_INVALID));
|
|
|
|
if (!gtk_tree_rbtree_is_nil (node->left))
|
|
|
|
g_assert (!GTK_TREE_RBNODE_FLAG_SET (node->left, GTK_TREE_RBNODE_DESCENDANTS_INVALID));
|
|
|
|
if (!gtk_tree_rbtree_is_nil (node->right))
|
|
|
|
g_assert (!GTK_TREE_RBNODE_FLAG_SET (node->right, GTK_TREE_RBNODE_DESCENDANTS_INVALID));
|
2011-11-21 21:26:42 +00:00
|
|
|
if (node->children != NULL)
|
2019-01-07 03:28:09 +00:00
|
|
|
g_assert (!GTK_TREE_RBNODE_FLAG_SET (node->children->root, GTK_TREE_RBNODE_DESCENDANTS_INVALID));
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->left))
|
|
|
|
gtk_tree_rbtree_test_dirty (tree, node->left, GTK_TREE_RBNODE_FLAG_SET (node->left, GTK_TREE_RBNODE_DESCENDANTS_INVALID));
|
|
|
|
if (!gtk_tree_rbtree_is_nil (node->right))
|
|
|
|
gtk_tree_rbtree_test_dirty (tree, node->right, GTK_TREE_RBNODE_FLAG_SET (node->right, GTK_TREE_RBNODE_DESCENDANTS_INVALID));
|
|
|
|
if (node->children != NULL && !gtk_tree_rbtree_is_nil (node->children->root))
|
|
|
|
gtk_tree_rbtree_test_dirty (node->children, node->children->root, GTK_TREE_RBNODE_FLAG_SET (node->children->root, GTK_TREE_RBNODE_DESCENDANTS_INVALID));
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
static void gtk_tree_rbtree_test_structure (GtkTreeRBTree *tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
static guint
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test_structure_helper (GtkTreeRBTree *tree,
|
|
|
|
GtkTreeRBNode *node)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
guint left_blacks, right_blacks;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
g_assert (!gtk_tree_rbtree_is_nil (node));
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
g_assert (node->left != NULL);
|
|
|
|
g_assert (node->right != NULL);
|
|
|
|
g_assert (node->parent != NULL);
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->left))
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
g_assert (node->left->parent == node);
|
2019-01-07 03:28:09 +00:00
|
|
|
left_blacks = gtk_tree_rbtree_test_structure_helper (tree, node->left);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
left_blacks = 0;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->right))
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
g_assert (node->right->parent == node);
|
2019-01-07 03:28:09 +00:00
|
|
|
right_blacks = gtk_tree_rbtree_test_structure_helper (tree, node->right);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
right_blacks = 0;
|
|
|
|
|
|
|
|
if (node->children != NULL)
|
|
|
|
{
|
|
|
|
g_assert (node->children->parent_tree == tree);
|
|
|
|
g_assert (node->children->parent_node == node);
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test_structure (node->children);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (left_blacks == right_blacks);
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
return left_blacks + (GTK_TREE_RBNODE_GET_COLOR (node) == GTK_TREE_RBNODE_BLACK ? 1 : 0);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test_structure (GtkTreeRBTree *tree)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
g_assert (tree->root);
|
2019-01-07 03:28:09 +00:00
|
|
|
if (gtk_tree_rbtree_is_nil (tree->root))
|
2011-11-21 21:26:42 +00:00
|
|
|
return;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
g_assert (gtk_tree_rbtree_is_nil (tree->root->parent));
|
|
|
|
gtk_tree_rbtree_test_structure_helper (tree, tree->root);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 17:30:50 +00:00
|
|
|
static void
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test (GtkTreeRBTree *tree)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tmp_tree;
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
if (tree == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Test the entire tree */
|
|
|
|
tmp_tree = tree;
|
|
|
|
while (tmp_tree->parent_tree)
|
|
|
|
tmp_tree = tmp_tree->parent_tree;
|
2019-01-07 03:28:09 +00:00
|
|
|
|
|
|
|
if (gtk_tree_rbtree_is_nil (tmp_tree->root))
|
2011-11-21 21:26:42 +00:00
|
|
|
return;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test_structure (tmp_tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
g_assert ((_count_nodes (tmp_tree, tmp_tree->root->left) +
|
2019-01-07 03:28:09 +00:00
|
|
|
_count_nodes (tmp_tree, tmp_tree->root->right) + 1) == tmp_tree->root->count);
|
|
|
|
|
|
|
|
gtk_tree_rbtree_test_height (tmp_tree, tmp_tree->root);
|
|
|
|
gtk_tree_rbtree_test_dirty (tmp_tree, tmp_tree->root, GTK_TREE_RBNODE_FLAG_SET (tmp_tree->root, GTK_TREE_RBNODE_DESCENDANTS_INVALID));
|
2011-11-21 21:26:42 +00:00
|
|
|
g_assert (count_total (tmp_tree, tmp_tree->root) == tmp_tree->root->total_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* gtk_rbtree_print() - unused, for debugging only */
|
|
|
|
|
|
|
|
static void
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_rbtree_print_node (GtkTreeRBTree *tree,
|
|
|
|
GtkTreeRBNode *node,
|
|
|
|
gint depth)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
for (i = 0; i < depth; i++)
|
|
|
|
g_print ("\t");
|
|
|
|
|
|
|
|
g_print ("(%p - %s) (Offset %d) (Parity %d) (Validity %d%d%d)\n",
|
2019-01-07 03:28:09 +00:00
|
|
|
node,
|
|
|
|
(GTK_TREE_RBNODE_GET_COLOR (node) == GTK_TREE_RBNODE_BLACK) ? "BLACK" : " RED ",
|
|
|
|
node->offset,
|
|
|
|
node->total_count,
|
|
|
|
(GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_DESCENDANTS_INVALID)) ? 1 : 0,
|
|
|
|
(GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_INVALID)) ? 1 : 0,
|
|
|
|
(GTK_TREE_RBNODE_FLAG_SET (node, GTK_TREE_RBNODE_COLUMN_INVALID)) ? 1 : 0);
|
2011-11-21 21:26:42 +00:00
|
|
|
if (node->children != NULL)
|
|
|
|
{
|
|
|
|
g_print ("Looking at child.\n");
|
|
|
|
gtk_rbtree_print_node (node->children, node->children->root, depth + 1);
|
|
|
|
g_print ("Done looking at child.\n");
|
|
|
|
}
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->left))
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_rbtree_print_node (tree, node->left, depth + 1);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_is_nil (node->right))
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_rbtree_print_node (tree, node->right, depth + 1);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not static so the debugger finds it. */
|
2019-01-07 03:28:09 +00:00
|
|
|
void gtk_rbtree_print (GtkTreeRBTree *tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
void
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_rbtree_print (GtkTreeRBTree *tree)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
if (gtk_tree_rbtree_is_nil (tree->root))
|
2011-11-21 21:26:42 +00:00
|
|
|
g_print ("Empty tree...\n");
|
|
|
|
else
|
|
|
|
gtk_rbtree_print_node (tree, tree->root, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* actual tests */
|
|
|
|
|
|
|
|
static guint
|
2019-01-07 03:28:09 +00:00
|
|
|
append_elements (GtkTreeRBTree *tree,
|
|
|
|
guint depth,
|
|
|
|
guint elements_per_depth,
|
|
|
|
gboolean check,
|
|
|
|
guint height)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBNode *node;
|
2011-11-21 21:26:42 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_assert (depth > 0);
|
|
|
|
|
|
|
|
node = NULL;
|
|
|
|
depth--;
|
|
|
|
|
|
|
|
for (i = 0; i < elements_per_depth; i++)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
node = gtk_tree_rbtree_insert_after (tree, node, ++height, TRUE);
|
2011-11-21 21:26:42 +00:00
|
|
|
if (depth)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
node->children = gtk_tree_rbtree_new ();
|
2011-11-21 21:26:42 +00:00
|
|
|
node->children->parent_tree = tree;
|
|
|
|
node->children->parent_node = node;
|
2011-11-22 14:35:53 +00:00
|
|
|
height = append_elements (node->children, depth, elements_per_depth, check, height);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
2011-11-22 14:35:53 +00:00
|
|
|
if (check)
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
static GtkTreeRBTree *
|
|
|
|
create_rbtree (guint depth,
|
|
|
|
guint elements_per_depth,
|
2011-11-22 14:35:53 +00:00
|
|
|
gboolean check)
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
tree = gtk_tree_rbtree_new ();
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2011-11-22 14:35:53 +00:00
|
|
|
append_elements (tree, depth, elements_per_depth, check, 0);
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_create (void)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2011-11-22 14:35:53 +00:00
|
|
|
tree = create_rbtree (5, 5, TRUE);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_free (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_insert_after (void)
|
|
|
|
{
|
|
|
|
guint i;
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
|
|
|
GtkTreeRBNode *node;
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
tree = gtk_tree_rbtree_new ();
|
2011-11-21 21:26:42 +00:00
|
|
|
node = NULL;
|
|
|
|
|
|
|
|
for (i = 1; i <= 100; i++)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
node = gtk_tree_rbtree_insert_after (tree, node, i, TRUE);
|
|
|
|
gtk_tree_rbtree_test (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
g_assert (tree->root->count == i);
|
|
|
|
g_assert (tree->root->total_count == i);
|
|
|
|
g_assert (tree->root->offset == i * (i + 1) / 2);
|
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_free (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_insert_before (void)
|
|
|
|
{
|
|
|
|
guint i;
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
|
|
|
GtkTreeRBNode *node;
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
tree = gtk_tree_rbtree_new ();
|
2011-11-21 21:26:42 +00:00
|
|
|
node = NULL;
|
|
|
|
|
|
|
|
for (i = 1; i <= 100; i++)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
node = gtk_tree_rbtree_insert_before (tree, node, i, TRUE);
|
|
|
|
gtk_tree_rbtree_test (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
g_assert (tree->root->count == i);
|
|
|
|
g_assert (tree->root->total_count == i);
|
|
|
|
g_assert (tree->root->offset == i * (i + 1) / 2);
|
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_free (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_remove_node (void)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2011-11-22 14:35:53 +00:00
|
|
|
tree = create_rbtree (3, 16, g_test_thorough ());
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
while (tree->root->count > 1)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *find_tree;
|
|
|
|
GtkTreeRBNode *find_node;
|
2011-11-21 21:26:42 +00:00
|
|
|
guint i;
|
2019-01-07 03:28:09 +00:00
|
|
|
|
2011-11-21 21:26:42 +00:00
|
|
|
i = g_test_rand_int_range (0, tree->root->total_count);
|
2019-01-07 03:28:09 +00:00
|
|
|
if (!gtk_tree_rbtree_find_index (tree, i, &find_tree, &find_node))
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
/* We search an available index, so we mustn't fail. */
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
2019-01-07 03:28:09 +00:00
|
|
|
|
|
|
|
gtk_tree_rbtree_test (find_tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
if (find_tree->root->count == 1)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_remove (find_tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
else
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_remove_node (find_tree, find_node);
|
|
|
|
gtk_tree_rbtree_test (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_free (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_remove_root (void)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
|
|
|
GtkTreeRBNode *node;
|
|
|
|
|
|
|
|
tree = gtk_tree_rbtree_new ();
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
node = gtk_tree_rbtree_insert_after (tree, NULL, 1, TRUE);
|
|
|
|
gtk_tree_rbtree_insert_after (tree, node, 2, TRUE);
|
|
|
|
gtk_tree_rbtree_insert_before (tree, node, 3, TRUE);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_remove_node (tree, node);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_free (tree);
|
2011-11-21 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2011-11-22 22:14:09 +00:00
|
|
|
static gint *
|
|
|
|
fisher_yates_shuffle (guint n_items)
|
|
|
|
{
|
|
|
|
gint *list;
|
|
|
|
guint i, j;
|
|
|
|
|
|
|
|
list = g_new (gint, n_items);
|
|
|
|
|
|
|
|
for (i = 0; i < n_items; i++)
|
|
|
|
{
|
|
|
|
j = g_random_int_range (0, i + 1);
|
|
|
|
list[i] = list[j];
|
|
|
|
list[j] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
static GtkTreeRBTree *
|
|
|
|
create_unsorted_tree (gint *order,
|
|
|
|
guint n)
|
2011-11-22 22:14:09 +00:00
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
|
|
|
GtkTreeRBNode *node;
|
2011-11-22 22:14:09 +00:00
|
|
|
guint i;
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
tree = gtk_tree_rbtree_new ();
|
2011-11-22 22:14:09 +00:00
|
|
|
node = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
node = gtk_tree_rbtree_insert_after (tree, node, 0, TRUE);
|
2011-11-22 22:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
node = gtk_tree_rbtree_find_count (tree, order[i] + 1);
|
|
|
|
gtk_tree_rbtree_node_set_height (tree, node, i);
|
2011-11-22 22:14:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test (tree);
|
2011-11-22 22:14:09 +00:00
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_reorder (void)
|
|
|
|
{
|
|
|
|
guint n = g_test_perf () ? 1000000 : 100;
|
2019-01-07 03:28:09 +00:00
|
|
|
GtkTreeRBTree *tree;
|
|
|
|
GtkTreeRBNode *node;
|
2011-11-22 22:14:09 +00:00
|
|
|
gint *reorder;
|
|
|
|
guint i;
|
|
|
|
double elapsed;
|
|
|
|
|
|
|
|
reorder = fisher_yates_shuffle (n);
|
|
|
|
tree = create_unsorted_tree (reorder, n);
|
|
|
|
|
|
|
|
g_test_timer_start ();
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_reorder (tree, reorder, n);
|
2011-11-22 22:14:09 +00:00
|
|
|
|
|
|
|
elapsed = g_test_timer_elapsed ();
|
|
|
|
if (g_test_perf ())
|
|
|
|
g_test_minimized_result (elapsed, "reordering rbtree with %u items: %gsec", n, elapsed);
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_test (tree);
|
2011-11-22 22:14:09 +00:00
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
for (node = gtk_tree_rbtree_first (tree), i = 0;
|
2011-11-22 22:14:09 +00:00
|
|
|
node != NULL;
|
2019-01-07 03:28:09 +00:00
|
|
|
node = gtk_tree_rbtree_next (tree, node), i++)
|
2011-11-22 22:14:09 +00:00
|
|
|
{
|
2019-01-07 03:28:09 +00:00
|
|
|
g_assert (GTK_TREE_RBNODE_GET_HEIGHT (node) == i);
|
2011-11-22 22:14:09 +00:00
|
|
|
}
|
|
|
|
g_assert (i == n);
|
|
|
|
|
2019-01-07 03:28:09 +00:00
|
|
|
gtk_tree_rbtree_free (tree);
|
2016-02-26 20:50:08 +00:00
|
|
|
|
|
|
|
g_free (reorder);
|
2011-11-22 22:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-11-21 21:26:42 +00:00
|
|
|
int
|
2019-01-07 03:28:09 +00:00
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
2011-11-21 21:26:42 +00:00
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
setlocale (LC_ALL, "C");
|
|
|
|
g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=%s");
|
|
|
|
|
|
|
|
g_test_add_func ("/rbtree/create", test_create);
|
|
|
|
g_test_add_func ("/rbtree/insert_after", test_insert_after);
|
|
|
|
g_test_add_func ("/rbtree/insert_before", test_insert_before);
|
|
|
|
g_test_add_func ("/rbtree/remove_node", test_remove_node);
|
|
|
|
g_test_add_func ("/rbtree/remove_root", test_remove_root);
|
2011-11-22 22:14:09 +00:00
|
|
|
g_test_add_func ("/rbtree/reorder", test_reorder);
|
2011-11-21 21:26:42 +00:00
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|