2014-02-05 18:56:04 -08:00
|
|
|
#include "spec_helper.h"
|
2014-02-18 09:07:00 -08:00
|
|
|
#include "rules/character_set.h"
|
2014-02-05 18:56:04 -08:00
|
|
|
|
|
|
|
|
using namespace rules;
|
|
|
|
|
|
|
|
|
|
START_TEST
|
|
|
|
|
|
|
|
|
|
describe("character sets", []() {
|
2014-02-08 15:26:10 -08:00
|
|
|
char max_char = 255;
|
|
|
|
|
|
|
|
|
|
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-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-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-02-06 09:12:03 -08:00
|
|
|
|
|
|
|
|
set = CharacterSet({ 'c' });
|
|
|
|
|
auto c = set.complement();
|
2014-02-07 12:57:35 -08:00
|
|
|
set.add_set(c);
|
2014-02-16 22:13:08 -08:00
|
|
|
AssertThat(set, Equals(CharacterSet({ {0, max_char} })));
|
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-02-06 09:12:03 -08: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-02-07 12:57:35 -08:00
|
|
|
|
|
|
|
|
describe("computing differences", []() {
|
|
|
|
|
it("works for disjoint sets", []() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','z'} });
|
|
|
|
|
set1.remove_set(CharacterSet({ {'A','Z'} }));
|
|
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'z'} })));
|
2014-02-07 12:57:35 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("works when one set spans the other", []() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','z'} });
|
|
|
|
|
set1.remove_set(CharacterSet({ {'d','s'} }));
|
2014-02-07 12:57:35 -08:00
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'c'}, {'t', 'z'} })));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("works for sets that overlap", []() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','s'} });
|
|
|
|
|
set1.remove_set(CharacterSet({ {'m','z'} }));
|
|
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'l'} })));
|
2014-02-07 12:57:35 -08:00
|
|
|
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set2({ {'m','z'} });
|
|
|
|
|
set2.remove_set(CharacterSet({ {'a','s'} }));
|
|
|
|
|
AssertThat(set2, Equals(CharacterSet({ {'t', 'z'} })));
|
2014-02-07 12:57:35 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("works for sets with multiple ranges", []() {
|
|
|
|
|
CharacterSet set1({ {'a','d'}, {'m', 'z'} });
|
|
|
|
|
set1.remove_set(CharacterSet({ {'c','o'}, {'s','x'} }));
|
|
|
|
|
AssertThat(set1, Equals(CharacterSet({ {'a', 'b'}, {'p','r'}, {'y','z'} })));
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-02-08 15:26:10 -08:00
|
|
|
|
|
|
|
|
describe("computing intersections", []() {
|
|
|
|
|
it("returns an empty set for disjoint sets", []() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','d'} });
|
|
|
|
|
CharacterSet set2({ {'e','x'} });
|
2014-02-08 15:26:10 -08:00
|
|
|
AssertThat(set1.intersect(set2), Equals(CharacterSet()));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("works for sets with a single overlapping range", []() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','e'} });
|
|
|
|
|
CharacterSet set2({ {'c','x'} });
|
|
|
|
|
AssertThat(set1.intersect(set2), Equals(CharacterSet({ {'c', 'e'} })));
|
2014-02-08 15:26:10 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("works for sets with two overlapping ranges", []() {
|
2014-02-16 22:13:08 -08:00
|
|
|
CharacterSet set1({ {'a','e'}, {'w','z'} });
|
|
|
|
|
CharacterSet set2({ {'c','y'} });
|
2014-02-08 15:26:10 -08:00
|
|
|
AssertThat(set1.intersect(set2), Equals(CharacterSet({ {'c', 'e'}, {'w', 'y'} })));
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-02-05 18:56:04 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
END_TEST
|