Implement CharacterSet intersections

This commit is contained in:
Max Brunsfeld 2014-02-08 15:26:10 -08:00
parent b01c672fca
commit e92ac719f4
3 changed files with 54 additions and 25 deletions

View file

@ -6,35 +6,37 @@ using namespace rules;
START_TEST
describe("character sets", []() {
describe("computing the complement", []() {
it("works for the set containing only the null character", []() {
char max_char = 255;
describe("computing the complement", [&]() {
it("works for the set containing only the null character", [&]() {
CharacterSet set1({ '\0' });
auto set2 = set1.complement();
AssertThat(set2, Equals(CharacterSet({
{ 1, -1 },
{ 1, max_char },
}, true)));
AssertThat(set2.complement(), Equals(set1));
});
it("works for single character sets", []() {
it("works for single character sets", [&]() {
CharacterSet set1({ 'b' });
auto set2 = set1.complement();
AssertThat(set2, Equals(CharacterSet({
{ 0, 'a' },
{ 'c', -1 },
{ 'c', max_char },
})));
AssertThat(set2.complement(), Equals(set1));
});
});
describe("computing unions", []() {
it("works for disjoint sets", []() {
describe("computing unions", [&]() {
it("works for disjoint sets", [&]() {
CharacterSet set({ {'a', 'z'} }, true);
set.add_set(CharacterSet({ {'A', 'Z'} }, true));
AssertThat(set, Equals(CharacterSet({ {'a', 'z'}, {'A', 'Z'}, })));
});
it("works for sets with adjacent ranges", []() {
it("works for sets with adjacent ranges", [&]() {
CharacterSet set({ {'a', 'r'} }, true);
set.add_set(CharacterSet({ {'s', 'z'} }, true));
AssertThat(set, Equals(CharacterSet({ {'a', 'z'} }, true)));
@ -42,7 +44,7 @@ describe("character sets", []() {
set = CharacterSet({ 'c' });
auto c = set.complement();
set.add_set(c);
AssertThat(set, Equals(CharacterSet({ {0, -1} }, true)));
AssertThat(set, Equals(CharacterSet({ {0, max_char} }, true)));
});
it("works when the result becomes a continuous range", []() {
@ -51,10 +53,10 @@ describe("character sets", []() {
AssertThat(set, Equals(CharacterSet({ {'a', 'z'} }, true)));
});
it("does nothing for the set of all characters", []() {
it("does nothing for the set of all characters", [&]() {
CharacterSet set({ 'a' });
set.add_set(set.complement());
AssertThat(set, Equals(CharacterSet({ {'\0', '\xff'} }, true)));
AssertThat(set, Equals(CharacterSet({ {'\0', max_char} }, true)));
});
});
@ -87,6 +89,26 @@ describe("character sets", []() {
AssertThat(set1, Equals(CharacterSet({ {'a', 'b'}, {'p','r'}, {'y','z'} })));
});
});
describe("computing intersections", []() {
it("returns an empty set for disjoint sets", []() {
CharacterSet set1({ {'a','d'} }, true);
CharacterSet set2({ {'e','x'} }, true);
AssertThat(set1.intersect(set2), Equals(CharacterSet()));
});
it("works for sets with a single overlapping range", []() {
CharacterSet set1({ {'a','e'} }, true);
CharacterSet set2({ {'c','x'} }, true);
AssertThat(set1.intersect(set2), Equals(CharacterSet({ {'c', 'e'} }, true)));
});
it("works for sets with two overlapping ranges", []() {
CharacterSet set1({ {'a','e'}, {'w','z'} }, true);
CharacterSet set2({ {'c','y'} }, true);
AssertThat(set1.intersect(set2), Equals(CharacterSet({ {'c', 'e'}, {'w', 'y'} })));
});
});
});
END_TEST