diff --git a/ChangeLog b/ChangeLog index c3a2cccd1..ed2726620 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-03-17 Matthias Clasen + + * glib/gsequence.[hc]: + * glib/glib.symbols: + * tests/sequence-test.c: Move the consistency + checks to the test. + 2007-03-16 Matthias Clasen * configure.in: Bump version diff --git a/glib/glib.symbols b/glib/glib.symbols index e706b90e2..5fc3163a5 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -963,7 +963,6 @@ g_sequence_iter_move g_sequence_iter_get_sequence g_sequence_iter_compare g_sequence_range_get_midpoint -g_sequence_self_test_internal_to_glib_dont_use #endif #endif diff --git a/glib/gsequence.c b/glib/gsequence.c index 3f97f548d..e39078cbc 100644 --- a/glib/gsequence.c +++ b/glib/gsequence.c @@ -1735,61 +1735,6 @@ node_insert_sorted (GSequenceNode *node, node_insert_before (closest, new); } -/* Self-test function */ -static int -count_nodes (GSequenceNode *node) -{ - if (!node) - return 0; - - return count_nodes (node->left) + count_nodes (node->right) + 1; -} - -static void -check_node (GSequenceNode *node) -{ - if (node) - { - g_assert (node->parent != node); - if (node->parent) - g_assert (node->parent->left == node || node->parent->right == node); - g_assert (node->n_nodes == count_nodes (node)); - if (node->left) - g_assert (get_priority (node) >= get_priority (node->left)); - if (node->right) - g_assert (get_priority (node) >= get_priority (node->right)); - check_node (node->left); - check_node (node->right); - } -} - -static gint -compute_height (GSequenceNode *node) -{ - int left, right; - - if (!node) - return 0; - - left = compute_height (node->left); - right = compute_height (node->right); - - return MAX (left, right) + 1; -} - -void -g_sequence_self_test_internal_to_glib_dont_use (GSequence *seq) -{ - GSequenceNode *node = find_root (seq->end_node); - - check_node (node); - - node = node_get_last (node); - - g_assert (seq->end_node == node); - g_assert (node->data == seq); - -} #define __G_SEQUENCE_C__ #include "galiasdef.c" diff --git a/glib/gsequence.h b/glib/gsequence.h index accdec122..ffac6c62e 100644 --- a/glib/gsequence.h +++ b/glib/gsequence.h @@ -118,7 +118,4 @@ GSequenceIter *g_sequence_range_get_midpoint (GSequenceIter *begin, GSequenceIter *end); -/* Private */ -void g_sequence_self_test_internal_to_glib_dont_use (GSequence *seq); - #endif /* __G_SEQUENCE_H__ */ diff --git a/tests/sequence-test.c b/tests/sequence-test.c index 2458f88f2..2a88c7df4 100644 --- a/tests/sequence-test.c +++ b/tests/sequence-test.c @@ -2,6 +2,78 @@ #include #include +/* Keep this in sync with gsequence.c !!! */ +typedef struct _GSequenceNode GSequenceNode; + +struct _GSequence +{ + GSequenceNode * end_node; + GDestroyNotify data_destroy_notify; + gboolean access_prohibited; + GSequence * real_sequence; +}; + +struct _GSequenceNode +{ + gint n_nodes; + GSequenceNode * parent; + GSequenceNode * left; + GSequenceNode * right; + gpointer data; +}; + +static guint +get_priority (GSequenceNode *node) +{ + guint key = GPOINTER_TO_UINT (node); + + key = (key << 15) - key - 1; + key = key ^ (key >> 12); + key = key + (key << 2); + key = key ^ (key >> 4); + key = key + (key << 3) + (key << 11); + key = key ^ (key >> 16); + + return key? key : 1; +} + +static void +check_node (GSequenceNode *node) +{ + if (node) + { + g_assert (node->parent != node); + if (node->parent) + g_assert (node->parent->left == node || node->parent->right == node); + g_assert (node->n_nodes == 1 + (node->left ? node->left->n_nodes : 0) + (node->right ? node->right->n_nodes : 0)); + if (node->left) + g_assert (get_priority (node) >= get_priority (node->left)); + if (node->right) + g_assert (get_priority (node) >= get_priority (node->right)); + check_node (node->left); + check_node (node->right); + } +} + +void +g_sequence_check (GSequence *seq) +{ + GSequenceNode *node = seq->end_node; + + while (node->parent) + node = node->parent; + + check_node (node); + + while (node->right) + node = node->right; + + g_assert (seq->end_node == node); + g_assert (node->data == seq); + +} + + enum { NEW, FREE, GET_LENGTH, FOREACH, FOREACH_RANGE, SORT, SORT_ITER, @@ -35,7 +107,7 @@ typedef struct int number; } Item; -void g_sequence_self_test_internal_to_glib_dont_use (GSequence *sequence); +void g_sequence_check (GSequence *sequence); static Item * fix_pointer (gconstpointer data) @@ -56,7 +128,7 @@ check_integrity (SequenceInfo *info) GSequenceIter *iter; int i; - g_sequence_self_test_internal_to_glib_dont_use (info->sequence); + g_sequence_check (info->sequence); if (g_sequence_get_length (info->sequence) != info->n_items) g_print ("%d %d\n", @@ -1148,7 +1220,7 @@ test_insert_sorted_non_pointer (void) compare_iter, NULL); } - g_sequence_self_test_internal_to_glib_dont_use (seq); + g_sequence_check (seq); g_sequence_free (seq); } @@ -1168,21 +1240,21 @@ test_stable_sort (void) for (i = 0; i < N_ITEMS; ++i) { iters[i] = g_sequence_append (seq, GINT_TO_POINTER (3000)); - g_sequence_self_test_internal_to_glib_dont_use (seq); + g_sequence_check (seq); g_assert (g_sequence_iter_get_sequence (iters[i]) == seq); } i = 0; iter = g_sequence_get_begin_iter (seq); g_assert (g_sequence_iter_get_sequence (iter) == seq); - g_sequence_self_test_internal_to_glib_dont_use (seq); + g_sequence_check (seq); while (!g_sequence_iter_is_end (iter)) { g_assert (g_sequence_iter_get_sequence (iters[i]) == seq); g_assert (iters[i++] == iter); iter = g_sequence_iter_next (iter); - g_sequence_self_test_internal_to_glib_dont_use (seq); + g_sequence_check (seq); } g_sequence_sort (seq, compare, NULL); @@ -1195,14 +1267,14 @@ test_stable_sort (void) g_assert (iters[i] == iter); iter = g_sequence_iter_next (iter); - g_sequence_self_test_internal_to_glib_dont_use (seq); + g_sequence_check (seq); i++; } for (i = N_ITEMS - 1; i >= 0; --i) { - g_sequence_self_test_internal_to_glib_dont_use (seq); + g_sequence_check (seq); g_assert (g_sequence_iter_get_sequence (iters[i]) == seq); g_assert (g_sequence_get_end_iter (seq) != iters[i]); g_sequence_sort_changed (iters[i], compare, NULL); @@ -1215,7 +1287,7 @@ test_stable_sort (void) g_assert (iters[i++] == iter); iter = g_sequence_iter_next (iter); - g_sequence_self_test_internal_to_glib_dont_use (seq); + g_sequence_check (seq); } }