From 32e94081fa7d7f0dddfdc1d4c9f5ee2b4e115f71 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 4 Apr 2014 08:07:46 -0700 Subject: [PATCH] Backfill tests --- .../build_tables/item_set_transitions_spec.cc | 32 ++++++++++++ .../build_tables/merge_transitions_spec.cc | 52 ++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 spec/compiler/build_tables/item_set_transitions_spec.cc diff --git a/spec/compiler/build_tables/item_set_transitions_spec.cc b/spec/compiler/build_tables/item_set_transitions_spec.cc new file mode 100644 index 00000000..85cf5542 --- /dev/null +++ b/spec/compiler/build_tables/item_set_transitions_spec.cc @@ -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({ {'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 \ No newline at end of file diff --git a/spec/compiler/build_tables/merge_transitions_spec.cc b/spec/compiler/build_tables/merge_transitions_spec.cc index f22df29e..daef24a2 100644 --- a/spec/compiler/build_tables/merge_transitions_spec.cc +++ b/spec/compiler/build_tables/merge_transitions_spec.cc @@ -10,10 +10,58 @@ describe("merging character set transitions", []() { typedef map 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(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(map2, map1, bitwise), Equals(merge_char_transitions(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(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(map2, map1, bitwise), Equals(merge_char_transitions(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 },