Skip to content

Commit f47eef0

Browse files
committed
[test] adding test for stack/path for candidate leaf functon
1 parent 41aa8a1 commit f47eef0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ TEST test_bst(void) {
7676
d = binary_tree_search(root, 7);
7777
ASSERT_STR_EQ(*d, "d");
7878

79+
binary_tree_stack_t stack = (binary_tree_stack_t){
80+
.stack = {NULL},
81+
.stack_size = 0
82+
};
83+
binary_tree_node_t *candidate;
84+
85+
candidate = binary_tree_candidate_leaf(root, 1, &stack);
86+
ASSERT_EQ(candidate, node1);
87+
ASSERT_EQ(stack.stack_size, 2);
88+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 2);
89+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 4);
90+
7991
binary_tree_rotate_left(root);
8092
/*
8193
Now tree should be in this state:
@@ -107,6 +119,12 @@ TEST test_bst(void) {
107119
d = binary_tree_search(root, 7);
108120
ASSERT_STR_EQ(*d, "d");
109121

122+
candidate = binary_tree_candidate_leaf(root, 3, &stack);
123+
ASSERT_EQ(candidate, node3);
124+
ASSERT_EQ(stack.stack_size, 3);
125+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 2);
126+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 4);
127+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 6);
110128

111129
binary_tree_rotate_right(root);
112130
// Tree should be in the original state
@@ -159,6 +177,13 @@ TEST test_bst(void) {
159177
d = binary_tree_search(root, 7);
160178
ASSERT_STR_EQ(*d, "d");
161179

180+
candidate = binary_tree_candidate_leaf(root, 7, &stack);
181+
ASSERT_EQ(candidate, node7);
182+
ASSERT_EQ(stack.stack_size, 3);
183+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 6);
184+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 4);
185+
ASSERT_EQ(binary_tree_stack_pop(&stack)->key, 2);
186+
162187
PASS();
163188
}
164189

0 commit comments

Comments
 (0)