Backfill tests

This commit is contained in:
Max Brunsfeld 2014-04-04 08:07:46 -07:00
parent 5a00ebbc99
commit 32e94081fa
2 changed files with 82 additions and 2 deletions

View file

@ -0,0 +1,32 @@
#include "compiler_spec_helper.h"
#include "compiler/build_tables/item_set_transitions.h"
#include "compiler/prepared_grammar.h"
using namespace rules;
using namespace build_tables;
START_TEST
describe("item set transitions", []() {
PreparedGrammar grammar({}, {});
describe("when two items in the set have transitions on the same character", [&]() {
it("merges the transitions by computing the union of the two item sets", [&]() {
LexItemSet set1({
LexItem(Symbol("A"), pattern("[a-f]")),
LexItem(Symbol("B"), pattern("[e-x]")) });
AssertThat(char_transitions(set1, grammar), Equals(map<CharacterSet, LexItemSet>({
{ CharacterSet({ {'a', 'd'} }), LexItemSet({
LexItem(Symbol("A"), blank()) }) },
{ CharacterSet({ {'e', 'f'} }), LexItemSet({
LexItem(Symbol("A"), blank()),
LexItem(Symbol("B"), blank()) }) },
{ CharacterSet({ {'g', 'x'} }), LexItemSet({
LexItem(Symbol("B"), blank()) }) },
})));
});
});
});
END_TEST

View file

@ -10,10 +10,58 @@ describe("merging character set transitions", []() {
typedef map<CharacterSet, int> int_map;
auto bitwise = [](int l, int r) -> int {
return l + r;
return l | r;
};
describe("when two of the right transitions intersect one of the left transitions", [&]() {
describe("when none of the transitions intersect", [&]() {
it("returns the union of the two sets of transitions", [&]() {
int_map map1({
{ CharacterSet({ 'a', 'c' }), 1 },
{ CharacterSet({ 'x', 'y' }), 2 },
{ CharacterSet({ '1', '9' }), 4 },
});
int_map map2({
{ CharacterSet({ ' ' }), 8 },
{ CharacterSet({ '\t' }), 16 },
});
AssertThat(merge_char_transitions<int>(map1, map2, bitwise), Equals(int_map({
{ CharacterSet({ 'a', 'c' }), 1 },
{ CharacterSet({ 'x', 'y' }), 2 },
{ CharacterSet({ '1', '9' }), 4 },
{ CharacterSet({ ' ' }), 8 },
{ CharacterSet({ '\t' }), 16 },
})));
AssertThat(merge_char_transitions<int>(map2, map1, bitwise), Equals(merge_char_transitions<int>(map1, map2, bitwise)));
});
});
describe("when transitions intersect", [&]() {
it("merges the intersecting transitions using the provided function", [&]() {
int_map map1({
{ CharacterSet({ {'a', 'f'}, {'A', 'F'} }), 1 },
{ CharacterSet({ {'0', '9'} }), 2 },
});
int_map map2({
{ CharacterSet({ 'c' }), 4 },
{ CharacterSet({ '3' }), 8 },
});
AssertThat(merge_char_transitions<int>(map1, map2, bitwise), Equals(int_map({
{ CharacterSet({ {'a', 'b'}, {'d', 'f'}, {'A', 'F'} }), 1 },
{ CharacterSet({ {'c'} }), 5 },
{ CharacterSet({ {'0', '2'}, {'4', '9'} }), 2 },
{ CharacterSet({ '3' }), 10 },
})));
AssertThat(merge_char_transitions<int>(map2, map1, bitwise), Equals(merge_char_transitions<int>(map1, map2, bitwise)));
});
});
describe("when two of the right transitions intersect the same left transition", [&]() {
it("splits the left-hand transition correctly", [&]() {
int_map map1({
{ CharacterSet({ 'a', 'c' }), 1 },