tree-sitter/spec/runtime/stack_spec.cc

547 lines
20 KiB
C++
Raw Normal View History

#include "spec_helper.h"
#include "helpers/tree_helpers.h"
2016-01-28 21:18:57 -08:00
#include "helpers/record_alloc.h"
#include "helpers/stream_methods.h"
2015-09-18 18:04:52 -07:00
#include "runtime/stack.h"
2015-05-25 20:21:13 -07:00
#include "runtime/tree.h"
#include "runtime/length.h"
2016-01-28 21:18:57 -08:00
#include "runtime/alloc.h"
2015-05-25 20:21:13 -07:00
enum {
2015-11-20 00:01:53 -08:00
stateA, stateB, stateC, stateD, stateE, stateF, stateG, stateH, stateI, stateJ
};
enum {
symbol0, symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8,
symbol9, symbol10
};
struct TreeSelectionSpy {
int call_count;
TSTree *tree_to_return;
const TSTree *arguments[2];
};
TSLength operator*(const TSLength &length, size_t factor) {
return {length.bytes * factor, length.chars * factor, 0, length.columns * factor};
}
extern "C"
int tree_selection_spy_callback(void *data, TSTree *left, TSTree *right) {
TreeSelectionSpy *spy = (TreeSelectionSpy *)data;
spy->call_count++;
spy->arguments[0] = left;
spy->arguments[1] = right;
if (spy->tree_to_return == left)
return -1;
else
return 1;
}
2016-02-17 20:41:29 -08:00
void free_pop_results(StackPopResultArray *pop_results) {
2016-01-28 21:18:57 -08:00
for (size_t i = 0; i < pop_results->size; i++) {
StackPopResult pop_result = pop_results->contents[i];
2016-01-28 21:18:57 -08:00
bool matches_prior_trees = false;
for (size_t j = 0; j < i; j++) {
StackPopResult prior_result = pop_results->contents[j];
if (pop_result.trees.contents == prior_result.trees.contents) {
2016-01-28 21:18:57 -08:00
matches_prior_trees = true;
break;
}
}
if (!matches_prior_trees) {
for (size_t j = 0; j < pop_result.trees.size; j++)
ts_tree_release(pop_result.trees.contents[j]);
array_delete(&pop_result.trees);
2016-01-28 21:18:57 -08:00
}
}
}
2015-05-25 20:21:13 -07:00
2016-01-28 21:18:57 -08:00
START_TEST
2015-09-18 18:04:52 -07:00
describe("Stack", [&]() {
Stack *stack;
const size_t tree_count = 11;
2015-05-25 20:21:13 -07:00
TSTree *trees[tree_count];
TreeSelectionSpy tree_selection_spy{0, NULL, {NULL, NULL}};
TSLength tree_len = {2, 3, 0, 3};
TSSymbolMetadata metadata = {true, true, true, true};
2015-05-25 20:21:13 -07:00
before_each([&]() {
2016-01-28 21:18:57 -08:00
record_alloc::start();
stack = ts_stack_new();
ts_stack_set_tree_selection_callback(stack,
&tree_selection_spy,
tree_selection_spy_callback
);
2015-05-25 20:21:13 -07:00
for (size_t i = 0; i < tree_count; i++)
2016-02-12 14:07:30 -08:00
trees[i] = ts_tree_make_leaf(i, ts_length_zero(), tree_len, {
true, true, false, true,
});
2015-05-25 20:21:13 -07:00
});
after_each([&]() {
2015-09-18 18:04:52 -07:00
ts_stack_delete(stack);
2015-05-25 20:21:13 -07:00
for (size_t i = 0; i < tree_count; i++)
ts_tree_release(trees[i]);
2016-01-28 21:18:57 -08:00
record_alloc::stop();
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
2015-05-25 20:21:13 -07:00
});
2015-07-10 17:50:38 -07:00
describe("pushing entries to the stack", [&]() {
2015-06-18 16:34:52 -07:00
it("adds entries to the stack", [&]() {
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_head(stack, 0), Equals<const StackEntry *>(nullptr));
2015-05-25 20:21:13 -07:00
2015-06-18 16:34:52 -07:00
/*
* . <--0-- A*
2015-06-18 16:34:52 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[0], stateA);
2015-09-18 18:04:52 -07:00
const StackEntry *entry1 = ts_stack_head(stack, 0);
AssertThat(*entry1, Equals<StackEntry>({stateA, tree_len}));
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_entry_next_count(entry1), Equals(1));
AssertThat(ts_stack_entry_next(entry1, 0), Equals<const StackEntry *>(nullptr));
2015-05-25 20:21:13 -07:00
2015-06-18 16:34:52 -07:00
/*
* . <--0-- A <--1-- B*
2015-06-18 16:34:52 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[1], stateB);
2015-09-18 18:04:52 -07:00
const StackEntry *entry2 = ts_stack_head(stack, 0);
AssertThat(*entry2, Equals<StackEntry>({stateB, tree_len * 2}));
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_entry_next_count(entry2), Equals(1));
AssertThat(ts_stack_entry_next(entry2, 0), Equals(entry1));
2015-05-25 20:21:13 -07:00
2015-06-18 16:34:52 -07:00
/*
* . <--0-- A <--1-- B <--2-- C*
2015-06-18 16:34:52 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[2], stateC);
2015-09-18 18:04:52 -07:00
const StackEntry *entry3 = ts_stack_head(stack, 0);
AssertThat(*entry3, Equals<StackEntry>({stateC, tree_len * 3}));
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_entry_next_count(entry3), Equals(1));
AssertThat(ts_stack_entry_next(entry3, 0), Equals(entry2));
2015-06-18 16:34:52 -07:00
});
2015-06-03 09:44:13 -07:00
});
2015-05-25 20:21:13 -07:00
2015-07-10 17:50:38 -07:00
describe("popping nodes from the stack", [&]() {
2015-06-18 16:34:52 -07:00
before_each([&]() {
/*
* . <--0-- A <--1-- B <--2-- C*
2015-06-18 16:34:52 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[0], stateA);
ts_stack_push(stack, 0, trees[1], stateB);
ts_stack_push(stack, 0, trees[2], stateC);
2015-06-18 16:34:52 -07:00
});
it("removes the given number of nodes from the stack", [&]() {
/*
* . <--0-- A*
2015-06-18 16:34:52 -07:00
*/
2016-02-17 20:41:29 -08:00
StackPopResultArray results = ts_stack_pop(stack, 0, 2, false);
2016-01-28 21:18:57 -08:00
AssertThat(results.size, Equals<size_t>(1));
2016-02-17 20:41:29 -08:00
StackPopResult result = results.contents[0];
AssertThat(result.trees, Equals(vector<TSTree *>({ trees[1], trees[2] })));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({stateA, tree_len}));
2016-01-28 21:18:57 -08:00
free_pop_results(&results);
2015-06-18 16:34:52 -07:00
/*
* .
*/
2016-01-28 21:18:57 -08:00
results = ts_stack_pop(stack, 0, 1, false);
AssertThat(results.size, Equals<size_t>(1));
2016-02-17 20:41:29 -08:00
result = results.contents[0];
AssertThat(result.trees, Equals(vector<TSTree *>({ trees[0] })));
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head(stack, 0), Equals<const StackEntry *>(nullptr));
2016-01-28 21:18:57 -08:00
free_pop_results(&results);
2015-06-18 16:34:52 -07:00
});
it("does not count 'extra' trees toward the count", [&]() {
2015-12-22 14:20:58 -08:00
trees[1]->extra = true;
2015-06-18 16:34:52 -07:00
/*
* .
*/
2016-02-17 20:41:29 -08:00
StackPopResultArray results = ts_stack_pop(stack, 0, 2, false);
2016-01-28 21:18:57 -08:00
AssertThat(results.size, Equals<size_t>(1));
2016-02-17 20:41:29 -08:00
StackPopResult result = results.contents[0];
AssertThat(result.trees, Equals(vector<TSTree *>({ trees[0], trees[1], trees[2] })));
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head(stack, 0), Equals<const StackEntry *>(nullptr));
2016-01-28 21:18:57 -08:00
free_pop_results(&results);
2015-06-18 16:34:52 -07:00
});
2015-06-18 16:34:52 -07:00
it("pops the entire stack when given a negative count", [&]() {
/*
* .
*/
2016-02-17 20:41:29 -08:00
StackPopResultArray results = ts_stack_pop(stack, 0, -1, false);
2016-01-28 21:18:57 -08:00
AssertThat(results.size, Equals<size_t>(1));
2016-02-17 20:41:29 -08:00
StackPopResult result = results.contents[0];
AssertThat(result.trees, Equals(vector<TSTree *>({ trees[0], trees[1], trees[2] })));
2016-01-28 21:18:57 -08:00
free_pop_results(&results);
2015-06-18 16:34:52 -07:00
});
});
2015-07-10 17:50:38 -07:00
describe("splitting the stack", [&]() {
2015-06-18 16:34:52 -07:00
it("creates a new independent head with the same entries", [&]() {
2015-06-03 09:44:13 -07:00
/*
* . <--0-- A <--1-- B <--2-- C*
2015-06-03 09:44:13 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[0], stateA);
ts_stack_push(stack, 0, trees[1], stateB);
ts_stack_push(stack, 0, trees[2], stateC);
2015-06-03 09:44:13 -07:00
/*
* . <--0-- A <--1-- B <--2-- C*
*
* `-*
*/
2015-09-18 18:04:52 -07:00
int new_index = ts_stack_split(stack, 0);
AssertThat(ts_stack_head_count(stack), Equals(2));
2015-06-18 16:34:52 -07:00
AssertThat(new_index, Equals(1));
AssertThat(ts_stack_top_state(stack, 1), Equals(stateC));
2015-06-18 16:34:52 -07:00
2015-06-03 09:44:13 -07:00
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D*
*
* `-*
2015-06-03 09:44:13 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[3], stateD);
2016-02-17 20:41:29 -08:00
StackPopResultArray pop_results = ts_stack_pop(stack, 1, 1, false);
2015-06-18 16:34:52 -07:00
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({stateD, tree_len * 4}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({stateB, tree_len * 2}));
2016-01-28 21:18:57 -08:00
AssertThat(pop_results.size, Equals<size_t>(1));
2016-02-17 20:41:29 -08:00
StackPopResult pop_result = pop_results.contents[0];
AssertThat(pop_result.trees.size, Equals<size_t>(1));
2016-01-28 21:18:57 -08:00
free_pop_results(&pop_results);
2015-06-18 16:34:52 -07:00
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D*
*
* `---4--- E <--5-- F*
2015-06-18 16:34:52 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 1, trees[4], stateE);
ts_stack_push(stack, 1, trees[5], stateF);
2015-05-25 20:21:13 -07:00
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({stateD, tree_len * 4}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({stateF, tree_len * 4}));
});
2015-07-10 17:50:38 -07:00
});
describe("pushing the same state onto two different heads of the stack", [&]() {
before_each([&]() {
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D*
*
* `---4--- E <--5-- F*
2015-07-10 17:50:38 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[0], stateA);
ts_stack_push(stack, 0, trees[1], stateB);
2015-09-18 18:04:52 -07:00
ts_stack_split(stack, 0);
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[2], stateC);
ts_stack_push(stack, 0, trees[3], stateD);
ts_stack_push(stack, 1, trees[4], stateE);
ts_stack_push(stack, 1, trees[5], stateF);
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({stateD, tree_len * 4}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({stateF, tree_len * 4}));
2015-07-10 17:50:38 -07:00
});
2015-05-25 20:21:13 -07:00
it("merges the heads", [&]() {
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D <--6-- G*
* ^ |
* `---4--- E <--5-- F <--7---'
*/
2016-02-25 21:46:13 -08:00
AssertThat(ts_stack_push(stack, 0, trees[6], stateG), Equals(StackPushResultContinued));
AssertThat(ts_stack_push(stack, 1, trees[7], stateG), Equals(StackPushResultMerged));
2015-07-10 17:50:38 -07:00
AssertThat(ts_stack_head_count(stack), Equals(1));
const StackEntry *entry1 = ts_stack_head(stack, 0);
AssertThat(*entry1, Equals<StackEntry>({stateG, tree_len * 5}));
AssertThat(ts_stack_entry_next_count(entry1), Equals(2));
AssertThat(*ts_stack_entry_next(entry1, 0), Equals<StackEntry>({stateD, tree_len * 4}));
AssertThat(*ts_stack_entry_next(entry1, 1), Equals<StackEntry>({stateF, tree_len * 4}));
2015-07-10 17:50:38 -07:00
});
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*
*/
2016-02-25 21:46:13 -08:00
AssertThat(ts_stack_push(stack, 0, trees[6], stateG), Equals(StackPushResultContinued));
AssertThat(ts_stack_push(stack, 0, trees[7], stateH), Equals(StackPushResultContinued));
AssertThat(ts_stack_push(stack, 1, trees[6], stateG), Equals(StackPushResultContinued));
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D <--6-- G <--7--H*
* |
* `---4--- E <--5-- F <--8---'
*/
2016-02-25 21:46:13 -08:00
AssertThat(ts_stack_push(stack, 1, trees[7], stateH), Equals(StackPushResultMerged));
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head_count(stack), Equals(1));
StackEntry *head = ts_stack_head(stack, 0);
AssertThat(*head, Equals<StackEntry>({stateH, tree_len * 6}))
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_entry_next_count(head), Equals(1));
2015-09-18 18:04:52 -07:00
StackEntry *next = ts_stack_entry_next(head, 0);
AssertThat(*next, Equals<StackEntry>({stateG, tree_len * 5}))
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_entry_next_count(next), Equals(2));
});
});
describe("when the first head is only one node deep", [&]() {
2016-01-28 21:18:57 -08:00
it("creates a node with one null successor and one non-null successor", [&]() {
2016-01-28 23:15:22 -08:00
ts_tree_retain(trees[2]);
ts_tree_retain(trees[3]);
2016-01-28 21:18:57 -08:00
TSTree *parent = ts_tree_make_node(5, 2, tree_array({ trees[2], trees[3] }), metadata);
2016-01-28 23:15:22 -08:00
/*
* . <--2-- B <--3-- C
* ^ |
* `--------5--------'
*/
ts_stack_clear(stack);
ts_stack_split(stack, 0);
2016-02-25 21:46:13 -08:00
AssertThat(ts_stack_push(stack, 0, parent, stateC), Equals(StackPushResultContinued));
AssertThat(ts_stack_push(stack, 1, trees[2], stateB), Equals(StackPushResultContinued));
AssertThat(ts_stack_push(stack, 1, trees[3], stateC), Equals(StackPushResultMerged));
AssertThat(ts_stack_head_count(stack), Equals(1));
StackEntry *head = ts_stack_head(stack, 0);
AssertThat(*head, Equals<StackEntry>({stateC, tree_len * 2}));
AssertThat(ts_stack_entry_next_count(head), Equals(2));
AssertThat(ts_stack_entry_next(head, 0), Equals<StackEntry *>(nullptr));
AssertThat(*ts_stack_entry_next(head, 1), Equals<StackEntry>({stateB, tree_len}));
2016-01-28 21:18:57 -08:00
ts_tree_release(parent);
});
});
2015-07-10 17:50:38 -07:00
});
describe("popping from a stack head that has been merged", [&]() {
before_each([&]() {
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D <--4-- E*
* ^ |
* `---5--- F <--6-- G <--7---'
2015-07-10 17:50:38 -07:00
*/
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[0], stateA);
ts_stack_push(stack, 0, trees[1], stateB);
2015-09-18 18:04:52 -07:00
ts_stack_split(stack, 0);
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[2], stateC);
ts_stack_push(stack, 0, trees[3], stateD);
ts_stack_push(stack, 0, trees[4], stateE);
ts_stack_push(stack, 1, trees[5], stateF);
ts_stack_push(stack, 1, trees[6], stateG);
ts_stack_push(stack, 1, trees[7], stateE);
2015-11-20 00:01:53 -08:00
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_top_state(stack, 0), Equals(stateE));
2015-11-20 00:01:53 -08:00
AssertThat(ts_stack_entry_next_count(ts_stack_head(stack, 0)), Equals(2));
2015-07-10 17:50:38 -07:00
});
describe("when there are two paths that lead to two different heads", [&]() {
it("returns an entry for each revealed head", [&]() {
2015-06-03 09:44:13 -07:00
/*
* . <--0-- A <--1-- B <--2-- C*
* ^
* `---5--- F*
2015-06-03 09:44:13 -07:00
*/
2016-02-17 20:41:29 -08:00
StackPopResultArray results = ts_stack_pop(stack, 0, 2, false);
2015-06-03 09:44:13 -07:00
2016-02-17 14:45:00 -08:00
AssertThat(results.size, Equals<size_t>(2));
StackPopResult result1 = results.contents[0];
AssertThat(result1.head_index, Equals(0));
AssertThat(ts_stack_top_state(stack, 0), Equals(stateC));
AssertThat(result1.trees, Equals(vector<TSTree *>({ trees[3], trees[4] })));
2015-07-10 17:50:38 -07:00
StackPopResult result2 = results.contents[1];
AssertThat(result2.head_index, Equals(1));
AssertThat(ts_stack_top_state(stack, 1), Equals(stateF));
AssertThat(result2.trees, Equals(vector<TSTree *>({ trees[6], trees[7] })));
2015-07-10 17:50:38 -07:00
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head_count(stack), Equals(2));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({stateC, tree_len * 3}));
AssertThat(*ts_stack_head(stack, 1), Equals<StackEntry>({stateF, tree_len * 3}));
2016-01-28 21:18:57 -08:00
2016-02-17 14:45:00 -08:00
free_pop_results(&results);
2015-07-10 17:50:38 -07:00
});
});
describe("when there is one path, leading to one head", [&]() {
it("returns a single entry", [&]() {
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D <--4-- E <--8--H*
* ^ |
* `---5--- F <--6-- G <--7---'
2015-07-10 17:50:38 -07:00
*/
2016-02-25 21:46:13 -08:00
AssertThat(ts_stack_push(stack, 0, trees[8], stateH), Equals(StackPushResultContinued));
2015-09-18 18:04:52 -07:00
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_top_state(stack, 0), Equals(stateH));
2015-07-10 17:50:38 -07:00
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D <--4-- E*
* ^ |
* `---5--- F <--6-- G <--7---'
2015-07-10 17:50:38 -07:00
*/
2016-02-17 20:41:29 -08:00
StackPopResultArray results = ts_stack_pop(stack, 0, 1, false);
2016-02-17 14:45:00 -08:00
AssertThat(results.size, Equals<size_t>(1));
StackPopResult result1 = results.contents[0];
AssertThat(result1.head_index, Equals(0));
AssertThat(result1.trees, Equals(vector<TSTree *>({ trees[8] })));
2015-11-20 00:01:53 -08:00
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_top_state(stack, 0), Equals(stateE));
2016-01-28 21:18:57 -08:00
2016-02-17 14:45:00 -08:00
free_pop_results(&results);
2015-06-18 16:34:52 -07:00
});
2015-07-10 17:50:38 -07:00
});
2015-06-18 16:34:52 -07:00
2015-07-10 17:50:38 -07:00
describe("when there are two paths that converge at the same head", [&]() {
describe("when the first path is preferred by the callback", [&]() {
it("returns one entry for that head, with the first path of trees", [&]() {
tree_selection_spy.tree_to_return = trees[2];
/*
* . <--0-- A <--1-- B*
*/
2016-02-17 20:41:29 -08:00
StackPopResultArray results = ts_stack_pop(stack, 0, 3, false);
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({stateB, tree_len * 2}));
2016-02-17 14:45:00 -08:00
AssertThat(results.size, Equals<size_t>(1));
StackPopResult result1 = results.contents[0];
AssertThat(result1.head_index, Equals(0));
AssertThat(result1.trees, Equals(vector<TSTree *>({ trees[2], trees[3], trees[4] })));
2016-02-17 14:45:00 -08:00
free_pop_results(&results);
});
});
2016-01-28 21:18:57 -08:00
describe("when the second path is preferred by the callback", [&]() {
it("returns one entry for that head, with the second path of trees", [&]() {
tree_selection_spy.tree_to_return = trees[4];
/*
* . <--0-- A <--1-- B*
*/
2016-02-17 20:41:29 -08:00
StackPopResultArray results = ts_stack_pop(stack, 0, 3, false);
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(*ts_stack_head(stack, 0), Equals<StackEntry>({stateB, tree_len * 2}));
2016-02-17 14:45:00 -08:00
AssertThat(results.size, Equals<size_t>(1));
StackPopResult result1 = results.contents[0];
AssertThat(result1.head_index, Equals(0));
AssertThat(result1.trees, Equals(vector<TSTree *>({ trees[5], trees[6], trees[7] })))
2016-02-17 14:45:00 -08:00
free_pop_results(&results);
});
2015-11-20 00:01:53 -08:00
});
});
});
describe("popping from a stack head that has been 3-way merged", [&]() {
before_each([&]() {
/*
* . <--0-- A <--1-- B <--2-- C <--3-- D <--10-- I
* ^ |
* `---4--- E <--5-- F <--6---'
* | |
* `---7--- G <--8-- H <--9---'
2015-11-20 00:01:53 -08:00
*/
ts_stack_clear(stack);
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[0], stateA);
2015-11-20 00:01:53 -08:00
ts_stack_split(stack, 0);
ts_stack_split(stack, 1);
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[1], stateB);
ts_stack_push(stack, 0, trees[2], stateC);
ts_stack_push(stack, 0, trees[3], stateD);
ts_stack_push(stack, 1, trees[4], stateE);
ts_stack_push(stack, 1, trees[5], stateF);
ts_stack_push(stack, 1, trees[6], stateD);
ts_stack_push(stack, 1, trees[7], stateG);
ts_stack_push(stack, 1, trees[8], stateH);
ts_stack_push(stack, 1, trees[9], stateD);
2015-11-20 00:01:53 -08:00
AssertThat(ts_stack_head_count(stack), Equals(1));
AssertThat(ts_stack_entry_next_count(ts_stack_head(stack, 0)), Equals(3));
2016-02-25 21:46:13 -08:00
ts_stack_push(stack, 0, trees[10], stateI);
AssertThat(ts_stack_entry_next_count(ts_stack_head(stack, 0)), Equals(1));
2015-11-20 00:01:53 -08:00
});
describe("when there are three different paths that lead to three different heads", [&]() {
it("returns three entries with different arrays of trees", [&]() {
/*
* . <--0-- A <--1-- B <--2-- C*
* ^
* `---4--- E <--5-- F*
* |
* `---7--- G <--8-- H*
2015-11-20 00:01:53 -08:00
*/
StackPopResultArray results = ts_stack_pop(stack, 0, 2, false);
2015-11-20 00:01:53 -08:00
AssertThat(ts_stack_head_count(stack), Equals(3));
2016-02-17 14:45:00 -08:00
AssertThat(results.size, Equals<size_t>(3));
2015-11-20 00:01:53 -08:00
StackPopResult result1 = results.contents[0];
AssertThat(ts_stack_top_state(stack, 0), Equals(stateC));
AssertThat(result1.head_index, Equals(0));
AssertThat(result1.trees, Equals(vector<TSTree *>({ trees[3], trees[10] })))
2015-11-20 00:01:53 -08:00
StackPopResult result2 = results.contents[1];
AssertThat(ts_stack_top_state(stack, 1), Equals(stateF));
AssertThat(result2.head_index, Equals(1));
AssertThat(result2.trees, Equals(vector<TSTree *>({ trees[6], trees[10] })))
2015-11-20 00:01:53 -08:00
StackPopResult result3 = results.contents[2];
AssertThat(ts_stack_top_state(stack, 2), Equals(stateH));
AssertThat(result3.head_index, Equals(2));
AssertThat(result3.trees, Equals(vector<TSTree *>({ trees[9], trees[10] })))
2016-01-28 21:18:57 -08:00
2016-02-17 14:45:00 -08:00
free_pop_results(&results);
});
});
2015-05-25 20:21:13 -07:00
});
});
END_TEST
2015-06-03 09:44:13 -07:00
2015-09-18 18:04:52 -07:00
bool operator==(const StackEntry &left, const StackEntry &right) {
return left.state == right.state && ts_length_eq(left.position, right.position);
2015-06-03 09:44:13 -07:00
}
2015-09-18 18:04:52 -07:00
std::ostream &operator<<(std::ostream &stream, const StackEntry &entry) {
return stream << "{" << entry.state << ", " << entry.position << "}";
}
std::ostream &operator<<(std::ostream &stream, const TreeArray &array) {
stream << "[";
bool first = true;
for (size_t i = 0; i < array.size; i++) {
if (!first)
stream << ", ";
first = false;
stream << array.contents[i];
}
return stream << "]";
2015-06-03 09:44:13 -07:00
}