/** * g_node_max_height: * @root: a #GNode * * Gets the maximum height of all branches beneath a #GNode. * This is the maximum distance from the #GNode to all leaf nodes. * * If @root is %NULL, 0 is returned. If @root has no children, * 1 is returned. If @root has children, 2 is returned. And so on. * * Returns: the maximum height of the tree beneath @root */ guint g_node_max_height (GNode *root) { GNode *child; guint max_height = 0; if (!root) return 0; child = root->children; while (child) { guint tmp_height; tmp_height = g_node_max_height (child); if (tmp_height > max_height) max_height = tmp_height; child = child->next; } return max_height + 1; }
static void g_node_test (void) { GNode *root; GNode *node; GNode *node_B; GNode *node_D; GNode *node_F; GNode *node_G; GNode *node_J; guint i; gchar *tstring; failed = FALSE; root = g_node_new (C2P ('A')); TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1); node_B = g_node_new (C2P ('B')); g_node_append (root, node_B); TEST (NULL, 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); node_F = g_node_new (C2P ('F')); g_node_append (root, node_F); TEST (NULL, 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')); 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'))); TEST (NULL, g_node_depth (root) == 1); TEST (NULL, g_node_max_height (root) == 4); TEST (NULL, g_node_depth (node_G->children->next) == 4); TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7); TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4); TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11); TEST (NULL, g_node_max_height (node_F) == 3); TEST (NULL, g_node_n_children (node_G) == 4); TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F); TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL); TEST (NULL, 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); TEST (NULL, P2C (node->data) == ('C' + i)); } for (i = 0; i < g_node_n_children (node_G); i++) TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i); /* we have built: A * / \ * B F * / | \ \ * C D E G * / /\ \ * H I J K * * for in-order traversal, 'G' is considered to be the "left" * child of 'F', which will cause 'F' to be the last node visited. */ tstring = NULL; g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0); g_free (tstring); tstring = NULL; g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0); g_free (tstring); tstring = NULL; g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0); g_free (tstring); tstring = NULL; g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0); g_free (tstring); tstring = NULL; g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "CDEHIJK") == 0); g_free (tstring); tstring = NULL; g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "ABFG") == 0); g_free (tstring); tstring = NULL; g_node_reverse_children (node_B); g_node_reverse_children (node_G); g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0); g_free (tstring); tstring = NULL; g_node_append (node_D, g_node_new (C2P ('L'))); g_node_append (node_D, g_node_new (C2P ('M'))); g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring); TEST (tstring, strcmp (tstring, "ABFEDCGLMKJIH") == 0); g_free (tstring); tstring = NULL; g_node_destroy (root); /* allocation tests */ root = g_node_new (NULL); node = root; for (i = 0; i < 2048; i++) { g_node_append (node, g_node_new (NULL)); if ((i%5) == 4) node = node->children->next; } TEST (NULL, g_node_max_height (root) > 100); TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048); g_node_destroy (root); if (failed) exit(1); }
END_TEST START_TEST(test_dbmail_mailbox_build_imap_search) { char *args; char **array; uint64_t idx = 0; gboolean sorted = 1; DbmailMailbox *mb, *mc, *md; // first case mb = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival cc date reverse from size subject to ) us-ascii " "HEADER FROM [email protected] SINCE 1-Feb-1994"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mb, array, &idx, sorted); dbmail_mailbox_free(mb); g_strfreev(array); // second case idx = 0; mc = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival ) ( cc ) us-ascii HEADER FROM [email protected] " "SINCE 1-Feb-1990"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(mc, array, &idx, sorted); dbmail_mailbox_free(mc); g_strfreev(array); // third case idx = 0; md = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("( arrival cc date reverse from size subject to ) us-ascii " "HEADER FROM test ( SINCE 1-Feb-1995 OR HEADER SUBJECT test " "HEADER SUBJECT foo )"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(md, array, &idx, sorted); fail_unless(g_node_max_height(g_node_get_root(md->search))==4, "build_search: tree too shallow"); dbmail_mailbox_free(md); g_strfreev(array); // fourth case idx = 0; md = dbmail_mailbox_new(get_mailbox_id("INBOX")); args = g_strdup("1,* ( arrival cc date reverse from size subject to ) us-ascii " "HEADER FROM test ( SINCE 1-Feb-1995 OR HEADER SUBJECT test " "HEADER SUBJECT foo )"); array = g_strsplit(args," ",0); g_free(args); dbmail_mailbox_build_imap_search(md, array, &idx, 1); fail_unless(g_node_max_height(g_node_get_root(md->search))==4, "build_search: tree too shallow"); dbmail_mailbox_free(md); g_strfreev(array); }