2014-06-23 18:50:03 -07:00
|
|
|
#include "runtime/runtime_spec_helper.h"
|
2014-07-14 21:11:15 -07:00
|
|
|
#include "runtime/tree.h"
|
2014-01-07 21:50:32 -08:00
|
|
|
|
|
|
|
|
START_TEST
|
|
|
|
|
|
2014-06-02 13:32:36 -07:00
|
|
|
enum {
|
|
|
|
|
cat = 2,
|
|
|
|
|
dog = 3,
|
|
|
|
|
pig = 4,
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-04 13:46:18 -07:00
|
|
|
static const char *names[] = { "error", "end", "cat", "dog", "pig" };
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2014-08-27 12:56:36 -07:00
|
|
|
describe("Tree", []() {
|
2014-08-06 13:00:35 -07:00
|
|
|
TSTree *tree1, *tree2, *parent1;
|
|
|
|
|
|
|
|
|
|
before_each([&]() {
|
2014-09-02 07:41:29 -07:00
|
|
|
tree1 = ts_tree_make_leaf(cat, 5, 2, 0);
|
|
|
|
|
tree2 = ts_tree_make_leaf(cat, 3, 1, 0);
|
2014-08-27 12:56:36 -07:00
|
|
|
parent1 = ts_tree_make_node(dog, 2, tree_array({ tree1, tree2, }), 0);
|
2014-08-06 13:00:35 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
after_each([&]() {
|
|
|
|
|
ts_tree_release(tree1);
|
|
|
|
|
ts_tree_release(tree2);
|
|
|
|
|
ts_tree_release(parent1);
|
|
|
|
|
});
|
|
|
|
|
|
2014-08-28 13:22:06 -07:00
|
|
|
describe("building a parent node", [&]() {
|
2014-08-06 13:00:35 -07:00
|
|
|
it("computes its size based on its child nodes", [&]() {
|
2014-09-02 07:41:29 -07:00
|
|
|
AssertThat(parent1->size, Equals<size_t>(9));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("computes its padding based on its first child", [&]() {
|
|
|
|
|
AssertThat(parent1->padding, Equals<size_t>(2));
|
2014-08-28 13:22:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("computes the offset of each child node", [&]() {
|
|
|
|
|
size_t count;
|
|
|
|
|
TSTreeChild *children = ts_tree_visible_children(parent1, &count);
|
|
|
|
|
|
|
|
|
|
AssertThat(count, Equals<size_t>(2));
|
|
|
|
|
AssertThat(children[0].tree, Equals(tree1));
|
|
|
|
|
AssertThat(children[0].offset, Equals<size_t>(0));
|
|
|
|
|
AssertThat(children[1].tree, Equals(tree2));
|
2014-09-02 07:41:29 -07:00
|
|
|
AssertThat(children[1].offset, Equals<size_t>(
|
|
|
|
|
tree1->size + tree2->padding));
|
2014-08-28 13:22:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("when one of the child nodes is hidden", [&]() {
|
|
|
|
|
TSTree *grandparent, *tree3;
|
|
|
|
|
|
|
|
|
|
before_each([&]() {
|
|
|
|
|
parent1->options = TSTreeOptionsHidden;
|
2014-09-02 07:41:29 -07:00
|
|
|
tree3 = ts_tree_make_leaf(cat, 8, 5, 0);
|
2014-08-28 13:22:06 -07:00
|
|
|
grandparent = ts_tree_make_node(pig, 2, tree_array({
|
|
|
|
|
parent1,
|
|
|
|
|
tree3,
|
|
|
|
|
}), 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
after_each([&]() {
|
|
|
|
|
ts_tree_release(tree3);
|
|
|
|
|
ts_tree_release(grandparent);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("claims the hidden node's children as its own", [&]() {
|
|
|
|
|
size_t count;
|
|
|
|
|
TSTreeChild *children = ts_tree_visible_children(grandparent, &count);
|
|
|
|
|
|
|
|
|
|
AssertThat(count, Equals<size_t>(3));
|
|
|
|
|
AssertThat(children[0].tree, Equals(tree1));
|
|
|
|
|
AssertThat(children[0].offset, Equals<size_t>(0));
|
|
|
|
|
AssertThat(children[1].tree, Equals(tree2));
|
2014-09-02 07:41:29 -07:00
|
|
|
AssertThat(children[1].offset, Equals<size_t>(
|
|
|
|
|
tree1->size + tree2->padding));
|
2014-08-28 13:22:06 -07:00
|
|
|
AssertThat(children[2].tree, Equals(tree3));
|
2014-09-02 07:41:29 -07:00
|
|
|
AssertThat(children[2].offset, Equals<size_t>(
|
|
|
|
|
tree1->size + tree2->padding + tree2->size + tree3->padding));
|
2014-08-28 13:22:06 -07:00
|
|
|
});
|
2014-08-06 13:00:35 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("equality", [&]() {
|
|
|
|
|
it("returns true for identical trees", [&]() {
|
2014-09-02 07:41:29 -07:00
|
|
|
TSTree *tree1_copy = ts_tree_make_leaf(cat, 5, 2, 0);
|
2014-08-06 13:00:35 -07:00
|
|
|
AssertThat(ts_tree_equals(tree1, tree1_copy), Equals(1));
|
2014-09-02 07:41:29 -07:00
|
|
|
TSTree *tree2_copy = ts_tree_make_leaf(cat, 3, 1, 0);
|
2014-08-06 13:00:35 -07:00
|
|
|
AssertThat(ts_tree_equals(tree2, tree2_copy), Equals(1));
|
|
|
|
|
|
|
|
|
|
TSTree *parent2 = ts_tree_make_node(dog, 2, tree_array({
|
|
|
|
|
tree1_copy, tree2_copy,
|
|
|
|
|
}), 0);
|
|
|
|
|
AssertThat(ts_tree_equals(parent1, parent2), Equals(1));
|
|
|
|
|
|
|
|
|
|
ts_tree_release(tree1_copy);
|
|
|
|
|
ts_tree_release(tree2_copy);
|
|
|
|
|
ts_tree_release(parent2);
|
2014-01-07 21:50:32 -08:00
|
|
|
});
|
2014-06-09 21:14:38 -07:00
|
|
|
|
2014-08-06 13:00:35 -07:00
|
|
|
it("returns false for trees with different symbols", [&]() {
|
2014-09-02 07:41:29 -07:00
|
|
|
TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0, 0);
|
2014-08-06 13:00:35 -07:00
|
|
|
AssertThat(ts_tree_equals(tree1, different_tree), Equals(0));
|
|
|
|
|
ts_tree_release(different_tree);
|
2014-06-02 13:32:36 -07:00
|
|
|
});
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2014-08-06 13:00:35 -07:00
|
|
|
it("returns false for trees with different children", [&]() {
|
2014-09-02 07:41:29 -07:00
|
|
|
TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0, 0);
|
2014-08-06 13:00:35 -07:00
|
|
|
TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({
|
|
|
|
|
different_tree, different_tree,
|
|
|
|
|
}), 0);
|
|
|
|
|
|
|
|
|
|
AssertThat(ts_tree_equals(different_parent, parent1), Equals(0));
|
|
|
|
|
AssertThat(ts_tree_equals(parent1, different_parent), Equals(0));
|
|
|
|
|
|
|
|
|
|
ts_tree_release(different_tree);
|
|
|
|
|
ts_tree_release(different_parent);
|
2014-01-07 21:50:32 -08:00
|
|
|
});
|
2014-08-06 13:00:35 -07:00
|
|
|
});
|
2014-03-08 15:26:27 -08:00
|
|
|
|
2014-08-06 13:00:35 -07:00
|
|
|
describe("serialization", [&]() {
|
|
|
|
|
it("returns a readable string", [&]() {
|
2014-08-27 12:56:36 -07:00
|
|
|
char *string1 = ts_tree_string(tree1, names);
|
2014-08-06 13:00:35 -07:00
|
|
|
AssertThat(string(string1), Equals("(cat)"));
|
|
|
|
|
free(string1);
|
2014-06-09 21:16:33 -07:00
|
|
|
|
2014-08-27 12:56:36 -07:00
|
|
|
char *string2 = ts_tree_string(parent1, names);
|
2014-08-06 13:00:35 -07:00
|
|
|
AssertThat(string(string2), Equals("(dog (cat) (cat))"));
|
|
|
|
|
free(string2);
|
2014-01-07 21:50:32 -08:00
|
|
|
});
|
2014-08-27 12:56:36 -07:00
|
|
|
|
|
|
|
|
it("hides invisible nodes", [&]() {
|
|
|
|
|
tree2->options = TSTreeOptionsHidden;
|
|
|
|
|
|
|
|
|
|
char *string1 = ts_tree_string(parent1, names);
|
|
|
|
|
AssertThat(string(string1), Equals("(dog (cat))"));
|
|
|
|
|
free(string1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("when the root node is not visible", [&]() {
|
|
|
|
|
it("still serializes it", [&]() {
|
|
|
|
|
parent1->options = TSTreeOptionsHidden;
|
|
|
|
|
|
|
|
|
|
char *string1 = ts_tree_string(parent1, names);
|
|
|
|
|
AssertThat(string(string1), Equals("(dog (cat) (cat))"));
|
|
|
|
|
free(string1);
|
|
|
|
|
|
|
|
|
|
tree1->options = TSTreeOptionsHidden;
|
|
|
|
|
|
|
|
|
|
char *string2 = ts_tree_string(tree1, names);
|
|
|
|
|
AssertThat(string(string2), Equals("(cat)"));
|
|
|
|
|
free(string2);
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-08-06 13:00:35 -07:00
|
|
|
});
|
2014-01-07 21:50:32 -08:00
|
|
|
});
|
|
|
|
|
|
2014-03-08 15:26:27 -08:00
|
|
|
END_TEST
|