diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index 721881d5..05be07e8 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -275,6 +275,30 @@ describe("Stack", [&]() { AssertThat(ts_stack_entry_next_count(next), Equals(2)); }); }); + + describe("when the first head is only one node deep", [&]() { + it("adds it as an additional successor node to The Null node", [&]() { + /* + * .__A0. + * B1.__/ + */ + ts_stack_clear(stack); + ts_stack_split(stack, 0); + ts_stack_push(stack, 0, stateA, trees[0]); + bool merged = ts_stack_push(stack, 1, stateB, trees[1]); + AssertThat(merged, IsFalse()); + merged = ts_stack_push(stack, 1, stateA, trees[0]); + AssertThat(merged, IsTrue()); + + AssertThat(ts_stack_head_count(stack), Equals(1)); + StackEntry *head = ts_stack_head(stack, 0); + AssertThat(*head, Equals({trees[0], stateA})); + + AssertThat(ts_stack_entry_next_count(head), Equals(2)); + AssertThat(ts_stack_entry_next(head, 0), Equals(nullptr)); + AssertThat(*ts_stack_entry_next(head, 1), Equals({trees[1], stateB})); + }); + }); }); describe("popping from a stack head that has been merged", [&]() { diff --git a/src/runtime/stack.c b/src/runtime/stack.c index bda889b1..3a4dd308 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -124,8 +124,11 @@ static void ts_stack__add_node_successor(Stack *self, StackNode *node, StackNode *new_successor) { for (int i = 0; i < node->successor_count; i++) { StackNode *successor = node->successors[i]; + if (!successor) + continue; if (successor == new_successor) return; + if (successor->entry.state == new_successor->entry.state) { if (successor->entry.tree != new_successor->entry.tree) { successor->entry.tree = self->tree_selection_callback.callback(