summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-09-20 17:06:55 -0600
committermo khan <mo.khan@gmail.com>2020-09-20 17:06:55 -0600
commit2a9f5f30b3a0bd62cc9889d291a7dd18dc258e51 (patch)
tree1d5aaa578f89ac51d096ca2fa742f3797727640b /src
parente1466ec3d4b2e641329d62e382f036e8d6ad8395 (diff)
test: ensure rb node parents are equal
Diffstat (limited to 'src')
-rw-r--r--src/03/rb_tree.c9
-rw-r--r--src/03/rb_tree_test.c14
2 files changed, 23 insertions, 0 deletions
diff --git a/src/03/rb_tree.c b/src/03/rb_tree.c
index d1861f5..802791e 100644
--- a/src/03/rb_tree.c
+++ b/src/03/rb_tree.c
@@ -231,6 +231,15 @@ bool rb_equals(RBTree *tree, RBTree *other_tree) {
if (!tree || !other_tree)
return tree == other_tree;
+ if (tree->parent && !other_tree->parent)
+ return false;
+
+ if (!tree->parent && other_tree->parent)
+ return false;
+
+ if (tree->parent && tree->parent->value != other_tree->parent->value)
+ return false;
+
return tree->value == other_tree->value
&& tree->colour == other_tree->colour
&& rb_equals(tree->left, other_tree->left)
diff --git a/src/03/rb_tree_test.c b/src/03/rb_tree_test.c
index 1bf4e5a..3fca748 100644
--- a/src/03/rb_tree_test.c
+++ b/src/03/rb_tree_test.c
@@ -226,6 +226,19 @@ Ensure(equals_returns_false_when_root_and_right_subtree_are_not_equal) {
assert_that(rb_equals(tree, other_tree), is_equal_to(false));
}
+Ensure(equals_returns_false_when_parent_is_not_equal) {
+ RBTree *tree = rb_tree_initialize(20);
+ tree = rb_tree_insert(tree, 30);
+
+ RBTree *other_tree = rb_tree_initialize(20);
+ other_tree = rb_tree_insert(other_tree, 30);
+
+ other_tree->right->parent = NULL;
+
+ assert_that(rb_equals(tree, other_tree), is_equal_to(false));
+ assert_that(rb_equals(other_tree, tree), is_equal_to(false));
+}
+
Ensure(is_valid_returns_false_when_root_is_red) {
RBTree *tree = rb_tree_initialize(20);
tree->colour = red;
@@ -286,6 +299,7 @@ TestSuite *rb_tree_tests() {
add_test(x, equals_returns_true_when_root_and_left_subtree_are_equal);
add_test(x, equals_returns_false_when_root_and_left_subtree_are_not_equal);
add_test(x, equals_returns_false_when_root_and_right_subtree_are_not_equal);
+ add_test(x, equals_returns_false_when_parent_is_not_equal);
add_test(x, is_valid_returns_false_when_root_is_red);
add_test(x, is_valid_returns_false_when_red_node_has_red_child);