* nih/tests/test_tree.c (test_next, test_prev, test_next_pre) (test_prev_pre, test_next_post, test_pre_post): Add tests for a single-node tree, because we found a bug where it didn't work. * nih/tree.c (nih_tree_next): Bug fix; check there is a right node before checking whether we've returned from it, otherwise we could confuse it with starting at the top of the tree. (nih_tree_prev): Likewise check there is a left node before checking whether we've returned from it.
diff --git a/ChangeLog b/ChangeLog index e26e51b..5a0b777 100644 --- a/ChangeLog +++ b/ChangeLog
@@ -1,3 +1,14 @@ +2007-06-22 Scott James Remnant <scott@netsplit.com> + + * nih/tests/test_tree.c (test_next, test_prev, test_next_pre) + (test_prev_pre, test_next_post, test_pre_post): Add tests for a + single-node tree, because we found a bug where it didn't work. + * nih/tree.c (nih_tree_next): Bug fix; check there is a right node + before checking whether we've returned from it, otherwise we could + confuse it with starting at the top of the tree. + (nih_tree_prev): Likewise check there is a left node before checking + whether we've returned from it. + 2007-06-20 Scott James Remnant <scott@netsplit.com> * nih/config.c (nih_config_parse_stanza): Free name after use.
diff --git a/nih/tests/test_tree.c b/nih/tests/test_tree.c index 5b65220..4a86c3d 100644 --- a/nih/tests/test_tree.c +++ b/nih/tests/test_tree.c
@@ -530,6 +530,19 @@ for (i = 0; i < 12; i++) nih_free (node[i]); + + + /* Check that we can iterate a tree with a single node. */ + TEST_FEATURE ("with single node"); + node[0] = nih_tree_new (NULL); + + ptr = nih_tree_next (node[0], NULL); + TEST_EQ_P (ptr, node[0]); + + ptr = nih_tree_next (node[0], ptr); + TEST_EQ_P (ptr, NULL); + + nih_free (node[0]); } void @@ -712,6 +725,19 @@ for (i = 0; i < 12; i++) nih_free (node[i]); + + + /* Check that we can iterate a tree with a single node. */ + TEST_FEATURE ("with single node"); + node[0] = nih_tree_new (NULL); + + ptr = nih_tree_prev (node[0], NULL); + TEST_EQ_P (ptr, node[0]); + + ptr = nih_tree_prev (node[0], ptr); + TEST_EQ_P (ptr, NULL); + + nih_free (node[0]); } @@ -808,6 +834,19 @@ for (i = 0; i < 12; i++) nih_free (node[i]); + + + /* Check that we can iterate a tree with a single node. */ + TEST_FEATURE ("with single node"); + node[0] = nih_tree_new (NULL); + + ptr = nih_tree_next_pre (node[0], NULL); + TEST_EQ_P (ptr, node[0]); + + ptr = nih_tree_next_pre (node[0], ptr); + TEST_EQ_P (ptr, NULL); + + nih_free (node[0]); } void @@ -990,6 +1029,19 @@ for (i = 0; i < 12; i++) nih_free (node[i]); + + + /* Check that we can iterate a tree with a single node. */ + TEST_FEATURE ("with single node"); + node[0] = nih_tree_new (NULL); + + ptr = nih_tree_prev_pre (node[0], NULL); + TEST_EQ_P (ptr, node[0]); + + ptr = nih_tree_prev_pre (node[0], ptr); + TEST_EQ_P (ptr, NULL); + + nih_free (node[0]); } @@ -1086,6 +1138,19 @@ for (i = 0; i < 12; i++) nih_free (node[i]); + + + /* Check that we can iterate a tree with a single node. */ + TEST_FEATURE ("with single node"); + node[0] = nih_tree_new (NULL); + + ptr = nih_tree_next_post (node[0], NULL); + TEST_EQ_P (ptr, node[0]); + + ptr = nih_tree_next_post (node[0], ptr); + TEST_EQ_P (ptr, NULL); + + nih_free (node[0]); } void @@ -1268,6 +1333,19 @@ for (i = 0; i < 12; i++) nih_free (node[i]); + + + /* Check that we can iterate a tree with a single node. */ + TEST_FEATURE ("with single node"); + node[0] = nih_tree_new (NULL); + + ptr = nih_tree_prev_post (node[0], NULL); + TEST_EQ_P (ptr, node[0]); + + ptr = nih_tree_prev_post (node[0], ptr); + TEST_EQ_P (ptr, NULL); + + nih_free (node[0]); }
diff --git a/nih/tree.c b/nih/tree.c index fd6ff58..ea1ee28 100644 --- a/nih/tree.c +++ b/nih/tree.c
@@ -313,7 +313,7 @@ if ((prev == node->parent) && node->left) { node = node->left; - } else if (prev == node->right) { + } else if (node->right && (prev == node->right)) { if (node == tree) return NULL; @@ -368,7 +368,7 @@ if ((prev == node->parent) && node->right) { node = node->right; - } else if (prev == node->left) { + } else if (node->left && (prev == node->left)) { if (node == tree) return NULL;