2014-06-23 18:50:03 -07:00
|
|
|
#include "compiler/compiler_spec_helper.h"
|
2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/rules/character_set.h"
|
2014-02-05 18:56:04 -08:00
|
|
|
|
|
|
|
|
using namespace rules;
|
|
|
|
|
|
|
|
|
|
START_TEST
|
|
|
|
|
|
|
|
|
|
describe("character sets", []() {
|
2014-03-31 18:47:18 -07:00
|
|
|
unsigned char max_char = 255;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-08 15:26:10 -08:00
|
|
|
describe("computing the complement", [&]() {
|
|
|
|
|
it("works for the set containing only the null character", [&]() {
|
2014-02-05 18:56:04 -08:00
|
|
|
CharacterSet set1({ '\0' });
|
|
|
|
|
auto set2 = set1.complement();
|
|
|
|
|
AssertThat(set2, Equals(CharacterSet({
|
2014-02-16 22:13:08 -08:00
|
|
|
{ 1, max_char }
|
|
|
|
|
})));
|
2014-02-05 18:56:04 -08:00
|
|
|
AssertThat(set2.complement(), Equals(set1));
|
|
|
|
|
});
|
|
|
|
|
|
2014-02-08 15:26:10 -08:00
|
|
|
it("works for single character sets", [&]() {
|
2014-02-05 18:56:04 -08:00
|
|
|
CharacterSet set1({ 'b' });
|
|
|
|
|
auto set2 = set1.complement();
|
|
|
|
|
AssertThat(set2, Equals(CharacterSet({
|
|
|
|
|
{ 0, 'a' },
|
2014-02-08 15:26:10 -08:00
|
|
|
{ 'c', max_char },
|
2014-02-05 18:56:04 -08:00
|
|
|
})));
|
|
|
|
|
AssertThat(set2.complement(), Equals(set1));
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-08 15:26:10 -08:00
|
|
|
describe("computing unions", [&]() {
|
|
|
|
|
it("works for disjoint sets", [&]() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set({ {'a', 'z'} });
|
|
|
|
|
set.add_set(CharacterSet({ {'A', 'Z'} }));
|
|
|
|
|
AssertThat(set, Equals(CharacterSet({ {'a', 'z'}, {'A', 'Z'} })));
|
2014-02-05 18:56:04 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-08 15:26:10 -08:00
|
|
|
it("works for sets with adjacent ranges", [&]() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set({ CharacterRange('a', 'r') });
|
|
|
|
|
set.add_set(CharacterSet({ CharacterRange('s', 'z') }));
|
|
|
|
|
AssertThat(set, Equals(CharacterSet({ {'a', 'z'} })));
|
2014-03-31 18:38:54 -07:00
|
|
|
});
|
2014-04-04 13:10:55 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
it("becomes the complete set when the complement is added", [&]() {
|
|
|
|
|
CharacterSet set({ 'c' });
|
|
|
|
|
auto complement = set.complement();
|
|
|
|
|
set.add_set(complement);
|
2014-02-16 22:13:08 -08:00
|
|
|
AssertThat(set, Equals(CharacterSet({ {0, max_char} })));
|
2014-02-05 18:56:04 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-05 18:56:04 -08:00
|
|
|
it("works when the result becomes a continuous range", []() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set({ {'a', 'd'}, {'f', 'z'} });
|
|
|
|
|
set.add_set(CharacterSet({ {'c', 'g'} }));
|
|
|
|
|
AssertThat(set, Equals(CharacterSet({ {'a', 'z'} })));
|
2014-02-05 18:56:04 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-08 15:26:10 -08:00
|
|
|
it("does nothing for the set of all characters", [&]() {
|
2014-02-06 12:58:00 -08:00
|
|
|
CharacterSet set({ 'a' });
|
2014-02-07 12:57:35 -08:00
|
|
|
set.add_set(set.complement());
|
2014-02-16 22:13:08 -08:00
|
|
|
AssertThat(set, Equals(CharacterSet({ {'\0', max_char} })));
|
2014-02-06 09:12:03 -08:00
|
|
|
});
|
2014-02-05 18:56:04 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
describe("subtracting sets", []() {
|
|
|
|
|
CharacterSet intersection;
|
2014-04-04 13:10:55 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
it("works for disjoint sets", [&]() {
|
2014-03-09 20:20:49 -07:00
|
|
|
CharacterSet set1({ {'a', 'z'} });
|
2014-03-31 18:38:54 -07:00
|
|
|
intersection = set1.remove_set(CharacterSet({ {'A', 'Z'} }));
|
2014-02-16 22:13:08 -08:00
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'z'} })));
|
2014-03-31 18:38:54 -07:00
|
|
|
AssertThat(intersection, Equals(CharacterSet()));
|
2014-02-07 12:57:35 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
it("works when one set is a proper subset of the other", [&]() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','z'} });
|
2014-03-31 18:38:54 -07:00
|
|
|
intersection = set1.remove_set(CharacterSet({ {'d', 's'} }));
|
2014-02-07 12:57:35 -08:00
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'c'}, {'t', 'z'} })));
|
2014-03-31 18:38:54 -07:00
|
|
|
AssertThat(intersection, Equals(CharacterSet({ {'d', 's'} })));
|
2014-02-07 12:57:35 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
it("works for a set that overlaps the right side", [&]() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','s'} });
|
2014-03-31 18:38:54 -07:00
|
|
|
intersection = set1.remove_set(CharacterSet({ {'m', 'z'} }));
|
2014-02-16 22:13:08 -08:00
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'l'} })));
|
2014-03-31 18:38:54 -07:00
|
|
|
AssertThat(intersection, Equals(CharacterSet({ {'m', 's'} })));
|
|
|
|
|
});
|
2014-04-04 13:10:55 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
it("works for a set that overlaps the left side", [&]() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set2({ {'m','z'} });
|
2014-03-31 18:38:54 -07:00
|
|
|
intersection = set2.remove_set(CharacterSet({ {'a', 's'} }));
|
2014-02-16 22:13:08 -08:00
|
|
|
AssertThat(set2, Equals(CharacterSet({ {'t', 'z'} })));
|
2014-03-31 18:38:54 -07:00
|
|
|
AssertThat(intersection, Equals(CharacterSet({ {'m', 's'} })));
|
2014-02-07 12:57:35 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
it("works for sets with multiple ranges", [&]() {
|
2014-03-09 20:20:49 -07:00
|
|
|
CharacterSet set1({ {'a', 'd'}, {'m', 'z'} });
|
2014-03-31 18:38:54 -07:00
|
|
|
intersection = set1.remove_set(CharacterSet({ {'c', 'o'}, {'s', 'x'} }));
|
2014-03-09 20:20:49 -07:00
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'b'}, {'p', 'r'}, {'y', 'z'} })));
|
2014-03-31 18:38:54 -07:00
|
|
|
AssertThat(intersection, Equals(CharacterSet({ {'c', 'd'}, {'m', 'o'}, {'s', 'x'} })));
|
2014-02-07 12:57:35 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-31 18:38:54 -07:00
|
|
|
it("works when the result is empty", [&]() {
|
|
|
|
|
CharacterSet set1({ 'd' });
|
|
|
|
|
intersection = set1.remove_set(CharacterSet({ 'a', 'd', 'x' }));
|
|
|
|
|
AssertThat(set1, Equals(CharacterSet()));
|
|
|
|
|
AssertThat(intersection, Equals(CharacterSet({ 'd' })));
|
2014-03-24 19:17:25 -07:00
|
|
|
});
|
2014-02-08 15:26:10 -08:00
|
|
|
});
|
2014-02-05 18:56:04 -08:00
|
|
|
});
|
|
|
|
|
|
2014-03-09 20:20:49 -07:00
|
|
|
END_TEST
|