Add non-base-case construct tests and fix construct logic
This commit is contained in:
@@ -6,28 +6,34 @@
|
||||
#include "construct.h"
|
||||
#include "testing.h"
|
||||
|
||||
static bool
|
||||
accepts_from_state(const fsa_t *nfa, int state_id, const char *input)
|
||||
static const char *
|
||||
match_from_state(const fsa_t *nfa, int state_id, const char *input)
|
||||
{
|
||||
const fsa_state_t *state = &nfa->states[state_id];
|
||||
if ('\0' == *input)
|
||||
return state->final;
|
||||
|
||||
const bool final = state->final;
|
||||
const bool end_of_input = '\0' == *input;
|
||||
for (int i = 0; i < state->count; ++i) {
|
||||
if (EPSILON == state->rules[i].input
|
||||
&& accepts_from_state(nfa, state->rules[i].next, input))
|
||||
return true;
|
||||
if (*input == state->rules[i].input
|
||||
&& accepts_from_state(nfa, state->rules[i].next, input + 1))
|
||||
return true;
|
||||
if ((!final || !end_of_input) && EPSILON == state->rules[i].input) {
|
||||
const char *s
|
||||
= match_from_state(nfa, state->rules[i].next, input);
|
||||
if (NULL != s)
|
||||
return s;
|
||||
}
|
||||
if (!end_of_input && *input == state->rules[i].input) {
|
||||
const char *s
|
||||
= match_from_state(nfa, state->rules[i].next, input + 1);
|
||||
if (NULL != s)
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return final ? input : NULL;
|
||||
}
|
||||
|
||||
static bool accepts(const fsa_t *nfa, const char *input)
|
||||
static const char *match(const fsa_t *nfa, const char *input)
|
||||
{
|
||||
return accepts_from_state(nfa, nfa->initial, input);
|
||||
return match_from_state(nfa, nfa->initial, input);
|
||||
}
|
||||
|
||||
static void test_empty_expression(void)
|
||||
@@ -44,7 +50,7 @@ static void test_empty_expression(void)
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_TRUE(accepts(&fsa, ""));
|
||||
ASSERT_NOT_NULL(match(&fsa, ""));
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
@@ -65,8 +71,8 @@ static void test_literal_expression(void)
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_TRUE(accepts(&fsa, "a"));
|
||||
ASSERT_FALSE(accepts(&fsa, "b"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "a"));
|
||||
ASSERT_NULL(match(&fsa, "b"));
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
@@ -89,10 +95,14 @@ static void test_sequence(void)
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_TRUE(accepts(&fsa, "abc"));
|
||||
ASSERT_FALSE(accepts(&fsa, "a"));
|
||||
ASSERT_FALSE(accepts(&fsa, "ab"));
|
||||
ASSERT_FALSE(accepts(&fsa, "d"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "abc"));
|
||||
ASSERT_NULL(match(&fsa, "a"));
|
||||
ASSERT_NULL(match(&fsa, "ab"));
|
||||
ASSERT_NULL(match(&fsa, "d"));
|
||||
|
||||
const char *s = "abcd";
|
||||
const char *t = match(&fsa, s);
|
||||
ASSERT_EQ(s + 3, t);
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
@@ -116,10 +126,14 @@ static void test_union(void)
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_TRUE(accepts(&fsa, "a"));
|
||||
ASSERT_TRUE(accepts(&fsa, "b"));
|
||||
ASSERT_TRUE(accepts(&fsa, "c"));
|
||||
ASSERT_FALSE(accepts(&fsa, "d"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "a"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "b"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "c"));
|
||||
ASSERT_NULL(match(&fsa, "d"));
|
||||
|
||||
const char *s = "aa";
|
||||
const char *t = match(&fsa, s);
|
||||
ASSERT_EQ(s + 1, t);
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
@@ -139,10 +153,13 @@ static void test_star(void)
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_TRUE(accepts(&fsa, ""));
|
||||
ASSERT_TRUE(accepts(&fsa, "a"));
|
||||
ASSERT_TRUE(accepts(&fsa, "aaaaaa"));
|
||||
ASSERT_FALSE(accepts(&fsa, "b"));
|
||||
ASSERT_NOT_NULL(match(&fsa, ""));
|
||||
ASSERT_NOT_NULL(match(&fsa, "a"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "aaaaaa"));
|
||||
|
||||
const char *s = "b";
|
||||
const char *t = match(&fsa, s);
|
||||
ASSERT_EQ(s, t);
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
@@ -171,8 +188,97 @@ static void test_subexpression(void)
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_TRUE(accepts(&fsa, "a"));
|
||||
ASSERT_FALSE(accepts(&fsa, "b"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "a"));
|
||||
ASSERT_NULL(match(&fsa, "b"));
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
}
|
||||
|
||||
static void test_sequence_containing_starred_union(void)
|
||||
{
|
||||
// ab(c|d)*
|
||||
regex_term_t *inner_terms0 = malloc(1 * sizeof(regex_term_t));
|
||||
inner_terms0[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
inner_terms0[0].type = REGEX_TERM_LITERAL;
|
||||
inner_terms0[0].literal = 'c';
|
||||
regex_term_t *inner_terms1 = malloc(1 * sizeof(regex_term_t));
|
||||
inner_terms1[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
inner_terms1[0].type = REGEX_TERM_LITERAL;
|
||||
inner_terms1[0].literal = 'd';
|
||||
regex_sequence_t *inner_alternatives
|
||||
= malloc(2 * sizeof(regex_sequence_t));
|
||||
inner_alternatives[0].count = inner_alternatives[0].capacity = 1;
|
||||
inner_alternatives[0].contents = inner_terms0;
|
||||
inner_alternatives[1].count = inner_alternatives[1].capacity = 1;
|
||||
inner_alternatives[1].contents = inner_terms1;
|
||||
regex_term_t *terms = malloc(3 * sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms[0].type = REGEX_TERM_LITERAL;
|
||||
terms[0].literal = 'a';
|
||||
terms[1].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms[1].type = REGEX_TERM_LITERAL;
|
||||
terms[1].literal = 'b';
|
||||
terms[2].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms[2].type = REGEX_TERM_SUBEXPR;
|
||||
terms[2].subexpr.count = terms[2].subexpr.capacity = 2;
|
||||
terms[2].subexpr.contents = inner_alternatives;
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms;
|
||||
regex_t regex = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_NOT_NULL(match(&fsa, "ab"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "abc"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "abccc"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "abd"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "abddd"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "abcddcc"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "abddccd"));
|
||||
ASSERT_NULL(match(&fsa, "c"));
|
||||
ASSERT_NULL(match(&fsa, "d"));
|
||||
ASSERT_NULL(match(&fsa, "foo"));
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
}
|
||||
|
||||
static void
|
||||
test_union_of_single_term_and_sequence_containing_starred_term(void)
|
||||
{
|
||||
regex_term_t *terms0 = malloc(1 * sizeof(regex_term_t));
|
||||
terms0[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms0[0].type = REGEX_TERM_LITERAL;
|
||||
terms0[0].literal = 'a';
|
||||
regex_term_t *terms1 = malloc(2 * sizeof(regex_term_t));
|
||||
terms1[0].quantifier = REGEX_QUANTIFIER_STAR;
|
||||
terms1[0].type = REGEX_TERM_LITERAL;
|
||||
terms1[0].literal = 'b';
|
||||
terms1[1].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms1[1].type = REGEX_TERM_LITERAL;
|
||||
terms1[1].literal = 'c';
|
||||
regex_sequence_t *alternatives = malloc(2 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms0;
|
||||
alternatives[1].count = alternatives[1].capacity = 2;
|
||||
alternatives[1].contents = terms1;
|
||||
regex_t regex = { .count = 2, .capacity = 2, .contents = alternatives };
|
||||
|
||||
fsa_t fsa;
|
||||
construct(®ex, &fsa);
|
||||
|
||||
ASSERT_NOT_NULL(match(&fsa, "a"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "c"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "bc"));
|
||||
ASSERT_NOT_NULL(match(&fsa, "bbbbbc"));
|
||||
ASSERT_NULL(match(&fsa, "foo"));
|
||||
|
||||
const char *s = "ba";
|
||||
const char *t = match(&fsa, s);
|
||||
ASSERT_EQ(s + 1, t);
|
||||
|
||||
regex_free(®ex);
|
||||
fsa_free(&fsa);
|
||||
@@ -181,11 +287,18 @@ static void test_subexpression(void)
|
||||
int main(void)
|
||||
{
|
||||
TESTING_BEGIN();
|
||||
|
||||
// Base cases
|
||||
test_empty_expression();
|
||||
test_literal_expression();
|
||||
test_sequence();
|
||||
test_union();
|
||||
test_star();
|
||||
test_subexpression();
|
||||
|
||||
// Compound expressions
|
||||
test_sequence_containing_starred_union();
|
||||
test_union_of_single_term_and_sequence_containing_starred_term();
|
||||
|
||||
return TESTING_END();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user