Merge equivalent stacks in a separate stage of parsing
* No more automatic merging every time a state is pushed to the stack * When popping from the stack, the current version is always preserved
This commit is contained in:
parent
827573f1c7
commit
695be5bc79
7 changed files with 582 additions and 755 deletions
|
|
@ -93,7 +93,6 @@ describe("Stack", [&]() {
|
|||
TSTree *trees[tree_count];
|
||||
TreeSelectionSpy tree_selection_spy;
|
||||
TSLength tree_len = {2, 3, 0, 3};
|
||||
TSSymbolMetadata metadata = {true, true, true, true};
|
||||
|
||||
before_each([&]() {
|
||||
record_alloc::start();
|
||||
|
|
@ -120,9 +119,9 @@ describe("Stack", [&]() {
|
|||
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
|
||||
});
|
||||
|
||||
describe("pushing entries to the stack", [&]() {
|
||||
it("adds entries to the stack", [&]() {
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
describe("push(version, tree, is_pending, state)", [&]() {
|
||||
it("adds entries to the given version of the stack", [&]() {
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(1));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(0));
|
||||
AssertThat(ts_stack_top_position(stack, 0), Equals(ts_length_zero()));
|
||||
|
||||
|
|
@ -150,473 +149,382 @@ describe("Stack", [&]() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("popping nodes from the stack", [&]() {
|
||||
describe("merge()", [&]() {
|
||||
before_each([&]() {
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
// . <──0── A <──1── B*
|
||||
// ↑
|
||||
// └───2─── C*
|
||||
ts_stack_push(stack, 0, trees[0], false, stateA);
|
||||
ts_stack_pop_count(stack, 0, 0);
|
||||
ts_stack_push(stack, 0, trees[1], false, stateB);
|
||||
ts_stack_push(stack, 0, trees[2], false, stateC);
|
||||
ts_stack_push(stack, 1, trees[2], false, stateC);
|
||||
});
|
||||
|
||||
it("removes the given number of nodes from the stack", [&]() {
|
||||
// . <──0── A*
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 2);
|
||||
AssertThat(pop_result.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
it("combines versions that have the same top states and positions", [&]() {
|
||||
// . <──0── A <──1── B <──3── D*
|
||||
// ↑
|
||||
// └───2─── C <──4── D*
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
ts_stack_push(stack, 1, trees[4], false, stateD);
|
||||
|
||||
StackSlice slice = pop_result.slices.contents[0];
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[1], trees[2] })));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateA));
|
||||
free_slice_array(&pop_result.slices);
|
||||
|
||||
// .*
|
||||
pop_result = ts_stack_pop_count(stack, 0, 1);
|
||||
AssertThat(pop_result.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
|
||||
slice = pop_result.slices.contents[0];
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[0] })));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(0));
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
// . <──0── A <──1── B <──3── D*
|
||||
// ↑ |
|
||||
// └───2─── C <──4───┘
|
||||
ts_stack_merge(stack);
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(1));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateD, 0},
|
||||
{stateB, 1},
|
||||
{stateC, 1},
|
||||
{stateA, 2},
|
||||
{0, 3},
|
||||
})));
|
||||
});
|
||||
|
||||
it("does not count 'extra' trees toward the count", [&]() {
|
||||
trees[1]->extra = true;
|
||||
|
||||
// .*
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 2);
|
||||
AssertThat(pop_result.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
|
||||
StackSlice slice = pop_result.slices.contents[0];
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[0], trees[1], trees[2] })));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(0));
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
it("does not combine versions that have different states", [&]() {
|
||||
ts_stack_merge(stack);
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
|
||||
});
|
||||
|
||||
it("pops the entire stack when given a negative count", [&]() {
|
||||
// .*
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, -1);
|
||||
AssertThat(pop_result.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
it("does not combine versions that have different positions", [&]() {
|
||||
// . <──0── A <──1── B <────3──── D*
|
||||
// ↑
|
||||
// └───2─── C <──4── D*
|
||||
trees[3]->size = tree_len * 3;
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
ts_stack_push(stack, 1, trees[4], false, stateD);
|
||||
|
||||
StackSlice slice = pop_result.slices.contents[0];
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[0], trees[1], trees[2] })));
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
ts_stack_merge(stack);
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
|
||||
});
|
||||
|
||||
describe("when an error state exists above the given depth", [&]() {
|
||||
it("stops popping nodes at the error", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── ERROR <──4── D*
|
||||
ts_stack_push(stack, 0, trees[3], false, ts_parse_state_error);
|
||||
ts_stack_push(stack, 0, trees[4], false, stateD);
|
||||
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 3);
|
||||
AssertThat(pop_result.status, Equals(StackPopResult::StackPopStoppedAtError));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(ts_parse_state_error));
|
||||
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
StackSlice slice = pop_result.slices.contents[0];
|
||||
AssertThat(slice.version, Equals(0));
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[4] })));
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
});
|
||||
});
|
||||
|
||||
describe("popping pending nodes from the stack", [&]() {
|
||||
it("removes the top node from the stack if it was pushed in pending mode", [&]() {
|
||||
ts_stack_push(stack, 0, trees[3], true, stateD);
|
||||
|
||||
StackPopResult pop = ts_stack_pop_pending(stack, 0);
|
||||
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateC, 0},
|
||||
{stateB, 1},
|
||||
{stateA, 2},
|
||||
{0, 3},
|
||||
})));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
|
||||
it("does nothing if the top node was not pushed in pending mode", [&]() {
|
||||
describe("when the merged versions have more than one common entry", [&]() {
|
||||
it("combines all of the top common entries", [&]() {
|
||||
// . <──0── A <──1── B <──3── D <──5── E*
|
||||
// ↑
|
||||
// └───2─── C <──4── D <──5── E*
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
ts_stack_push(stack, 0, trees[5], false, stateE);
|
||||
ts_stack_push(stack, 1, trees[4], false, stateD);
|
||||
ts_stack_push(stack, 1, trees[5], false, stateE);
|
||||
|
||||
StackPopResult pop = ts_stack_pop_pending(stack, 0);
|
||||
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop.slices.size, Equals<size_t>(0));
|
||||
|
||||
// . <──0── A <──1── B <──3── D <──5── E*
|
||||
// ↑ |
|
||||
// └───2─── C <──4───┘
|
||||
ts_stack_merge(stack);
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(1));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateD, 0},
|
||||
{stateC, 1},
|
||||
{stateE, 0},
|
||||
{stateD, 1},
|
||||
{stateB, 2},
|
||||
{stateC, 2},
|
||||
{stateA, 3},
|
||||
{0, 4},
|
||||
})));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("pop_count(version, count)", [&]() {
|
||||
before_each([&]() {
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
ts_stack_push(stack, 0, trees[0], false, stateA);
|
||||
ts_stack_push(stack, 0, trees[1], false, stateB);
|
||||
ts_stack_push(stack, 0, trees[2], false, stateC);
|
||||
});
|
||||
|
||||
it("creates a new version with the given number of entries removed", [&]() {
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
// ↑
|
||||
// └─*
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 2);
|
||||
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
|
||||
|
||||
StackSlice slice = pop.slices.contents[0];
|
||||
AssertThat(slice.version, Equals<StackVersion>(1));
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[1], trees[2] })));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateA));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
|
||||
it("does not count 'extra' trees toward the given count", [&]() {
|
||||
trees[1]->extra = true;
|
||||
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
// ↑
|
||||
// └─*
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 2);
|
||||
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
|
||||
StackSlice slice = pop.slices.contents[0];
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[0], trees[1], trees[2] })));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(0));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
|
||||
it("stops popping entries early if it reaches an error tree", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── ERROR <──4── D*
|
||||
ts_stack_push(stack, 0, trees[3], false, ts_parse_state_error);
|
||||
ts_stack_push(stack, 0, trees[4], false, stateD);
|
||||
|
||||
// . <──0── A <──1── B <──2── C <──3── ERROR <──4── D*
|
||||
// ↑
|
||||
// └─*
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 3);
|
||||
AssertThat(pop.status, Equals(StackPopResult::StackPopStoppedAtError));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(ts_parse_state_error));
|
||||
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
StackSlice slice = pop.slices.contents[0];
|
||||
AssertThat(slice.version, Equals<StackVersion>(1));
|
||||
AssertThat(slice.trees, Equals(vector<TSTree *>({ trees[4] })));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
|
||||
describe("when the version has been merged", [&]() {
|
||||
before_each([&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I*
|
||||
// ↑ |
|
||||
// └───4─── E <──5── F <──6───┘
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 3);
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
});
|
||||
});
|
||||
ts_stack_push(stack, 1, trees[4], false, stateE);
|
||||
ts_stack_push(stack, 1, trees[5], false, stateF);
|
||||
ts_stack_push(stack, 1, trees[6], false, stateD);
|
||||
ts_stack_merge(stack);
|
||||
ts_stack_push(stack, 0, trees[10], false, stateI);
|
||||
|
||||
describe("splitting the stack", [&]() {
|
||||
it("creates a new independent version with the same entries", [&]() {
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
ts_stack_push(stack, 0, trees[0], false, stateA);
|
||||
ts_stack_push(stack, 0, trees[1], false, stateB);
|
||||
ts_stack_push(stack, 0, trees[2], false, stateC);
|
||||
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
// ↑
|
||||
// └─*
|
||||
int new_index = ts_stack_split(stack, 0);
|
||||
AssertThat(ts_stack_version_count(stack), Equals(2));
|
||||
AssertThat(new_index, Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateC));
|
||||
|
||||
// . <──0── A <──1── B <──2── C <──3── D*
|
||||
// ↑
|
||||
// └─*
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 1, 1);
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(2));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateD));
|
||||
AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 4));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateB));
|
||||
AssertThat(ts_stack_top_position(stack, 1), Equals(tree_len * 2));
|
||||
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
StackSlice slice = pop_result.slices.contents[0];
|
||||
AssertThat(slice.trees.size, Equals<size_t>(1));
|
||||
free_slice_array(&pop_result.slices);
|
||||
|
||||
// . <──0── A <──1── B <──2── C <──3── D*
|
||||
// ↑
|
||||
// └───4─── E <──5── F*
|
||||
ts_stack_push(stack, 1, trees[4], false, stateE);
|
||||
ts_stack_push(stack, 1, trees[5], false, stateF);
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(2));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateD));
|
||||
AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 4));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateF));
|
||||
AssertThat(ts_stack_top_position(stack, 1), Equals(tree_len * 4));
|
||||
});
|
||||
});
|
||||
|
||||
describe("pushing the same state onto two different versions of the stack", [&]() {
|
||||
before_each([&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D*
|
||||
// ↑
|
||||
// └───4─── E <──5── F*
|
||||
ts_stack_push(stack, 0, trees[0], false, stateA);
|
||||
ts_stack_push(stack, 0, trees[1], false, stateB);
|
||||
ts_stack_split(stack, 0);
|
||||
ts_stack_push(stack, 0, trees[2], false, stateC);
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
ts_stack_push(stack, 1, trees[4], false, stateE);
|
||||
ts_stack_push(stack, 1, trees[5], false, stateF);
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(2));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateD, 0},
|
||||
{stateC, 1},
|
||||
{stateB, 2},
|
||||
{stateA, 3},
|
||||
{0, 4},
|
||||
})));
|
||||
AssertThat(get_stack_entries(stack, 1), Equals(vector<StackEntry>({
|
||||
{stateF, 0},
|
||||
{stateE, 1},
|
||||
{stateB, 2},
|
||||
{stateA, 3},
|
||||
{0, 4},
|
||||
})));
|
||||
});
|
||||
|
||||
it("merges the versions", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──6── G*
|
||||
// ↑ |
|
||||
// └───4─── E <──5── F <──7───┘
|
||||
AssertThat(ts_stack_push(stack, 0, trees[6], false, stateG), Equals(StackPushContinued));
|
||||
AssertThat(ts_stack_push(stack, 1, trees[7], false, stateG), Equals(StackPushMerged));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateG, 0},
|
||||
{stateD, 1},
|
||||
{stateF, 1},
|
||||
{stateC, 2},
|
||||
{stateE, 2},
|
||||
{stateB, 3},
|
||||
{stateA, 4},
|
||||
{0, 5},
|
||||
})));
|
||||
});
|
||||
|
||||
describe("when the merged nodes share a successor", [&]() {
|
||||
it("recursively merges the successor nodes", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──6── G <──7──H*
|
||||
// ↑
|
||||
// └───4─── E <──5── F <──8── G*
|
||||
AssertThat(ts_stack_push(stack, 0, trees[6], false, stateG), Equals(StackPushContinued));
|
||||
AssertThat(ts_stack_push(stack, 0, trees[7], false, stateH), Equals(StackPushContinued));
|
||||
AssertThat(ts_stack_push(stack, 1, trees[6], false, stateG), Equals(StackPushContinued));
|
||||
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──6── G <──7──H*
|
||||
// ↑ |
|
||||
// └───4─── E <──5── F <──8───┘
|
||||
AssertThat(ts_stack_push(stack, 1, trees[7], false, stateH), Equals(StackPushMerged));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(1));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateH, 0},
|
||||
{stateG, 1},
|
||||
{stateD, 2},
|
||||
{stateI, 0},
|
||||
{stateD, 1},
|
||||
{stateC, 2},
|
||||
{stateF, 2},
|
||||
{stateC, 3},
|
||||
{stateB, 3},
|
||||
{stateE, 3},
|
||||
{stateB, 4},
|
||||
{stateA, 5},
|
||||
{0, 6},
|
||||
{stateA, 4},
|
||||
{0, 5},
|
||||
})));
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the first version is only one node deep", [&]() {
|
||||
it("creates a node with one null successor and one non-null successor", [&]() {
|
||||
ts_tree_retain(trees[2]);
|
||||
ts_tree_retain(trees[3]);
|
||||
TSTree *parent = ts_tree_make_node(5, 2, tree_array({ trees[2], trees[3] }), metadata);
|
||||
describe("when there are two paths that reveal different versions", [&]() {
|
||||
it("returns an entry for each revealed version", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I*
|
||||
// ↑ ↑
|
||||
// | └*
|
||||
// |
|
||||
// └───4─── E*
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 3);
|
||||
AssertThat(pop.slices.size, Equals<size_t>(2));
|
||||
|
||||
// . <──────5─────── C*
|
||||
// ↑ |
|
||||
// └───2─── B ───3───┘
|
||||
ts_stack_clear(stack);
|
||||
ts_stack_split(stack, 0);
|
||||
AssertThat(ts_stack_push(stack, 0, parent, false, stateC), Equals(StackPushContinued));
|
||||
AssertThat(ts_stack_push(stack, 1, trees[2], false, stateB), Equals(StackPushContinued));
|
||||
AssertThat(ts_stack_push(stack, 1, trees[3], false, stateC), Equals(StackPushMerged));
|
||||
StackSlice slice1 = pop.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals<StackVersion>(1));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[2], trees[3], trees[10] })));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateC));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateC, 0},
|
||||
{0, 1},
|
||||
{stateB, 1},
|
||||
{0, 2},
|
||||
})));
|
||||
StackSlice slice2 = pop.slices.contents[1];
|
||||
AssertThat(slice2.version, Equals<StackVersion>(2));
|
||||
AssertThat(slice2.trees, Equals(vector<TSTree *>({ trees[5], trees[6], trees[10] })));
|
||||
|
||||
ts_tree_release(parent);
|
||||
});
|
||||
});
|
||||
});
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(3));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateI, 0},
|
||||
{stateD, 1},
|
||||
{stateC, 2},
|
||||
{stateF, 2},
|
||||
{stateB, 3},
|
||||
{stateE, 3},
|
||||
{stateA, 4},
|
||||
{0, 5},
|
||||
})));
|
||||
AssertThat(get_stack_entries(stack, 1), Equals(vector<StackEntry>({
|
||||
{stateB, 0},
|
||||
{stateA, 1},
|
||||
{0, 2},
|
||||
})));
|
||||
AssertThat(get_stack_entries(stack, 2), Equals(vector<StackEntry>({
|
||||
{stateE, 0},
|
||||
{stateA, 1},
|
||||
{0, 2},
|
||||
})));
|
||||
|
||||
describe("popping from a stack version that has been merged", [&]() {
|
||||
before_each([&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──4── E*
|
||||
// ↑ |
|
||||
// └───5─── F <──6── G <──7───┘
|
||||
ts_stack_push(stack, 0, trees[0], false, stateA);
|
||||
ts_stack_push(stack, 0, trees[1], false, stateB);
|
||||
ts_stack_split(stack, 0);
|
||||
ts_stack_push(stack, 0, trees[2], false, stateC);
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
ts_stack_push(stack, 0, trees[4], false, stateE);
|
||||
ts_stack_push(stack, 1, trees[5], false, stateF);
|
||||
ts_stack_push(stack, 1, trees[6], false, stateG);
|
||||
ts_stack_push(stack, 1, trees[7], false, stateE);
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateE, 0},
|
||||
{stateD, 1},
|
||||
{stateG, 1},
|
||||
{stateC, 2},
|
||||
{stateF, 2},
|
||||
{stateB, 3},
|
||||
{stateA, 4},
|
||||
{0, 5},
|
||||
})));
|
||||
});
|
||||
|
||||
describe("when there are two paths that lead to two different versions", [&]() {
|
||||
it("returns an entry for each revealed version", [&]() {
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
// ↑
|
||||
// └───5─── F*
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 2);
|
||||
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(2));
|
||||
StackSlice slice1 = pop_result.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals(0));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateC));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[3], trees[4] })));
|
||||
|
||||
StackSlice slice2 = pop_result.slices.contents[1];
|
||||
AssertThat(slice2.version, Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateF));
|
||||
AssertThat(slice2.trees, Equals(vector<TSTree *>({ trees[6], trees[7] })));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(2));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateC, 0},
|
||||
{stateB, 1},
|
||||
{stateA, 2},
|
||||
{0, 3},
|
||||
})));
|
||||
AssertThat(get_stack_entries(stack, 1), Equals(vector<StackEntry>({
|
||||
{stateF, 0},
|
||||
{stateB, 1},
|
||||
{stateA, 2},
|
||||
{0, 3},
|
||||
})));
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when there is one path, leading to one version", [&]() {
|
||||
it("returns a single entry", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──4── E <──8──H*
|
||||
// ↑ |
|
||||
// └───5─── F <──6── G <──7───┘
|
||||
AssertThat(ts_stack_push(stack, 0, trees[8], false, stateH), Equals(StackPushContinued));
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateH));
|
||||
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──4── E*
|
||||
// ↑ |
|
||||
// └───5─── F <──6── G <──7───┘
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 1);
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
StackSlice slice1 = pop_result.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals(0));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[8] })));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateE));
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when there are two paths that converge at the same version", [&]() {
|
||||
describe("when the first path is preferred by the callback", [&]() {
|
||||
it("returns one entry for that version, with the first path of trees", [&]() {
|
||||
tree_selection_spy.tree_to_return = trees[2];
|
||||
|
||||
// . <──0── A <──1── B*
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 3);
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateB));
|
||||
AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 2));
|
||||
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
StackSlice slice1 = pop_result.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals(0));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[2], trees[3], trees[4] })));
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the second path is preferred by the callback", [&]() {
|
||||
it("returns one entry for that version, with the second path of trees", [&]() {
|
||||
describe("when there is one path that ends at a merged version", [&]() {
|
||||
it("returns a single entry", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I*
|
||||
// | |
|
||||
// └───5─── F <──6── G <──7───┘
|
||||
// |
|
||||
// └*
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 1);
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
|
||||
StackSlice slice1 = pop.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals<StackVersion>(1));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[10] })));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateI));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateD));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when there are two paths that converge on one version", [&]() {
|
||||
it("returns the first path of trees if they are selected by the selection callback", [&]() {
|
||||
tree_selection_spy.tree_to_return = trees[1];
|
||||
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I*
|
||||
// ↑ |
|
||||
// ├───4─── E <──5── F <──6───┘
|
||||
// |
|
||||
// └*
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 4);
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
|
||||
StackSlice slice1 = pop.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals<StackVersion>(1));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[1], trees[2], trees[3], trees[10] })));
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateI));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateA));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
|
||||
it("returns the second path of trees if they are selected by the selection callback", [&]() {
|
||||
tree_selection_spy.tree_to_return = trees[4];
|
||||
|
||||
// . <──0── A <──1── B*
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 3);
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateB));
|
||||
AssertThat(ts_stack_top_position(stack, 0), Equals(tree_len * 2));
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I*
|
||||
// ↑ |
|
||||
// ├───4─── E <──5── F <──6───┘
|
||||
// |
|
||||
// └*
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 4);
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(1));
|
||||
StackSlice slice1 = pop_result.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals(0));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[5], trees[6], trees[7] })))
|
||||
StackSlice slice1 = pop.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals<StackVersion>(1));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[4], trees[5], trees[6], trees[10] })))
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateI));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateA));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when there are three paths that lead to three different versions", [&]() {
|
||||
it("returns three entries with different arrays of trees", [&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I*
|
||||
// ↑ |
|
||||
// ├───4─── E <──5── F <──6───┘
|
||||
// | |
|
||||
// └───7─── G <──8── H <──9───┘
|
||||
StackPopResult pop = ts_stack_pop_count(stack, 0, 4);
|
||||
free_slice_array(&pop.slices);
|
||||
ts_stack_push(stack, 1, trees[7], false, stateG);
|
||||
ts_stack_push(stack, 1, trees[8], false, stateH);
|
||||
ts_stack_push(stack, 1, trees[9], false, stateD);
|
||||
ts_stack_push(stack, 1, trees[10], false, stateI);
|
||||
ts_stack_merge(stack);
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(1));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateI, 0},
|
||||
{stateD, 1},
|
||||
{stateC, 2},
|
||||
{stateF, 2},
|
||||
{stateH, 2},
|
||||
{stateB, 3},
|
||||
{stateE, 3},
|
||||
{stateG, 3},
|
||||
{stateA, 4},
|
||||
{0, 5},
|
||||
})));
|
||||
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I*
|
||||
// ↑ ↑
|
||||
// | └*
|
||||
// |
|
||||
// ├───4─── E <──5── F*
|
||||
// |
|
||||
// └───7─── G <──8── H*
|
||||
pop = ts_stack_pop_count(stack, 0, 2);
|
||||
AssertThat(pop.slices.size, Equals<size_t>(3));
|
||||
|
||||
StackSlice slice1 = pop.slices.contents[0];
|
||||
AssertThat(slice1.version, Equals<StackVersion>(1));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[3], trees[10] })))
|
||||
|
||||
StackSlice slice2 = pop.slices.contents[1];
|
||||
AssertThat(slice2.version, Equals<StackVersion>(2));
|
||||
AssertThat(slice2.trees, Equals(vector<TSTree *>({ trees[6], trees[10] })))
|
||||
|
||||
StackSlice slice3 = pop.slices.contents[2];
|
||||
AssertThat(slice3.version, Equals<StackVersion>(3));
|
||||
AssertThat(slice3.trees, Equals(vector<TSTree *>({ trees[9], trees[10] })))
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals<size_t>(4));
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateI));
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateC));
|
||||
AssertThat(ts_stack_top_state(stack, 2), Equals(stateF));
|
||||
AssertThat(ts_stack_top_state(stack, 3), Equals(stateH));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("popping from a stack version that has been 3-way merged", [&]() {
|
||||
describe("pop_pending(version)", [&]() {
|
||||
before_each([&]() {
|
||||
// . <──0── A <──1── B <──2── C <──3── D <──10── I
|
||||
// ↑ |
|
||||
// ├───4─── E <──5── F <──6───┤
|
||||
// | |
|
||||
// └───7─── G <──8── H <──9───┘
|
||||
ts_stack_clear(stack);
|
||||
ts_stack_push(stack, 0, trees[0], false, stateA);
|
||||
ts_stack_split(stack, 0);
|
||||
ts_stack_split(stack, 1);
|
||||
ts_stack_push(stack, 0, trees[1], false, stateB);
|
||||
ts_stack_push(stack, 0, trees[2], false, stateC);
|
||||
ts_stack_push(stack, 0, trees[3], false, stateD);
|
||||
ts_stack_push(stack, 1, trees[4], false, stateE);
|
||||
ts_stack_push(stack, 1, trees[5], false, stateF);
|
||||
ts_stack_push(stack, 1, trees[6], false, stateD);
|
||||
ts_stack_push(stack, 1, trees[7], false, stateG);
|
||||
ts_stack_push(stack, 1, trees[8], false, stateH);
|
||||
ts_stack_push(stack, 1, trees[9], false, stateD);
|
||||
ts_stack_push(stack, 0, trees[10], false, stateI);
|
||||
|
||||
AssertThat(ts_stack_version_count(stack), Equals(1));
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateI, 0},
|
||||
{stateD, 1},
|
||||
{stateC, 2},
|
||||
{stateF, 2},
|
||||
{stateH, 2},
|
||||
{stateB, 3},
|
||||
{stateE, 3},
|
||||
{stateG, 3},
|
||||
{stateA, 4},
|
||||
{0, 5},
|
||||
})));
|
||||
});
|
||||
|
||||
describe("when there are three different paths that lead to three different versions", [&]() {
|
||||
it("returns three entries with different arrays of trees", [&]() {
|
||||
// . <──0── A <──1── B <──2── C*
|
||||
// ↑
|
||||
// ├───4─── E <──5── F*
|
||||
// |
|
||||
// └───7─── G <──8── H*
|
||||
StackPopResult pop_result = ts_stack_pop_count(stack, 0, 2);
|
||||
AssertThat(ts_stack_version_count(stack), Equals(3));
|
||||
it("removes the top node from the stack if it was pushed in pending mode", [&]() {
|
||||
ts_stack_push(stack, 0, trees[1], true, stateB);
|
||||
|
||||
AssertThat(pop_result.slices.size, Equals<size_t>(3));
|
||||
StackPopResult pop = ts_stack_pop_pending(stack, 0);
|
||||
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop.slices.size, Equals<size_t>(1));
|
||||
|
||||
StackSlice slice1 = pop_result.slices.contents[0];
|
||||
AssertThat(ts_stack_top_state(stack, 0), Equals(stateC));
|
||||
AssertThat(slice1.version, Equals(0));
|
||||
AssertThat(slice1.trees, Equals(vector<TSTree *>({ trees[3], trees[10] })))
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateA, 0},
|
||||
{0, 1},
|
||||
})));
|
||||
|
||||
StackSlice slice2 = pop_result.slices.contents[1];
|
||||
AssertThat(ts_stack_top_state(stack, 1), Equals(stateF));
|
||||
AssertThat(slice2.version, Equals(1));
|
||||
AssertThat(slice2.trees, Equals(vector<TSTree *>({ trees[6], trees[10] })))
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
|
||||
StackSlice slice3 = pop_result.slices.contents[2];
|
||||
AssertThat(ts_stack_top_state(stack, 2), Equals(stateH));
|
||||
AssertThat(slice3.version, Equals(2));
|
||||
AssertThat(slice3.trees, Equals(vector<TSTree *>({ trees[9], trees[10] })))
|
||||
it("does nothing if the top node was not pushed in pending mode", [&]() {
|
||||
ts_stack_push(stack, 0, trees[1], false, stateB);
|
||||
|
||||
free_slice_array(&pop_result.slices);
|
||||
});
|
||||
StackPopResult pop = ts_stack_pop_pending(stack, 0);
|
||||
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
|
||||
AssertThat(pop.slices.size, Equals<size_t>(0));
|
||||
|
||||
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
|
||||
{stateB, 0},
|
||||
{stateA, 1},
|
||||
{0, 2},
|
||||
})));
|
||||
|
||||
free_slice_array(&pop.slices);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue